| 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 MODELCACHEIAN_H |
| 21 | #define MODELCACHEIAN_Hcxx . . $ `Kernel/modelCacheIan.h:21:9`! |
| 22 | |
| 23 | #include "modelCacheSingleton.h"cxx . . $ `<file>/Kernel/modelCacheSingleton.h`/ |
| 24 | #include "dlCompletionTree.h"cxx . . $ `<file>/Kernel/dlCompletionTree.h`/ |
| 25 | #include "dlDag.h"cxx . . $ `<file>/Kernel/dlDag.h`/ |
| 26 | #include "tSetAsTree.h"cxx . . $ `<file>/Kernel/tSetAsTree.h`/ |
| 27 | |
| 28 | class SaveLoadManagercxx . . $ SaveLoadManager#; |
| 29 | |
| 30 | /** model caching implementation of Ian's algorithms. |
| 31 | Very fast cache check, but less precise than AsTree one. |
| 32 | */ |
| 33 | class modelCacheIancxx . . $ modelCacheIan#: public modelCacheInterfacecxx . . $ modelCacheInterface# |
| 34 | { |
| 35 | // FIXME!! for now only |
| 36 | friend class DLConceptTaxonomycxx . . $ DLConceptTaxonomy#; |
| 37 | protected: // types |
| 38 | /// define the type of an index set |
| 39 | typedef TSetAsTreecxx . . $ TSetAsTree# IndexSetcxx . . $ modelCacheIan#IndexSet#; |
| 40 | /// node label iterator |
| 41 | typedef DlCompletionTreecxx . . $ DlCompletionTree#::const_label_iteratorcxx . . $ DlCompletionTree#const_label_iterator# l_iteratorcxx . . $ modelCacheIan#l_iterator#; |
| 42 | /// edges iterator |
| 43 | typedef DlCompletionTreecxx . . $ DlCompletionTree#::const_edge_iteratorcxx . . $ DlCompletionTree#const_edge_iterator# e_iteratorcxx . . $ modelCacheIan#e_iterator#; |
| 44 | |
| 45 | protected: // members |
| 46 | // sets for the cache |
| 47 | |
| 48 | /// named concepts that appears positively det-lly in a root node of a cache |
| 49 | IndexSetcxx . . $ modelCacheIan#IndexSet# posDConceptscxx . . $ modelCacheIan#posDConcepts.; |
| 50 | /// named concepts that appears positively non-det in a root node of a cache |
| 51 | IndexSetcxx . . $ modelCacheIan#IndexSet# posNConceptscxx . . $ modelCacheIan#posNConcepts.; |
| 52 | /// named concepts that appears negatively det-lly in a root node of a cache |
| 53 | IndexSetcxx . . $ modelCacheIan#IndexSet# negDConceptscxx . . $ modelCacheIan#negDConcepts.; |
| 54 | /// named concepts that appears negatively non-det in a root node of a cache |
| 55 | IndexSetcxx . . $ modelCacheIan#IndexSet# negNConceptscxx . . $ modelCacheIan#negNConcepts.; |
| 56 | #ifdef RKG_USE_SIMPLE_RULES |
| 57 | /// extra det-lly concepts that are (partial) Simple Rule applications |
| 58 | IndexSet extraDConcepts; |
| 59 | /// extra non-det concepts that are (partial) Simple Rule applications |
| 60 | IndexSet extraNConcepts; |
| 61 | #endif |
| 62 | /// role names that are labels of the outgoing edges from the root node |
| 63 | IndexSetcxx . . $ modelCacheIan#IndexSet# existsRolescxx . . $ modelCacheIan#existsRoles.; |
| 64 | /// role names that appears in the \A restrictions in the root node |
| 65 | IndexSetcxx . . $ modelCacheIan#IndexSet# forallRolescxx . . $ modelCacheIan#forallRoles.; |
| 66 | /// role names that appears in the atmost restrictions in the root node |
| 67 | IndexSetcxx . . $ modelCacheIan#IndexSet# funcRolescxx . . $ modelCacheIan#funcRoles.; |
| 68 | |
| 69 | /// current state of cache model; recalculates on every change |
| 70 | modelCacheStatecxx . . $ modelCacheState# curStatecxx . . $ modelCacheIan#curState. = csValidcxx . . $ csValid.; |
| 71 | |
| 72 | protected: // methods |
| 73 | /// log a particular implementation of a cache entry |
| 74 | void logCacheEntryImplcxx . . $ modelCacheIan#logCacheEntryImpl(83f5f38750778ac8). ( void ) const override; |
| 75 | /// add single concept from label to cache |
| 76 | void processConceptcxx . . $ modelCacheIan#processConcept(adb2ecfc6a6b7458). ( const DLVertexcxx . . $ DLVertex#& cur, bool pos, bool det ); |
| 77 | /// add all roles that are accepted by an automaton from a given entry |
| 78 | void processAutomatoncxx . . $ modelCacheIan#processAutomaton(c29169630048b37c). ( const DLVertexcxx . . $ DLVertex#& cur ); |
| 79 | /// process CT label in given interval; set Deterministic accordingly |
| 80 | void processLabelIntervalcxx . . $ modelCacheIan#processLabelInterval(fe232a4c7c664ebb). ( const DLDagcxx . . $ DLDag#& DLHeap, l_iteratorcxx . . $ modelCacheIan#l_iterator# start, l_iteratorcxx . . $ modelCacheIan#l_iterator# end ) |
| 81 | { |
| 82 | for ( l_iteratorcxx . . $ modelCacheIan#l_iterator# p = start; p !=cxx . . $ __gnu_cxx/`operator!=`(ef3c5749062e825c). end; ++cxx . . $ __gnu_cxx/__normal_iterator#operator++(93f2e9c3bc4efc49).p ) |
| 83 | processConceptcxx . . $ modelCacheIan#processConcept(adb2ecfc6a6b7458). ( DLHeap[cxx . . $ DLDag#`operator[]`(674db0146e4e3588).*cxx . . $ __gnu_cxx/__normal_iterator#`operator*`(c076846f881f3e7e).p], isPositivecxx . . $ isPositive(83c267b20bac841a).(p->cxx . . $ __gnu_cxx/__normal_iterator#`operator->`(a68f0626e749d9f6).bpcxx . . $ ConceptWDep#bp(455f465bc33b4cdf).()), p->cxx . . $ __gnu_cxx/__normal_iterator#`operator->`(a68f0626e749d9f6).getDepcxx . . $ ConceptWDep#getDep(bce6462a16d96b29).().emptycxx . . $ TDepSet#empty(50ce9a9e25b4a850).() ); |
| 84 | } |
| 85 | /// fills cache sets by tree->Label; set Deterministic accordingly |
| 86 | void initCacheByLabelcxx . . $ modelCacheIan#initCacheByLabel(fbc0cf1968348b5b). ( const DLDagcxx . . $ DLDag#& DLHeap, const DlCompletionTreecxx . . $ DlCompletionTree#* pCT ) |
| 87 | { |
| 88 | processLabelIntervalcxx . . $ modelCacheIan#processLabelInterval(fe232a4c7c664ebb). ( DLHeap, pCT->beginl_sccxx . . $ DlCompletionTree#beginl_sc(e55d760a93e9f197).(), pCT->endl_sccxx . . $ DlCompletionTree#endl_sc(e55d760a93e9f197).() ); |
| 89 | processLabelIntervalcxx . . $ modelCacheIan#processLabelInterval(fe232a4c7c664ebb). ( DLHeap, pCT->beginl_cccxx . . $ DlCompletionTree#beginl_cc(e55d760a93e9f197).(), pCT->endl_cccxx . . $ DlCompletionTree#endl_cc(e55d760a93e9f197).() ); |
| 90 | } |
| 91 | /// adds role to exists- and func-role if necessary |
| 92 | void addRoleToCachecxx . . $ modelCacheIan#addRoleToCache(67b1ad1f11addbc2). ( const TRolecxx . . $ TRole#* R ) |
| 93 | { |
| 94 | existsRolescxx . . $ modelCacheIan#existsRoles..insertcxx . . $ TSetAsTree#insert(cb2f890c2fdad230).(R->indexcxx . . $ ClassifiableEntry#index(ddd8539a2fcdd4ce).()); |
| 95 | if ( R->isTopFunccxx . . $ TRole#isTopFunc(50ce9a9e25b4a850).() ) // all other top-funcs would be added separately |
| 96 | funcRolescxx . . $ modelCacheIan#funcRoles..insertcxx . . $ TSetAsTree#insert(cb2f890c2fdad230).(R->indexcxx . . $ ClassifiableEntry#index(ddd8539a2fcdd4ce).()); |
| 97 | } |
| 98 | /// adds role (and all its super-roles) to exists- and funcRoles |
| 99 | void addExistsRolecxx . . $ modelCacheIan#addExistsRole(67b1ad1f11addbc2). ( const TRolecxx . . $ TRole#* R ) |
| 100 | { |
| 101 | addRoleToCachecxx . . $ modelCacheIan#addRoleToCache(67b1ad1f11addbc2).(R); |
| 102 | for ( auto sup: R->ancestorscxx . . $ TRole#ancestors(5c1fd3e4e1fb59f2).() ) |
| 103 | addRoleToCachecxx . . $ modelCacheIan#addRoleToCache(67b1ad1f11addbc2).(sup); |
| 104 | } |
| 105 | |
| 106 | // access to the arrays |
| 107 | |
| 108 | /// get RW access to D-concepts wrt polarity |
| 109 | IndexSetcxx . . $ modelCacheIan#IndexSet#& getDConceptscxx . . $ modelCacheIan#getDConcepts(60cdb7ab70d95073). ( bool pos ) { return pos ? posDConceptscxx . . $ modelCacheIan#posDConcepts. : negDConceptscxx . . $ modelCacheIan#negDConcepts.; } |
| 110 | /// get RW access to N-concepts wrt polarity |
| 111 | IndexSetcxx . . $ modelCacheIan#IndexSet#& getNConceptscxx . . $ modelCacheIan#getNConcepts(60cdb7ab70d95073). ( bool pos ) { return pos ? posNConceptscxx . . $ modelCacheIan#posNConcepts. : negNConceptscxx . . $ modelCacheIan#negNConcepts.; } |
| 112 | /// get RO access to D-concepts wrt polarity |
| 113 | const IndexSetcxx . . $ modelCacheIan#IndexSet#& getDConceptscxx . . $ modelCacheIan#getDConcepts(ae3169f0774a60fb). ( bool pos ) const { return pos ? posDConceptscxx . . $ modelCacheIan#posDConcepts. : negDConceptscxx . . $ modelCacheIan#negDConcepts.; } |
| 114 | /// get RO access to N-concepts wrt polarity |
| 115 | const IndexSetcxx . . $ modelCacheIan#IndexSet#& getNConceptscxx . . $ modelCacheIan#getNConcepts(ae3169f0774a60fb). ( bool pos ) const { return pos ? posNConceptscxx . . $ modelCacheIan#posNConcepts. : negNConceptscxx . . $ modelCacheIan#negNConcepts.; } |
| 116 | #ifdef RKG_USE_SIMPLE_RULES |
| 117 | /// get RW access to extra concepts wrt deterministic flag |
| 118 | IndexSet& getExtra ( bool det ) { return det ? extraDConcepts : extraNConcepts; } |
| 119 | /// get RO access to extra concepts wrt deterministic flag |
| 120 | const IndexSet& getExtra ( bool det ) const { return det ? extraDConcepts : extraNConcepts; } |
| 121 | #endif |
| 122 | |
| 123 | // merge support |
| 124 | |
| 125 | /// implementation of merging with Singleton cache type |
| 126 | modelCacheStatecxx . . $ modelCacheState# isMergeableSingletoncxx . . $ modelCacheIan#isMergeableSingleton(82cef7062dc31a0c). ( BipolarPointercxx . . $ BipolarPointer# bp ) const; |
| 127 | /// implementation of merging with Ian's cache type |
| 128 | modelCacheStatecxx . . $ modelCacheState# isMergeableIancxx . . $ modelCacheIan#isMergeableIan(344470c102e55d69). ( const modelCacheIancxx . . $ modelCacheIan#* p ) const; |
| 129 | /// actual merge with a singleton cache |
| 130 | void mergeSingletoncxx . . $ modelCacheIan#mergeSingleton(d4f767463ce0a6b3). ( BipolarPointercxx . . $ BipolarPointer# bp ); |
| 131 | /// actual merge with an Ian's cache |
| 132 | void mergeIancxx . . $ modelCacheIan#mergeIan(98877cad36d42a4d). ( const modelCacheIancxx . . $ modelCacheIan#* p ); |
| 133 | |
| 134 | public: |
| 135 | /// Create cache model of given CompletionTree using given HEAP |
| 136 | modelCacheIancxx . . $ modelCacheIan#modelCacheIan(ec72f3a57810126b). ( const DLDagcxx . . $ DLDag#& heap, const DlCompletionTreecxx . . $ DlCompletionTree#* p, bool flagNominals, unsigned int nC, unsigned int nR ) |
| 137 | : modelCacheInterfacecxx . . $ modelCacheInterface#modelCacheInterface(b45ddcbc8423892c).(flagNominals) |
| 138 | , posDConceptscxx . . $ modelCacheIan#posDConcepts.(nC) |
| 139 | , posNConceptscxx . . $ TSetAsTree#TSetAsTree(cb2f890c2fdad230).(nC) |
| 140 | , negDConceptscxx . . $ modelCacheIan#negDConcepts.(nC) |
| 141 | , negNConceptscxx . . $ TSetAsTree#TSetAsTree(cb2f890c2fdad230).(nC) |
| 142 | # ifdef RKG_USE_SIMPLE_RULES |
| 143 | , extraDConcepts(nC) |
| 144 | , extraNConcepts(nC) |
| 145 | # endif |
| 146 | , existsRolescxx . . $ TSetAsTree#TSetAsTree(cb2f890c2fdad230).(nR) |
| 147 | , forallRolescxx . . $ TSetAsTree#TSetAsTree(cb2f890c2fdad230).(nR) |
| 148 | , funcRolescxx . . $ modelCacheIan#funcRoles.(nR) |
| 149 | { |
| 150 | initCacheByLabelcxx . . $ modelCacheIan#initCacheByLabel(fbc0cf1968348b5b). ( heap, p ); |
| 151 | initRolesFromArcscxx . . $ modelCacheIan#initRolesFromArcs(43c45bc7ff600176).(p); |
| 152 | } |
| 153 | /// empty c'tor |
| 154 | modelCacheIancxx . . $ modelCacheIan#modelCacheIan(459becc0a76c23b8). ( bool flagNominals, unsigned int nC, unsigned int nR ) |
| 155 | : modelCacheInterfacecxx . . $ modelCacheInterface#(flagNominals) |
| 156 | , posDConceptscxx . . $ TSetAsTree#TSetAsTree(cb2f890c2fdad230).(nC) |
| 157 | , posNConceptscxx . . $ modelCacheIan#posNConcepts.(nC) |
| 158 | , negDConceptscxx . . $ modelCacheIan#negDConcepts.(nC) |
| 159 | , negNConceptscxx . . $ modelCacheIan#negNConcepts.(nC) |
| 160 | # ifdef RKG_USE_SIMPLE_RULES |
| 161 | , extraDConcepts(nC) |
| 162 | , extraNConcepts(nC) |
| 163 | # endif |
| 164 | , existsRolescxx . . $ TSetAsTree#TSetAsTree(cb2f890c2fdad230).(nR) |
| 165 | , forallRolescxx . . $ TSetAsTree#TSetAsTree(cb2f890c2fdad230).(nR) |
| 166 | , funcRolescxx . . $ modelCacheIan#funcRoles.(nR) |
| 167 | {} |
| 168 | /// copy c'tor |
| 169 | modelCacheIancxx . . $ modelCacheIan#modelCacheIan(5f887466150fe9c4). ( const modelCacheIancxx . . $ modelCacheIan#& m ) |
| 170 | : modelCacheInterfacecxx . . $ modelCacheInterface#(m.hasNominalNodecxx . . $ modelCacheInterface#hasNominalNode.) |
| 171 | , posDConceptscxx . . $ modelCacheIan#posDConcepts.(m.posDConceptscxx . . $ modelCacheIan#posDConcepts.) |
| 172 | , posNConceptscxx . . $ modelCacheIan#posNConcepts.(m.posNConceptscxx . . $ modelCacheIan#posNConcepts.) |
| 173 | , negDConceptscxx . . $ modelCacheIan#negDConcepts.(m.negDConceptscxx . . $ modelCacheIan#negDConcepts.) |
| 174 | , negNConceptscxx . . $ modelCacheIan#negNConcepts.(m.negNConceptscxx . . $ modelCacheIan#negNConcepts.) |
| 175 | # ifdef RKG_USE_SIMPLE_RULES |
| 176 | , extraDConcepts(m.extraDConcepts) |
| 177 | , extraNConcepts(m.extraNConcepts) |
| 178 | # endif |
| 179 | , existsRolescxx . . $ TSetAsTree#TSetAsTree(5d667e7359765dc3).(m.existsRolescxx . . $ modelCacheIan#existsRoles.) |
| 180 | , forallRolescxx . . $ TSetAsTree#TSetAsTree(5d667e7359765dc3).(m.forallRolescxx . . $ modelCacheIan#forallRoles.) |
| 181 | , funcRolescxx . . $ modelCacheIan#funcRoles.(m.funcRolescxx . . $ modelCacheIan#funcRoles.) |
| 182 | , curStatecxx . . $ modelCacheIan#curState.(m.getStatecxx . . $ modelCacheIan#getState(2c070a27c5715e50).()) |
| 183 | {} |
| 184 | /// create a clone of the given cache |
| 185 | modelCacheIancxx . . $ modelCacheIan#* clonecxx . . $ modelCacheIan#clone(173124799b59559d). ( void ) const { return new modelCacheIancxx . . $ modelCacheIan#(*this); } |
| 186 | |
| 187 | /** Check the internal state of the model cache. The check is very fast. |
| 188 | Does NOT return csUnknown |
| 189 | */ |
| 190 | modelCacheStatecxx . . $ modelCacheState# getStatecxx . . $ modelCacheIan#getState(2c070a27c5715e50). ( void ) const override { return curStatecxx . . $ modelCacheIan#curState.; } |
| 191 | |
| 192 | /// init existRoles from arcs; can be used to create pseudo-cache with deps of CT edges |
| 193 | void initRolesFromArcscxx . . $ modelCacheIan#initRolesFromArcs(43c45bc7ff600176). ( const DlCompletionTreecxx . . $ DlCompletionTree#* pCT ) |
| 194 | { |
| 195 | for ( e_iteratorcxx . . $ modelCacheIan#e_iterator# q = pCT->begincxx . . $ DlCompletionTree#begin(d3473f034184ffc9).(), q_end = pCT->endcxx . . $ DlCompletionTree#end(d3473f034184ffc9).(); q <cxx . . $ __gnu_cxx/`operator<`(ef3c5749062e825c). q_end; ++cxx . . $ __gnu_cxx/__normal_iterator#operator++(93f2e9c3bc4efc49).q ) |
| 196 | if ( !(*cxx . . $ __gnu_cxx/__normal_iterator#`operator*`(c076846f881f3e7e).q)->isIBlockedcxx . . $ DlCompletionTreeArc#isIBlocked(50ce9a9e25b4a850).() ) |
| 197 | addExistsRolecxx . . $ modelCacheIan#addExistsRole(67b1ad1f11addbc2). ( (*cxx . . $ __gnu_cxx/__normal_iterator#`operator*`(c076846f881f3e7e).q)->getRolecxx . . $ DlCompletionTreeArc#getRole(69f26110a94b259f).() ); |
| 198 | |
| 199 | curStatecxx . . $ modelCacheIan#curState. = csValidcxx . . $ csValid.; |
| 200 | } |
| 201 | /// clear the cache |
| 202 | void clearcxx . . $ modelCacheIan#clear(49f6e7a06ebc5aa8). ( void ); |
| 203 | |
| 204 | /// check whether two caches can be merged; @return state of "merged" model |
| 205 | modelCacheStatecxx . . $ modelCacheState# canMergecxx . . $ modelCacheIan#canMerge(be618e219bff59e1). ( const modelCacheInterfacecxx . . $ modelCacheInterface#* cache ) const override; |
| 206 | /// Merge given model to current one; return state of the merged model |
| 207 | modelCacheStatecxx . . $ modelCacheState# mergecxx . . $ modelCacheIan#merge(3c316d00313db7eb). ( const modelCacheInterfacecxx . . $ modelCacheInterface#* cache ); |
| 208 | |
| 209 | /// get type of cache (deep or shallow) |
| 210 | bool shallowCachecxx . . $ modelCacheIan#shallowCache(50ce9a9e25b4a850). ( void ) const override { return existsRolescxx . . $ modelCacheIan#existsRoles..emptycxx . . $ TSetAsTree#empty(50ce9a9e25b4a850).(); } |
| 211 | |
| 212 | //---------------------------------------------- |
| 213 | //-- save/load interface; implementation in SaveLoad.cpp |
| 214 | //---------------------------------------------- |
| 215 | |
| 216 | /// save internal state of the Kernel to a file NAME |
| 217 | void Savecxx . . $ modelCacheIan#Save(aaac402dd6f8bcf7). ( SaveLoadManagercxx . . $ SaveLoadManager#& m ) const; |
| 218 | /// load internal state of the Kernel from a file NAME |
| 219 | void Loadcxx . . $ modelCacheIan#Load(da67b8e43f9c9b96). ( SaveLoadManagercxx . . $ SaveLoadManager#& m ); |
| 220 | }; // modelCacheIan |
| 221 | |
| 222 | #endif |