| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
#ifndef TAXONOMYCREATOR_H |
| 21 |
#define TAXONOMYCREATOR_H |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
#include "Taxonomy.h" |
| 26 |
#include "SearchableStack.h" |
| 27 |
#include "tSignature.h" |
| 28 |
|
| 29 |
class TaxonomyCreator |
| 30 |
{ |
| 31 |
protected: |
| 32 |
|
| 33 |
typedef ClassifiableEntry::linkSet SubsumerSet; |
| 34 |
|
| 35 |
typedef SubsumerSet::iterator ss_iterator; |
| 36 |
|
| 37 |
class KnownSubsumers |
| 38 |
{ |
| 39 |
public: |
| 40 |
|
| 41 |
virtual ~KnownSubsumers() = default; |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
virtual ss_iterator s_begin ( void ) = 0; |
| 47 |
|
| 48 |
virtual ss_iterator s_end ( void ) = 0; |
| 49 |
|
| 50 |
virtual ss_iterator p_begin ( void ) = 0; |
| 51 |
|
| 52 |
virtual ss_iterator p_end ( void ) = 0; |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
bool s_empty ( void ) { return s_begin() == s_end(); } |
| 58 |
|
| 59 |
bool p_empty ( void ) { return p_begin() == p_end(); } |
| 60 |
|
| 61 |
virtual bool isPossibleSub ( const ClassifiableEntry* ) const { return true; } |
| 62 |
}; |
| 63 |
|
| 64 |
|
| 65 |
class ToldSubsumers: public KnownSubsumers |
| 66 |
{ |
| 67 |
protected: |
| 68 |
|
| 69 |
ss_iterator beg, end; |
| 70 |
|
| 71 |
public: |
| 72 |
|
| 73 |
ToldSubsumers ( ss_iterator b, ss_iterator e ) : beg(b), end(e) {} |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
ss_iterator s_begin ( void ) override { return beg; } |
| 79 |
|
| 80 |
ss_iterator s_end ( void ) override { return end; } |
| 81 |
|
| 82 |
ss_iterator p_begin ( void ) override { return end; } |
| 83 |
|
| 84 |
ss_iterator p_end ( void ) override { return end; } |
| 85 |
}; |
| 86 |
|
| 87 |
protected: |
| 88 |
|
| 89 |
Taxonomy* pTax; |
| 90 |
|
| 91 |
std::vector<ClassifiableEntry*> Syns; |
| 92 |
|
| 93 |
|
| 94 |
TLabeller valueLabel; |
| 95 |
|
| 96 |
const ClassifiableEntry* curEntry= nullptr; |
| 97 |
|
| 98 |
|
| 99 |
unsigned int nEntries = 0; |
| 100 |
|
| 101 |
unsigned long nCDEntries = 0; |
| 102 |
|
| 103 |
|
| 104 |
bool upDirection = false; |
| 105 |
|
| 106 |
bool useCompletelyDefined = false; |
| 107 |
|
| 108 |
|
| 109 |
SearchableStack <ClassifiableEntry*> waitStack; |
| 110 |
|
| 111 |
SearchableStack <KnownSubsumers*> ksStack; |
| 112 |
|
| 113 |
SearchableStack <const TSignature*> sigStack; |
| 114 |
|
| 115 |
protected: |
| 116 |
|
| 117 |
void setCurrentEntry ( const ClassifiableEntry* p ) |
| 118 |
{ |
| 119 |
pTax->getCurrent()->clear(); |
| 120 |
pTax->getCurrent()->setSample(p); |
| 121 |
curEntry = p; |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
virtual bool classifySynonym ( void ) { return pTax->processSynonym(); } |
| 130 |
|
| 131 |
|
| 132 |
void setToldSubsumers ( void ); |
| 133 |
|
| 134 |
void setNonRedundantCandidates ( void ); |
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
virtual bool immediatelyClassified ( void ) { return classifySynonym(); } |
| 142 |
|
| 143 |
void setupTopDown ( void ) |
| 144 |
{ |
| 145 |
setToldSubsumers(); |
| 146 |
if ( !needTopDown() ) |
| 147 |
{ |
| 148 |
++nCDEntries; |
| 149 |
setNonRedundantCandidates(); |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
virtual bool needTopDown ( void ) const { return false; } |
| 154 |
|
| 155 |
virtual void runTopDown ( void ) {} |
| 156 |
|
| 157 |
virtual bool needBottomUp ( void ) const { return false; } |
| 158 |
|
| 159 |
virtual void runBottomUp ( void ) {} |
| 160 |
|
| 161 |
|
| 162 |
virtual void preClassificationActions ( void ) {} |
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
void performClassification ( void ); |
| 170 |
|
| 171 |
void generalTwoPhaseClassification ( void ); |
| 172 |
|
| 173 |
bool isDirectParent ( TaxonomyVertex* v ) const; |
| 174 |
|
| 175 |
void addPossibleParent ( TaxonomyVertex* parent ) |
| 176 |
{ |
| 177 |
if ( parent && isDirectParent(parent) ) |
| 178 |
pTax->getCurrent()->addNeighbour ( true, parent ); |
| 179 |
} |
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
bool isValued ( TaxonomyVertex* node ) const { return node->isValued(valueLabel); } |
| 187 |
|
| 188 |
bool getValue ( TaxonomyVertex* node ) const { return node->getValue(); } |
| 189 |
|
| 190 |
bool setValue ( TaxonomyVertex* node, bool value ) const { return node->setValued ( value, valueLabel ); } |
| 191 |
|
| 192 |
|
| 193 |
virtual KnownSubsumers* buildKnownSubsumers ( ClassifiableEntry* p ) |
| 194 |
{ return new ToldSubsumers(p->told().begin(), p->told().end()); } |
| 195 |
|
| 196 |
virtual const TSignature* buildSignature ( ClassifiableEntry* ) { return nullptr; } |
| 197 |
|
| 198 |
void addTop ( ClassifiableEntry* p ) |
| 199 |
{ |
| 200 |
waitStack.push(p); |
| 201 |
ksStack.push(buildKnownSubsumers(p)); |
| 202 |
sigStack.push(buildSignature(p)); |
| 203 |
} |
| 204 |
|
| 205 |
void removeTop ( void ) |
| 206 |
{ |
| 207 |
waitStack.pop(); |
| 208 |
delete ksStack.top(); |
| 209 |
ksStack.pop(); |
| 210 |
sigStack.pop(); |
| 211 |
} |
| 212 |
|
| 213 |
ClassifiableEntry* prepareTS ( ClassifiableEntry* cur ); |
| 214 |
|
| 215 |
void classifyTop ( void ) |
| 216 |
{ |
| 217 |
fpp_assert ( !waitStack.empty() ); |
| 218 |
setCurrentEntry(waitStack.top()); |
| 219 |
performClassification(); |
| 220 |
removeTop(); |
| 221 |
} |
| 222 |
|
| 223 |
void propagateTrueUp ( TaxonomyVertex* node ); |
| 224 |
|
| 225 |
void propagateFalseDown ( TaxonomyVertex* node ); |
| 226 |
|
| 227 |
bool setAndPropagate ( TaxonomyVertex* node, bool value ) |
| 228 |
{ |
| 229 |
if ( value ) |
| 230 |
propagateTrueUp(node); |
| 231 |
else |
| 232 |
propagateFalseDown(node); |
| 233 |
return value; |
| 234 |
} |
| 235 |
|
| 236 |
ss_iterator told_begin ( void ) { return ksStack.top()->s_begin(); } |
| 237 |
ss_iterator told_end ( void ) { return ksStack.top()->s_end(); } |
| 238 |
|
| 239 |
|
| 240 |
virtual bool needLogging ( void ) const { return false; } |
| 241 |
|
| 242 |
public: |
| 243 |
|
| 244 |
explicit TaxonomyCreator ( Taxonomy* tax ) : pTax(tax) {} |
| 245 |
|
| 246 |
TaxonomyCreator ( const TaxonomyCreator& ) = delete; |
| 247 |
|
| 248 |
TaxonomyCreator& operator = ( const TaxonomyCreator& ) = delete; |
| 249 |
|
| 250 |
virtual ~TaxonomyCreator() = default; |
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
void classifyEntry ( ClassifiableEntry* p ) |
| 258 |
{ |
| 259 |
fpp_assert ( waitStack.empty() ); |
| 260 |
|
| 261 |
|
| 262 |
if ( p->isNonClassifiable() ) |
| 263 |
return; |
| 264 |
prepareTS(p); |
| 265 |
} |
| 266 |
|
| 267 |
void clearLabels ( void ) { pTax->clearVisited(); valueLabel.newLabel(); } |
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
void setCompletelyDefined ( bool use ) { useCompletelyDefined = use; } |
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
virtual void print ( std::ostream& o ) const; |
| 278 |
}; |
| 279 |
|
| 280 |
#endif |