| 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 | #include "Reasoner.h" |
| 21 | |
| 22 | // uncomment the following line to debug cascaded caching |
| 23 | //#define TMP_CACHE_DEBUG |
| 24 | |
| 25 | const modelCacheInterface* |
| 26 | DlSatTester :: createCache ( BipolarPointer p ) |
| 27 | { |
| 28 | fpp_assert ( isValid(p) ); // safety check |
| 29 | |
| 30 | const modelCacheInterface* cache; |
| 31 | |
| 32 | // check if cache already calculated |
| 33 | if ( (cache = DLHeap.getCache(p)) != nullptr ) |
| 34 | return cache; |
| 35 | |
| 36 | #ifdef TMP_CACHE_DEBUG |
| 37 | std::cerr << "\nCCache for " << p << ":"; |
| 38 | #endif |
| 39 | if ( !unlikely(tBox.testHasTopRole()) ) |
| 40 | prepareCascadedCache(p); |
| 41 | |
| 42 | // it may be a cycle and the cache for p is already calculated |
| 43 | if ( (cache = DLHeap.getCache(p)) != nullptr ) |
| 44 | return cache; |
| 45 | |
| 46 | // need to build cache |
| 47 | DLHeap.setCache ( p, buildCache(p) ); |
| 48 | return DLHeap.getCache(p); |
| 49 | } |
| 50 | |
| 51 | void |
| 52 | DlSatTester :: prepareCascadedCache ( BipolarPointer p ) |
| 53 | { |
| 54 | /// cycle found -- shall be processed without caching |
| 55 | if ( inProcess.find(p) != inProcess.end() ) |
| 56 | { |
| 57 | # ifdef TMP_CACHE_DEBUG |
| 58 | std::cerr << " cycle with " << p << ";"; |
| 59 | # endif |
| 60 | return; |
| 61 | } |
| 62 | |
| 63 | const DLVertex& v = DLHeap[p]; |
| 64 | bool pos = isPositive(p); |
| 65 | |
| 66 | // check if a concept already cached |
| 67 | if ( v.getCache(pos) != nullptr ) |
| 68 | return; |
| 69 | |
| 70 | switch ( v.Type() ) |
| 71 | { |
| 72 | case dtTop: // no need to put cache for this |
| 73 | break; |
| 74 | |
| 75 | case dtDataType: // data things are checked by data reasoner |
| 76 | case dtDataValue: |
| 77 | case dtDataExpr: |
| 78 | break; |
| 79 | |
| 80 | case dtAnd: |
| 81 | { |
| 82 | for ( DLVertex::const_iterator q = v.begin(), q_end = v.end(); q < q_end; ++q ) |
| 83 | prepareCascadedCache(createBiPointer(*q,pos)); |
| 84 | break; |
| 85 | } |
| 86 | |
| 87 | case dtPSingleton: |
| 88 | case dtNSingleton: |
| 89 | case dtNConcept: |
| 90 | case dtPConcept: |
| 91 | if ( isNegative(p) && isPNameTag(v.Type()) ) |
| 92 | return; |
| 93 | inProcess.insert(p); |
| 94 | # ifdef TMP_CACHE_DEBUG |
| 95 | std::cerr << " expanding " << p << ";"; |
| 96 | # endif |
| 97 | prepareCascadedCache(createBiPointer(v.getC(),pos)); |
| 98 | inProcess.erase(p); |
| 99 | break; |
| 100 | |
| 101 | case dtForall: |
| 102 | case dtLE: |
| 103 | { |
| 104 | const TRole* R = v.getRole(); |
| 105 | if ( R->isDataRole() ) // skip data-related stuff |
| 106 | break; |
| 107 | if ( unlikely(R->isTop()) ) // no need to cache top-role stuff |
| 108 | break; |
| 109 | BipolarPointer x = createBiPointer(v.getC(),pos); |
| 110 | |
| 111 | // build cache for C in \AR.C |
| 112 | if ( x != bpTOP ) |
| 113 | { |
| 114 | inProcess.insert(x); |
| 115 | # ifdef TMP_CACHE_DEBUG |
| 116 | std::cerr << " caching " << x << ";"; |
| 117 | # endif |
| 118 | createCache(x); |
| 119 | inProcess.erase(x); |
| 120 | } |
| 121 | |
| 122 | // build cache for the Range(R) if necessary |
| 123 | x = R->getBPRange(); |
| 124 | if ( x != bpTOP ) |
| 125 | { |
| 126 | inProcess.insert(x); |
| 127 | # ifdef TMP_CACHE_DEBUG |
| 128 | std::cerr << " caching range(" << v.getRole()->getName() << ") = " << x << ";"; |
| 129 | # endif |
| 130 | createCache(x); |
| 131 | inProcess.erase(x); |
| 132 | } |
| 133 | |
| 134 | break; |
| 135 | } |
| 136 | |
| 137 | case dtIrr: |
| 138 | break; |
| 139 | |
| 140 | default: |
| 141 | fpp_unreachable(); |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | /// build cache for given DAG node using SAT tests; @return cache |
| 146 | modelCacheInterface* |
| 147 | DlSatTester :: buildCache ( BipolarPointer p ) |
| 148 | { |
| 149 | if ( LLM.isWritable(llDagSat) ) |
| 150 | { |
| 151 | LL << "\nChecking satisfiability of DAG entry " << p; |
| 152 | tBox.PrintDagEntry(LL,p); |
| 153 | LL << ":\n"; |
| 154 | } |
| 155 | |
| 156 | #ifdef TMP_CACHE_DEBUG |
| 157 | std::cerr << " building cache for " << p << "..."; |
| 158 | #endif |
| 159 | |
| 160 | bool sat = runSat(p); |
| 161 | |
| 162 | #ifdef TMP_CACHE_DEBUG |
| 163 | std::cerr << " done"; |
| 164 | #endif |
| 165 | |
| 166 | // unsat => P -> \bot |
| 167 | if ( !sat && LLM.isWritable(llAlways) ) |
| 168 | LL << "\nDAG entry " << p << " is unsatisfiable\n"; |
| 169 | |
| 170 | return buildCacheByCGraph(sat); |
| 171 | } |
| 172 |