| 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 DLCOMPLETIONTREEARC_H |
| 21 | #define DLCOMPLETIONTREEARC_H |
| 22 | |
| 23 | #include "globaldef.h" |
| 24 | #include "DeletelessAllocator.h" |
| 25 | #include "DepSet.h" |
| 26 | #include "tRole.h" |
| 27 | #include "tRestorer.h" |
| 28 | |
| 29 | class DlCompletionTree; |
| 30 | |
| 31 | class DlCompletionTreeArc |
| 32 | { |
| 33 | friend class DlCompletionGraph; |
| 34 | public: // external type definitions |
| 35 | /// type for the edges allocator |
| 36 | typedef DeletelessAllocator<DlCompletionTreeArc> EdgeAllocator; |
| 37 | |
| 38 | protected: // members |
| 39 | /// pointer to "to" node |
| 40 | DlCompletionTree* Node = nullptr; |
| 41 | /// role, labelling given arc |
| 42 | const TRole* Role = nullptr; |
| 43 | /// dep-set of the arc |
| 44 | DepSet depSet; |
| 45 | /// pointer to reverse arc |
| 46 | DlCompletionTreeArc* Reverse = nullptr; |
| 47 | /// true if the edge going from a predecessor to a successor |
| 48 | bool SuccEdge = true; |
| 49 | |
| 50 | private: // methods |
| 51 | /// init an arc with R as a label and NODE on given LEVEL; use it inside MAKEARCS only |
| 52 | void init ( const TRole* role, const DepSet& dep, DlCompletionTree* node ) |
| 53 | { |
| 54 | Role = role; |
| 55 | depSet = dep; |
| 56 | Node = node; |
| 57 | Reverse = nullptr; |
| 58 | } |
| 59 | |
| 60 | protected: // classes |
| 61 | /// class for restoring edge |
| 62 | class TCTEdgeRestorer: public TRestorer |
| 63 | { |
| 64 | protected: |
| 65 | DlCompletionTreeArc* p; |
| 66 | const TRole* r; |
| 67 | public: |
| 68 | explicit TCTEdgeRestorer ( DlCompletionTreeArc* q ) : p(q), r(q->Role) {} |
| 69 | void restore ( void ) override { p->Role = r; p->Reverse->Role = r->inverse(); } |
| 70 | }; // TCTEdgeRestorer |
| 71 | |
| 72 | /// class for restoring dep-set |
| 73 | class TCTEdgeDepRestorer: public TRestorer |
| 74 | { |
| 75 | protected: |
| 76 | DlCompletionTreeArc* p; |
| 77 | DepSet dep; |
| 78 | public: |
| 79 | explicit TCTEdgeDepRestorer ( DlCompletionTreeArc* q ) : p(q), dep(q->getDep()) {} |
| 80 | void restore ( void ) override { p->depSet = dep; } |
| 81 | }; // TCTEdgeDepRestorer |
| 82 | |
| 83 | protected: // methods |
| 84 | |
| 85 | /// set given arc as a reverse of current |
| 86 | void setReverse ( DlCompletionTreeArc* v ) |
| 87 | { |
| 88 | Reverse = v; |
| 89 | v->Reverse = this; |
| 90 | } |
| 91 | |
| 92 | public: // interface |
| 93 | /// empty c'tor |
| 94 | DlCompletionTreeArc() = default; |
| 95 | /// no copy c'tor |
| 96 | DlCompletionTreeArc ( const DlCompletionTreeArc& ) = delete; |
| 97 | /// no assignment |
| 98 | DlCompletionTreeArc& operator = ( const DlCompletionTreeArc& ) = delete; |
| 99 | |
| 100 | /// get label of the edge |
| 101 | const TRole* getRole ( void ) const { return Role; } |
| 102 | /// get dep-set of the edge |
| 103 | const DepSet& getDep ( void ) const { return depSet; } |
| 104 | |
| 105 | /// set the successor field |
| 106 | void setSuccEdge ( bool val ) { SuccEdge = val; } |
| 107 | /// @return true if the edge is the successor one |
| 108 | bool isSuccEdge ( void ) const { return SuccEdge; } |
| 109 | /// @return true if the edge is the predecessor one |
| 110 | bool isPredEdge ( void ) const { return !SuccEdge; } |
| 111 | |
| 112 | /// get (RW) access to the end of arc |
| 113 | DlCompletionTree* getArcEnd ( void ) const { return Node; } |
| 114 | /// get access to reverse arc |
| 115 | DlCompletionTreeArc* getReverse ( void ) const { return Reverse; } |
| 116 | |
| 117 | /// check if arc is labelled by a super-role of PROLE |
| 118 | bool isNeighbour ( const TRole* pRole ) const |
| 119 | { |
| 120 | return !isIBlocked() && ( *pRole >= *getRole() ); |
| 121 | } |
| 122 | |
| 123 | /// is arc merged to another |
| 124 | bool isIBlocked ( void ) const { return (Role == nullptr); } |
| 125 | /// check whether the edge is reflexive |
| 126 | bool isReflexiveEdge ( void ) const { return getArcEnd() == getReverse()->getArcEnd(); } |
| 127 | |
| 128 | //---------------------------------------------- |
| 129 | // saving/restoring |
| 130 | //---------------------------------------------- |
| 131 | |
| 132 | /// save and invalidate arc (together with reverse arc) |
| 133 | TRestorer* save ( void ) |
| 134 | { |
| 135 | if ( Role == nullptr ) // don't invalidate edge twice |
| 136 | return nullptr; |
| 137 | |
| 138 | TRestorer* ret = new TCTEdgeRestorer(this); |
| 139 | Role = nullptr; |
| 140 | Reverse->Role = nullptr; |
| 141 | return ret; |
| 142 | } |
| 143 | |
| 144 | /// add dep-set to an edge; return restorer |
| 145 | TRestorer* addDep ( const DepSet& dep ) |
| 146 | { |
| 147 | if ( dep.empty() ) |
| 148 | return nullptr; |
| 149 | TRestorer* ret = new TCTEdgeDepRestorer(this); |
| 150 | depSet.add(dep); |
| 151 | return ret; |
| 152 | } |
| 153 | |
| 154 | // output |
| 155 | |
| 156 | /// print current arc |
| 157 | void Print ( std::ostream& o ) const |
| 158 | { o << "<" << ( isIBlocked() ? "-" : Role->getName() ) << depSet << ">"; } |
| 159 | }; // DlCompletionTreeArc |
| 160 | |
| 161 | #endif // DLCOMPLETIONTREEARC_H |