| 1 | /* This file is part of the FaCT++ DL reasoner |
| 2 | Copyright (C) 2003-2015 Dmitry Tsarkov and The University of Manchester |
| 3 | Copyright (C) 2015-2017 Dmitry Tsarkov |
| 4 | |
| 5 | This library is free software; you can redistribute it and/or |
| 6 | modify it under the terms of the GNU Lesser General Public |
| 7 | License as published by the Free Software Foundation; either |
| 8 | version 2.1 of the License, or (at your option) any later version. |
| 9 | |
| 10 | This library is distributed in the hope that it will be useful, |
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | Lesser General Public License for more details. |
| 14 | |
| 15 | You should have received a copy of the GNU Lesser General Public |
| 16 | License along with this library; if not, write to the Free Software |
| 17 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 | */ |
| 19 | |
| 20 | #include "dlTBox.h" |
| 21 | |
| 22 | //----------------------------------------------------------------------------- |
| 23 | //-- Subsumption axioms and support |
| 24 | //----------------------------------------------------------------------------- |
| 25 | |
| 26 | // return true if undefined concept found |
| 27 | void TBox :: addSubsumeAxiom ( DLTree* sub, DLTree* sup ) |
| 28 | { |
| 29 | // for C [= C: nothing to do |
| 30 | if ( equalTrees ( sub, sup ) ) |
| 31 | { |
| 32 | deleteTree(sub); |
| 33 | deleteTree(sup); |
| 34 | return; |
| 35 | } |
| 36 | |
| 37 | // try to apply C [= CN |
| 38 | if ( isCN(sup) ) |
| 39 | { |
| 40 | sup = applyAxiomCToCN ( sub, sup ); |
| 41 | if ( sup == nullptr ) |
| 42 | return; |
| 43 | } |
| 44 | |
| 45 | // try to apply CN [= C |
| 46 | if ( isCN(sub) ) |
| 47 | { |
| 48 | sub = applyAxiomCNToC ( sub, sup ); |
| 49 | if ( sub == nullptr ) |
| 50 | return; |
| 51 | } |
| 52 | |
| 53 | // check if an axiom looks like T [= \AR.C |
| 54 | if ( axiomToRangeDomain ( sub, sup ) ) |
| 55 | ; |
| 56 | else // general axiom |
| 57 | processGCI ( sub, sup ); |
| 58 | } |
| 59 | |
| 60 | /// tries to apply axiom D [= CN; @return NULL if applicable or new CN |
| 61 | DLTree* |
| 62 | TBox :: applyAxiomCToCN ( DLTree* D, DLTree* CN ) |
| 63 | { |
| 64 | TConcept* C = resolveSynonym(getCI(CN)); |
| 65 | if ( C == nullptr ) // not applicable |
| 66 | return CN; |
| 67 | |
| 68 | // D [= BOTTOM: transform later as GCI |
| 69 | if ( C == pBottom ) |
| 70 | { |
| 71 | deleteTree(CN); |
| 72 | return createBottom(); |
| 73 | } |
| 74 | |
| 75 | // D [= TOP: nothing to do |
| 76 | if ( C == pTop ) |
| 77 | deleteTree(D); |
| 78 | // check for D [= CN with CN [= D already defined |
| 79 | // don't do this for D is a DN and C is an individual as cycle detection will do it better |
| 80 | else if ( equalTrees ( C->Description, D ) && !( C->isSingleton() && isName(D) ) ) |
| 81 | deleteTree ( makeNonPrimitive(C,D) ); |
| 82 | else // n/a |
| 83 | return CN; |
| 84 | |
| 85 | // success |
| 86 | deleteTree(CN); |
| 87 | return nullptr; |
| 88 | } |
| 89 | |
| 90 | /// tries to apply axiom CN [= D; @return NULL if applicable or new CN |
| 91 | DLTree* |
| 92 | TBox :: applyAxiomCNToC ( DLTree* CN, DLTree* D ) |
| 93 | { |
| 94 | TConcept* C = resolveSynonym(getCI(CN)); |
| 95 | if ( C == nullptr ) // not applicable |
| 96 | return CN; |
| 97 | |
| 98 | // TOP [= D: transform later as GCI |
| 99 | if ( C == pTop ) |
| 100 | { |
| 101 | deleteTree(CN); |
| 102 | return createTop(); |
| 103 | } |
| 104 | |
| 105 | // BOTTOM [= D: nothing to do |
| 106 | if ( C == pBottom ) |
| 107 | deleteTree(D); |
| 108 | else if ( C->isPrimitive() ) |
| 109 | C->addDesc(D); |
| 110 | else // C is defined |
| 111 | addSubsumeForDefined ( C, D ); |
| 112 | |
| 113 | // success |
| 114 | deleteTree(CN); |
| 115 | return nullptr; |
| 116 | } |
| 117 | |
| 118 | /// add an axiom CN [= E for defined CN (CN=D already in base) |
| 119 | void |
| 120 | TBox :: addSubsumeForDefined ( TConcept* C, DLTree* E ) |
| 121 | { |
| 122 | // if E is a syntactic sub-class of D, then nothing to do |
| 123 | if ( isSubTree ( E, C->Description ) ) |
| 124 | { |
| 125 | deleteTree(E); |
| 126 | return; |
| 127 | } |
| 128 | |
| 129 | // try to see whether C contains a reference to itself at the top level |
| 130 | if ( C->hasSelfInDesc() ) |
| 131 | { |
| 132 | // remember the old description value |
| 133 | DLTree* D = clone(C->Description); |
| 134 | // remove C from the description |
| 135 | C->removeSelfFromDescription(); |
| 136 | // the trees should differ here |
| 137 | fpp_assert ( !equalTrees ( D, C->Description ) ); |
| 138 | // note that we don't know exact semantics of C for now; |
| 139 | // we need to split it's definition and work via GCIs |
| 140 | makeDefinitionPrimitive ( C, E, D ); |
| 141 | } |
| 142 | else // here we have the definition of C = D, and subsumption C [= E |
| 143 | { |
| 144 | if ( 1 ) // for now: it's not clear of what's going wrong |
| 145 | processGCI ( getTree(C), E ); |
| 146 | else // here we leave the definition of C = D, and delay the processing of C [= E |
| 147 | { |
| 148 | ConceptDefMap::iterator p = ExtraConceptDefs.find(C); |
| 149 | if ( p == ExtraConceptDefs.end() ) // no such entry |
| 150 | ExtraConceptDefs[C] = E; // save C [= E |
| 151 | else // we have C [= X; change to C [= (X and E) |
| 152 | p->second = createSNFAnd ( p->second, E ); |
| 153 | } |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | bool TBox :: axiomToRangeDomain ( DLTree* sub, DLTree* sup ) |
| 158 | { |
| 159 | // applicability check for T [= A R.C |
| 160 | if ( sub->Element() == TOP && sup->Element () == FORALL ) |
| 161 | { |
| 162 | resolveRole(sup->Left())->setRange(clone(sup->Right())); |
| 163 | // free unused memory |
| 164 | deleteTree(sub); |
| 165 | deleteTree(sup); |
| 166 | return true; |
| 167 | } |
| 168 | // applicability check for E R.T [= D |
| 169 | if ( sub->Element() == NOT && sub->Left()->Element() == FORALL && sub->Left()->Right()->Element() == BOTTOM ) |
| 170 | { |
| 171 | resolveRole(sub->Left()->Left())->setDomain(sup); |
| 172 | deleteTree(sub); |
| 173 | return true; |
| 174 | } |
| 175 | return false; |
| 176 | } |
| 177 | |
| 178 | //----------------------------------------------------------------------------- |
| 179 | //-- Equality axioms and support |
| 180 | //----------------------------------------------------------------------------- |
| 181 | |
| 182 | /// process the definition LHS = RHS |
| 183 | void |
| 184 | TBox :: addEqualityAxiom ( DLTree* lhs, DLTree* rhs ) |
| 185 | { |
| 186 | // check whether LHS is a named concept |
| 187 | TConcept* C = resolveSynonym(getCI(lhs)); |
| 188 | bool isNamedLHS = ( C && C != pTop && C != pBottom ); |
| 189 | |
| 190 | // check whether RHS is a named concept |
| 191 | TConcept* D = resolveSynonym(getCI(rhs)); |
| 192 | bool isNamedRHS = ( D && D != pTop && D != pBottom ); |
| 193 | |
| 194 | // try to make a definition C = RHS for C with no definition |
| 195 | if ( isNamedLHS && addNonprimitiveDefinition ( C, rhs ) ) |
| 196 | { |
| 197 | deleteTree(lhs); |
| 198 | return; |
| 199 | } |
| 200 | |
| 201 | // try to make a definition RHS = LHS for RHS = C with no definition |
| 202 | if ( isNamedRHS && addNonprimitiveDefinition ( D, lhs ) ) |
| 203 | { |
| 204 | deleteTree(rhs); |
| 205 | return; |
| 206 | } |
| 207 | |
| 208 | // try to make a definition C = RHS for C [= D |
| 209 | if ( isNamedLHS && switchToNonprimitive ( C, rhs ) ) |
| 210 | { |
| 211 | deleteTree(lhs); |
| 212 | return; |
| 213 | } |
| 214 | |
| 215 | // try to make a definition RHS = LHS for RHS = C with C [= D |
| 216 | if ( isNamedRHS && switchToNonprimitive ( D, lhs ) ) |
| 217 | { |
| 218 | deleteTree(rhs); |
| 219 | return; |
| 220 | } |
| 221 | |
| 222 | // fail to make a concept definition; separate the definition |
| 223 | addSubsumeAxiom ( clone(lhs), clone(rhs) ); |
| 224 | addSubsumeAxiom ( rhs, lhs ); |
| 225 | } |
| 226 | |
| 227 | /// tries to add C = RHS for the concept C; @return true if OK |
| 228 | bool |
| 229 | TBox :: addNonprimitiveDefinition ( TConcept* C, DLTree* rhs ) |
| 230 | { |
| 231 | // check whether the case is C=D for a (concept-like) D |
| 232 | TConcept* D = getCI(rhs); |
| 233 | |
| 234 | // nothing to do for the case C := D for named concepts C,D with D = C already |
| 235 | if ( D && resolveSynonym(D) == C ) |
| 236 | { |
| 237 | deleteTree(rhs); |
| 238 | return true; |
| 239 | } |
| 240 | |
| 241 | // can't have C=D where C is a nominal and D is a concept |
| 242 | if ( C->isSingleton() && D != nullptr && !D->isSingleton() ) |
| 243 | return false; |
| 244 | |
| 245 | // check the case whether C=RHS or C [= \top |
| 246 | if ( C->canInitNonPrim(rhs) ) |
| 247 | { |
| 248 | // delete return value in case of (possibly) duplicated description |
| 249 | deleteTree ( makeNonPrimitive ( C, rhs ) ); |
| 250 | return true; |
| 251 | } |
| 252 | |
| 253 | // can't make definition |
| 254 | return false; |
| 255 | } |
| 256 | |
| 257 | /// tries to add C = RHS for the concept C [= X; @return true if OK |
| 258 | bool |
| 259 | TBox :: switchToNonprimitive ( TConcept* C, DLTree* rhs ) |
| 260 | { |
| 261 | // make sure that we avoid making an individual equals to something else |
| 262 | TConcept* D = resolveSynonym(getCI(rhs)); |
| 263 | if ( C->isSingleton() && D && !D->isSingleton() ) |
| 264 | return false; |
| 265 | |
| 266 | // check whether we process C=D where C is defined as C[=E |
| 267 | if ( alwaysPreferEquals && C->isPrimitive() ) // change C to C=... with additional GCI C[=x |
| 268 | { |
| 269 | addSubsumeForDefined ( C, makeNonPrimitive(C,rhs) ); |
| 270 | return true; |
| 271 | } |
| 272 | |
| 273 | return false; |
| 274 | } |
| 275 | |
| 276 | //----------------------------------------------------------------------------- |
| 277 | //-- N-ary concept axioms |
| 278 | //----------------------------------------------------------------------------- |
| 279 | |
| 280 | void TBox :: processDisjointC ( ea_iterator beg, ea_iterator end ) |
| 281 | { |
| 282 | ExpressionArray prim, rest; |
| 283 | |
| 284 | for ( ; beg < end; ++beg ) |
| 285 | if ( isName(*beg) && |
| 286 | static_cast<const TConcept*>((*beg)->Element().getNE())->isPrimitive() ) |
| 287 | prim.push_back(*beg); |
| 288 | else |
| 289 | rest.push_back(*beg); |
| 290 | |
| 291 | // both primitive concept and others are in DISJ statement |
| 292 | if ( !prim.empty() && !rest.empty() ) |
| 293 | { |
| 294 | DLTree* nrest = buildDisjAux ( rest.begin(), rest.end() ); |
| 295 | |
| 296 | for ( ea_iterator q = prim.begin(), q_end = prim.end(); q < q_end; ++q ) |
| 297 | addSubsumeAxiom ( clone(*q), clone(nrest) ); |
| 298 | |
| 299 | deleteTree(nrest); |
| 300 | } |
| 301 | |
| 302 | // no primitive concepts between DJ elements |
| 303 | |
| 304 | if ( !rest.empty() ) |
| 305 | processDisjoint ( rest.begin(), rest.end() ); |
| 306 | |
| 307 | // all non-PC are done; prim is non-empty |
| 308 | // FIXME!! do it in more optimal way later |
| 309 | if ( !prim.empty() ) |
| 310 | processDisjoint ( prim.begin(), prim.end() ); |
| 311 | } |
| 312 | |
| 313 | void TBox :: processEquivalentC ( ea_iterator beg, ea_iterator end ) |
| 314 | { |
| 315 | for ( ; beg+1 < end; ++beg ) |
| 316 | addEqualityAxiom ( *beg, clone(*(beg+1)) ); |
| 317 | // now beg+1 == end, so beg points to the last element |
| 318 | deleteTree(*beg); |
| 319 | } |
| 320 | |
| 321 | //----------------------------------------------------------------------------- |
| 322 | //-- N-ary individual axioms |
| 323 | //----------------------------------------------------------------------------- |
| 324 | |
| 325 | void TBox :: processDifferent ( ea_iterator beg, ea_iterator end ) |
| 326 | { |
| 327 | SingletonVector acc; |
| 328 | for ( ; beg < end; ++beg ) |
| 329 | if ( isIndividual(*beg) ) // only nominals in DIFFERENT command |
| 330 | { |
| 331 | acc.push_back(toIndividual((*beg)->Element().getNE())); |
| 332 | deleteTree(*beg); |
| 333 | } |
| 334 | else |
| 335 | throw EFaCTPlusPlus("Only individuals allowed in processDifferent()"); |
| 336 | |
| 337 | // register vector of disjoint nominals in proper place |
| 338 | if ( acc.size() > 1 ) |
| 339 | Different.push_back(acc); |
| 340 | } |
| 341 | |
| 342 | void TBox :: processSame ( ea_iterator beg, ea_iterator end ) |
| 343 | { |
| 344 | if ( beg == end ) |
| 345 | return; |
| 346 | |
| 347 | if ( !isIndividual(*beg) ) // only nominals in SAME command |
| 348 | throw EFaCTPlusPlus("Only individuals allowed in processSame()"); |
| 349 | |
| 350 | for ( ; beg+1 < end; ++beg ) |
| 351 | { |
| 352 | if ( !isIndividual(*(beg+1)) ) |
| 353 | throw EFaCTPlusPlus("Only individuals allowed in processSame()"); |
| 354 | addEqualityAxiom ( *beg, clone(*(beg+1)) ); |
| 355 | } |
| 356 | // now beg+1 == end, so beg points to the last element |
| 357 | deleteTree(*beg); |
| 358 | } |
| 359 | |
| 360 | //----------------------------------------------------------------------------- |
| 361 | //-- N-ary role axioms |
| 362 | //----------------------------------------------------------------------------- |
| 363 | |
| 364 | void TBox :: processDisjointR ( ea_iterator beg, ea_iterator end ) |
| 365 | { |
| 366 | if ( beg == end ) |
| 367 | throw EFaCTPlusPlus("Empty disjoint role axiom"); |
| 368 | |
| 369 | ea_iterator p, q; |
| 370 | |
| 371 | // check that all id's are correct role names |
| 372 | for ( p = beg; p < end; ++p ) |
| 373 | if ( isTopRole(*p) ) |
| 374 | throw EFaCTPlusPlus("Universal role in the disjoint roles axiom"); |
| 375 | |
| 376 | RoleMaster& RM = getRM(resolveRole(*beg)); |
| 377 | |
| 378 | // make a disjoint roles |
| 379 | for ( p = beg; p < end; ++p ) |
| 380 | { |
| 381 | TRole* r = resolveRole(*p); |
| 382 | |
| 383 | // FIXME: this could be done more optimal... |
| 384 | for ( q = p+1; q < end; ++q ) |
| 385 | RM.addDisjointRoles ( r, resolveRole(*q) ); |
| 386 | |
| 387 | deleteTree(*p); |
| 388 | } |
| 389 | } |
| 390 | |
| 391 | void TBox :: processEquivalentR ( ea_iterator beg, ea_iterator end ) |
| 392 | { |
| 393 | if ( beg != end ) |
| 394 | { |
| 395 | RoleMaster& RM = getRM(resolveRole(*beg)); |
| 396 | for ( ; beg != end-1; ++beg ) |
| 397 | { |
| 398 | RM.addRoleSynonym ( resolveRole(*beg), resolveRole(*(beg+1)) ); |
| 399 | deleteTree(*beg); |
| 400 | } |
| 401 | deleteTree(*beg); |
| 402 | } |
| 403 | } |
| 404 |