| 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 TONTOLOGYLOADER_H |
| 21 | #define TONTOLOGYLOADER_H |
| 22 | |
| 23 | #include "tOntology.h" |
| 24 | #include "tExpressionTranslator.h" |
| 25 | #include "dlTBox.h" |
| 26 | |
| 27 | class TOntologyLoader: public DLAxiomVisitor |
| 28 | { |
| 29 | protected: // members |
| 30 | /// KB to load the ontology |
| 31 | TBox& kb; |
| 32 | /// Transforms TDLExpression hierarchy to the DLTree* |
| 33 | TExpressionTranslator ETrans; |
| 34 | /// temporary vector for arguments of TBox n-ary axioms |
| 35 | std::vector<DLTree*> ArgList; |
| 36 | |
| 37 | protected: // methods |
| 38 | /// get DLTree corresponding to an expression EXPR |
| 39 | DLTree* e ( const TDLExpression* expr ) |
| 40 | { |
| 41 | expr->accept(ETrans); |
| 42 | return ETrans; |
| 43 | } |
| 44 | /// get role by the DLTree; throw exception if unable |
| 45 | TRole* getRole ( const TDLRoleExpression* r, const char* reason ) |
| 46 | { |
| 47 | try |
| 48 | { |
| 49 | return resolveRole(TreeDeleter(e(r))); |
| 50 | } |
| 51 | catch ( const EFaCTPlusPlus& ) { throw EFaCTPlusPlus(reason); } |
| 52 | } |
| 53 | /// get an individual be the DLTree; throw exception if unable |
| 54 | TIndividual* getIndividual ( const TDLIndividualExpression* I, const char* reason ) |
| 55 | { |
| 56 | try |
| 57 | { |
| 58 | TreeDeleter i{e(I)}; |
| 59 | if ( i == nullptr ) |
| 60 | throw EFaCTPlusPlus(reason); |
| 61 | return static_cast<TIndividual*>(kb.getCI(i)); |
| 62 | } |
| 63 | catch(...) |
| 64 | { |
| 65 | throw EFaCTPlusPlus(reason); |
| 66 | } |
| 67 | } |
| 68 | /// ensure that the expression EXPR has its named entities linked to the KB ones |
| 69 | void ensureNames ( const TDLExpression* Expr ) |
| 70 | { |
| 71 | fpp_assert ( Expr != nullptr ); // FORNOW |
| 72 | } |
| 73 | /// prepare arguments for the [begin,end) interval |
| 74 | template <typename Collection> |
| 75 | void prepareArgList ( Collection argList ) |
| 76 | { |
| 77 | ArgList.clear(); |
| 78 | for ( const auto& expr: argList ) |
| 79 | { |
| 80 | ensureNames(expr); |
| 81 | ArgList.push_back(e(expr)); |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | public: // visitor interface |
| 86 | void visit ( const TDLAxiomDeclaration& axiom ) override |
| 87 | { |
| 88 | ensureNames(axiom.getDeclaration()); |
| 89 | TreeDeleter(e(axiom.getDeclaration())); // register names in the KB |
| 90 | } |
| 91 | |
| 92 | // n-ary axioms |
| 93 | |
| 94 | void visit ( const TDLAxiomEquivalentConcepts& axiom ) override |
| 95 | { |
| 96 | prepareArgList(axiom); |
| 97 | kb.processEquivalentC(ArgList.begin(),ArgList.end()); |
| 98 | } |
| 99 | void visit ( const TDLAxiomDisjointConcepts& axiom ) override |
| 100 | { |
| 101 | prepareArgList(axiom); |
| 102 | kb.processDisjointC(ArgList.begin(),ArgList.end()); |
| 103 | } |
| 104 | void visit ( const TDLAxiomDisjointUnion& axiom ) override |
| 105 | { |
| 106 | // first make a disjoint axiom |
| 107 | prepareArgList(axiom); |
| 108 | kb.processDisjointC(ArgList.begin(),ArgList.end()); |
| 109 | // now define C as a union-of axiom |
| 110 | ArgList.clear(); |
| 111 | ensureNames(axiom.getC()); |
| 112 | ArgList.push_back(e(axiom.getC())); |
| 113 | DLTree* acc = createBottom(); |
| 114 | for ( const auto* arg : axiom ) |
| 115 | acc = createSNFOr ( acc, e(arg) ); |
| 116 | ArgList.push_back(acc); |
| 117 | kb.processEquivalentC(ArgList.begin(),ArgList.end()); |
| 118 | } |
| 119 | void visit ( const TDLAxiomEquivalentORoles& axiom ) override |
| 120 | { |
| 121 | prepareArgList(axiom); |
| 122 | kb.processEquivalentR(ArgList.begin(),ArgList.end()); |
| 123 | } |
| 124 | void visit ( const TDLAxiomEquivalentDRoles& axiom ) override |
| 125 | { |
| 126 | prepareArgList(axiom); |
| 127 | kb.processEquivalentR(ArgList.begin(),ArgList.end()); |
| 128 | } |
| 129 | void visit ( const TDLAxiomDisjointORoles& axiom ) override |
| 130 | { |
| 131 | prepareArgList(axiom); |
| 132 | kb.processDisjointR(ArgList.begin(),ArgList.end()); |
| 133 | } |
| 134 | void visit ( const TDLAxiomDisjointDRoles& axiom ) override |
| 135 | { |
| 136 | prepareArgList(axiom); |
| 137 | kb.processDisjointR(ArgList.begin(),ArgList.end()); |
| 138 | } |
| 139 | void visit ( const TDLAxiomSameIndividuals& axiom ) override |
| 140 | { |
| 141 | prepareArgList(axiom); |
| 142 | kb.processSame(ArgList.begin(),ArgList.end()); |
| 143 | } |
| 144 | void visit ( const TDLAxiomDifferentIndividuals& axiom ) override |
| 145 | { |
| 146 | prepareArgList(axiom); |
| 147 | kb.processDifferent(ArgList.begin(),ArgList.end()); |
| 148 | } |
| 149 | void visit ( const TDLAxiomFairnessConstraint& axiom ) override |
| 150 | { |
| 151 | prepareArgList(axiom); |
| 152 | kb.setFairnessConstraint(ArgList.begin(),ArgList.end()); |
| 153 | } |
| 154 | |
| 155 | // role axioms |
| 156 | |
| 157 | void visit ( const TDLAxiomRoleInverse& axiom ) override |
| 158 | { |
| 159 | ensureNames(axiom.getRole()); |
| 160 | ensureNames(axiom.getInvRole()); |
| 161 | TRole* R = getRole ( axiom.getRole(), "Role expression expected in Role Inverse axiom" ); |
| 162 | TRole* iR = getRole ( axiom.getInvRole(), "Role expression expected in Role Inverse axiom" ); |
| 163 | kb.getRM(R).addRoleSynonym ( iR->inverse(), R ); |
| 164 | } |
| 165 | void visit ( const TDLAxiomORoleSubsumption& axiom ) override |
| 166 | { |
| 167 | ensureNames(axiom.getRole()); |
| 168 | ensureNames(axiom.getSubRole()); |
| 169 | DLTree* Sub = e(axiom.getSubRole()); |
| 170 | TRole* R = getRole ( axiom.getRole(), "Role expression expected in Object Roles Subsumption axiom" ); |
| 171 | kb.getRM(R).addRoleParent ( Sub, R ); |
| 172 | } |
| 173 | void visit ( const TDLAxiomDRoleSubsumption& axiom ) override |
| 174 | { |
| 175 | ensureNames(axiom.getRole()); |
| 176 | ensureNames(axiom.getSubRole()); |
| 177 | TRole* R = getRole ( axiom.getRole(), "Role expression expected in Data Roles Subsumption axiom" ); |
| 178 | TRole* S = getRole ( axiom.getSubRole(), "Role expression expected in Data Roles Subsumption axiom" ); |
| 179 | kb.getDRM().addRoleParent ( S, R ); |
| 180 | } |
| 181 | void visit ( const TDLAxiomORoleDomain& axiom ) override |
| 182 | { |
| 183 | ensureNames(axiom.getRole()); |
| 184 | ensureNames(axiom.getDomain()); |
| 185 | TRole* R = getRole ( axiom.getRole(), "Role expression expected in Object Role Domain axiom" ); |
| 186 | DLTree* C = e(axiom.getDomain()); |
| 187 | |
| 188 | if ( unlikely(R->isTop()) ) // add GCI |
| 189 | kb.addSubsumeAxiom ( createTop(), C ); |
| 190 | else if ( likely(!R->isBottom()) ) // nothing to do for bottom |
| 191 | R->setDomain(C); |
| 192 | } |
| 193 | void visit ( const TDLAxiomDRoleDomain& axiom ) override |
| 194 | { |
| 195 | ensureNames(axiom.getRole()); |
| 196 | ensureNames(axiom.getDomain()); |
| 197 | TRole* R = getRole ( axiom.getRole(), "Role expression expected in Data Role Domain axiom" ); |
| 198 | DLTree* C = e(axiom.getDomain()); |
| 199 | |
| 200 | if ( unlikely(R->isTop()) ) // add GCI |
| 201 | kb.addSubsumeAxiom ( createTop(), C ); |
| 202 | else if ( likely(!R->isBottom()) ) // nothing to do for bottom |
| 203 | R->setDomain(C); |
| 204 | } |
| 205 | void visit ( const TDLAxiomORoleRange& axiom ) override |
| 206 | { |
| 207 | ensureNames(axiom.getRole()); |
| 208 | ensureNames(axiom.getRange()); |
| 209 | TRole* R = getRole ( axiom.getRole(), "Role expression expected in Object Role Range axiom" ); |
| 210 | DLTree* C = e(axiom.getRange()); |
| 211 | |
| 212 | if ( unlikely(R->isTop()) ) // add GCI |
| 213 | kb.addSubsumeAxiom ( createTop(), C ); |
| 214 | else if ( likely(!R->isBottom()) ) // nothing to do for bottom |
| 215 | R->setRange(C); |
| 216 | } |
| 217 | void visit ( const TDLAxiomDRoleRange& axiom ) override |
| 218 | { |
| 219 | ensureNames(axiom.getRole()); |
| 220 | ensureNames(axiom.getRange()); |
| 221 | getRole ( axiom.getRole(), "Role expression expected in Data Role Range axiom" )->setRange(e(axiom.getRange())); |
| 222 | } |
| 223 | void visit ( const TDLAxiomRoleTransitive& axiom ) override |
| 224 | { |
| 225 | ensureNames(axiom.getRole()); |
| 226 | TRole* R = getRole ( axiom.getRole(), "Role expression expected in Role Transitivity axiom" ); |
| 227 | if ( !R->isTop() && !R->isBottom() ) // top/bottom roles are always transitive |
| 228 | R->setTransitive(); |
| 229 | } |
| 230 | void visit ( const TDLAxiomRoleReflexive& axiom ) override |
| 231 | { |
| 232 | ensureNames(axiom.getRole()); |
| 233 | TRole* R = getRole ( axiom.getRole(), "Role expression expected in Role Reflexivity axiom" ); |
| 234 | if ( R->isBottom() ) // empty role can't be reflexive |
| 235 | throw EFPPInconsistentKB(); |
| 236 | if ( !R->isTop() ) // universal role always reflexive |
| 237 | R->setReflexive(true); |
| 238 | } |
| 239 | void visit ( const TDLAxiomRoleIrreflexive& axiom ) override |
| 240 | { |
| 241 | ensureNames(axiom.getRole()); |
| 242 | TRole* R = getRole ( axiom.getRole(), "Role expression expected in Role Irreflexivity axiom" ); |
| 243 | if ( R->isTop() ) // KB became inconsistent |
| 244 | throw EFPPInconsistentKB(); |
| 245 | if ( !R->isBottom() ) // empty role already irreflexive |
| 246 | { |
| 247 | R->setDomain(createSNFNot(createSNFSelf(e(axiom.getRole())))); |
| 248 | R->setIrreflexive(true); |
| 249 | } |
| 250 | } |
| 251 | void visit ( const TDLAxiomRoleSymmetric& axiom ) override |
| 252 | { |
| 253 | ensureNames(axiom.getRole()); |
| 254 | TRole* R = getRole ( axiom.getRole(), "Role expression expected in Role Symmetry axiom" ); |
| 255 | if ( !R->isTop() && !R->isBottom() ) // both are symmetric |
| 256 | { |
| 257 | R->setSymmetric(true); |
| 258 | kb.getORM().addRoleParent ( R, R->inverse() ); |
| 259 | } |
| 260 | } |
| 261 | void visit ( const TDLAxiomRoleAsymmetric& axiom ) override |
| 262 | { |
| 263 | ensureNames(axiom.getRole()); |
| 264 | TRole* R = getRole ( axiom.getRole(), "Role expression expected in Role Asymmetry axiom" ); |
| 265 | if ( R->isTop() ) // KB became inconsistent |
| 266 | throw EFPPInconsistentKB(); |
| 267 | if ( !R->isBottom() ) |
| 268 | { |
| 269 | R->setAsymmetric(true); |
| 270 | kb.getORM().addDisjointRoles ( R, R->inverse() ); |
| 271 | } |
| 272 | } |
| 273 | void visit ( const TDLAxiomORoleFunctional& axiom ) override |
| 274 | { |
| 275 | ensureNames(axiom.getRole()); |
| 276 | TRole* R = getRole ( axiom.getRole(), "Role expression expected in Object Role Functionality axiom" ); |
| 277 | if ( R->isTop() ) // NOTE!! not really the case (1-point models are fine here), but OWL 2 forbid this |
| 278 | throw EFPPInconsistentKB(); |
| 279 | if ( !R->isBottom() ) |
| 280 | R->setFunctional(); |
| 281 | } |
| 282 | void visit ( const TDLAxiomDRoleFunctional& axiom ) override |
| 283 | { |
| 284 | ensureNames(axiom.getRole()); |
| 285 | TRole* R = getRole ( axiom.getRole(), "Role expression expected in Data Role Functionality axiom" ); |
| 286 | if ( R->isTop() ) // KB became inconsistent |
| 287 | throw EFPPInconsistentKB(); |
| 288 | if ( !R->isBottom() ) |
| 289 | R->setFunctional(); |
| 290 | } |
| 291 | void visit ( const TDLAxiomRoleInverseFunctional& axiom ) override |
| 292 | { |
| 293 | ensureNames(axiom.getRole()); |
| 294 | TRole* R = getRole ( axiom.getRole(), "Role expression expected in Role Inverse Functionality axiom" ); |
| 295 | if ( R->isTop() ) // NOTE!! not really the case (1-point models are fine here), but OWL 2 forbid this |
| 296 | throw EFPPInconsistentKB(); |
| 297 | if ( !R->isBottom() ) |
| 298 | R->inverse()->setFunctional(); |
| 299 | } |
| 300 | |
| 301 | // concept/individual axioms |
| 302 | |
| 303 | void visit ( const TDLAxiomConceptInclusion& axiom ) override |
| 304 | { |
| 305 | ensureNames(axiom.getSubC()); |
| 306 | ensureNames(axiom.getSupC()); |
| 307 | DLTree* C = e(axiom.getSubC()); |
| 308 | DLTree* D = e(axiom.getSupC()); |
| 309 | kb.addSubsumeAxiom ( C, D ); |
| 310 | } |
| 311 | void visit ( const TDLAxiomInstanceOf& axiom ) override |
| 312 | { |
| 313 | ensureNames(axiom.getIndividual()); |
| 314 | ensureNames(axiom.getC()); |
| 315 | getIndividual ( axiom.getIndividual(), "Individual expected in Instance axiom" ); |
| 316 | DLTree* I = e(axiom.getIndividual()); |
| 317 | DLTree* C = e(axiom.getC()); |
| 318 | kb.addSubsumeAxiom ( I, C ); |
| 319 | } |
| 320 | void visit ( const TDLAxiomRelatedTo& axiom ) override |
| 321 | { |
| 322 | ensureNames(axiom.getIndividual()); |
| 323 | ensureNames(axiom.getRelation()); |
| 324 | ensureNames(axiom.getRelatedIndividual()); |
| 325 | TRole* R = getRole ( axiom.getRelation(), "Role expression expected in Related To axiom" ); |
| 326 | if ( R->isBottom() ) |
| 327 | throw EFPPInconsistentKB(); |
| 328 | if ( !R->isTop() ) // nothing to do for universal role |
| 329 | { |
| 330 | TIndividual* I = getIndividual ( axiom.getIndividual(), "Individual expected in Related To axiom" ); |
| 331 | TIndividual* J = getIndividual ( axiom.getRelatedIndividual(), "Individual expected in Related To axiom" ); |
| 332 | kb.RegisterIndividualRelation ( I, R, J ); |
| 333 | } |
| 334 | } |
| 335 | void visit ( const TDLAxiomRelatedToNot& axiom ) override |
| 336 | { |
| 337 | ensureNames(axiom.getIndividual()); |
| 338 | ensureNames(axiom.getRelation()); |
| 339 | ensureNames(axiom.getRelatedIndividual()); |
| 340 | TRole* R = getRole ( axiom.getRelation(), "Role expression expected in Related To Not axiom" ); |
| 341 | if ( R->isTop() ) // inconsistent ontology |
| 342 | throw EFPPInconsistentKB(); |
| 343 | // make sure everything is consistent |
| 344 | getIndividual ( axiom.getIndividual(), "Individual expected in Related To Not axiom" ), |
| 345 | getIndividual ( axiom.getRelatedIndividual(), "Individual expected in Related To Not axiom" ); |
| 346 | if ( !R->isBottom() ) // nothing to do |
| 347 | kb.addSubsumeAxiom ( // make an axiom i:AR.\neg{j} |
| 348 | e(axiom.getIndividual()), |
| 349 | createSNFForall ( e(axiom.getRelation()), createSNFNot(e(axiom.getRelatedIndividual())) ) ); |
| 350 | } |
| 351 | void visit ( const TDLAxiomValueOf& axiom ) override |
| 352 | { |
| 353 | ensureNames(axiom.getIndividual()); |
| 354 | ensureNames(axiom.getAttribute()); |
| 355 | getIndividual ( axiom.getIndividual(), "Individual expected in Value Of axiom" ); |
| 356 | // FIXME!! think about ensuring the value |
| 357 | TRole* R = getRole ( axiom.getAttribute(), "Role expression expected in Value Of axiom" ); |
| 358 | if ( R->isBottom() ) |
| 359 | throw EFPPInconsistentKB(); |
| 360 | if ( !R->isTop() ) // nothing to do for universal role |
| 361 | kb.addSubsumeAxiom ( // make an axiom i:EA.V |
| 362 | e(axiom.getIndividual()), |
| 363 | createSNFExists ( e(axiom.getAttribute()), e(axiom.getValue())) ); |
| 364 | } |
| 365 | void visit ( const TDLAxiomValueOfNot& axiom ) override |
| 366 | { |
| 367 | ensureNames(axiom.getIndividual()); |
| 368 | ensureNames(axiom.getAttribute()); |
| 369 | getIndividual ( axiom.getIndividual(), "Individual expected in Value Of Not axiom" ); |
| 370 | // FIXME!! think about ensuring the value |
| 371 | TRole* R = getRole ( axiom.getAttribute(), "Role expression expected in Value Of Not axiom" ); |
| 372 | if ( R->isTop() ) |
| 373 | throw EFPPInconsistentKB(); |
| 374 | if ( !R->isBottom() ) // nothing to do for empty role |
| 375 | kb.addSubsumeAxiom ( // make an axiom i:AA.\neg V |
| 376 | e(axiom.getIndividual()), |
| 377 | createSNFForall ( e(axiom.getAttribute()), createSNFNot(e(axiom.getValue()))) ); |
| 378 | } |
| 379 | |
| 380 | public: // interface |
| 381 | /// init c'tor |
| 382 | explicit TOntologyLoader ( TBox& KB ) : kb(KB), ETrans(KB) {} |
| 383 | |
| 384 | /// load ontology to a given KB |
| 385 | void visitOntology ( TOntology& ontology ) |
| 386 | { |
| 387 | ontology.visitOntology(*this); |
| 388 | kb.finishLoading(); |
| 389 | } |
| 390 | }; // TOntologyLoader |
| 391 | |
| 392 | #endif |