| 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_H |
| 22 | |
| 23 | #include "modelCacheSingleton.h" |
| 24 | #include "dlCompletionTree.h" |
| 25 | #include "dlDag.h" |
| 26 | #include "tSetAsTree.h" |
| 27 | |
| 28 | class SaveLoadManager; |
| 29 | |
| 30 | /** model caching implementation of Ian's algorithms. |
| 31 | Very fast cache check, but less precise than AsTree one. |
| 32 | */ |
| 33 | class modelCacheIan: public modelCacheInterface |
| 34 | { |
| 35 | // FIXME!! for now only |
| 36 | friend class DLConceptTaxonomy; |
| 37 | protected: // types |
| 38 | /// define the type of an index set |
| 39 | typedef TSetAsTree IndexSet; |
| 40 | /// node label iterator |
| 41 | typedef DlCompletionTree::const_label_iterator l_iterator; |
| 42 | /// edges iterator |
| 43 | typedef DlCompletionTree::const_edge_iterator 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 | IndexSet posDConcepts; |
| 50 | /// named concepts that appears positively non-det in a root node of a cache |
| 51 | IndexSet posNConcepts; |
| 52 | /// named concepts that appears negatively det-lly in a root node of a cache |
| 53 | IndexSet negDConcepts; |
| 54 | /// named concepts that appears negatively non-det in a root node of a cache |
| 55 | IndexSet 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 | IndexSet existsRoles; |
| 64 | /// role names that appears in the \A restrictions in the root node |
| 65 | IndexSet forallRoles; |
| 66 | /// role names that appears in the atmost restrictions in the root node |
| 67 | IndexSet funcRoles; |
| 68 | |
| 69 | /// current state of cache model; recalculates on every change |
| 70 | modelCacheState curState = csValid; |
| 71 | |
| 72 | protected: // methods |
| 73 | /// log a particular implementation of a cache entry |
| 74 | void logCacheEntryImpl ( void ) const override; |
| 75 | /// add single concept from label to cache |
| 76 | void processConcept ( const DLVertex& cur, bool pos, bool det ); |
| 77 | /// add all roles that are accepted by an automaton from a given entry |
| 78 | void processAutomaton ( const DLVertex& cur ); |
| 79 | /// process CT label in given interval; set Deterministic accordingly |
| 80 | void processLabelInterval ( const DLDag& DLHeap, l_iterator start, l_iterator end ) |
| 81 | { |
| 82 | for ( l_iterator p = start; p != end; ++p ) |
| 83 | processConcept ( DLHeap[*p], isPositive(p->bp()), p->getDep().empty() ); |
| 84 | } |
| 85 | /// fills cache sets by tree->Label; set Deterministic accordingly |
| 86 | void initCacheByLabel ( const DLDag& DLHeap, const DlCompletionTree* pCT ) |
| 87 | { |
| 88 | processLabelInterval ( DLHeap, pCT->beginl_sc(), pCT->endl_sc() ); |
| 89 | processLabelInterval ( DLHeap, pCT->beginl_cc(), pCT->endl_cc() ); |
| 90 | } |
| 91 | /// adds role to exists- and func-role if necessary |
| 92 | void addRoleToCache ( const TRole* R ) |
| 93 | { |
| 94 | existsRoles.insert(R->index()); |
| 95 | if ( R->isTopFunc() ) // all other top-funcs would be added separately |
| 96 | funcRoles.insert(R->index()); |
| 97 | } |
| 98 | /// adds role (and all its super-roles) to exists- and funcRoles |
| 99 | void addExistsRole ( const TRole* R ) |
| 100 | { |
| 101 | addRoleToCache(R); |
| 102 | for ( auto sup: R->ancestors() ) |
| 103 | addRoleToCache(sup); |
| 104 | } |
| 105 | |
| 106 | // access to the arrays |
| 107 | |
| 108 | /// get RW access to D-concepts wrt polarity |
| 109 | IndexSet& getDConcepts ( bool pos ) { return pos ? posDConcepts : negDConcepts; } |
| 110 | /// get RW access to N-concepts wrt polarity |
| 111 | IndexSet& getNConcepts ( bool pos ) { return pos ? posNConcepts : negNConcepts; } |
| 112 | /// get RO access to D-concepts wrt polarity |
| 113 | const IndexSet& getDConcepts ( bool pos ) const { return pos ? posDConcepts : negDConcepts; } |
| 114 | /// get RO access to N-concepts wrt polarity |
| 115 | const IndexSet& getNConcepts ( bool pos ) const { return pos ? posNConcepts : 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 | modelCacheState isMergeableSingleton ( BipolarPointer bp ) const; |
| 127 | /// implementation of merging with Ian's cache type |
| 128 | modelCacheState isMergeableIan ( const modelCacheIan* p ) const; |
| 129 | /// actual merge with a singleton cache |
| 130 | void mergeSingleton ( BipolarPointer bp ); |
| 131 | /// actual merge with an Ian's cache |
| 132 | void mergeIan ( const modelCacheIan* p ); |
| 133 | |
| 134 | public: |
| 135 | /// Create cache model of given CompletionTree using given HEAP |
| 136 | modelCacheIan ( const DLDag& heap, const DlCompletionTree* p, bool flagNominals, unsigned int nC, unsigned int nR ) |
| 137 | : modelCacheInterface(flagNominals) |
| 138 | , posDConcepts(nC) |
| 139 | , posNConcepts(nC) |
| 140 | , negDConcepts(nC) |
| 141 | , negNConcepts(nC) |
| 142 | # ifdef RKG_USE_SIMPLE_RULES |
| 143 | , extraDConcepts(nC) |
| 144 | , extraNConcepts(nC) |
| 145 | # endif |
| 146 | , existsRoles(nR) |
| 147 | , forallRoles(nR) |
| 148 | , funcRoles(nR) |
| 149 | { |
| 150 | initCacheByLabel ( heap, p ); |
| 151 | initRolesFromArcs(p); |
| 152 | } |
| 153 | /// empty c'tor |
| 154 | modelCacheIan ( bool flagNominals, unsigned int nC, unsigned int nR ) |
| 155 | : modelCacheInterface(flagNominals) |
| 156 | , posDConcepts(nC) |
| 157 | , posNConcepts(nC) |
| 158 | , negDConcepts(nC) |
| 159 | , negNConcepts(nC) |
| 160 | # ifdef RKG_USE_SIMPLE_RULES |
| 161 | , extraDConcepts(nC) |
| 162 | , extraNConcepts(nC) |
| 163 | # endif |
| 164 | , existsRoles(nR) |
| 165 | , forallRoles(nR) |
| 166 | , funcRoles(nR) |
| 167 | {} |
| 168 | /// copy c'tor |
| 169 | modelCacheIan ( const modelCacheIan& m ) |
| 170 | : modelCacheInterface(m.hasNominalNode) |
| 171 | , posDConcepts(m.posDConcepts) |
| 172 | , posNConcepts(m.posNConcepts) |
| 173 | , negDConcepts(m.negDConcepts) |
| 174 | , negNConcepts(m.negNConcepts) |
| 175 | # ifdef RKG_USE_SIMPLE_RULES |
| 176 | , extraDConcepts(m.extraDConcepts) |
| 177 | , extraNConcepts(m.extraNConcepts) |
| 178 | # endif |
| 179 | , existsRoles(m.existsRoles) |
| 180 | , forallRoles(m.forallRoles) |
| 181 | , funcRoles(m.funcRoles) |
| 182 | , curState(m.getState()) |
| 183 | {} |
| 184 | /// create a clone of the given cache |
| 185 | modelCacheIan* clone ( void ) const { return new modelCacheIan(*this); } |
| 186 | |
| 187 | /** Check the internal state of the model cache. The check is very fast. |
| 188 | Does NOT return csUnknown |
| 189 | */ |
| 190 | modelCacheState getState ( void ) const override { return curState; } |
| 191 | |
| 192 | /// init existRoles from arcs; can be used to create pseudo-cache with deps of CT edges |
| 193 | void initRolesFromArcs ( const DlCompletionTree* pCT ) |
| 194 | { |
| 195 | for ( e_iterator q = pCT->begin(), q_end = pCT->end(); q < q_end; ++q ) |
| 196 | if ( !(*q)->isIBlocked() ) |
| 197 | addExistsRole ( (*q)->getRole() ); |
| 198 | |
| 199 | curState = csValid; |
| 200 | } |
| 201 | /// clear the cache |
| 202 | void clear ( void ); |
| 203 | |
| 204 | /// check whether two caches can be merged; @return state of "merged" model |
| 205 | modelCacheState canMerge ( const modelCacheInterface* cache ) const override; |
| 206 | /// Merge given model to current one; return state of the merged model |
| 207 | modelCacheState merge ( const modelCacheInterface* cache ); |
| 208 | |
| 209 | /// get type of cache (deep or shallow) |
| 210 | bool shallowCache ( void ) const override { return existsRoles.empty(); } |
| 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 Save ( SaveLoadManager& m ) const; |
| 218 | /// load internal state of the Kernel from a file NAME |
| 219 | void Load ( SaveLoadManager& m ); |
| 220 | }; // modelCacheIan |
| 221 | |
| 222 | #endif |