| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 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: |
| 30 |
|
| 31 |
TBox& kb; |
| 32 |
|
| 33 |
TExpressionTranslator ETrans; |
| 34 |
|
| 35 |
std::vector<DLTree*> ArgList; |
| 36 |
|
| 37 |
protected: |
| 38 |
|
| 39 |
DLTree* e ( const TDLExpression* expr ) |
| 40 |
{ |
| 41 |
expr->accept(ETrans); |
| 42 |
return ETrans; |
| 43 |
} |
| 44 |
|
| 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 |
|
| 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 |
|
| 69 |
void ensureNames ( const TDLExpression* Expr ) |
| 70 |
{ |
| 71 |
fpp_assert ( Expr != nullptr ); |
| 72 |
} |
| 73 |
|
| 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: |
| 86 |
void visit ( const TDLAxiomDeclaration& axiom ) override |
| 87 |
{ |
| 88 |
ensureNames(axiom.getDeclaration()); |
| 89 |
TreeDeleter(e(axiom.getDeclaration())); |
| 90 |
} |
| 91 |
|
| 92 |
|
| 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 |
|
| 107 |
prepareArgList(axiom); |
| 108 |
kb.processDisjointC(ArgList.begin(),ArgList.end()); |
| 109 |
|
| 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 |
|
| 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()) ) |
| 189 |
kb.addSubsumeAxiom ( createTop(), C ); |
| 190 |
else if ( likely(!R->isBottom()) ) |
| 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()) ) |
| 201 |
kb.addSubsumeAxiom ( createTop(), C ); |
| 202 |
else if ( likely(!R->isBottom()) ) |
| 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()) ) |
| 213 |
kb.addSubsumeAxiom ( createTop(), C ); |
| 214 |
else if ( likely(!R->isBottom()) ) |
| 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() ) |
| 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() ) |
| 235 |
throw EFPPInconsistentKB(); |
| 236 |
if ( !R->isTop() ) |
| 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() ) |
| 244 |
throw EFPPInconsistentKB(); |
| 245 |
if ( !R->isBottom() ) |
| 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() ) |
| 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() ) |
| 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() ) |
| 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() ) |
| 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() ) |
| 296 |
throw EFPPInconsistentKB(); |
| 297 |
if ( !R->isBottom() ) |
| 298 |
R->inverse()->setFunctional(); |
| 299 |
} |
| 300 |
|
| 301 |
|
| 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() ) |
| 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() ) |
| 342 |
throw EFPPInconsistentKB(); |
| 343 |
|
| 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() ) |
| 347 |
kb.addSubsumeAxiom ( |
| 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 |
|
| 357 |
TRole* R = getRole ( axiom.getAttribute(), "Role expression expected in Value Of axiom" ); |
| 358 |
if ( R->isBottom() ) |
| 359 |
throw EFPPInconsistentKB(); |
| 360 |
if ( !R->isTop() ) |
| 361 |
kb.addSubsumeAxiom ( |
| 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 |
|
| 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() ) |
| 375 |
kb.addSubsumeAxiom ( |
| 376 |
e(axiom.getIndividual()), |
| 377 |
createSNFForall ( e(axiom.getAttribute()), createSNFNot(e(axiom.getValue()))) ); |
| 378 |
} |
| 379 |
|
| 380 |
public: |
| 381 |
|
| 382 |
explicit TOntologyLoader ( TBox& KB ) : kb(KB), ETrans(KB) {} |
| 383 |
|
| 384 |
|
| 385 |
void visitOntology ( TOntology& ontology ) |
| 386 |
{ |
| 387 |
ontology.visitOntology(*this); |
| 388 |
kb.finishLoading(); |
| 389 |
} |
| 390 |
}; |
| 391 |
|
| 392 |
#endif |