| 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 DLVERTEX_H |
| 21 | #define DLVERTEX_H |
| 22 | |
| 23 | #include <vector> |
| 24 | #include <cstring> // memset |
| 25 | #include <iosfwd> |
| 26 | |
| 27 | #include "globaldef.h" |
| 28 | #include "BiPointer.h" |
| 29 | #include "modelCacheInterface.h" |
| 30 | #include "mergeableLabel.h" // for sort inferences |
| 31 | |
| 32 | class DLDag; |
| 33 | class TRole; |
| 34 | class TNamedEntry; |
| 35 | class SaveLoadManager; |
| 36 | |
| 37 | /// different Concept Expression tags |
| 38 | /* |
| 39 | * The one who changing this should always check: |
| 40 | * - Additional fields in the class DLVertex |
| 41 | * => operator == |
| 42 | * => hash functions |
| 43 | * - DLVertex methods omitStat(), getTagName(), Print() |
| 44 | * - DLDag methods *Stat() |
| 45 | * - DLDag methods getIndex(), updateIndex() |
| 46 | * - tree2dag() |
| 47 | * - mergeSorts(v) |
| 48 | * - setRelevant() |
| 49 | * - PrintDagEntry() |
| 50 | * - ToDoPriorMatrix::getIndex() |
| 51 | * - CGLabel::isComplexConcept() |
| 52 | * - prepareCascadedCache() |
| 53 | * - commonTacticBody() |
| 54 | * - DLVertex::Save/Load methods (SaveLoad.cpp) |
| 55 | */ |
| 56 | enum DagTag { |
| 57 | // illegal entry |
| 58 | dtBad = 0, |
| 59 | // operations |
| 60 | dtTop, |
| 61 | dtAnd, |
| 62 | dtForall, |
| 63 | dtLE, |
| 64 | dtIrr, // \neg\exists R.Self |
| 65 | dtProj, // aux vertex with Projection FROM the current node |
| 66 | dtNN, // NN-rule was applied |
| 67 | dtChoose, // choose-rule |
| 68 | |
| 69 | // ID's |
| 70 | dtPConcept, // primitive concept |
| 71 | dtNConcept, // non-primitive concept |
| 72 | dtPSingleton, |
| 73 | dtNSingleton, |
| 74 | dtDataType, |
| 75 | dtDataValue, |
| 76 | dtDataExpr, // data type with restrictions |
| 77 | }; |
| 78 | |
| 79 | /// check whether given DagTag is a primitive named concept-like entity |
| 80 | inline bool isPNameTag ( DagTag tag ) { return (tag == dtPConcept || tag == dtPSingleton); } |
| 81 | /// check whether given DagTag is a non-primitive named concept-like entity |
| 82 | inline bool isNNameTag ( DagTag tag ) { return (tag == dtNConcept || tag == dtNSingleton); } |
| 83 | /// check whether given DagTag is a named concept-like entity |
| 84 | inline bool isCNameTag ( DagTag tag ) { return isPNameTag(tag) || isNNameTag(tag); } |
| 85 | |
| 86 | // define complex switch labels |
| 87 | #define dtConcept dtPConcept: case dtNConcept |
| 88 | #define dtSingleton dtPSingleton: case dtNSingleton |
| 89 | #define dtName dtConcept: case dtSingleton |
| 90 | #define dtData dtDataType: case dtDataValue: case dtDataExpr |
| 91 | |
| 92 | /// interface for the cache of DLVertex |
| 93 | class DLVertexCache |
| 94 | { |
| 95 | protected: // members |
| 96 | /// cache for the positive entry |
| 97 | const modelCacheInterface* pCache = nullptr; |
| 98 | /// cache for the negative entry |
| 99 | const modelCacheInterface* nCache = nullptr; |
| 100 | |
| 101 | public: // interface |
| 102 | /// d'tor |
| 103 | virtual ~DLVertexCache() { delete pCache; delete nCache; } |
| 104 | |
| 105 | // cache interface |
| 106 | |
| 107 | /// return cache wrt positive flag |
| 108 | const modelCacheInterface* getCache ( bool pos ) const { return pos ? pCache : nCache; } |
| 109 | /// set cache wrt positive flag; note that cache is set up only once |
| 110 | void setCache ( bool pos, const modelCacheInterface* p ) |
| 111 | { |
| 112 | if ( pos ) |
| 113 | pCache = p; |
| 114 | else |
| 115 | nCache = p; |
| 116 | } |
| 117 | }; // DLVertexCache |
| 118 | |
| 119 | class DLVertexStatistic |
| 120 | { |
| 121 | public: // types |
| 122 | /// type for a statistic |
| 123 | typedef unsigned short int StatType; |
| 124 | |
| 125 | protected: // members |
| 126 | /// maximal depth, size and frequency of reference of the expression |
| 127 | StatType stat[10]; |
| 128 | |
| 129 | public: // static methods |
| 130 | /// get access to statistic by the depth of a concept |
| 131 | static unsigned int getStatIndexDepth ( bool pos ) { return (pos ? 0 : 1); } |
| 132 | /// get access to statistic by the size of a concept |
| 133 | static unsigned int getStatIndexSize ( bool pos ) { return (pos ? 2 : 3); } |
| 134 | /// get access to statistic by the # of branching rules of a concept |
| 135 | static unsigned int getStatIndexBranch ( bool pos ) { return (pos ? 4 : 5); } |
| 136 | /// get access to statistic by the # of generating rules of a concept |
| 137 | static unsigned int getStatIndexGener ( bool pos ) { return (pos ? 6 : 7); } |
| 138 | /// get access to statistic by the freq of a concept |
| 139 | static unsigned int getStatIndexFreq ( bool pos ) { return (pos ? 8 : 9); } |
| 140 | |
| 141 | public: // interface |
| 142 | /// default c'tor |
| 143 | DLVertexStatistic ( void ) { std::memset ( stat, 0, sizeof(stat) ); } |
| 144 | /// empty d'tor |
| 145 | virtual ~DLVertexStatistic() = default; |
| 146 | |
| 147 | // set methods |
| 148 | |
| 149 | /// add-up all stat values at once by explicit values |
| 150 | void updateStatValues ( StatType d, StatType s, StatType b, StatType g, bool pos ) |
| 151 | { |
| 152 | stat[getStatIndexSize(pos)] += s; |
| 153 | stat[getStatIndexBranch(pos)] += b; |
| 154 | stat[getStatIndexGener(pos)] += g; |
| 155 | if ( d > stat[getStatIndexDepth(pos)] ) |
| 156 | stat[getStatIndexDepth(pos)] = d; |
| 157 | } |
| 158 | /// add-up all values at once by a given vertex |
| 159 | void updateStatValues ( const DLVertexStatistic& v, bool posV, bool pos ) |
| 160 | { updateStatValues ( v.getDepth(posV), v.getSize(posV), v.getBranch(posV), v.getGener(posV), pos ); } |
| 161 | /// increment frequency value |
| 162 | void incFreqValue ( bool pos ) { ++stat[getStatIndexFreq(pos)]; } |
| 163 | |
| 164 | // get methods |
| 165 | |
| 166 | /// general access to a stat value by index |
| 167 | StatType getStat ( unsigned int i ) const { return stat[i]; } |
| 168 | /// general access to a stat value by index |
| 169 | StatType getDepth ( bool pos ) const { return stat[getStatIndexDepth(pos)]; } |
| 170 | /// general access to a stat value by index |
| 171 | StatType getSize ( bool pos ) const { return stat[getStatIndexSize(pos)]; } |
| 172 | /// general access to a stat value by index |
| 173 | StatType getBranch ( bool pos ) const { return stat[getStatIndexBranch(pos)]; } |
| 174 | /// general access to a stat value by index |
| 175 | StatType getGener ( bool pos ) const { return stat[getStatIndexGener(pos)]; } |
| 176 | /// general access to a stat value by index |
| 177 | StatType getFreq ( bool pos ) const { return stat[getStatIndexFreq(pos)]; } |
| 178 | }; // DLVertexStatistic |
| 179 | |
| 180 | /// tag of the vertex and bits and code for efficient DFS algorithms |
| 181 | class DLVertexTagDFS |
| 182 | { |
| 183 | protected: // members |
| 184 | /// main operation in concept expression |
| 185 | // WARNING: the Visual Studio C++ compiler treat this as a signed integer, |
| 186 | // so I've added extra bit to stay in the unsigned field |
| 187 | DagTag Op : 6; // 17 types |
| 188 | /// aux field for DFS in presence of cycles |
| 189 | bool VisitedPos : 1; |
| 190 | /// aux field for DFS in presence of cycles |
| 191 | bool ProcessedPos : 1; |
| 192 | /// true iff node is involved in cycle |
| 193 | bool inCyclePos : 1; |
| 194 | /// aux field for DFS in presence of cycles |
| 195 | bool VisitedNeg : 1; |
| 196 | /// aux field for DFS in presence of cycles |
| 197 | bool ProcessedNeg : 1; |
| 198 | /// true iff node is involved in cycle |
| 199 | bool inCycleNeg : 1; |
| 200 | /// padding |
| 201 | unsigned unused : 4; |
| 202 | |
| 203 | public: // interface |
| 204 | /// default c'tor |
| 205 | explicit DLVertexTagDFS ( DagTag op ) |
| 206 | : Op(op) |
| 207 | , VisitedPos(false) |
| 208 | , ProcessedPos(false) |
| 209 | , inCyclePos(false) |
| 210 | , VisitedNeg(false) |
| 211 | , ProcessedNeg(false) |
| 212 | , inCycleNeg(false) |
| 213 | {} |
| 214 | /// empty d'tor |
| 215 | virtual ~DLVertexTagDFS() = default; |
| 216 | |
| 217 | // tag access |
| 218 | |
| 219 | /// return tag of the CE |
| 220 | DagTag Type ( void ) const { return Op; } |
| 221 | |
| 222 | // DFS-related method |
| 223 | |
| 224 | /// check whether current Vertex is being visited |
| 225 | bool isVisited ( bool pos ) const { return (pos ? VisitedPos : VisitedNeg); } |
| 226 | /// check whether current Vertex is processed |
| 227 | bool isProcessed ( bool pos ) const { return (pos ? ProcessedPos : ProcessedNeg); } |
| 228 | /// set that the node is being visited |
| 229 | void setVisited ( bool pos ) { if ( pos ) VisitedPos = true; else VisitedNeg = true; } |
| 230 | /// set that the node' DFS processing is completed |
| 231 | void setProcessed ( bool pos ) |
| 232 | { |
| 233 | if ( pos ) |
| 234 | { |
| 235 | ProcessedPos = true; |
| 236 | VisitedPos = false; |
| 237 | } |
| 238 | else |
| 239 | { |
| 240 | ProcessedNeg = true; |
| 241 | VisitedNeg = false; |
| 242 | } |
| 243 | } |
| 244 | /// clear DFS flags |
| 245 | void clearDFS ( void ) { ProcessedPos = VisitedPos = ProcessedNeg = VisitedNeg = false; } |
| 246 | /// check whether concept is in cycle |
| 247 | bool isInCycle ( bool pos ) const { return (pos ? inCyclePos : inCycleNeg); } |
| 248 | /// set concept is in cycle |
| 249 | void setInCycle ( bool pos ) { if ( pos ) inCyclePos = true; else inCycleNeg = true; } |
| 250 | }; // DLVertexTagDFS |
| 251 | |
| 252 | /// usage of the particular vertex during reasoning |
| 253 | class DLVertexUsage |
| 254 | { |
| 255 | public: // types |
| 256 | /// type for a statistic |
| 257 | typedef unsigned long UsageType; |
| 258 | |
| 259 | protected: // members |
| 260 | /// usage statistic for pos- and neg occurrences of a vertex |
| 261 | UsageType posUsage = 0; |
| 262 | UsageType negUsage = 0; |
| 263 | |
| 264 | public: // interface |
| 265 | /// empty d'tor |
| 266 | virtual ~DLVertexUsage() = default; |
| 267 | |
| 268 | /// get access to a usage wrt POS |
| 269 | UsageType getUsage ( bool pos ) const { return pos ? posUsage : negUsage; } |
| 270 | /// increment usage of the node |
| 271 | void incUsage ( bool pos ) { if ( pos ) ++posUsage; else ++negUsage; } |
| 272 | }; // DLVertexUsage |
| 273 | |
| 274 | class DLVertexSort |
| 275 | { |
| 276 | protected: // members |
| 277 | /// maximal depth, size and frequency of reference of the expression |
| 278 | mergeableLabel Sort; |
| 279 | |
| 280 | public: // interface |
| 281 | /// empty d'tor |
| 282 | virtual ~DLVertexSort() = default; |
| 283 | |
| 284 | // label access methods |
| 285 | |
| 286 | /// get RW access to the label |
| 287 | mergeableLabel& getSort ( void ) { return Sort; } |
| 288 | /// get RO access to the label |
| 289 | const mergeableLabel& getSort ( void ) const { return Sort; } |
| 290 | /// merge local label to label LABEL |
| 291 | void merge ( mergeableLabel& label ) { Sort.merge(label); } |
| 292 | }; // DLVertexSort |
| 293 | |
| 294 | /// Class for normalised Concept Expressions |
| 295 | class DLVertex |
| 296 | : public DLVertexCache |
| 297 | , public DLVertexStatistic |
| 298 | #ifdef RKG_PRINT_DAG_USAGE |
| 299 | , public DLVertexUsage |
| 300 | #endif |
| 301 | , public DLVertexTagDFS |
| 302 | #ifdef RKG_USE_SORTED_REASONING |
| 303 | , public DLVertexSort |
| 304 | #endif |
| 305 | { |
| 306 | protected: // typedefs |
| 307 | /// base type for array of BPs |
| 308 | typedef std::vector<BipolarPointer> BaseType; |
| 309 | |
| 310 | public: // typedefs |
| 311 | /// RO access to the elements of node |
| 312 | typedef BaseType::const_iterator const_iterator; |
| 313 | /// RO access to the elements of node in reverse order |
| 314 | typedef BaseType::const_reverse_iterator const_reverse_iterator; |
| 315 | |
| 316 | protected: // members |
| 317 | /// set of arguments (CEs, numbers for NR) |
| 318 | BaseType Child; |
| 319 | /// pointer to concept-like entry (for PConcept, etc) |
| 320 | TNamedEntry* Concept = nullptr; |
| 321 | /// pointer to role (for E\A, NR) |
| 322 | const TRole* Role = nullptr; |
| 323 | /// projection role (used for projection op only) |
| 324 | const TRole* ProjRole = nullptr; |
| 325 | /// C if available |
| 326 | BipolarPointer C = bpINVALID; |
| 327 | /// n if available |
| 328 | unsigned int n = 0; |
| 329 | |
| 330 | public: // interface |
| 331 | /// c'tor for Top/CN/And (before adding any operands) |
| 332 | explicit DLVertex ( DagTag op ) |
| 333 | : DLVertexTagDFS(op) |
| 334 | {} |
| 335 | /// c'tor for Refl/Irr |
| 336 | DLVertex ( DagTag op, const TRole* R ) |
| 337 | : DLVertexTagDFS(op) |
| 338 | , Role(R) |
| 339 | {} |
| 340 | /// c'tor for CN/DE; C is an operand |
| 341 | DLVertex ( DagTag op, BipolarPointer c ) |
| 342 | : DLVertexTagDFS(op) |
| 343 | , C(c) |
| 344 | {} |
| 345 | /// c'tor for <= n R_C; and for \A R{n}_C; Note order C, n, R->pointer |
| 346 | DLVertex ( DagTag op, unsigned int m, const TRole* R, BipolarPointer c ) |
| 347 | : DLVertexTagDFS(op) |
| 348 | , Role(R) |
| 349 | , C(c) |
| 350 | , n(m) |
| 351 | {} |
| 352 | /// c'tor for ProjFrom R C ProjR |
| 353 | DLVertex ( const TRole* R, BipolarPointer c, const TRole* ProjR ) |
| 354 | : DLVertexTagDFS(dtProj) |
| 355 | , Role(R) |
| 356 | , ProjRole(ProjR) |
| 357 | , C(c) |
| 358 | {} |
| 359 | /// no copy c'tor |
| 360 | DLVertex ( const DLVertex& ) = delete; |
| 361 | /// no assignment |
| 362 | DLVertex& operator = ( const DLVertex& ) = delete; |
| 363 | |
| 364 | /// compare 2 CEs |
| 365 | bool operator == ( const DLVertex& v ) const |
| 366 | { |
| 367 | return (Type() == v.Type()) && |
| 368 | (Role == v.Role) && |
| 369 | (ProjRole == v.ProjRole) && |
| 370 | (C == v.C) && |
| 371 | (n == v.n) && |
| 372 | (Child == v.Child); |
| 373 | } |
| 374 | /// compare 2 CEs |
| 375 | bool operator != ( const DLVertex& v ) const { return !(*this == v); } |
| 376 | /// return C for concepts/quantifiers/NR vertices |
| 377 | BipolarPointer getC ( void ) const { return C; } |
| 378 | /// return N for the (<= n R) vertex |
| 379 | unsigned int getNumberLE ( void ) const { return n; } |
| 380 | /// return N for the (>= n R) vertex |
| 381 | unsigned int getNumberGE ( void ) const { return n+1; } |
| 382 | /// return STATE for the (\all R{state}.C) vertex |
| 383 | unsigned int getState ( void ) const { return n; } |
| 384 | |
| 385 | /// return pointer to the first concept name of the entry |
| 386 | const_iterator begin ( void ) const { return Child.begin(); } |
| 387 | /// return pointer after the last concept name of the entry |
| 388 | const_iterator end ( void ) const { return Child.end(); } |
| 389 | |
| 390 | /// return pointer to the last concept name of the entry; WARNING!! works for AND only |
| 391 | const_reverse_iterator rbegin ( void ) const { return Child.rbegin(); } |
| 392 | /// return pointer before the first concept name of the entry; WARNING!! works for AND only |
| 393 | const_reverse_iterator rend ( void ) const { return Child.rend(); } |
| 394 | |
| 395 | /// return pointer to Role for the Role-like vertices |
| 396 | const TRole* getRole ( void ) const { return Role; } |
| 397 | /// return pointer to Projection Role for the Projection vertices |
| 398 | const TRole* getProjRole ( void ) const { return ProjRole; } |
| 399 | /// get (RW) TConcept for concept-like fields |
| 400 | TNamedEntry* getConcept ( void ) { return Concept; } |
| 401 | /// get (RO) TConcept for concept-like fields |
| 402 | const TNamedEntry* getConcept ( void ) const { return Concept; } |
| 403 | |
| 404 | /// set TConcept value to entry |
| 405 | void setConcept ( TNamedEntry* p ) { Concept = p; } |
| 406 | /// set a concept (child) to Name-like vertex |
| 407 | void setChild ( BipolarPointer p ) { C = p; } |
| 408 | /// adds a child to 'AND' vertex; returns TRUE if contradiction found |
| 409 | bool addChild ( BipolarPointer p ); |
| 410 | |
| 411 | // methods for choosing ordering in the OR fields |
| 412 | |
| 413 | /// whether statistic's gathering should be omitted due to the type of a vertex |
| 414 | bool omitStat ( bool pos ) const; |
| 415 | /// sort entry using DAG's compare method |
| 416 | void sortEntry ( const DLDag& dag ); |
| 417 | |
| 418 | // output |
| 419 | |
| 420 | /// get text name for CE tag |
| 421 | const char* getTagName ( void ) const; |
| 422 | /// print the whole node |
| 423 | void Print ( std::ostream& o ) const; |
| 424 | |
| 425 | // save/load interface; implementation is in SaveLoad.cpp |
| 426 | |
| 427 | /// save entry |
| 428 | void Save ( SaveLoadManager& m ) const; |
| 429 | /// load entry |
| 430 | void Load ( SaveLoadManager& m ); |
| 431 | }; // DLVertex |
| 432 | |
| 433 | |
| 434 | /// whether statistic's gathering should be omitted due to the type of a vertex |
| 435 | inline bool |
| 436 | DLVertex :: omitStat ( bool pos ) const |
| 437 | { |
| 438 | switch ( Type() ) |
| 439 | { |
| 440 | case dtDataType: |
| 441 | case dtDataValue: |
| 442 | case dtDataExpr: |
| 443 | case dtNN: // no way to get it in expressions |
| 444 | case dtChoose: // same |
| 445 | case dtBad: |
| 446 | case dtTop: |
| 447 | return true; |
| 448 | case dtPConcept: |
| 449 | case dtPSingleton: |
| 450 | case dtProj: |
| 451 | return !pos; |
| 452 | default: |
| 453 | return false; |
| 454 | } |
| 455 | } |
| 456 | |
| 457 | /** |
| 458 | * returns true iff corresponding NRs may clash. |
| 459 | * Clash may appears for (>= n R) and (<= m R) if n > m. |
| 460 | * Since \neg (<= n R) represents (>= (n+1) R), so |
| 461 | * comparison became (n+1) > m, or n >= m |
| 462 | */ |
| 463 | inline bool mayClashNR ( unsigned int geNR, unsigned int leNR ) |
| 464 | { |
| 465 | return geNR >= leNR; |
| 466 | } |
| 467 | |
| 468 | #endif |