| 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 TAXIOM_H |
| 21 | #define TAXIOM_H |
| 22 | |
| 23 | // uncomment this to have absorption debug messages |
| 24 | //#define RKG_DEBUG_ABSORPTION |
| 25 | |
| 26 | #ifdef RKG_DEBUG_ABSORPTION |
| 27 | # include <iostream> |
| 28 | #else |
| 29 | # include <iosfwd> |
| 30 | #endif |
| 31 | |
| 32 | #include <vector> |
| 33 | |
| 34 | #include "globaldef.h" |
| 35 | #include "dltree.h" |
| 36 | #include "tConcept.h" |
| 37 | #include "tRole.h" |
| 38 | #include "counter.h" |
| 39 | |
| 40 | class TBox; |
| 41 | |
| 42 | // statistical counters lives here |
| 43 | namespace Stat |
| 44 | { |
| 45 | class SAbsRepCN: public counter<SAbsRepCN> {}; |
| 46 | class SAbsRepForall: public counter<SAbsRepForall> {}; |
| 47 | class SAbsSplit: public counter<SAbsSplit> {}; |
| 48 | class SAbsBApply: public counter<SAbsBApply> {}; |
| 49 | class SAbsTApply: public counter<SAbsTApply> {}; |
| 50 | class SAbsCApply: public counter<SAbsCApply> {}; |
| 51 | class SAbsCAttempt: public counter<SAbsCAttempt> {}; |
| 52 | class SAbsNApply: public counter<SAbsNApply> {}; |
| 53 | class SAbsNAttempt: public counter<SAbsNAttempt> {}; |
| 54 | class SAbsRApply: public counter<SAbsRApply> {}; |
| 55 | class SAbsRAttempt: public counter<SAbsRAttempt> {}; |
| 56 | } |
| 57 | |
| 58 | // NS for different DLTree matchers for trees in axiom |
| 59 | namespace InAx |
| 60 | { |
| 61 | /// build an RW concept from a given [C|I]NAME-rooted DLTree |
| 62 | inline TConcept* getConcept ( DLTree* p ) |
| 63 | { return static_cast<TConcept*>(p->Element().getNE()); } |
| 64 | /// build an RO concept from a given [C|I]NAME-rooted DLTree |
| 65 | inline const TConcept* getConcept ( const DLTree* p ) |
| 66 | { return static_cast<const TConcept*>(p->Element().getNE()); } |
| 67 | /// @ return true if a concept C is a concept is non-primitive |
| 68 | bool isNP ( const TConcept* C, TBox& KB ); |
| 69 | |
| 70 | /// @return true iff P is a TOP |
| 71 | inline bool isTop ( const DLTree* p ) { return p->Element() == BOTTOM; } |
| 72 | /// @return true iff P is a BOTTOM |
| 73 | inline bool isBot ( const DLTree* p ) { return p->Element() == TOP; } |
| 74 | /// @return true iff P is a positive concept name |
| 75 | inline bool isPosCN ( const DLTree* p ) { return p->Element() == NOT && isName(p->Left()); } |
| 76 | /// @return true iff P is a positive non-primitive CN |
| 77 | inline bool isPosNP ( const DLTree* p, TBox& KB ) |
| 78 | { return isPosCN(p) && isNP(getConcept(p->Left()),KB); } |
| 79 | /// @return true iff P is a positive primitive CN |
| 80 | inline bool isPosPC ( const DLTree* p ) |
| 81 | { return isPosCN(p) && getConcept(p->Left())->isPrimitive(); } |
| 82 | /// @return true iff P is a negative concept name |
| 83 | inline bool isNegCN ( const DLTree* p ) { return isName(p); } |
| 84 | /// @return true iff P is a negative non-primitive CN |
| 85 | inline bool isNegNP ( const DLTree* p, TBox& KB ) |
| 86 | { return isNegCN(p) && isNP(getConcept(p),KB); } |
| 87 | /// @return true iff P is a negative primitive CN |
| 88 | inline bool isNegPC ( const DLTree* p ) |
| 89 | { return isNegCN(p) && getConcept(p)->isPrimitive(); } |
| 90 | /// @return true iff P is an AND expression |
| 91 | inline bool isAnd ( const DLTree* p ) { return p->Element() == NOT && p->Left()->Element() == AND; } |
| 92 | /// @return true iff P is an OR expression |
| 93 | inline bool isOr ( const DLTree* p ) { return p->Element() == AND; } |
| 94 | /// @return true iff P is a general FORALL expression |
| 95 | inline bool isForall ( const DLTree* p ) { return p->Element() == NOT && p->Left()->Element() == FORALL; } |
| 96 | /// @return true iff P is an object FORALL expression |
| 97 | inline bool isOForall ( const DLTree* p ) { return isForall(p) && !resolveRole(p->Left()->Left())->isDataRole(); } |
| 98 | /// @return true iff P is a FORALL expression suitable for absorption |
| 99 | inline bool isAbsForall ( const DLTree* p ) |
| 100 | { |
| 101 | if ( !isOForall(p) ) |
| 102 | return false; |
| 103 | const DLTree* C = p->Left()->Right(); |
| 104 | if ( isTop(C) ) // no sense to replace \AR.BOTTOM as it well lead to the same GCI |
| 105 | return false; |
| 106 | return !isName(C) || !getConcept(C)->isSystem(); |
| 107 | } |
| 108 | /// @return true iff P is a FORALL expression suitable for absorption with name at the end |
| 109 | inline bool isSimpleForall ( const DLTree* p ) |
| 110 | { |
| 111 | if ( !isAbsForall(p) ) |
| 112 | return false; |
| 113 | const DLTree* C = p->Left()->Right(); |
| 114 | // forall is simple if its filler is a name of a primitive concept |
| 115 | return isName(C) && (getConcept(C)->Description == nullptr); |
| 116 | } |
| 117 | } // InAx |
| 118 | |
| 119 | class TAxiom |
| 120 | { |
| 121 | protected: // types |
| 122 | /// type for axiom's representation, suitable for absorption |
| 123 | typedef std::vector<DLTree*> absorptionSet; |
| 124 | /// RW iterator for the elements of GCI |
| 125 | typedef absorptionSet::iterator iterator; |
| 126 | /// RO iterator for the elements of GCI |
| 127 | typedef absorptionSet::const_iterator const_iterator; |
| 128 | /// set of iterators to work with |
| 129 | typedef absorptionSet WorkSet; |
| 130 | |
| 131 | protected: // members |
| 132 | /// GCI is presented in the form (or Disjuncts); |
| 133 | absorptionSet Disjuncts; |
| 134 | /// the origin of an axiom if obtained during processing |
| 135 | const TAxiom* origin; |
| 136 | |
| 137 | protected: // methods |
| 138 | // access to labels |
| 139 | |
| 140 | /// RW begin |
| 141 | iterator begin ( void ) { return Disjuncts.begin(); } |
| 142 | /// RW end |
| 143 | iterator end ( void ) { return Disjuncts.end(); } |
| 144 | /// RO begin |
| 145 | const_iterator begin ( void ) const { return Disjuncts.begin(); } |
| 146 | /// RO end |
| 147 | const_iterator end ( void ) const { return Disjuncts.end(); } |
| 148 | |
| 149 | /// create a copy of a given GCI; ignore SKIP entry |
| 150 | TAxiom* copy ( const DLTree* skip ) const |
| 151 | { |
| 152 | TAxiom* ret = new TAxiom(this); |
| 153 | for ( const auto& C: Disjuncts ) |
| 154 | if ( C != skip ) |
| 155 | ret->Disjuncts.push_back(clone(C)); |
| 156 | return ret; |
| 157 | } |
| 158 | |
| 159 | // single disjunct's optimisations |
| 160 | |
| 161 | /// simplify (OR C ...) for a non-primitive C in a given position |
| 162 | TAxiom* simplifyPosNP ( const DLTree* rep ) const |
| 163 | { |
| 164 | Stat::SAbsRepCN(); |
| 165 | TAxiom* ret = copy(rep); |
| 166 | ret->add(createSNFNot(clone(InAx::getConcept(rep->Left())->Description))); |
| 167 | # ifdef RKG_DEBUG_ABSORPTION |
| 168 | std::cout << " simplify CN expression for" << rep->Left(); |
| 169 | # endif |
| 170 | return ret; |
| 171 | } |
| 172 | /// simplify (OR ~C ...) for a non-primitive C in a given position |
| 173 | TAxiom* simplifyNegNP ( const DLTree* rep ) const |
| 174 | { |
| 175 | Stat::SAbsRepCN(); |
| 176 | TAxiom* ret = copy(rep); |
| 177 | ret->add(clone(InAx::getConcept(rep)->Description)); |
| 178 | # ifdef RKG_DEBUG_ABSORPTION |
| 179 | std::cout << " simplify ~CN expression for" << rep; |
| 180 | # endif |
| 181 | return ret; |
| 182 | } |
| 183 | /// simplify (OR (SOME R C) ...)) in a given position |
| 184 | TAxiom* simplifyForall ( const DLTree* rep, TBox& KB ) const; |
| 185 | /// split (OR (AND...) ...) in a given position |
| 186 | void split ( std::vector<TAxiom*>& acc, const DLTree* rep, DLTree* pAnd ) const |
| 187 | { |
| 188 | if ( pAnd->Element().getToken() == AND ) |
| 189 | { // split the AND |
| 190 | split ( acc, rep, pAnd->Left() ); |
| 191 | split ( acc, rep, pAnd->Right() ); |
| 192 | } |
| 193 | else |
| 194 | { |
| 195 | TAxiom* ret = copy(rep); |
| 196 | ret->add(createSNFNot(clone(pAnd))); |
| 197 | acc.push_back(ret); |
| 198 | } |
| 199 | } |
| 200 | /// create a concept expression corresponding to a given GCI; ignore SKIP entry |
| 201 | DLTree* createAnAxiom ( const DLTree* skip ) const; |
| 202 | |
| 203 | public: // interface |
| 204 | /// create an empty GCI |
| 205 | explicit TAxiom ( const TAxiom* parent ) : origin(parent) {} |
| 206 | /// copy c'tor |
| 207 | TAxiom ( const TAxiom& ) = delete; |
| 208 | /// assignment |
| 209 | TAxiom& operator = ( const TAxiom& ) = delete; |
| 210 | /// d'tor: delete elements if AX is not in use |
| 211 | ~TAxiom() |
| 212 | { |
| 213 | for ( auto& p: Disjuncts ) |
| 214 | deleteTree(p); |
| 215 | } |
| 216 | |
| 217 | /// add DLTree to an axiom |
| 218 | void add ( DLTree* p ); |
| 219 | /// check whether 2 axioms are the same |
| 220 | bool operator == ( const TAxiom& ax ) const |
| 221 | { |
| 222 | if ( Disjuncts.size() != ax.Disjuncts.size() ) |
| 223 | return false; |
| 224 | auto p = begin(), q = ax.begin(), p_end = end(); |
| 225 | for ( ; p != p_end; ++p, ++q ) |
| 226 | if ( !equalTrees(*p,*q) ) |
| 227 | return false; |
| 228 | return true; |
| 229 | } |
| 230 | |
| 231 | /// replace a defined concept with its description |
| 232 | TAxiom* simplifyCN ( TBox& KB ) const; |
| 233 | /// replace a universal restriction with a fresh concept |
| 234 | TAxiom* simplifyForall ( TBox& KB ) const; |
| 235 | /// replace a simple universal restriction with a fresh concept |
| 236 | TAxiom* simplifySForall ( TBox& KB ) const; |
| 237 | /// split an axiom; @return new axiom and/or NULL |
| 238 | bool split ( std::vector<TAxiom*>& acc ) const |
| 239 | { |
| 240 | acc.clear(); |
| 241 | for ( const auto& C: Disjuncts ) |
| 242 | if ( InAx::isAnd(C) ) |
| 243 | { |
| 244 | Stat::SAbsSplit(); |
| 245 | # ifdef RKG_DEBUG_ABSORPTION |
| 246 | std::cout << " split AND expression" << C->Left(); |
| 247 | # endif |
| 248 | split ( acc, C, C->Left() ); |
| 249 | // no need to split more than once: |
| 250 | // every extra splits would be together with unsplitted parts |
| 251 | // like: (A or B) and (C or D) would be transform into |
| 252 | // A and (C or D), B and (C or D), (A or B) and C, (A or B) and D |
| 253 | // so just return here |
| 254 | return true; |
| 255 | } |
| 256 | |
| 257 | return false; |
| 258 | } |
| 259 | /// @return true iff an axiom is the same as one of its ancestors |
| 260 | bool isCyclic ( void ) const |
| 261 | { |
| 262 | for ( auto p = origin; p; p = p->origin ) |
| 263 | if ( *p == *this ) |
| 264 | { |
| 265 | # ifdef RKG_DEBUG_ABSORPTION |
| 266 | std::cout << " same as ancestor"; |
| 267 | # endif |
| 268 | return true; |
| 269 | } |
| 270 | return false; |
| 271 | } |
| 272 | /// absorb into BOTTOM; @return true if absorption is performed |
| 273 | bool absorbIntoBottom ( void ) const; |
| 274 | /// absorb into TOP; @return true if absorption is performed |
| 275 | bool absorbIntoTop ( TBox& KB ) const; |
| 276 | /// absorb into concept; @return true if absorption is performed |
| 277 | bool absorbIntoConcept ( TBox& KB ) const; |
| 278 | /// absorb into negation of a concept; @return true if absorption is performed |
| 279 | bool absorbIntoNegConcept ( TBox& KB ) const; |
| 280 | /// absorb into role domain; @return true if absorption is performed |
| 281 | bool absorbIntoDomain ( void ) const; |
| 282 | /// create a concept expression corresponding to a given GCI |
| 283 | DLTree* createAnAxiom ( void ) const { return createAnAxiom(nullptr); } |
| 284 | |
| 285 | #ifdef RKG_DEBUG_ABSORPTION |
| 286 | /// dump GCI for debug purposes |
| 287 | void dump ( std::ostream& o ) const; |
| 288 | #endif |
| 289 | }; // TAxiom; |
| 290 | |
| 291 | #endif |