| 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_Hcxx . . $ `Kernel/Modularity.h:21:9`! |
| 22 | |
| 23 | #include <queue> |
| 24 | |
| 25 | // uncomment the next line to use AD to speed up modularisation |
| 26 | #define RKG_USE_AD_IN_MODULE_EXTRACTIONcxx . . $ `Kernel/Modularity.h:26:9`! |
| 27 | |
| 28 | #include "tOntology.h"cxx . . $ `<file>/Kernel/tOntology.h`/ |
| 29 | #include "SigIndex.h"cxx . . $ `<file>/Kernel/SigIndex.h`/ |
| 30 | #include "LocalityChecker.h"cxx . . $ `<file>/Kernel/LocalityChecker.h`/ |
| 31 | |
| 32 | #include "ModuleType.h"cxx . . $ `<file>/Kernel/ModuleType.h`/ |
| 33 | |
| 34 | #ifdef RKG_USE_AD_IN_MODULE_EXTRACTIONcxx . . $ `Kernel/Modularity.h:26:9`! |
| 35 | # include "tOntologyAtom.h"cxx . . $ `<file>/Kernel/tOntologyAtom.h`/ |
| 36 | #endif |
| 37 | |
| 38 | /// class to create modules of an ontology wrt module type |
| 39 | class TModularizercxx . . $ TModularizer# |
| 40 | { |
| 41 | protected: // types |
| 42 | /// RO iterator over axiom vector |
| 43 | typedef AxiomVeccxx . . $ AxiomVec#::const_iteratorcxx . . $ std/vector#const_iterator# const_iteratorcxx . . $ TModularizer#const_iterator#; |
| 44 | |
| 45 | protected: // members |
| 46 | /// shared signature signature |
| 47 | TSignaturecxx . . $ TSignature# sigcxx . . $ TModularizer#sig.; |
| 48 | /// internal syntactic locality checker |
| 49 | LocalityCheckercxx . . $ LocalityChecker#* Checkercxx . . $ TModularizer#Checker.; |
| 50 | /// module as a list of axioms |
| 51 | AxiomVeccxx . . $ AxiomVec# Modulecxx . . $ TModularizer#Module.; |
| 52 | /// pointer to a sig index; if not NULL then use optimized algo |
| 53 | SigIndexcxx . . $ SigIndex# sigIndexcxx . . $ TModularizer#sigIndex.; |
| 54 | /// queue of unprocessed entities |
| 55 | stdcxx . . $ std/::queuecxx . . $ std/queue#<const TNamedEntitycxx . . $ TNamedEntity#*> WorkQueuecxx . . $ TModularizer#WorkQueue.; |
| 56 | /// number of locality check calls |
| 57 | unsigned long long nCheckscxx . . $ TModularizer#nChecks. = 0; |
| 58 | /// number of non-local axioms |
| 59 | unsigned long long nNonLocalcxx . . $ TModularizer#nNonLocal. = 0; |
| 60 | /// true if no atoms are processed ATM |
| 61 | bool noAtomsProcessingcxx . . $ TModularizer#noAtomsProcessing. = true; |
| 62 | |
| 63 | protected: // methods |
| 64 | /// update SIG wrt the axiom signature |
| 65 | void addAxiomSigcxx . . $ TModularizer#addAxiomSig(7393b46d99f9f765). ( const TSignaturecxx . . $ TSignature#& axiomSig ) |
| 66 | { |
| 67 | for ( const TNamedEntitycxx . . $ TNamedEntity#* entity : axiomSig ) |
| 68 | if ( !sigcxx . . $ TModularizer#sig..containscxx . . $ TSignature#contains(d552066cd41c9e94).(entity) ) // new one |
| 69 | { |
| 70 | WorkQueuecxx . . $ TModularizer#WorkQueue..pushcxx . . $ std/queue#push(5cd2eaf705c8bad3).(entity); |
| 71 | sigcxx . . $ TModularizer#sig..addcxx . . $ TSignature#add(70f9d3c046cd5911).(entity); |
| 72 | } |
| 73 | } |
| 74 | /// add an axiom to a module |
| 75 | void addAxiomToModulecxx . . $ TModularizer#addAxiomToModule(ade74f2f8b540a23). ( TDLAxiomcxx . . $ TDLAxiom#* axiom ) |
| 76 | { |
| 77 | axiom->setInModulecxx . . $ TDLAxiom#setInModule(b45ddcbc8423892c).(true); |
| 78 | Modulecxx . . $ TModularizer#Module..push_backcxx . . $ std/vector#push_back(77d9f17553126ecb).(axiom); |
| 79 | // update the signature |
| 80 | addAxiomSigcxx . . $ TModularizer#addAxiomSig(7393b46d99f9f765).(axiom->getSignaturecxx . . $ TDLAxiom#getSignature(67d28bb768a72444).()); |
| 81 | } |
| 82 | /// @return true iff an AXiom is non-local |
| 83 | bool isNonLocalcxx . . $ TModularizer#isNonLocal(c12ce7eacb2f7daa). ( const TDLAxiomcxx . . $ TDLAxiom#* ax ) |
| 84 | { |
| 85 | ++nCheckscxx . . $ TModularizer#nChecks.; |
| 86 | if ( Checkercxx . . $ TModularizer#Checker.->localcxx . . $ LocalityChecker#local(c12ce7eacb2f7daa).(ax) ) |
| 87 | return false; |
| 88 | ++nNonLocalcxx . . $ TModularizer#nNonLocal.; |
| 89 | return true; |
| 90 | } |
| 91 | |
| 92 | /// add an axiom if it is non-local (or in noCheck is true) |
| 93 | void addNonLocalcxx . . $ TModularizer#addNonLocal(9ec1dfe7d5aa700a). ( TDLAxiomcxx . . $ TDLAxiom#* ax, bool noCheck ) |
| 94 | { |
| 95 | if ( unlikelycxx . . $ `Kernel/globaldef.h:29:10`!(noCheck) || unlikelycxx . . $ TModularizer#isNonLocal(c12ce7eacb2f7daa).(isNonLocal(ax)) ) |
| 96 | { |
| 97 | addAxiomToModulecxx . . $ TModularizer#addAxiomToModule(ade74f2f8b540a23).(ax); |
| 98 | |
| 99 | # ifdef RKG_USE_AD_IN_MODULE_EXTRACTIONcxx . . $ `Kernel/Modularity.h:26:9`! |
| 100 | if ( noAtomsProcessingcxx . . $ TModularizer#noAtomsProcessing. && ax->getAtomcxx . . $ TDLAxiom#getAtom(a636214afeaaa94e).() != nullptr ) |
| 101 | { |
| 102 | noAtomsProcessingcxx . . $ TModularizer#noAtomsProcessing. = false; |
| 103 | addNonLocalcxx . . $ TModularizer#addNonLocal(14ddc1e86372ac35). ( ax->getAtomcxx . . $ TDLAxiom#getAtom(a636214afeaaa94e).()->getModulecxx . . $ TOntologyAtom#getModule(73e4577bb59fbb07).(), /*noCheck=*/true ); |
| 104 | noAtomsProcessingcxx . . $ TModularizer#noAtomsProcessing. = true; |
| 105 | } |
| 106 | # endif |
| 107 | } |
| 108 | } |
| 109 | /// add all the non-local axioms from given axiom-set AxSet |
| 110 | void addNonLocalcxx . . $ TModularizer#addNonLocal(14ddc1e86372ac35). ( const AxiomVeccxx . . $ AxiomVec#& AxSet, bool noCheck ) |
| 111 | { |
| 112 | for ( TDLAxiomcxx . . $ TDLAxiom#* axiom : AxSet ) |
| 113 | if ( !axiom->isInModulecxx . . $ TDLAxiom#isInModule(50ce9a9e25b4a850).() && axiom->isInSScxx . . $ TDLAxiom#isInSS(50ce9a9e25b4a850).() ) // in the given range but not in module yet |
| 114 | addNonLocalcxx . . $ TModularizer#addNonLocal(9ec1dfe7d5aa700a). ( axiom, noCheck ); |
| 115 | } |
| 116 | /// build a module traversing axioms by a signature |
| 117 | void extractModuleQueuecxx . . $ TModularizer#extractModuleQueue(49f6e7a06ebc5aa8). ( void ) |
| 118 | { |
| 119 | // init queue with a sig |
| 120 | for ( const TNamedEntitycxx . . $ TNamedEntity#* entity : sigcxx . . $ TModularizer#sig. ) |
| 121 | WorkQueuecxx . . $ TModularizer#WorkQueue..pushcxx . . $ std/queue#push(5cd2eaf705c8bad3).(entity); |
| 122 | // add all the axioms that are non-local wrt given value of a top-locality |
| 123 | addNonLocalcxx . . $ TModularizer#addNonLocal(14ddc1e86372ac35). ( sigIndexcxx . . $ TModularizer#sigIndex..getNonLocalcxx . . $ SigIndex#getNonLocal(78afa40af56be0e7).(sigcxx . . $ TModularizer#sig..topCLocalcxx . . $ TSignature#topCLocal(50ce9a9e25b4a850).()), /*noCheck=*/true ); |
| 124 | // main cycle |
| 125 | while ( !WorkQueuecxx . . $ TModularizer#WorkQueue..emptycxx . . $ std/queue#empty(50ce9a9e25b4a850).() ) |
| 126 | { |
| 127 | const TNamedEntitycxx . . $ TNamedEntity#* entity = WorkQueuecxx . . $ TModularizer#WorkQueue..frontcxx . . $ std/queue#front(d8c1a3bbdff15733).(); |
| 128 | WorkQueuecxx . . $ TModularizer#WorkQueue..popcxx . . $ std/queue#pop(49f6e7a06ebc5aa8).(); |
| 129 | // for all the axioms that contains entity in their signature |
| 130 | addNonLocalcxx . . $ TModularizer#addNonLocal(14ddc1e86372ac35). ( sigIndexcxx . . $ TModularizer#sigIndex..getAxiomscxx . . $ SigIndex#getAxioms(cd43868b9f3c5fba).(entity), /*noCheck=*/false ); |
| 131 | } |
| 132 | } |
| 133 | /// extract module wrt presence of a sig index |
| 134 | void extractModulecxx . . $ TModularizer#extractModule(ff922734e0f040a5). ( const_iteratorcxx . . $ TModularizer#const_iterator# begin, const_iteratorcxx . . $ TModularizer#const_iterator# end ) |
| 135 | { |
| 136 | size_tcxx . . $ size_t# size = (size_tcxx . . $ size_t#)(end-cxx . . $ __gnu_cxx/operator-(a40477c209547c13).begin); |
| 137 | Modulecxx . . $ TModularizer#Module..clearcxx . . $ std/vector#clear(49f6e7a06ebc5aa8).(); |
| 138 | Modulecxx . . $ TModularizer#Module..reservecxx . . $ std/vector#reserve(2c44cd906ae4e829).(size); |
| 139 | // clear the module flag in the input |
| 140 | const_iteratorcxx . . $ TModularizer#const_iterator# pcxx . . $ __gnu_cxx/__normal_iterator#__normal_iterator(49f6e7a06ebc5aa8).; |
| 141 | for ( p =cxx . . $ __gnu_cxx/__normal_iterator#`operator=`(683292690af7599c). begin; p !=cxx . . $ __gnu_cxx/`operator!=`(ef3c5749062e825c). end; ++cxx . . $ __gnu_cxx/__normal_iterator#operator++(93f2e9c3bc4efc49).p ) |
| 142 | (*cxx . . $ __gnu_cxx/__normal_iterator#`operator*`(c076846f881f3e7e).p)->setInModulecxx . . $ TDLAxiom#setInModule(b45ddcbc8423892c).(false); |
| 143 | for ( p =cxx . . $ __gnu_cxx/__normal_iterator#`operator=`(683292690af7599c). begin; p !=cxx . . $ __gnu_cxx/`operator!=`(ef3c5749062e825c). end; ++cxx . . $ __gnu_cxx/__normal_iterator#operator++(93f2e9c3bc4efc49).p ) |
| 144 | if ( (*cxx . . $ __gnu_cxx/__normal_iterator#`operator*`(c076846f881f3e7e).p)->isUsedcxx . . $ TDLAxiom#isUsed(50ce9a9e25b4a850).() ) |
| 145 | (*cxx . . $ __gnu_cxx/__normal_iterator#`operator*`(c076846f881f3e7e).p)->setInSScxx . . $ TDLAxiom#setInSS(b45ddcbc8423892c).(true); |
| 146 | extractModuleQueuecxx . . $ TModularizer#extractModuleQueue(49f6e7a06ebc5aa8).(); |
| 147 | for ( p =cxx . . $ __gnu_cxx/__normal_iterator#`operator=`(683292690af7599c). begin; p !=cxx . . $ __gnu_cxx/`operator!=`(ef3c5749062e825c). end; ++cxx . . $ __gnu_cxx/__normal_iterator#operator++(93f2e9c3bc4efc49).p ) |
| 148 | (*cxx . . $ __gnu_cxx/__normal_iterator#`operator*`(c076846f881f3e7e).p)->setInSScxx . . $ TDLAxiom#setInSS(b45ddcbc8423892c).(false); |
| 149 | } |
| 150 | |
| 151 | public: // interface |
| 152 | /// init c'tor |
| 153 | explicit TModularizercxx . . $ TModularizer#TModularizer(95217cfac156eddf). ( ModuleMethodcxx . . $ ModuleMethod# moduleMethod ) |
| 154 | : Checkercxx . . $ TModularizer#Checker.(createLocalityCheckercxx . . $ createLocalityChecker(e4e24d5bf7d7495f).(moduleMethod,&sigcxx . . $ TModularizer#sig.)) |
| 155 | , sigIndexcxx . . $ TModularizer#sigIndex.(Checkercxx . . $ TModularizer#Checker.) |
| 156 | {} |
| 157 | // d'tor |
| 158 | ~cxx . . $ TModularizer#`~TModularizer`(49f6e7a06ebc5aa8).TModularizercxx . . $ TModularizer#() { delete Checkercxx . . $ TModularizer#Checker.; } |
| 159 | |
| 160 | /// allow the checker to preprocess an ontology if necessary |
| 161 | void preprocessOntologycxx . . $ TModularizer#preprocessOntology(b963b3dd7533a896). ( const AxiomVeccxx . . $ AxiomVec#& vec ) |
| 162 | { |
| 163 | Checkercxx . . $ TModularizer#Checker.->preprocessOntologycxx . . $ LocalityChecker#preprocessOntology(b963b3dd7533a896).(vec); |
| 164 | sigIndexcxx . . $ TModularizer#sigIndex..clearcxx . . $ SigIndex#clear(49f6e7a06ebc5aa8).(); |
| 165 | sigIndexcxx . . $ TModularizer#sigIndex..preprocessOntologycxx . . $ SigIndex#preprocessOntology(b963b3dd7533a896).(vec); |
| 166 | nCheckscxx . . $ TModularizer#nChecks. += 2*vec.sizecxx . . $ std/vector#size(fe4d2d262d4164a8).(); |
| 167 | } |
| 168 | /// extract module wrt SIGNATURE and TYPE from the set of axioms [BEGIN,END) |
| 169 | void extractcxx . . $ TModularizer#extract(67bca874b954bf38). ( const_iteratorcxx . . $ TModularizer#const_iterator# begin, const_iteratorcxx . . $ TModularizer#const_iterator# end, const TSignaturecxx . . $ TSignature#& signature, ModuleTypecxx . . $ ModuleType# type ) |
| 170 | { |
| 171 | bool topLocality = (type == M_TOPcxx . . $ M_TOP.); |
| 172 | |
| 173 | sigcxx . . $ TModularizer#sig. =cxx . . $ TSignature#`operator=`(6f74423c3ed79251). signature; |
| 174 | sigcxx . . $ TModularizer#sig..setLocalitycxx . . $ TSignature#setLocality(b45ddcbc8423892c).(topLocality); |
| 175 | extractModulecxx . . $ TModularizer#extractModule(ff922734e0f040a5). ( begin, end ); |
| 176 | |
| 177 | if ( type != M_STARcxx . . $ M_STAR. ) |
| 178 | return; |
| 179 | |
| 180 | // here there is a star: do the cycle until stabilization |
| 181 | size_tcxx . . $ size_t# size; |
| 182 | AxiomVeccxx . . $ AxiomVec# oldModulecxx . . $ std/vector#vector(49f6e7a06ebc5aa8).; |
| 183 | do |
| 184 | { |
| 185 | size = Modulecxx . . $ TModularizer#Module..sizecxx . . $ std/vector#size(fe4d2d262d4164a8).(); |
| 186 | oldModule.swapcxx . . $ std/vector#swap(a8be04fc17cbdf49).(Modulecxx . . $ TModularizer#Module.); |
| 187 | topLocality = !topLocality; |
| 188 | |
| 189 | sigcxx . . $ TModularizer#sig. =cxx . . $ TSignature#`operator=`(6f74423c3ed79251). signature; |
| 190 | sigcxx . . $ TModularizer#sig..setLocalitycxx . . $ TSignature#setLocality(b45ddcbc8423892c).(topLocality); |
| 191 | extractModulecxx . . $ TModularizer#extractModule(ff922734e0f040a5). ( oldModulecxx . . $ __gnu_cxx/__normal_iterator#__normal_iterator(ea0f1a22ba5f21a0)..begincxx . . $ std/vector#begin(33b10a496a38335b).(), oldModulecxx . . $ __gnu_cxx/__normal_iterator#__normal_iterator(ea0f1a22ba5f21a0)..endcxx . . $ std/vector#end(33b10a496a38335b).() ); |
| 192 | } while ( size != Modulecxx . . $ TModularizer#Module..sizecxx . . $ std/vector#size(fe4d2d262d4164a8).() ); |
| 193 | } |
| 194 | /// extract module wrt SIGNATURE and TYPE from the axiom vector VEC |
| 195 | void extractcxx . . $ TModularizer#extract(8de34f3ef94cc7cc). ( const AxiomVeccxx . . $ AxiomVec#& Vec, const TSignaturecxx . . $ TSignature#& signature, ModuleTypecxx . . $ ModuleType# type ) |
| 196 | { extractcxx . . $ TModularizer#extract(67bca874b954bf38). ( Vec.begincxx . . $ std/vector#begin(359ca5030082c9f).(), Vec.endcxx . . $ std/vector#end(359ca5030082c9f).(), signature, type ); } |
| 197 | /// extract module wrt SIGNATURE and TYPE from O |
| 198 | void extractcxx . . $ TModularizer#extract(622b9056d94a7959). ( const TOntologycxx . . $ TOntology#& O, const TSignaturecxx . . $ TSignature#& signature, ModuleTypecxx . . $ ModuleType# type ) |
| 199 | { extractcxx . . $ TModularizer#extract(8de34f3ef94cc7cc). ( O.getAxiomscxx . . $ TOntology#getAxioms(73e4577bb59fbb07).(), signature, type ); } |
| 200 | /// @return true iff the axiom AX is a tautology wrt given type |
| 201 | bool isTautologycxx . . $ TModularizer#isTautology(5bc3322d491988e). ( TDLAxiomcxx . . $ TDLAxiom#* ax, ModuleTypecxx . . $ ModuleType# type ) |
| 202 | { |
| 203 | bool topLocality = (type == M_TOPcxx . . $ M_TOP.); |
| 204 | sigcxx . . $ TModularizer#sig. =cxx . . $ TSignature#`operator=`(6f74423c3ed79251). ax->getSignaturecxx . . $ TDLAxiom#getSignature(67d28bb768a72444).(); |
| 205 | sigcxx . . $ TModularizer#sig..setLocalitycxx . . $ TSignature#setLocality(b45ddcbc8423892c).(topLocality); |
| 206 | // axiom is a tautology if it is local wrt its own signature |
| 207 | bool toReturn = Checkercxx . . $ TModularizer#Checker.->localcxx . . $ LocalityChecker#local(c12ce7eacb2f7daa).(ax); |
| 208 | if ( likelycxx . . $ M_STAR. ( type != M_STAR || !toReturn ) ) |
| 209 | return toReturn; |
| 210 | // here it is STAR case and AX is local wrt BOT |
| 211 | sigcxx . . $ TModularizer#sig..setLocalitycxx . . $ TSignature#setLocality(b45ddcbc8423892c).(!topLocality); |
| 212 | return Checkercxx . . $ TModularizer#Checker.->localcxx . . $ LocalityChecker#local(c12ce7eacb2f7daa).(ax); |
| 213 | } |
| 214 | |
| 215 | /// get RW access to the sigIndex (mainly to (un-)register axioms on the fly) |
| 216 | SigIndexcxx . . $ SigIndex#* getSigIndexcxx . . $ TModularizer#getSigIndex(eb53b4cf68e66109). ( void ) { return &sigIndexcxx . . $ TModularizer#sigIndex.; } |
| 217 | |
| 218 | /// get the last computed module |
| 219 | const AxiomVeccxx . . $ AxiomVec#& getModulecxx . . $ TModularizer#getModule(73e4577bb59fbb07). ( void ) const { return Modulecxx . . $ TModularizer#Module.; } |
| 220 | /// get access to a signature |
| 221 | const TSignaturecxx . . $ TSignature#& getSignaturecxx . . $ TModularizer#getSignature(cb1eb85423b8268e). ( void ) const { return sigcxx . . $ TModularizer#sig.; } |
| 222 | /// get access to the Locality checker |
| 223 | LocalityCheckercxx . . $ LocalityChecker#* getLocalityCheckercxx . . $ TModularizer#getLocalityChecker(217d0df15fc6e9c). ( void ) { return Checkercxx . . $ TModularizer#Checker.; } |
| 224 | /// get number of checks made |
| 225 | unsigned long long getNCheckscxx . . $ TModularizer#getNChecks(1c5fc1fd41a99ae7). ( void ) const { return nCheckscxx . . $ TModularizer#nChecks.; } |
| 226 | /// get number of axioms that were local |
| 227 | unsigned long long getNNonLocalcxx . . $ TModularizer#getNNonLocal(1c5fc1fd41a99ae7). ( void ) const { return nNonLocalcxx . . $ TModularizer#nNonLocal.; } |
| 228 | }; // TModularizer |
| 229 | |
| 230 | #endif |