| 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 SIGINDEX_H |
| 21 | #define SIGINDEX_H |
| 22 | |
| 23 | #include <map> |
| 24 | |
| 25 | #include "tDLAxiom.h" |
| 26 | #include "tSignature.h" |
| 27 | #include "LocalityChecker.h" |
| 28 | |
| 29 | class SigIndex |
| 30 | { |
| 31 | public: // types |
| 32 | /// RW iterator over set of axioms |
| 33 | typedef AxiomVec::iterator iterator; |
| 34 | /// RO iterator over set of axioms |
| 35 | typedef AxiomVec::const_iterator const_iterator; |
| 36 | |
| 37 | protected: // types |
| 38 | /// map between entities and axioms that contains them in their signature |
| 39 | typedef std::map<const TNamedEntity*, AxiomVec> EntityAxiomMap; |
| 40 | |
| 41 | protected: // members |
| 42 | /// map itself |
| 43 | EntityAxiomMap Base; |
| 44 | /// locality checker |
| 45 | LocalityChecker* Checker; |
| 46 | /// sets of axioms non-local wrt the empty signature |
| 47 | AxiomVec topNonLocal, botNonLocal; |
| 48 | /// empty signature to test the non-locality |
| 49 | TSignature emptySig; |
| 50 | /// number of registered axioms |
| 51 | unsigned int nRegistered = 0; |
| 52 | /// number of registered axioms |
| 53 | unsigned int nUnregistered = 0; |
| 54 | |
| 55 | protected: // methods |
| 56 | /// add an axiom AX to an axiom set AXIOMS |
| 57 | void add ( AxiomVec& axioms, TDLAxiom* ax ) { axioms.push_back(ax); } |
| 58 | /// remove an axiom AX from an axiom set AXIOMS |
| 59 | void remove ( AxiomVec& axioms, TDLAxiom* ax ) |
| 60 | { |
| 61 | for ( iterator p = axioms.begin(), p_end = axioms.end(); p != p_end; ++p ) |
| 62 | if ( *p == ax ) |
| 63 | { |
| 64 | *p = axioms.back(); |
| 65 | axioms.pop_back(); |
| 66 | break; |
| 67 | } |
| 68 | } |
| 69 | /// add axiom AX to the non-local set with top-locality value TOP |
| 70 | void checkNonLocal ( TDLAxiom* ax, bool top ) |
| 71 | { |
| 72 | emptySig.setLocality(top); |
| 73 | Checker->setSignatureValue(emptySig); |
| 74 | if ( !Checker->local(ax) ) |
| 75 | add ( top ? topNonLocal : botNonLocal, ax ); |
| 76 | } |
| 77 | |
| 78 | // work with axioms |
| 79 | |
| 80 | /// register an axiom |
| 81 | void registerAx ( TDLAxiom* ax ) |
| 82 | { |
| 83 | for ( TSignature::iterator p = ax->getSignature().begin(), p_end = ax->getSignature().end(); p != p_end; ++p ) |
| 84 | add ( Base[*p], ax ); |
| 85 | // check whether the axiom is non-local |
| 86 | checkNonLocal ( ax, /*top=*/false ); |
| 87 | checkNonLocal ( ax, /*top=*/true ); |
| 88 | ++nRegistered; |
| 89 | } |
| 90 | /// unregister an axiom AX |
| 91 | void unregisterAx ( TDLAxiom* ax ) |
| 92 | { |
| 93 | for ( TSignature::iterator p = ax->getSignature().begin(), p_end = ax->getSignature().end(); p != p_end; ++p ) |
| 94 | remove ( Base[*p], ax ); |
| 95 | // remove from the non-locality |
| 96 | remove ( topNonLocal, ax ); |
| 97 | remove ( botNonLocal, ax ); |
| 98 | ++nUnregistered; |
| 99 | } |
| 100 | |
| 101 | public: // interface |
| 102 | /// empty c'tor |
| 103 | explicit SigIndex ( LocalityChecker* checker ) : Checker(checker) {} |
| 104 | |
| 105 | // work with axioms |
| 106 | |
| 107 | /// process an axiom wrt its Used status |
| 108 | void processAx ( TDLAxiom* ax ) |
| 109 | { |
| 110 | if ( ax->isUsed() ) |
| 111 | registerAx(ax); |
| 112 | else |
| 113 | unregisterAx(ax); |
| 114 | } |
| 115 | /// preprocess given set of axioms |
| 116 | void preprocessOntology ( const AxiomVec& axioms ) |
| 117 | { |
| 118 | for (TDLAxiom* axiom : axioms) |
| 119 | processAx(axiom); |
| 120 | } |
| 121 | /// clear internal structures |
| 122 | void clear ( void ) |
| 123 | { |
| 124 | Base.clear(); |
| 125 | topNonLocal.clear(); |
| 126 | botNonLocal.clear(); |
| 127 | } |
| 128 | |
| 129 | // get the set by the index |
| 130 | |
| 131 | /// given an entity, return a set of all axioms that contain this entity in a signature |
| 132 | const AxiomVec& getAxioms ( const TNamedEntity* entity ) { return Base[entity]; } |
| 133 | /// get the non-local axioms with top-locality value TOP |
| 134 | const AxiomVec& getNonLocal ( bool top ) const { return top ? topNonLocal : botNonLocal; } |
| 135 | |
| 136 | // access to statistics |
| 137 | |
| 138 | /// get number of ever processed axioms |
| 139 | unsigned int nProcessedAx ( void ) const { return nRegistered; } |
| 140 | /// get number of currently registered axioms |
| 141 | unsigned int nRegisteredAx ( void ) const { return nRegistered - nUnregistered; } |
| 142 | }; // SigIndex |
| 143 | |
| 144 | #endif |