| 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"cxx . . $ `<file>/Kernel/Reasoner.h`/ |
| 21 | |
| 22 | // uncomment the following line to debug cascaded caching |
| 23 | //#define TMP_CACHE_DEBUG |
| 24 | |
| 25 | const modelCacheInterfacecxx . . $ modelCacheInterface#* |
| 26 | DlSatTestercxx . . $ DlSatTester# :: createCachecxx . . $ DlSatTester#createCache(b3d3cd5834c92364). ( BipolarPointercxx . . $ BipolarPointer# p ) |
| 27 | { |
| 28 | fpp_assertcxx . . $ `Kernel/fpp_assert.h:46:10`! ( isValid(p) ); // safety check |
| 29 | |
| 30 | const modelCacheInterfacecxx . . $ modelCacheInterface#* cache; |
| 31 | |
| 32 | // check if cache already calculated |
| 33 | if ( (cache = DLHeapcxx . . $ DlSatTester#DLHeap..getCachecxx . . $ DLDag#getCache(a400da756ae1652b).(p)) != nullptr ) |
| 34 | return cache; |
| 35 | |
| 36 | #ifdef TMP_CACHE_DEBUG |
| 37 | std::cerr << "\nCCache for " << p << ":"; |
| 38 | #endif |
| 39 | if ( !unlikelycxx . . $ `Kernel/globaldef.h:29:10`!(tBox.testHasTopRole()) ) |
| 40 | prepareCascadedCachecxx . . $ DlSatTester#prepareCascadedCache(d4f767463ce0a6b3).(p); |
| 41 | |
| 42 | // it may be a cycle and the cache for p is already calculated |
| 43 | if ( (cache = DLHeapcxx . . $ DlSatTester#DLHeap..getCachecxx . . $ DLDag#getCache(a400da756ae1652b).(p)) != nullptr ) |
| 44 | return cache; |
| 45 | |
| 46 | // need to build cache |
| 47 | DLHeapcxx . . $ DlSatTester#DLHeap..setCachecxx . . $ DLDag#setCache(5d5e965ff81adf26). ( p, buildCachecxx . . $ DlSatTester#buildCache(f17fff7e095f06bd).(p) ); |
| 48 | return DLHeapcxx . . $ DlSatTester#DLHeap..getCachecxx . . $ DLDag#getCache(a400da756ae1652b).(p); |
| 49 | } |
| 50 | |
| 51 | void |
| 52 | DlSatTestercxx . . $ DlSatTester# :: prepareCascadedCachecxx . . $ DlSatTester#prepareCascadedCache(d4f767463ce0a6b3). ( BipolarPointercxx . . $ BipolarPointer# p ) |
| 53 | { |
| 54 | /// cycle found -- shall be processed without caching |
| 55 | if ( inProcesscxx . . $ DlSatTester#inProcess..findcxx . . $ std/set#find(f8922f46a681e6cc).(p) !=cxx . . $ std/`operator!=`(7e1308a0738ea758). inProcesscxx . . $ DlSatTester#inProcess..endcxx . . $ std/set#end(a1617ab40ea76896).() ) |
| 56 | { |
| 57 | # ifdef TMP_CACHE_DEBUG |
| 58 | std::cerr << " cycle with " << p << ";"; |
| 59 | # endif |
| 60 | return; |
| 61 | } |
| 62 | |
| 63 | const DLVertexcxx . . $ DLVertex#& v = DLHeapcxx . . $ DlSatTester#DLHeap.[cxx . . $ DLDag#`operator[]`(e265d70853dfed20).p]; |
| 64 | bool pos = isPositivecxx . . $ isPositive(83c267b20bac841a).(p); |
| 65 | |
| 66 | // check if a concept already cached |
| 67 | if ( v.getCachecxx . . $ DLVertexCache#getCache(b56d42e3b093d25c).(pos) != nullptr ) |
| 68 | return; |
| 69 | |
| 70 | switch ( v.Typecxx . . $ DLVertexTagDFS#Type(d9c59f5f195b46b8).() ) |
| 71 | { |
| 72 | case dtTopcxx . . $ dtTop.: // no need to put cache for this |
| 73 | break; |
| 74 | |
| 75 | case dtDataTypecxx . . $ dtDataType.: // data things are checked by data reasoner |
| 76 | case dtDataValuecxx . . $ dtDataValue.: |
| 77 | case dtDataExprcxx . . $ dtDataExpr.: |
| 78 | break; |
| 79 | |
| 80 | case dtAndcxx . . $ dtAnd.: |
| 81 | { |
| 82 | for ( DLVertexcxx . . $ DLVertex#::const_iteratorcxx . . $ DLVertex#const_iterator# q = v.begincxx . . $ DLVertex#begin(d2502ee2ca3b883a).(), q_end = v.endcxx . . $ DLVertex#end(d2502ee2ca3b883a).(); q <cxx . . $ __gnu_cxx/`operator<`(ef3c5749062e825c). q_end; ++cxx . . $ __gnu_cxx/__normal_iterator#operator++(93f2e9c3bc4efc49).q ) |
| 83 | prepareCascadedCachecxx . . $ DlSatTester#prepareCascadedCache(d4f767463ce0a6b3).(createBiPointercxx . . $ createBiPointer(d455ce14f374a3ab).(*cxx . . $ __gnu_cxx/__normal_iterator#`operator*`(c076846f881f3e7e).q,pos)); |
| 84 | break; |
| 85 | } |
| 86 | |
| 87 | case dtPSingletoncxx . . $ dtPSingleton.: |
| 88 | case dtNSingletoncxx . . $ dtNSingleton.: |
| 89 | case dtNConceptcxx . . $ dtNConcept.: |
| 90 | case dtPConceptcxx . . $ dtPConcept.: |
| 91 | if ( isNegativecxx . . $ isNegative(83c267b20bac841a).(p) && isPNameTagcxx . . $ isPNameTag(7b512e7165d9f2af).(v.Typecxx . . $ DLVertexTagDFS#Type(d9c59f5f195b46b8).()) ) |
| 92 | return; |
| 93 | inProcesscxx . . $ DlSatTester#inProcess..insertcxx . . $ std/set#insert(411a10a755068dfe).(p); |
| 94 | # ifdef TMP_CACHE_DEBUG |
| 95 | std::cerr << " expanding " << p << ";"; |
| 96 | # endif |
| 97 | prepareCascadedCachecxx . . $ DlSatTester#prepareCascadedCache(d4f767463ce0a6b3).(createBiPointercxx . . $ createBiPointer(d455ce14f374a3ab).(v.getCcxx . . $ DLVertex#getC(455f465bc33b4cdf).(),pos)); |
| 98 | inProcesscxx . . $ DlSatTester#inProcess..erasecxx . . $ std/set#erase(3a53bfe100edb289).(p); |
| 99 | break; |
| 100 | |
| 101 | case dtForallcxx . . $ dtForall.: |
| 102 | case dtLEcxx . . $ dtLE.: |
| 103 | { |
| 104 | const TRolecxx . . $ TRole#* R = v.getRolecxx . . $ DLVertex#getRole(69f26110a94b259f).(); |
| 105 | if ( R->isDataRolecxx . . $ TRole#isDataRole(50ce9a9e25b4a850).() ) // skip data-related stuff |
| 106 | break; |
| 107 | if ( unlikelycxx . . $ `Kernel/globaldef.h:29:10`!(R->isTop()) ) // no need to cache top-role stuff |
| 108 | break; |
| 109 | BipolarPointercxx . . $ BipolarPointer# x = createBiPointercxx . . $ createBiPointer(d455ce14f374a3ab).(v.getCcxx . . $ DLVertex#getC(455f465bc33b4cdf).(),pos); |
| 110 | |
| 111 | // build cache for C in \AR.C |
| 112 | if ( x != bpTOPcxx . . $ bpTOP. ) |
| 113 | { |
| 114 | inProcesscxx . . $ DlSatTester#inProcess..insertcxx . . $ std/set#insert(411a10a755068dfe).(x); |
| 115 | # ifdef TMP_CACHE_DEBUG |
| 116 | std::cerr << " caching " << x << ";"; |
| 117 | # endif |
| 118 | createCachecxx . . $ DlSatTester#createCache(b3d3cd5834c92364).(x); |
| 119 | inProcesscxx . . $ DlSatTester#inProcess..erasecxx . . $ std/set#erase(3a53bfe100edb289).(x); |
| 120 | } |
| 121 | |
| 122 | // build cache for the Range(R) if necessary |
| 123 | x = R->getBPRangecxx . . $ TRole#getBPRange(455f465bc33b4cdf).(); |
| 124 | if ( x != bpTOPcxx . . $ bpTOP. ) |
| 125 | { |
| 126 | inProcesscxx . . $ DlSatTester#inProcess..insertcxx . . $ std/set#insert(411a10a755068dfe).(x); |
| 127 | # ifdef TMP_CACHE_DEBUG |
| 128 | std::cerr << " caching range(" << v.getRole()->getName() << ") = " << x << ";"; |
| 129 | # endif |
| 130 | createCachecxx . . $ DlSatTester#createCache(b3d3cd5834c92364).(x); |
| 131 | inProcesscxx . . $ DlSatTester#inProcess..erasecxx . . $ std/set#erase(3a53bfe100edb289).(x); |
| 132 | } |
| 133 | |
| 134 | break; |
| 135 | } |
| 136 | |
| 137 | case dtIrrcxx . . $ dtIrr.: |
| 138 | break; |
| 139 | |
| 140 | default: |
| 141 | fpp_unreachablecxx . . $ `Kernel/fpp_assert.h:49:9`!(); |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | /// build cache for given DAG node using SAT tests; @return cache |
| 146 | modelCacheInterfacecxx . . $ modelCacheInterface#* |
| 147 | DlSatTestercxx . . $ DlSatTester# :: buildCachecxx . . $ DlSatTester#buildCache(f17fff7e095f06bd). ( BipolarPointercxx . . $ BipolarPointer# p ) |
| 148 | { |
| 149 | if ( LLMcxx . . $ LLM..isWritablecxx . . $ LeveLogger#isWritable(8de63fdb265b07c1).(llDagSatcxx . . $ llDagSat.) ) |
| 150 | { |
| 151 | LLcxx . . $ LL. <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). "\nChecking satisfiability of DAG entry " <<cxx . . $ std/basic_ostream#`operator<<`(23b6575433a77e57). p; |
| 152 | tBoxcxx . . $ DlSatTester#tBox..PrintDagEntrycxx . . $ TBox#PrintDagEntry(94fe421b246ddbdb).(LLcxx . . $ LL.,p); |
| 153 | LLcxx . . $ LL. <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). ":\n"; |
| 154 | } |
| 155 | |
| 156 | #ifdef TMP_CACHE_DEBUG |
| 157 | std::cerr << " building cache for " << p << "..."; |
| 158 | #endif |
| 159 | |
| 160 | bool sat = runSatcxx . . $ DlSatTester#runSat(cf9ef193275fa6d8).(p); |
| 161 | |
| 162 | #ifdef TMP_CACHE_DEBUG |
| 163 | std::cerr << " done"; |
| 164 | #endif |
| 165 | |
| 166 | // unsat => P -> \bot |
| 167 | if ( !sat && LLMcxx . . $ LLM..isWritablecxx . . $ LeveLogger#isWritable(8de63fdb265b07c1).(llAlwayscxx . . $ llAlways.) ) |
| 168 | LLcxx . . $ LL. <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). "\nDAG entry " <<cxx . . $ std/basic_ostream#`operator<<`(23b6575433a77e57). p <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). " is unsatisfiable\n"; |
| 169 | |
| 170 | return buildCacheByCGraphcxx . . $ DlSatTester#buildCacheByCGraph(b0360ae62ba10694).(sat); |
| 171 | } |
| 172 |