| 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 DATAREASONING_H |
| 21 | #define DATAREASONING_H |
| 22 | |
| 23 | |
| 24 | #include <map> |
| 25 | #include <vector> |
| 26 | |
| 27 | #include "tDataEntry.h" |
| 28 | #include "DataTypeComparator.h" |
| 29 | #include "ConceptWithDep.h" |
| 30 | #include "dlDag.h" |
| 31 | #include "logging.h" |
| 32 | |
| 33 | class DataTypeAppearance |
| 34 | { |
| 35 | protected: // classes |
| 36 | /// data interval with dep-sets |
| 37 | class DepInterval |
| 38 | { |
| 39 | protected: // members |
| 40 | /// interval itself |
| 41 | TDataInterval Constraints; |
| 42 | /// local dep-set |
| 43 | DepSet locDep; |
| 44 | public: // interface |
| 45 | /// get RO access to the base data interval |
| 46 | const TDataInterval& getDataInterval ( void ) const { return Constraints; } |
| 47 | /// update MIN border of an TYPE's interval with VALUE wrt EXCL |
| 48 | bool update ( bool min, bool excl, const ComparableDT& value, const DepSet& dep ) |
| 49 | { |
| 50 | if ( !Constraints.update ( min, excl, value ) ) |
| 51 | return false; |
| 52 | locDep += dep; |
| 53 | return true; |
| 54 | } |
| 55 | /// correct MIN and MAX operands of a type |
| 56 | bool checkMinMaxClash ( DepSet& dep ) const; |
| 57 | /// check if the interval is consistent wrt given type |
| 58 | bool consistent ( const ComparableDT& type, DepSet& dep ) const |
| 59 | { |
| 60 | if ( Constraints.consistent(type) ) |
| 61 | return true; |
| 62 | dep += locDep; |
| 63 | return false; |
| 64 | } |
| 65 | /// clear the interval |
| 66 | void clear ( void ) { Constraints.clear(); locDep.clear(); } |
| 67 | }; // DepInterval |
| 68 | |
| 69 | /// datatype restriction is a set of intervals |
| 70 | typedef std::vector<DepInterval> DTConstraint; |
| 71 | typedef DTConstraint::iterator iterator; |
| 72 | typedef DTConstraint::const_iterator const_iterator; |
| 73 | |
| 74 | public: // members |
| 75 | /// dep-set for positive type appearance |
| 76 | DepSet* PType = nullptr; |
| 77 | /// dep-set for negative type appearance |
| 78 | DepSet* NType = nullptr; |
| 79 | |
| 80 | protected: // members |
| 81 | /// interval of possible values |
| 82 | DTConstraint Constraints; |
| 83 | /// accumulated dep-set |
| 84 | DepSet accDep; |
| 85 | /// dep-set for the clash |
| 86 | DepSet& clashDep; |
| 87 | |
| 88 | // local values for the updating |
| 89 | |
| 90 | /// local value for the min/max flag |
| 91 | bool localMin = false; |
| 92 | /// local value for the incl/excl flag |
| 93 | bool localExcl = false; |
| 94 | /// local value for the added value |
| 95 | ComparableDT localValue; |
| 96 | /// local dep-set for the update |
| 97 | DepSet localDep; |
| 98 | |
| 99 | protected: // methods |
| 100 | /// set clash dep-set to DEP, report with given REASON; @return true to simplify callers |
| 101 | bool reportClash ( const DepSet& dep, const char* reason ) |
| 102 | { |
| 103 | if ( LLM.isWritable(llCDAction) ) // level of logging |
| 104 | LL << " DT-" << reason; // inform about clash... |
| 105 | |
| 106 | clashDep = dep; |
| 107 | return true; |
| 108 | } |
| 109 | /// set the local parameters for updating |
| 110 | void setLocal ( bool min, bool excl, const ComparableDT& value, const DepSet& dep ) |
| 111 | { |
| 112 | localMin = min; |
| 113 | localExcl = excl; |
| 114 | localValue = value; |
| 115 | localDep = dep; |
| 116 | } |
| 117 | /// update and add a single interval I to the constraints. @return true iff clash occurs |
| 118 | bool addUpdatedInterval ( DepInterval i ) |
| 119 | { |
| 120 | if ( !i.consistent ( localValue, localDep ) ) // types are mismatch |
| 121 | return reportClash ( localDep, "C-IT" ); |
| 122 | if ( !i.update ( localMin, localExcl, localValue, localDep ) ) |
| 123 | Constraints.push_back(i); |
| 124 | else if ( !hasPType() || !i.checkMinMaxClash(accDep) ) |
| 125 | Constraints.push_back(i); |
| 126 | return false; |
| 127 | } |
| 128 | /// update and add all the intervals from the given range. @return true iff clash occurs |
| 129 | bool addIntervals ( iterator begin, iterator end ) |
| 130 | { |
| 131 | for ( ; begin != end; ++begin ) |
| 132 | if ( addUpdatedInterval(*begin) ) |
| 133 | return true; |
| 134 | return false; |
| 135 | } |
| 136 | /// add interval INT positively to the DTA |
| 137 | bool addPosInterval ( const TDataInterval& Int, const DepSet& dep ); |
| 138 | /// add interval INT negatively to the DTA |
| 139 | bool addNegInterval ( const TDataInterval& Int, const DepSet& dep ); |
| 140 | |
| 141 | public: // methods |
| 142 | /// empty c'tor |
| 143 | explicit DataTypeAppearance ( DepSet& dep ) : clashDep(dep) {} |
| 144 | /// empty d'tor |
| 145 | ~DataTypeAppearance() { delete PType; delete NType; } |
| 146 | |
| 147 | /// clear the appearance flags |
| 148 | void clear ( void ) |
| 149 | { |
| 150 | delete PType; |
| 151 | PType = nullptr; |
| 152 | delete NType; |
| 153 | NType = nullptr; |
| 154 | Constraints.clear(); |
| 155 | Constraints.emplace_back(); |
| 156 | accDep.clear(); |
| 157 | } |
| 158 | |
| 159 | // presence interface |
| 160 | |
| 161 | /// check if type is present positively in the node |
| 162 | bool hasPType ( void ) const { return PType != nullptr; } |
| 163 | /// set the presence of the type depending of polarity (POS) and save a dep-set DEP; @return true if clash was found |
| 164 | bool setTypePresence ( bool pos, const DepSet& dep ) |
| 165 | { |
| 166 | DepSet*& pDep = pos ? PType : NType; |
| 167 | if ( likely(pDep == nullptr) ) // 1st access |
| 168 | pDep = new DepSet(dep); |
| 169 | else // FIXME!! think whether it is necessary to use the LATEST branching point |
| 170 | pDep->add(dep); |
| 171 | |
| 172 | // check the case both pos- and neg types are present |
| 173 | if ( PType != nullptr && NType != nullptr ) |
| 174 | return reportClash ( *PType+*NType, "TNT" ); |
| 175 | return false; |
| 176 | } |
| 177 | |
| 178 | // comparison methods |
| 179 | |
| 180 | /// @return true iff there is at least one point that two DTA share |
| 181 | bool operator == ( const DataTypeAppearance& other ) const; |
| 182 | /// @return true iff there is at least one point in OTHER that there is not in THIS |
| 183 | bool operator < ( const DataTypeAppearance& other ) const; |
| 184 | |
| 185 | // complex methods |
| 186 | |
| 187 | /// add restrictions [POS]INT to intervals |
| 188 | bool addInterval ( bool pos, const TDataInterval& Int, const DepSet& dep ) |
| 189 | { |
| 190 | if ( LLM.isWritable(llCDAction) ) // level of logging |
| 191 | LL << ' ' << (pos ? '+' : '-') << Int; |
| 192 | return pos ? addPosInterval ( Int, dep ) : addNegInterval ( Int, dep ); |
| 193 | } |
| 194 | }; // DataTypeAppearance |
| 195 | |
| 196 | class DataTypeReasoner |
| 197 | { |
| 198 | protected: // types |
| 199 | /// vector of data types |
| 200 | typedef std::vector<DataTypeAppearance*> DTAVector; |
| 201 | /// map from positive BPs (DT pNames) to corresponding data type |
| 202 | typedef std::map<const TDataEntry*,size_t> TypeMap; |
| 203 | |
| 204 | protected: // members |
| 205 | /// vector of a types |
| 206 | DTAVector Types; |
| 207 | /// map Type.pName->Type appearance |
| 208 | TypeMap Map; |
| 209 | /// external DAG |
| 210 | const DLDag& DLHeap; |
| 211 | /// type that has pos-entry |
| 212 | DataTypeAppearance* posType = nullptr; |
| 213 | /// dep-set for the clash for *all* the types |
| 214 | DepSet clashDep; |
| 215 | |
| 216 | protected: // methods |
| 217 | /// process data value |
| 218 | bool processDataValue ( bool pos, const TDataEntry* c, const DepSet& dep ) |
| 219 | { |
| 220 | DataTypeAppearance* type = getDTAbyValue(c); |
| 221 | |
| 222 | if ( pos && setTypePresence ( type, pos, dep ) ) |
| 223 | return true; |
| 224 | |
| 225 | // create interval [c,c] |
| 226 | TDataInterval constraints; |
| 227 | constraints.updateMin ( /*excl=*/false, c->getComp() ); |
| 228 | constraints.updateMax ( /*excl=*/false, c->getComp() ); |
| 229 | return type->addInterval ( pos, constraints, dep ); |
| 230 | } |
| 231 | /// process data expr |
| 232 | bool processDataExpr ( bool pos, const TDataEntry* c, const DepSet& dep ) |
| 233 | { |
| 234 | const TDataInterval& constraints = *c->getFacet(); |
| 235 | if ( constraints.empty() ) |
| 236 | return false; |
| 237 | DataTypeAppearance* type = getDTAbyValue(c); |
| 238 | if ( pos && setTypePresence ( type, pos, dep ) ) |
| 239 | return true; |
| 240 | return type->addInterval ( pos, constraints, dep ); |
| 241 | } |
| 242 | |
| 243 | /// get data entry structure by a BP |
| 244 | const TDataEntry* getDataEntry ( BipolarPointer p ) const |
| 245 | { return static_cast<const TDataEntry*>(DLHeap[p].getConcept()); } |
| 246 | |
| 247 | // get access to proper DataTypeAppearance |
| 248 | |
| 249 | /// get DTA by given data-type pointer |
| 250 | DataTypeAppearance* getDTAbyType ( const TDataEntry* dataType ) |
| 251 | { |
| 252 | # ifdef ENABLE_CHECKING |
| 253 | fpp_assert ( Map.find(dataType) != Map.end() ); |
| 254 | # endif |
| 255 | return Types[Map[dataType]]; |
| 256 | } |
| 257 | /// get DTA by given data-value pointer |
| 258 | DataTypeAppearance* getDTAbyValue ( const TDataEntry* dataValue ) |
| 259 | { |
| 260 | # ifdef ENABLE_CHECKING |
| 261 | fpp_assert ( !dataValue->isBasicDataType() ); |
| 262 | # endif |
| 263 | return getDTAbyType(dataValue->getType()); |
| 264 | } |
| 265 | |
| 266 | bool setTypePresence ( DataTypeAppearance* type, bool pos, const DepSet& dep ) |
| 267 | { |
| 268 | // delegate negative ones to a type |
| 269 | if ( !pos ) |
| 270 | return type->setTypePresence ( /*pos=*/false, dep ); |
| 271 | |
| 272 | // setup pos-type if necessary |
| 273 | if ( posType == nullptr ) |
| 274 | posType = type; |
| 275 | // same type -- nothing to do |
| 276 | if ( posType == type ) |
| 277 | return type->setTypePresence ( /*pos=*/true, dep ); |
| 278 | // the other type: clash straight away |
| 279 | if ( LLM.isWritable(llCDAction) ) // level of logging |
| 280 | LL << " DT-TT"; // inform about clash... |
| 281 | |
| 282 | clashDep = *(posType->PType); |
| 283 | clashDep += dep; |
| 284 | return true; |
| 285 | } |
| 286 | |
| 287 | public: // interface |
| 288 | /// c'tor: save DAG |
| 289 | explicit DataTypeReasoner ( const DLDag& dag ) : DLHeap(dag) {} |
| 290 | /// empty d'tor |
| 291 | ~DataTypeReasoner() |
| 292 | { |
| 293 | for ( DTAVector::iterator p = Types.begin(); p < Types.end(); ++p ) |
| 294 | delete *p; |
| 295 | } |
| 296 | |
| 297 | // managing DTR |
| 298 | |
| 299 | /// add data type to the reasoner |
| 300 | void registerDataType ( const TDataEntry* p ) |
| 301 | { |
| 302 | Map[p] = Types.size(); |
| 303 | Types.push_back(new DataTypeAppearance(clashDep)); |
| 304 | } |
| 305 | /// prepare types for the reasoning |
| 306 | void clear ( void ) |
| 307 | { |
| 308 | for ( DTAVector::iterator p = Types.begin(), p_end = Types.end(); p < p_end; ++p ) |
| 309 | (*p)->clear(); |
| 310 | posType = nullptr; |
| 311 | } |
| 312 | |
| 313 | // comparison methods |
| 314 | |
| 315 | /// @return true iff there is at least one point that two DTA share |
| 316 | bool operator == ( const DataTypeReasoner& other ) const |
| 317 | { |
| 318 | if ( posType == nullptr || other.posType == nullptr ) |
| 319 | return false; |
| 320 | return *posType == *(other.posType); |
| 321 | } |
| 322 | /// @return true iff there is at least one point in OTHER that there is not in THIS |
| 323 | bool operator < ( const DataTypeReasoner& other ) const |
| 324 | { |
| 325 | if ( posType == nullptr || other.posType == nullptr ) |
| 326 | return false; |
| 327 | return *posType < *(other.posType); |
| 328 | } |
| 329 | |
| 330 | // filling structures and getting answers |
| 331 | |
| 332 | /// add data entry to the DTAVector; @return true iff data-data clash was found |
| 333 | bool addDataEntry ( BipolarPointer p, const DepSet& dep ); |
| 334 | /// get clash-set |
| 335 | const DepSet& getClashSet ( void ) const { return clashDep; } |
| 336 | }; // DataTypeReasoner |
| 337 | |
| 338 | |
| 339 | #endif |