| 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 | #ifndef DLTBOX_H |
| 21 | #define DLTBOX_H |
| 22 | |
| 23 | #include <string> |
| 24 | #include <vector> |
| 25 | #include <set> |
| 26 | #include <map> |
| 27 | |
| 28 | #include "tConcept.h" |
| 29 | #include "tIndividual.h" |
| 30 | #include "modelCacheSingleton.h" |
| 31 | #include "RoleMaster.h" |
| 32 | #include "LogicFeature.h" |
| 33 | #include "dlDag.h" |
| 34 | #include "ifOptions.h" |
| 35 | #include "PriorityMatrix.h" |
| 36 | #include "tRelated.h" |
| 37 | #include "tNECollection.h" |
| 38 | #include "tAxiomSet.h" |
| 39 | #include "DataTypeCenter.h" |
| 40 | #include "tProgressMonitor.h" |
| 41 | #include "tKBFlags.h" |
| 42 | |
| 43 | class DlSatTester; |
| 44 | class Taxonomy; |
| 45 | class DLConceptTaxonomy; |
| 46 | class dumpInterface; |
| 47 | class TSignature; |
| 48 | class SaveLoadManager; |
| 49 | |
| 50 | /// enumeration for the reasoner status |
| 51 | enum KBStatus |
| 52 | { |
| 53 | kbEmpty, // no axioms loaded yet; not used in TBox |
| 54 | kbLoading, // axioms are added to the KB, no preprocessing done |
| 55 | kbCChecked, // KB is preprocessed and consistency checked |
| 56 | kbClassified, // KB is classified |
| 57 | kbRealised, // KB is realised |
| 58 | }; |
| 59 | |
| 60 | class TBox |
| 61 | { |
| 62 | friend class DlSatTester; |
| 63 | friend class NominalReasoner; |
| 64 | friend class ReasoningKernel; |
| 65 | friend class TAxiom; // FIXME!! while TConcept can't get rid of told cycles |
| 66 | friend class DLConceptTaxonomy; |
| 67 | |
| 68 | public: // type interface |
| 69 | /// vector of CONCEPT-like elements |
| 70 | typedef std::vector<TConcept*> ConceptVector; |
| 71 | /// vector of SINGLETON-like elements |
| 72 | typedef std::vector<TIndividual*> SingletonVector; |
| 73 | /// map between names and corresponding module signatures |
| 74 | typedef std::map<const TNamedEntity*, const TSignature*> NameSigMap; |
| 75 | |
| 76 | protected: // types |
| 77 | /// type for DISJOINT-like statements |
| 78 | typedef std::vector<DLTree*> ExpressionArray; |
| 79 | /// set of concepts together with creation methods |
| 80 | typedef TNECollection<TConcept> ConceptCollection; |
| 81 | /// collection of individuals |
| 82 | typedef TNECollection<TIndividual> IndividualCollection; |
| 83 | /// type for the array of Related elements |
| 84 | typedef std::vector<TRelated*> RelatedCollection; |
| 85 | /// type for a collection of DIFFERENT individuals |
| 86 | typedef std::vector<SingletonVector> DifferentIndividuals; |
| 87 | /// type for a A -> C map |
| 88 | typedef std::map<TConcept*, DLTree*> ConceptDefMap; |
| 89 | /// type for a set of concepts |
| 90 | typedef std::set<TConcept*> ConceptSet; |
| 91 | |
| 92 | /// class for simple rules like Ch :- Cb1, Cbi, CbN; all C are primitive named concepts |
| 93 | class TSimpleRule |
| 94 | { |
| 95 | public: // type interface |
| 96 | /// type for the rule body |
| 97 | typedef TBox::ConceptVector TRuleBody; |
| 98 | /// RW iterator over body |
| 99 | typedef TRuleBody::iterator iterator; |
| 100 | /// RO iterator over body |
| 101 | typedef TRuleBody::const_iterator const_iterator; |
| 102 | |
| 103 | public: // members |
| 104 | /// body of the rule |
| 105 | TRuleBody Body; |
| 106 | /// head of the rule as a DLTree |
| 107 | DLTree* tHead; |
| 108 | /// head of the rule as a BP |
| 109 | BipolarPointer bpHead = bpINVALID; |
| 110 | |
| 111 | public: // interface |
| 112 | /// init c'tor |
| 113 | TSimpleRule ( const TRuleBody& body, DLTree* head ) |
| 114 | : Body(body) |
| 115 | , tHead(head) |
| 116 | {} |
| 117 | /// no copy c'tor |
| 118 | TSimpleRule ( const TSimpleRule& ) = delete; |
| 119 | /// no assignment |
| 120 | TSimpleRule& operator= ( const TSimpleRule& ) = delete; |
| 121 | /// empty d'tor |
| 122 | virtual ~TSimpleRule() { deleteTree(tHead); } |
| 123 | }; // TSimpleRule |
| 124 | /// all simple rules in KB |
| 125 | typedef std::vector<TSimpleRule*> TSimpleRules; |
| 126 | /// R-C cache for the \forall R.C replacement in GCIs |
| 127 | typedef std::vector<std::pair<DLTree*,TConcept*> > TRCCache; |
| 128 | |
| 129 | protected: // typedefs |
| 130 | /// RW concept iterator |
| 131 | typedef ConceptCollection::iterator c_iterator; |
| 132 | /// RW individual iterator |
| 133 | typedef IndividualCollection::iterator i_iterator; |
| 134 | /// RO ExpressionArray iterator |
| 135 | typedef ExpressionArray::const_iterator ea_iterator; |
| 136 | |
| 137 | public: // interface |
| 138 | /// RO concept iterator |
| 139 | typedef ConceptCollection::const_iterator c_const_iterator; |
| 140 | /// RO individual iterator |
| 141 | typedef IndividualCollection::const_iterator i_const_iterator; |
| 142 | |
| 143 | protected: // members |
| 144 | /// relevance label (to determine logical features) |
| 145 | TLabeller relevance; |
| 146 | /// DAG of all expressions |
| 147 | DLDag DLHeap; |
| 148 | |
| 149 | /// reasoner for TBox-related queries w/o nominals |
| 150 | DlSatTester* stdReasoner = nullptr; |
| 151 | /// reasoner for TBox-related queries with nominals |
| 152 | DlSatTester* nomReasoner = nullptr; |
| 153 | |
| 154 | /// progress monitor |
| 155 | TProgressMonitor* pMonitor = nullptr; |
| 156 | |
| 157 | /// vectors for Completely defined, Non-CD and Non-primitive concepts |
| 158 | ConceptVector arrayCD, arrayNoCD, arrayNP; |
| 159 | /// taxonomy structure of a TBox |
| 160 | Taxonomy* pTax = nullptr; |
| 161 | /// classifier |
| 162 | DLConceptTaxonomy* pTaxCreator = nullptr; |
| 163 | /// name-signature map |
| 164 | NameSigMap* pName2Sig = nullptr; |
| 165 | /// DataType center |
| 166 | DataTypeCenter DTCenter; |
| 167 | /// set of reasoning options |
| 168 | const ifOptionSet* pOptions; |
| 169 | /// status of the KB |
| 170 | KBStatus Status = kbLoading; |
| 171 | |
| 172 | /// global KB features |
| 173 | LogicFeatures KBFeatures; |
| 174 | /// GCI features |
| 175 | LogicFeatures GCIFeatures; |
| 176 | /// nominal cloud features |
| 177 | LogicFeatures NCFeatures; |
| 178 | /// aux features |
| 179 | LogicFeatures auxFeatures; |
| 180 | /// pointer to current feature (in case of local ones) |
| 181 | LogicFeatures* curFeature = nullptr; |
| 182 | |
| 183 | // auxiliary concepts for Taxonomy |
| 184 | |
| 185 | /// concept representing Top |
| 186 | TConcept* pTop; |
| 187 | /// concept representing Bottom |
| 188 | TConcept* pBottom; |
| 189 | /// concept representing temporary one that can not be used anywhere in the ontology |
| 190 | TConcept* pTemp; |
| 191 | /// temporary concept that represents query |
| 192 | TConcept* pQuery = nullptr; |
| 193 | |
| 194 | /// all named concepts |
| 195 | ConceptCollection Concepts; |
| 196 | /// all named individuals/nominals |
| 197 | IndividualCollection Individuals; |
| 198 | /// "normal" (object) roles |
| 199 | RoleMaster ORM; |
| 200 | /// data roles |
| 201 | RoleMaster DRM; |
| 202 | /// set of GCIs |
| 203 | TAxiomSet Axioms; |
| 204 | /// given individual-individual relations |
| 205 | RelatedCollection RelatedI; |
| 206 | /// known disjoint sets of individuals |
| 207 | DifferentIndividuals Different; |
| 208 | /// all simple rules in KB |
| 209 | TSimpleRules SimpleRules; |
| 210 | /// map to show the possible equivalence between individuals |
| 211 | std::map<TConcept*, std::pair<TIndividual*,bool> > SameI; |
| 212 | /// map from A->C for axioms A [= C where A = D is in the TBox |
| 213 | ConceptDefMap ExtraConceptDefs; |
| 214 | |
| 215 | /// internalisation of a general axioms |
| 216 | BipolarPointer T_G = bpTOP; |
| 217 | /// KB flags about GCIs |
| 218 | TKBFlags GCIs; |
| 219 | |
| 220 | /// cache for the \forall R.C replacements during absorption |
| 221 | TRCCache RCCache; |
| 222 | |
| 223 | /// maps from concept index to concept itself |
| 224 | ConceptVector ConceptMap; |
| 225 | |
| 226 | /// number of concepts and individuals; used to set index for modelCache |
| 227 | unsigned int nC = 0; |
| 228 | /// number of all distinct roles; used to set index for modelCache |
| 229 | unsigned int nR = 0; |
| 230 | /// current aux concept's ID |
| 231 | unsigned int auxConceptID = 0; |
| 232 | /// how many times nominals were found during translation to DAG; local to BuildDAG |
| 233 | unsigned int nNominalReferences = 0; |
| 234 | /// number of relevant calls to named concepts; local to relevance |
| 235 | unsigned long nRelevantCCalls = 0; |
| 236 | /// number of relevant calls to concept expressions; local to relevance |
| 237 | unsigned long nRelevantBCalls = 0; |
| 238 | |
| 239 | /// searchable stack for the told subsumers |
| 240 | std::set<TConcept*> CInProcess; |
| 241 | /// all the synonyms in the told subsumers' cycle |
| 242 | std::vector<TConcept*> ToldSynonyms; |
| 243 | |
| 244 | /// fairness constraints |
| 245 | ConceptVector Fairness; |
| 246 | |
| 247 | /// priority matrix for To-Do lists |
| 248 | ToDoPriorMatrix PriorityMatrix; |
| 249 | /// single SAT/SUB test timeout in milliseconds |
| 250 | unsigned long testTimeout = 0; |
| 251 | |
| 252 | //--------------------------------------------------------------------------- |
| 253 | // Reasoner's members: there are many reasoner classes, some members are shared |
| 254 | //--------------------------------------------------------------------------- |
| 255 | |
| 256 | /// flag for switching semantic branching |
| 257 | bool useSemanticBranching = true; |
| 258 | /// flag for switching backjumping |
| 259 | bool useBackjumping = true; |
| 260 | /// whether or not check blocking status as late as possible |
| 261 | bool useLazyBlocking = true; |
| 262 | /// flag for switching between Anywhere and Ancestor blocking |
| 263 | bool useAnywhereBlocking = true; |
| 264 | /// flag to use caching during completion tree construction |
| 265 | bool useNodeCache = true; |
| 266 | /// how many nodes skip before block; work only with FAIRNESS |
| 267 | int nSkipBeforeBlock = 0; |
| 268 | |
| 269 | //--------------------------------------------------------------------------- |
| 270 | // User-defined flags |
| 271 | //--------------------------------------------------------------------------- |
| 272 | |
| 273 | /// flag for creating taxonomy |
| 274 | bool useCompletelyDefined = true; |
| 275 | /// flag for dumping TBox relevant to query |
| 276 | bool dumpQuery = false; |
| 277 | /// whether or not we need classification. Set up in checkQueryNames() |
| 278 | bool needClassification = true; |
| 279 | /// shall we prefer C=D axioms to C[=E in definition of concepts |
| 280 | bool alwaysPreferEquals = true; |
| 281 | /// use special domains as GCIs |
| 282 | bool useSpecialDomains = false; |
| 283 | /// shall verbose output be used |
| 284 | bool verboseOutput = false; |
| 285 | /// create cache of individual relations during consistency check (much faster but could be incomplete) |
| 286 | bool precacheRelated = true; |
| 287 | |
| 288 | //--------------------------------------------------------------------------- |
| 289 | // Internally defined flags |
| 290 | //--------------------------------------------------------------------------- |
| 291 | |
| 292 | /// whether we use sorted reasoning; depends on some simplifications |
| 293 | bool useSortedReasoning = true; |
| 294 | /// flag whether TBox is GALEN-like |
| 295 | bool isLikeGALEN = false; |
| 296 | /// flag whether TBox is WINE-like |
| 297 | bool isLikeWINE = false; |
| 298 | |
| 299 | /// whether KB is consistent |
| 300 | bool Consistent = true; |
| 301 | |
| 302 | /// time spend for preprocessing |
| 303 | float preprocTime = 0.0; |
| 304 | /// time spend for consistency checking |
| 305 | float consistTime = 0.0; |
| 306 | |
| 307 | protected: // methods |
| 308 | /// init all flags using given set of options |
| 309 | void readConfig ( const ifOptionSet* Options ); |
| 310 | /// initialise Top and Bottom internal concepts |
| 311 | void initTopBottom ( void ); |
| 312 | |
| 313 | |
| 314 | //----------------------------------------------------------------------------- |
| 315 | //-- internal iterators |
| 316 | //----------------------------------------------------------------------------- |
| 317 | |
| 318 | /// RW begin() for concepts |
| 319 | c_iterator c_begin ( void ) { return Concepts.begin(); } |
| 320 | /// RW end() for concepts |
| 321 | c_iterator c_end ( void ) { return Concepts.end(); } |
| 322 | |
| 323 | /// RW begin() for individuals |
| 324 | i_iterator i_begin ( void ) { return Individuals.begin(); } |
| 325 | /// RW end() for individuals |
| 326 | i_iterator i_end ( void ) { return Individuals.end(); } |
| 327 | |
| 328 | //----------------------------------------------------------------------------- |
| 329 | //-- internal ensure*-like interface |
| 330 | //----------------------------------------------------------------------------- |
| 331 | |
| 332 | /// @return concept by given Named Entry ID |
| 333 | static TConcept* toConcept ( TNamedEntry* id ) { return static_cast<TConcept*>(id); } |
| 334 | /// @return concept by given Named Entry ID |
| 335 | static const TConcept* toConcept ( const TNamedEntry* id ) { return static_cast<const TConcept*>(id); } |
| 336 | /// @return individual by given Named Entry ID |
| 337 | static TIndividual* toIndividual ( TNamedEntry* id ) { return static_cast<TIndividual*>(id); } |
| 338 | /// @return individual by given Named Entry ID |
| 339 | static const TIndividual* toIndividual ( const TNamedEntry* id ) { return static_cast<const TIndividual*>(id); } |
| 340 | |
| 341 | //----------------------------------------------------------------------------- |
| 342 | //-- internal BP-to-concept interface |
| 343 | //----------------------------------------------------------------------------- |
| 344 | |
| 345 | /// set P as a concept corresponding BP |
| 346 | void setBPforConcept ( BipolarPointer bp, TConcept* p ) |
| 347 | { |
| 348 | DLHeap[bp].setConcept(p); |
| 349 | p->pName = bp; |
| 350 | } |
| 351 | |
| 352 | /// get concept by it's BP (non-const version) |
| 353 | TDataEntry* getDataEntryByBP ( BipolarPointer bp ) |
| 354 | { |
| 355 | TDataEntry* p = static_cast<TDataEntry*>(DLHeap[bp].getConcept()); |
| 356 | fpp_assert ( p != nullptr ); |
| 357 | return p; |
| 358 | } |
| 359 | /// get concept by it's BP (const version) |
| 360 | const TDataEntry* getDataEntryByBP ( BipolarPointer bp ) const |
| 361 | { |
| 362 | const TDataEntry* p = static_cast<const TDataEntry*>(DLHeap[bp].getConcept()); |
| 363 | fpp_assert ( p != nullptr ); |
| 364 | return p; |
| 365 | } |
| 366 | |
| 367 | |
| 368 | //----------------------------------------------------------------------------- |
| 369 | //-- internal concept building interface |
| 370 | //----------------------------------------------------------------------------- |
| 371 | |
| 372 | /// checks if C is defined as C=D and set Synonyms accordingly |
| 373 | void checkEarlySynonym ( TConcept* C ) |
| 374 | { |
| 375 | if ( C->isSynonym() ) |
| 376 | return; // nothing to do |
| 377 | if ( C->isPrimitive() ) |
| 378 | return; // couldn't be a synonym |
| 379 | if ( !isCN(C->Description) ) |
| 380 | return; // complex expression -- not a synonym(imm.) |
| 381 | |
| 382 | C->setSynonym(getCI(C->Description)); |
| 383 | C->initToldSubsumers(); |
| 384 | } |
| 385 | /// make concept C non-primitive with definition DESC; @return it's old description |
| 386 | DLTree* makeNonPrimitive ( TConcept* C, DLTree* desc ) |
| 387 | { |
| 388 | DLTree* ret = C->makeNonPrimitive(desc); |
| 389 | checkEarlySynonym(C); |
| 390 | return ret; |
| 391 | } |
| 392 | |
| 393 | //----------------------------------------------------------------------------- |
| 394 | //-- support for n-ary predicates |
| 395 | //----------------------------------------------------------------------------- |
| 396 | |
| 397 | /// build a construction in the form AND (\neg q_i) |
| 398 | template <typename Iterator> |
| 399 | DLTree* buildDisjAux ( Iterator beg, Iterator end ) |
| 400 | { |
| 401 | DLTree* t = createTop(); |
| 402 | for ( Iterator i = beg; i < end; ++i ) |
| 403 | t = createSNFAnd ( t, createSNFNot(clone(*i)) ); |
| 404 | return t; |
| 405 | } |
| 406 | /// process a disjoint set [beg,end) in a usual manner |
| 407 | template <typename Iterator> |
| 408 | void processDisjoint ( Iterator beg, Iterator end ) |
| 409 | { |
| 410 | for ( Iterator i = beg; i < end; ++i ) |
| 411 | addSubsumeAxiom ( *i, buildDisjAux ( i+1, end ) ); |
| 412 | } |
| 413 | //----------------------------------------------------------------------------- |
| 414 | //-- internal DAG building methods |
| 415 | //----------------------------------------------------------------------------- |
| 416 | |
| 417 | /// build a DAG-structure for concepts and axioms |
| 418 | void buildDAG ( void ); |
| 419 | |
| 420 | /// translate concept P (together with definition) to DAG representation |
| 421 | void addConceptToHeap ( TConcept* p ); |
| 422 | /// register data-related expression in the DAG; @return it's DAG index |
| 423 | BipolarPointer addDataExprToHeap ( TDataEntry* p ); |
| 424 | /// builds DAG entry by general concept expression |
| 425 | BipolarPointer tree2dag ( const DLTree* ); |
| 426 | /// create forall node (together with transitive sub-roles entries) |
| 427 | BipolarPointer forall2dag ( const TRole* R, BipolarPointer C ); |
| 428 | /// create atmost node (together with NN-rule entries) |
| 429 | BipolarPointer atmost2dag ( unsigned int n, const TRole* R, BipolarPointer C ); |
| 430 | /// create REFLEXIVE node |
| 431 | BipolarPointer reflexive2dag ( const TRole* R ) |
| 432 | { |
| 433 | // input check: only simple roles are allowed in the reflexivity construction |
| 434 | if ( !R->isSimple() ) |
| 435 | throw EFPPNonSimpleRole(R->getName()); |
| 436 | return inverse ( DLHeap.add ( new DLVertex ( dtIrr, R ) ) ); |
| 437 | } |
| 438 | /// create node for AND expression T |
| 439 | BipolarPointer and2dag ( const DLTree* t ); |
| 440 | /// add elements of T to and-like vertex V; @return true if clash occurs |
| 441 | bool fillANDVertex ( DLVertex* v, const DLTree* t ); |
| 442 | /// create forall node for data role |
| 443 | BipolarPointer dataForall2dag ( const TRole* R, BipolarPointer C ) |
| 444 | { return DLHeap.add ( new DLVertex ( dtForall, 0, R, C ) ); } |
| 445 | /// create atmost node for data role |
| 446 | BipolarPointer dataAtMost2dag ( unsigned int n, const TRole* R, BipolarPointer C ) |
| 447 | { return DLHeap.add ( new DLVertex ( dtLE, n, R, C ) ); } |
| 448 | /// @return a pointer to concept representation |
| 449 | BipolarPointer concept2dag ( TConcept* p ) |
| 450 | { |
| 451 | if ( p == nullptr ) |
| 452 | return bpINVALID; |
| 453 | if ( !isValid(p->pName) ) |
| 454 | addConceptToHeap(p); |
| 455 | return p->resolveId(); |
| 456 | } |
| 457 | |
| 458 | //----------------------------------------------------------------------------- |
| 459 | //-- internal parser (input) interface |
| 460 | //----------------------------------------------------------------------------- |
| 461 | |
| 462 | /// set the flag that forbid usage of undefined names for concepts/roles; @return old value |
| 463 | bool setForbidUndefinedNames ( bool val ) |
| 464 | { |
| 465 | ORM.setUndefinedNames(!val); |
| 466 | DRM.setUndefinedNames(!val); |
| 467 | Individuals.setLocked(val); |
| 468 | return Concepts.setLocked(val); |
| 469 | } |
| 470 | /// tries to apply axiom C [= CN; @return NULL if applicable or new CN |
| 471 | DLTree* applyAxiomCToCN ( DLTree* C, DLTree* CN ); |
| 472 | /// tries to apply axiom CN [= C; @return NULL if applicable or new CN |
| 473 | DLTree* applyAxiomCNToC ( DLTree* CN, DLTree* C ); |
| 474 | /// tries to add C = RHS for the concept C; @return true if OK |
| 475 | bool addNonprimitiveDefinition ( TConcept* C, DLTree* rhs ); |
| 476 | /// tries to add C = RHS for the concept C [= X; @return true if OK |
| 477 | bool switchToNonprimitive ( TConcept* C, DLTree* rhs ); |
| 478 | /// transform definition C=D' with C [= E into C [= (D' and E) with D [= C |
| 479 | /// D is usually D', but see addSubsumeForDefined() |
| 480 | void makeDefinitionPrimitive ( TConcept* C, DLTree* E, DLTree* D ) |
| 481 | { |
| 482 | C->setPrimitive(); // now we have C [= D' |
| 483 | C->addDesc(E); // here C [= (D' and E) |
| 484 | C->initToldSubsumers(); |
| 485 | // all we need is to add (old C's desc)D [= C |
| 486 | addSubsumeAxiom ( D, getTree(C) ); |
| 487 | } |
| 488 | |
| 489 | // for complex Concept operations |
| 490 | /// try to absorb GCI C[=D; if not possible, just record this GCI |
| 491 | void processGCI ( DLTree* C, DLTree* D ) { Axioms.addAxiom ( C, D ); } |
| 492 | |
| 493 | // recognize Range/Domain restriction in an axiom and transform it into role R&D. |
| 494 | // return true if transformation was performed |
| 495 | bool axiomToRangeDomain ( DLTree* l, DLTree* r ); |
| 496 | |
| 497 | /// @return true if BP represents a named entry in a DAG |
| 498 | bool isNamedConcept ( BipolarPointer bp ) const |
| 499 | { |
| 500 | DagTag tag = DLHeap[bp].Type(); |
| 501 | return isCNameTag(tag) || tag == dtDataType || tag == dtDataValue; |
| 502 | } |
| 503 | |
| 504 | /// get aux concept obtained from C=\AR.~D by forall replacement |
| 505 | TConcept* getRCCache ( const DLTree* C ) const |
| 506 | { |
| 507 | for ( TRCCache::const_iterator p = RCCache.begin(), p_end = RCCache.end(); p < p_end; ++p ) |
| 508 | if ( equalTrees ( C, p->first ) ) |
| 509 | return p->second; |
| 510 | return nullptr; |
| 511 | } |
| 512 | /// add CN as a cache entry for C=\AR.~D> |
| 513 | void setRCCache ( DLTree* C, TConcept* CN ) { RCCache.push_back(std::make_pair(C,CN)); } |
| 514 | |
| 515 | /// check if TBox contains too many GCIs to switch strategy |
| 516 | bool isGalenLikeTBox ( void ) const { return isLikeGALEN; } |
| 517 | /// check if TBox contains too many nominals and GCIs to switch strategy |
| 518 | bool isWineLikeTBox ( void ) const { return isLikeWINE; } |
| 519 | |
| 520 | //----------------------------------------------------------------------------- |
| 521 | //-- internal preprocessing methods |
| 522 | //----------------------------------------------------------------------------- |
| 523 | |
| 524 | /// build a roles taxonomy and a DAG |
| 525 | void Preprocess ( void ); |
| 526 | /// transform C [= D with C = E into GCIs |
| 527 | void TransformExtraSubsumptions ( void ); |
| 528 | /// absorb all axioms |
| 529 | void AbsorbAxioms ( void ) |
| 530 | { |
| 531 | unsigned int nSynonyms = countSynonyms(); |
| 532 | Axioms.absorb(); |
| 533 | if ( countSynonyms() > nSynonyms ) |
| 534 | replaceAllSynonyms(); |
| 535 | if ( Axioms.wasRoleAbsorptionApplied() ) |
| 536 | initToldSubsumers(); |
| 537 | } |
| 538 | |
| 539 | /// pre-process RELATED axioms: resolve synonyms, mark individuals as related |
| 540 | void preprocessRelated ( void ); |
| 541 | /// determine all sorts in KB (make job only for SORTED_REASONING) |
| 542 | void determineSorts ( void ); |
| 543 | |
| 544 | /// calculate statistic for DAG and Roles |
| 545 | void CalculateStatistic ( void ); |
| 546 | /// Remove DLTree* from TConcept after DAG is constructed |
| 547 | void RemoveExtraDescriptions ( void ); |
| 548 | |
| 549 | /// init Range and Domain for all roles of RM; sets hasGCI if R&D was found |
| 550 | void initRangeDomain ( RoleMaster& RM ); |
| 551 | |
| 552 | /// set told TOP concept whether necessary |
| 553 | void initToldSubsumers ( void ) |
| 554 | { |
| 555 | for ( c_iterator pc = c_begin(); pc != c_end(); ++pc ) |
| 556 | if ( !(*pc)->isSynonym() ) |
| 557 | (*pc)->initToldSubsumers(); |
| 558 | for ( i_iterator pi = i_begin(); pi != i_end(); ++pi ) |
| 559 | if ( !(*pi)->isSynonym() ) |
| 560 | (*pi)->initToldSubsumers(); |
| 561 | } |
| 562 | /// set told TOP concept whether necessary |
| 563 | void setToldTop ( void ) |
| 564 | { |
| 565 | for ( c_iterator pc = c_begin(); pc != c_end(); ++pc ) |
| 566 | (*pc)->setToldTop(pTop); |
| 567 | for ( i_iterator pi = i_begin(); pi != i_end(); ++pi ) |
| 568 | (*pi)->setToldTop(pTop); |
| 569 | } |
| 570 | /// calculate TS depth for all concepts |
| 571 | void calculateTSDepth ( void ) |
| 572 | { |
| 573 | for ( c_iterator pc = c_begin(); pc != c_end(); ++pc ) |
| 574 | (*pc)->calculateTSDepth(); |
| 575 | for ( i_iterator pi = i_begin(); pi != i_end(); ++pi ) |
| 576 | (*pi)->calculateTSDepth(); |
| 577 | } |
| 578 | |
| 579 | /// find referential cycles (like A [= (and B C), B [= A) and transform them to synonyms (B=A, A[=C) |
| 580 | void transformToldCycles ( void ); |
| 581 | /// check if P appears in referential cycle; |
| 582 | /// @return concept which creates cycle, NULL if no such concept exists. |
| 583 | TConcept* checkToldCycle ( TConcept* p ); |
| 584 | /// transform i [= C [= j into i=C=j for i,j nominals |
| 585 | void transformSingletonHierarchy ( void ); |
| 586 | /// make P and all its non-singleton parents synonyms to its singleton parent; @return that singleton |
| 587 | TIndividual* transformSingletonWithSP ( TConcept* p ); |
| 588 | /// helper to the previous function |
| 589 | TIndividual* getSPForConcept ( TConcept* p ); |
| 590 | |
| 591 | /// @return true if C is referenced in TREE; use PROCESSED to record explored names |
| 592 | bool isReferenced ( TConcept* C, DLTree* tree, ConceptSet& processed ) const; |
| 593 | /// @return true if C is referenced in the definition of concept D; use PROCESSED to record explored names |
| 594 | bool isReferenced ( TConcept* C, TConcept* D, ConceptSet& processed ) const |
| 595 | { |
| 596 | // mark D as processed |
| 597 | processed.insert(D); |
| 598 | // check the description of D |
| 599 | if ( D->Description == nullptr ) |
| 600 | return false; |
| 601 | if ( isReferenced ( C, D->Description, processed ) ) |
| 602 | return true; |
| 603 | // we are done for primitive concepts |
| 604 | if ( D->isPrimitive() ) |
| 605 | return false; |
| 606 | // check if D has an extra description |
| 607 | auto p = ExtraConceptDefs.find(D); |
| 608 | if ( p != ExtraConceptDefs.end() ) |
| 609 | return isReferenced ( C, p->second, processed ); |
| 610 | return false; |
| 611 | } |
| 612 | |
| 613 | /// @return number of synonyms in the KB |
| 614 | unsigned int countSynonyms ( void ) const |
| 615 | { |
| 616 | unsigned int nSynonyms = 0; |
| 617 | for ( c_const_iterator pc = c_begin(); pc != c_end(); ++pc ) |
| 618 | if ( (*pc)->isSynonym() ) |
| 619 | ++nSynonyms; |
| 620 | |
| 621 | for ( i_const_iterator pi = i_begin(); pi != i_end(); ++pi ) |
| 622 | if ( (*pi)->isSynonym() ) |
| 623 | ++nSynonyms; |
| 624 | return nSynonyms; |
| 625 | } |
| 626 | /// replace all synonyms in concept descriptions with their definitions |
| 627 | void replaceAllSynonyms ( void ); |
| 628 | |
| 629 | /// init Extra Rule field in concepts given by a vector V with a given INDEX |
| 630 | inline void |
| 631 | initRuleFields ( const ConceptVector& v, size_t index ) const |
| 632 | { |
| 633 | for ( ConceptVector::const_iterator q = v.begin(), q_end = v.end(); q < q_end; ++q ) |
| 634 | (*q)->addExtraRule(index); |
| 635 | } |
| 636 | /// mark all concepts wrt their classification tag |
| 637 | void fillsClassificationTag ( void ) |
| 638 | { |
| 639 | for ( c_const_iterator pc = c_begin(); pc != c_end(); ++pc ) |
| 640 | (*pc)->getClassTag(); |
| 641 | for ( i_const_iterator pi = i_begin(); pi != i_end(); ++pi ) |
| 642 | (*pi)->getClassTag(); |
| 643 | } |
| 644 | /// set new concept index for given C wrt existing nC |
| 645 | void setConceptIndex ( TConcept* C ) |
| 646 | { |
| 647 | C->setIndex(nC); |
| 648 | ConceptMap.push_back(C); |
| 649 | ++nC; |
| 650 | } |
| 651 | /// set index on all classifiable entries |
| 652 | void setAllIndexes ( void ); |
| 653 | |
| 654 | //----------------------------------------------------------------------------- |
| 655 | //-- internal reasoner-related interface |
| 656 | //----------------------------------------------------------------------------- |
| 657 | |
| 658 | /// @return true iff reasoners were initialised |
| 659 | bool reasonersInited ( void ) const { return stdReasoner != nullptr; } |
| 660 | /// get RW reasoner wrt nominal case |
| 661 | DlSatTester* getReasoner ( void ) |
| 662 | { |
| 663 | fpp_assert ( curFeature != nullptr ); |
| 664 | if ( curFeature->hasSingletons() ) |
| 665 | return nomReasoner; |
| 666 | else |
| 667 | return stdReasoner; |
| 668 | } |
| 669 | /// get RO reasoner wrt nominal case |
| 670 | const DlSatTester* getReasoner ( void ) const |
| 671 | { |
| 672 | fpp_assert ( curFeature != nullptr ); |
| 673 | if ( curFeature->hasSingletons() ) |
| 674 | return nomReasoner; |
| 675 | else |
| 676 | return stdReasoner; |
| 677 | } |
| 678 | /// check whether KB is consistent; @return true if it is |
| 679 | bool performConsistencyCheck ( void ); // implemented in Reasoner.h |
| 680 | |
| 681 | //----------------------------------------------------------------------------- |
| 682 | //-- internal reasoning interface |
| 683 | //----------------------------------------------------------------------------- |
| 684 | |
| 685 | /// init reasoning service: create reasoner(s) |
| 686 | void initReasoner ( void ); // implemented in Reasoner.h |
| 687 | /// init taxonomy and classifier |
| 688 | void initTaxonomy ( void ); // implemented in DLConceptTaxonomy.h |
| 689 | /// set NameSigMap |
| 690 | void setNameSigMap ( NameSigMap* p ) { pName2Sig = p; } |
| 691 | /// creating taxonomy for given TBox; include individuals if necessary |
| 692 | void createTaxonomy ( bool needIndividuals ); |
| 693 | /// partition all elements in [begin,end) range wtr their tags |
| 694 | template <typename Iterator> |
| 695 | unsigned int fillArrays ( Iterator begin, Iterator end ) |
| 696 | { |
| 697 | unsigned int n = 0; |
| 698 | for ( Iterator p = begin; p < end; ++p ) |
| 699 | { |
| 700 | if ( (*p)->isNonClassifiable() ) |
| 701 | continue; |
| 702 | ++n; |
| 703 | switch ( (*p)->getClassTag() ) |
| 704 | { |
| 705 | case cttTrueCompletelyDefined: |
| 706 | arrayCD.push_back(*p); |
| 707 | break; |
| 708 | default: |
| 709 | arrayNoCD.push_back(*p); |
| 710 | break; |
| 711 | case cttNonPrimitive: |
| 712 | case cttHasNonPrimitiveTS: |
| 713 | arrayNP.push_back(*p); |
| 714 | break; |
| 715 | } |
| 716 | } |
| 717 | |
| 718 | return n; |
| 719 | } |
| 720 | /// classify all concepts from given COLLECTION with given CD value |
| 721 | void classifyConcepts ( const ConceptVector& collection, bool curCompletelyDefined, const char* type ); |
| 722 | /// classify single concept |
| 723 | void classifyEntry ( TConcept* entry ); |
| 724 | |
| 725 | //----------------------------------------------------------------------------- |
| 726 | //-- internal cache-related methods |
| 727 | //----------------------------------------------------------------------------- |
| 728 | |
| 729 | /// init const cache for either bpTOP or bpBOTTOM |
| 730 | void initConstCache ( BipolarPointer p ) { DLHeap.setCache ( p, createConstCache(p) ); } |
| 731 | /// init [singleton] cache for given concept and polarity |
| 732 | void initSingletonCache ( const TConcept* p, bool pos ) |
| 733 | { DLHeap.setCache ( createBiPointer(p->pName,pos), new modelCacheSingleton(createBiPointer(p->index(),pos)) ); } |
| 734 | /// create cache for ~C where C is a primitive concept (as it is simple) |
| 735 | void buildSimpleCache ( void ); |
| 736 | |
| 737 | //----------------------------------------------------------------------------- |
| 738 | //-- internal output helper methods |
| 739 | //----------------------------------------------------------------------------- |
| 740 | |
| 741 | void PrintDagEntry ( std::ostream& o, BipolarPointer p ) const; |
| 742 | /// print one concept-like entry |
| 743 | void PrintConcept ( std::ostream& o, const TConcept* p ) const; |
| 744 | /// print all registered concepts |
| 745 | void PrintConcepts ( std::ostream& o ) const |
| 746 | { |
| 747 | if ( Concepts.size() == 0 ) |
| 748 | return; |
| 749 | o << "Concepts (" << Concepts.size() << "):\n"; |
| 750 | for ( c_const_iterator pc = c_begin(); pc != c_end(); ++pc ) |
| 751 | PrintConcept(o,*pc); |
| 752 | } |
| 753 | /// print all registered individuals |
| 754 | void PrintIndividuals ( std::ostream& o ) const |
| 755 | { |
| 756 | if ( Individuals.size() == 0 ) |
| 757 | return; |
| 758 | o << "Individuals (" << Individuals.size() << "):\n"; |
| 759 | for ( i_const_iterator pi = i_begin(); pi != i_end(); ++pi ) |
| 760 | PrintConcept(o,*pi); |
| 761 | } |
| 762 | void PrintSimpleRules ( std::ostream& o ) const |
| 763 | { |
| 764 | if ( SimpleRules.empty() ) |
| 765 | return; |
| 766 | o << "Simple rules (" << SimpleRules.size() << "):\n"; |
| 767 | for ( TSimpleRules::const_iterator p = SimpleRules.begin(); p < SimpleRules.end(); ++p ) |
| 768 | { |
| 769 | ConceptVector::const_iterator q = (*p)->Body.begin(), q_end = (*p)->Body.end(); |
| 770 | o << "(" << (*q)->getName(); |
| 771 | while ( ++q < q_end ) |
| 772 | o << ", " << (*q)->getName(); |
| 773 | o << ") => " << (*p)->tHead << "\n"; |
| 774 | } |
| 775 | } |
| 776 | void PrintAxioms ( std::ostream& o ) const |
| 777 | { |
| 778 | if ( T_G == bpTOP ) |
| 779 | return; |
| 780 | o << "Axioms:\nT [="; |
| 781 | PrintDagEntry ( o, T_G ); |
| 782 | } |
| 783 | /// print KB features to LL |
| 784 | void printFeatures ( void ) const; |
| 785 | |
| 786 | //----------------------------------------------------------------------------- |
| 787 | //-- save/load support; implementation in SaveLoad.cpp |
| 788 | //----------------------------------------------------------------------------- |
| 789 | |
| 790 | //----------------------------------------------------------------------------- |
| 791 | //-- internal relevance helper methods |
| 792 | //----------------------------------------------------------------------------- |
| 793 | /// is given concept relevant wrt current TBox |
| 794 | bool isRelevant ( const TConcept* p ) const { return p->isRelevant(relevance); } |
| 795 | /// set given concept relevant wrt current TBox |
| 796 | void setRelevant1 ( TConcept* p ); |
| 797 | /// set given concept relevant wrt current TBox if not checked yet |
| 798 | void setRelevant ( TConcept* p ) { if ( !isRelevant(p) ) setRelevant1(p); } |
| 799 | |
| 800 | /// is given role relevant wrt current TBox |
| 801 | bool isRelevant ( const TRole* p ) const { return p->isRelevant(relevance); } |
| 802 | /// set given role relevant wrt current TBox |
| 803 | void setRelevant1 ( TRole* p ); |
| 804 | /// set given role relevant wrt current TBox if not checked yet |
| 805 | void setRelevant ( TRole* p ) |
| 806 | { |
| 807 | if ( ( likely(p->getId() != 0) || p->isTop() ) && !isRelevant(p) ) |
| 808 | setRelevant1(p); |
| 809 | } |
| 810 | |
| 811 | /// set given DAG entry relevant wrt current TBox |
| 812 | void setRelevant ( BipolarPointer p ); |
| 813 | |
| 814 | /// gather information about logical features of relevant concept |
| 815 | void collectLogicFeature ( const TConcept* p ) const |
| 816 | { |
| 817 | if ( curFeature ) |
| 818 | curFeature->fillConceptData(p); |
| 819 | } |
| 820 | /// gather information about logical features of relevant role |
| 821 | void collectLogicFeature ( const TRole* p ) const |
| 822 | { |
| 823 | if ( curFeature ) // update features w.r.t. current concept |
| 824 | curFeature->fillRoleData ( p, isRelevant(p->inverse()) ); |
| 825 | } |
| 826 | /// gather information about logical features of relevant DAG entry |
| 827 | void collectLogicFeature ( const DLVertex& v, bool pos ) const |
| 828 | { |
| 829 | if ( curFeature ) |
| 830 | curFeature->fillDAGData ( v, pos ); |
| 831 | } |
| 832 | /// mark all active GCIs relevant |
| 833 | void markGCIsRelevant ( void ) { setRelevant(T_G); } |
| 834 | |
| 835 | //----------------------------------------------------------------------------- |
| 836 | //-- internal relevance interface |
| 837 | //----------------------------------------------------------------------------- |
| 838 | /// set all TBox content (namely, concepts and GCIs) relevant |
| 839 | void markAllRelevant ( void ) |
| 840 | { |
| 841 | for ( c_iterator pc = c_begin(); pc != c_end(); ++pc ) |
| 842 | setRelevant(*pc); |
| 843 | for ( i_iterator pi = i_begin(); pi != i_end(); ++pi ) |
| 844 | setRelevant(*pi); |
| 845 | |
| 846 | markGCIsRelevant(); |
| 847 | } |
| 848 | /// mark chosen part of TBox (P, Q and GCIs) relevant |
| 849 | void calculateRelevant ( TConcept* p, TConcept* q ) |
| 850 | { |
| 851 | setRelevant(p); |
| 852 | if ( q != nullptr ) |
| 853 | setRelevant(q); |
| 854 | markGCIsRelevant(); |
| 855 | } |
| 856 | /// clear all relevance info |
| 857 | void clearRelevanceInfo ( void ) { relevance.newLabel(); } |
| 858 | /// gather relevance statistic for the whole KB |
| 859 | void gatherRelevanceInfo ( void ); |
| 860 | /// put relevance information to a concept's data |
| 861 | void setConceptRelevant ( TConcept* p ) |
| 862 | { |
| 863 | curFeature = &p->posFeatures; |
| 864 | setRelevant(p->pBody); |
| 865 | KBFeatures |= p->posFeatures; |
| 866 | collectLogicFeature(p); |
| 867 | clearRelevanceInfo(); |
| 868 | |
| 869 | // nothing to do for neg-prim concepts |
| 870 | if ( p->isPrimitive() ) |
| 871 | return; |
| 872 | |
| 873 | curFeature = &p->negFeatures; |
| 874 | setRelevant(inverse(p->pBody)); |
| 875 | KBFeatures |= p->negFeatures; |
| 876 | clearRelevanceInfo(); |
| 877 | } |
| 878 | /// update AUX features with the given one; update roles if necessary |
| 879 | void updateAuxFeatures ( const LogicFeatures& lf ) |
| 880 | { |
| 881 | if ( !lf.empty() ) |
| 882 | { |
| 883 | auxFeatures |= lf; |
| 884 | auxFeatures.mergeRoles(); |
| 885 | } |
| 886 | } |
| 887 | /// prepare features for SAT(P), or SUB(P,Q) test |
| 888 | void prepareFeatures ( const TConcept* pConcept, const TConcept* qConcept ); |
| 889 | /// clear current features |
| 890 | void clearFeatures ( void ) { curFeature = nullptr; } |
| 891 | |
| 892 | //----------------------------------------------------------------------------- |
| 893 | //-- internal dump output interface |
| 894 | //----------------------------------------------------------------------------- |
| 895 | /// dump concept-like essence using given dump method |
| 896 | void dumpConcept ( dumpInterface* dump, const TConcept* p ) const; |
| 897 | /// dump role-like essence using given dump method |
| 898 | void dumpRole ( dumpInterface* dump, const TRole* p ) const; |
| 899 | /// dump general concept expression using given dump method |
| 900 | void dumpExpression ( dumpInterface* dump, BipolarPointer p ) const; |
| 901 | /// dump all (relevant) roles |
| 902 | void dumpAllRoles ( dumpInterface* dump ) const; |
| 903 | |
| 904 | //----------------------------------------------------------------------------- |
| 905 | //-- internal save/load interface; implementation in SaveLoad.cpp |
| 906 | //----------------------------------------------------------------------------- |
| 907 | |
| 908 | /// init pointer2int maps |
| 909 | void initPointerMaps ( SaveLoadManager& m ) const; |
| 910 | |
| 911 | public: |
| 912 | /// init c'tor |
| 913 | TBox ( const ifOptionSet* Options, |
| 914 | const std::string& TopORoleName, |
| 915 | const std::string& BotORoleName, |
| 916 | const std::string& TopDRoleName, |
| 917 | const std::string& BotDRoleName ); |
| 918 | /// no copy c'tor |
| 919 | TBox ( const TBox& ) = delete; |
| 920 | /// no assignment |
| 921 | TBox& operator = ( const TBox& ) = delete; |
| 922 | /// d'tor |
| 923 | ~TBox(); |
| 924 | |
| 925 | /// get RW access to used Role Master |
| 926 | RoleMaster& getORM ( void ) { return ORM; } |
| 927 | /// get RO access to used Role Master |
| 928 | const RoleMaster& getORM ( void ) const { return ORM; } |
| 929 | /// get RW access to used DataRole Master |
| 930 | RoleMaster& getDRM ( void ) { return DRM; } |
| 931 | /// get RO access to used DataRole Master |
| 932 | const RoleMaster& getDRM ( void ) const { return DRM; } |
| 933 | /// get RW access to the RoleMaster depending of the R |
| 934 | RoleMaster& getRM ( const TRole* R ) { return R->isDataRole() ? getDRM() : getORM(); } |
| 935 | /// get RO access to the RoleMaster depending of the R |
| 936 | const RoleMaster& getRM ( const TRole* R ) const { return R->isDataRole() ? getDRM() : getORM(); } |
| 937 | /// get RW access to a DT center |
| 938 | DataTypeCenter& getDataTypeCenter ( void ) { return DTCenter; } |
| 939 | /// get RO access to a DT center |
| 940 | const DataTypeCenter& getDataTypeCenter ( void ) const { return DTCenter; } |
| 941 | /// get RO access to DAG (needed for KE) |
| 942 | const DLDag& getDag ( void ) const { return DLHeap; } |
| 943 | |
| 944 | /// set the value of a test timeout in milliseconds to VALUE |
| 945 | void setTestTimeout ( unsigned long value ) { testTimeout = value; } |
| 946 | /// (dis-)allow reasoner to use the undefined names in queries |
| 947 | void setUseUndefinedNames ( bool value ) { Concepts.setAllowFresh(value); } |
| 948 | /// set flag to use node cache to value VAL |
| 949 | void setUseNodeCache ( bool val ) { useNodeCache = val; } |
| 950 | |
| 951 | //----------------------------------------------------------------------------- |
| 952 | //-- public parser ensure* interface |
| 953 | //----------------------------------------------------------------------------- |
| 954 | |
| 955 | /// return registered concept by given NAME; @return NULL if can't register |
| 956 | TConcept* getConcept ( const std::string& name ) { return Concepts.get(name); } |
| 957 | /// return registered individual by given NAME; @return NULL if can't register |
| 958 | TIndividual* getIndividual ( const std::string& name ) { return Individuals.get(name); } |
| 959 | |
| 960 | /// @return true iff given NAME is a name of a registered individual |
| 961 | bool isIndividual ( const std::string& name ) const { return Individuals.isRegistered(name); } |
| 962 | /// @return true iff given ENTRY is a registered individual |
| 963 | bool isIndividual ( const TNamedEntry* entry ) const { return isIndividual(entry->getName()); } |
| 964 | /// @return true iff given TREE represents a registered individual |
| 965 | bool isIndividual ( const DLTree* tree ) const |
| 966 | { return (tree->Element().getToken() == INAME && isIndividual(tree->Element().getNE())); } |
| 967 | /// @return true iff given DLTree represents a data value |
| 968 | static bool isDataValue ( const DLTree* entry ) |
| 969 | { |
| 970 | return entry->Element().getToken() == DATAEXPR && |
| 971 | static_cast<const TDataEntry*>(entry->Element().getNE())->isDataValue(); |
| 972 | } |
| 973 | |
| 974 | /// get TOP/BOTTOM/CN/IN by the DLTree entry |
| 975 | TConcept* getCI ( const DLTree* name ) |
| 976 | { |
| 977 | if ( name->Element() == TOP ) |
| 978 | return pTop; |
| 979 | if ( name->Element() == BOTTOM ) |
| 980 | return pBottom; |
| 981 | |
| 982 | if ( !isName(name) ) |
| 983 | return nullptr; |
| 984 | |
| 985 | if ( name->Element().getToken() == CNAME ) |
| 986 | return toConcept(name->Element().getNE()); |
| 987 | else |
| 988 | return toIndividual(name->Element().getNE()); |
| 989 | } |
| 990 | /// get a DL tree by a given concept-like C |
| 991 | DLTree* getTree ( TConcept* C ) const |
| 992 | { |
| 993 | if ( C == nullptr ) |
| 994 | return nullptr; |
| 995 | if ( C == pTop ) |
| 996 | return createTop(); |
| 997 | if ( C == pBottom ) |
| 998 | return createBottom(); |
| 999 | return createEntry ( isIndividual(C) ? INAME : CNAME, C ); |
| 1000 | } |
| 1001 | /// get fresh concept |
| 1002 | DLTree* getFreshConcept ( void ) const { return createEntry ( CNAME, pTemp ); } |
| 1003 | |
| 1004 | // n-ary absorption support |
| 1005 | |
| 1006 | /// get unique aux concept |
| 1007 | TConcept* getAuxConcept ( DLTree* desc = nullptr ); |
| 1008 | /// replace RC=(AR:~C) with X such that C [= AR^-:X for fresh X. @return X |
| 1009 | TConcept* replaceForall ( DLTree* RC ); |
| 1010 | /// @return true iff C has a cyclic definition, ie is referenced in its own description |
| 1011 | bool isCyclic ( TConcept* C ) const |
| 1012 | { |
| 1013 | ConceptSet processed; |
| 1014 | return isReferenced ( C, C, processed ); |
| 1015 | } |
| 1016 | |
| 1017 | //----------------------------------------------------------------------------- |
| 1018 | //-- public input axiom interface |
| 1019 | //----------------------------------------------------------------------------- |
| 1020 | |
| 1021 | /// register individual relation <a,b>:R |
| 1022 | void RegisterIndividualRelation ( TNamedEntry* a, TNamedEntry* R, TNamedEntry* b ) |
| 1023 | { |
| 1024 | if ( !isIndividual(a) || !isIndividual(b) ) |
| 1025 | throw EFaCTPlusPlus("Individual expected in related()"); |
| 1026 | RelatedI.push_back ( new |
| 1027 | TRelated ( toIndividual(a), |
| 1028 | toIndividual(b), |
| 1029 | static_cast<TRole*>(R) ) ); |
| 1030 | RelatedI.push_back ( new |
| 1031 | TRelated ( toIndividual(b), |
| 1032 | toIndividual(a), |
| 1033 | static_cast<TRole*>(R)->inverse() ) ); |
| 1034 | } |
| 1035 | |
| 1036 | /// add general subsumption axiom C [= D |
| 1037 | void addSubsumeAxiom ( DLTree* C, DLTree* D ); |
| 1038 | /// add axiom CN [= D for concept CN |
| 1039 | void addSubsumeAxiom ( TConcept* C, DLTree* D ) { addSubsumeAxiom ( getTree(C), D ); } |
| 1040 | /// add an axiom CN [= E for defined CN (CN=D already in base) |
| 1041 | void addSubsumeForDefined ( TConcept* C, DLTree* E ); |
| 1042 | /// add an axiom LHS = RHS |
| 1043 | void addEqualityAxiom ( DLTree* lhs, DLTree* rhs ); |
| 1044 | |
| 1045 | /// add simple rule RULE to the TBox' rules |
| 1046 | inline |
| 1047 | void addSimpleRule ( TSimpleRule* Rule ) |
| 1048 | { |
| 1049 | initRuleFields ( Rule->Body, SimpleRules.size() ); |
| 1050 | SimpleRules.push_back(Rule); |
| 1051 | } |
| 1052 | /// add binary simple rule (C0,C1)=>H |
| 1053 | void addBSimpleRule ( TConcept* C0, TConcept* C1, DLTree* H ) |
| 1054 | { |
| 1055 | ConceptVector v; |
| 1056 | v.push_back(C0); |
| 1057 | v.push_back(C1); |
| 1058 | addSimpleRule ( new TSimpleRule ( v, H ) ); |
| 1059 | } |
| 1060 | |
| 1061 | // external-set methods for set-of-concept-expressions |
| 1062 | void processEquivalentC ( ea_iterator beg, ea_iterator end ); |
| 1063 | void processDisjointC ( ea_iterator beg, ea_iterator end ); |
| 1064 | void processEquivalentR ( ea_iterator beg, ea_iterator end ); |
| 1065 | void processDisjointR ( ea_iterator beg, ea_iterator end ); |
| 1066 | void processSame ( ea_iterator beg, ea_iterator end ); |
| 1067 | void processDifferent ( ea_iterator beg, ea_iterator end ); |
| 1068 | |
| 1069 | /// let TBox know that the whole ontology is loaded |
| 1070 | void finishLoading ( void ) { setForbidUndefinedNames(true); } |
| 1071 | /// @return true if KB contains fairness constraints |
| 1072 | bool hasFC ( void ) const { return !Fairness.empty(); } |
| 1073 | /// add concept expression C as a fairness constraint |
| 1074 | void setFairnessConstraint ( ea_iterator beg, ea_iterator end ) |
| 1075 | { |
| 1076 | for ( ; beg < end; ++beg ) |
| 1077 | if ( isName(*beg) ) |
| 1078 | { |
| 1079 | Fairness.push_back(getCI(*beg)); |
| 1080 | deleteTree(*beg); |
| 1081 | } |
| 1082 | else |
| 1083 | { |
| 1084 | // build a flag for a FC |
| 1085 | TConcept* fc = getAuxConcept(); |
| 1086 | Fairness.push_back(fc); |
| 1087 | // make an axiom: FC = C |
| 1088 | addEqualityAxiom ( getTree(fc), *beg ); |
| 1089 | } |
| 1090 | // in presence of fairness constraints use ancestor blocking |
| 1091 | if ( useAnywhereBlocking && hasFC() ) |
| 1092 | { |
| 1093 | useAnywhereBlocking = false; |
| 1094 | if ( LLM.isWritable(llAlways) ) |
| 1095 | LL << "\nFairness constraints: set useAnywhereBlocking = 0"; |
| 1096 | } |
| 1097 | } |
| 1098 | |
| 1099 | //----------------------------------------------------------------------------- |
| 1100 | //-- public access interface |
| 1101 | //----------------------------------------------------------------------------- |
| 1102 | |
| 1103 | /// GCI Axioms access |
| 1104 | BipolarPointer getTG ( void ) const { return T_G; } |
| 1105 | /// get simple rule by its INDEX |
| 1106 | const TSimpleRule* getSimpleRule ( size_t index ) const { return SimpleRules[index]; } |
| 1107 | |
| 1108 | /// check if the relevant part of KB contains inverse roles. |
| 1109 | bool isIRinQuery ( void ) const |
| 1110 | { |
| 1111 | if ( curFeature != nullptr ) |
| 1112 | return curFeature->hasInverseRole(); |
| 1113 | else |
| 1114 | return KBFeatures.hasInverseRole(); |
| 1115 | } |
| 1116 | /// check if the relevant part of KB contains number restrictions. |
| 1117 | bool isNRinQuery ( void ) const |
| 1118 | { |
| 1119 | const LogicFeatures* p = curFeature ? curFeature : &KBFeatures; |
| 1120 | return p->hasFunctionalRestriction() || p->hasNumberRestriction() || p->hasQNumberRestriction(); |
| 1121 | } |
| 1122 | /// check if the relevant part of KB contains singletons |
| 1123 | bool testHasNominals ( void ) const |
| 1124 | { |
| 1125 | if ( curFeature != nullptr ) |
| 1126 | return curFeature->hasSingletons(); |
| 1127 | else |
| 1128 | return KBFeatures.hasSingletons(); |
| 1129 | } |
| 1130 | /// check if the relevant part of KB contains top role |
| 1131 | bool testHasTopRole ( void ) const |
| 1132 | { |
| 1133 | if ( curFeature != nullptr ) |
| 1134 | return curFeature->hasTopRole(); |
| 1135 | else |
| 1136 | return KBFeatures.hasTopRole(); |
| 1137 | } |
| 1138 | /// check if Sorted Reasoning is applicable |
| 1139 | bool canUseSortedReasoning ( void ) const |
| 1140 | { return useSortedReasoning && !GCIs.isGCI() && !GCIs.isReflexive(); } |
| 1141 | |
| 1142 | /// @return true iff individual C is known to be p-blocked by another one |
| 1143 | bool isBlockedInd ( TConcept* C ) const { return SameI.find(C) != SameI.end(); } |
| 1144 | /// get individual that blocks C; works only for blocked individuals C |
| 1145 | TIndividual* getBlockingInd ( TConcept* C ) const { return SameI.find(C)->second.first; } |
| 1146 | /// @return true iff an individual blocks C deterministically |
| 1147 | bool isBlockingDet ( TConcept* C ) const { return SameI.find(C)->second.second; } |
| 1148 | |
| 1149 | //----------------------------------------------------------------------------- |
| 1150 | //-- public iterators |
| 1151 | //----------------------------------------------------------------------------- |
| 1152 | |
| 1153 | /// RO begin() for concepts |
| 1154 | c_const_iterator c_begin ( void ) const { return Concepts.begin(); } |
| 1155 | /// RO end() for concepts |
| 1156 | c_const_iterator c_end ( void ) const { return Concepts.end(); } |
| 1157 | |
| 1158 | /// RO begin() for individuals |
| 1159 | i_const_iterator i_begin ( void ) const { return Individuals.begin(); } |
| 1160 | /// RO end() for individuals |
| 1161 | i_const_iterator i_end ( void ) const { return Individuals.end(); } |
| 1162 | |
| 1163 | //----------------------------------------------------------------------------- |
| 1164 | //-- public reasoning interface |
| 1165 | //----------------------------------------------------------------------------- |
| 1166 | /// prepare to reasoning |
| 1167 | void prepareReasoning ( void ); |
| 1168 | /// perform classification (assuming KB is consistent) |
| 1169 | void performClassification ( void ) { createTaxonomy ( /*needIndividuals=*/false ); } |
| 1170 | /// perform realisation (assuming KB is consistent) |
| 1171 | void performRealisation ( void ) { createTaxonomy ( /*needIndividuals=*/true ); } |
| 1172 | /// reclassify taxonomy wrt changed sets |
| 1173 | void reclassify ( const std::set<const TNamedEntity*>& MPlus, const std::set<const TNamedEntity*>& MMinus ); |
| 1174 | |
| 1175 | /// get (READ-WRITE) access to internal Taxonomy of concepts |
| 1176 | Taxonomy* getTaxonomy ( void ) { return pTax; } |
| 1177 | |
| 1178 | /// set given structure as a progress monitor |
| 1179 | void setProgressMonitor ( TProgressMonitor* pMon ) { pMonitor = pMon; } |
| 1180 | /// check that reasoning progress was cancelled by external application |
| 1181 | bool isCancelled ( void ) const { return pMonitor != nullptr && pMonitor->isCancelled(); } |
| 1182 | /// set verbose output (ie, default progress monitor, concept and role taxonomies) wrt given VALUE |
| 1183 | void setVerboseOutput ( bool value ) { verboseOutput = value; } |
| 1184 | |
| 1185 | /// create (and DAG-ify) query concept via its definition |
| 1186 | TConcept* createQueryConcept ( const DLTree* query ); |
| 1187 | /// preprocess query concept: put description into DAG |
| 1188 | void preprocessQueryConcept ( TConcept* query ); |
| 1189 | /// classify query concept |
| 1190 | void classifyQueryConcept ( void ); |
| 1191 | /// delete all query-related stuff |
| 1192 | void clearQueryConcept ( void ) { DLHeap.removeQuery(); } |
| 1193 | /// @return true if the concept in question is a query concept |
| 1194 | bool isComplexQuery ( const TConcept* queryConcept ) const { return queryConcept == pQuery; } |
| 1195 | |
| 1196 | //----------------------------------------------------------------------------- |
| 1197 | //-- public reasoning interface |
| 1198 | //----------------------------------------------------------------------------- |
| 1199 | |
| 1200 | /// get status flag |
| 1201 | KBStatus getStatus ( void ) const { return Status; } |
| 1202 | /// set consistency flag |
| 1203 | void setConsistency ( bool val ) |
| 1204 | { |
| 1205 | Status = kbCChecked; |
| 1206 | Consistent = val; |
| 1207 | } |
| 1208 | /// check if the ontology is consistent |
| 1209 | bool isConsistent ( void ) |
| 1210 | { |
| 1211 | if ( Status < kbCChecked ) |
| 1212 | { |
| 1213 | prepareReasoning(); |
| 1214 | if ( Status < kbCChecked && Consistent ) // we can detect inconsistency during preprocessing |
| 1215 | setConsistency(performConsistencyCheck()); |
| 1216 | } |
| 1217 | return Consistent; |
| 1218 | } |
| 1219 | /// check if a subsumption C [= D holds |
| 1220 | bool isSubHolds ( const TConcept* C, const TConcept* D ); |
| 1221 | /// check if a concept C is satisfiable |
| 1222 | bool isSatisfiable ( const TConcept* C ); |
| 1223 | /// check that 2 individuals are the same |
| 1224 | bool isSameIndividuals ( const TIndividual* a, const TIndividual* b ); |
| 1225 | /// check if 2 roles are disjoint |
| 1226 | bool isDisjointRoles ( const TRole* R, const TRole* S ); |
| 1227 | /// check if the role R is irreflexive |
| 1228 | bool isIrreflexive ( const TRole* R ); |
| 1229 | |
| 1230 | /// fills cache entry for given concept; SUB means that the concept is on the right side of a subsumption test |
| 1231 | const modelCacheInterface* initCache ( const TConcept* pConcept, bool sub = false ); |
| 1232 | |
| 1233 | /// build a completion tree for a concept C (no caching as it breaks the idea of KE). @return the root node |
| 1234 | const DlCompletionTree* buildCompletionTree ( const TConcept* C ); |
| 1235 | |
| 1236 | /// test if 2 concept non-subsumption can be determined by cache merging |
| 1237 | enum modelCacheState testCachedNonSubsumption ( const TConcept* p, const TConcept* q ); |
| 1238 | /// test if 2 concept non-subsumption can be determined by sorts checking |
| 1239 | bool testSortedNonSubsumption ( const TConcept* p, const TConcept* q ) |
| 1240 | { |
| 1241 | // sorted reasoning doesn't work in presence of GCIs |
| 1242 | if ( !canUseSortedReasoning() ) |
| 1243 | return false; |
| 1244 | // doesn't work for the SAT tests |
| 1245 | if ( q == nullptr ) |
| 1246 | return false; |
| 1247 | return !DLHeap.haveSameSort ( p->pName, q->pName ); |
| 1248 | } |
| 1249 | //----------------------------------------------------------------------------- |
| 1250 | //-- public output interface |
| 1251 | //----------------------------------------------------------------------------- |
| 1252 | |
| 1253 | /// dump query processing TIME, reasoning statistics and a (preprocessed) TBox |
| 1254 | void writeReasoningResult ( std::ostream& o, float time ) const; |
| 1255 | /// print TBox as a whole |
| 1256 | void Print ( std::ostream& o ) const |
| 1257 | { |
| 1258 | DLHeap.PrintStat(o); |
| 1259 | ORM.Print ( o, "Object" ); |
| 1260 | DRM.Print ( o, "Data" ); |
| 1261 | PrintConcepts(o); |
| 1262 | PrintIndividuals(o); |
| 1263 | PrintSimpleRules(o); |
| 1264 | PrintAxioms(o); |
| 1265 | DLHeap.Print(o); |
| 1266 | } |
| 1267 | |
| 1268 | /// create dump of relevant part of query using given method |
| 1269 | void dump ( dumpInterface* dump ) const; |
| 1270 | |
| 1271 | //----------------------------------------------------------------------------- |
| 1272 | //-- save/load interface; implementation in SaveLoad.cpp |
| 1273 | //----------------------------------------------------------------------------- |
| 1274 | |
| 1275 | /// save the KB into the given stream |
| 1276 | void Save ( SaveLoadManager& m ); |
| 1277 | /// load the KB from given stream wrt STATUS |
| 1278 | void Load ( SaveLoadManager& m, KBStatus status ); |
| 1279 | /// save taxonomy with names (used in the incremental) |
| 1280 | void SaveTaxonomy ( SaveLoadManager& m, const std::set<const TNamedEntry*>& excluded ); |
| 1281 | /// load taxonomy with names (used in the incremental) |
| 1282 | void LoadTaxonomy ( SaveLoadManager& m ); |
| 1283 | }; // TBox |
| 1284 | |
| 1285 | #endif |