| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
#ifndef DATAREASONING_H |
| 21 |
#define DATAREASONING_H |
| 22 |
|
| 23 |
|
| 24 |
#include <map> |
| 25 |
#include <vector> |
| 26 |
|
| 27 |
#include "tDataEntry.h" |
| 28 |
#include "DataTypeComparator.h" |
| 29 |
#include "ConceptWithDep.h" |
| 30 |
#include "dlDag.h" |
| 31 |
#include "logging.h" |
| 32 |
|
| 33 |
class DataTypeAppearance |
| 34 |
{ |
| 35 |
protected: |
| 36 |
|
| 37 |
class DepInterval |
| 38 |
{ |
| 39 |
protected: |
| 40 |
|
| 41 |
TDataInterval Constraints; |
| 42 |
|
| 43 |
DepSet locDep; |
| 44 |
public: |
| 45 |
|
| 46 |
const TDataInterval& getDataInterval ( void ) const { return Constraints; } |
| 47 |
|
| 48 |
bool update ( bool min, bool excl, const ComparableDT& value, const DepSet& dep ) |
| 49 |
{ |
| 50 |
if ( !Constraints.update ( min, excl, value ) ) |
| 51 |
return false; |
| 52 |
locDep += dep; |
| 53 |
return true; |
| 54 |
} |
| 55 |
|
| 56 |
bool checkMinMaxClash ( DepSet& dep ) const; |
| 57 |
|
| 58 |
bool consistent ( const ComparableDT& type, DepSet& dep ) const |
| 59 |
{ |
| 60 |
if ( Constraints.consistent(type) ) |
| 61 |
return true; |
| 62 |
dep += locDep; |
| 63 |
return false; |
| 64 |
} |
| 65 |
|
| 66 |
void clear ( void ) { Constraints.clear(); locDep.clear(); } |
| 67 |
}; |
| 68 |
|
| 69 |
|
| 70 |
typedef std::vector<DepInterval> DTConstraint; |
| 71 |
typedef DTConstraint::iterator iterator; |
| 72 |
typedef DTConstraint::const_iterator const_iterator; |
| 73 |
|
| 74 |
public: |
| 75 |
|
| 76 |
DepSet* PType = nullptr; |
| 77 |
|
| 78 |
DepSet* NType = nullptr; |
| 79 |
|
| 80 |
protected: |
| 81 |
|
| 82 |
DTConstraint Constraints; |
| 83 |
|
| 84 |
DepSet accDep; |
| 85 |
|
| 86 |
DepSet& clashDep; |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
bool localMin = false; |
| 92 |
|
| 93 |
bool localExcl = false; |
| 94 |
|
| 95 |
ComparableDT localValue; |
| 96 |
|
| 97 |
DepSet localDep; |
| 98 |
|
| 99 |
protected: |
| 100 |
|
| 101 |
bool reportClash ( const DepSet& dep, const char* reason ) |
| 102 |
{ |
| 103 |
if ( LLM.isWritable(llCDAction) ) |
| 104 |
LL << " DT-" << reason; |
| 105 |
|
| 106 |
clashDep = dep; |
| 107 |
return true; |
| 108 |
} |
| 109 |
|
| 110 |
void setLocal ( bool min, bool excl, const ComparableDT& value, const DepSet& dep ) |
| 111 |
{ |
| 112 |
localMin = min; |
| 113 |
localExcl = excl; |
| 114 |
localValue = value; |
| 115 |
localDep = dep; |
| 116 |
} |
| 117 |
|
| 118 |
bool addUpdatedInterval ( DepInterval i ) |
| 119 |
{ |
| 120 |
if ( !i.consistent ( localValue, localDep ) ) |
| 121 |
return reportClash ( localDep, "C-IT" ); |
| 122 |
if ( !i.update ( localMin, localExcl, localValue, localDep ) ) |
| 123 |
Constraints.push_back(i); |
| 124 |
else if ( !hasPType() || !i.checkMinMaxClash(accDep) ) |
| 125 |
Constraints.push_back(i); |
| 126 |
return false; |
| 127 |
} |
| 128 |
|
| 129 |
bool addIntervals ( iterator begin, iterator end ) |
| 130 |
{ |
| 131 |
for ( ; begin != end; ++begin ) |
| 132 |
if ( addUpdatedInterval(*begin) ) |
| 133 |
return true; |
| 134 |
return false; |
| 135 |
} |
| 136 |
|
| 137 |
bool addPosInterval ( const TDataInterval& Int, const DepSet& dep ); |
| 138 |
|
| 139 |
bool addNegInterval ( const TDataInterval& Int, const DepSet& dep ); |
| 140 |
|
| 141 |
public: |
| 142 |
|
| 143 |
explicit DataTypeAppearance ( DepSet& dep ) : clashDep(dep) {} |
| 144 |
|
| 145 |
~DataTypeAppearance() { delete PType; delete NType; } |
| 146 |
|
| 147 |
|
| 148 |
void clear ( void ) |
| 149 |
{ |
| 150 |
delete PType; |
| 151 |
PType = nullptr; |
| 152 |
delete NType; |
| 153 |
NType = nullptr; |
| 154 |
Constraints.clear(); |
| 155 |
Constraints.emplace_back(); |
| 156 |
accDep.clear(); |
| 157 |
} |
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
bool hasPType ( void ) const { return PType != nullptr; } |
| 163 |
|
| 164 |
bool setTypePresence ( bool pos, const DepSet& dep ) |
| 165 |
{ |
| 166 |
DepSet*& pDep = pos ? PType : NType; |
| 167 |
if ( likely(pDep == nullptr) ) |
| 168 |
pDep = new DepSet(dep); |
| 169 |
else |
| 170 |
pDep->add(dep); |
| 171 |
|
| 172 |
|
| 173 |
if ( PType != nullptr && NType != nullptr ) |
| 174 |
return reportClash ( *PType+*NType, "TNT" ); |
| 175 |
return false; |
| 176 |
} |
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
bool operator == ( const DataTypeAppearance& other ) const; |
| 182 |
|
| 183 |
bool operator < ( const DataTypeAppearance& other ) const; |
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
bool addInterval ( bool pos, const TDataInterval& Int, const DepSet& dep ) |
| 189 |
{ |
| 190 |
if ( LLM.isWritable(llCDAction) ) |
| 191 |
LL << ' ' << (pos ? '+' : '-') << Int; |
| 192 |
return pos ? addPosInterval ( Int, dep ) : addNegInterval ( Int, dep ); |
| 193 |
} |
| 194 |
}; |
| 195 |
|
| 196 |
class DataTypeReasoner |
| 197 |
{ |
| 198 |
protected: |
| 199 |
|
| 200 |
typedef std::vector<DataTypeAppearance*> DTAVector; |
| 201 |
|
| 202 |
typedef std::map<const TDataEntry*,size_t> TypeMap; |
| 203 |
|
| 204 |
protected: |
| 205 |
|
| 206 |
DTAVector Types; |
| 207 |
|
| 208 |
TypeMap Map; |
| 209 |
|
| 210 |
const DLDag& DLHeap; |
| 211 |
|
| 212 |
DataTypeAppearance* posType = nullptr; |
| 213 |
|
| 214 |
DepSet clashDep; |
| 215 |
|
| 216 |
protected: |
| 217 |
|
| 218 |
bool processDataValue ( bool pos, const TDataEntry* c, const DepSet& dep ) |
| 219 |
{ |
| 220 |
DataTypeAppearance* type = getDTAbyValue(c); |
| 221 |
|
| 222 |
if ( pos && setTypePresence ( type, pos, dep ) ) |
| 223 |
return true; |
| 224 |
|
| 225 |
|
| 226 |
TDataInterval constraints; |
| 227 |
constraints.updateMin ( false, c->getComp() ); |
| 228 |
constraints.updateMax ( false, c->getComp() ); |
| 229 |
return type->addInterval ( pos, constraints, dep ); |
| 230 |
} |
| 231 |
|
| 232 |
bool processDataExpr ( bool pos, const TDataEntry* c, const DepSet& dep ) |
| 233 |
{ |
| 234 |
const TDataInterval& constraints = *c->getFacet(); |
| 235 |
if ( constraints.empty() ) |
| 236 |
return false; |
| 237 |
DataTypeAppearance* type = getDTAbyValue(c); |
| 238 |
if ( pos && setTypePresence ( type, pos, dep ) ) |
| 239 |
return true; |
| 240 |
return type->addInterval ( pos, constraints, dep ); |
| 241 |
} |
| 242 |
|
| 243 |
|
| 244 |
const TDataEntry* getDataEntry ( BipolarPointer p ) const |
| 245 |
{ return static_cast<const TDataEntry*>(DLHeap[p].getConcept()); } |
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
DataTypeAppearance* getDTAbyType ( const TDataEntry* dataType ) |
| 251 |
{ |
| 252 |
# ifdef ENABLE_CHECKING |
| 253 |
fpp_assert ( Map.find(dataType) != Map.end() ); |
| 254 |
# endif |
| 255 |
return Types[Map[dataType]]; |
| 256 |
} |
| 257 |
|
| 258 |
DataTypeAppearance* getDTAbyValue ( const TDataEntry* dataValue ) |
| 259 |
{ |
| 260 |
# ifdef ENABLE_CHECKING |
| 261 |
fpp_assert ( !dataValue->isBasicDataType() ); |
| 262 |
# endif |
| 263 |
return getDTAbyType(dataValue->getType()); |
| 264 |
} |
| 265 |
|
| 266 |
bool setTypePresence ( DataTypeAppearance* type, bool pos, const DepSet& dep ) |
| 267 |
{ |
| 268 |
|
| 269 |
if ( !pos ) |
| 270 |
return type->setTypePresence ( false, dep ); |
| 271 |
|
| 272 |
|
| 273 |
if ( posType == nullptr ) |
| 274 |
posType = type; |
| 275 |
|
| 276 |
if ( posType == type ) |
| 277 |
return type->setTypePresence ( true, dep ); |
| 278 |
|
| 279 |
if ( LLM.isWritable(llCDAction) ) |
| 280 |
LL << " DT-TT"; |
| 281 |
|
| 282 |
clashDep = *(posType->PType); |
| 283 |
clashDep += dep; |
| 284 |
return true; |
| 285 |
} |
| 286 |
|
| 287 |
public: |
| 288 |
|
| 289 |
explicit DataTypeReasoner ( const DLDag& dag ) : DLHeap(dag) {} |
| 290 |
|
| 291 |
~DataTypeReasoner() |
| 292 |
{ |
| 293 |
for ( DTAVector::iterator p = Types.begin(); p < Types.end(); ++p ) |
| 294 |
delete *p; |
| 295 |
} |
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
void registerDataType ( const TDataEntry* p ) |
| 301 |
{ |
| 302 |
Map[p] = Types.size(); |
| 303 |
Types.push_back(new DataTypeAppearance(clashDep)); |
| 304 |
} |
| 305 |
|
| 306 |
void clear ( void ) |
| 307 |
{ |
| 308 |
for ( DTAVector::iterator p = Types.begin(), p_end = Types.end(); p < p_end; ++p ) |
| 309 |
(*p)->clear(); |
| 310 |
posType = nullptr; |
| 311 |
} |
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
bool operator == ( const DataTypeReasoner& other ) const |
| 317 |
{ |
| 318 |
if ( posType == nullptr || other.posType == nullptr ) |
| 319 |
return false; |
| 320 |
return *posType == *(other.posType); |
| 321 |
} |
| 322 |
|
| 323 |
bool operator < ( const DataTypeReasoner& other ) const |
| 324 |
{ |
| 325 |
if ( posType == nullptr || other.posType == nullptr ) |
| 326 |
return false; |
| 327 |
return *posType < *(other.posType); |
| 328 |
} |
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
bool addDataEntry ( BipolarPointer p, const DepSet& dep ); |
| 334 |
|
| 335 |
const DepSet& getClashSet ( void ) const { return clashDep; } |
| 336 |
}; |
| 337 |
|
| 338 |
|
| 339 |
#endif |