| 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 "tAxiom.h" |
| 21 | #include "dlTBox.h" |
| 22 | |
| 23 | // this is to define the hard cycle in concepts: C = \exists R D, D = \exists S C |
| 24 | // we saw cycles of that type of length 2 |
| 25 | |
| 26 | typedef std::set<const TConcept*> ConceptSet; |
| 27 | |
| 28 | static bool |
| 29 | hasDefCycle ( const TConcept* C, ConceptSet& visited ) |
| 30 | { |
| 31 | // interested in non-primitive |
| 32 | if ( C->isPrimitive() ) |
| 33 | return false; |
| 34 | // already seen -- cycle |
| 35 | if ( visited.count(C) > 0 ) |
| 36 | return true; |
| 37 | // check the structure: looking for the \exists R.C |
| 38 | const DLTree* p = C->Description; |
| 39 | if ( p->Element().getToken() != NOT ) |
| 40 | return false; |
| 41 | p = p->Left(); |
| 42 | if ( p->Element().getToken() != FORALL ) |
| 43 | return false; |
| 44 | p = p->Right(); |
| 45 | if ( p->Element().getToken() != NOT ) |
| 46 | return false; |
| 47 | p = p->Left(); |
| 48 | if ( !isName(p) ) |
| 49 | return false; |
| 50 | // here P is a concept |
| 51 | // remember C |
| 52 | visited.insert(C); |
| 53 | // check p |
| 54 | return hasDefCycle ( static_cast<const TConcept*>(p->Element().getNE()), visited ); |
| 55 | } |
| 56 | |
| 57 | static bool |
| 58 | hasDefCycle ( const TConcept* C ) |
| 59 | { |
| 60 | ConceptSet visited; |
| 61 | return hasDefCycle ( C, visited ); |
| 62 | } |
| 63 | |
| 64 | bool |
| 65 | InAx :: isNP ( const TConcept* C, TBox& ) |
| 66 | { |
| 67 | return C->isNonPrimitive() && !hasDefCycle(C); |
| 68 | } |
| 69 | |
| 70 | /// add DLTree to an axiom |
| 71 | void |
| 72 | TAxiom :: add ( DLTree* p ) |
| 73 | { |
| 74 | if ( InAx::isBot(p) ) // BOT or X == X |
| 75 | return; // nothing to do |
| 76 | // flatten the disjunctions on the fly |
| 77 | if ( InAx::isOr(p) ) |
| 78 | { |
| 79 | add(clone(p->Left())); |
| 80 | add(clone(p->Right())); |
| 81 | deleteTree(p); |
| 82 | return; |
| 83 | } |
| 84 | for ( const auto& C: Disjuncts ) |
| 85 | if ( equalTrees(p,C) ) |
| 86 | { |
| 87 | deleteTree(p); |
| 88 | return; |
| 89 | } |
| 90 | Disjuncts.push_back(p); |
| 91 | } |
| 92 | |
| 93 | TAxiom* |
| 94 | TAxiom :: simplifyCN ( TBox& KB ) const |
| 95 | { |
| 96 | for ( const auto& C: Disjuncts ) |
| 97 | { |
| 98 | if ( InAx::isPosNP(C,KB) ) |
| 99 | return simplifyPosNP(C); |
| 100 | else if ( InAx::isNegNP(C,KB) ) |
| 101 | return simplifyNegNP(C); |
| 102 | } |
| 103 | |
| 104 | return nullptr; |
| 105 | } |
| 106 | |
| 107 | TAxiom* |
| 108 | TAxiom :: simplifyForall ( TBox& KB ) const |
| 109 | { |
| 110 | for ( const auto& C: Disjuncts ) |
| 111 | if ( InAx::isAbsForall(C) ) |
| 112 | return simplifyForall ( C, KB ); |
| 113 | |
| 114 | return nullptr; |
| 115 | } |
| 116 | |
| 117 | TAxiom* |
| 118 | TAxiom :: simplifySForall ( TBox& KB ) const |
| 119 | { |
| 120 | for ( const auto& C: Disjuncts ) |
| 121 | if ( InAx::isSimpleForall(C) ) |
| 122 | return simplifyForall ( C, KB ); |
| 123 | |
| 124 | return nullptr; |
| 125 | } |
| 126 | |
| 127 | TAxiom* |
| 128 | TAxiom :: simplifyForall ( const DLTree* rep, TBox& KB ) const |
| 129 | { |
| 130 | Stat::SAbsRepForall(); |
| 131 | DLTree* pAll = rep->Left(); // (all R ~C) |
| 132 | #ifdef RKG_DEBUG_ABSORPTION |
| 133 | std::cout << " simplify ALL expression" << pAll; |
| 134 | #endif |
| 135 | TAxiom* ret = copy(rep); |
| 136 | ret->add(KB.getTree(KB.replaceForall(clone(pAll)))); |
| 137 | return ret; |
| 138 | } |
| 139 | |
| 140 | DLTree* |
| 141 | TAxiom :: createAnAxiom ( const DLTree* skip ) const |
| 142 | { |
| 143 | // create new OR vertex for the axiom: |
| 144 | DLTree* Or = createTop(); |
| 145 | for ( const auto& C: Disjuncts ) |
| 146 | if ( C != skip ) |
| 147 | Or = createSNFAnd ( clone(C), Or ); |
| 148 | |
| 149 | return createSNFNot(Or); |
| 150 | } |
| 151 | |
| 152 | #ifdef RKG_DEBUG_ABSORPTION |
| 153 | void TAxiom :: dump ( std::ostream& o ) const |
| 154 | { |
| 155 | o << " (neg-and"; |
| 156 | for ( const auto& C: Disjuncts ) |
| 157 | o << C; |
| 158 | o << ")"; |
| 159 | } |
| 160 | #endif |
| 161 | |
| 162 | /// absorb into BOTTOM; @return true if absorption is performed |
| 163 | bool |
| 164 | TAxiom :: absorbIntoBottom ( void ) const |
| 165 | { |
| 166 | absorptionSet Pos, Neg; |
| 167 | for ( const auto& C: Disjuncts ) |
| 168 | switch ( C->Element().getToken() ) |
| 169 | { |
| 170 | case BOTTOM: // axiom in the form T [= T or ...; nothing to do |
| 171 | Stat::SAbsBApply(); |
| 172 | # ifdef RKG_DEBUG_ABSORPTION |
| 173 | std::cout << " Absorb into BOTTOM"; |
| 174 | # endif |
| 175 | return true; |
| 176 | case TOP: // skip it here |
| 177 | break; |
| 178 | case NOT: // something negated: put it into NEG |
| 179 | Neg.push_back(C->Left()); |
| 180 | break; |
| 181 | default: // something positive: save in POS |
| 182 | Pos.push_back(C); |
| 183 | break; |
| 184 | } |
| 185 | |
| 186 | // now check whether there is a concept in both POS and NEG |
| 187 | for ( const auto& neg: Neg ) |
| 188 | for ( const auto& pos: Pos ) |
| 189 | if ( equalTrees ( neg, pos ) ) |
| 190 | { |
| 191 | Stat::SAbsBApply(); |
| 192 | # ifdef RKG_DEBUG_ABSORPTION |
| 193 | std::cout << " Absorb into BOTTOM due to (not" << neg << ") and" << pos; |
| 194 | # endif |
| 195 | return true; |
| 196 | } |
| 197 | return false; |
| 198 | } |
| 199 | |
| 200 | bool |
| 201 | TAxiom :: absorbIntoTop ( TBox& KB ) const |
| 202 | { |
| 203 | TConcept* Cand = nullptr; |
| 204 | |
| 205 | // check whether the axiom is Top [= C |
| 206 | for ( const auto& C: Disjuncts ) |
| 207 | if ( InAx::isBot(C) ) // BOTTOMS are fine |
| 208 | continue; |
| 209 | else if ( InAx::isPosCN(C) ) // CN found |
| 210 | { |
| 211 | if ( Cand != nullptr ) // more than one concept |
| 212 | return false; |
| 213 | Cand = InAx::getConcept(C->Left()); |
| 214 | if ( Cand->isSingleton() ) // doesn't work with nominals |
| 215 | return false; |
| 216 | } |
| 217 | else |
| 218 | return false; |
| 219 | |
| 220 | if ( Cand == nullptr ) |
| 221 | return false; |
| 222 | |
| 223 | // make an absorption |
| 224 | Stat::SAbsTApply(); |
| 225 | DLTree* desc = KB.makeNonPrimitive ( Cand, createTop() ); |
| 226 | |
| 227 | #ifdef RKG_DEBUG_ABSORPTION |
| 228 | std::cout << " T-Absorb GCI to axiom"; |
| 229 | if ( desc ) |
| 230 | std::cout << "s *TOP* [=" << desc << " and"; |
| 231 | std::cout << " " << Cand->getName() << " = *TOP*"; |
| 232 | #endif |
| 233 | if ( desc ) |
| 234 | KB.addSubsumeAxiom ( createTop(), desc ); |
| 235 | |
| 236 | return true; |
| 237 | } |
| 238 | |
| 239 | bool |
| 240 | TAxiom :: absorbIntoConcept ( TBox& KB ) const |
| 241 | { |
| 242 | WorkSet Cons; |
| 243 | DLTree* bestConcept = nullptr; |
| 244 | |
| 245 | // finds all primitive concept names |
| 246 | for ( const auto& C: Disjuncts ) |
| 247 | if ( InAx::isNegPC(C) ) // FIXME!! review this during implementation of Nominal Absorption |
| 248 | { |
| 249 | Stat::SAbsCAttempt(); |
| 250 | Cons.push_back(C); |
| 251 | if ( InAx::getConcept(C)->isSystem() ) |
| 252 | bestConcept = C; |
| 253 | } |
| 254 | |
| 255 | // if no concept names -- return; |
| 256 | if ( Cons.empty() ) |
| 257 | return false; |
| 258 | |
| 259 | Stat::SAbsCApply(); |
| 260 | // FIXME!! as for now: just take the 1st concept name |
| 261 | if ( bestConcept == nullptr ) |
| 262 | bestConcept = Cons[0]; |
| 263 | |
| 264 | // normal concept absorption |
| 265 | TConcept* Concept = InAx::getConcept(bestConcept); |
| 266 | |
| 267 | #ifdef RKG_DEBUG_ABSORPTION |
| 268 | std::cout << " C-Absorb GCI to concept " << Concept->getName(); |
| 269 | if ( Cons.size() > 1 ) |
| 270 | { |
| 271 | std::cout << " (other options are"; |
| 272 | for ( const auto& C: Cons ) |
| 273 | if ( C != bestConcept ) |
| 274 | std::cout << " " << InAx::getConcept(C)->getName(); |
| 275 | std::cout << ")"; |
| 276 | } |
| 277 | #endif |
| 278 | |
| 279 | // adds a new definition |
| 280 | Concept->addDesc(createAnAxiom(bestConcept)); |
| 281 | Concept->removeSelfFromDescription(); |
| 282 | // in case T [= (A or \neg B) and (B and \neg A) there appears a cycle A [= B [= A |
| 283 | // so remove potential cycle |
| 284 | // FIXME!! just because TConcept can't get rid of cycle by itself |
| 285 | KB.clearRelevanceInfo(); |
| 286 | KB.checkToldCycle(Concept); |
| 287 | KB.clearRelevanceInfo(); |
| 288 | |
| 289 | return true; |
| 290 | } |
| 291 | |
| 292 | /// absorb into negation of a concept; @return true if absorption is performed |
| 293 | bool |
| 294 | TAxiom :: absorbIntoNegConcept ( TBox& KB ) const |
| 295 | { |
| 296 | WorkSet Cons; |
| 297 | TConcept* Concept; |
| 298 | const DLTree* bestConcept = nullptr; |
| 299 | |
| 300 | // finds all primitive negated concept names without description |
| 301 | for ( const auto& C: Disjuncts ) |
| 302 | if ( C->Element().getToken() == NOT && isName(C->Left()) |
| 303 | && (Concept = InAx::getConcept(C->Left()))->isPrimitive() |
| 304 | && !Concept->isSingleton() && Concept->Description == nullptr ) |
| 305 | { |
| 306 | Stat::SAbsNAttempt(); |
| 307 | Cons.push_back(C); |
| 308 | } |
| 309 | |
| 310 | // if no concept names -- return; |
| 311 | if ( Cons.empty() ) |
| 312 | return false; |
| 313 | |
| 314 | Stat::SAbsNApply(); |
| 315 | // FIXME!! as for now: just take the 1st concept name |
| 316 | if ( bestConcept == nullptr ) |
| 317 | bestConcept = Cons[0]; |
| 318 | |
| 319 | // normal concept absorption |
| 320 | Concept = InAx::getConcept(bestConcept->Left()); |
| 321 | |
| 322 | #ifdef RKG_DEBUG_ABSORPTION |
| 323 | std::cout << " N-Absorb GCI to concept " << Concept->getName(); |
| 324 | if ( Cons.size() > 1 ) |
| 325 | { |
| 326 | std::cout << " (other options are"; |
| 327 | for ( const auto& C: Cons ) |
| 328 | if ( C != bestConcept ) |
| 329 | std::cout << " " << InAx::getConcept(C->Left())->getName(); |
| 330 | std::cout << ")"; |
| 331 | } |
| 332 | #endif |
| 333 | |
| 334 | // replace ~C [= D with C=~notC, notC [= D: |
| 335 | // make notC [= D |
| 336 | TConcept* nC = KB.getAuxConcept(createAnAxiom(bestConcept)); |
| 337 | // define C = ~notC; C had an empty desc, so it's safe not to delete it |
| 338 | KB.makeNonPrimitive ( Concept, createSNFNot(KB.getTree(nC)) ); |
| 339 | return true; |
| 340 | } |
| 341 | |
| 342 | bool |
| 343 | TAxiom :: absorbIntoDomain ( void ) const |
| 344 | { |
| 345 | WorkSet Cons; |
| 346 | const DLTree* bestSome = nullptr; |
| 347 | |
| 348 | // find all forall concepts |
| 349 | for ( const auto& C: Disjuncts ) |
| 350 | if ( C->Element() == NOT && |
| 351 | ( C->Left()->Element() == FORALL // \neg ER.C |
| 352 | || C->Left()->Element() == LE )) // \neg >= n R.C |
| 353 | { |
| 354 | Stat::SAbsRAttempt(); |
| 355 | Cons.push_back(C); |
| 356 | // check for the direct domain case |
| 357 | if ( C->Left()->Right()->Element() == BOTTOM ) |
| 358 | { // found proper absorption candidate |
| 359 | bestSome = C; |
| 360 | break; |
| 361 | } |
| 362 | } |
| 363 | |
| 364 | // if there are no EXISTS concepts -- return; |
| 365 | if ( Cons.empty() ) |
| 366 | return false; |
| 367 | |
| 368 | Stat::SAbsRApply(); |
| 369 | TRole* Role; |
| 370 | |
| 371 | if ( bestSome != nullptr ) |
| 372 | Role = resolveRole(bestSome->Left()->Left()); |
| 373 | else |
| 374 | // FIXME!! as for now: just take the 1st role name |
| 375 | Role = resolveRole(Cons[0]->Left()->Left()); |
| 376 | |
| 377 | #ifdef RKG_DEBUG_ABSORPTION |
| 378 | std::cout << " R-Absorb GCI to the domain of role " << Role->getName(); |
| 379 | if ( Cons.size() > 1 ) |
| 380 | { |
| 381 | std::cout << " (other options are"; |
| 382 | for ( const auto& C: Cons ) |
| 383 | if ( C != bestSome ) |
| 384 | std::cout << " " << resolveRole(C->Left()->Left())->getName(); |
| 385 | std::cout << ")"; |
| 386 | } |
| 387 | #endif |
| 388 | |
| 389 | // here bestSome is either actual domain, or END(); both cases are fine |
| 390 | Role->setDomain(createAnAxiom(bestSome)); |
| 391 | |
| 392 | return true; |
| 393 | } |