| 1 | /* This file is part of the FaCT++ DL reasoner |
| 2 | Copyright (C) 2011-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 SEMANTICLOCALITYCHECKER_H |
| 21 | #define SEMANTICLOCALITYCHECKER_H |
| 22 | |
| 23 | #include "LocalityChecker.h" |
| 24 | #include "Kernel.h" |
| 25 | |
| 26 | /// semantic locality checker for DL axioms |
| 27 | class SemanticLocalityChecker: public LocalityChecker |
| 28 | { |
| 29 | protected: // members |
| 30 | /// Reasoner to detect the tautology |
| 31 | ReasoningKernel Kernel; |
| 32 | /// Expression manager of a kernel |
| 33 | TExpressionManager* pEM; |
| 34 | /// map between axioms and concept expressions |
| 35 | std::map<const TDLAxiom*, const TDLConceptExpression*> ExprMap; |
| 36 | |
| 37 | protected: // methods |
| 38 | /// @return expression necessary to build query for a given type of an axiom; @return NULL if none necessary |
| 39 | const TDLConceptExpression* getExpr ( const TDLAxiom* axiom ) |
| 40 | { |
| 41 | if ( const TDLAxiomRelatedTo* art = dynamic_cast<const TDLAxiomRelatedTo*>(axiom) ) |
| 42 | return pEM->Value ( art->getRelation(), art->getRelatedIndividual() ); |
| 43 | if ( const TDLAxiomValueOf* avo = dynamic_cast<const TDLAxiomValueOf*>(axiom) ) |
| 44 | return pEM->Value ( avo->getAttribute(), avo->getValue() ); |
| 45 | if ( const TDLAxiomORoleDomain* ord = dynamic_cast<const TDLAxiomORoleDomain*>(axiom) ) |
| 46 | return pEM->Exists ( ord->getRole(), pEM->Top() ); |
| 47 | if ( const TDLAxiomORoleRange* orr = dynamic_cast<const TDLAxiomORoleRange*>(axiom) ) |
| 48 | return pEM->Exists ( orr->getRole(), pEM->Not(orr->getRange()) ); |
| 49 | if ( const TDLAxiomDRoleDomain* drd = dynamic_cast<const TDLAxiomDRoleDomain*>(axiom) ) |
| 50 | return pEM->Exists ( drd->getRole(), pEM->DataTop() ); |
| 51 | if ( const TDLAxiomDRoleRange* drr = dynamic_cast<const TDLAxiomDRoleRange*>(axiom) ) |
| 52 | return pEM->Exists ( drr->getRole(), pEM->DataNot(drr->getRange()) ); |
| 53 | if ( const TDLAxiomRelatedToNot* rtn = dynamic_cast<const TDLAxiomRelatedToNot*>(axiom) ) |
| 54 | return pEM->Not ( pEM->Value ( rtn->getRelation(), rtn->getRelatedIndividual() ) ); |
| 55 | if ( const TDLAxiomValueOfNot* von = dynamic_cast<const TDLAxiomValueOfNot*>(axiom) ) |
| 56 | return pEM->Not ( pEM->Value ( von->getAttribute(), von->getValue() ) ); |
| 57 | // everything else doesn't require expression to be build |
| 58 | return nullptr; |
| 59 | } |
| 60 | |
| 61 | public: // interface |
| 62 | /// init c'tor |
| 63 | explicit SemanticLocalityChecker ( const TSignature* sig ) : LocalityChecker(sig) |
| 64 | { |
| 65 | pEM = Kernel.getExpressionManager(); |
| 66 | // for tests we will need TB names to be from the OWL 2 namespace |
| 67 | pEM->setTopBottomRoles( |
| 68 | "http://www.w3.org/2002/07/owl#topObjectProperty", |
| 69 | "http://www.w3.org/2002/07/owl#bottomObjectProperty", |
| 70 | "http://www.w3.org/2002/07/owl#topDataProperty", |
| 71 | "http://www.w3.org/2002/07/owl#bottomDataProperty"); |
| 72 | } |
| 73 | |
| 74 | /// init kernel with the ontology signature and init expression map |
| 75 | void preprocessOntology ( const AxiomVec& Axioms ) override |
| 76 | { |
| 77 | TSignature s; |
| 78 | ExprMap.clear(); |
| 79 | for ( TDLAxiom* axiom : Axioms ) |
| 80 | { |
| 81 | ExprMap[axiom] = getExpr(axiom); |
| 82 | s.add(axiom->getSignature()); |
| 83 | } |
| 84 | |
| 85 | Kernel.clearKB(); |
| 86 | // register all the objects in the ontology signature |
| 87 | for (const TNamedEntity* entity : s) |
| 88 | Kernel.declare(dynamic_cast<const TDLExpression*>(entity)); |
| 89 | // prepare the reasoner to check tautologies |
| 90 | Kernel.realiseKB(); |
| 91 | // after TBox appears there, set signature to translate |
| 92 | Kernel.setSignature(getSignature()); |
| 93 | // disallow usage of the expression cache as same expressions will lead to different translations |
| 94 | Kernel.setIgnoreExprCache(true); |
| 95 | } |
| 96 | |
| 97 | public: // visitor interface |
| 98 | void visit ( const TDLAxiomDeclaration& ) override { isLocal = true; } |
| 99 | |
| 100 | void visit ( const TDLAxiomEquivalentConcepts& axiom ) override |
| 101 | { |
| 102 | isLocal = false; |
| 103 | TDLAxiomEquivalentConcepts::iterator p = axiom.begin(), p_end = axiom.end(); |
| 104 | const TDLConceptExpression* C = *p; |
| 105 | while ( ++p != p_end ) |
| 106 | if ( !Kernel.isEquivalent ( C, *p ) ) |
| 107 | return; |
| 108 | isLocal = true; |
| 109 | } |
| 110 | void visit ( const TDLAxiomDisjointConcepts& axiom ) override |
| 111 | { |
| 112 | isLocal = false; |
| 113 | for ( TDLAxiomDisjointConcepts::iterator p = axiom.begin(), q, p_end = axiom.end(); p != p_end; ++p ) |
| 114 | for ( q = p+1; q != p_end; ++q ) |
| 115 | if ( !Kernel.isDisjoint ( *p, *q ) ) |
| 116 | return; |
| 117 | isLocal = true; |
| 118 | } |
| 119 | void visit ( const TDLAxiomDisjointUnion& axiom ) override |
| 120 | { |
| 121 | isLocal = false; |
| 122 | // check A = (or C1... Cn) |
| 123 | TDLAxiomDisjointUnion::iterator p, q, p_end = axiom.end(); |
| 124 | pEM->newArgList(); |
| 125 | for ( p = axiom.begin(); p != p_end; ++p ) |
| 126 | pEM->addArg(*p); |
| 127 | if ( !Kernel.isEquivalent ( axiom.getC(), pEM->Or() ) ) |
| 128 | return; |
| 129 | // check disjoint(C1... Cn) |
| 130 | for ( p = axiom.begin(); p != p_end; ++p ) |
| 131 | for ( q = p+1; q != p_end; ++q ) |
| 132 | if ( !Kernel.isDisjoint ( *p, *q ) ) |
| 133 | return; |
| 134 | isLocal = true; |
| 135 | } |
| 136 | void visit ( const TDLAxiomEquivalentORoles& axiom ) override |
| 137 | { |
| 138 | isLocal = false; |
| 139 | TDLAxiomEquivalentORoles::iterator p = axiom.begin(), p_end = axiom.end(); |
| 140 | const TDLObjectRoleExpression* R = *p; |
| 141 | while ( ++p != p_end ) |
| 142 | if ( !(Kernel.isSubRoles ( R, *p ) && Kernel.isSubRoles ( *p, R )) ) |
| 143 | return; |
| 144 | isLocal = true; |
| 145 | } |
| 146 | // tautology if all the subsumptions Ri [= Rj holds |
| 147 | void visit ( const TDLAxiomEquivalentDRoles& axiom ) override |
| 148 | { |
| 149 | isLocal = false; |
| 150 | TDLAxiomEquivalentDRoles::iterator p = axiom.begin(), p_end = axiom.end(); |
| 151 | const TDLDataRoleExpression* R = *p; |
| 152 | while ( ++p != p_end ) |
| 153 | if ( !(Kernel.isSubRoles ( R, *p ) && Kernel.isSubRoles ( *p, R )) ) |
| 154 | return; |
| 155 | isLocal = true; |
| 156 | } |
| 157 | void visit ( const TDLAxiomDisjointORoles& axiom ) override |
| 158 | { |
| 159 | pEM->newArgList(); |
| 160 | for (const auto* arg : axiom) |
| 161 | pEM->addArg(arg); |
| 162 | isLocal = Kernel.isDisjointRoles(); |
| 163 | } |
| 164 | void visit ( const TDLAxiomDisjointDRoles& axiom ) override |
| 165 | { |
| 166 | pEM->newArgList(); |
| 167 | for (const auto* arg : axiom) |
| 168 | pEM->addArg(arg); |
| 169 | isLocal = Kernel.isDisjointRoles(); |
| 170 | } |
| 171 | // never local |
| 172 | void visit ( const TDLAxiomSameIndividuals& ) override { isLocal = false; } |
| 173 | // never local |
| 174 | void visit ( const TDLAxiomDifferentIndividuals& ) override { isLocal = false; } |
| 175 | /// there is no such axiom in OWL API, but I hope nobody would use Fairness here |
| 176 | void visit ( const TDLAxiomFairnessConstraint& ) override { isLocal = true; } |
| 177 | |
| 178 | // R = inverse(S) is tautology iff R [= S- and S [= R- |
| 179 | void visit ( const TDLAxiomRoleInverse& axiom ) override |
| 180 | { |
| 181 | isLocal = Kernel.isSubRoles ( axiom.getRole(), pEM->Inverse(axiom.getInvRole()) ) && |
| 182 | Kernel.isSubRoles ( axiom.getInvRole(), pEM->Inverse(axiom.getRole()) ); |
| 183 | } |
| 184 | void visit ( const TDLAxiomORoleSubsumption& axiom ) override |
| 185 | { |
| 186 | // check whether the LHS is a role chain |
| 187 | if ( const TDLObjectRoleChain* Chain = dynamic_cast<const TDLObjectRoleChain*>(axiom.getSubRole()) ) |
| 188 | { |
| 189 | pEM->newArgList(); |
| 190 | for (const auto* arg : *Chain) |
| 191 | pEM->addArg(arg); |
| 192 | isLocal = Kernel.isSubChain(axiom.getRole()); |
| 193 | } |
| 194 | // check whether the LHS is a plain role or inverse |
| 195 | else if ( const TDLObjectRoleExpression* Sub = dynamic_cast<const TDLObjectRoleExpression*>(axiom.getSubRole()) ) |
| 196 | isLocal = Kernel.isSubRoles ( Sub, axiom.getRole() ); |
| 197 | else // here we have a projection expression. FIXME!! for now |
| 198 | isLocal = true; |
| 199 | } |
| 200 | void visit ( const TDLAxiomDRoleSubsumption& axiom ) override { isLocal = Kernel.isSubRoles ( axiom.getSubRole(), axiom.getRole() ); } |
| 201 | // Domain(R) = C is tautology iff ER.Top [= C |
| 202 | void visit ( const TDLAxiomORoleDomain& axiom ) override { isLocal = Kernel.isSubsumedBy ( ExprMap[&axiom], axiom.getDomain() ); } |
| 203 | void visit ( const TDLAxiomDRoleDomain& axiom ) override { isLocal = Kernel.isSubsumedBy ( ExprMap[&axiom], axiom.getDomain() ); } |
| 204 | // Range(R) = C is tautology iff ER.~C is unsatisfiable |
| 205 | void visit ( const TDLAxiomORoleRange& axiom ) override { isLocal = !Kernel.isSatisfiable(ExprMap[&axiom]); } |
| 206 | void visit ( const TDLAxiomDRoleRange& axiom ) override { isLocal = !Kernel.isSatisfiable(ExprMap[&axiom]); } |
| 207 | void visit ( const TDLAxiomRoleTransitive& axiom ) override { isLocal = Kernel.isTransitive(axiom.getRole()); } |
| 208 | void visit ( const TDLAxiomRoleReflexive& axiom ) override { isLocal = Kernel.isReflexive(axiom.getRole()); } |
| 209 | void visit ( const TDLAxiomRoleIrreflexive& axiom ) override { isLocal = Kernel.isIrreflexive(axiom.getRole()); } |
| 210 | void visit ( const TDLAxiomRoleSymmetric& axiom ) override { isLocal = Kernel.isSymmetric(axiom.getRole()); } |
| 211 | void visit ( const TDLAxiomRoleAsymmetric& axiom ) override { isLocal = Kernel.isAsymmetric(axiom.getRole()); } |
| 212 | void visit ( const TDLAxiomORoleFunctional& axiom ) override { isLocal = Kernel.isFunctional(axiom.getRole()); } |
| 213 | void visit ( const TDLAxiomDRoleFunctional& axiom ) override { isLocal = Kernel.isFunctional(axiom.getRole()); } |
| 214 | void visit ( const TDLAxiomRoleInverseFunctional& axiom ) override { isLocal = Kernel.isInverseFunctional(axiom.getRole()); } |
| 215 | |
| 216 | void visit ( const TDLAxiomConceptInclusion& axiom ) override { isLocal = Kernel.isSubsumedBy ( axiom.getSubC(), axiom.getSupC() ); } |
| 217 | // for top locality, this might be local |
| 218 | void visit ( const TDLAxiomInstanceOf& axiom ) override { isLocal = Kernel.isInstance ( axiom.getIndividual(), axiom.getC() ); } |
| 219 | // R(i,j) holds if {i} [= \ER.{j} |
| 220 | void visit ( const TDLAxiomRelatedTo& axiom ) override { isLocal = Kernel.isInstance ( axiom.getIndividual(), ExprMap[&axiom] ); } |
| 221 | ///!R(i,j) holds if {i} [= \AR.!{j}=!\ER.{j} |
| 222 | void visit ( const TDLAxiomRelatedToNot& axiom ) override { isLocal = Kernel.isInstance ( axiom.getIndividual(), ExprMap[&axiom] ); } |
| 223 | // R(i,v) holds if {i} [= \ER.{v} |
| 224 | void visit ( const TDLAxiomValueOf& axiom ) override { isLocal = Kernel.isInstance ( axiom.getIndividual(), ExprMap[&axiom] ); } |
| 225 | // !R(i,v) holds if {i} [= !\ER.{v} |
| 226 | void visit ( const TDLAxiomValueOfNot& axiom ) override { isLocal = Kernel.isInstance ( axiom.getIndividual(), ExprMap[&axiom] ); } |
| 227 | }; // SemanticLocalityChecker |
| 228 | |
| 229 | #endif |