| 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 DLDAG_H |
| 21 | #define DLDAG_H |
| 22 | |
| 23 | #include <vector> |
| 24 | #include <cstring> // strlen |
| 25 | |
| 26 | #include "globaldef.h" // for statistic printed |
| 27 | #include "fpp_assert.h" |
| 28 | #include "dlVertex.h" |
| 29 | #include "dlVHash.h" |
| 30 | #include "ifOptions.h" |
| 31 | #include "tRole.h" |
| 32 | #include "ConceptWithDep.h" |
| 33 | #include "tNECollection.h" |
| 34 | |
| 35 | class RoleMaster; |
| 36 | class TConcept; |
| 37 | |
| 38 | /** DAG of DL Vertices used in FaCT++ reasoner */ |
| 39 | class DLDag |
| 40 | { |
| 41 | public: // types |
| 42 | typedef std::vector<DLVertex*> HeapType; |
| 43 | typedef std::vector<BipolarPointer> StatVector; |
| 44 | /// typedef for the hash-table |
| 45 | typedef dlVHashTable HashTable; |
| 46 | |
| 47 | protected: // members |
| 48 | /// body of DAG |
| 49 | HeapType Heap; |
| 50 | /// all the AND nodes (needs to recompute) |
| 51 | StatVector listAnds; |
| 52 | /// hash-table for vertices (and, all, LE) fast search |
| 53 | HashTable indexAnd, indexAll, indexLE; |
| 54 | |
| 55 | /// DAG size after the whole ontology is loaded |
| 56 | size_t finalDagSize = 0; |
| 57 | /// cache efficiency -- statistic purposes |
| 58 | unsigned int nCacheHits = 0; |
| 59 | |
| 60 | #ifdef RKG_USE_SORTED_REASONING |
| 61 | /// size of sort array |
| 62 | size_t sortArraySize = 0; |
| 63 | #endif |
| 64 | |
| 65 | // tunable flags (set by readConfig) |
| 66 | |
| 67 | /// sort strings: option[0] for SAT/cache tests, option[1] for SUB/classify tests |
| 68 | const char* orSortSat = nullptr; |
| 69 | const char* orSortSub = nullptr; |
| 70 | /// sort index (if necessary). Possible values are Size, Depth, Freq |
| 71 | unsigned int iSort = 0; |
| 72 | /// whether or not sorting order is ascending |
| 73 | bool sortAscend = false; |
| 74 | /// prefer non-generating rules in OR orderings |
| 75 | bool preferNonGen = false; |
| 76 | |
| 77 | /// flag whether cache should be used |
| 78 | bool useDLVCache = true; |
| 79 | |
| 80 | protected: // methods |
| 81 | /// setup flags by given option set |
| 82 | void readConfig ( const ifOptionSet* Options ); |
| 83 | /// check if given string is correct sort ordering representation |
| 84 | bool isCorrectOption ( const char* str ) |
| 85 | { |
| 86 | if ( str == nullptr ) |
| 87 | return false; |
| 88 | size_t n = strlen(str); |
| 89 | if ( n < 1 || n > 3 ) |
| 90 | return false; |
| 91 | char Method = str[0], |
| 92 | Order = n >= 2 ? str[1] : 'a', |
| 93 | NGPref = n == 3 ? str[2] : 'p'; |
| 94 | return ( Method == 'S' || Method == 'D' || Method == 'F' || |
| 95 | Method == 'B' || Method == 'G' || Method == '0' ) |
| 96 | && ( Order == 'a' || Order == 'd' ) && ( NGPref == 'p' || NGPref == 'n' ); |
| 97 | } |
| 98 | /// gather vertex statistics (no freq) |
| 99 | void computeVertexStat ( BipolarPointer p ); |
| 100 | /// helper for the recursion |
| 101 | void computeVertexStat ( BipolarPointer p, bool pos ) { computeVertexStat ( createBiPointer ( p, pos ) ); } |
| 102 | /// update vertex statistics (no freq) wrt calculated values of children |
| 103 | void updateVertexStat ( BipolarPointer p ); |
| 104 | /// helper for the recursion |
| 105 | void updateVertexStat ( DLVertex& v, BipolarPointer p, bool pos ) |
| 106 | { |
| 107 | const DLVertex& w = (*this)[p]; |
| 108 | bool posW = pos == isPositive(p); |
| 109 | |
| 110 | // update in-cycle information |
| 111 | if ( w.isInCycle(posW) ) |
| 112 | v.setInCycle(pos); |
| 113 | |
| 114 | v.updateStatValues ( w, posW, pos ); |
| 115 | } |
| 116 | /// gather vertex freq statistics |
| 117 | void computeVertexFreq ( BipolarPointer p ); |
| 118 | /// helper for the recursion |
| 119 | void computeVertexFreq ( BipolarPointer p, bool pos ) { computeVertexFreq ( createBiPointer ( p, pos ) ); } |
| 120 | /// change order of ADD elements wrt statistic |
| 121 | void Recompute ( void ) |
| 122 | { |
| 123 | for ( StatVector::const_iterator p = listAnds.begin(), p_end = listAnds.end(); p < p_end; ++p ) |
| 124 | (*this)[*p].sortEntry(*this); |
| 125 | } |
| 126 | /// set OR sort flags based on given option string; Recompute if necessary |
| 127 | void setOrderOptions ( const char* opt ); |
| 128 | /// clear all DFS info from elements of DAG |
| 129 | void clearDFS ( void ) |
| 130 | { |
| 131 | for ( size_t i = Heap.size()-1; i > 0; --i ) |
| 132 | Heap[i]->clearDFS(); |
| 133 | } |
| 134 | |
| 135 | /// get index corresponding to DLVertex's tag |
| 136 | const HashTable& getIndex ( DagTag tag ) const |
| 137 | { |
| 138 | switch(tag) |
| 139 | { |
| 140 | case dtAnd: return indexAnd; |
| 141 | case dtIrr: |
| 142 | case dtForall: return indexAll; |
| 143 | case dtLE: return indexLE; |
| 144 | default: fpp_unreachable(); // no such index |
| 145 | } |
| 146 | } |
| 147 | /// update index corresponding to DLVertex's tag |
| 148 | void updateIndex ( DagTag tag, BipolarPointer value ) |
| 149 | { |
| 150 | switch(tag) |
| 151 | { |
| 152 | case dtAnd: indexAnd.addElement(value); listAnds.push_back(value); break; |
| 153 | case dtIrr: |
| 154 | case dtForall: indexAll.addElement(value); break; |
| 155 | case dtLE: indexLE.addElement(value); break; |
| 156 | default: break; // nothing to do |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | #ifdef RKG_USE_SORTED_REASONING |
| 161 | // internal sort interface |
| 162 | |
| 163 | /// merge sorts for a given role |
| 164 | void mergeSorts ( TRole* R ); |
| 165 | /// merge sorts for a given vertex |
| 166 | void mergeSorts ( DLVertex& v ); |
| 167 | #endif |
| 168 | |
| 169 | #ifdef RKG_PRINT_DAG_USAGE |
| 170 | /// print usage of DAG |
| 171 | void PrintDAGUsage ( std::ostream& o ) const; |
| 172 | #endif |
| 173 | |
| 174 | public: // interface |
| 175 | /// the only c'tor |
| 176 | explicit DLDag ( const ifOptionSet* Options ); |
| 177 | /// no copy c'tor |
| 178 | DLDag ( const DLDag& ) = delete; |
| 179 | /// no assignment |
| 180 | DLDag& operator= ( const DLDag& ) = delete; |
| 181 | /// d'tor |
| 182 | ~DLDag(); |
| 183 | |
| 184 | // construction methods |
| 185 | |
| 186 | /// get index of given vertex; include vertex to DAG if necessary |
| 187 | BipolarPointer add ( DLVertex* v ); |
| 188 | /// add vertex to the end of DAG and calculate it's statistic if necessary |
| 189 | BipolarPointer directAdd ( DLVertex* v ) |
| 190 | { |
| 191 | BipolarPointer toReturn = BipolarPointer(Heap.size()); |
| 192 | Heap.push_back(v); |
| 193 | // return an index of just added entry |
| 194 | return toReturn; |
| 195 | } |
| 196 | /// add vertex to the end of DAG and calculate it's statistic if necessary; put it into cache |
| 197 | BipolarPointer directAddAndCache ( DLVertex* v ) |
| 198 | { |
| 199 | BipolarPointer ret = directAdd(v); |
| 200 | if ( useDLVCache ) |
| 201 | updateIndex ( v->Type(), ret ); |
| 202 | return ret; |
| 203 | } |
| 204 | /// check if given index points to the last DAG entry |
| 205 | bool isLast ( BipolarPointer p ) const { return (getValue(p) == Heap.size()-1); } |
| 206 | |
| 207 | // access methods |
| 208 | |
| 209 | /// whether to use cache for nodes |
| 210 | void setExpressionCache ( bool val ) { useDLVCache = val; } |
| 211 | /// return true if p1 is less than p2 using chosen sort order |
| 212 | bool less ( BipolarPointer p1, BipolarPointer p2 ) const; |
| 213 | |
| 214 | /// access by index (non-const version) |
| 215 | DLVertex& operator [] ( BipolarPointer i ) |
| 216 | { |
| 217 | # ifdef ENABLE_CHECKING |
| 218 | fpp_assert ( isValid(i) ); |
| 219 | fpp_assert ( getValue(i) < size() ); |
| 220 | # endif |
| 221 | return *Heap[getValue(i)]; |
| 222 | } |
| 223 | /// RW access by index in complex concept |
| 224 | DLVertex& operator [] ( const ConceptWDep& cwd ) { return (*this)[cwd.bp()]; } |
| 225 | /// access by index (const version) |
| 226 | const DLVertex& operator [] ( BipolarPointer i ) const |
| 227 | { |
| 228 | # ifdef ENABLE_CHECKING |
| 229 | fpp_assert ( isValid(i) ); |
| 230 | fpp_assert ( getValue(i) < size() ); |
| 231 | # endif |
| 232 | return *Heap[getValue(i)]; |
| 233 | } |
| 234 | /// RO access by index in complex concept |
| 235 | const DLVertex& operator [] ( const ConceptWDep& cwd ) const { return (*this)[cwd.bp()]; } |
| 236 | /// replace existing vertex at index I with a vertex V |
| 237 | void replaceVertex ( BipolarPointer i, DLVertex* v, TNamedEntry* C ) |
| 238 | { |
| 239 | delete Heap[getValue(i)]; |
| 240 | Heap[getValue(i)] = v; |
| 241 | v->setConcept(C); |
| 242 | } |
| 243 | |
| 244 | /// get size of DAG |
| 245 | size_t size ( void ) const { return Heap.size (); } |
| 246 | /// get approximation of the size after query is added |
| 247 | size_t maxSize ( void ) const { return size() + ( size() < 220 ? 10 : size()/20 ); } |
| 248 | /// set the final DAG size |
| 249 | void setFinalSize ( void ) { finalDagSize = size(); setExpressionCache(false); } |
| 250 | /// resize DAG to its original size (to clear intermediate query) |
| 251 | void removeQuery ( void ); |
| 252 | |
| 253 | // option interface |
| 254 | |
| 255 | /// set defaults of OR orderings |
| 256 | void setOrderDefaults ( const char* defSat, const char* defSub ); |
| 257 | /// use SUB options to OR ordering |
| 258 | void setSubOrder ( void ) { setOrderOptions(orSortSub); } |
| 259 | /// use SAT options to OR ordering; |
| 260 | void setSatOrder ( void ) { setOrderOptions(orSortSat); } |
| 261 | /// gather statistics necessary for the OR ordering |
| 262 | void gatherStatistic ( void ); |
| 263 | |
| 264 | // cache interface |
| 265 | |
| 266 | /// get cache for given BiPointer (may return NULL if no cache defined) |
| 267 | const modelCacheInterface* getCache ( BipolarPointer p ) const |
| 268 | { return operator[](p).getCache(isPositive(p)); } |
| 269 | /// set cache for given BiPointer; @return given cache |
| 270 | void setCache ( BipolarPointer p, const modelCacheInterface* cache ) |
| 271 | { operator[](p).setCache ( isPositive(p), cache ); } |
| 272 | |
| 273 | // sort interface |
| 274 | |
| 275 | #ifdef RKG_USE_SORTED_REASONING |
| 276 | /// merge two given DAG entries |
| 277 | void merge ( mergeableLabel& ml, BipolarPointer p ) |
| 278 | { |
| 279 | if ( p != bpINVALID && p != bpTOP && p != bpBOTTOM ) |
| 280 | (*this)[p].merge(ml); |
| 281 | } |
| 282 | /// build the sort system for given TBox |
| 283 | void determineSorts ( RoleMaster& ORM, RoleMaster& DRM ); |
| 284 | /// update sorts for <a,b>:R construction |
| 285 | void updateSorts ( BipolarPointer a, TRole* R, BipolarPointer b ) |
| 286 | { |
| 287 | merge ( R->getDomainLabel(), a ); |
| 288 | merge ( R->getRangeLabel(), b ); |
| 289 | } |
| 290 | /// check if two BPs are of the same sort |
| 291 | bool haveSameSort ( BipolarPointer p, BipolarPointer q ) const |
| 292 | { |
| 293 | fpp_assert ( p > 0 && q > 0 ); // sanity check |
| 294 | |
| 295 | // everything has the same label as TOP |
| 296 | if ( unlikely(p == 1 || q == 1) ) |
| 297 | return true; |
| 298 | |
| 299 | // if some concepts were added to DAG => nothing to say |
| 300 | if ( unlikely(getValue(p) >= sortArraySize || getValue(q) >= sortArraySize) ) |
| 301 | return true; |
| 302 | |
| 303 | // check whether two sorts are identical |
| 304 | return (*this)[p].getSort() == (*this)[q].getSort(); |
| 305 | } |
| 306 | #else |
| 307 | bool haveSameSort ( unsigned int p, unsigned int q ) { return true; } |
| 308 | #endif |
| 309 | |
| 310 | // output interface |
| 311 | |
| 312 | /// print DAG size and number of cache hits, together with DAG usage |
| 313 | void PrintStat ( std::ostream& o ) const |
| 314 | { |
| 315 | o << "Heap size = " << Heap.size () << " nodes\n" |
| 316 | << "There were " << nCacheHits << " cache hits\n"; |
| 317 | # ifdef RKG_PRINT_DAG_USAGE |
| 318 | PrintDAGUsage(o); |
| 319 | # endif |
| 320 | } |
| 321 | /// print the whole DAG |
| 322 | void Print ( std::ostream& o ) const |
| 323 | { |
| 324 | o << "\nDag structure"; |
| 325 | |
| 326 | for ( size_t i = 1; i < size(); ++i ) |
| 327 | { |
| 328 | o << "\n" << i << " "; |
| 329 | Heap[i]->Print(o); |
| 330 | } |
| 331 | |
| 332 | o << std::endl; |
| 333 | } |
| 334 | }; // DLDag |
| 335 | |
| 336 | #include "dlVHashImpl.h" |
| 337 | |
| 338 | inline BipolarPointer |
| 339 | DLDag :: add ( DLVertex* v ) |
| 340 | { |
| 341 | BipolarPointer ret = useDLVCache ? getIndex(v->Type()).locate(*v) : bpINVALID; |
| 342 | |
| 343 | if ( !isValid(ret) ) // we fail to find such vertex -- it's new |
| 344 | return directAddAndCache(v); |
| 345 | |
| 346 | // node was found in cache |
| 347 | ++nCacheHits; |
| 348 | delete v; |
| 349 | return ret; |
| 350 | } |
| 351 | |
| 352 | #endif |