Debian Codegraph @

Kernel/Kernel.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 Copyright (C) 2021-2024 Ivan Rygaev and Tilde MI
5
6 This library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
10
11 This library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public
17 License along with this library; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 #include "Kernel.h"
22 #include "tOntologyLoader.h"
23 #include "tOntologyPrinterLISP.h"
24 #include "AtomicDecomposer.h"
25 #include "OntologyBasedModularizer.h"
26 #include "eFPPSaveLoad.h"
27 #include "SaveLoadManager.h"
28 #include "TripleGatherer.h"
29
30 const char* ReasoningKernel :: Version = "1.8.2-SNAPSHOT";
31 const char* ReasoningKernel :: SupportedDL = "SROIQ(D)";
32 const char* ReasoningKernel :: Copyright =
33 "Copyright (C) Dmitry Tsarkov, Ivan Rygaev, 2002-2024";
34 const char* ReasoningKernel :: ReleaseDate = "2 February 2024";
35
36 // print the FaCT++ information only once
37 static bool KernelFirstRun = true;
38
39 // debug related individual/values switch
40 //#define FPP_DEBUG_PRINT_RELATED_PROGRESS
41
42 ReasoningKernel :: ReasoningKernel ( void )
43 {
44 // Intro
45 if ( KernelFirstRun )
46 {
47 std::cerr << "FaCT++.Kernel: Reasoner for the " << SupportedDL << " Description Logic, " << 8*sizeof(void*) << "-bit\n"
48 << Copyright << ". Version " << Version << " (" << ReleaseDate << ")\n";
49 KernelFirstRun = false;
50 }
51
52 // init option set (fill with options):
53 if ( initOptions () )
54 throw EFaCTPlusPlus("FaCT++ kernel: Cannot init options");
55 }
56
57 /// d'tor
58 ReasoningKernel :: ~ReasoningKernel()
59 {
60 clearTBox();
61 deleteTree(cachedQueryTree);
62 delete pMonitor;
63 delete pSLManager;
64 for ( NameSigMap::iterator p = Name2Sig.begin(), p_end = Name2Sig.end(); p != p_end; ++p )
65 delete p->second;
66 }
67
68 /// clear TBox and related structures; keep ontology in place
69 void
70 ReasoningKernel :: clearTBox ( void )
71 {
72 delete pTBox;
73 pTBox = nullptr;
74 delete pET;
75 pET = nullptr;
76 delete KE;
77 KE = nullptr;
78 delete AD;
79 AD = nullptr;
80 delete ModSem;
81 ModSem = nullptr;
82 delete ModSyn;
83 ModSyn = nullptr;
84 delete ModSynCount;
85 ModSynCount = nullptr;
86 // during preprocessing the TBox names were cached. clear that cache now.
87 getExpressionManager()->clearNameCache();
88 }
89
90 bool
91 ReasoningKernel :: needForceReload ( void ) const
92 {
93 // if no TBox known -- reload
94 if ( pTBox == nullptr )
95 return true;
96 // if ontology wasn't change -- no need to reload
97 if ( !Ontology.isChanged() )
98 return false;
99 // no incremental required -- nothing to do
100 if ( !useIncrementalReasoning )
101 return true;
102 return false;
103 }
104
105 /// force the re-classification of the changed ontology
106 void
107 ReasoningKernel :: forceReload ( void )
108 {
109 // reset TBox
110 clearTBox();
111 newKB();
112
113 // Protege (as the only user of non-trivial monitors with reload) does not accept multiple usage of a monitor
114 // so switch it off after the 1st usage
115 pMonitor = nullptr;
116
117 // (re)load ontology
118 TOntologyLoader OntologyLoader(*getTBox());
119 OntologyLoader.visitOntology(Ontology);
120
121 if ( dumpOntology )
122 dumpLISP(std::cout);
123 if ( useIncrementalReasoning )
124 initIncremental();
125
126 // after loading ontology became processed completely
127 Ontology.setProcessed();
128 }
129
130 void ReasoningKernel :: dumpLISP ( std::ostream& o_ ) {
131 TLISPOntologyPrinter OntologyPrinter(o_);
132 // First print all the declaration (as datarole declarations should be before usages)
133 OntologyPrinter.setPrintFlags(/*declarations=*/true, /*axioms=*/false);
134 Ontology.visitOntology(OntologyPrinter);
135 // Then print logical axioms
136 OntologyPrinter.setPrintFlags(/*declarations=*/false, /*axioms=*/true);
137 Ontology.visitOntology(OntologyPrinter);
138 }
139
140
141 //-------------------------------------------------
142 // Prepare reasoning/query
143 //-------------------------------------------------
144
145 void
146 ReasoningKernel :: ClassifyOrLoad ( bool needIndividuals )
147 {
148 if ( pSLManager != nullptr ) // try to load the taxonomy
149 {
150 if ( pSLManager->existsContent() )
151 { // previous version exists
152 try
153 {
154 Load(); // loaded => nothing to do
155 return;
156 }
157 catch ( const EFPPSaveLoad& )
158 {
159 // fail to load -- fall through to the real action
160 }
161 }
162 }
163 // perform the real classification
164 if ( needIndividuals )
165 pTBox->performRealisation();
166 else
167 pTBox->performClassification();
168
169 // save the result if necessary
170 if ( pSLManager != nullptr )
171 Save();
172 }
173
174 void
175 ReasoningKernel :: processKB ( KBStatus status )
176 {
177 fpp_assert ( status >= kbCChecked );
178
179 // check whether reasoning was failed
180 if ( reasoningFailed )
181 throw EFaCTPlusPlus("Can't answer queries due to previous errors");
182
183 KBStatus curStatus = getStatus();
184
185 if ( curStatus >= status )
186 { // nothing to do; but make sure that we are consistent
187 if ( !isKBConsistent() )
188 throw EFPPInconsistentKB();
189 return;
190 }
191
192 // here curStatus < kbRealised, and status >= kbChecked
193 if ( curStatus == kbEmpty || curStatus == kbLoading )
194 { // load and preprocess KB -- here might be failures
195 reasoningFailed = true;
196
197 // load the axioms from the ontology to the TBox
198 if ( needForceReload() )
199 forceReload();
200 else // just do incremental classification and exit
201 {
202 doIncremental();
203 reasoningFailed = false;
204 return;
205 }
206
207 // do the preprocessing and consistency check
208 pTBox->isConsistent();
209
210 // if there were no exception thrown -- clear the failure status
211 reasoningFailed = false;
212
213 // if the consistency check is all we need -- return
214 if ( status == kbCChecked )
215 return;
216 }
217
218 // here we need to do classification or realisation
219
220 if ( !pTBox->isConsistent() ) // nothing to do for inconsistent ontologies
221 return;
222
223 ClassifyOrLoad(status == kbRealised);
224 }
225
226 //-----------------------------------------------------------------------------
227 //-- query caching support
228 //-----------------------------------------------------------------------------
229
230 /// classify query; cache is ready at the point. NAMED means whether concept is just a name
231 void
232 ReasoningKernel :: classifyQuery ( void )
233 {
234 // make sure KB is classified
235 classifyKB();
236
237 // ... and the cache entry is properly cleared
238 fpp_assert ( cachedVertex == nullptr );
239
240 // classify general query expression
241 bool complexQuery = getTBox()->isComplexQuery(cachedConcept);
242 if ( complexQuery )
243 getTBox()->classifyQueryConcept();
244
245 // now cached concept is classified
246 cachedVertex = cachedConcept->getTaxVertex();
247
248 if ( unlikely(cachedVertex == nullptr) ) // fresh concept
249 {
250 fpp_assert (!complexQuery);
251 cachedVertex = getCTaxonomy()->getFreshVertex(cachedConcept);
252 }
253
254 // setup proper cache level
255 cacheLevel = csClassified;
256 }
257
258 void
259 ReasoningKernel :: setUpSatCache ( DLTree* query )
260 {
261 // if KB was changed since it was classified,
262 // we should catch it before
263 fpp_assert ( !Ontology.isChanged() );
264
265 // check if the query is already cached
266 if ( checkQueryCache(query) )
267 { // the level should be the same
268 fpp_assert ( cacheLevel == csSat );
269 // nothing to do
270 deleteTree(query);
271 return;
272 }
273
274 // clear currently cached query
275 clearQueryCache();
276
277 // setup concept to be queried
278 setQueryConcept(query);
279
280 // everything is fine -- set up cache now
281 setQueryCache(query);
282 cacheLevel = csSat;
283 }
284
285 void
286 ReasoningKernel :: setUpCache ( TConceptExpr* query, cacheStatus level )
287 {
288 // if KB was changed since it was classified,
289 // we should catch it before
290 fpp_assert ( !Ontology.isChanged() );
291
292 // check if the query is already cached
293 if ( checkQueryCache(query) )
294 {
295 fpp_assert ( cacheLevel != csEmpty );
296 // query cached with the same or lower level -- nothing to do
297 if ( level <= cacheLevel )
298 return;
299 // the only other option is: cache level is SAT but CLASSIFIED is requested
300 fpp_assert ( level == csClassified );
301 fpp_assert ( cacheLevel == csSat );
302 // classify query and return
303 classifyQuery();
304 return;
305 }
306
307 // clear currently cached query
308 clearQueryCache();
309
310 // setup concept to be queried
311 setQueryConcept(TreeDeleter(e(query)));
312
313 // everything is fine -- set up cache now
314 setQueryCache(query);
315 cacheLevel = csSat;
316
317 if ( level == csClassified )
318 classifyQuery();
319 }
320
321 //-------------------------------------------------
322 // concept subsumption query implementation
323 //-------------------------------------------------
324
325 /// class for exploring concept taxonomy to find super classes
326 class SupConceptActor: public WalkerInterface
327 {
328 protected:
329 const ClassifiableEntry* pe;
330 void entry ( const ClassifiableEntry* q ) { if ( pe == q ) throw std::exception(); }
331 public:
332 explicit SupConceptActor ( ClassifiableEntry* q ) : pe(q) {}
333 bool apply ( const TaxonomyVertex& v ) override
334 {
335 entry(v.getPrimer());
336 for ( const auto& synonym: v.synonyms() )
337 entry(synonym);
338 return true;
339 }
340 }; // SupConceptActor
341
342 /// @return true iff C [= D holds
343 bool
344 ReasoningKernel :: checkSub ( TConcept* C, TConcept* D )
345 {
346 // check whether a concept is fresh
347 if ( unlikely(!isValid(D->pName)) ) // D is fresh
348 {
349 if ( unlikely(!isValid(C->pName)) ) // C is fresh
350 return C == D; // 2 fresh concepts subsumes one another iff they are the same
351 else // C is known
352 return !getTBox()->isSatisfiable(C); // C [= D iff C=\bottom
353 }
354 else // D is known
355 if ( unlikely(!isValid(C->pName)) ) // C is fresh
356 // C [= D iff D = \top, or ~D = \bottom
357 return !checkSatTree(createSNFNot(getTBox()->getTree(C)));
358 // here C and D are known (not fresh)
359 // check the obvious ones
360 if ( unlikely(D->isTop()) || unlikely(C->isBottom()) )
361 return true;
362 if ( getStatus() < kbClassified ) // unclassified => do via SAT test
363 return getTBox()->isSubHolds ( C, D );
364 // classified => do the taxonomy traversal
365 SupConceptActor actor(D);
366 Taxonomy* tax = getCTaxonomy();
367 try { tax->getRelativesInfo</*needCurrent=*/true, /*onlyDirect=*/false, /*upDirection=*/true> ( C->getTaxVertex(), actor ); return false; }
368 catch (...) { tax->clearVisited(); return true; }
369 }
370
371 //-------------------------------------------------
372 // all-disjoint query implementation
373 //-------------------------------------------------
374
375 bool
376 ReasoningKernel :: isDisjointRoles ( void )
377 {
378 // grab all roles from the arg-list
379 typedef const std::vector<const TDLExpression*> TExprVec;
380 typedef std::vector<const TRole*> TRoleVec;
381 TExprVec Disj = getExpressionManager()->getArgList();
382 TRoleVec Roles;
383 Roles.reserve(Disj.size());
384 unsigned int nTopRoles = 0;
385
386 for ( auto* arg : Disj )
387 {
388 if ( TORoleExpr* ORole = dynamic_cast<TORoleExpr*>(arg) )
389 {
390 TRole* R = getRole ( ORole, "Role expression expected in isDisjointRoles()" );
391 if ( R->isBottom() )
392 continue; // empty role is disjoint with everything
393 if ( R->isTop() )
394 ++nTopRoles; // count universal roles
395 else
396 Roles.push_back(R);
397 }
398 else if ( TDRoleExpr* DRole = dynamic_cast<TDRoleExpr*>(arg) )
399 {
400 TRole* R = getRole ( DRole, "Role expression expected in isDisjointRoles()" );
401 if ( R->isBottom() )
402 continue; // empty role is disjoint with everything
403 if ( R->isTop() )
404 ++nTopRoles; // count universal roles
405 else
406 Roles.push_back(R);
407 }
408 else
409 throw EFaCTPlusPlus ( "Role expression expected in isDisjointRoles()" );
410 }
411
412 // deal with top-roles
413 if ( nTopRoles > 0 )
414 {
415 if ( nTopRoles > 1 || !Roles.empty() )
416 return false; // universal role is not disjoint with anything but the bottom role
417 else
418 return true;
419 }
420
421 // test pair-wise disjointness
422 TRoleVec::const_iterator q = Roles.begin(), q_end = Roles.end(), s;
423 for ( ; q != q_end; ++q )
424 for ( s = q+1; s != q_end; ++s )
425 if ( !getTBox()->isDisjointRoles(*q,*s) )
426 return false;
427
428 return true;
429 }
430
431 //-------------------------------------------------
432 // related individuals implementation
433 //-------------------------------------------------
434
435 class RIActor: public WalkerInterface
436 {
437 protected:
438 ReasoningKernel::CIVec acc;
439
440 /// process single entry in a vertex label
441 bool tryEntry ( const ClassifiableEntry* p )
442 {
443 // check the applicability
444 if ( p->isSystem() || !static_cast<const TConcept*>(p)->isSingleton() )
445 return false;
446
447 // print the concept
448 acc.push_back(static_cast<const TIndividual*>(p));
449 return true;
450 }
451
452 public:
453 RIActor() = default;
454
455 bool apply ( const TaxonomyVertex& v ) override
456 {
457 bool ret = tryEntry(v.getPrimer());
458
459 for ( const auto& synonym: v.synonyms() )
460 ret |= tryEntry(synonym);
461
462 return ret;
463 }
464 void clear ( void ) { acc.clear(); }
465 const ReasoningKernel::CIVec& getAcc ( void ) const { return acc; }
466 }; // RIActor
467
468 ReasoningKernel::CIVec
469 ReasoningKernel :: buildRelatedCache ( TIndividual* I, const TRole* R )
470 {
471 #ifdef FPP_DEBUG_PRINT_RELATED_PROGRESS
472 std::cout << "Related for " << I->getName() << " via property " << R->getName() << "\n";
473 #endif
474
475 // for synonyms: use the representative's cache
476 if ( R->isSynonym() )
477 return getRelated ( I, resolveSynonym(R) );
478
479 // FIXME!! return an empty set for data roles
480 if ( R->isDataRole() )
481 return CIVec();
482
483 // empty role has no fillers
484 if ( R->isBottom() )
485 return CIVec();
486
487 // now fills the query
488 RIActor actor;
489 // ask for instances of \exists R^-.{i}
490 TORoleExpr* InvR = R->getId() > 0
491 ? getExpressionManager()->Inverse(getExpressionManager()->ObjectRole(R->getName()))
492 : getExpressionManager()->ObjectRole(R->inverse()->getName());
493 TConceptExpr* query =
494 R->isTop() ? getExpressionManager()->Top() : // universal role has all the named individuals as a filler
495 getExpressionManager()->Value ( InvR, getExpressionManager()->Individual(I->getName()) );
496 getInstances ( query, actor );
497 return actor.getAcc();
498 }
499
500 /// @return in Rs all (DATA)-roles R s.t. (I,x):R; add inverses if NEEDI is true
501 void
502 ReasoningKernel :: getRelatedRoles ( const TIndividualExpr* I, NamesVector& Rs, bool data, bool needI )
503 {
504 realiseKB(); // ensure KB is ready to answer the query
505 Rs.clear();
506
507 TIndividual* i = getIndividual ( I, "individual name expected in the getRelatedRoles()" );
508 for ( const auto& R: (data ? getDRM() : getORM()) )
509 if ( ( R->getId() > 0 || needI ) && !getRelated(i,R).empty() )
510 Rs.push_back(R);
511 }
512
513 void
514 ReasoningKernel :: getRoleFillers ( const TIndividualExpr* I, const TORoleExpr* R, IndividualSet& Result )
515 {
516 CIVec vec = getRoleFillers( I, R );
517 for ( CIVec::iterator p = vec.begin(), p_end = vec.end(); p < p_end; ++p )
518 Result.push_back(const_cast<TIndividual*>(*p));
519 }
520 const ReasoningKernel::CIVec&
521 ReasoningKernel :: getRoleFillers ( const TIndividualExpr* I, const TORoleExpr* R)
522 {
523 realiseKB(); // ensure KB is ready to answer the query
524 return getRelated ( getIndividual ( I, "Individual name expected in the getRoleFillers()" ),
525 getRole ( R, "Role expression expected in the getRoleFillers()" ) );
526 }
527
528
529 /// @return true iff R(I,J) holds
530 bool
531 ReasoningKernel :: isRelated ( const TIndividualExpr* I, const TORoleExpr* R, const TIndividualExpr* J )
532 {
533 realiseKB(); // ensure KB is ready to answer the query
534 TIndividual* i = getIndividual ( I, "Individual name expected in the isRelated()" );
535 TRole* r = getRole ( R, "Role expression expected in the isRelated()" );
536 if ( r->isDataRole() )
537 return false; // FIXME!! not implemented
538
539 TIndividual* j = getIndividual ( J, "Individual name expected in the isRelated()" );
540 CIVec vec = getRelated ( i, r );
541 for ( CIVec::iterator p = vec.begin(), p_end = vec.end(); p < p_end; ++p )
542 if ( j == (*p) )
543 return true;
544
545 return false;
546 }
547
548 /// @return true iff A(I,V) holds
549 bool
550 ReasoningKernel :: isRelated ( const TIndividualExpr* I, const TDRoleExpr* A, const TDataValueExpr* V )
551 {
552 TDLConceptDataValue exists(A, V);
553 return isInstance(I, &exists);
554 }
555
556 TDLConceptExpression* ReasoningKernel::parseConcept(const std::string& name)
557 {
558 auto* em = getExpressionManager();
559
560 if (name == OWL_THING)
561 return em->Top();
562 else if (name == OWL_NOTHING)
563 return em->Bottom();
564 else
565 return em->Concept(name);
566 }
567
568 const TConcept* ReasoningKernel::getConcept(TDLIndividualName* i_exp, TDLConceptExpression* c_exp)
569 {
570 auto* em = getExpressionManager();
571
572 if (strlen(i_exp->getName()) == 0)
573 return nullptr;
574 else if (c_exp == em->Top())
575 return static_cast<const TConcept*>(getTBox()->getTaxonomy()->getTopVertex()->getPrimer());
576 else if (c_exp == em->Bottom())
577 return static_cast<const TConcept*>(getTBox()->getTaxonomy()->getBottomVertex()->getPrimer());
578 else if (i_exp->getEntry() != nullptr)
579 return static_cast<const TConcept*>(i_exp->getEntry());
580 else
581 return static_cast<const TConcept*>(static_cast<TDLConceptName*>(c_exp)->getEntry());
582 }
583
584 const TRole* ReasoningKernel::getRole(TDLObjectRoleExpression* o_exp, TDLDataRoleExpression* d_exp)
585 {
586 auto* em = getExpressionManager();
587 auto& orm = getORM();
588 auto& drm = getDRM();
589
590
591 if (o_exp == em->ObjectRoleTop())
592 {
593 return orm.getTopRole();
594 }
595 else if (o_exp == em->ObjectRoleBottom())
596 {
597 return orm.getBotRole();
598 }
599 else if (d_exp == em->DataRoleTop())
600 {
601 return drm.getTopRole();
602 }
603 else if (d_exp == em->DataRoleBottom())
604 {
605 return drm.getBotRole();
606 }
607 else
608 {
609 auto* o_name = static_cast<TDLObjectRoleName*>(o_exp);
610 auto* d_name = static_cast<TDLDataRoleName*>(d_exp);
611
612 if (strlen(o_name->getName()) == 0 || isSpecialRole(o_name->getName()))
613 return nullptr;
614 else if (o_name->getEntry() != nullptr)
615 return static_cast<TRole*>(o_name->getEntry());
616 else
617 return static_cast<TRole*>(d_name->getEntry());
618 }
619 }
620
621 TDLObjectRoleExpression* ReasoningKernel::parseObjectRole(const std::string& name)
622 {
623 auto* em = getExpressionManager();
624
625 if (name == OWL_TOP_OBJECT_PROPERTY)
626 return em->ObjectRoleTop();
627 else if (name == OWL_BOTTOM_OBJECT_PROPERTY)
628 return em->ObjectRoleBottom();
629 else
630 return em->ObjectRole(name);
631 }
632
633 TDLDataRoleExpression* ReasoningKernel::parseDataRole(const std::string& name)
634 {
635 auto* em = getExpressionManager();
636
637 if (name == OWL_TOP_DATA_PROPERTY)
638 return em->DataRoleTop();
639 else if (name == OWL_BOTTOM_DATA_PROPERTY)
640 return em->DataRoleBottom();
641 else
642 return em->DataRole(name);
643 }
644
645 const char* ReasoningKernel::getXsdType(const TDataEntry* dataValue) const
646 {
647 const char* typeName = dataValue->getType()->getName();
648
649 if (strcmp(typeName, "number") == 0)
650 return TDataTypeManager::getIntTypeName();
651 else if (strcmp(typeName, "real") == 0)
652 return TDataTypeManager::getRealTypeName();
653 else if (strcmp(typeName, "bool") == 0)
654 return TDataTypeManager::getBoolTypeName();
655 else
656 return "default";
657 }
658
659 void ReasoningKernel::getTriples(const std::string& q_subj_name, const std::string& q_role_name, const std::string& q_obj_name, std::set<std::vector<std::string>>& triples)
660 {
661 TBox* tBox = getTBox();
662 auto* em = getExpressionManager();
663
664 TDLIndividualName* q_subj_i_exp = em->Individual(q_subj_name);
665 TDLConceptExpression* q_subj_c_exp = parseConcept(q_subj_name);
666 TDLObjectRoleExpression* q_subj_o_role_exp = parseObjectRole(q_subj_name);
667 TDLDataRoleExpression* q_subj_d_role_exp = parseDataRole(q_subj_name);
668
669 TDLObjectRoleName* q_o_role_exp = em->ObjectRole(q_role_name);
670 TDLDataRoleName* q_d_role_exp = em->DataRole(q_role_name);
671
672 TDLIndividualName* q_obj_i_exp = em->Individual(q_obj_name);
673 TDLConceptExpression* q_obj_c_exp = parseConcept(q_obj_name);
674 TDLObjectRoleExpression* q_obj_o_role_exp = parseObjectRole(q_obj_name);
675 TDLDataRoleExpression* q_obj_d_role_exp = parseDataRole(q_obj_name);
676
677 const TConcept* q_subj = getConcept(q_subj_i_exp, q_subj_c_exp);
678 const TRole* q_subj_role = getRole(q_subj_o_role_exp, q_subj_d_role_exp);
679 const TRole* q_role = getRole(q_o_role_exp, q_d_role_exp);
680 const TConcept* q_obj = getConcept(q_obj_i_exp, q_obj_c_exp);
681 const TRole* q_obj_role = getRole(q_obj_o_role_exp, q_obj_d_role_exp);
682
683 // If subject or role is not found in the ontology, return an empty set
684 if ((!q_subj_name.empty() && q_subj == nullptr && q_subj_role == nullptr) || (!q_role_name.empty() && !isSpecialRole(q_role_name) && q_role == nullptr))
685 return;
686
687 if (q_subj_name.empty())
688 {
689 // Subject is not specified in the query
690
691 // If role and object are specified in the query process special roles separately
692 if (q_role_name == RDF_TYPE && q_obj != nullptr)
693 {
694 // All instances of a specific class
695
696 if (!q_obj->isSingleton())
697 {
698 TripleGatherer gatherer(&triples, false, RDF_TYPE, q_obj_name.c_str(), [](const ClassifiableEntry* entry)
699 {
700 return static_cast<const TConcept*>(entry)->isSingleton();
701 });
702
703 getInstances(q_obj_c_exp, gatherer);
704 }
705 else
706 {
707 return;
708 }
709 }
710 else if (q_role_name == RDFS_SUBCLASS_OF && q_obj != nullptr)
711 {
712 // All subclasses of a specific class
713
714 if (!q_obj->isSingleton())
715 {
716 TripleGatherer gatherer(&triples, false, RDFS_SUBCLASS_OF, q_obj_name.c_str(), [q_obj](const ClassifiableEntry* entry)
717 {
718 const TConcept* concept = static_cast<const TConcept*>(entry);
719 return entry != q_obj && !concept->isSingleton() && !concept->isBottom();
720 });
721
722 getSubConcepts(q_obj_c_exp, false, gatherer, true);
723 }
724 else
725 {
726 return;
727 }
728 }
729 else if (q_role_name == OWL_SAME_AS && q_obj != nullptr)
730 {
731 // All equivalent individuals to a specific individual
732
733 if (q_obj->isSingleton())
734 {
735 TripleGatherer gatherer(&triples, false, OWL_SAME_AS, q_obj_name.c_str(), [q_obj](const ClassifiableEntry* entry)
736 {
737 return entry != q_obj && static_cast<const TConcept*>(entry)->isSingleton();
738 });
739
740 getSameAs(q_obj_i_exp, gatherer);
741 }
742 else
743 {
744 return;
745 }
746 }
747 else if (q_role_name == OWL_EQUIVALENT_CLASS && q_obj != nullptr)
748 {
749 // All equivalent classes to a specific class
750
751 if (!q_obj->isSingleton())
752 {
753 TripleGatherer gatherer(&triples, false, OWL_EQUIVALENT_CLASS, q_obj_name.c_str(), [q_obj](const ClassifiableEntry* entry)
754 {
755 return entry != q_obj && !static_cast<const TConcept*>(entry)->isSingleton();
756 });
757
758 getEquivalentConcepts(q_obj_c_exp, gatherer);
759 }
760 else
761 {
762 return;
763 }
764 }
765 /*else if (q_role_name == OWL_DISJOINT_WITH)
766 {
767 // All disjoint classes with a specific class
768
769 if (!q_obj->isSingleton())
770 {
771 TripleGatherer gatherer(&triples, false, OWL_DISJOINT_WITH, q_obj_name.c_str(), [](const ClassifiableEntry* entry)
772 {
773 const TConcept* concept = static_cast<const TConcept*>(entry);
774 return !concept->isSingleton() && !concept->isSystem() && !concept->isBottom();
775 });
776
777 getDisjointConcepts(q_obj_c_exp, gatherer);
778 }
779 else
780 {
781 return;
782 }
783 }*/
784 else if (q_role_name == RDFS_SUBPROPERTY_OF && q_obj_role != nullptr)
785 {
786 // All subproperties of a specific property
787
788 TripleGatherer gatherer(&triples, false, RDFS_SUBPROPERTY_OF, q_obj_name.c_str(), [q_obj_role](const ClassifiableEntry* entry)
789 {
790 return entry != q_obj_role && static_cast<const TNamedEntry*>(entry)->getId() >= 0;
791 });
792
793 if (q_obj_role->isDataRole())
794 getSubRoles(q_obj_d_role_exp, false, gatherer);
795 else
796 getSubRoles(q_obj_o_role_exp, false, gatherer);
797 }
798 else if (q_role_name == OWL_EQUIVALENT_PROPERTY && q_obj_role != nullptr)
799 {
800 // All equivalent properties to a specific property
801
802 TripleGatherer gatherer(&triples, false, OWL_EQUIVALENT_PROPERTY, q_obj_name.c_str(), [q_obj_role](const ClassifiableEntry* entry)
803 {
804 return entry != q_obj_role && static_cast<const TNamedEntry*>(entry)->getId() >= 0;
805 });
806
807 if (q_obj_role->isDataRole())
808 getEquivalentRoles(q_obj_d_role_exp, gatherer);
809 else
810 getEquivalentRoles(q_obj_o_role_exp, gatherer);
811 }
812 else if (q_role_name == OWL_INVERSE_OF && q_obj_role != nullptr)
813 {
814 // Inverse property
815
816 if (!q_obj_role->isDataRole())
817 {
818 const auto* inverse_role = q_obj_role->inverse();
819
820 if (inverse_role != nullptr && inverse_role->getId() >= 0)
821 pushTriple(triples, toRdf(inverse_role->getName()), OWL_INVERSE_OF, q_obj_name.c_str());
822 }
823 }
824 else
825 {
826 // Otherwise take the list of all individuals, classes and roles and run the same query for each individual, class or role separately
827
828 for (auto p_concept = tBox->c_begin(); p_concept < tBox->c_end(); p_concept++)
829 {
830 const TConcept* concept = *p_concept;
831
832 if (!concept->isTop() && !concept->isBottom())
833 getTriples(concept->getName(), q_role_name, q_obj_name, triples);
834 }
835
836 for (auto p_ind = tBox->i_begin(); p_ind < tBox->i_end(); p_ind++)
837 {
838 const TIndividual* ind = *p_ind;
839 getTriples(ind->getName(), q_role_name, q_obj_name, triples);
840 }
841
842 const auto& orm = tBox->getORM();
843
844 for (auto p_role = orm.begin(); p_role < orm.end(); p_role++)
845 {
846 const TRole* role = *p_role;
847 getTriples(role->getName(), q_role_name, q_obj_name, triples);
848 }
849
850 const auto& drm = tBox->getDRM();
851
852 for (auto p_role = drm.begin(); p_role < drm.end(); p_role++)
853 {
854 const TRole* role = *p_role;
855 getTriples(role->getName(), q_role_name, q_obj_name, triples);
856 }
857 }
858 }
859 else if (q_subj != nullptr)
860 {
861 // Subject is specified in the query and it is not a role
862
863 if (q_subj->isSingleton())
864 {
865 // Query subject is an individual
866
867 const TIndividual* q_subj_ind = static_cast<const TIndividual*>(q_subj);
868
869 NamesVector roles;
870
871 // Fill required role list (either with all roles or with a single role from the query)
872 if (q_role_name.empty())
873 getRelatedRoles(q_subj_i_exp, roles, false, false);
874 else if (!isSpecialRole(q_role_name) && !q_role->isDataRole())
875 roles.push_back(q_role);
876
877 // If types are needed
878 if (q_role_name.empty() || q_role_name == RDF_TYPE)
879 {
880 if (q_obj_name.empty())
881 {
882 // All types
883 TripleGatherer gatherer(&triples, true, RDF_TYPE, q_subj_name.c_str(), [](const ClassifiableEntry* entry)
884 {
885 return !static_cast<const TConcept*>(entry)->isSingleton();
886 });
887
888 getTypes(q_subj_i_exp, false, gatherer);
889 }
890 else if (q_obj != nullptr && !q_obj->isSingleton())
891 {
892 // One specific type from the query
893 if (isInstance(q_subj_i_exp, q_obj_c_exp))
894 pushTriple(triples, q_subj_name.c_str(), RDF_TYPE, q_obj_name.c_str());
895 }
896 }
897
898 // If synonyms are needed
899 if (q_role_name.empty() || q_role_name == OWL_SAME_AS)
900 {
901 if (q_obj_name.empty())
902 {
903 // All synonyms
904 TripleGatherer gatherer(&triples, true, OWL_SAME_AS, q_subj_name.c_str(), [q_subj](const ClassifiableEntry* entry)
905 {
906 return entry != q_subj && static_cast<const TConcept*>(entry)->isSingleton();
907 });
908
909 getSameAs(q_subj_i_exp, gatherer);
910 }
911 else if (q_obj != nullptr && q_obj->isSingleton() && q_obj != q_subj)
912 {
913 // A single synonym to a specific individual from the query
914 if (isSameIndividuals(q_subj_i_exp, q_obj_i_exp))
915 pushTriple(triples, q_subj_name.c_str(), OWL_SAME_AS, q_obj_name.c_str());
916 }
917 }
918
919 // Gather object role values
920 for (const TNamedEntry* named_entry : roles)
921 {
922 const TRole* role = static_cast<const TRole*>(named_entry);
923
924 const TORoleExpr* role_exp = getExpressionManager()->ObjectRole(role->getName());
925
926 if (q_obj_name.empty())
927 {
928 // All values
929 for (const TIndividual* obj : getRoleFillers(q_subj_i_exp, role_exp))
930 pushTriple(triples, q_subj->getName(), role->getName(), obj->getName());
931 }
932 else if (q_obj != nullptr && q_obj->isSingleton())
933 {
934 // A specific value from the query
935 if (isRelated(q_subj_i_exp, role_exp, q_obj_i_exp))
936 pushTriple(triples, q_subj->getName(), role->getName(), q_obj->getName());
937 }
938 }
939
940 // Gather data role values
941 if (q_role_name.empty())
942 {
943 // All data roles
944 const TDataValueMap* dataValueMap = q_subj_ind->getDataValueMap();
945
946 for (const auto& pair : dataValueMap->getAllDataValues())
947 {
948 const TRole* role = pair.first;
949 const TDataValueMap::DEVec& values = pair.second;
950
951 for (const TDataEntry* value : values)
952 {
953 if (q_obj_name.empty() || value->getName() == q_obj_name)
954 pushTriple(triples, q_subj->getName(), role->getName(), value->getName(), getXsdType(value));
955 }
956 }
957 }
958 else if (q_subj_ind->hasDataValueCache(q_role))
959 {
960 // A specific data role from the query
961 for (const TDataEntry* value : q_subj_ind->getDataValueCache(q_role))
962 {
963 if (q_obj_name.empty() || value->getName() == q_obj_name)
964 pushTriple(triples, q_subj->getName(), q_role->getName(), value->getName(), getXsdType(value));
965 }
966 }
967 }
968 else
969 {
970 // Query subject is a class
971
972 if ((q_role_name.empty() || q_role_name == RDF_TYPE) && (q_obj_name.empty() || q_obj_name == OWL_CLASS))
973 pushTriple(triples, q_subj_name.c_str(), RDF_TYPE, OWL_CLASS);
974
975 // If superclasses are needed
976 if (!q_subj->isBottom() && (q_role_name.empty() || q_role_name == RDFS_SUBCLASS_OF))
977 {
978 if (q_obj_name.empty())
979 {
980 // All superclasses
981 TripleGatherer gatherer(&triples, true, RDFS_SUBCLASS_OF, q_subj_name.c_str(), [q_subj](const ClassifiableEntry* entry)
982 {
983 return entry != q_subj && !static_cast<const TConcept*>(entry)->isSingleton();
984 });
985
986 getSupConcepts(q_subj_c_exp, false, gatherer, true);
987 }
988 else if (q_obj != nullptr && !q_obj->isSingleton())
989 {
990 // A specific superclass from the query
991 if (q_obj != q_subj && isSubsumedBy(q_subj_c_exp, q_obj_c_exp))
992 pushTriple(triples, q_subj_name.c_str(), RDFS_SUBCLASS_OF, q_obj_name.c_str());
993 }
994 }
995
996 // If synonyms are needed
997 if (q_role_name.empty() || q_role_name == OWL_EQUIVALENT_CLASS)
998 {
999 if (q_obj_name.empty())
1000 {
1001 // All synonyms
1002 TripleGatherer gatherer(&triples, true, OWL_EQUIVALENT_CLASS, q_subj_name.c_str(), [q_subj](const ClassifiableEntry* entry)
1003 {
1004 return entry != q_subj && !static_cast<const TConcept*>(entry)->isSingleton();
1005 });
1006
1007 getEquivalentConcepts(q_subj_c_exp, gatherer);
1008 }
1009 else if (q_obj != nullptr && !q_obj->isSingleton() && q_obj != q_subj)
1010 {
1011 // A synonyms to a specific class from the query
1012 if (isEquivalent(q_subj_c_exp, q_obj_c_exp))
1013 pushTriple(triples, q_subj_name.c_str(), OWL_EQUIVALENT_CLASS, q_obj_name.c_str());
1014 }
1015 }
1016
1017 // If disjoints are needed
1018 /*if (q_subj != nullptr && q_role_name.empty() || q_role_name == OWL_DISJOINT_WITH)
1019 {
1020 if (q_obj_name.empty())
1021 {
1022 // All disjoint classes
1023 TripleGatherer gatherer(&triples, true, OWL_DISJOINT_WITH, q_subj_name.c_str(), [](const ClassifiableEntry* entry)
1024 {
1025 const TConcept* concept = static_cast<const TConcept*>(entry);
1026 return !concept->isSingleton() && !concept->isSystem() && !concept->isBottom();
1027 });
1028
1029 getDisjointConcepts(q_subj_c_exp, gatherer);
1030 }
1031 else if (q_obj != nullptr && !q_obj->isSingleton())
1032 {
1033 // A specific disjoint class from the query
1034 if (isDisjoint(q_subj_c_exp, q_obj_c_exp))
1035 pushTriple(triples, q_subj_name.c_str(), OWL_DISJOINT_WITH, q_obj_name.c_str());
1036 }
1037 }*/
1038 }
1039 }
1040 else if (q_subj_role != nullptr)
1041 {
1042 // Subject is specified in the query and it is a role
1043
1044 if (q_subj_role->isDataRole())
1045 {
1046 if (q_subj_name != OWL_TOP_DATA_PROPERTY)
1047 {
1048 // Data role properties
1049
1050 if (q_role_name.empty() || q_role_name == RDF_TYPE)
1051 {
1052 if (q_obj_name.empty() || q_obj_name == OWL_DATATYPE_PROPERTY)
1053 pushTriple(triples, q_subj_name.c_str(), RDF_TYPE, OWL_DATATYPE_PROPERTY);
1054
1055 if (isFunctional(q_subj_d_role_exp) && (q_obj_name.empty() || q_obj_name == OWL_FUNCTIONAL_PROPERTY))
1056 pushTriple(triples, q_subj_name.c_str(), RDF_TYPE, OWL_FUNCTIONAL_PROPERTY);
1057 }
1058
1059 if (q_role_name.empty() || q_role_name == RDFS_DOMAIN)
1060 {
1061 TripleGatherer gatherer(&triples, true, RDFS_DOMAIN, q_subj_name.c_str(), [q_obj_name, q_obj](const ClassifiableEntry* entry)
1062 {
1063 return !entry->isSystem() && (q_obj_name.empty() || entry == q_obj);
1064 });
1065
1066 getDRoleDomain(q_subj_d_role_exp, true, gatherer);
1067 }
1068
1069 if (q_role_name.empty() || q_role_name == RDFS_SUBPROPERTY_OF)
1070 {
1071 if (q_obj_role != nullptr)
1072 {
1073 if (q_obj_role->isDataRole() && isSubRoles(q_subj_d_role_exp, q_obj_d_role_exp))
1074 pushTriple(triples, q_subj_name.c_str(), RDFS_SUBPROPERTY_OF, q_obj_name.c_str());
1075 }
1076 else if (q_obj_name.empty())
1077 {
1078 TripleGatherer gatherer(&triples, true, RDFS_SUBPROPERTY_OF, q_subj_name.c_str(), [q_subj_role](const ClassifiableEntry* entry)
1079 {
1080 return entry != q_subj_role;
1081 });
1082
1083 getSupRoles(q_subj_d_role_exp, false, gatherer);
1084 }
1085 }
1086
1087 if (q_role_name.empty() || q_role_name == OWL_EQUIVALENT_PROPERTY)
1088 {
1089 TripleGatherer gatherer(&triples, true, OWL_EQUIVALENT_PROPERTY, q_subj_name.c_str(), [q_subj_role, q_obj_name, q_obj_role](const ClassifiableEntry* entry)
1090 {
1091 return entry != q_subj_role && (q_obj_name.empty() || entry == q_obj_role);
1092 });
1093
1094 getEquivalentRoles(q_subj_d_role_exp, gatherer);
1095 }
1096 }
1097 }
1098 else if (q_subj_name != OWL_TOP_OBJECT_PROPERTY)
1099 {
1100 // Object role properties
1101
1102 if (q_role_name.empty() || q_role_name == RDF_TYPE)
1103 {
1104 if (q_obj_name.empty() || q_obj_name == OWL_OBJECT_PROPERTY)
1105 pushTriple(triples, q_subj_name.c_str(), RDF_TYPE, OWL_OBJECT_PROPERTY);
1106
1107 if (isFunctional(q_subj_o_role_exp) && (q_obj_name.empty() || q_obj_name == OWL_FUNCTIONAL_PROPERTY))
1108 pushTriple(triples, q_subj_name.c_str(), RDF_TYPE, OWL_FUNCTIONAL_PROPERTY);
1109
1110 if (isInverseFunctional(q_subj_o_role_exp) && (q_obj_name.empty() || q_obj_name == OWL_INVERSE_FUNCTIONAL_PROPERTY))
1111 pushTriple(triples, q_subj_name.c_str(), RDF_TYPE, OWL_INVERSE_FUNCTIONAL_PROPERTY);
1112
1113 if (isSymmetric(q_subj_o_role_exp) && (q_obj_name.empty() || q_obj_name == OWL_SYMMETRIC_PROPERTY))
1114 pushTriple(triples, q_subj_name.c_str(), RDF_TYPE, OWL_SYMMETRIC_PROPERTY);
1115
1116 if (isAsymmetric(q_subj_o_role_exp) && (q_obj_name.empty() || q_obj_name == OWL_ASYMMETRIC_PROPERTY))
1117 pushTriple(triples, q_subj_name.c_str(), RDF_TYPE, OWL_ASYMMETRIC_PROPERTY);
1118
1119 if (isTransitive(q_subj_o_role_exp) && (q_obj_name.empty() || q_obj_name == OWL_TRANSITIVE_PROPERTY))
1120 pushTriple(triples, q_subj_name.c_str(), RDF_TYPE, OWL_TRANSITIVE_PROPERTY);
1121
1122 if (isReflexive(q_subj_o_role_exp) && (q_obj_name.empty() || q_obj_name == OWL_REFLEXIVE_PROPERTY))
1123 pushTriple(triples, q_subj_name.c_str(), RDF_TYPE, OWL_REFLEXIVE_PROPERTY);
1124
1125 if (isIrreflexive(q_subj_o_role_exp) && (q_obj_name.empty() || q_obj_name == OWL_IRREFLEXIVE_PROPERTY))
1126 pushTriple(triples, q_subj_name.c_str(), RDF_TYPE, OWL_IRREFLEXIVE_PROPERTY);
1127 }
1128
1129 if (q_role_name.empty() || q_role_name == RDFS_DOMAIN)
1130 {
1131 TripleGatherer gatherer(&triples, true, RDFS_DOMAIN, q_subj_name.c_str(), [q_subj, q_obj_name, q_obj](const ClassifiableEntry* entry)
1132 {
1133 return !entry->isSystem() && (q_obj_name.empty() || entry == q_obj);
1134 });
1135
1136 getORoleDomain(q_subj_o_role_exp, true, gatherer);
1137 }
1138
1139 if (q_role_name.empty() || q_role_name == RDFS_RANGE)
1140 {
1141 TripleGatherer gatherer(&triples, true, RDFS_RANGE, q_subj_name.c_str(), [q_subj, q_obj_name, q_obj](const ClassifiableEntry* entry)
1142 {
1143 return !entry->isSystem() && (q_obj_name.empty() || entry == q_obj);
1144 });
1145
1146 getRoleRange(q_subj_o_role_exp, true, gatherer);
1147 }
1148
1149 if (q_role_name.empty() || q_role_name == RDFS_SUBPROPERTY_OF)
1150 {
1151 if (q_obj_role != nullptr)
1152 {
1153 if (!q_obj_role->isDataRole() && isSubRoles(q_subj_o_role_exp, q_obj_o_role_exp))
1154 pushTriple(triples, q_subj_name.c_str(), RDFS_SUBPROPERTY_OF, q_obj_name.c_str());
1155 }
1156 else if (q_obj_name.empty())
1157 {
1158 TripleGatherer gatherer(&triples, true, RDFS_SUBPROPERTY_OF, q_subj_name.c_str(), [q_subj_role](const ClassifiableEntry* entry)
1159 {
1160 return entry != q_subj_role && static_cast<const TNamedEntry*>(entry)->getId() >= 0;
1161 });
1162
1163 getSupRoles(q_subj_o_role_exp, false, gatherer);
1164 }
1165 }
1166
1167 if (q_role_name.empty() || q_role_name == OWL_EQUIVALENT_PROPERTY)
1168 {
1169 TripleGatherer gatherer(&triples, true, OWL_EQUIVALENT_PROPERTY, q_subj_name.c_str(), [q_subj_role, q_obj_name, q_obj_role](const ClassifiableEntry* entry)
1170 {
1171 return entry != q_subj_role && static_cast<const TNamedEntry*>(entry)->getId() >= 0 && (q_obj_name.empty() || entry == q_obj_role);
1172 });
1173
1174 getEquivalentRoles(q_subj_o_role_exp, gatherer);
1175 }
1176
1177 if (q_role_name.empty() || q_role_name == OWL_INVERSE_OF)
1178 {
1179 const auto* inverse_role = q_subj_role->inverse();
1180
1181 if (inverse_role != nullptr && inverse_role->getId() >= 0 && (q_obj_name.empty() || q_obj_role == inverse_role))
1182 pushTriple(triples, q_subj_name.c_str(), OWL_INVERSE_OF, toRdf(inverse_role->getName()));
1183 }
1184
1185 }
1186 }
1187 }
1188
1189 //----------------------------------------------------------------------------------
1190 // atomic decomposition queries
1191 //----------------------------------------------------------------------------------
1192
1193 /// create new atomic decomposition of the loaded ontology using TYPE. @return size of the AD
1194 size_t
1195 ReasoningKernel :: getAtomicDecompositionSize ( ModuleMethod moduleMethod, ModuleType moduleType )
1196 {
1197 // init AD field
1198 if ( unlikely(AD != nullptr) )
1199 delete AD;
1200
1201 AD = new AtomicDecomposer(getModExtractor(moduleMethod)->getModularizer());
1202 return AD->getAOS ( &Ontology, moduleType )->size();
1203 }
1204 /// get a set of axioms that corresponds to the atom with the id INDEX
1205 const TOntologyAtom::AxiomSet&
1206 ReasoningKernel :: getAtomAxioms ( unsigned int index ) const
1207 {
1208 return (*AD->getAOS())[index]->getAtomAxioms();
1209 }
1210 /// get a set of axioms that corresponds to the module of the atom with the id INDEX
1211 const TOntologyAtom::AxiomSet&
1212 ReasoningKernel :: getAtomModule ( unsigned int index ) const
1213 {
1214 return (*AD->getAOS())[index]->getModule();
1215 }
1216 /// get a set of atoms on which atom with index INDEX depends
1217 const TOntologyAtom::AtomSet&
1218 ReasoningKernel :: getAtomDependents ( unsigned int index ) const
1219 {
1220 return (*AD->getAOS())[index]->getDepAtoms();
1221 }
1222
1223 /// get a number of locality checks performed for creating an AD
1224 unsigned long long
1225 ReasoningKernel :: getLocCheckNumber ( void ) const
1226 {
1227 return AD->getLocCheckNumber();
1228 }
1229
1230 OntologyBasedModularizer*
1231 ReasoningKernel :: getModExtractor ( ModuleMethod moduleMethod )
1232 {
1233 // check whether we need init
1234 OntologyBasedModularizer*& pMod = getModPointer(moduleMethod);
1235 if ( unlikely(pMod == nullptr) )
1236 pMod = new OntologyBasedModularizer ( getOntology(), moduleMethod );
1237 return pMod;
1238 }
1239
1240 /// get a set of axioms that corresponds to the atom with the id INDEX
1241 const AxiomVec&
1242 ReasoningKernel :: getModule ( ModuleMethod moduleMethod, ModuleType moduleType )
1243 {
1244 // init signature
1245 TSignature Sig;
1246 // NB: we don't care about locality type here as modularizer will change it
1247 Sig.setLocality(false);
1248 for ( const TDLExpression* expr : getExpressionManager()->getArgList() )
1249 if ( const TNamedEntity* entity = dynamic_cast<const TNamedEntity*>(expr) )
1250 Sig.add(entity);
1251 return getModExtractor(moduleMethod)->getModule ( Sig, moduleType );
1252 }
1253
1254 /// get a set of axioms that corresponds to the atom with the id INDEX
1255 const AxiomVec&
1256 ReasoningKernel :: getNonLocal ( ModuleMethod moduleMethod, ModuleType moduleType )
1257 {
1258 // init signature
1259 TSignature Sig;
1260 Sig.setLocality(moduleType == M_TOP); // true for TOP, false for BOT/STAR
1261 for ( const TDLExpression* expr : getExpressionManager()->getArgList() )
1262 if ( const TNamedEntity* entity = dynamic_cast<const TNamedEntity*>(expr) )
1263 Sig.add(entity);
1264
1265 // TLISPOntologyPrinter lp(std::cout);
1266 // do check
1267 LocalityChecker* LC = getModExtractor(moduleMethod)->getModularizer()->getLocalityChecker();
1268 LC->setSignatureValue(Sig);
1269 Result.clear();
1270 for ( TDLAxiom* axiom : getOntology() )
1271 {
1272 // std::cout << "Checking locality of ";
1273 // axiom->accept(lp);
1274 if ( !LC->local(axiom) )
1275 {
1276 // std::cout << "AX non-local" << std::endl;
1277 Result.push_back(axiom);
1278 }
1279 // else
1280 // std::cout << "AX local" << std::endl;
1281 }
1282 return Result;
1283 }
1284
1285 //----------------------------------------------------------------------------------
1286 // save/load interface
1287 //----------------------------------------------------------------------------------
1288
1289 /// check whether @return true if a file with reasoner state with a given NAME exists.
1290 bool
1291 ReasoningKernel :: checkSaveLoadContext ( const std::string& name ) const
1292 {
1293 return SaveLoadManager(name).existsContent();
1294 }
1295
1296 /// set a save/load file to a given NAME
1297 bool
1298 ReasoningKernel :: setSaveLoadContext ( const std::string& name )
1299 {
1300 delete pSLManager;
1301 pSLManager = new SaveLoadManager(name);
1302 return pSLManager->existsContent();
1303 }
1304
1305 /// clear a cache for a given name
1306 bool
1307 ReasoningKernel :: clearSaveLoadContext ( const std::string& name ) const
1308 {
1309 if ( checkSaveLoadContext(name) )
1310 {
1311 SaveLoadManager(name).clearContent();
1312 return true;
1313 }
1314 return false;
1315 }
1316
1317 //******************************************
1318 //* Initialization
1319 //******************************************
1320
1321 bool ReasoningKernel :: initOptions ( void )
1322 {
1323 // register all possible options used in FaCT++ Kernel
1324
1325 // options for TBox
1326
1327 // register "dumpQuery" option -- 11-08-04
1328 if ( KernelOptions.RegisterOption (
1329 "dumpQuery",
1330 "Option 'dumpQuery' dumps sub-TBox relevant to given satisfiability/subsumption query.",
1331 ifOption::iotBool,
1332 "false"
1333 ) )
1334 return true;
1335
1336 // register "absorptionFlags" option (04/05/2005)
1337 if ( KernelOptions.RegisterOption (
1338 "absorptionFlags",
1339 "Option 'absorptionFlags' sets up absorption process for general axioms. "
1340 "It text field of arbitrary length; every symbol means the absorption action: "
1341 "(B)ottom Absorption), (T)op absorption, (E)quivalent concepts replacement, (C)oncept absorption, "
1342 "(N)egated concept absorption, (F)orall expression replacement, Simple (f)orall expression replacement, "
1343 "(R)ole absorption, (S)plit",
1344 ifOption::iotText,
1345 "BTEfCFSR"
1346 ) )
1347 return true;
1348
1349 // register "alwaysPreferEquals" option (26/01/2006)
1350 if ( KernelOptions.RegisterOption (
1351 "alwaysPreferEquals",
1352 "Option 'alwaysPreferEquals' allows user to enforce usage of C=D definition instead of C[=D "
1353 "during absorption, even if implication appeared earlier in stream of axioms.",
1354 ifOption::iotBool,
1355 "true"
1356 ) )
1357 return true;
1358
1359 // register "useSpecialDomains" option (25/10/2011)
1360 if ( KernelOptions.RegisterOption (
1361 "useSpecialDomains",
1362 "Option 'useSpecialDomains' (development) controls the special processing of R&D for non-simple roles. "
1363 "Should always be set to true.",
1364 ifOption::iotBool,
1365 "true"
1366 ) )
1367 return true;
1368
1369 // options for DLDag
1370
1371 // register "orSortSub" option (20/12/2004)
1372 if ( KernelOptions.RegisterOption (
1373 "orSortSub",
1374 "Option 'orSortSub' define the sorting order of OR vertices in the DAG used in subsumption tests. "
1375 "Option has form of string 'Mop', where 'M' is a sort field (could be 'D' for depth, 'S' for size, 'F' "
1376 "for frequency, and '0' for no sorting), 'o' is a order field (could be 'a' for ascending and 'd' "
1377 "for descending mode), and 'p' is a preference field (could be 'p' for preferring non-generating "
1378 "rules and 'n' for not doing so).",
1379 ifOption::iotText,
1380 "0"
1381 ) )
1382 return true;
1383
1384 // register "orSortSat" option (20/12/2004)
1385 if ( KernelOptions.RegisterOption (
1386 "orSortSat",
1387 "Option 'orSortSat' define the sorting order of OR vertices in the DAG used in satisfiability tests "
1388 "(used mostly in caching). Option has form of string 'Mop', see orSortSub for details.",
1389 ifOption::iotText,
1390 "0"
1391 ) )
1392 return true;
1393
1394 // options for ToDoTable
1395
1396 // register "IAOEFLG" option
1397 if ( KernelOptions.RegisterOption (
1398 "IAOEFLG",
1399 "Option 'IAOEFLG' define the priorities of different operations in TODO list. Possible values are "
1400 "7-digit strings with ony possible digit are 0-6. The digits on the places 1, 2, ..., 7 are for "
1401 "priority of Id, And, Or, Exists, Forall, LE and GE operations respectively. The smaller number means "
1402 "the higher priority. All other constructions (TOP, BOTTOM, etc) has priority 0.",
1403 ifOption::iotText,
1404 "1263005"
1405 ) )
1406 return true;
1407
1408 // options for Reasoner
1409
1410 // register "useSemanticBranching" option
1411 if ( KernelOptions.RegisterOption (
1412 "useSemanticBranching",
1413 "Option 'useSemanticBranching' switch semantic branching on and off. The usage of semantic branching "
1414 "usually leads to faster reasoning, but sometime could give small overhead.",
1415 ifOption::iotBool,
1416 "true"
1417 ) )
1418 return true;
1419
1420 // register "useBackjumping" option
1421 if ( KernelOptions.RegisterOption (
1422 "useBackjumping",
1423 "Option 'useBackjumping' switch backjumping on and off. The usage of backjumping "
1424 "usually leads to much faster reasoning.",
1425 ifOption::iotBool,
1426 "true"
1427 ) )
1428 return true;
1429
1430 // register "testTimeout" option -- 21/08/09
1431 if ( KernelOptions.RegisterOption (
1432 "testTimeout",
1433 "Option 'testTimeout' sets timeout for a single reasoning test in milliseconds.",
1434 ifOption::iotInt,
1435 "0"
1436 ) )
1437 return true;
1438
1439 // options for Blocking
1440
1441 // register "useLazyBlocking" option -- 08-03-04
1442 if ( KernelOptions.RegisterOption (
1443 "useLazyBlocking",
1444 "Option 'useLazyBlocking' makes checking of blocking status as small as possible. This greatly "
1445 "increase speed of reasoning.",
1446 ifOption::iotBool,
1447 "true"
1448 ) )
1449 return true;
1450
1451 // register "useAnywhereBlocking" option (18/08/2008)
1452 if ( KernelOptions.RegisterOption (
1453 "useAnywhereBlocking",
1454 "Option 'useAnywhereBlocking' allow user to choose between Anywhere and Ancestor blocking.",
1455 ifOption::iotBool,
1456 "true"
1457 ) )
1458 return true;
1459
1460 // register "skipBeforeBlock" option (28/02/2009)
1461 if ( KernelOptions.RegisterOption (
1462 "skipBeforeBlock",
1463 "Internal use only. Option 'skipBeforeBlock' allow user to skip given number of nodes before make a block.",
1464 ifOption::iotInt,
1465 "0"
1466 ) )
1467 return true;
1468
1469 // options for Taxonomy
1470
1471 // register "useCompletelyDefined" option
1472 if ( KernelOptions.RegisterOption (
1473 "useCompletelyDefined",
1474 "Option 'useCompletelyDefined' leads to simpler Taxonomy creation if TBox contains no non-primitive "
1475 "concepts. Unfortunately, it is quite rare case.",
1476 ifOption::iotBool,
1477 "true"
1478 ) )
1479 return true;
1480
1481 // options for kernel
1482
1483 // register "checkAD" option (24/02/2012)
1484 if ( KernelOptions.RegisterOption (
1485 "checkAD",
1486 "Option 'checkAD' forces FaCT++ to create the AD and exit instead of performing classification",
1487 ifOption::iotBool,
1488 "false"
1489 ) )
1490 return true;
1491
1492 // register "dumpOntology" option (28/03/2013)
1493 if ( KernelOptions.RegisterOption (
1494 "dumpOntology",
1495 "Option 'dumpOntology' dumps the ontology loaded into the reasoner in a LISP-like format",
1496 ifOption::iotBool,
1497 "false"
1498 ) )
1499 return true;
1500
1501 // register "useIncrementalReasoning" option (21/06/2013)
1502 if ( KernelOptions.RegisterOption (
1503 "useIncrementalReasoning",
1504 "Option 'useIncrementalReasoning' (development) allows one to reason efficiently about small changes in the ontology.",
1505 ifOption::iotBool,
1506 "false"
1507 ) )
1508 return true;
1509
1510 // register "allowUndefinedNames" option (03/11/2013)
1511 if ( KernelOptions.RegisterOption (
1512 "allowUndefinedNames",
1513 "Option 'allowUndefinedNames' describes the policy of undefined names.",
1514 ifOption::iotBool,
1515 "true"
1516 ) )
1517 return true;
1518
1519 // register "precacheRelated" option -- 16/11/2021
1520 if (KernelOptions.RegisterOption(
1521 "precacheRelated",
1522 "Option 'precacheRelated' allows filling cache of individual relations during the consistency check, "
1523 "which allows for much faster (but sometimes incomplete) queries.",
1524 ifOption::iotBool,
1525 "true"
1526 ))
1527 return true;
1528
1529 // all was registered OK
1530 return false;
1531 }
1532