| 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 MODULARITY_H |
| 21 | #define MODULARITY_H |
| 22 | |
| 23 | #include <queue> |
| 24 | |
| 25 | // uncomment the next line to use AD to speed up modularisation |
| 26 | #define RKG_USE_AD_IN_MODULE_EXTRACTION |
| 27 | |
| 28 | #include "tOntology.h" |
| 29 | #include "SigIndex.h" |
| 30 | #include "LocalityChecker.h" |
| 31 | |
| 32 | #include "ModuleType.h" |
| 33 | |
| 34 | #ifdef RKG_USE_AD_IN_MODULE_EXTRACTION |
| 35 | # include "tOntologyAtom.h" |
| 36 | #endif |
| 37 | |
| 38 | /// class to create modules of an ontology wrt module type |
| 39 | class TModularizer |
| 40 | { |
| 41 | protected: // types |
| 42 | /// RO iterator over axiom vector |
| 43 | typedef AxiomVec::const_iterator const_iterator; |
| 44 | |
| 45 | protected: // members |
| 46 | /// shared signature signature |
| 47 | TSignature sig; |
| 48 | /// internal syntactic locality checker |
| 49 | LocalityChecker* Checker; |
| 50 | /// module as a list of axioms |
| 51 | AxiomVec Module; |
| 52 | /// pointer to a sig index; if not NULL then use optimized algo |
| 53 | SigIndex sigIndex; |
| 54 | /// queue of unprocessed entities |
| 55 | std::queue<const TNamedEntity*> WorkQueue; |
| 56 | /// number of locality check calls |
| 57 | unsigned long long nChecks = 0; |
| 58 | /// number of non-local axioms |
| 59 | unsigned long long nNonLocal = 0; |
| 60 | /// true if no atoms are processed ATM |
| 61 | bool noAtomsProcessing = true; |
| 62 | |
| 63 | protected: // methods |
| 64 | /// update SIG wrt the axiom signature |
| 65 | void addAxiomSig ( const TSignature& axiomSig ) |
| 66 | { |
| 67 | for ( const TNamedEntity* entity : axiomSig ) |
| 68 | if ( !sig.contains(entity) ) // new one |
| 69 | { |
| 70 | WorkQueue.push(entity); |
| 71 | sig.add(entity); |
| 72 | } |
| 73 | } |
| 74 | /// add an axiom to a module |
| 75 | void addAxiomToModule ( TDLAxiom* axiom ) |
| 76 | { |
| 77 | axiom->setInModule(true); |
| 78 | Module.push_back(axiom); |
| 79 | // update the signature |
| 80 | addAxiomSig(axiom->getSignature()); |
| 81 | } |
| 82 | /// @return true iff an AXiom is non-local |
| 83 | bool isNonLocal ( const TDLAxiom* ax ) |
| 84 | { |
| 85 | ++nChecks; |
| 86 | if ( Checker->local(ax) ) |
| 87 | return false; |
| 88 | ++nNonLocal; |
| 89 | return true; |
| 90 | } |
| 91 | |
| 92 | /// add an axiom if it is non-local (or in noCheck is true) |
| 93 | void addNonLocal ( TDLAxiom* ax, bool noCheck ) |
| 94 | { |
| 95 | if ( unlikely(noCheck) || unlikely(isNonLocal(ax)) ) |
| 96 | { |
| 97 | addAxiomToModule(ax); |
| 98 | |
| 99 | # ifdef RKG_USE_AD_IN_MODULE_EXTRACTION |
| 100 | if ( noAtomsProcessing && ax->getAtom() != nullptr ) |
| 101 | { |
| 102 | noAtomsProcessing = false; |
| 103 | addNonLocal ( ax->getAtom()->getModule(), /*noCheck=*/true ); |
| 104 | noAtomsProcessing = true; |
| 105 | } |
| 106 | # endif |
| 107 | } |
| 108 | } |
| 109 | /// add all the non-local axioms from given axiom-set AxSet |
| 110 | void addNonLocal ( const AxiomVec& AxSet, bool noCheck ) |
| 111 | { |
| 112 | for ( TDLAxiom* axiom : AxSet ) |
| 113 | if ( !axiom->isInModule() && axiom->isInSS() ) // in the given range but not in module yet |
| 114 | addNonLocal ( axiom, noCheck ); |
| 115 | } |
| 116 | /// build a module traversing axioms by a signature |
| 117 | void extractModuleQueue ( void ) |
| 118 | { |
| 119 | // init queue with a sig |
| 120 | for ( const TNamedEntity* entity : sig ) |
| 121 | WorkQueue.push(entity); |
| 122 | // add all the axioms that are non-local wrt given value of a top-locality |
| 123 | addNonLocal ( sigIndex.getNonLocal(sig.topCLocal()), /*noCheck=*/true ); |
| 124 | // main cycle |
| 125 | while ( !WorkQueue.empty() ) |
| 126 | { |
| 127 | const TNamedEntity* entity = WorkQueue.front(); |
| 128 | WorkQueue.pop(); |
| 129 | // for all the axioms that contains entity in their signature |
| 130 | addNonLocal ( sigIndex.getAxioms(entity), /*noCheck=*/false ); |
| 131 | } |
| 132 | } |
| 133 | /// extract module wrt presence of a sig index |
| 134 | void extractModule ( const_iterator begin, const_iterator end ) |
| 135 | { |
| 136 | size_t size = (size_t)(end-begin); |
| 137 | Module.clear(); |
| 138 | Module.reserve(size); |
| 139 | // clear the module flag in the input |
| 140 | const_iterator p; |
| 141 | for ( p = begin; p != end; ++p ) |
| 142 | (*p)->setInModule(false); |
| 143 | for ( p = begin; p != end; ++p ) |
| 144 | if ( (*p)->isUsed() ) |
| 145 | (*p)->setInSS(true); |
| 146 | extractModuleQueue(); |
| 147 | for ( p = begin; p != end; ++p ) |
| 148 | (*p)->setInSS(false); |
| 149 | } |
| 150 | |
| 151 | public: // interface |
| 152 | /// init c'tor |
| 153 | explicit TModularizer ( ModuleMethod moduleMethod ) |
| 154 | : Checker(createLocalityChecker(moduleMethod,&sig)) |
| 155 | , sigIndex(Checker) |
| 156 | {} |
| 157 | // d'tor |
| 158 | ~TModularizer() { delete Checker; } |
| 159 | |
| 160 | /// allow the checker to preprocess an ontology if necessary |
| 161 | void preprocessOntology ( const AxiomVec& vec ) |
| 162 | { |
| 163 | Checker->preprocessOntology(vec); |
| 164 | sigIndex.clear(); |
| 165 | sigIndex.preprocessOntology(vec); |
| 166 | nChecks += 2*vec.size(); |
| 167 | } |
| 168 | /// extract module wrt SIGNATURE and TYPE from the set of axioms [BEGIN,END) |
| 169 | void extract ( const_iterator begin, const_iterator end, const TSignature& signature, ModuleType type ) |
| 170 | { |
| 171 | bool topLocality = (type == M_TOP); |
| 172 | |
| 173 | sig = signature; |
| 174 | sig.setLocality(topLocality); |
| 175 | extractModule ( begin, end ); |
| 176 | |
| 177 | if ( type != M_STAR ) |
| 178 | return; |
| 179 | |
| 180 | // here there is a star: do the cycle until stabilization |
| 181 | size_t size; |
| 182 | AxiomVec oldModule; |
| 183 | do |
| 184 | { |
| 185 | size = Module.size(); |
| 186 | oldModule.swap(Module); |
| 187 | topLocality = !topLocality; |
| 188 | |
| 189 | sig = signature; |
| 190 | sig.setLocality(topLocality); |
| 191 | extractModule ( oldModule.begin(), oldModule.end() ); |
| 192 | } while ( size != Module.size() ); |
| 193 | } |
| 194 | /// extract module wrt SIGNATURE and TYPE from the axiom vector VEC |
| 195 | void extract ( const AxiomVec& Vec, const TSignature& signature, ModuleType type ) |
| 196 | { extract ( Vec.begin(), Vec.end(), signature, type ); } |
| 197 | /// extract module wrt SIGNATURE and TYPE from O |
| 198 | void extract ( const TOntology& O, const TSignature& signature, ModuleType type ) |
| 199 | { extract ( O.getAxioms(), signature, type ); } |
| 200 | /// @return true iff the axiom AX is a tautology wrt given type |
| 201 | bool isTautology ( TDLAxiom* ax, ModuleType type ) |
| 202 | { |
| 203 | bool topLocality = (type == M_TOP); |
| 204 | sig = ax->getSignature(); |
| 205 | sig.setLocality(topLocality); |
| 206 | // axiom is a tautology if it is local wrt its own signature |
| 207 | bool toReturn = Checker->local(ax); |
| 208 | if ( likely ( type != M_STAR || !toReturn ) ) |
| 209 | return toReturn; |
| 210 | // here it is STAR case and AX is local wrt BOT |
| 211 | sig.setLocality(!topLocality); |
| 212 | return Checker->local(ax); |
| 213 | } |
| 214 | |
| 215 | /// get RW access to the sigIndex (mainly to (un-)register axioms on the fly) |
| 216 | SigIndex* getSigIndex ( void ) { return &sigIndex; } |
| 217 | |
| 218 | /// get the last computed module |
| 219 | const AxiomVec& getModule ( void ) const { return Module; } |
| 220 | /// get access to a signature |
| 221 | const TSignature& getSignature ( void ) const { return sig; } |
| 222 | /// get access to the Locality checker |
| 223 | LocalityChecker* getLocalityChecker ( void ) { return Checker; } |
| 224 | /// get number of checks made |
| 225 | unsigned long long getNChecks ( void ) const { return nChecks; } |
| 226 | /// get number of axioms that were local |
| 227 | unsigned long long getNNonLocal ( void ) const { return nNonLocal; } |
| 228 | }; // TModularizer |
| 229 | |
| 230 | #endif |