| 1 | /* This file is part of the FaCT++ DL reasoner |
| 2 | Copyright (C) 2008-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 TONTOLOGY_H |
| 21 | #define TONTOLOGY_H |
| 22 | |
| 23 | #include <vector> |
| 24 | |
| 25 | #include "globaldef.h" |
| 26 | #include "tDLAxiom.h" |
| 27 | #include "tExpressionManager.h" |
| 28 | |
| 29 | /// define ontology as a set of axioms |
| 30 | class TOntology |
| 31 | { |
| 32 | public: // types |
| 33 | /// RW iterator over the base type |
| 34 | typedef AxiomVec::iterator iterator; |
| 35 | |
| 36 | protected: // members |
| 37 | /// all the axioms |
| 38 | AxiomVec Axioms; |
| 39 | /// retracted since last change |
| 40 | AxiomVec Retracted; |
| 41 | /// expression manager that builds all the expressions for the axioms |
| 42 | TExpressionManager EManager; |
| 43 | /// id to be given to the next axiom |
| 44 | unsigned int axiomId = 0; |
| 45 | /// index of the 1st unprocessed axiom |
| 46 | size_t axiomToProcess = 0; |
| 47 | /// true iff ontology was changed |
| 48 | bool changed = false; |
| 49 | |
| 50 | public: // interface |
| 51 | /// empty c'tor |
| 52 | TOntology() = default; |
| 53 | /// d'tor |
| 54 | ~TOntology() { clear(); } |
| 55 | |
| 56 | /// @return true iff the ontology was changed since its last load |
| 57 | bool isChanged ( void ) const { return changed; } |
| 58 | /// set the processed marker to the end of the ontology |
| 59 | void setProcessed ( void ) { axiomToProcess = Axioms.size(); Retracted.clear(); changed = false; } |
| 60 | |
| 61 | /// add given axiom to the ontology |
| 62 | TDLAxiom* add ( TDLAxiom* p ) |
| 63 | { |
| 64 | p->setId(++axiomId); |
| 65 | Axioms.push_back(p); |
| 66 | changed = true; |
| 67 | return p; |
| 68 | } |
| 69 | /// retract given axiom to the ontology |
| 70 | void retract ( TDLAxiom* p ) |
| 71 | { |
| 72 | // if ( p->getId() <= Axioms.size() && Axioms[p->getId()-1] == p ) |
| 73 | { |
| 74 | changed = true; |
| 75 | p->setUsed(false); |
| 76 | Retracted.push_back(p); |
| 77 | } |
| 78 | } |
| 79 | /// mark all the axioms as not in the module |
| 80 | void clearModuleInfo ( void ) |
| 81 | { |
| 82 | for ( iterator p = Axioms.begin(), p_end = Axioms.end(); p < p_end; ++p ) |
| 83 | (*p)->setInModule(false); |
| 84 | } |
| 85 | /// safe clear the ontology (do not remove axioms) |
| 86 | void safeClear ( void ) { Axioms.clear(); } |
| 87 | /// clear axioms (delete all axioms) |
| 88 | void clearAxioms ( void ) |
| 89 | { |
| 90 | for ( iterator p = Axioms.begin(), p_end = Axioms.end(); p < p_end; ++p ) |
| 91 | delete *p; |
| 92 | safeClear(); |
| 93 | } |
| 94 | /// clear the ontology |
| 95 | void clear ( void ) |
| 96 | { |
| 97 | clearAxioms(); |
| 98 | Retracted.clear(); |
| 99 | EManager.clear(); |
| 100 | axiomToProcess = 0; |
| 101 | changed = false; |
| 102 | } |
| 103 | |
| 104 | /// get access to an expression manager |
| 105 | TExpressionManager* getExpressionManager ( void ) { return &EManager; } |
| 106 | |
| 107 | // access to axioms |
| 108 | |
| 109 | /// get RW access to all axioms in the ontology |
| 110 | AxiomVec& getAxioms ( void ) { return Axioms; } |
| 111 | /// get RO access to all axioms in the ontology |
| 112 | const AxiomVec& getAxioms ( void ) const { return Axioms; } |
| 113 | |
| 114 | /// RW begin() for the whole ontology |
| 115 | iterator begin ( void ) { return Axioms.begin(); } |
| 116 | /// RW end() for the whole ontology |
| 117 | iterator end ( void ) { return Axioms.end(); } |
| 118 | /// RW begin() for the unprocessed part of the ontology |
| 119 | iterator beginUnprocessed ( void ) { return Axioms.begin()+(long)axiomToProcess; } |
| 120 | /// RW end() for the processed part of the ontology |
| 121 | iterator endProcessed ( void ) { return beginUnprocessed(); } |
| 122 | /// RW begin() for retracted axioms |
| 123 | iterator beginRetracted ( void ) { return Retracted.begin(); } |
| 124 | /// RW end() for retracted axioms |
| 125 | iterator endRetracted ( void ) { return Retracted.end(); } |
| 126 | /// get access to the I'th axiom |
| 127 | TDLAxiom* operator [] ( size_t i ) { return Axioms[i]; } |
| 128 | |
| 129 | /// apply an axiom visitor to all used axioms in the ontology |
| 130 | void visitOntology ( DLAxiomVisitor& visitor ) |
| 131 | { |
| 132 | for ( TOntology::iterator p = begin(), p_end = end(); p < p_end; ++p ) |
| 133 | if ( likely((*p)->isUsed()) ) |
| 134 | (*p)->accept(visitor); |
| 135 | } |
| 136 | /// size of the ontology |
| 137 | size_t size ( void ) const { return Axioms.size(); } |
| 138 | /// get signature of all ontology axioms |
| 139 | TSignature getSignature ( void ) |
| 140 | { |
| 141 | TSignature sig; |
| 142 | for ( iterator p = begin(), p_end = end(); p != p_end; ++p ) |
| 143 | if ( likely((*p)->isUsed()) ) |
| 144 | sig.add((*p)->getSignature()); |
| 145 | return sig; |
| 146 | } |
| 147 | }; // TOntology |
| 148 | |
| 149 | #endif |