| 1 | /* This file is part of the FaCT++ DL reasoner |
| 2 | Copyright (C) 2005-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 TDATAENTRY_H |
| 21 | #define TDATAENTRY_H |
| 22 | |
| 23 | #include "tNamedEntry.h" |
| 24 | #include "BiPointer.h" |
| 25 | #include "tLabeller.h" |
| 26 | #include "DataTypeComparator.h" |
| 27 | |
| 28 | // if min() and max() are defined, then they conflicts with a TDI members |
| 29 | #undef min |
| 30 | #undef max |
| 31 | |
| 32 | /// class for representing general data restriction |
| 33 | class TDataInterval |
| 34 | { |
| 35 | public: // members |
| 36 | /// left border of the interval |
| 37 | ComparableDT min; |
| 38 | /// right border of the interval |
| 39 | ComparableDT max; |
| 40 | /// type of the left border |
| 41 | bool minExcl; |
| 42 | /// type of the right border |
| 43 | bool maxExcl; |
| 44 | |
| 45 | public: // interface |
| 46 | |
| 47 | /// clear an interval |
| 48 | void clear ( void ) { min = max = ComparableDT(); } |
| 49 | |
| 50 | /// check if min value range have been set |
| 51 | bool hasMin ( void ) const { return min.inited(); } |
| 52 | /// check if max value range have been set |
| 53 | bool hasMax ( void ) const { return max.inited(); } |
| 54 | /// no constraints |
| 55 | bool empty ( void ) const { return !hasMin() && !hasMax(); } |
| 56 | /// closed interval |
| 57 | bool closed ( void ) const { return hasMin() && hasMax(); } |
| 58 | |
| 59 | /// update MIN border of an interval with VALUE wrt EXCL |
| 60 | bool updateMin ( bool excl, const ComparableDT& value ) |
| 61 | { |
| 62 | if ( hasMin() ) // another min value: check if we need update |
| 63 | { |
| 64 | // constraint is >= or > |
| 65 | if ( min > value ) // was: {7,}; now: {5,}: no update needed |
| 66 | return false; |
| 67 | if ( min == value && // was: (5,}; now: [5,}: no update needed |
| 68 | minExcl && !excl ) |
| 69 | return false; |
| 70 | // fallthrough: update is necessary for everything else |
| 71 | } |
| 72 | |
| 73 | min = value; |
| 74 | minExcl = min.correctMin(excl); |
| 75 | return true; |
| 76 | } |
| 77 | /// update MAX border of an interval with VALUE wrt EXCL |
| 78 | bool updateMax ( bool excl, const ComparableDT& value ) |
| 79 | { |
| 80 | if ( hasMax() ) // another max value: check if we need update |
| 81 | { |
| 82 | // constraint is <= or < |
| 83 | if ( max < value ) // was: {,5}; now: {,7}: no update needed |
| 84 | return false; |
| 85 | if ( max == value && // was: {,5); now: {,5]: no update needed |
| 86 | maxExcl && !excl ) |
| 87 | return false; |
| 88 | // fallthrough: update is necessary for everything else |
| 89 | } |
| 90 | |
| 91 | max = value; |
| 92 | maxExcl = max.correctMax(excl); |
| 93 | return true; |
| 94 | } |
| 95 | /// update given border of an interval with VALUE wrt EXCL |
| 96 | bool update ( bool min, bool excl, const ComparableDT& value ) |
| 97 | { return min ? updateMin ( excl, value ) : updateMax ( excl, value ); } |
| 98 | /// update wrt another interval |
| 99 | bool update ( const TDataInterval& Int ) |
| 100 | { |
| 101 | bool ret = false; |
| 102 | if ( Int.hasMin() ) |
| 103 | ret |= updateMin ( Int.minExcl, Int.min ); |
| 104 | if ( Int.hasMax() ) |
| 105 | ret |= updateMax ( Int.maxExcl, Int.max ); |
| 106 | return ret; |
| 107 | } |
| 108 | /// @return true iff all the data is consistent wrt given TYPE |
| 109 | bool consistent ( const ComparableDT& dtype ) const |
| 110 | { |
| 111 | if ( hasMin() && !min.compatible(dtype) ) |
| 112 | return false; |
| 113 | if ( hasMax() && !max.compatible(dtype) ) |
| 114 | return false; |
| 115 | return true; |
| 116 | } |
| 117 | |
| 118 | /// print an interval |
| 119 | void Print ( std::ostream& o ) const |
| 120 | { |
| 121 | if ( hasMin() ) |
| 122 | o << (minExcl ? '(' : '[') << min; |
| 123 | else |
| 124 | o << '{'; |
| 125 | o << ','; |
| 126 | if ( hasMax() ) |
| 127 | o << max << (maxExcl ? ')' : ']'); |
| 128 | else |
| 129 | o << '}'; |
| 130 | } |
| 131 | /// print an interval as a LISP; FIXME!! hack |
| 132 | void printLISP ( std::ostream& o, const char* type ) const |
| 133 | { |
| 134 | if ( hasMin() && hasMax() ) |
| 135 | o << "(and "; |
| 136 | if ( hasMin() ) |
| 137 | { |
| 138 | o << "(g" << (minExcl ? 't' : 'e') << " (" << type; |
| 139 | min.printValue(o); |
| 140 | o << "))"; |
| 141 | } |
| 142 | if ( hasMax() ) |
| 143 | { |
| 144 | o << "(l" << (maxExcl ? 't' : 'e') << " (" << type; |
| 145 | max.printValue(o); |
| 146 | o << "))"; |
| 147 | } |
| 148 | if ( hasMin() && hasMax() ) |
| 149 | o << ")"; |
| 150 | } |
| 151 | }; // TDataInterval |
| 152 | |
| 153 | /// class for representing general data entry ("name" of data type or data value) |
| 154 | class TDataEntry: public TNamedEntry |
| 155 | { |
| 156 | private: // members |
| 157 | /// label to use in relevant-only checks |
| 158 | TLabeller::LabelType rel; |
| 159 | |
| 160 | protected: // members |
| 161 | /// corresponding type (Type has NULL in the field) |
| 162 | const TDataEntry* Type; |
| 163 | /// DAG index of the entry |
| 164 | BipolarPointer pName; |
| 165 | /// ComparableDT, used only for values |
| 166 | ComparableDT comp; |
| 167 | /// restriction to the entry |
| 168 | TDataInterval Constraints; |
| 169 | |
| 170 | protected: // methods |
| 171 | /// set COMP for the (typed) data value |
| 172 | void setComp ( const std::string& typeName ) |
| 173 | { |
| 174 | // FIXME!! do the thing properly; unify the usage of DT names |
| 175 | if ( typeName == "string" ) |
| 176 | comp = ComparableDT(getName()); |
| 177 | else if ( typeName == "number" ) |
| 178 | comp = ComparableDT(atol(getName())); |
| 179 | else if ( typeName == "real" ) |
| 180 | comp = ComparableDT((float)atof(getName())); |
| 181 | else if ( typeName == "bool" ) // FIXME!! dirty hack |
| 182 | comp = ComparableDT(getName()); |
| 183 | else if ( typeName == "time" ) |
| 184 | comp = ComparableDT ( atol(getName()), 0 ); |
| 185 | else // no more types available |
| 186 | fpp_unreachable(); |
| 187 | } |
| 188 | |
| 189 | public: // interface |
| 190 | /// create data entry with given name |
| 191 | explicit TDataEntry ( const std::string& name ) |
| 192 | : TNamedEntry(name) |
| 193 | , Type(nullptr) |
| 194 | , pName(bpINVALID) |
| 195 | , comp() |
| 196 | {} |
| 197 | /// no copy c'tor |
| 198 | TDataEntry ( const TDataEntry& ) = delete; |
| 199 | /// no assignment |
| 200 | TDataEntry& operator = ( const TDataEntry& ) = delete; |
| 201 | |
| 202 | // type/value part |
| 203 | |
| 204 | /// check if data entry represents basic data type |
| 205 | bool isBasicDataType ( void ) const { return Type == nullptr && Constraints.empty(); } |
| 206 | /// check if data entry represents restricted data type |
| 207 | bool isRestrictedDataType ( void ) const { return !Constraints.empty(); } |
| 208 | /// check if data entry represents data value |
| 209 | bool isDataValue ( void ) const { return Type != nullptr && Constraints.empty(); } |
| 210 | |
| 211 | /// set host data type for the data value |
| 212 | void setHostType ( const TDataEntry* type ) { Type = type; setComp(type->getName()); } |
| 213 | /// get host type |
| 214 | const TDataEntry* getType ( void ) const { return Type; } |
| 215 | |
| 216 | /// get comparable variant of DE |
| 217 | const ComparableDT& getComp ( void ) const { return comp; } |
| 218 | |
| 219 | // facet part |
| 220 | |
| 221 | /// get RW access to constraints of the DE |
| 222 | TDataInterval* getFacet ( void ) { return &Constraints; } |
| 223 | /// get RO access to constraints of the DE |
| 224 | const TDataInterval* getFacet ( void ) const { return &Constraints; } |
| 225 | |
| 226 | // relevance part |
| 227 | |
| 228 | /// is given concept relevant to given Labeller's state |
| 229 | bool isRelevant ( const TLabeller& lab ) const { return lab.isLabelled(rel); } |
| 230 | /// make given concept relevant to given Labeller's state |
| 231 | void setRelevant ( const TLabeller& lab ) { lab.set(rel); } |
| 232 | |
| 233 | // BP part |
| 234 | |
| 235 | /// get pointer to DAG entry corresponding to the data entry |
| 236 | BipolarPointer getBP ( void ) const { return pName; } |
| 237 | /// set DAG index of the data entry |
| 238 | void setBP ( BipolarPointer p ) { pName = p; } |
| 239 | |
| 240 | // printing LISP FIXME!! |
| 241 | void printLISP ( std::ostream& o ) const |
| 242 | { |
| 243 | o << ' '; |
| 244 | if ( isBasicDataType() ) |
| 245 | o << "(" << getName() << ")"; |
| 246 | else if ( isDataValue() ) |
| 247 | { |
| 248 | o << "(" << getType()->getName(); |
| 249 | comp.printValue(o); |
| 250 | o << ")"; |
| 251 | } |
| 252 | else if ( isRestrictedDataType() ) |
| 253 | Constraints.printLISP ( o, getType()->getName() ); |
| 254 | else |
| 255 | fpp_unreachable(); |
| 256 | } |
| 257 | }; // TDataEntry |
| 258 | |
| 259 | inline std::ostream& |
| 260 | operator << ( std::ostream& o, const TDataInterval& c ) |
| 261 | { |
| 262 | c.Print(o); |
| 263 | return o; |
| 264 | } |
| 265 | |
| 266 | #endif |