| 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 TAXIOMSET_H |
| 21 | #define TAXIOMSET_H |
| 22 | |
| 23 | #include "tAxiom.h" |
| 24 | |
| 25 | class TBox; |
| 26 | |
| 27 | namespace Stat |
| 28 | { |
| 29 | class SAbsInput: public counter<SAbsInput> {}; |
| 30 | class SAbsAction: public counter<SAbsAction> {}; |
| 31 | } |
| 32 | |
| 33 | /// set of GCIs, absorbable and not |
| 34 | class TAxiomSet |
| 35 | { |
| 36 | protected: // internal types |
| 37 | /// set of GCIs |
| 38 | typedef std::vector<TAxiom*> AxiomCollection; |
| 39 | /// method applying to the axiom |
| 40 | typedef bool (TAxiomSet::*AbsMethod)(const TAxiom*); |
| 41 | /// array of methods in application order |
| 42 | typedef std::vector<AbsMethod> AbsActVector; |
| 43 | |
| 44 | protected: // members |
| 45 | /// host TBox that holds all concepts/etc |
| 46 | TBox& Host; |
| 47 | /// set of axioms that accumulates incoming (and newly created) axioms; |
| 48 | AxiomCollection Accum; |
| 49 | /// set of absorption action, in order |
| 50 | AbsActVector ActionVector; |
| 51 | |
| 52 | protected: // methods |
| 53 | |
| 54 | /// add already built GCI p |
| 55 | void insertGCI ( TAxiom* p ) |
| 56 | { |
| 57 | # ifdef RKG_DEBUG_ABSORPTION |
| 58 | std::cout << "\n new axiom (" << Accum.size() << "):"; |
| 59 | p->dump(std::cout); |
| 60 | # endif |
| 61 | Accum.push_back(p); |
| 62 | } |
| 63 | /// @return true iff axiom Q is a copy of already existing axiom |
| 64 | bool copyOfExisting ( const TAxiom* q ) const |
| 65 | { |
| 66 | for ( const TAxiom* axiom : Accum ) |
| 67 | if ( *q == *axiom ) |
| 68 | { |
| 69 | # ifdef RKG_DEBUG_ABSORPTION |
| 70 | std::cout << " same as (" << p-Accum.begin() << "); skip"; |
| 71 | # endif |
| 72 | return true; |
| 73 | } |
| 74 | return false; |
| 75 | } |
| 76 | /// absorb single GCI wrt absorption flags |
| 77 | bool absorbGCI ( const TAxiom* p ); |
| 78 | /// split given axiom |
| 79 | bool split ( const TAxiom* p ); |
| 80 | /// helper that inserts an axiom into Accum; @return bool if success |
| 81 | bool processNewAxiom ( TAxiom* q ) |
| 82 | { |
| 83 | // no input axiom -- nothing to add |
| 84 | if ( q == nullptr ) |
| 85 | return false; |
| 86 | // if an axiom is a copy of already processed one -- fail to add (will result in a cycle) |
| 87 | if ( q->isCyclic() ) |
| 88 | { |
| 89 | delete q; |
| 90 | return false; |
| 91 | } |
| 92 | // if an axiom is a copy of a new one -- succeed but didn't really add anything |
| 93 | if ( copyOfExisting(q) ) |
| 94 | { |
| 95 | delete q; |
| 96 | return true; |
| 97 | } |
| 98 | // fresh axiom -- add it |
| 99 | insertGCI(q); |
| 100 | return true; |
| 101 | } |
| 102 | /// replace a defined concept with its description |
| 103 | bool simplifyCN ( const TAxiom* p ) { return processNewAxiom(p->simplifyCN(Host)); } |
| 104 | /// replace a universal restriction with a fresh concept |
| 105 | bool simplifyForall ( const TAxiom* p ) { return processNewAxiom(p->simplifyForall(Host)); } |
| 106 | /// replace a simple universal restriction with a fresh concept |
| 107 | bool simplifySForall ( const TAxiom* p ) { return processNewAxiom(p->simplifySForall(Host)); } |
| 108 | |
| 109 | /// absorb single axiom AX into BOTTOM; @return true if succeed |
| 110 | bool absorbIntoBottom ( const TAxiom* ax ) { return ax->absorbIntoBottom(); } |
| 111 | /// absorb single axiom AX into TOP; @return true if succeed |
| 112 | bool absorbIntoTop ( const TAxiom* ax ) { return ax->absorbIntoTop(Host); } |
| 113 | /// absorb single axiom AX into concept; @return true if succeed |
| 114 | bool absorbIntoConcept ( const TAxiom* ax ) { return ax->absorbIntoConcept(Host); } |
| 115 | /// absorb single axiom AX into negated concept; @return true if succeed |
| 116 | bool absorbIntoNegConcept ( const TAxiom* ax ) { return ax->absorbIntoNegConcept(Host); } |
| 117 | /// absorb single axiom AX into role domain; @return true if succeed |
| 118 | bool absorbIntoDomain ( const TAxiom* ax ) { return ax->absorbIntoDomain(); } |
| 119 | |
| 120 | public: // interface |
| 121 | /// c'tor |
| 122 | explicit TAxiomSet ( TBox& host ) |
| 123 | : Host(host) |
| 124 | {} |
| 125 | /// d'tor |
| 126 | ~TAxiomSet(); |
| 127 | |
| 128 | /// init all absorption-related flags using given set of option |
| 129 | bool initAbsorptionFlags ( const std::string& flags ); |
| 130 | /// add axiom for the GCI C [= D |
| 131 | void addAxiom ( DLTree* C, DLTree* D ) |
| 132 | { |
| 133 | Stat::SAbsInput(); |
| 134 | TAxiom* p = new TAxiom(nullptr); |
| 135 | p->add(C); |
| 136 | p->add(createSNFNot(D)); |
| 137 | insertGCI(p); |
| 138 | } |
| 139 | |
| 140 | /// absorb set of axioms; @return size of not absorbed set |
| 141 | size_t absorb ( void ); |
| 142 | /// get number of (not absorbed) GCIs |
| 143 | size_t size ( void ) const { return Accum.size(); } |
| 144 | /// @return true if non-concept absorption were executed |
| 145 | bool wasRoleAbsorptionApplied ( void ) const { return Stat::SAbsRApply::objects_created > 0; } |
| 146 | /// get GCI of all non-absorbed axioms |
| 147 | DLTree* getGCI ( void ) const |
| 148 | { |
| 149 | DLTree* ret = createTop(); |
| 150 | for ( const auto& axiom: Accum ) |
| 151 | ret = createSNFAnd ( ret, axiom->createAnAxiom() ); |
| 152 | |
| 153 | return ret; |
| 154 | } |
| 155 | |
| 156 | /// print absorption statistics |
| 157 | void PrintStatistics ( void ) const; |
| 158 | }; // TAxiomSet |
| 159 | |
| 160 | #endif |