| 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 "dlCompletionGraph.h" |
| 21 | |
| 22 | DlCompletionTreeArc* |
| 23 | DlCompletionGraph :: createEdge ( |
| 24 | DlCompletionTree* from, |
| 25 | DlCompletionTree* to, |
| 26 | bool isPredEdge, |
| 27 | const TRole* roleName, |
| 28 | const DepSet& dep ) |
| 29 | { |
| 30 | // create 2 arcs between FROM and TO |
| 31 | DlCompletionTreeArc* forward = CTEdgeHeap.get(); |
| 32 | forward->init ( roleName, dep, to ); |
| 33 | forward->setSuccEdge(!isPredEdge); |
| 34 | DlCompletionTreeArc* backward = CTEdgeHeap.get(); |
| 35 | backward->init ( roleName->inverse(), dep, from ); |
| 36 | backward->setSuccEdge(isPredEdge); |
| 37 | // set the reverse link |
| 38 | forward->setReverse(backward); |
| 39 | |
| 40 | // connect them to nodes |
| 41 | saveNode ( from, branchingLevel ); |
| 42 | saveNode ( to, branchingLevel ); |
| 43 | // add the edges as corresponding neighbours |
| 44 | from->addNeighbour(forward); |
| 45 | to->addNeighbour(backward); |
| 46 | if ( LLM.isWritable(llGTA) ) |
| 47 | { |
| 48 | LL << " ce("; |
| 49 | if ( isPredEdge ) |
| 50 | LL << to->getId() << "<-" << from->getId(); |
| 51 | else |
| 52 | LL << from->getId() << "->" << to->getId(); |
| 53 | LL << "," << roleName->getName() << ")"; |
| 54 | } |
| 55 | |
| 56 | return forward; |
| 57 | } |
| 58 | |
| 59 | /// replace the EDGE (that comes from old node to X) with a new one, that comes from NODE to X |
| 60 | DlCompletionTreeArc* |
| 61 | DlCompletionGraph :: moveEdge ( DlCompletionTree* node, DlCompletionTreeArc* edge, |
| 62 | bool isPredEdge, const DepSet& dep ) |
| 63 | { |
| 64 | // skip already purged edges |
| 65 | if ( edge->isIBlocked()) |
| 66 | return nullptr; |
| 67 | |
| 68 | // skip edges not leading to nominal nodes |
| 69 | if ( !isPredEdge && !edge->getArcEnd()->isNominalNode() ) |
| 70 | return nullptr; |
| 71 | |
| 72 | const TRole* R = edge->getRole(); |
| 73 | |
| 74 | // we shall copy reflexive edges in a specific way |
| 75 | if ( edge->isReflexiveEdge() ) |
| 76 | return createLoop ( node, R, dep ); |
| 77 | |
| 78 | DlCompletionTree* to = edge->getArcEnd(); |
| 79 | |
| 80 | // invalidate old edge |
| 81 | invalidateEdge(edge); |
| 82 | |
| 83 | // try to find for NODE->TO (TO->NODE) whether we |
| 84 | // have TO->NODE (NODE->TO) edge already |
| 85 | for ( DlCompletionTree::const_edge_iterator p = node->begin(), p_end = node->end(); p < p_end; ++p ) |
| 86 | if ( (*p)->getArcEnd() == to && (*p)->isPredEdge() != isPredEdge ) |
| 87 | return addRoleLabel ( node, to, !isPredEdge, R, dep ); |
| 88 | |
| 89 | return addRoleLabel ( node, to, isPredEdge, R, dep ); |
| 90 | } |
| 91 | |
| 92 | // merge labels; see SHOIN paper for detailed description |
| 93 | void DlCompletionGraph :: Merge ( DlCompletionTree* from, DlCompletionTree* to, |
| 94 | const DepSet& dep, |
| 95 | std::vector<DlCompletionTreeArc*>& edges ) |
| 96 | { |
| 97 | edges.clear(); |
| 98 | |
| 99 | // 1. For all x: x->FROM make x->TO |
| 100 | // FIXME!! no optimisations (in case there exists an edge TO->x labelled with R-) |
| 101 | // 2. For all nominal x: FROM->x make TO->x |
| 102 | // FIXME!! no optimisations (in case there exists an edge x->TO labelled with R-) |
| 103 | for ( DlCompletionTree::const_edge_iterator p = from->begin(), p_end = from->end(); p < p_end; ++p ) |
| 104 | { |
| 105 | if ( (*p)->isPredEdge() || (*p)->getArcEnd()->isNominalNode() ) |
| 106 | { |
| 107 | DlCompletionTreeArc* temp = moveEdge ( to, *p, (*p)->isPredEdge(), dep ); |
| 108 | if ( temp != nullptr ) |
| 109 | edges.push_back(temp); |
| 110 | } |
| 111 | if ( (*p)->isSuccEdge() ) |
| 112 | purgeEdge ( *p, to, dep ); |
| 113 | } |
| 114 | |
| 115 | // 4. For all x: FROM \neq x, add TO \neq x |
| 116 | updateIR ( to, from, dep ); |
| 117 | |
| 118 | // 5. Purge FROM |
| 119 | purgeNode ( from, to, dep ); |
| 120 | } |
| 121 | |
| 122 | void |
| 123 | DlCompletionGraph :: purgeNode ( DlCompletionTree* p, const DlCompletionTree* root, const DepSet& dep ) |
| 124 | { |
| 125 | if ( p->isPBlocked() ) |
| 126 | return; |
| 127 | |
| 128 | saveRareCond ( p->setPBlocked ( root, dep ) ); |
| 129 | |
| 130 | // update successors |
| 131 | for ( DlCompletionTree::const_edge_iterator q = p->begin(); q != p->end(); ++q ) |
| 132 | if ( (*q)->isSuccEdge() && !(*q)->isIBlocked() ) |
| 133 | purgeEdge ( *q, root, dep ); |
| 134 | } |
| 135 | |
| 136 | /// purge edge E with given ROOT and DEP-set |
| 137 | void |
| 138 | DlCompletionGraph :: purgeEdge ( DlCompletionTreeArc* e, const DlCompletionTree* root, const DepSet& dep ) |
| 139 | { |
| 140 | invalidateEdge(e); // invalidate given link |
| 141 | if ( e->getArcEnd()->isBlockableNode() ) |
| 142 | purgeNode ( e->getArcEnd(), root, dep ); // purge blockable successor |
| 143 | } |
| 144 | |
| 145 | // save/restore |
| 146 | |
| 147 | void DlCompletionGraph :: save ( void ) |
| 148 | { |
| 149 | SaveState* s = Stack.push(); |
| 150 | s->nNodes = endUsed; |
| 151 | s->sNodes = SavedNodes.size(); |
| 152 | s->nEdges = CTEdgeHeap.size(); |
| 153 | RareStack.incLevel(); |
| 154 | ++branchingLevel; |
| 155 | } |
| 156 | |
| 157 | void DlCompletionGraph :: restore ( unsigned int level ) |
| 158 | { |
| 159 | fpp_assert ( level > 0 ); |
| 160 | branchingLevel = level; |
| 161 | RareStack.restore(level); |
| 162 | SaveState* s = Stack.pop(level); |
| 163 | endUsed = s->nNodes; |
| 164 | size_t nSaved = s->sNodes; |
| 165 | iterator p = SavedNodes.begin()+(long)nSaved, p_end = SavedNodes.end(); |
| 166 | if ( endUsed < size_t(p_end-p) ) // it's cheaper to restore all nodes |
| 167 | for ( p = begin(), p_end = end(); p < p_end; ++p ) |
| 168 | restoreNode ( (*p), level ); |
| 169 | else |
| 170 | for ( ; p < p_end; ++p ) |
| 171 | if ( (*p)->getId() < endUsed ) // don't restore nodes that are dead anyway |
| 172 | restoreNode ( (*p), level ); |
| 173 | SavedNodes.resize(nSaved); |
| 174 | CTEdgeHeap.resize(s->nEdges); |
| 175 | } |
| 176 | |
| 177 | // printing CGraph |
| 178 | |
| 179 | void DlCompletionGraph :: Print ( std::ostream& o ) |
| 180 | { |
| 181 | // init indentation and node labels |
| 182 | CGPIndent = 0; |
| 183 | std::vector<bool> temp ( endUsed, false ); |
| 184 | CGPFlag.swap(temp); |
| 185 | |
| 186 | const_iterator p = begin(), p_end = end(); |
| 187 | unsigned int i = 1; // node id |
| 188 | |
| 189 | // mark all nominals as already printed: they full subtrees will be output with a nominal cloud |
| 190 | for ( ++p; p < p_end && (*p)->isNominalNode(); ++p, ++i ) |
| 191 | CGPFlag[i] = true; |
| 192 | |
| 193 | // print tree starting from the root node |
| 194 | p = begin(); |
| 195 | PrintNode ( *p, o ); |
| 196 | |
| 197 | // if there are nominals in the graph -- print the nominal cloud |
| 198 | for ( ++p; p < p_end && (*p)->isNominalNode(); ++p ) |
| 199 | { |
| 200 | // print given nominal node in full |
| 201 | CGPFlag[(*p)->getId()] = false; |
| 202 | PrintNode ( *p, o ); |
| 203 | } |
| 204 | o << "\n"; |
| 205 | } |
| 206 | |
| 207 | void |
| 208 | DlCompletionGraph :: PrintEdge ( DlCompletionTree::const_edge_iterator edge, const DlCompletionTree* parent, std::ostream& o ) |
| 209 | { |
| 210 | const DlCompletionTree* node = (*edge)->getArcEnd(); |
| 211 | bool succEdge = (*edge)->isSuccEdge(); |
| 212 | |
| 213 | PrintIndent(o); |
| 214 | for ( ; edge != parent->end(); ++edge ) |
| 215 | if ( (*edge)->getArcEnd() == node && (*edge)->isSuccEdge() == succEdge ) |
| 216 | o << " ", (*edge)->Print(o); // print edge's label |
| 217 | |
| 218 | if ( node == parent ) // print loop |
| 219 | { |
| 220 | PrintIndent(o); |
| 221 | o << "-loop to node " << parent->getId(); |
| 222 | } |
| 223 | else |
| 224 | PrintNode ( node, o ); |
| 225 | } |
| 226 | |
| 227 | /// print node of the graph with proper indentation |
| 228 | void |
| 229 | DlCompletionGraph :: PrintNode ( const DlCompletionTree* node, std::ostream& o ) |
| 230 | { |
| 231 | if ( CGPIndent ) |
| 232 | { |
| 233 | PrintIndent(o); |
| 234 | o << "-"; |
| 235 | } |
| 236 | else |
| 237 | o << "\n"; |
| 238 | |
| 239 | node->PrintBody(o); // print node's label |
| 240 | |
| 241 | // don't print subtree twice |
| 242 | if ( CGPFlag[node->getId()] ) |
| 243 | { |
| 244 | o << "d"; |
| 245 | return; |
| 246 | } |
| 247 | |
| 248 | CGPFlag[node->getId()] = true; // mark node printed |
| 249 | |
| 250 | // we want to print incoming edges for the nominal cloud |
| 251 | bool wantPred = node->isNominalNode(); |
| 252 | |
| 253 | // print all children |
| 254 | ++CGPIndent; |
| 255 | for ( DlCompletionTree::const_edge_iterator p = node->begin(); p != node->end(); ++p ) |
| 256 | if ( (*p)->isSuccEdge() || ( wantPred && (*p)->getArcEnd()->isNominalNode() ) ) |
| 257 | PrintEdge ( p, node, o ); |
| 258 | --CGPIndent; |
| 259 | } |