| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
#ifndef DLDAG_H |
| 21 |
#define DLDAG_H |
| 22 |
|
| 23 |
#include <vector> |
| 24 |
#include <cstring> |
| 25 |
|
| 26 |
#include "globaldef.h" |
| 27 |
#include "fpp_assert.h" |
| 28 |
#include "dlVertex.h" |
| 29 |
#include "dlVHash.h" |
| 30 |
#include "ifOptions.h" |
| 31 |
#include "tRole.h" |
| 32 |
#include "ConceptWithDep.h" |
| 33 |
#include "tNECollection.h" |
| 34 |
|
| 35 |
class RoleMaster; |
| 36 |
class TConcept; |
| 37 |
|
| 38 |
|
| 39 |
class DLDag |
| 40 |
{ |
| 41 |
public: |
| 42 |
typedef std::vector<DLVertex*> HeapType; |
| 43 |
typedef std::vector<BipolarPointer> StatVector; |
| 44 |
|
| 45 |
typedef dlVHashTable HashTable; |
| 46 |
|
| 47 |
protected: |
| 48 |
|
| 49 |
HeapType Heap; |
| 50 |
|
| 51 |
StatVector listAnds; |
| 52 |
|
| 53 |
HashTable indexAnd, indexAll, indexLE; |
| 54 |
|
| 55 |
|
| 56 |
size_t finalDagSize = 0; |
| 57 |
|
| 58 |
unsigned int nCacheHits = 0; |
| 59 |
|
| 60 |
#ifdef RKG_USE_SORTED_REASONING |
| 61 |
|
| 62 |
size_t sortArraySize = 0; |
| 63 |
#endif |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
const char* orSortSat = nullptr; |
| 69 |
const char* orSortSub = nullptr; |
| 70 |
|
| 71 |
unsigned int iSort = 0; |
| 72 |
|
| 73 |
bool sortAscend = false; |
| 74 |
|
| 75 |
bool preferNonGen = false; |
| 76 |
|
| 77 |
|
| 78 |
bool useDLVCache = true; |
| 79 |
|
| 80 |
protected: |
| 81 |
|
| 82 |
void readConfig ( const ifOptionSet* Options ); |
| 83 |
|
| 84 |
bool isCorrectOption ( const char* str ) |
| 85 |
{ |
| 86 |
if ( str == nullptr ) |
| 87 |
return false; |
| 88 |
size_t n = strlen(str); |
| 89 |
if ( n < 1 || n > 3 ) |
| 90 |
return false; |
| 91 |
char Method = str[0], |
| 92 |
Order = n >= 2 ? str[1] : 'a', |
| 93 |
NGPref = n == 3 ? str[2] : 'p'; |
| 94 |
return ( Method == 'S' || Method == 'D' || Method == 'F' || |
| 95 |
Method == 'B' || Method == 'G' || Method == '0' ) |
| 96 |
&& ( Order == 'a' || Order == 'd' ) && ( NGPref == 'p' || NGPref == 'n' ); |
| 97 |
} |
| 98 |
|
| 99 |
void computeVertexStat ( BipolarPointer p ); |
| 100 |
|
| 101 |
void computeVertexStat ( BipolarPointer p, bool pos ) { computeVertexStat ( createBiPointer ( p, pos ) ); } |
| 102 |
|
| 103 |
void updateVertexStat ( BipolarPointer p ); |
| 104 |
|
| 105 |
void updateVertexStat ( DLVertex& v, BipolarPointer p, bool pos ) |
| 106 |
{ |
| 107 |
const DLVertex& w = (*this)[p]; |
| 108 |
bool posW = pos == isPositive(p); |
| 109 |
|
| 110 |
|
| 111 |
if ( w.isInCycle(posW) ) |
| 112 |
v.setInCycle(pos); |
| 113 |
|
| 114 |
v.updateStatValues ( w, posW, pos ); |
| 115 |
} |
| 116 |
|
| 117 |
void computeVertexFreq ( BipolarPointer p ); |
| 118 |
|
| 119 |
void computeVertexFreq ( BipolarPointer p, bool pos ) { computeVertexFreq ( createBiPointer ( p, pos ) ); } |
| 120 |
|
| 121 |
void Recompute ( void ) |
| 122 |
{ |
| 123 |
for ( StatVector::const_iterator p = listAnds.begin(), p_end = listAnds.end(); p < p_end; ++p ) |
| 124 |
(*this)[*p].sortEntry(*this); |
| 125 |
} |
| 126 |
|
| 127 |
void setOrderOptions ( const char* opt ); |
| 128 |
|
| 129 |
void clearDFS ( void ) |
| 130 |
{ |
| 131 |
for ( size_t i = Heap.size()-1; i > 0; --i ) |
| 132 |
Heap[i]->clearDFS(); |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
const HashTable& getIndex ( DagTag tag ) const |
| 137 |
{ |
| 138 |
switch(tag) |
| 139 |
{ |
| 140 |
case dtAnd: return indexAnd; |
| 141 |
case dtIrr: |
| 142 |
case dtForall: return indexAll; |
| 143 |
case dtLE: return indexLE; |
| 144 |
default: fpp_unreachable(); |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
void updateIndex ( DagTag tag, BipolarPointer value ) |
| 149 |
{ |
| 150 |
switch(tag) |
| 151 |
{ |
| 152 |
case dtAnd: indexAnd.addElement(value); listAnds.push_back(value); break; |
| 153 |
case dtIrr: |
| 154 |
case dtForall: indexAll.addElement(value); break; |
| 155 |
case dtLE: indexLE.addElement(value); break; |
| 156 |
default: break; |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
#ifdef RKG_USE_SORTED_REASONING |
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
void mergeSorts ( TRole* R ); |
| 165 |
|
| 166 |
void mergeSorts ( DLVertex& v ); |
| 167 |
#endif |
| 168 |
|
| 169 |
#ifdef RKG_PRINT_DAG_USAGE |
| 170 |
|
| 171 |
void PrintDAGUsage ( std::ostream& o ) const; |
| 172 |
#endif |
| 173 |
|
| 174 |
public: |
| 175 |
|
| 176 |
explicit DLDag ( const ifOptionSet* Options ); |
| 177 |
|
| 178 |
DLDag ( const DLDag& ) = delete; |
| 179 |
|
| 180 |
DLDag& operator= ( const DLDag& ) = delete; |
| 181 |
|
| 182 |
~DLDag(); |
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
BipolarPointer add ( DLVertex* v ); |
| 188 |
|
| 189 |
BipolarPointer directAdd ( DLVertex* v ) |
| 190 |
{ |
| 191 |
BipolarPointer toReturn = BipolarPointer(Heap.size()); |
| 192 |
Heap.push_back(v); |
| 193 |
|
| 194 |
return toReturn; |
| 195 |
} |
| 196 |
|
| 197 |
BipolarPointer directAddAndCache ( DLVertex* v ) |
| 198 |
{ |
| 199 |
BipolarPointer ret = directAdd(v); |
| 200 |
if ( useDLVCache ) |
| 201 |
updateIndex ( v->Type(), ret ); |
| 202 |
return ret; |
| 203 |
} |
| 204 |
|
| 205 |
bool isLast ( BipolarPointer p ) const { return (getValue(p) == Heap.size()-1); } |
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
void setExpressionCache ( bool val ) { useDLVCache = val; } |
| 211 |
|
| 212 |
bool less ( BipolarPointer p1, BipolarPointer p2 ) const; |
| 213 |
|
| 214 |
|
| 215 |
DLVertex& operator [] ( BipolarPointer i ) |
| 216 |
{ |
| 217 |
# ifdef ENABLE_CHECKING |
| 218 |
fpp_assert ( isValid(i) ); |
| 219 |
fpp_assert ( getValue(i) < size() ); |
| 220 |
# endif |
| 221 |
return *Heap[getValue(i)]; |
| 222 |
} |
| 223 |
|
| 224 |
DLVertex& operator [] ( const ConceptWDep& cwd ) { return (*this)[cwd.bp()]; } |
| 225 |
|
| 226 |
const DLVertex& operator [] ( BipolarPointer i ) const |
| 227 |
{ |
| 228 |
# ifdef ENABLE_CHECKING |
| 229 |
fpp_assert ( isValid(i) ); |
| 230 |
fpp_assert ( getValue(i) < size() ); |
| 231 |
# endif |
| 232 |
return *Heap[getValue(i)]; |
| 233 |
} |
| 234 |
|
| 235 |
const DLVertex& operator [] ( const ConceptWDep& cwd ) const { return (*this)[cwd.bp()]; } |
| 236 |
|
| 237 |
void replaceVertex ( BipolarPointer i, DLVertex* v, TNamedEntry* C ) |
| 238 |
{ |
| 239 |
delete Heap[getValue(i)]; |
| 240 |
Heap[getValue(i)] = v; |
| 241 |
v->setConcept(C); |
| 242 |
} |
| 243 |
|
| 244 |
|
| 245 |
size_t size ( void ) const { return Heap.size (); } |
| 246 |
|
| 247 |
size_t maxSize ( void ) const { return size() + ( size() < 220 ? 10 : size()/20 ); } |
| 248 |
|
| 249 |
void setFinalSize ( void ) { finalDagSize = size(); setExpressionCache(false); } |
| 250 |
|
| 251 |
void removeQuery ( void ); |
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
void setOrderDefaults ( const char* defSat, const char* defSub ); |
| 257 |
|
| 258 |
void setSubOrder ( void ) { setOrderOptions(orSortSub); } |
| 259 |
|
| 260 |
void setSatOrder ( void ) { setOrderOptions(orSortSat); } |
| 261 |
|
| 262 |
void gatherStatistic ( void ); |
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
const modelCacheInterface* getCache ( BipolarPointer p ) const |
| 268 |
{ return operator[](p).getCache(isPositive(p)); } |
| 269 |
|
| 270 |
void setCache ( BipolarPointer p, const modelCacheInterface* cache ) |
| 271 |
{ operator[](p).setCache ( isPositive(p), cache ); } |
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
#ifdef RKG_USE_SORTED_REASONING |
| 276 |
|
| 277 |
void merge ( mergeableLabel& ml, BipolarPointer p ) |
| 278 |
{ |
| 279 |
if ( p != bpINVALID && p != bpTOP && p != bpBOTTOM ) |
| 280 |
(*this)[p].merge(ml); |
| 281 |
} |
| 282 |
|
| 283 |
void determineSorts ( RoleMaster& ORM, RoleMaster& DRM ); |
| 284 |
|
| 285 |
void updateSorts ( BipolarPointer a, TRole* R, BipolarPointer b ) |
| 286 |
{ |
| 287 |
merge ( R->getDomainLabel(), a ); |
| 288 |
merge ( R->getRangeLabel(), b ); |
| 289 |
} |
| 290 |
|
| 291 |
bool haveSameSort ( BipolarPointer p, BipolarPointer q ) const |
| 292 |
{ |
| 293 |
fpp_assert ( p > 0 && q > 0 ); |
| 294 |
|
| 295 |
|
| 296 |
if ( unlikely(p == 1 || q == 1) ) |
| 297 |
return true; |
| 298 |
|
| 299 |
|
| 300 |
if ( unlikely(getValue(p) >= sortArraySize || getValue(q) >= sortArraySize) ) |
| 301 |
return true; |
| 302 |
|
| 303 |
|
| 304 |
return (*this)[p].getSort() == (*this)[q].getSort(); |
| 305 |
} |
| 306 |
#else |
| 307 |
bool haveSameSort ( unsigned int p, unsigned int q ) { return true; } |
| 308 |
#endif |
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
void PrintStat ( std::ostream& o ) const |
| 314 |
{ |
| 315 |
o << "Heap size = " << Heap.size () << " nodes\n" |
| 316 |
<< "There were " << nCacheHits << " cache hits\n"; |
| 317 |
# ifdef RKG_PRINT_DAG_USAGE |
| 318 |
PrintDAGUsage(o); |
| 319 |
# endif |
| 320 |
} |
| 321 |
|
| 322 |
void Print ( std::ostream& o ) const |
| 323 |
{ |
| 324 |
o << "\nDag structure"; |
| 325 |
|
| 326 |
for ( size_t i = 1; i < size(); ++i ) |
| 327 |
{ |
| 328 |
o << "\n" << i << " "; |
| 329 |
Heap[i]->Print(o); |
| 330 |
} |
| 331 |
|
| 332 |
o << std::endl; |
| 333 |
} |
| 334 |
}; |
| 335 |
|
| 336 |
#include "dlVHashImpl.h" |
| 337 |
|
| 338 |
inline BipolarPointer |
| 339 |
DLDag :: add ( DLVertex* v ) |
| 340 |
{ |
| 341 |
BipolarPointer ret = useDLVCache ? getIndex(v->Type()).locate(*v) : bpINVALID; |
| 342 |
|
| 343 |
if ( !isValid(ret) ) |
| 344 |
return directAddAndCache(v); |
| 345 |
|
| 346 |
|
| 347 |
++nCacheHits; |
| 348 |
delete v; |
| 349 |
return ret; |
| 350 |
} |
| 351 |
|
| 352 |
#endif |