| 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 "dlCompletionTree.h" |
| 21 | |
| 22 | /// check if transitive R-successor of the NODE labelled with C |
| 23 | const DlCompletionTree* |
| 24 | DlCompletionTree :: isTSuccLabelled ( const TRole* R, BipolarPointer C ) const |
| 25 | { |
| 26 | if ( isLabelledBy(C) ) |
| 27 | return this; |
| 28 | // don't check nominal nodes (prevent cycles) |
| 29 | if ( isNominalNode() ) |
| 30 | return nullptr; |
| 31 | |
| 32 | // check all other successors |
| 33 | const DlCompletionTree* ret = nullptr; |
| 34 | for ( const_edge_iterator p = begin(), p_end = end(); p < p_end; ++p ) |
| 35 | if ( (*p)->isSuccEdge() && |
| 36 | (*p)->isNeighbour(R) && |
| 37 | !(*p)->isReflexiveEdge() && // prevent cycles |
| 38 | (ret = (*p)->getArcEnd()->isTSuccLabelled(R,C)) != nullptr ) |
| 39 | return ret; |
| 40 | |
| 41 | // not happens |
| 42 | return nullptr; |
| 43 | } |
| 44 | |
| 45 | /// check if transitive R-predecessor of the NODE labelled with C; skip FROM node |
| 46 | const DlCompletionTree* |
| 47 | DlCompletionTree :: isTPredLabelled ( const TRole* R, BipolarPointer C, const DlCompletionTree* from ) const |
| 48 | { |
| 49 | if ( isLabelledBy(C) ) |
| 50 | return this; |
| 51 | // don't check nominal nodes (prevent cycles) |
| 52 | if ( isNominalNode() ) |
| 53 | return nullptr; |
| 54 | |
| 55 | // check all other successors |
| 56 | const DlCompletionTree* ret = nullptr; |
| 57 | for ( const_edge_iterator p = begin(), p_end = end(); p < p_end; ++p ) |
| 58 | if ( (*p)->isSuccEdge() && (*p)->isNeighbour(R) && (*p)->getArcEnd() != from && |
| 59 | (ret = (*p)->getArcEnd()->isTSuccLabelled(R,C)) != nullptr ) |
| 60 | return ret; |
| 61 | |
| 62 | // check predecessor |
| 63 | if ( hasParent() && isParentArcLabelled(R) ) |
| 64 | return getParentNode()->isTPredLabelled ( R, C, this ); |
| 65 | else |
| 66 | return nullptr; |
| 67 | } |
| 68 | |
| 69 | const DlCompletionTree* |
| 70 | DlCompletionTree :: isNSomeApplicable ( const TRole* R, BipolarPointer C ) const |
| 71 | { |
| 72 | for ( DlCompletionTree::const_edge_iterator p = begin(), p_end = end(); p < p_end; ++p ) |
| 73 | if ( (*p)->isNeighbour(R) && (*p)->getArcEnd()->isLabelledBy(C) ) |
| 74 | return (*p)->getArcEnd(); // already contained such a label |
| 75 | |
| 76 | return nullptr; |
| 77 | } |
| 78 | |
| 79 | const DlCompletionTree* |
| 80 | DlCompletionTree :: isTSomeApplicable ( const TRole* R, BipolarPointer C ) const |
| 81 | { |
| 82 | const DlCompletionTree* ret = nullptr; |
| 83 | |
| 84 | for ( DlCompletionTree::const_edge_iterator p = begin(), p_end = end(); p < p_end; ++p ) |
| 85 | if ( (*p)->isNeighbour(R) ) |
| 86 | { |
| 87 | if ( (*p)->isPredEdge() ) |
| 88 | ret = (*p)->getArcEnd()->isTPredLabelled(R,C,this); |
| 89 | else |
| 90 | ret = (*p)->getArcEnd()->isTSuccLabelled(R,C); |
| 91 | |
| 92 | if ( ret ) |
| 93 | return ret; // already contained such a label |
| 94 | } |
| 95 | |
| 96 | return nullptr; |
| 97 | } |
| 98 | |
| 99 | #ifdef RKG_IR_IN_NODE_LABEL |
| 100 | //---------------------------------------------- |
| 101 | // inequality relation methods |
| 102 | //---------------------------------------------- |
| 103 | |
| 104 | // check if IR for the node contains C |
| 105 | bool DlCompletionTree :: inIRwithC ( const ConceptWDep& C, DepSet& dep ) const |
| 106 | { |
| 107 | if ( IR.empty() ) |
| 108 | return false; |
| 109 | |
| 110 | for ( const auto& cwd: IR ) |
| 111 | if ( cwd.bp() == C.bp() ) |
| 112 | { |
| 113 | dep += cwd.getDep(); |
| 114 | dep += C.getDep(); |
| 115 | return true; |
| 116 | } |
| 117 | |
| 118 | return false; |
| 119 | } |
| 120 | |
| 121 | // check if the NODE's and current node's IR are labeled with the same level |
| 122 | bool DlCompletionTree :: nonMergeable ( const DlCompletionTree* node, DepSet& dep ) const |
| 123 | { |
| 124 | if ( IR.empty() || node->IR.empty() ) |
| 125 | return false; |
| 126 | |
| 127 | for ( const auto& cwd: node->IR ) |
| 128 | if ( inIRwithC ( cwd, dep ) ) |
| 129 | return true; |
| 130 | |
| 131 | return false; |
| 132 | } |
| 133 | |
| 134 | /// update IR of the current node with IR from NODE and additional clash-set; @return restorer |
| 135 | TRestorer* DlCompletionTree :: updateIR ( const DlCompletionTree* node, const DepSet& toAdd ) |
| 136 | { |
| 137 | if ( node->IR.empty() ) |
| 138 | return nullptr; // nothing to do |
| 139 | |
| 140 | // save current state |
| 141 | TRestorer* ret = new IRRestorer(this); |
| 142 | |
| 143 | // copy all elements from NODE's IR to current node. |
| 144 | // FIXME!! do not check if some of them are already in there |
| 145 | for ( const auto& cwd: node->IR ) |
| 146 | IR.emplace_back ( cwd, toAdd ); |
| 147 | |
| 148 | return ret; |
| 149 | } |
| 150 | #endif // RKG_IR_IN_NODE_LABEL |
| 151 | |
| 152 | // saving/restoring |
| 153 | void DlCompletionTree :: save ( SaveState* nss ) const |
| 154 | { |
| 155 | nss->curLevel = curLevel; |
| 156 | nss->nNeighbours = Neighbour.size(); |
| 157 | Label.save(nss->lab); |
| 158 | |
| 159 | logSRNode("SaveNode"); |
| 160 | } |
| 161 | |
| 162 | void DlCompletionTree :: restore ( SaveState* nss ) |
| 163 | { |
| 164 | if ( nss == nullptr ) |
| 165 | return; |
| 166 | |
| 167 | // level restore |
| 168 | curLevel = nss->curLevel; |
| 169 | |
| 170 | // label restore |
| 171 | Label.restore ( nss->lab, getCurLevel() ); |
| 172 | |
| 173 | // remove new neighbours |
| 174 | if ( RKG_USE_DYNAMIC_BACKJUMPING ) |
| 175 | { // FIXME!! do nothing for now |
| 176 | // for ( int j = Neighbour.size()-1; j >= 0; --j ) |
| 177 | // if ( Neighbour[j]->getNode()->creLevel <= getCurLevel() ) |
| 178 | // { |
| 179 | // Neighbour.resize(j+1); |
| 180 | // break; |
| 181 | // } |
| 182 | } |
| 183 | else |
| 184 | Neighbour.resize(nss->nNeighbours); |
| 185 | |
| 186 | // it's cheaper to dirty affected flag than to consistently save nodes |
| 187 | affected = true; |
| 188 | |
| 189 | // clear memory |
| 190 | delete nss; |
| 191 | |
| 192 | logSRNode("RestNode"); |
| 193 | } |
| 194 | |
| 195 | // output |
| 196 | void DlCompletionTree :: PrintBody ( std::ostream& o ) const |
| 197 | { |
| 198 | o << id; |
| 199 | if ( isNominalNode() ) |
| 200 | o << "o" << getNominalLevel(); |
| 201 | o << '(' << getCurLevel() << ')'; |
| 202 | |
| 203 | // data node? |
| 204 | if ( isDataNode() ) |
| 205 | o << "d"; |
| 206 | |
| 207 | // label (if any) |
| 208 | Label.print(o); |
| 209 | |
| 210 | // blocking status information |
| 211 | logNodeBStatus(o); |
| 212 | } |