Debian Codegraph @

Kernel/Tactic.cpp

raw symbols SCIP
1 /* This file is part of the FaCT++ DL reasoner
2 Copyright (C) 2003-2015 Dmitry Tsarkov and The University of Manchester
3 Copyright (C) 2015-2017 Dmitry Tsarkov
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20 #include "globaldef.h"
21
22 #include <algorithm> // for std::sort
23
24 #include "Reasoner.h"
25 #include "logging.h"
26
27 #define switchResult(expr)\
28 do { if (unlikely(expr)) return true; } while (false)
29
30 /********************************************************************************
31 * Tactics section;
32 *
33 * Each Tactic should have a (small) Usability function <name>
34 * and a Real tactic function <name>Body
35 *
36 * Each tactic returns:
37 * - true - if expansion of CUR lead to clash
38 * - false - otherwise
39 *
40 ******************************************************************************/
41
42 // main local Tactic
43 bool DlSatTester :: commonTactic ( void )
44 {
45 #ifdef ENABLE_CHECKING
46 fpp_assert ( curConcept.bp() != bpINVALID );
47 #endif
48
49 // check if Node is cached and we tries to expand existing result
50 // also don't do anything for p-blocked nodes (can't be unblocked)
51 if ( curNode->isCached() || curNode->isPBlocked() )
52 return false;
53
54 // informs about starting calculations...
55 if ( LLM.isWritable(llGTA) )
56 logStartEntry();
57
58 bool ret = false;
59
60 // apply tactic only if Node is not an i-blocked
61 if ( !isIBlocked() )
62 ret = commonTacticBody ( DLHeap[curConcept] );
63
64 if ( LLM.isWritable(llGTA) )
65 logFinishEntry(ret);
66
67 return ret;
68 }
69
70 //-------------------------------------------------------------------------------
71 // Simple tactics
72 //-------------------------------------------------------------------------------
73
74 bool DlSatTester :: commonTacticBody ( const DLVertex& cur )
75 {
76 // show DAG usage
77 #ifdef RKG_PRINT_DAG_USAGE
78 const_cast<DLVertex&>(cur).incUsage(isPositive(curConcept.bp()));
79 #endif
80 incStat(nTacticCalls);
81
82 // call proper tactic
83 switch ( cur.Type() )
84 {
85 case dtTop:
86 fpp_unreachable(); // can't appear here; addToDoEntry deals with constants
87 return false;
88
89 case dtDataType: // data things are checked by data inferer
90 case dtDataValue:
91 incStat(nUseless);
92 return false;
93
94 case dtPSingleton:
95 case dtNSingleton:
96 if ( isPositive (curConcept.bp()) ) // real singleton
97 return commonTacticBodySingleton(cur);
98 else // negated singleton -- nothing to do with.
99 return commonTacticBodyId(cur);
100
101 case dtNConcept:
102 case dtPConcept:
103 return commonTacticBodyId(cur);
104
105 case dtAnd:
106 if ( isPositive (curConcept.bp()) ) // this is AND vertex
107 return commonTacticBodyAnd(cur);
108 else // OR
109 return commonTacticBodyOr(cur);
110
111 case dtForall:
112 if ( isNegative(curConcept.bp()) ) // SOME vertex
113 return commonTacticBodySome(cur);
114
115 // ALL vertex
116 return commonTacticBodyAll(cur);
117
118 case dtIrr:
119 if ( isNegative(curConcept.bp()) ) // SOME R.Self vertex
120 return commonTacticBodySomeSelf(cur.getRole());
121 else // don't need invalidate cache, as IRREFL can only lead to CLASH
122 return commonTacticBodyIrrefl(cur.getRole());
123
124 case dtLE:
125 if ( isNegative (curConcept.bp()) ) // >= vertex
126 return commonTacticBodyGE(cur);
127
128 // <= vertex
129 if ( isFunctionalVertex(cur) )
130 return commonTacticBodyFunc(cur);
131 else
132 return commonTacticBodyLE(cur);
133
134 case dtProj:
135 fpp_assert ( isPositive (curConcept.bp()) );
136 return commonTacticBodyProj ( cur.getRole(), cur.getC(), cur.getProjRole() );
137
138 case dtChoose:
139 fpp_assert ( isPositive (curConcept.bp()) );
140 return applyChooseRule ( curNode, cur.getC() );
141
142 default:
143 fpp_unreachable();
144 return false;
145 }
146 }
147
148 bool DlSatTester :: commonTacticBodyId ( const DLVertex& cur )
149 {
150 #ifdef ENABLE_CHECKING
151 fpp_assert ( isCNameTag(cur.Type()) ); // safety check
152 #endif
153
154 incStat(nIdCalls);
155 const DepSet& dep = curConcept.getDep();
156
157 #ifdef RKG_USE_SIMPLE_RULES
158 // check if we have some simple rules
159 if ( isPositive(curConcept.bp()) )
160 switchResult ( applyExtraRulesIf(static_cast<const TConcept*>(cur.getConcept())) );
161 #endif
162
163 // get either body(p) or inverse(body(p)), depends on sign of current ID
164 BipolarPointer C = isPositive(curConcept.bp()) ? cur.getC() : inverse(cur.getC());
165 return addToDoEntry ( curNode, C, dep );
166 }
167
168 /// @return true if the rule is applicable; set the dep-set accordingly
169 bool
170 DlSatTester :: applicable ( const TBox::TSimpleRule& rule )
171 {
172 BipolarPointer bp = curConcept.bp();
173 const CWDArray& lab = curNode->label().getLabel(/*isComplex=*/false);
174 // dep-set to keep track for all the concepts in a rule-head
175 DepSet dep = curConcept.getDep();
176
177 for ( const auto& atom: rule.Body )
178 {
179 if ( atom->pName == bp )
180 continue;
181 // FIXME!! double check that's correct (no need to negate pName)
182 if ( findConceptClash ( lab, atom->pName, dep ) )
183 dep = getClashSet(); // such a concept exists -- remember clash set
184 else // no such concept -- can not fire a rule
185 return false;
186 }
187
188 // rule will be fired -- set the dep-set
189 setClashSet(dep);
190 return true;
191 }
192
193 bool
194 DlSatTester :: applyExtraRules ( const TConcept* C )
195 {
196 for ( auto p = C->er_begin(), p_end = C->er_end(); p != p_end; ++p )
197 {
198 auto rule = tBox.getSimpleRule(*p);
199 incStat(nSRuleAdd);
200 if ( applicable(*rule) ) // apply the rule's head
201 {
202 incStat(nSRuleFire);
203 switchResult ( addToDoEntry ( curNode, rule->bpHead, getClashSet() ) );
204 }
205 }
206
207 return false;
208 }
209
210 /// add C to a set of session GCIs; init all nodes with (C,dep)
211 bool
212 DlSatTester :: addSessionGCI ( BipolarPointer C, const DepSet& dep )
213 {
214 SessionGCIs.push_back(C);
215 for ( auto& node: CGraph )
216 if ( isNodeGloballyUsed(node) && addToDoEntry ( node, C, dep, "sg" ) )
217 return true;
218 return false;
219 }
220
221 bool DlSatTester :: commonTacticBodySingleton ( const DLVertex& cur )
222 {
223 #ifdef ENABLE_CHECKING
224 fpp_assert ( cur.Type() == dtPSingleton || cur.Type() == dtNSingleton ); // safety check
225 #endif
226
227 incStat(nSingletonCalls);
228
229 // can use this rule only in the Nominal reasoner
230 fpp_assert ( hasNominals() );
231
232 // if the test REALLY uses nominals, remember this
233 encounterNominal = true;
234
235 const TIndividual* C = static_cast<const TIndividual*>(cur.getConcept());
236 fpp_assert ( C->node != nullptr );
237
238 // if node for C was purged due to merge -- find proper one
239 DepSet dep = curConcept.getDep();
240 DlCompletionTree* realNode = C->node->resolvePBlocker(dep);
241
242 if ( realNode != curNode ) // check if o-rule is applicable
243 // apply o-rule: merge 2 nodes
244 // don't need to actually expand P: it was/will be done in C->node
245 return Merge ( curNode, realNode, dep );
246
247 // singleton behaves as a general named concepts besides nominal cloud
248 return commonTacticBodyId(cur);
249 }
250
251 //-------------------------------------------------------------------------------
252 // AND/OR processing
253 //-------------------------------------------------------------------------------
254
255 bool DlSatTester :: commonTacticBodyAnd ( const DLVertex& cur )
256 {
257 #ifdef ENABLE_CHECKING
258 fpp_assert ( isPositive(curConcept.bp()) && ( cur.Type() == dtAnd ) ); // safety check
259 #endif
260
261 incStat(nAndCalls);
262
263 const DepSet& dep = curConcept.getDep();
264
265 // FIXME!! I don't know why, but performance is usually BETTER if using r-iters.
266 // It's their only usage, so after investigation they can be dropped
267 for ( DLVertex::const_reverse_iterator q = cur.rbegin(); q != cur.rend(); ++q )
268 switchResult ( addToDoEntry ( curNode, *q, dep ) );
269
270 return false;
271 }
272
273 bool DlSatTester :: commonTacticBodyOr ( const DLVertex& cur ) // for C \or D concepts
274 {
275 #ifdef ENABLE_CHECKING
276 fpp_assert ( isNegative(curConcept.bp()) && cur.Type() == dtAnd ); // safety check
277 #endif
278
279 incStat(nOrCalls);
280
281 if ( isFirstBranchCall() ) // check the structure of OR operation (number of applicable concepts)
282 {
283 DepSet dep;
284 if ( planOrProcessing ( cur, dep ) )
285 { // found existing component
286 if ( LLM.isWritable(llGTA) )
287 LL << " E(" << OrConceptsToTest.back() << ")";
288 return false;
289 }
290 if ( OrConceptsToTest.empty() )
291 { // no more applicable concepts:
292 // set global dep-set using accumulated deps
293 setClashSet(dep);
294 return true;
295 }
296 // not a branching: just add a single concept
297 if ( OrConceptsToTest.size() == 1 )
298 {
299 BipolarPointer C = OrConceptsToTest.back();
300 return insertToDoEntry ( curNode, ConceptWDep(C,dep), DLHeap[C].Type(), "bcp" );
301 }
302
303 // more than one alternative: use branching context
304 createBCOr();
305 bContext->branchDep = dep;
306 static_cast<BCOr*>(bContext)->applicableOrEntries.swap(OrConceptsToTest);
307 }
308
309 // now it is OR case with 1 or more applicable concepts
310 return processOrEntry();
311 }
312
313 bool DlSatTester :: planOrProcessing ( const DLVertex& cur, DepSet& dep )
314 {
315 OrConceptsToTest.clear();
316 dep = curConcept.getDep();
317 DepSet dummy;
318
319 // check all OR components for the clash
320 const CGLabel& lab = curNode->label();
321 for ( DLVertex::const_iterator q = cur.begin(), q_end = cur.end(); q < q_end; ++q )
322 {
323 BipolarPointer C = inverse(*q);
324 switch ( tryAddConcept ( lab.getLabel(DLHeap[C].Type()), C, dummy ) )
325 {
326 case acrClash: // clash found -- OK
327 dep.add(getClashSet());
328 continue;
329 case acrExist: // already have such concept -- save it to the 1st position
330 OrConceptsToTest.resize(1);
331 OrConceptsToTest[0] = C;
332 return true;
333 case acrDone:
334 OrConceptsToTest.push_back(C);
335 continue;
336 default: // safety check
337 fpp_unreachable();
338 }
339 }
340
341 return false;
342 }
343
344 bool DlSatTester :: processOrEntry ( void )
345 {
346 // save the context here as after save() it would be lost
347 const BCOr* bcOr = static_cast<BCOr*>(bContext);
348 BCOr::or_iterator p = bcOr->orBeg(), p_end = bcOr->orCur();
349 BipolarPointer C = *p_end;
350 const char* reason = nullptr;
351 DepSet dep;
352
353 if ( bcOr->isLastOrEntry() )
354 {
355 // cumulative dep-set will be used
356 prepareBranchDep();
357 dep = getBranchDep();
358 // no more branching decisions
359 determiniseBranchingOp();
360 reason = "bcp";
361 }
362 else
363 {
364 // save current state
365 save();
366 // new (just branched) dep-set
367 dep = getCurDepSet();
368 incStat(nOrBrCalls);
369 }
370
371 // if semantic branching is in use -- add previous entries to the label
372 if ( useSemanticBranching() )
373 for ( ; p < p_end; ++p )
374 if ( addToDoEntry ( curNode, ConceptWDep(inverse(*p),dep), "sb" ) )
375 fpp_unreachable(); // Both Exists and Clash are errors
376
377 // add new entry to current node
378 if ( RKG_USE_DYNAMIC_BACKJUMPING )
379 return addToDoEntry ( curNode, ConceptWDep(C,dep), reason );
380 else // we know the result would be DONE
381 return insertToDoEntry ( curNode, ConceptWDep(C,dep), DLHeap[C].Type(), reason );
382 }
383
384 //-------------------------------------------------------------------------------
385 // ALL processing
386 //-------------------------------------------------------------------------------
387
388 bool DlSatTester :: commonTacticBodyAllComplex ( const DLVertex& cur )
389 {
390 const DepSet& dep = curConcept.getDep();
391 unsigned int state = cur.getState();
392 BipolarPointer C = curConcept.bp()-(BipolarPointer)state; // corresponds to AR{0}.X
393 auto& RST = cur.getRole()->getAutomaton()[state];
394
395 // apply all empty transitions
396 if ( RST.hasEmptyTransition() )
397 for ( const auto& trans: RST )
398 {
399 incStat(nAutoEmptyLookups);
400
401 if ( trans.empty() )
402 switchResult ( addToDoEntry ( curNode, C+BipolarPointer(trans.final()), dep, "e" ) );
403 }
404
405 // apply all top-role transitions
406 if ( unlikely(RST.hasTopTransition()) )
407 for ( const auto& trans: RST )
408 {
409 if ( trans.isTop() )
410 switchResult ( addSessionGCI ( C+BipolarPointer(trans.final()), dep ) );
411 }
412
413 // apply final-state rule
414 if ( state == 1 )
415 switchResult ( addToDoEntry ( curNode, cur.getC(), dep ) );
416
417 // check whether automaton applicable to any edges
418 incStat(nAllCalls);
419
420 // check all neighbours
421 for ( const auto& neighbour: *curNode )
422 {
423 if ( RST.recognise(neighbour->getRole()) )
424 switchResult ( applyTransitions ( neighbour, RST, C, dep+neighbour->getDep() ) );
425 }
426
427 return false;
428 }
429
430 bool DlSatTester :: commonTacticBodyAllSimple ( const DLVertex& cur )
431 {
432 const RAStateTransitions& RST = cur.getRole()->getAutomaton()[0];
433 const DepSet& dep = curConcept.getDep();
434 BipolarPointer C = cur.getC();
435
436 // check whether automaton applicable to any edges
437 incStat(nAllCalls);
438
439 // check all neighbours; as the role is simple then recognise() == applicable()
440 for ( const auto& neighbour: *curNode )
441 {
442 if ( RST.recognise(neighbour->getRole()) )
443 switchResult ( addToDoEntry ( neighbour->getArcEnd(), C, dep+neighbour->getDep() ) );
444 }
445
446 return false;
447 }
448
449 //-------------------------------------------------------------------------------
450 // Support for ALL processing
451 //-------------------------------------------------------------------------------
452
453 /** Perform expansion of (C=\AR{state}.X).DEP to an EDGE with a given reason */
454 bool DlSatTester :: applyTransitions ( const DlCompletionTreeArc* edge,
455 const RAStateTransitions& RST,
456 BipolarPointer C,
457 const DepSet& dep, const char* reason )
458 {
459 DlCompletionTree* node = edge->getArcEnd();
460 // fast lane: the single transition which is applicable
461 if ( RST.isSingleton() )
462 return addToDoEntry ( node, C+(BipolarPointer)RST.getTransitionEnd(), dep, reason );
463
464 const TRole* R = edge->getRole();
465
466 // try to apply all transitions to edge
467 for ( const auto& trans: RST )
468 {
469 incStat(nAutoTransLookups);
470 if ( trans.applicable(R) )
471 switchResult ( addToDoEntry ( node, C+BipolarPointer(trans.final()), dep, reason ) );
472 }
473
474 return false;
475 }
476
477 //-------------------------------------------------------------------------------
478 // SOME processing
479 //-------------------------------------------------------------------------------
480
481 bool DlSatTester :: commonTacticBodySome ( const DLVertex& cur ) // for ER.C concepts
482 {
483 #ifdef ENABLE_CHECKING
484 fpp_assert ( isNegative(curConcept.bp()) && cur.Type() == dtForall );
485 #endif
486
487 const DepSet& dep = curConcept.getDep();
488 const TRole* R = cur.getRole();
489 BipolarPointer C = inverse(cur.getC());
490
491 if ( unlikely(R->isTop()) )
492 return commonTacticBodySomeUniv(cur);
493
494 // check if we already have R-neighbour labeled with C
495 if ( isSomeExists ( R, C ) )
496 return false;
497 // try to check the case (some R (or C D)), where C is in the label of an R-neighbour
498 if ( isNegative(C) && DLHeap[C].Type() == dtAnd )
499 for ( const auto& arg: DLHeap[C] )
500 if ( isSomeExists ( R, inverse(arg) ) )
501 return false;
502
503 // check for the case \ER.{o}
504 if ( tBox.testHasNominals() && isPositive(C) )
505 {
506 const DLVertex& nom = DLHeap[C];
507 if ( nom.Type() == dtPSingleton || nom.Type() == dtNSingleton )
508 return commonTacticBodyValue ( R, static_cast<const TIndividual*>(nom.getConcept()) );
509 }
510
511 incStat(nSomeCalls);
512
513 // check if we have functional role
514 if ( R->isFunctional() )
515 for ( const auto& topf: R->topfuncs() )
516 switch ( tryAddConcept ( curNode->label().getLabel(/*isComplex=*/true), topf->getFunctional(), dep ) )
517 {
518 case acrClash: // addition leads to clash
519 return true;
520 case acrDone: // should be add to a label
521 {
522 // we are changing current Node => save it
523 updateLevel ( curNode, dep );
524
525 ConceptWDep rFuncRestriction ( topf->getFunctional(), dep );
526 // NOTE! not added into TODO (because will be checked right now)
527 CGraph.addConceptToNode ( curNode, rFuncRestriction, /*isComplex=*/true );
528 setUsed(rFuncRestriction.bp());
529
530 if ( LLM.isWritable(llGTA) )
531 LL << " nf(" << rFuncRestriction << ")";
532 }
533 break;
534 case acrExist: // already exists
535 break;
536 default: // safety check
537 fpp_unreachable();
538 }
539
540
541 bool rFunc = false; // flag is true if we have functional restriction with this Role name
542 const TRole* RF = R; // most general functional super-role of given one
543 ConceptWDep rFuncRestriction; // role's functional restriction w/dep
544
545 // set up rFunc; rfRole contains more generic functional superrole of rName
546 for ( DlCompletionTree::const_label_iterator pc = curNode->beginl_cc(); pc != curNode->endl_cc(); ++pc )
547 { // found such vertex (<=1 R)
548 const ConceptWDep& LC = *pc;
549 const DLVertex& ver = DLHeap[LC];
550
551 if ( isPositive(LC.bp()) && isFunctionalVertex(ver) && *ver.getRole() >= *R )
552 if ( !rFunc || // 1st functional restriction found or another one...
553 *ver.getRole() >= *RF ) // ... with more generic role
554 {
555 rFunc = true;
556 RF = ver.getRole();
557 rFuncRestriction = LC;
558 }
559 }
560
561 if ( rFunc ) // functional role found => add new concept to existing node
562 {
563 const DlCompletionTreeArc* functionalArc = nullptr;
564
565 // check if we have an (R)-successor or (R-)-predecessor
566 for ( const auto& edge: *curNode )
567 if ( edge->isNeighbour (RF) )
568 {
569 functionalArc = edge;
570 break;
571 }
572
573 // perform actions if such arc was found
574 if ( functionalArc != nullptr )
575 {
576 if ( LLM.isWritable(llGTA) )
577 LL << " f(" << rFuncRestriction << "):";
578
579 DlCompletionTree* succ = functionalArc->getArcEnd();
580
581 // add current dependencies (from processed entry)
582 DepSet newDep = { functionalArc->getDep() };
583 newDep.add(dep);
584
585 // check if merging will lead to clash because of disjoint roles
586 if ( R->isDisjoint() && checkDisjointRoleClash ( curNode, succ, R, newDep ) )
587 return true;
588
589 // add current role label (to both arc and its reverse)
590 functionalArc = CGraph.addRoleLabel ( curNode, succ, functionalArc->isPredEdge(), R, newDep );
591
592 // adds concept to the end of arc
593 switchResult ( addToDoEntry ( succ, C, newDep ) );
594
595 // if new role label was added...
596 if ( RF != R )
597 {
598 // add Range and Domain of a new role; this includes functional, so remove it from the latter
599 switchResult ( initHeadOfNewEdge ( curNode, R, newDep, "RD" ) );
600 switchResult ( initHeadOfNewEdge ( succ, R->inverse(), newDep, "RR" ) );
601
602 // check AR.C in both sides of functionalArc
603 // FIXME!! for simplicity, check the functionality here (see bEx017). It seems
604 // only necessary when R has several functional super-roles, so the condition
605 // can be simplified
606 switchResult ( applyUniversalNR ( curNode, functionalArc, newDep, redoForall | redoFunc ) );
607 // if new role label was added to a functionalArc, some functional restrictions
608 // in the SUCC node might became applicable. See bFunctional1x test
609 switchResult ( applyUniversalNR ( succ, functionalArc->getReverse(), newDep,
610 redoForall | redoFunc | redoAtMost ) );
611 }
612
613 return false;
614 }
615 }
616
617 //------------------------------------------------
618 // no functional role or 1st arc -- create new arc
619 //------------------------------------------------
620
621 // there no such neighbour - create new successor
622 // all FUNCs are already checked; no (new) irreflexivity possible
623 return createNewEdge ( cur.getRole(), C, redoForall|redoAtMost );
624 }
625
626 /// expansion rule for existential quantifier in the form ER {o}
627 bool DlSatTester :: commonTacticBodyValue ( const TRole* R, const TIndividual* nom )
628 {
629 DepSet dep(curConcept.getDep());
630
631 // check blocking conditions
632 if ( isCurNodeBlocked() )
633 return false;
634
635 incStat(nSomeCalls);
636
637 fpp_assert ( nom->node != nullptr );
638
639 // if node for NOM was purged due to merge -- find proper one
640 DlCompletionTree* realNode = nom->node->resolvePBlocker(dep);
641
642 // check if merging will lead to clash because of disjoint roles
643 if ( R->isDisjoint() && checkDisjointRoleClash ( curNode, realNode, R, dep ) )
644 return true;
645
646 // here we are sure that there is a nominal connected to a root node
647 encounterNominal = true;
648
649 // create new edge between curNode and the given nominal node
650 DlCompletionTreeArc* edge =
651 CGraph.addRoleLabel ( curNode, realNode, /*linkToParent=*/false, R, dep );
652
653 // add all necessary concepts to both ends of the edge
654 return setupEdge ( edge, dep, redoEverything );
655 }
656
657 bool
658 DlSatTester :: commonTacticBodySomeUniv ( const DLVertex& cur )
659 {
660 // check blocking conditions
661 if ( isCurNodeBlocked() )
662 return false;
663
664 incStat(nSomeCalls);
665
666 BipolarPointer C = inverse(cur.getC());
667 // check whether C is already in CGraph
668 for ( const auto& node: CGraph )
669 if ( isObjectNodeUnblocked(node) && node->label().contains(C) )
670 return false;
671 // make new node labeled with C
672 return initNewNode ( CGraph.getNewNode(), curConcept.getDep(), C );
673 }
674
675 //-------------------------------------------------------------------------------
676 // Support for SOME processing
677 //-------------------------------------------------------------------------------
678
679 bool DlSatTester :: createNewEdge ( const TRole* R, BipolarPointer C, unsigned int redoFlags )
680 {
681 const DepSet& dep = curConcept.getDep();
682
683 // check blocking conditions
684 if ( isCurNodeBlocked() )
685 {
686 incStat(nUseless);
687 return false;
688 }
689
690 DlCompletionTreeArc* pA = createOneNeighbour ( R, dep );
691
692 // add necessary label
693 return initNewNode ( pA->getArcEnd(), dep, C ) || setupEdge ( pA, dep, redoFlags );
694 }
695
696 /// create new ROLE-neighbour to curNode; return edge to it
697 DlCompletionTreeArc* DlSatTester :: createOneNeighbour ( const TRole* R, const DepSet& dep, CTNominalLevel level )
698 {
699 // check whether is called from NN-rule
700 bool forNN = level != BlockableLevel;
701
702 // create a proper neighbour
703 DlCompletionTreeArc* pA = CGraph.createNeighbour ( curNode, /*isPredEdge=*/forNN, R, dep );
704 DlCompletionTree* node = pA->getArcEnd();
705
706 // set nominal node's level if necessary
707 if ( forNN )
708 node->setNominalLevel(level);
709
710 // check whether created node is data node
711 if ( R->isDataRole() )
712 node->setDataNode();
713
714 // log newly created node
715 CHECK_LL_RETURN_VALUE(llGTA,pA);
716
717 if ( R->isDataRole() )
718 LL << " DN(";
719 else
720 LL << " cn(";
721 LL << node->getId() << dep << ")";
722
723 return pA;
724 }
725
726 bool DlSatTester :: isCurNodeBlocked ( void )
727 {
728 // for non-lazy blocking blocked status is correct
729 if ( !useLazyBlocking() )
730 return curNode->isBlocked();
731
732 // update node's blocked status
733 if ( !curNode->isBlocked() && curNode->isAffected() )
734 {
735 updateLevel ( curNode, curConcept.getDep() );
736 CGraph.detectBlockedStatus(curNode);
737 }
738
739 return curNode->isBlocked();
740 }
741
742 void
743 DlSatTester :: applyAllGeneratingRules ( DlCompletionTree* node )
744 {
745 const CGLabel& label = node->label();
746 for ( CGLabel::const_iterator p = label.begin_cc(), p_end = label.end_cc(); p != p_end; ++p )
747 {
748 // need only ER.C or >=nR.C concepts
749 if ( isPositive(p->bp()) )
750 continue;
751
752 switch ( DLHeap[*p].Type() )
753 {
754 case dtForall:
755 case dtLE:
756 addExistingToDoEntry ( node, label.getCCOffset(p), "ubd" );
757 break;
758
759 default:
760 break;
761 }
762 }
763 }
764
765 bool
766 DlSatTester :: setupEdge ( DlCompletionTreeArc* pA, const DepSet& dep, unsigned int redoFlags )
767 {
768 DlCompletionTree* child = pA->getArcEnd();
769 DlCompletionTree* from = pA->getReverse()->getArcEnd();
770
771 // adds Range and Domain
772 switchResult ( initHeadOfNewEdge ( from, pA->getRole(), dep, "RD" ) );
773 switchResult ( initHeadOfNewEdge ( child, pA->getReverse()->getRole(), dep, "RR" ) );
774
775 // check if we have any AR.X concepts in current node
776 if ( redoFlags != 0 )
777 switchResult ( applyUniversalNR ( from, pA, dep, redoFlags ) );
778
779 // for nominal children and loops -- just apply things for the inverses
780 if ( pA->isPredEdge() || child->isNominalNode() || child == from )
781 {
782 if ( redoFlags != 0 )
783 switchResult ( applyUniversalNR ( child, pA->getReverse(), dep, redoFlags ) );
784 }
785 else
786 {
787 if ( child->isDataNode() )
788 {
789 checkDataNode = true;
790 switchResult ( checkDataClash(child) );
791 }
792 else // check if it is possible to use cache for new node
793 switchResult ( usageByState(tryCacheNode(child)) );
794 }
795
796 // all done
797 return false;
798 }
799
800 bool DlSatTester :: applyUniversalNR ( DlCompletionTree* Node,
801 const DlCompletionTreeArc* arcSample,
802 const DepSet& dep_, unsigned int redoFlags )
803 {
804 // check whether a flag is set
805 if ( redoFlags == 0 )
806 return false;
807
808 const TRole* R = arcSample->getRole();
809 DepSet dep = dep_ + arcSample->getDep();
810
811 for ( DlCompletionTree::const_label_iterator
812 p = Node->beginl_cc(), p_end = Node->endl_cc(); p != p_end; ++p )
813 {
814 // need only AR.C concepts where ARC is labelled with R
815 if ( isNegative(p->bp()) )
816 continue;
817
818 const DLVertex& v = DLHeap[*p];
819 const TRole* vR = v.getRole();
820
821 switch ( v.Type() )
822 {
823 case dtIrr:
824 if ( redoFlags & redoIrr )
825 switchResult ( checkIrreflexivity ( arcSample, vR, dep ) );
826 break;
827
828 case dtForall:
829 {
830 if ( (redoFlags & redoForall) == 0 )
831 break;
832 if ( unlikely(vR->isTop()) )
833 break;
834
835 /// check whether transition is possible
836 const RAStateTransitions& RST = vR->getAutomaton()[v.getState()];
837 if ( !RST.recognise(R) )
838 break;
839
840 if ( vR->isSimple() ) // R is recognised so just add the final state!
841 switchResult ( addToDoEntry ( arcSample->getArcEnd(), v.getC(), dep+p->getDep(), "ae" ) );
842 else
843 switchResult ( applyTransitions ( arcSample, RST, p->bp()-(BipolarPointer)v.getState(), dep+p->getDep(), "ae" ) );
844 break;
845 }
846
847 case dtLE:
848 if ( isFunctionalVertex(v) )
849 {
850 if ( (redoFlags & redoFunc) && (*vR >= *R) )
851 addExistingToDoEntry ( Node, Node->label().getCCOffset(p), "f" );
852 }
853 else
854 if ( (redoFlags & redoAtMost) && (*vR >= *R) )
855 addExistingToDoEntry ( Node, Node->label().getCCOffset(p), "le" );
856 break;
857
858 default:
859 break;
860 }
861 }
862
863 return false;
864 }
865
866 /// add necessary concepts to the head of the new EDGE
867 bool
868 DlSatTester :: initHeadOfNewEdge ( DlCompletionTree* node, const TRole* R, const DepSet& dep, const char* reason )
869 {
870 // if R is functional, then add FR with given DEP-set to NODE
871 if ( R->isFunctional() )
872 for ( const auto& topf: R->topfuncs() )
873 switchResult ( addToDoEntry ( node, topf->getFunctional(), dep, "fr" ) );
874
875 // setup Domain for R
876 switchResult ( addToDoEntry ( node, R->getBPDomain(), dep, reason ) );
877
878 if ( !RKG_UPDATE_RND_FROM_SUPERROLES )
879 for ( const auto& sup: R->ancestors() )
880 switchResult ( addToDoEntry ( node, sup->getBPDomain(), dep, reason ) );
881
882 return false;
883 }
884
885 //-------------------------------------------------------------------------------
886 // Func/LE/GE processing
887 //-------------------------------------------------------------------------------
888
889 bool DlSatTester :: commonTacticBodyFunc ( const DLVertex& cur ) // for <=1 R concepts
890 {
891 #ifdef ENABLE_CHECKING
892 fpp_assert ( isPositive(curConcept.bp()) && isFunctionalVertex(cur) );
893 #endif
894
895 const TRole* R = cur.getRole();
896
897 if ( unlikely(R->isTop()) )
898 return processTopRoleFunc(cur);
899
900 // check whether we need to apply NN rule first
901 if ( isNNApplicable ( R, bpTOP, curConcept.bp()+1 ) )
902 return commonTacticBodyNN(cur); // after application func-rule would be checked again
903
904 incStat(nFuncCalls);
905
906 if ( isQuickClashLE(cur) )
907 return true;
908
909 // locate all R-neighbours of curNode
910 DepSet dummy; // not used
911 findNeighbours ( R, bpTOP, dummy );
912
913 // check if we have nodes to merge
914 if ( EdgesToMerge.size() < 2 )
915 return false;
916
917 // merge all nodes to the first (the least wrt nominal hierarchy) found node
918 EdgeVector::iterator q = EdgesToMerge.begin(), q_end = EdgesToMerge.end();
919 DlCompletionTree* sample = (*q)->getArcEnd();
920 DepSet depF (curConcept.getDep()); // dep-set for merging
921 depF.add((*q)->getDep());
922
923 // merge all elements to sample (sample wouldn't be merge)
924 while ( ++q != q_end )
925 // during merge EdgesToMerge may became purged (see Nasty4) => check this
926 if ( !(*q)->getArcEnd()->isPBlocked() )
927 switchResult ( Merge ( (*q)->getArcEnd(), sample, depF+(*q)->getDep() ) );
928
929 return false;
930 }
931
932
933 bool DlSatTester :: commonTacticBodyLE ( const DLVertex& cur ) // for <=nR.C concepts
934 {
935 #ifdef ENABLE_CHECKING
936 fpp_assert ( isPositive(curConcept.bp()) && ( cur.Type() == dtLE ) );
937 #endif
938
939 incStat(nLeCalls);
940 const TRole* R = cur.getRole();
941
942 if ( unlikely(R->isTop()) )
943 return processTopRoleLE(cur);
944
945 BipolarPointer C = cur.getC();
946 bool needInit = true;
947
948 if ( !isFirstBranchCall() )
949 {
950 if ( dynamic_cast<BCNN*>(bContext) != nullptr )
951 return commonTacticBodyNN(cur); // after application <=-rule would be checked again
952 if ( dynamic_cast<BCLE<DlCompletionTreeArc>*>(bContext) != nullptr )
953 needInit = false; // clash in LE-rule: skip the initial checks
954 else // the only possible case is choose-rule; in this case just continue
955 fpp_assert ( dynamic_cast<BCChoose*>(bContext) != nullptr );
956 }
957 else // if we are here that it IS first LE call
958 if ( isQuickClashLE(cur) )
959 return true;
960
961 // initial phase: choose-rule, NN-rule
962 if ( needInit )
963 {
964 // check if we have Qualified NR
965 if ( C != bpTOP )
966 switchResult ( commonTacticBodyChoose ( R, C ) );
967
968 // check whether we need to apply NN rule first
969 if ( isNNApplicable ( R, C, /*stopper=*/curConcept.bp()+(BipolarPointer)cur.getNumberLE() ) )
970 return commonTacticBodyNN(cur); // after application <=-rule would be checked again
971 }
972
973 // we need to repeat merge until there will be necessary amount of edges
974 for (;;)
975 {
976 if ( isFirstBranchCall() )
977 if ( initLEProcessing(cur) )
978 return false;
979
980 BCLE<DlCompletionTreeArc>* bcLE = static_cast<BCLE<DlCompletionTreeArc>*>(bContext);
981
982 if ( bcLE->noMoreLEOptions() )
983 { // set global clashset to cumulative one from previous branch failures
984 useBranchDep();
985 return true;
986 }
987
988 // get from- and to-arcs using corresponding indexes in Edges
989 DlCompletionTreeArc* fromArc = bcLE->getFrom();
990 DlCompletionTreeArc* toArc = bcLE->getTo();
991 DlCompletionTree* from = fromArc->getArcEnd();
992 DlCompletionTree* to = toArc->getArcEnd();
993
994 DepSet dep; // empty dep-set
995 // fast check for FROM =/= TO
996 if ( CGraph.nonMergeable ( from, to, dep ) )
997 {
998 // need this for merging two nominal nodes
999 dep.add(fromArc->getDep());
1000 dep.add(toArc->getDep());
1001 // add dep-set from labels
1002 if ( C == bpTOP ) // dep-set is known now
1003 setClashSet(dep);
1004 else // QCR: update dep-set wrt C
1005 {
1006 // here we know that C is in both labels; set a proper clash-set
1007 bool isComplex = CGLabel::isComplexConcept(DLHeap[C].Type());
1008 bool test {false};
1009
1010 // here dep contains the clash-set
1011 test = findConceptClash ( from->label().getLabel(isComplex), C, dep );
1012 fpp_assert(test);
1013 dep += getClashSet(); // save new dep-set
1014
1015 test = findConceptClash ( to->label().getLabel(isComplex), C, dep );
1016 fpp_assert(test);
1017 // both clash-sets are now in common clash-set
1018 }
1019
1020 nextBranchingOption();
1021 fpp_assert(!isFirstBranchCall());
1022 continue;
1023 }
1024
1025 save();
1026
1027 // add depset from current level and FROM arc and to current dep.set
1028 DepSet curDep(getCurDepSet());
1029 curDep.add(fromArc->getDep());
1030
1031 switchResult ( Merge ( from, to, curDep ) );
1032 // it might be the case (see bIssue28) that after the merge there is an R-neighbour
1033 // that have neither C or ~C in its label (it was far in the nominal cloud)
1034 if ( C != bpTOP )
1035 switchResult ( commonTacticBodyChoose ( R, C ) );
1036 }
1037 }
1038
1039 bool
1040 DlSatTester :: initLEProcessing ( const DLVertex& cur )
1041 {
1042 DepSet dep;
1043 // check the amount of neighbours we have
1044 findNeighbours ( cur.getRole(), cur.getC(), dep );
1045
1046 // if the number of R-neighbours satisfies condition -- nothing to do
1047 if ( EdgesToMerge.size() <= cur.getNumberLE() )
1048 return true;
1049
1050 // init context
1051 createBCLE();
1052 bContext->branchDep += dep;
1053
1054 // setup BCLE
1055 BCLE<DlCompletionTreeArc>* bcLE = static_cast<BCLE<DlCompletionTreeArc>*>(bContext);
1056
1057 bcLE->ItemsToMerge.swap(EdgesToMerge);
1058 bcLE->resetMCI();
1059 return false;
1060 }
1061
1062
1063 bool DlSatTester :: commonTacticBodyGE ( const DLVertex& cur ) // for >=nR.C concepts
1064 {
1065 #ifdef ENABLE_CHECKING
1066 fpp_assert ( isNegative(curConcept.bp()) && cur.Type() == dtLE );
1067 #endif
1068
1069 // check blocking conditions
1070 if ( isCurNodeBlocked() )
1071 return false;
1072
1073 const TRole* R = cur.getRole();
1074
1075 if ( unlikely(R->isTop()) )
1076 return processTopRoleGE(cur);
1077
1078 incStat(nGeCalls);
1079
1080 if ( isQuickClashGE(cur) )
1081 return true;
1082
1083 // create N new different edges
1084 return createDifferentNeighbours ( R, cur.getC(), curConcept.getDep(), cur.getNumberGE(), BlockableLevel );
1085 }
1086
1087 //-------------------------------------------------------------------------------
1088 // Func/LE/GE with top role processing
1089 //-------------------------------------------------------------------------------
1090
1091 bool
1092 DlSatTester :: processTopRoleFunc ( const DLVertex& cur ) // for <=1 R concepts
1093 {
1094 #ifdef ENABLE_CHECKING
1095 fpp_assert ( isPositive(curConcept.bp()) && isFunctionalVertex(cur) );
1096 #endif
1097
1098 incStat(nFuncCalls);
1099
1100 if ( isQuickClashLE(cur) )
1101 return true;
1102
1103 // locate all R-neighbours of curNode
1104 DepSet dummy; // not used
1105 findCLabelledNodes ( bpTOP, dummy );
1106
1107 // check if we have nodes to merge
1108 if ( NodesToMerge.size() < 2 )
1109 return false;
1110
1111 // merge all nodes to the first (the least wrt nominal hierarchy) found node
1112 NodeVector::iterator q = NodesToMerge.begin(), q_end = NodesToMerge.end();
1113 DlCompletionTree* sample = *q;
1114 DepSet dep (curConcept.getDep()); // dep-set for merging
1115
1116 // merge all elements to sample (sample wouldn't be merge)
1117 while ( ++q != q_end )
1118 // during merge EdgesToMerge may became purged (see Nasty4) => check this
1119 if ( !(*q)->isPBlocked() )
1120 switchResult ( Merge ( *q, sample, dep ) );
1121
1122 return false;
1123 }
1124
1125 bool
1126 DlSatTester :: processTopRoleLE ( const DLVertex& cur ) // for <=nR.C concepts
1127 {
1128 #ifdef ENABLE_CHECKING
1129 fpp_assert ( isPositive(curConcept.bp()) && ( cur.Type() == dtLE ) );
1130 #endif
1131
1132 BipolarPointer C = cur.getC();
1133 bool needInit = true;
1134
1135 if ( !isFirstBranchCall() )
1136 {
1137 if ( dynamic_cast<BCLE<DlCompletionTree>*>(bContext) != nullptr )
1138 needInit = false; // clash in LE-rule: skip the initial checks
1139 else // the only possible case is choose-rule; in this case just continue
1140 fpp_assert ( dynamic_cast<BCChoose*>(bContext) != nullptr );
1141 }
1142 else // if we are here that it IS first LE call
1143 if ( isQuickClashLE(cur) )
1144 return true;
1145
1146 // initial phase: choose-rule, NN-rule
1147 if ( needInit )
1148 {
1149 // check if we have Qualified NR
1150 if ( C != bpTOP )
1151 switchResult ( applyChooseRuleGlobally(C) );
1152 }
1153
1154 // we need to repeat merge until there will be necessary amount of edges
1155 for (;;)
1156 {
1157 if ( isFirstBranchCall() )
1158 if ( initTopLEProcessing(cur) )
1159 return false;
1160
1161 BCLE<DlCompletionTree>* bcLE = static_cast<BCLE<DlCompletionTree>*>(bContext);
1162
1163 if ( bcLE->noMoreLEOptions() )
1164 { // set global clashset to cumulative one from previous branch failures
1165 useBranchDep();
1166 return true;
1167 }
1168
1169 // get from- and to-arcs using corresponding indexes in Edges
1170 DlCompletionTree* from = bcLE->getFrom();
1171 DlCompletionTree* to = bcLE->getTo();
1172
1173 DepSet dep; // empty dep-set
1174 // fast check for FROM =/= TO
1175 if ( CGraph.nonMergeable ( from, to, dep ) )
1176 {
1177 // add dep-set from labels
1178 if ( C == bpTOP ) // dep-set is known now
1179 setClashSet(dep);
1180 else // QCR: update dep-set wrt C
1181 {
1182 // here we know that C is in both labels; set a proper clash-set
1183 bool isComplex = CGLabel::isComplexConcept(DLHeap[C].Type());
1184 bool test {false};
1185
1186 // here dep contains the clash-set
1187 test = findConceptClash ( from->label().getLabel(isComplex), C, dep );
1188 fpp_assert(test);
1189 dep += getClashSet(); // save new dep-set
1190
1191 test = findConceptClash ( to->label().getLabel(isComplex), C, dep );
1192 fpp_assert(test);
1193 // both clash-sets are now in common clash-set
1194 }
1195
1196 nextBranchingOption();
1197 fpp_assert(!isFirstBranchCall());
1198 continue;
1199 }
1200
1201 save();
1202
1203 // add depset from current level and FROM arc and to current dep.set
1204 DepSet curDep(getCurDepSet());
1205
1206 switchResult ( Merge ( from, to, curDep ) );
1207 }
1208 }
1209
1210 bool
1211 DlSatTester :: processTopRoleGE ( const DLVertex& cur ) // for >=nR.C concepts
1212 {
1213 #ifdef ENABLE_CHECKING
1214 fpp_assert ( isNegative(curConcept.bp()) && cur.Type() == dtLE );
1215 fpp_assert ( !isCurNodeBlocked() );
1216 #endif
1217
1218 incStat(nGeCalls);
1219
1220 if ( isQuickClashGE(cur) )
1221 return true;
1222
1223 // create N new different edges
1224 // FIXME!! for now
1225 return createDifferentNeighbours ( cur.getRole(), cur.getC(), curConcept.getDep(), cur.getNumberGE(), BlockableLevel );
1226 }
1227
1228 bool
1229 DlSatTester :: initTopLEProcessing ( const DLVertex& cur )
1230 {
1231 DepSet dep;
1232 // check the amount of neighbours we have
1233 findCLabelledNodes ( cur.getC(), dep );
1234
1235 // if the number of R-neighbours satisfies condition -- nothing to do
1236 if ( NodesToMerge.size() <= cur.getNumberLE() )
1237 return true;
1238
1239 // init context
1240 createBCTopLE();
1241 bContext->branchDep += dep;
1242
1243 // setup BCLE
1244 BCLE<DlCompletionTree>* bcLE = static_cast<BCLE<DlCompletionTree>*>(bContext);
1245
1246 bcLE->ItemsToMerge.swap(NodesToMerge);
1247 bcLE->resetMCI();
1248 return false;
1249 }
1250
1251
1252 //-------------------------------------------------------------------------------
1253 // Support for Func/LE/GE processing
1254 //-------------------------------------------------------------------------------
1255
1256 /// create N R-neighbours of curNode with given Nominal LEVEL labelled with C
1257 bool DlSatTester :: createDifferentNeighbours ( const TRole* R, BipolarPointer C, const DepSet& dep,
1258 unsigned int n, CTNominalLevel level )
1259 {
1260 // create N new edges with the same IR
1261 DlCompletionTreeArc* pA = nullptr;
1262 CGraph.initIR();
1263 for ( unsigned int i = 0; i < n; ++i )
1264 {
1265 pA = createOneNeighbour ( R, dep, level );
1266 DlCompletionTree* child = pA->getArcEnd();
1267
1268 // make CHILD different from other created nodes
1269 // don't care about return value as clash can't occur
1270 CGraph.setCurIR ( child, dep );
1271
1272 // add necessary new node labels and setup new edge
1273 switchResult ( initNewNode ( child, dep, C ) );
1274 switchResult ( setupEdge ( pA, dep, redoForall ) );
1275 }
1276 CGraph.finiIR();
1277
1278 // re-apply all <= NR in curNode; do it only once for all created nodes; no need for Irr
1279 return applyUniversalNR ( curNode, pA, dep, redoFunc|redoAtMost );
1280 }
1281
1282 /// check if ATLEAST and ATMOST restrictions are in clash; setup depset from CUR
1283 bool DlSatTester :: isNRClash ( const DLVertex& atleast, const DLVertex& atmost, const ConceptWDep& reason )
1284 {
1285 if ( atmost.Type() != dtLE || atleast.Type() != dtLE )
1286 return false;
1287 if ( !checkNRclash ( atleast, atmost ) ) // no clash between them
1288 return false;
1289
1290 // clash exists: create dep-set
1291 setClashSet ( curConcept.getDep() + reason.getDep() );
1292
1293 // log clash reason
1294 if ( LLM.isWritable(llGTA) )
1295 logClash ( curNode, reason );
1296
1297 return true;
1298 }
1299
1300 /// aux method that checks whether clash occurs during the merge of labels
1301 bool
1302 DlSatTester :: checkMergeClash ( const CGLabel& from, const CGLabel& to, const DepSet& dep, unsigned int nodeId )
1303 {
1304 CGLabel::const_iterator p, p_end;
1305 DepSet clashDep(dep);
1306 bool clash = false;
1307 for ( p = from.begin_sc(), p_end = from.end_sc(); p < p_end; ++p )
1308 if ( isUsed(inverse(p->bp()))
1309 && findConceptClash ( to.getLabel(/*isComplex=*/false), inverse(*p) ) )
1310 {
1311 clash = true;
1312 clashDep.add(getClashSet());
1313 // log the clash
1314 if ( LLM.isWritable(llGTA) )
1315 LL << " x(" << nodeId << "," << p->bp() << getClashSet()+dep << ")";
1316 }
1317 for ( p = from.begin_cc(), p_end = from.end_cc(); p < p_end; ++p )
1318 if ( isUsed(inverse(p->bp()))
1319 && findConceptClash ( to.getLabel(/*isComplex=*/true), inverse(*p) ) )
1320 {
1321 clash = true;
1322 clashDep.add(getClashSet());
1323 // log the clash
1324 if ( LLM.isWritable(llGTA) )
1325 LL << " x(" << nodeId << "," << p->bp() << getClashSet()+dep << ")";
1326 }
1327 if ( clash )
1328 setClashSet(clashDep);
1329 return clash;
1330 }
1331
1332 bool DlSatTester :: mergeLabels ( const CGLabel& from, DlCompletionTree* to, const DepSet& dep )
1333 {
1334 CGLabel::const_iterator p, p_end;
1335 CGLabel& lab(to->label());
1336 // arrays for simple- and complex concepts in the merged-to vector
1337 CWDArray& sc(lab.getLabel(/*isComplex=*/false));
1338 CWDArray& cc(lab.getLabel(/*isComplex=*/true));
1339
1340 // due to merging, all the concepts in the TO label
1341 // should be updated to the new dep-set DEP
1342 // TODO!! check whether this is really necessary
1343 for ( p = sc.begin(), p_end = sc.end(); p < p_end; ++p )
1344 CGraph.saveRareCond ( sc.updateDepSet ( p->bp(), dep ) );
1345 for ( p = cc.begin(), p_end = cc.end(); p < p_end; ++p )
1346 CGraph.saveRareCond ( cc.updateDepSet ( p->bp(), dep ) );
1347
1348 // if the concept is already exists in the node label --
1349 // we still need to update it with a new dep-set (due to merging)
1350 // note that DEP is already there
1351 for ( p = from.begin_sc(), p_end = from.end_sc(); p < p_end; ++p )
1352 if ( findConcept ( sc, *p ) )
1353 CGraph.saveRareCond ( sc.updateDepSet ( p->bp(), p->getDep() ) );
1354 else
1355 switchResult ( insertToDoEntry ( to, ConceptWDep(*p,dep), DLHeap[*p].Type(), "M" ) );
1356 for ( p = from.begin_cc(), p_end = from.end_cc(); p < p_end; ++p )
1357 if ( findConcept ( cc, *p ) )
1358 CGraph.saveRareCond ( cc.updateDepSet ( p->bp(), p->getDep() ) );
1359 else
1360 switchResult ( insertToDoEntry ( to, ConceptWDep(*p,dep), DLHeap[*p].Type(), "M" ) );
1361
1362 return false;
1363 }
1364
1365 bool DlSatTester :: Merge ( DlCompletionTree* from, DlCompletionTree* to, const DepSet& depF )
1366 {
1367 // if node is already purged -- nothing to do
1368 fpp_assert ( !from->isPBlocked() );
1369
1370 // prevent node to be merged to itself
1371 fpp_assert ( from != to );
1372
1373 // never merge nominal node to blockable one
1374 fpp_assert ( to->getNominalLevel() <= from->getNominalLevel() );
1375
1376 if ( LLM.isWritable(llGTA) )
1377 LL << " m(" << from->getId() << "->" << to->getId() << ")";
1378
1379 incStat(nMergeCalls);
1380
1381 // can't merge 2 nodes which are in inequality relation
1382 DepSet dep(depF);
1383 if ( CGraph.nonMergeable ( from, to, dep ) )
1384 {
1385 setClashSet(dep);
1386 return true;
1387 }
1388
1389 // check for the clash before doing anything else
1390 if ( checkMergeClash ( from->label(), to->label(), depF, to->getId() ) )
1391 return true;
1392
1393 // copy all node labels
1394 switchResult ( mergeLabels ( from->label(), to, depF ) );
1395
1396 // correct graph structure
1397 typedef std::vector<DlCompletionTreeArc*> edgeVector;
1398 edgeVector edges;
1399 CGraph.Merge ( from, to, depF, edges );
1400
1401 // check whether a disjoint roles lead to clash
1402 for ( const auto& edge: edges )
1403 if ( edge->getRole()->isDisjoint() &&
1404 checkDisjointRoleClash ( edge->getReverse()->getArcEnd(), edge->getArcEnd(), edge->getRole(), depF ) )
1405 return true;
1406
1407 // nothing more to do with data nodes
1408 if ( to->isDataNode() ) // data concept -- run data center for it
1409 return checkDataClash(to);
1410
1411 // for every node added to TO, every ALL, Irr and <=-node should be checked
1412 for ( const auto& edge: edges )
1413 switchResult ( applyUniversalNR ( to, edge, depF, redoEverything ) );
1414
1415 // we do real action here, so the return value
1416 return false;
1417 }
1418
1419 bool
1420 DlSatTester :: checkDisjointRoleClash ( const DlCompletionTree* from, const DlCompletionTree* to,
1421 const TRole* R, const DepSet& dep )
1422 {
1423 // try to check whether link from->to labelled with something disjoint with R
1424 for ( const auto& edge: *from )
1425 if ( checkDisjointRoleClash ( edge, to, R, dep ) )
1426 return true;
1427 return false;
1428 }
1429
1430 // compare 2 CT edges wrt blockable/nominal nodes at their ends
1431 class EdgeCompare
1432 {
1433 public:
1434 bool operator() ( DlCompletionTreeArc* pa, DlCompletionTreeArc* qa ) const
1435 { return *(pa)->getArcEnd() < *(qa)->getArcEnd(); }
1436 }; // EdgeCompare
1437
1438 /// aux method to check whether edge ended to NODE should be added to EdgesToMerge
1439 template <typename Iterator>
1440 bool isNewEdge ( const DlCompletionTree* node, Iterator begin, Iterator end )
1441 {
1442 for ( Iterator q = begin; q != end; ++q )
1443 if ( (*q)->getArcEnd() == node ) // skip edges to the same node
1444 return false;
1445
1446 return true;
1447 }
1448
1449 void
1450 DlSatTester :: findNeighbours ( const TRole* Role, BipolarPointer C, DepSet& Dep )
1451 {
1452 EdgesToMerge.clear();
1453 bool isComplex = CGLabel::isComplexConcept(DLHeap[C].Type());
1454
1455 for ( auto& edge: *curNode )
1456 if ( edge->isNeighbour(Role)
1457 && isNewEdge ( edge->getArcEnd(), EdgesToMerge.begin(), EdgesToMerge.end() )
1458 && findChooseRuleConcept ( edge->getArcEnd()->label().getLabel(isComplex), C, Dep ) )
1459 EdgesToMerge.push_back(edge);
1460
1461 // sort EdgesToMerge: From named nominals to generated nominals to blockable nodes
1462 std::sort ( EdgesToMerge.begin(), EdgesToMerge.end(), EdgeCompare() );
1463 }
1464
1465 class NodeCompare
1466 {
1467 public:
1468 bool operator() ( DlCompletionTree* p, DlCompletionTree* q ) const { return *p < *q; }
1469 };
1470
1471 void
1472 DlSatTester :: findCLabelledNodes ( BipolarPointer C, DepSet& Dep )
1473 {
1474 NodesToMerge.clear();
1475 bool isComplex = CGLabel::isComplexConcept(DLHeap[C].Type());
1476
1477 // FIXME!! do we need this for d-blocked nodes?
1478 for ( auto& node: CGraph )
1479 if ( isNodeGloballyUsed(node) && findChooseRuleConcept ( node->label().getLabel(isComplex), C, Dep ) )
1480 NodesToMerge.push_back(node);
1481
1482 // sort EdgesToMerge: From named nominals to generated nominals to blockable nodes
1483 std::sort ( NodesToMerge.begin(), NodesToMerge.end(), NodeCompare() );
1484 }
1485
1486
1487 //-------------------------------------------------------------------------------
1488 // Choose-rule processing
1489 //-------------------------------------------------------------------------------
1490
1491 bool DlSatTester :: commonTacticBodyChoose ( const TRole* R, BipolarPointer C )
1492 {
1493 // apply choose-rule for every R-neighbour
1494 for ( auto& edge: *curNode )
1495 {
1496 if ( edge->isNeighbour(R) )
1497 switchResult ( applyChooseRule ( edge->getArcEnd(), C ) );
1498 }
1499
1500 return false;
1501 }
1502
1503 /// apply choose-rule for all vertices (necessary for Top role in QCR)
1504 bool
1505 DlSatTester :: applyChooseRuleGlobally ( BipolarPointer C )
1506 {
1507 for ( auto& node: CGraph )
1508 {
1509 if ( isObjectNodeUnblocked(node) ) // FIXME!! think about d-blocked nodes
1510 switchResult ( applyChooseRule ( node, C ) );
1511 }
1512
1513 return false;
1514 }
1515
1516 //-------------------------------------------------------------------------------
1517 // Support for choose-rule processing
1518 //-------------------------------------------------------------------------------
1519
1520 /// apply choose-rule to given node
1521 bool DlSatTester :: applyChooseRule ( DlCompletionTree* node, BipolarPointer C )
1522 {
1523 if ( node->isLabelledBy(C) || node->isLabelledBy(inverse(C)) )
1524 return false;
1525
1526 // now node will be labelled with ~C or C
1527 if ( isFirstBranchCall() )
1528 {
1529 createBCCh();
1530 // save current state
1531 save();
1532
1533 return addToDoEntry ( node, inverse(C), getCurDepSet(), "cr0" );
1534 }
1535 else
1536 {
1537 prepareBranchDep();
1538 DepSet dep(getBranchDep());
1539 determiniseBranchingOp();
1540 return addToDoEntry ( node, C, dep, "cr1" );
1541 }
1542 }
1543
1544 //-------------------------------------------------------------------------------
1545 // NN rule processing
1546 //-------------------------------------------------------------------------------
1547
1548 bool DlSatTester :: commonTacticBodyNN ( const DLVertex& cur ) // NN-rule
1549 {
1550 // here we KNOW that NN-rule is applicable, so skip some tests
1551 incStat(nNNCalls);
1552
1553 if ( isFirstBranchCall() )
1554 createBCNN();
1555
1556 const BCNN* bcNN = static_cast<BCNN*>(bContext);
1557
1558 // check whether we did all possible tries
1559 if ( bcNN->noMoreNNOptions(cur.getNumberLE()) )
1560 { // set global clashset to cumulative one from previous branch failures
1561 useBranchDep();
1562 return true;
1563 }
1564
1565 // take next NN number; save it as SAVE() will reset it to 0
1566 unsigned int NN = bcNN->value;
1567
1568 // prepare to addition to the label
1569 save();
1570
1571 // new (just branched) dep-set
1572 const DepSet curDep(getCurDepSet());
1573
1574 // make a stopper to mark that NN-rule is applied
1575 switchResult ( addToDoEntry ( curNode, curConcept.bp() + (BipolarPointer)cur.getNumberLE(), DepSet(), "NNs" ) );
1576
1577 // create curNN new different edges
1578 switchResult ( createDifferentNeighbours ( cur.getRole(), cur.getC(), curDep, NN, curNode->getNominalLevel()+1 ) );
1579
1580 // now remember NR we just created: it is (<= curNN R), so have to find it
1581 return addToDoEntry ( curNode, curConcept.bp() + (BipolarPointer)(cur.getNumberLE()-NN), curDep, "NN" );
1582 }
1583
1584 //-------------------------------------------------------------------------------
1585 // Support for NN rule processing
1586 //-------------------------------------------------------------------------------
1587
1588 /// @return true iff NN-rule wrt (<= R) is applicable to the curNode
1589 bool DlSatTester :: isNNApplicable ( const TRole* r, BipolarPointer C, BipolarPointer stopper ) const
1590 {
1591 // NN rule is only applicable to nominal nodes
1592 if ( !curNode->isNominalNode() )
1593 return false;
1594
1595 // check whether the NN-rule was already applied here for a given concept
1596 if ( isUsed(stopper) && curNode->isLabelledBy(stopper) )
1597 return false;
1598
1599 // check for the real applicability of the NN-rule here
1600 for ( const auto& edge: *curNode )
1601 {
1602 const DlCompletionTree* suspect = edge->getArcEnd();
1603
1604 // if there is an edge that require to run the rule, then we need it
1605 if ( edge->isPredEdge() && suspect->isBlockableNode() && edge->isNeighbour(r) && suspect->isLabelledBy(C) )
1606 {
1607 if ( LLM.isWritable(llGTA) )
1608 LL << " NN(" << suspect->getId() << ")";
1609
1610 return true;
1611 }
1612 }
1613
1614 // can't apply NN-rule
1615 return false;
1616 }
1617
1618 //-------------------------------------------------------------------------------
1619 // Support for (\neg) \E R.Self
1620 //-------------------------------------------------------------------------------
1621
1622 bool DlSatTester :: commonTacticBodySomeSelf ( const TRole* R )
1623 {
1624 // check blocking conditions
1625 if ( isCurNodeBlocked() )
1626 return false;
1627
1628 // nothing to do if R-loop already exists
1629 for ( const auto& edge: *curNode )
1630 if ( edge->getArcEnd() == curNode && edge->isNeighbour(R) )
1631 return false;
1632
1633 // create an R-loop through curNode
1634 const DepSet& dep = curConcept.getDep();
1635 DlCompletionTreeArc* pA = CGraph.createLoop ( curNode, R, dep );
1636 return setupEdge ( pA, dep, redoEverything );
1637 }
1638
1639 bool DlSatTester :: commonTacticBodyIrrefl ( const TRole* R )
1640 {
1641 // return clash if R-loop is found
1642 for ( const auto& edge: *curNode )
1643 if ( checkIrreflexivity ( edge, R, curConcept.getDep() ) )
1644 return true;
1645
1646 return false;
1647 }
1648
1649 //-------------------------------------------------------------------------------
1650 // Support for projection R\C -> P
1651 //-------------------------------------------------------------------------------
1652
1653 bool DlSatTester :: commonTacticBodyProj ( const TRole* R, BipolarPointer C, const TRole* ProjR )
1654 {
1655 // find an R-edge, try to apply projection-rule to it
1656
1657 // if ~C is in the label of curNode, do nothing
1658 if ( curNode->isLabelledBy(inverse(C)) )
1659 return false;
1660
1661 // checkProjection() might change curNode's edge vector and thus invalidate iterators
1662 DlCompletionTree::const_edge_iterator p = curNode->begin(), p_end = curNode->end();
1663
1664 for ( std::ptrdiff_t i = 0, n = p_end - p; i < n; ++i )
1665 {
1666 p = curNode->begin() + i;
1667 if ( (*p)->isNeighbour(R) )
1668 switchResult ( checkProjection ( *p, C, ProjR ) );
1669 }
1670
1671 return false;
1672 }
1673
1674 bool DlSatTester :: checkProjection ( DlCompletionTreeArc* pA, BipolarPointer C, const TRole* ProjR )
1675 {
1676 // nothing to do if pA is labelled by ProjR as well
1677 if ( pA->isNeighbour(ProjR) )
1678 return false;
1679 // if ~C is in the label of curNode, do nothing
1680 if ( curNode->isLabelledBy(inverse(C)) )
1681 return false;
1682
1683 // neither C nor ~C are in the label: make a choice
1684 DepSet dep(curConcept.getDep());
1685 dep.add(pA->getDep());
1686
1687 if ( !curNode->isLabelledBy(C) )
1688 {
1689 if ( isFirstBranchCall() )
1690 {
1691 createBCCh();
1692 // save current state
1693 save();
1694
1695 return addToDoEntry ( curNode, inverse(C), getCurDepSet(), "cr0" );
1696 }
1697 else
1698 {
1699 prepareBranchDep();
1700 dep.add(getBranchDep());
1701 determiniseBranchingOp();
1702 switchResult ( addToDoEntry ( curNode, C, dep, "cr1" ) );
1703 }
1704 }
1705 // here C is in the label of curNode: add ProjR to the edge if necessary
1706 DlCompletionTree* child = pA->getArcEnd();
1707 pA = CGraph.addRoleLabel ( curNode, child, pA->isPredEdge(), ProjR, dep );
1708 return setupEdge ( pA, dep, redoEverything );
1709 }