| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
#ifndef REASONER_H |
| 21 |
#define REASONER_H |
| 22 |
|
| 23 |
#include "globaldef.h" |
| 24 |
#include "tBranchingContext.h" |
| 25 |
#include "dlCompletionGraph.h" |
| 26 |
#include "dlTBox.h" |
| 27 |
#include "dlDag.h" |
| 28 |
#include "modelCacheIan.h" |
| 29 |
#include "tSaveStack.h" |
| 30 |
#include "procTimer.h" |
| 31 |
#include "DataReasoning.h" |
| 32 |
#include "ToDoList.h" |
| 33 |
#include "tFastSet.h" |
| 34 |
|
| 35 |
#if USE_LOGGING |
| 36 |
# define USE_REASONING_STATISTICS |
| 37 |
#endif |
| 38 |
|
| 39 |
#ifdef USE_REASONING_STATISTICS |
| 40 |
|
| 41 |
class AccumulatedStatistic |
| 42 |
{ |
| 43 |
protected: |
| 44 |
|
| 45 |
static AccumulatedStatistic* root; |
| 46 |
|
| 47 |
public: |
| 48 |
|
| 49 |
static void accumulateAll ( void ) |
| 50 |
{ |
| 51 |
for ( AccumulatedStatistic* cur = root; cur; cur = cur->next ) |
| 52 |
cur->accumulate(); |
| 53 |
} |
| 54 |
|
| 55 |
protected: |
| 56 |
|
| 57 |
unsigned int total = 0; |
| 58 |
|
| 59 |
unsigned int local = 0; |
| 60 |
|
| 61 |
AccumulatedStatistic* next; |
| 62 |
|
| 63 |
public: |
| 64 |
|
| 65 |
AccumulatedStatistic ( void ) : next(root) { root = this; } |
| 66 |
|
| 67 |
AccumulatedStatistic ( const AccumulatedStatistic& ) = delete; |
| 68 |
|
| 69 |
AccumulatedStatistic& operator = ( const AccumulatedStatistic& ) = delete; |
| 70 |
|
| 71 |
~AccumulatedStatistic() |
| 72 |
{ |
| 73 |
|
| 74 |
if ( root == this ) |
| 75 |
{ |
| 76 |
root = next; |
| 77 |
return; |
| 78 |
} |
| 79 |
|
| 80 |
AccumulatedStatistic *prev = root, *cur = root->next; |
| 81 |
|
| 82 |
while ( cur && cur != this ) |
| 83 |
prev = cur, cur = cur->next; |
| 84 |
if ( cur == this ) |
| 85 |
prev->next = next; |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
unsigned int get ( bool needLocal ) const { return needLocal ? local : total; } |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
void inc ( void ) |
| 97 |
{ |
| 98 |
++local; |
| 99 |
} |
| 100 |
|
| 101 |
void set ( unsigned int n ) |
| 102 |
{ |
| 103 |
local = n; |
| 104 |
} |
| 105 |
|
| 106 |
void accumulate ( void ) { total += local; local = 0; } |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
void Print ( std::ostream& o, bool needLocal, const char* prefix, const char* suffix ) const |
| 111 |
{ |
| 112 |
if ( get(needLocal) > 0 ) |
| 113 |
o << prefix << get(needLocal) << suffix; |
| 114 |
} |
| 115 |
}; |
| 116 |
#endif |
| 117 |
|
| 118 |
class DlSatTester |
| 119 |
{ |
| 120 |
protected: |
| 121 |
|
| 122 |
typedef ToDoList ToDoTableType; |
| 123 |
|
| 124 |
typedef std::vector<DlCompletionTreeArc*> EdgeVector; |
| 125 |
|
| 126 |
typedef std::vector<DlCompletionTree*> NodeVector; |
| 127 |
|
| 128 |
typedef DlCompletionTree::const_label_iterator const_label_iterator; |
| 129 |
|
| 130 |
typedef std::set<BipolarPointer> BPSet; |
| 131 |
|
| 132 |
protected: |
| 133 |
|
| 134 |
const unsigned int redoForall = 1; |
| 135 |
const unsigned int redoFunc = 2; |
| 136 |
const unsigned int redoAtMost = 4; |
| 137 |
const unsigned int redoIrr = 8; |
| 138 |
|
| 139 |
const unsigned int redoEverything = redoForall | redoFunc | redoAtMost | redoIrr; |
| 140 |
|
| 141 |
protected: |
| 142 |
|
| 143 |
class BCStack: public TSaveStack<BranchingContext> |
| 144 |
{ |
| 145 |
protected: |
| 146 |
|
| 147 |
DeletelessAllocator<BCOr> PoolOr; |
| 148 |
|
| 149 |
DeletelessAllocator<BCNN> PoolNN; |
| 150 |
|
| 151 |
DeletelessAllocator<BCLE<DlCompletionTreeArc> > PoolLE; |
| 152 |
|
| 153 |
DeletelessAllocator<BCLE<DlCompletionTree> > PoolTopLE; |
| 154 |
|
| 155 |
DeletelessAllocator<BCChoose> PoolCh; |
| 156 |
|
| 157 |
BCBarrier* bcBarrier; |
| 158 |
|
| 159 |
protected: |
| 160 |
|
| 161 |
BranchingContext* createNew ( void ) override { return nullptr; } |
| 162 |
|
| 163 |
BranchingContext* push ( BranchingContext* p ) |
| 164 |
{ |
| 165 |
p->init(); |
| 166 |
TSaveStack<BranchingContext>::push(); |
| 167 |
this->Base[this->last-1] = p; |
| 168 |
return p; |
| 169 |
} |
| 170 |
|
| 171 |
public: |
| 172 |
|
| 173 |
BCStack ( void ) : bcBarrier(new BCBarrier) {} |
| 174 |
|
| 175 |
~BCStack() override |
| 176 |
{ |
| 177 |
|
| 178 |
for ( iterator p = this->Base.begin(), p_end = this->Base.end(); p < p_end; ++p ) |
| 179 |
*p = nullptr; |
| 180 |
delete bcBarrier; |
| 181 |
} |
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
BranchingContext* pushOr ( void ) { return push(PoolOr.get()); } |
| 187 |
|
| 188 |
BranchingContext* pushNN ( void ) { return push(PoolNN.get()); } |
| 189 |
|
| 190 |
BranchingContext* pushLE ( void ) { return push(PoolLE.get()); } |
| 191 |
|
| 192 |
BranchingContext* pushTopLE ( void ) { return push(PoolTopLE.get()); } |
| 193 |
|
| 194 |
BranchingContext* pushCh ( void ) { return push(PoolCh.get()); } |
| 195 |
|
| 196 |
BranchingContext* pushBarrier ( void ) { return push(bcBarrier); } |
| 197 |
|
| 198 |
|
| 199 |
void clearPools ( void ) |
| 200 |
{ |
| 201 |
PoolOr.clear(); |
| 202 |
PoolNN.clear(); |
| 203 |
PoolLE.clear(); |
| 204 |
PoolTopLE.clear(); |
| 205 |
PoolCh.clear(); |
| 206 |
} |
| 207 |
|
| 208 |
void clear ( void ) override |
| 209 |
{ |
| 210 |
clearPools(); |
| 211 |
TSaveStack<BranchingContext>::clear(); |
| 212 |
} |
| 213 |
}; |
| 214 |
|
| 215 |
protected: |
| 216 |
|
| 217 |
TBox& tBox; |
| 218 |
|
| 219 |
DLDag& DLHeap; |
| 220 |
|
| 221 |
RoleMaster::TRoleVec ReflexiveRoles; |
| 222 |
|
| 223 |
|
| 224 |
TDepSetManager Manager; |
| 225 |
|
| 226 |
DlCompletionGraph CGraph; |
| 227 |
|
| 228 |
ToDoTableType TODO; |
| 229 |
|
| 230 |
DataTypeReasoner DTReasoner; |
| 231 |
|
| 232 |
TFastSet<unsigned int> pUsed, nUsed; |
| 233 |
|
| 234 |
modelCacheIan newNodeCache; |
| 235 |
|
| 236 |
modelCacheIan newNodeEdges; |
| 237 |
|
| 238 |
|
| 239 |
const TKBFlags& GCIs; |
| 240 |
|
| 241 |
|
| 242 |
std::set<BipolarPointer> inProcess; |
| 243 |
|
| 244 |
|
| 245 |
TsProcTimer satTimer; |
| 246 |
|
| 247 |
TsProcTimer subTimer; |
| 248 |
|
| 249 |
TsProcTimer testTimer; |
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
BCStack Stack; |
| 255 |
|
| 256 |
BranchingContext* bContext = nullptr; |
| 257 |
|
| 258 |
unsigned int tryLevel = InitBranchingLevelValue; |
| 259 |
|
| 260 |
unsigned int nonDetShift = 0; |
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
#ifdef USE_REASONING_STATISTICS |
| 265 |
AccumulatedStatistic |
| 266 |
nTacticCalls, |
| 267 |
nUseless, |
| 268 |
|
| 269 |
nIdCalls, |
| 270 |
nSingletonCalls, |
| 271 |
nOrCalls, |
| 272 |
nOrBrCalls, |
| 273 |
nAndCalls, |
| 274 |
nSomeCalls, |
| 275 |
nAllCalls, |
| 276 |
nFuncCalls, |
| 277 |
nLeCalls, |
| 278 |
nGeCalls, |
| 279 |
|
| 280 |
nNNCalls, |
| 281 |
nMergeCalls, |
| 282 |
|
| 283 |
nAutoEmptyLookups, |
| 284 |
nAutoTransLookups, |
| 285 |
|
| 286 |
nSRuleAdd, |
| 287 |
nSRuleFire, |
| 288 |
|
| 289 |
nStateSaves, |
| 290 |
nStateRestores, |
| 291 |
nNodeSaves, |
| 292 |
nNodeRestores, |
| 293 |
|
| 294 |
nLookups, |
| 295 |
|
| 296 |
nFairnessViolations, |
| 297 |
|
| 298 |
|
| 299 |
nCacheTry, |
| 300 |
nCacheFailedNoCache, |
| 301 |
nCacheFailedShallow, |
| 302 |
nCacheFailed, |
| 303 |
nCachedSat, |
| 304 |
nCachedUnsat; |
| 305 |
#endif |
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
DlCompletionTree* curNode = nullptr; |
| 311 |
|
| 312 |
ConceptWDep curConcept; |
| 313 |
|
| 314 |
std::vector<BipolarPointer> SessionGCIs; |
| 315 |
|
| 316 |
|
| 317 |
size_t dagSize = 0; |
| 318 |
|
| 319 |
|
| 320 |
BCOr::OrIndex OrConceptsToTest; |
| 321 |
|
| 322 |
EdgeVector EdgesToMerge; |
| 323 |
|
| 324 |
NodeVector NodesToMerge; |
| 325 |
|
| 326 |
DepSet clashSet; |
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
bool encounterNominal = false; |
| 332 |
|
| 333 |
bool checkDataNode = true; |
| 334 |
|
| 335 |
protected: |
| 336 |
|
| 337 |
|
| 338 |
#ifdef USE_REASONING_STATISTICS |
| 339 |
# define incStat(stat) stat.inc() |
| 340 |
#else |
| 341 |
# define incStat(stat) |
| 342 |
#endif |
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
unsigned long getSatTimeout ( void ) const { return tBox.testTimeout; } |
| 350 |
|
| 351 |
bool useSemanticBranching ( void ) const { return tBox.useSemanticBranching; } |
| 352 |
|
| 353 |
bool useLazyBlocking ( void ) const { return tBox.useLazyBlocking; } |
| 354 |
|
| 355 |
|
| 356 |
void resetSessionFlags ( void ); |
| 357 |
|
| 358 |
|
| 359 |
bool addToDoEntry ( DlCompletionTree* node, const ConceptWDep& C, const char* reason = nullptr ); |
| 360 |
|
| 361 |
bool addToDoEntry ( DlCompletionTree* node, BipolarPointer c, const DepSet& dep, const char* reason = nullptr ) |
| 362 |
{ return addToDoEntry ( node, ConceptWDep(c,dep), reason ); } |
| 363 |
|
| 364 |
bool insertToDoEntry ( DlCompletionTree* node, const ConceptWDep& C, DagTag tag, const char* reason ); |
| 365 |
|
| 366 |
bool correctCachedEntry ( DlCompletionTree* n ); |
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
bool isNodeGloballyUsed ( const DlCompletionTree* node ) const |
| 372 |
{ return ! ( node->isDataNode() || node->isIBlocked() || node->isPBlocked() ); } |
| 373 |
|
| 374 |
bool isObjectNodeUnblocked ( const DlCompletionTree* node ) const |
| 375 |
{ return isNodeGloballyUsed(node) && !node->isDBlocked(); } |
| 376 |
|
| 377 |
bool addSessionGCI ( BipolarPointer C, const DepSet& dep ); |
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
void updateName ( DlCompletionTree* node, BipolarPointer bp ) |
| 382 |
{ |
| 383 |
const CGLabel& lab = node->label(); |
| 384 |
CGLabel::const_iterator p; |
| 385 |
for ( p = lab.begin_sc(); p != lab.end_sc(); ++p ) |
| 386 |
if ( unlikely ( p->bp() == bp || p->bp() == inverse(bp) ) ) |
| 387 |
{ |
| 388 |
addExistingToDoEntry ( node, lab.getSCOffset(p), "sp" ); |
| 389 |
break; |
| 390 |
} |
| 391 |
} |
| 392 |
|
| 393 |
void updateName ( BipolarPointer bp ) |
| 394 |
{ |
| 395 |
for ( DlCompletionTree* node : CGraph ) |
| 396 |
if ( isNodeGloballyUsed(node) ) |
| 397 |
updateName ( node, bp ); |
| 398 |
} |
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
bool findConcept ( const CWDArray& lab, BipolarPointer bp ); |
| 404 |
|
| 405 |
bool findConcept ( const CWDArray& lab, const ConceptWDep& C ) { return findConcept ( lab, C.bp() ); } |
| 406 |
|
| 407 |
bool findConceptClash ( const CWDArray& lab, BipolarPointer bp, const DepSet& dep ); |
| 408 |
|
| 409 |
bool findConceptClash ( const CWDArray& lab, const ConceptWDep& C ) { return findConceptClash ( lab, C.bp(), C.getDep() ); } |
| 410 |
|
| 411 |
addConceptResult checkAddedConcept ( const CWDArray& lab, BipolarPointer bp, const DepSet& dep ); |
| 412 |
|
| 413 |
addConceptResult tryAddConcept ( const CWDArray& lab, BipolarPointer bp, const DepSet& dep ); |
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
void addExistingToDoEntry ( DlCompletionTree* node, int offset, const char* reason = nullptr ) |
| 420 |
{ |
| 421 |
const ConceptWDep& C = node->label().getConcept(offset); |
| 422 |
auto bp = C.bp(); |
| 423 |
TODO.addEntry ( node, DLHeap[bp].Type(), bp, offset ); |
| 424 |
if ( LLM.isWritable(llGTA) ) |
| 425 |
logEntry ( node, C, reason ); |
| 426 |
} |
| 427 |
|
| 428 |
void redoNodeLabel ( DlCompletionTree* node, const char* reason ) |
| 429 |
{ |
| 430 |
const CGLabel& lab = node->label(); |
| 431 |
CGLabel::const_iterator p { }; |
| 432 |
for ( p = lab.begin_sc(); p != lab.end_sc(); ++p ) |
| 433 |
addExistingToDoEntry ( node, lab.getSCOffset(p), reason ); |
| 434 |
for ( p = lab.begin_cc(); p != lab.end_cc(); ++p ) |
| 435 |
addExistingToDoEntry ( node, lab.getCCOffset(p), reason ); |
| 436 |
} |
| 437 |
|
| 438 |
|
| 439 |
bool checkSatisfiability ( void ); |
| 440 |
|
| 441 |
bool performAfterReasoning ( void ); |
| 442 |
|
| 443 |
void ensureDAGSize ( void ) |
| 444 |
{ |
| 445 |
if ( unlikely ( dagSize < DLHeap.size() ) ) |
| 446 |
{ |
| 447 |
dagSize = DLHeap.maxSize(); |
| 448 |
pUsed.ensureMaxSetSize(dagSize); |
| 449 |
nUsed.ensureMaxSetSize(dagSize); |
| 450 |
} |
| 451 |
} |
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
void prepareCascadedCache ( BipolarPointer p ); |
| 459 |
|
| 460 |
modelCacheInterface* buildCache ( BipolarPointer p ); |
| 461 |
|
| 462 |
modelCacheInterface* createModelCache ( const DlCompletionTree* p ) const |
| 463 |
{ return new modelCacheIan ( DLHeap, p, encounterNominal, tBox.nC, tBox.nR ); } |
| 464 |
|
| 465 |
|
| 466 |
bool canBeCached ( DlCompletionTree* node ); |
| 467 |
|
| 468 |
void doCacheNode ( DlCompletionTree* node ); |
| 469 |
|
| 470 |
modelCacheState reportNodeCached ( DlCompletionTree* node ); |
| 471 |
|
| 472 |
modelCacheState tryCacheNode ( DlCompletionTree* node ) |
| 473 |
{ |
| 474 |
modelCacheState ret = canBeCached(node) ? reportNodeCached(node) : csFailed; |
| 475 |
|
| 476 |
CGraph.saveRareCond(node->setCached(ret == csValid)); |
| 477 |
return ret; |
| 478 |
} |
| 479 |
|
| 480 |
static bool usageByState ( modelCacheState status ) |
| 481 |
{ |
| 482 |
return status == csInvalid; |
| 483 |
} |
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
virtual bool hasNominals ( void ) const { return false; } |
| 491 |
|
| 492 |
|
| 493 |
bool runSat ( void ); |
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
bool commonTactic ( void ); |
| 508 |
|
| 509 |
bool commonTacticBody ( const DLVertex& cur ); |
| 510 |
|
| 511 |
bool commonTacticBodyId ( const DLVertex& cur ); |
| 512 |
|
| 513 |
bool commonTacticBodySingleton ( const DLVertex& cur ); |
| 514 |
|
| 515 |
bool commonTacticBodyAnd ( const DLVertex& cur ); |
| 516 |
|
| 517 |
bool commonTacticBodyOr ( const DLVertex& cur ); |
| 518 |
|
| 519 |
bool commonTacticBodySome ( const DLVertex& cur ); |
| 520 |
|
| 521 |
bool commonTacticBodyValue ( const TRole* R, const TIndividual* nom ); |
| 522 |
|
| 523 |
bool commonTacticBodySomeUniv ( const DLVertex& cur ); |
| 524 |
|
| 525 |
bool commonTacticBodyAll ( const DLVertex& cur ); |
| 526 |
|
| 527 |
bool commonTacticBodyAllSimple ( const DLVertex& cur ); |
| 528 |
|
| 529 |
bool commonTacticBodyAllComplex ( const DLVertex& cur ); |
| 530 |
|
| 531 |
bool commonTacticBodySomeSelf ( const TRole* R ); |
| 532 |
|
| 533 |
bool commonTacticBodyIrrefl ( const TRole* R ); |
| 534 |
|
| 535 |
bool commonTacticBodyGE ( const DLVertex& cur ); |
| 536 |
|
| 537 |
bool commonTacticBodyLE ( const DLVertex& cur ); |
| 538 |
|
| 539 |
bool commonTacticBodyChoose ( const TRole* R, BipolarPointer C ); |
| 540 |
|
| 541 |
bool commonTacticBodyFunc ( const DLVertex& cur ); |
| 542 |
|
| 543 |
bool commonTacticBodyNN ( const DLVertex& cur ); |
| 544 |
|
| 545 |
bool commonTacticBodyProj ( const TRole* R, BipolarPointer C, const TRole* ProjR ); |
| 546 |
|
| 547 |
|
| 548 |
bool processTopRoleFunc ( const DLVertex& cur ); |
| 549 |
|
| 550 |
bool processTopRoleLE ( const DLVertex& cur ); |
| 551 |
|
| 552 |
bool processTopRoleGE ( const DLVertex& cur ); |
| 553 |
|
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
bool isIBlocked ( void ) const { return curNode->isIBlocked(); } |
| 558 |
|
| 559 |
bool isNNApplicable ( const TRole* r, BipolarPointer C, BipolarPointer stopper ) const; |
| 560 |
|
| 561 |
bool applyExtraRules ( const TConcept* p ); |
| 562 |
|
| 563 |
inline |
| 564 |
bool applyExtraRulesIf ( const TConcept* p ) |
| 565 |
{ |
| 566 |
if ( !p->hasExtraRules() ) |
| 567 |
return false; |
| 568 |
fpp_assert ( p->isPrimitive() ); |
| 569 |
return applyExtraRules(p); |
| 570 |
} |
| 571 |
|
| 572 |
|
| 573 |
|
| 574 |
|
| 575 |
bool applyChooseRule ( DlCompletionTree* node, BipolarPointer C ); |
| 576 |
|
| 577 |
|
| 578 |
|
| 579 |
|
| 580 |
bool isCurNodeBlocked ( void ); |
| 581 |
|
| 582 |
void applyAllGeneratingRules ( DlCompletionTree* node ); |
| 583 |
|
| 584 |
bool initNewNode ( DlCompletionTree* node, const DepSet& dep, BipolarPointer C ); |
| 585 |
|
| 586 |
bool applyReflexiveRoles ( DlCompletionTree* node, const DepSet& dep ); |
| 587 |
|
| 588 |
bool initHeadOfNewEdge ( DlCompletionTree* node, const TRole* R, const DepSet& dep, const char* reason ); |
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
bool isSomeExists ( const TRole* R, BipolarPointer C ) const |
| 594 |
{ |
| 595 |
if ( !isUsed(C) ) |
| 596 |
return false; |
| 597 |
const DlCompletionTree* where = curNode->isSomeApplicable ( R, C ); |
| 598 |
if ( where != nullptr ) |
| 599 |
if ( LLM.isWritable(llGTA) ) |
| 600 |
LL << " E(" << R->getName() << "," << where->getId() << "," << C << ")"; |
| 601 |
return where != nullptr; |
| 602 |
} |
| 603 |
|
| 604 |
bool setupEdge ( DlCompletionTreeArc* pA, const DepSet& curDep, unsigned int redoFlags = 0 ); |
| 605 |
|
| 606 |
bool createNewEdge ( const TRole* Role, BipolarPointer Concept, unsigned int redoFlags ); |
| 607 |
|
| 608 |
DlCompletionTreeArc* createOneNeighbour ( const TRole* Role, const DepSet& dep, |
| 609 |
CTNominalLevel level = BlockableLevel ); |
| 610 |
|
| 611 |
|
| 612 |
|
| 613 |
|
| 614 |
|
| 615 |
|
| 616 |
|
| 617 |
bool applyUniversalNR ( DlCompletionTree* Node, const DlCompletionTreeArc* arcSample, |
| 618 |
const DepSet& dep, unsigned int redoFlags ); |
| 619 |
|
| 620 |
|
| 621 |
|
| 622 |
|
| 623 |
bool isFirstBranchCall ( void ) const { return bContext == nullptr; } |
| 624 |
|
| 625 |
void initBC ( void ) |
| 626 |
{ |
| 627 |
|
| 628 |
bContext->curNode = curNode; |
| 629 |
bContext->curConcept = curConcept; |
| 630 |
bContext->branchDep = curConcept.getDep(); |
| 631 |
bContext->pUsedIndex = pUsed.size(); |
| 632 |
bContext->nUsedIndex = nUsed.size(); |
| 633 |
bContext->SGsize = SessionGCIs.size(); |
| 634 |
} |
| 635 |
|
| 636 |
void clearBC ( void ) { bContext = nullptr; } |
| 637 |
|
| 638 |
|
| 639 |
void createBCOr ( void ) { bContext = Stack.pushOr(); initBC(); } |
| 640 |
|
| 641 |
void createBCNN ( void ) { bContext = Stack.pushNN(); initBC(); } |
| 642 |
|
| 643 |
void createBCLE ( void ) { bContext = Stack.pushLE(); initBC(); } |
| 644 |
|
| 645 |
void createBCTopLE ( void ) { bContext = Stack.pushTopLE(); initBC(); } |
| 646 |
|
| 647 |
void createBCCh ( void ) { bContext = Stack.pushCh(); initBC(); } |
| 648 |
|
| 649 |
void createBCBarrier ( void ) { bContext = Stack.pushBarrier(); initBC(); } |
| 650 |
|
| 651 |
|
| 652 |
|
| 653 |
|
| 654 |
bool planOrProcessing ( const DLVertex& cur, DepSet& dep ); |
| 655 |
|
| 656 |
bool processOrEntry ( void ); |
| 657 |
|
| 658 |
|
| 659 |
|
| 660 |
|
| 661 |
bool createDifferentNeighbours ( const TRole* R, BipolarPointer C, const DepSet& dep, |
| 662 |
unsigned int n, CTNominalLevel level ); |
| 663 |
|
| 664 |
|
| 665 |
static bool isFunctionalVertex ( const DLVertex& v ) { return ( v.Type() == dtLE && v.getNumberLE() == 1 && v.getC() == bpTOP ); } |
| 666 |
|
| 667 |
bool checkNRclash ( const DLVertex& atleast, const DLVertex& atmost ) const |
| 668 |
{ |
| 669 |
return (atmost.getC() == bpTOP || atleast.getC() == atmost.getC()) && |
| 670 |
atleast.getNumberGE() > atmost.getNumberLE() && |
| 671 |
*atleast.getRole() <= *atmost.getRole(); |
| 672 |
} |
| 673 |
|
| 674 |
bool isNRClash ( const DLVertex& atleast, const DLVertex& atmost, const ConceptWDep& reason ); |
| 675 |
|
| 676 |
bool isQuickClashLE ( const DLVertex& atmost ) |
| 677 |
{ |
| 678 |
for ( DlCompletionTree::const_label_iterator q = curNode->beginl_cc(), q_end = curNode->endl_cc(); q < q_end; ++q ) |
| 679 |
if ( isNegative(q->bp()) |
| 680 |
&& isNRClash ( DLHeap[*q], atmost, *q ) ) |
| 681 |
return true; |
| 682 |
return false; |
| 683 |
} |
| 684 |
|
| 685 |
bool isQuickClashGE ( const DLVertex& atleast ) |
| 686 |
{ |
| 687 |
for ( DlCompletionTree::const_label_iterator q = curNode->beginl_cc(), q_end = curNode->endl_cc(); q < q_end; ++q ) |
| 688 |
if ( isPositive(q->bp()) |
| 689 |
&& isNRClash ( atleast, DLHeap[*q], *q ) ) |
| 690 |
return true; |
| 691 |
return false; |
| 692 |
} |
| 693 |
|
| 694 |
bool initLEProcessing ( const DLVertex& cur ); |
| 695 |
|
| 696 |
bool initTopLEProcessing ( const DLVertex& cur ); |
| 697 |
|
| 698 |
|
| 699 |
bool findChooseRuleConcept ( const CWDArray& label, BipolarPointer C, DepSet& Dep ) |
| 700 |
{ |
| 701 |
if ( C == bpTOP ) |
| 702 |
return true; |
| 703 |
if ( findConceptClash ( label, C, Dep ) ) |
| 704 |
{ |
| 705 |
Dep.add(getClashSet()); |
| 706 |
return true; |
| 707 |
} |
| 708 |
else if ( findConceptClash ( label, inverse(C), Dep ) ) |
| 709 |
{ |
| 710 |
Dep.add(getClashSet()); |
| 711 |
return false; |
| 712 |
} |
| 713 |
else |
| 714 |
fpp_unreachable(); |
| 715 |
} |
| 716 |
|
| 717 |
bool applyChooseRuleGlobally ( BipolarPointer C ); |
| 718 |
|
| 719 |
void findNeighbours ( const TRole* Role, BipolarPointer C, DepSet& Dep ); |
| 720 |
|
| 721 |
void findCLabelledNodes ( BipolarPointer C, DepSet& Dep ); |
| 722 |
|
| 723 |
bool checkMergeClash ( const CGLabel& from, const CGLabel& to, const DepSet& dep, unsigned int nodeId ); |
| 724 |
|
| 725 |
bool mergeLabels ( const CGLabel& from, DlCompletionTree* to, const DepSet& dep ); |
| 726 |
|
| 727 |
bool Merge ( DlCompletionTree* from, DlCompletionTree* to, const DepSet& depF ); |
| 728 |
|
| 729 |
bool checkDisjointRoleClash ( const DlCompletionTree* from, const DlCompletionTree* to, |
| 730 |
const TRole* R, const DepSet& dep ); |
| 731 |
|
| 732 |
bool checkDisjointRoleClash ( const DlCompletionTreeArc* edge, const DlCompletionTree* to, |
| 733 |
const TRole* R, const DepSet& dep ) |
| 734 |
{ |
| 735 |
if ( edge->getArcEnd() == to && edge->getRole()->isDisjoint(R) ) |
| 736 |
{ |
| 737 |
setClashSet(dep); |
| 738 |
updateClashSet(edge->getDep()); |
| 739 |
return true; |
| 740 |
} |
| 741 |
return false; |
| 742 |
} |
| 743 |
|
| 744 |
|
| 745 |
|
| 746 |
|
| 747 |
bool checkIrreflexivity ( const DlCompletionTreeArc* edge, |
| 748 |
const TRole* R, const DepSet& dep ) |
| 749 |
{ |
| 750 |
|
| 751 |
if ( edge->getArcEnd() != edge->getReverse()->getArcEnd() ) |
| 752 |
return false; |
| 753 |
|
| 754 |
if ( !edge->isNeighbour(R) && !edge->isNeighbour(R->inverse()) ) |
| 755 |
return false; |
| 756 |
|
| 757 |
|
| 758 |
setClashSet(dep); |
| 759 |
updateClashSet(edge->getDep()); |
| 760 |
return true; |
| 761 |
} |
| 762 |
|
| 763 |
|
| 764 |
bool applyTransitions ( const DlCompletionTreeArc* edge, const RAStateTransitions& RST, |
| 765 |
BipolarPointer C, const DepSet& dep, const char* reason = nullptr ); |
| 766 |
|
| 767 |
|
| 768 |
|
| 769 |
|
| 770 |
bool checkProjection ( DlCompletionTreeArc* pA, BipolarPointer C, const TRole* ProjR ); |
| 771 |
|
| 772 |
|
| 773 |
|
| 774 |
|
| 775 |
bool hasDataClash ( const DlCompletionTree* node ); |
| 776 |
|
| 777 |
bool checkDataClash ( const DlCompletionTree* node ) |
| 778 |
{ |
| 779 |
if ( hasDataClash(node) ) |
| 780 |
{ |
| 781 |
setClashSet(DTReasoner.getClashSet()); |
| 782 |
return true; |
| 783 |
} |
| 784 |
else |
| 785 |
return false; |
| 786 |
} |
| 787 |
|
| 788 |
|
| 789 |
|
| 790 |
|
| 791 |
void logIndentation ( void ) const; |
| 792 |
|
| 793 |
void logStartEntry ( void ) const; |
| 794 |
|
| 795 |
void logFinishEntry ( bool res ) const; |
| 796 |
|
| 797 |
void logNCEntry ( const DlCompletionTree* n, const ConceptWDep& C, |
| 798 |
const char* action, const char* reason ) const |
| 799 |
{ |
| 800 |
CHECK_LL_RETURN(llGTA); |
| 801 |
|
| 802 |
LL << " " << action << "("; |
| 803 |
n->logNode(); |
| 804 |
LL << "," << C.bp() << C.getDep() << ")"; |
| 805 |
if ( reason ) |
| 806 |
LL << reason; |
| 807 |
} |
| 808 |
|
| 809 |
void logEntry ( const DlCompletionTree* n, const ConceptWDep& C, const char* reason ) const |
| 810 |
{ logNCEntry ( n, C, "+", reason ); } |
| 811 |
|
| 812 |
void logClash ( const DlCompletionTree* n, const ConceptWDep& C ) const |
| 813 |
{ logNCEntry ( n, C, "x", DLHeap[C].getTagName() ); } |
| 814 |
|
| 815 |
void writeRoot ( unsigned int level ) |
| 816 |
{ |
| 817 |
if ( LLM.isWritable(level) ) |
| 818 |
CGraph.Print(LL); |
| 819 |
} |
| 820 |
|
| 821 |
|
| 822 |
void finaliseStatistic ( void ); |
| 823 |
|
| 824 |
void logStatisticData ( std::ostream& o, bool needLocal ) const; |
| 825 |
|
| 826 |
|
| 827 |
|
| 828 |
|
| 829 |
void restoreBC ( void ); |
| 830 |
|
| 831 |
unsigned int getCurLevel ( void ) const { return tryLevel; } |
| 832 |
|
| 833 |
void setCurLevel ( unsigned int level ) { tryLevel = level; } |
| 834 |
|
| 835 |
bool noBranchingOps ( void ) const { return tryLevel == InitBranchingLevelValue + nonDetShift; } |
| 836 |
|
| 837 |
unsigned int getSaveRestoreLevel ( const DepSet& ds ) const |
| 838 |
{ |
| 839 |
|
| 840 |
if ( RKG_USE_DYNAMIC_BACKJUMPING ) |
| 841 |
return ds.level()+1; |
| 842 |
else |
| 843 |
return getCurLevel(); |
| 844 |
} |
| 845 |
|
| 846 |
void save ( void ); |
| 847 |
|
| 848 |
void restore ( void ) { return restore(getCurLevel()-1); } |
| 849 |
|
| 850 |
void restore ( unsigned int newTryLevel ); |
| 851 |
|
| 852 |
void updateLevel ( DlCompletionTree* n, const DepSet& ds ) { CGraph.saveNode ( n, getSaveRestoreLevel(ds) ); } |
| 853 |
|
| 854 |
void determiniseBranchingOp ( void ) |
| 855 |
{ |
| 856 |
clearBC(); |
| 857 |
Stack.pop(); |
| 858 |
} |
| 859 |
|
| 860 |
|
| 861 |
|
| 862 |
|
| 863 |
const DepSet& getClashSet ( void ) const { return clashSet; } |
| 864 |
|
| 865 |
void setClashSet ( const DepSet& d ) { clashSet = d; } |
| 866 |
|
| 867 |
void updateClashSet ( const DepSet& d ) { clashSet.add(d); } |
| 868 |
|
| 869 |
DepSet getCurDepSet ( void ) const { return DepSet(Manager.get(getCurLevel()-1)); } |
| 870 |
|
| 871 |
|
| 872 |
DepSet& getBranchDep ( void ) { return bContext->branchDep; } |
| 873 |
|
| 874 |
const DepSet& getBranchDep ( void ) const { return bContext->branchDep; } |
| 875 |
|
| 876 |
void prepareBranchDep ( void ) { getBranchDep().restrict(getCurLevel()); } |
| 877 |
|
| 878 |
void useBranchDep ( void ) |
| 879 |
{ |
| 880 |
prepareBranchDep(); |
| 881 |
setClashSet(getBranchDep()); |
| 882 |
} |
| 883 |
|
| 884 |
void nextBranchingOption ( void ) |
| 885 |
{ |
| 886 |
getBranchDep().add(getClashSet()); |
| 887 |
bContext->nextOption(); |
| 888 |
} |
| 889 |
|
| 890 |
|
| 891 |
bool straightforwardRestore ( void ); |
| 892 |
|
| 893 |
bool backJumpedRestore ( void ); |
| 894 |
|
| 895 |
bool tunedRestore ( void ); |
| 896 |
|
| 897 |
|
| 898 |
bool isUsed ( BipolarPointer p ) const { return ( isPositive(p) ? pUsed : nUsed ).in(getValue(p)); } |
| 899 |
|
| 900 |
void setUsed ( BipolarPointer p ) { ( isPositive(p) ? pUsed : nUsed ).add(getValue(p)); } |
| 901 |
|
| 902 |
public: |
| 903 |
|
| 904 |
bool applicable ( const TBox::TSimpleRule& rule ); |
| 905 |
|
| 906 |
public: |
| 907 |
|
| 908 |
void repeatUnblockedNode ( DlCompletionTree* node, bool direct ) |
| 909 |
{ |
| 910 |
if ( direct ) |
| 911 |
applyAllGeneratingRules(node); |
| 912 |
else |
| 913 |
redoNodeLabel ( node, "ubi" ); |
| 914 |
} |
| 915 |
|
| 916 |
|
| 917 |
const DLDag& getDAG ( void ) const { return tBox.DLHeap; } |
| 918 |
|
| 919 |
public: |
| 920 |
|
| 921 |
explicit DlSatTester ( TBox& tbox ); |
| 922 |
|
| 923 |
DlSatTester ( const DlSatTester& ) = delete; |
| 924 |
|
| 925 |
DlSatTester& operator = ( const DlSatTester& ) = delete; |
| 926 |
|
| 927 |
virtual ~DlSatTester() = default; |
| 928 |
|
| 929 |
|
| 930 |
virtual void prepareReasoner ( void ); |
| 931 |
|
| 932 |
bool runSat ( BipolarPointer p, BipolarPointer q = bpTOP ); |
| 933 |
|
| 934 |
bool checkDisjointRoles ( const TRole* R, const TRole* S ); |
| 935 |
|
| 936 |
bool checkIrreflexivity ( const TRole* R ); |
| 937 |
|
| 938 |
|
| 939 |
const DlCompletionTree* getRootNode ( void ) const { return CGraph.getRoot(); } |
| 940 |
|
| 941 |
|
| 942 |
void setBlockingMethod ( bool hasInverse, bool hasQCR ) { CGraph.setBlockingMethod ( hasInverse, hasQCR ); } |
| 943 |
|
| 944 |
|
| 945 |
const modelCacheInterface* createCache ( BipolarPointer p ); |
| 946 |
|
| 947 |
modelCacheInterface* buildCacheByCGraph ( bool sat ) const |
| 948 |
{ |
| 949 |
if ( sat ) |
| 950 |
return createModelCache(getRootNode()); |
| 951 |
else |
| 952 |
return createConstCache(bpBOTTOM); |
| 953 |
} |
| 954 |
|
| 955 |
void writeTotalStatistic ( std::ostream& o ) |
| 956 |
{ |
| 957 |
# ifdef USE_REASONING_STATISTICS |
| 958 |
AccumulatedStatistic::accumulateAll(); |
| 959 |
logStatisticData ( o, false ); |
| 960 |
# endif |
| 961 |
printBlockingStat (o); |
| 962 |
clearBlockingStat(); |
| 963 |
o << "\n"; |
| 964 |
} |
| 965 |
|
| 966 |
|
| 967 |
float printReasoningTime ( std::ostream& o ) const; |
| 968 |
}; |
| 969 |
|
| 970 |
|
| 971 |
|
| 972 |
inline void DlSatTester :: resetSessionFlags ( void ) |
| 973 |
{ |
| 974 |
|
| 975 |
ensureDAGSize(); |
| 976 |
|
| 977 |
setUsed(bpTOP); |
| 978 |
setUsed(bpBOTTOM); |
| 979 |
|
| 980 |
encounterNominal = false; |
| 981 |
checkDataNode = true; |
| 982 |
} |
| 983 |
|
| 984 |
inline bool |
| 985 |
DlSatTester :: initNewNode ( DlCompletionTree* node, const DepSet& dep, BipolarPointer C ) |
| 986 |
{ |
| 987 |
if ( node->isDataNode() ) |
| 988 |
checkDataNode = false; |
| 989 |
node->setInit(C); |
| 990 |
if ( unlikely ( addToDoEntry ( node, C, dep ) ) ) |
| 991 |
return true; |
| 992 |
if ( node->isDataNode() ) |
| 993 |
return false; |
| 994 |
if ( unlikely ( addToDoEntry ( node, tBox.getTG(), dep ) ) ) |
| 995 |
return true; |
| 996 |
if ( unlikely ( GCIs.isReflexive() && applyReflexiveRoles ( node, dep ) ) ) |
| 997 |
return true; |
| 998 |
if ( unlikely ( !SessionGCIs.empty() ) ) |
| 999 |
for ( std::vector<BipolarPointer>::iterator p = SessionGCIs.begin(), p_end = SessionGCIs.end(); p != p_end; ++p ) |
| 1000 |
if ( unlikely ( addToDoEntry ( node, *p, dep, "sg" ) ) ) |
| 1001 |
return true; |
| 1002 |
|
| 1003 |
return false; |
| 1004 |
} |
| 1005 |
|
| 1006 |
inline bool |
| 1007 |
DlSatTester :: runSat ( BipolarPointer p, BipolarPointer q ) |
| 1008 |
{ |
| 1009 |
prepareReasoner(); |
| 1010 |
|
| 1011 |
|
| 1012 |
if ( initNewNode ( CGraph.getRoot(), DepSet(), p ) || |
| 1013 |
addToDoEntry ( CGraph.getRoot(), ConceptWDep(q) ) ) |
| 1014 |
return false; |
| 1015 |
|
| 1016 |
|
| 1017 |
TsProcTimer& timer = q == bpTOP ? satTimer : subTimer; |
| 1018 |
timer.Start(); |
| 1019 |
bool result = runSat(); |
| 1020 |
timer.Stop(); |
| 1021 |
return result; |
| 1022 |
} |
| 1023 |
|
| 1024 |
inline bool |
| 1025 |
DlSatTester :: checkDisjointRoles ( const TRole* R, const TRole* S ) |
| 1026 |
{ |
| 1027 |
prepareReasoner(); |
| 1028 |
|
| 1029 |
|
| 1030 |
DepSet dummy; |
| 1031 |
if ( initNewNode ( CGraph.getRoot(), dummy, bpTOP ) ) |
| 1032 |
return true; |
| 1033 |
|
| 1034 |
curNode = CGraph.getRoot(); |
| 1035 |
DlCompletionTreeArc* edgeR = createOneNeighbour ( R, dummy ); |
| 1036 |
DlCompletionTreeArc* edgeS = createOneNeighbour ( S, dummy ); |
| 1037 |
|
| 1038 |
if ( initNewNode ( edgeR->getArcEnd(), dummy, bpTOP ) |
| 1039 |
|| initNewNode ( edgeS->getArcEnd(), dummy, bpTOP ) |
| 1040 |
|| setupEdge ( edgeR, dummy ) |
| 1041 |
|| setupEdge ( edgeS, dummy ) |
| 1042 |
|| Merge ( edgeS->getArcEnd(), edgeR->getArcEnd(), dummy ) ) |
| 1043 |
return true; |
| 1044 |
|
| 1045 |
|
| 1046 |
curNode = nullptr; |
| 1047 |
return !runSat(); |
| 1048 |
} |
| 1049 |
|
| 1050 |
inline bool |
| 1051 |
DlSatTester :: checkIrreflexivity ( const TRole* R ) |
| 1052 |
{ |
| 1053 |
prepareReasoner(); |
| 1054 |
|
| 1055 |
|
| 1056 |
DepSet dummy; |
| 1057 |
if ( initNewNode ( CGraph.getRoot(), dummy, bpTOP ) ) |
| 1058 |
return true; |
| 1059 |
|
| 1060 |
curNode = CGraph.getRoot(); |
| 1061 |
DlCompletionTreeArc* edgeR = createOneNeighbour ( R, dummy ); |
| 1062 |
|
| 1063 |
if ( initNewNode ( edgeR->getArcEnd(), dummy, bpTOP ) |
| 1064 |
|| setupEdge ( edgeR, dummy ) |
| 1065 |
|| Merge ( edgeR->getArcEnd(), CGraph.getRoot(), dummy ) ) |
| 1066 |
return true; |
| 1067 |
|
| 1068 |
|
| 1069 |
curNode = nullptr; |
| 1070 |
return !runSat(); |
| 1071 |
} |
| 1072 |
|
| 1073 |
|
| 1074 |
inline bool DlSatTester :: backJumpedRestore ( void ) |
| 1075 |
{ |
| 1076 |
|
| 1077 |
if ( getClashSet().empty () ) |
| 1078 |
return true; |
| 1079 |
|
| 1080 |
|
| 1081 |
restore ( getClashSet().level() ); |
| 1082 |
return false; |
| 1083 |
} |
| 1084 |
|
| 1085 |
inline bool DlSatTester :: straightforwardRestore ( void ) |
| 1086 |
{ |
| 1087 |
if ( noBranchingOps() ) |
| 1088 |
return true; |
| 1089 |
else |
| 1090 |
{ |
| 1091 |
restore (); |
| 1092 |
return false; |
| 1093 |
} |
| 1094 |
} |
| 1095 |
|
| 1096 |
inline bool DlSatTester :: tunedRestore ( void ) |
| 1097 |
{ |
| 1098 |
if ( tBox.useBackjumping ) |
| 1099 |
return backJumpedRestore (); |
| 1100 |
else |
| 1101 |
return straightforwardRestore (); |
| 1102 |
} |
| 1103 |
|
| 1104 |
inline bool DlSatTester :: commonTacticBodyAll ( const DLVertex& cur ) |
| 1105 |
{ |
| 1106 |
#ifdef ENABLE_CHECKING |
| 1107 |
fpp_assert ( isPositive(curConcept.bp()) && cur.Type() == dtForall ); |
| 1108 |
#endif |
| 1109 |
|
| 1110 |
if ( unlikely(cur.getRole()->isTop()) ) |
| 1111 |
{ |
| 1112 |
incStat(nAllCalls); |
| 1113 |
return addSessionGCI ( cur.getC(), curConcept.getDep() ); |
| 1114 |
} |
| 1115 |
|
| 1116 |
if ( cur.getRole()->isSimple() ) |
| 1117 |
return commonTacticBodyAllSimple(cur); |
| 1118 |
else |
| 1119 |
return commonTacticBodyAllComplex(cur); |
| 1120 |
} |
| 1121 |
|
| 1122 |
|
| 1123 |
|
| 1124 |
|
| 1125 |
|
| 1126 |
inline const modelCacheInterface* |
| 1127 |
TBox :: initCache ( const TConcept* pConcept, bool sub ) |
| 1128 |
{ |
| 1129 |
BipolarPointer bp = sub ? inverse(pConcept->pName) : pConcept->pName; |
| 1130 |
const modelCacheInterface* cache = DLHeap.getCache(bp); |
| 1131 |
|
| 1132 |
if ( cache == nullptr ) |
| 1133 |
{ |
| 1134 |
if ( sub ) |
| 1135 |
prepareFeatures ( nullptr, pConcept ); |
| 1136 |
else |
| 1137 |
prepareFeatures ( pConcept, nullptr ); |
| 1138 |
cache = getReasoner()->createCache(bp); |
| 1139 |
clearFeatures(); |
| 1140 |
} |
| 1141 |
|
| 1142 |
return cache; |
| 1143 |
} |
| 1144 |
|
| 1145 |
|
| 1146 |
inline enum modelCacheState |
| 1147 |
TBox :: testCachedNonSubsumption ( const TConcept* p, const TConcept* q ) |
| 1148 |
{ |
| 1149 |
const modelCacheInterface* pCache = initCache ( p, false ); |
| 1150 |
const modelCacheInterface* nCache = initCache ( q, true ); |
| 1151 |
return pCache->canMerge(nCache); |
| 1152 |
} |
| 1153 |
|
| 1154 |
#endif |