| 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 GENERALSYNTACTICLOCALITYCHECKER_H |
| 21 | #define GENERALSYNTACTICLOCALITYCHECKER_H |
| 22 | |
| 23 | #include "LocalityChecker.h" |
| 24 | |
| 25 | /// syntactic locality checker for DL axioms |
| 26 | class GeneralSyntacticLocalityChecker: public LocalityChecker |
| 27 | { |
| 28 | protected: // methods |
| 29 | /// @return true iff EXPR is top equivalent |
| 30 | virtual bool isTopEquivalent ( const TDLExpression* expr ) = 0; |
| 31 | /// @return true iff EXPR is bottom equivalent |
| 32 | virtual bool isBotEquivalent ( const TDLExpression* expr ) = 0; |
| 33 | |
| 34 | protected: // methods |
| 35 | /// processing method for all Equivalent axioms; @return true if axiom is local |
| 36 | template <typename Entity> |
| 37 | bool processEquivalentAxiom ( const TDLNAryExpression<Entity>& axiom ) |
| 38 | { |
| 39 | // 1 element => local |
| 40 | if ( axiom.size() <= 1 ) |
| 41 | return true; |
| 42 | // axiom is local iff all the elements are either top- or bot-local |
| 43 | typename TDLNAryExpression<Entity>::iterator p = axiom.begin(), p_end = axiom.end(); |
| 44 | if ( isBotEquivalent(*p) ) |
| 45 | { // all should be \bot-eq |
| 46 | while ( ++p != p_end ) |
| 47 | if ( !isBotEquivalent(*p) ) |
| 48 | return false; |
| 49 | } |
| 50 | else if ( isTopEquivalent(*p) ) |
| 51 | { // all should be \top-eq |
| 52 | while ( ++p != p_end ) |
| 53 | if ( !isTopEquivalent(*p) ) |
| 54 | return false; |
| 55 | } |
| 56 | else // neither \bot- no \top-eq: non-local |
| 57 | return false; |
| 58 | |
| 59 | // all elements have the same locality |
| 60 | return true; |
| 61 | } |
| 62 | /// processing method for all Disjoint axioms; @return true if axiom is local |
| 63 | template <typename Entity> |
| 64 | bool processDisjointAxiom ( const TDLNAryExpression<Entity>& axiom ) |
| 65 | { |
| 66 | // local iff at most 1 element is not bot-equiv |
| 67 | bool hasNBE = false; |
| 68 | for ( typename TDLNAryExpression<Entity>::iterator p = axiom.begin(), p_end = axiom.end(); p != p_end; ++p ) |
| 69 | if ( !isBotEquivalent(*p) ) |
| 70 | { |
| 71 | if ( hasNBE ) // already seen one non-bot-eq element |
| 72 | return false; // non-local |
| 73 | else // record that 1 non-bot-eq element was found |
| 74 | hasNBE = true; |
| 75 | } |
| 76 | return true; |
| 77 | } |
| 78 | |
| 79 | public: // interface |
| 80 | /// init c'tor |
| 81 | explicit GeneralSyntacticLocalityChecker ( const TSignature* s ) : LocalityChecker(s) {} |
| 82 | |
| 83 | public: // visitor interface |
| 84 | void visit ( const TDLAxiomDeclaration& ) override { isLocal = true; } |
| 85 | |
| 86 | void visit ( const TDLAxiomEquivalentConcepts& axiom ) override { isLocal = processEquivalentAxiom(axiom); } |
| 87 | void visit ( const TDLAxiomDisjointConcepts& axiom ) override { isLocal = processDisjointAxiom(axiom); } |
| 88 | void visit ( const TDLAxiomDisjointUnion& axiom ) override |
| 89 | { |
| 90 | // DisjointUnion(A, C1,..., Cn) is local if |
| 91 | // (1) A and all of Ci are bot-equivalent, |
| 92 | // or (2) A and one Ci are top-equivalent and the remaining Cj are bot-equivalent |
| 93 | isLocal = false; |
| 94 | bool lhsIsTopEq; |
| 95 | if ( isTopEquivalent(axiom.getC()) ) |
| 96 | lhsIsTopEq = true; // need to check (2) |
| 97 | else if ( isBotEquivalent(axiom.getC()) ) |
| 98 | lhsIsTopEq = false; // need to check (1) |
| 99 | else |
| 100 | return; // neither (1) nor (2) |
| 101 | |
| 102 | bool topEqDesc = false; |
| 103 | for ( const auto* arg : axiom ) |
| 104 | if ( !isBotEquivalent(arg) ) |
| 105 | { |
| 106 | if ( lhsIsTopEq && isTopEquivalent(arg) ) |
| 107 | { |
| 108 | if ( topEqDesc ) |
| 109 | return; // 2nd top in there -- violate (2) -- non-local |
| 110 | else |
| 111 | topEqDesc = true; |
| 112 | } |
| 113 | else // either (1) or fail to have a top-eq for (2) |
| 114 | return; |
| 115 | } |
| 116 | |
| 117 | // check whether for (2) we found a top-eq concept |
| 118 | if ( lhsIsTopEq && !topEqDesc ) |
| 119 | return; |
| 120 | |
| 121 | // it is local in the end! |
| 122 | isLocal = true; |
| 123 | } |
| 124 | void visit ( const TDLAxiomEquivalentORoles& axiom ) override { isLocal = processEquivalentAxiom(axiom); } |
| 125 | void visit ( const TDLAxiomEquivalentDRoles& axiom ) override { isLocal = processEquivalentAxiom(axiom); } |
| 126 | void visit ( const TDLAxiomDisjointORoles& axiom ) override { isLocal = processDisjointAxiom(axiom); } |
| 127 | void visit ( const TDLAxiomDisjointDRoles& axiom ) override { isLocal = processDisjointAxiom(axiom); } |
| 128 | void visit ( const TDLAxiomSameIndividuals& ) override { isLocal = false; } |
| 129 | void visit ( const TDLAxiomDifferentIndividuals& ) override { isLocal = false; } |
| 130 | /// FaCT++ extension: there is no such axiom in OWL API, but I hope nobody would use Fairness here |
| 131 | void visit ( const TDLAxiomFairnessConstraint& ) override { isLocal = true; } |
| 132 | |
| 133 | void visit ( const TDLAxiomRoleInverse& axiom ) override |
| 134 | { |
| 135 | isLocal = ( isBotEquivalent(axiom.getRole()) && isBotEquivalent(axiom.getInvRole()) ) || |
| 136 | ( isTopEquivalent(axiom.getRole()) && isTopEquivalent(axiom.getInvRole()) ); |
| 137 | } |
| 138 | void visit ( const TDLAxiomORoleSubsumption& axiom ) override { isLocal = isTopEquivalent(axiom.getRole()) || isBotEquivalent(axiom.getSubRole()); } |
| 139 | void visit ( const TDLAxiomDRoleSubsumption& axiom ) override { isLocal = isTopEquivalent(axiom.getRole()) || isBotEquivalent(axiom.getSubRole()); } |
| 140 | void visit ( const TDLAxiomORoleDomain& axiom ) override |
| 141 | { isLocal = isTopEquivalent(axiom.getDomain()) || isBotEquivalent(axiom.getRole()); } |
| 142 | void visit ( const TDLAxiomDRoleDomain& axiom ) override |
| 143 | { isLocal = isTopEquivalent(axiom.getDomain()) || isBotEquivalent(axiom.getRole()); } |
| 144 | void visit ( const TDLAxiomORoleRange& axiom ) override |
| 145 | { isLocal = isTopEquivalent(axiom.getRange()) || isBotEquivalent(axiom.getRole()); } |
| 146 | void visit ( const TDLAxiomDRoleRange& axiom ) override |
| 147 | { isLocal = isTopEquivalent(axiom.getRange()) || isBotEquivalent(axiom.getRole()); } |
| 148 | void visit ( const TDLAxiomRoleTransitive& axiom ) override { isLocal = isBotEquivalent(axiom.getRole()) || isTopEquivalent(axiom.getRole()); } |
| 149 | /// as BotRole is irreflexive, the only local axiom is topEquivalent(R) |
| 150 | void visit ( const TDLAxiomRoleReflexive& axiom ) override { isLocal = isTopEquivalent(axiom.getRole()); } |
| 151 | void visit ( const TDLAxiomRoleIrreflexive& axiom ) override { isLocal = isBotEquivalent(axiom.getRole()); } |
| 152 | void visit ( const TDLAxiomRoleSymmetric& axiom ) override { isLocal = isBotEquivalent(axiom.getRole()) || isTopEquivalent(axiom.getRole()); } |
| 153 | void visit ( const TDLAxiomRoleAsymmetric& ) override { isLocal = false; } |
| 154 | void visit ( const TDLAxiomORoleFunctional& axiom ) override { isLocal = isBotEquivalent(axiom.getRole()); } |
| 155 | void visit ( const TDLAxiomDRoleFunctional& axiom ) override { isLocal = isBotEquivalent(axiom.getRole()); } |
| 156 | void visit ( const TDLAxiomRoleInverseFunctional& axiom ) override { isLocal = isBotEquivalent(axiom.getRole()); } |
| 157 | |
| 158 | void visit ( const TDLAxiomConceptInclusion& axiom ) override { isLocal = isBotEquivalent(axiom.getSubC()) || isTopEquivalent(axiom.getSupC()); } |
| 159 | void visit ( const TDLAxiomInstanceOf& axiom ) override { isLocal = isTopEquivalent(axiom.getC()); } |
| 160 | void visit ( const TDLAxiomRelatedTo& axiom ) override { isLocal = isTopEquivalent(axiom.getRelation()); } |
| 161 | void visit ( const TDLAxiomRelatedToNot& axiom ) override { isLocal = isBotEquivalent(axiom.getRelation()); } |
| 162 | void visit ( const TDLAxiomValueOf& axiom ) override { isLocal = isTopEquivalent(axiom.getAttribute()); } |
| 163 | void visit ( const TDLAxiomValueOfNot& axiom ) override { isLocal = isBotEquivalent(axiom.getAttribute()); } |
| 164 | }; // GeneralSyntacticLocalityChecker |
| 165 | |
| 166 | #endif |