| 1 | /* This file is part of the FaCT++ DL reasoner |
| 2 | Copyright (C) 2006-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 CGLABEL_H |
| 21 | #define CGLABEL_H |
| 22 | |
| 23 | #include "CWDArray.h" |
| 24 | #include "dlVertex.h" // for DagTag |
| 25 | |
| 26 | /// class implementing a label in a node of a completion graph |
| 27 | class CGLabel |
| 28 | { |
| 29 | public: // type interface |
| 30 | /// RO iterator on label |
| 31 | typedef CWDArray::const_iterator const_iterator; |
| 32 | |
| 33 | public: // external classes |
| 34 | /// class for save/restore |
| 35 | class SaveState |
| 36 | { |
| 37 | public: |
| 38 | /// states for simple-, complex- and extra labels |
| 39 | CWDArray::SaveState sc, cc; |
| 40 | }; // SaveState |
| 41 | |
| 42 | protected: // members |
| 43 | /// all simple concepts, labelled a node |
| 44 | CWDArray scLabel; |
| 45 | /// all complex concepts (ie, FORALL, GE), labelled a node |
| 46 | CWDArray ccLabel; |
| 47 | |
| 48 | public: // static methods |
| 49 | /// @return true iff TAG represents complex concept |
| 50 | static bool isComplexConcept ( DagTag tag ) |
| 51 | { |
| 52 | switch(tag) |
| 53 | { |
| 54 | case dtForall: |
| 55 | case dtLE: |
| 56 | case dtIrr: |
| 57 | case dtNN: |
| 58 | case dtChoose: |
| 59 | return true; |
| 60 | default: |
| 61 | return false; |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | public: // interface |
| 66 | /// init newly created node |
| 67 | void init ( void ); |
| 68 | |
| 69 | //---------------------------------------------- |
| 70 | // Label access interface |
| 71 | //---------------------------------------------- |
| 72 | |
| 73 | // label iterators |
| 74 | |
| 75 | /// begin() iterator for a label with simple concepts |
| 76 | const_iterator begin_sc ( void ) const { return scLabel.begin(); } |
| 77 | /// end() iterator for a label with simple concepts |
| 78 | const_iterator end_sc ( void ) const { return scLabel.end(); } |
| 79 | /// begin() iterator for a label with complex concepts |
| 80 | const_iterator begin_cc ( void ) const { return ccLabel.begin(); } |
| 81 | /// end() iterator for a label with complex concepts |
| 82 | const_iterator end_cc ( void ) const { return ccLabel.end(); } |
| 83 | |
| 84 | //---------------------------------------------- |
| 85 | // Label access interface |
| 86 | //---------------------------------------------- |
| 87 | /// get (RW) label associated with the concepts defined by the flag |
| 88 | CWDArray& getLabel ( bool isComplex ) { return isComplex ? ccLabel : scLabel; } |
| 89 | /// get (RO) label associated with the concepts defined by the flag |
| 90 | const CWDArray& getLabel ( bool isComplex ) const { return isComplex ? ccLabel : scLabel; } |
| 91 | /// get (RW) label associated with the concepts defined by TAG |
| 92 | CWDArray& getLabel ( DagTag tag ) { return getLabel(isComplexConcept(tag)); } |
| 93 | /// get (RO) label associated with the concepts defined by TAG |
| 94 | const CWDArray& getLabel ( DagTag tag ) const { return getLabel(isComplexConcept(tag)); } |
| 95 | |
| 96 | // TODO table interface |
| 97 | |
| 98 | /// get ToDoEntry offset by given SC labels' iterator |
| 99 | int getSCOffset ( const_iterator p ) const { return int(p-begin_sc()); } |
| 100 | /// get ToDoEntry offset by given CC labels' iterator |
| 101 | int getCCOffset ( const_iterator p ) const { return int(-(p-begin_cc()+1)); } |
| 102 | /// get the index of the latest (ie just inserted) concept in the label |
| 103 | int getLast ( DagTag tag ) const |
| 104 | { |
| 105 | if ( isComplexConcept(tag) ) |
| 106 | return getCCOffset(end_cc()-1); // complex concept |
| 107 | else |
| 108 | return getSCOffset(end_sc()-1); // simple concept |
| 109 | } |
| 110 | /// get the concept by given index in the node's label |
| 111 | const ConceptWDep& getConcept ( int n ) const |
| 112 | { |
| 113 | if ( n < 0 ) |
| 114 | return ccLabel.getConcept(size_t(-n-1)); |
| 115 | else |
| 116 | return scLabel.getConcept(size_t(n)); |
| 117 | } |
| 118 | /// get CC offset of a complex concept BP that appears in the label |
| 119 | int getCCOffset ( BipolarPointer bp ) const |
| 120 | { |
| 121 | for ( const_iterator p = begin_cc(), p_end = end_cc(); p < p_end; ++p ) |
| 122 | if ( *p == bp ) |
| 123 | return getCCOffset(p); |
| 124 | // BP should appear in the label |
| 125 | fpp_unreachable(); |
| 126 | } |
| 127 | |
| 128 | // check if node is labelled by given concept |
| 129 | |
| 130 | /// check whether node is labelled by (arbitrary) concept P |
| 131 | bool contains ( BipolarPointer p ) const; |
| 132 | /// check whether node is labelled by simple concept P |
| 133 | bool containsSC ( BipolarPointer p ) const { return scLabel.contains(p); } |
| 134 | /// check whether node is labelled by complex concept P |
| 135 | bool containsCC ( BipolarPointer p ) const { return ccLabel.contains(p); } |
| 136 | |
| 137 | //---------------------------------------------- |
| 138 | // Blocking support |
| 139 | //---------------------------------------------- |
| 140 | |
| 141 | /// check whether LABEL is a superset of a current one |
| 142 | bool operator <= ( const CGLabel& label ) const |
| 143 | { |
| 144 | return ( scLabel <= label.scLabel ) && ( ccLabel <= label.ccLabel ); |
| 145 | } |
| 146 | /// check whether LABEL is a subset of a current one |
| 147 | bool operator >= ( const CGLabel& label ) const { return label <= *this; } |
| 148 | /// check whether LABEL is the same as a current one |
| 149 | bool operator == ( const CGLabel& label ) const |
| 150 | { return (*this <= label) && (label <= *this); } |
| 151 | |
| 152 | //---------------------------------------------- |
| 153 | // Save/restore interface |
| 154 | //---------------------------------------------- |
| 155 | |
| 156 | /// save label using given SS |
| 157 | void save ( SaveState& ss ) const |
| 158 | { |
| 159 | scLabel.save(ss.sc); |
| 160 | ccLabel.save(ss.cc); |
| 161 | } |
| 162 | /// restore label to given LEVEL using given SS |
| 163 | void restore ( const SaveState& ss, unsigned int level ) |
| 164 | { |
| 165 | scLabel.restore(ss.sc,level); |
| 166 | ccLabel.restore(ss.cc,level); |
| 167 | } |
| 168 | |
| 169 | //---------------------------------------------- |
| 170 | // Output |
| 171 | //---------------------------------------------- |
| 172 | |
| 173 | /// print the whole label |
| 174 | void print ( std::ostream& o ) const |
| 175 | { |
| 176 | scLabel.print(o); |
| 177 | ccLabel.print(o); |
| 178 | } |
| 179 | }; // CGLabel |
| 180 | |
| 181 | inline void |
| 182 | CGLabel :: init ( void ) |
| 183 | { |
| 184 | // init label with reasonable size |
| 185 | scLabel.init(8); // FIXME!! correct size later on |
| 186 | ccLabel.init(4); // FIXME!! correct size later on |
| 187 | } |
| 188 | |
| 189 | inline bool |
| 190 | CGLabel :: contains ( BipolarPointer p ) const |
| 191 | { |
| 192 | #ifdef ENABLE_CHECKING |
| 193 | fpp_assert ( isCorrect(p) ); // sanity checking |
| 194 | #endif |
| 195 | switch(p) |
| 196 | { |
| 197 | case bpTOP: return true; |
| 198 | case bpBOTTOM: return false; |
| 199 | default: return containsSC(p) || containsCC(p); |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | #endif |