| 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 PRIORITYMATRIX_H |
| 21 | #define PRIORITYMATRIX_H |
| 22 | |
| 23 | #include "eFaCTPlusPlus.h" |
| 24 | #include "dlVertex.h" // DagTag |
| 25 | #include "logging.h" |
| 26 | |
| 27 | typedef unsigned short int ToDoListIndex; |
| 28 | |
| 29 | /// number of regular options (o- and NN-rules are not included) |
| 30 | constexpr ToDoListIndex nRegularOps = 7; |
| 31 | /// priority index for o- and ID operations (note that these ops have the highest priority) |
| 32 | constexpr ToDoListIndex iId = nRegularOps+1; |
| 33 | /// priority index for <= operation in nominal node |
| 34 | constexpr ToDoListIndex iNN = nRegularOps+2; |
| 35 | |
| 36 | /// Auxiliary class to get priorities on operations |
| 37 | class ToDoPriorMatrix |
| 38 | { |
| 39 | protected: // members |
| 40 | // regular operation indexes |
| 41 | ToDoListIndex iAnd = nRegularOps, |
| 42 | iOr = nRegularOps, |
| 43 | iExists = nRegularOps, |
| 44 | iForall = nRegularOps, |
| 45 | iLE = nRegularOps, |
| 46 | iGE = nRegularOps; |
| 47 | |
| 48 | public: // interface |
| 49 | /// init priorities via given string OPTIONS |
| 50 | void initPriorities ( const std::string& options, const char* optionName ); |
| 51 | /// get an index corresponding given Op, Sign and NominalNode |
| 52 | ToDoListIndex getIndex ( DagTag Op, bool Sign, bool NominalNode ) const; |
| 53 | }; // ToDoPriorMatrix |
| 54 | |
| 55 | inline void ToDoPriorMatrix :: initPriorities ( const std::string& options, const char* optionName ) |
| 56 | { |
| 57 | // check for correctness |
| 58 | if ( options.size () != 7 ) |
| 59 | throw EFaCTPlusPlus ( "ToDo List option string should have length 7" ); |
| 60 | |
| 61 | // get int priority value out of a char |
| 62 | auto char2int = [] (const char c) { return static_cast<ToDoListIndex>(c - '0'); }; |
| 63 | |
| 64 | // init values by symbols loaded |
| 65 | iAnd = char2int(options[1]); |
| 66 | iOr = char2int(options[2]); |
| 67 | iExists = char2int(options[3]); |
| 68 | iForall = char2int(options[4]); |
| 69 | iLE = char2int(options[5]); |
| 70 | iGE = char2int(options[6]); |
| 71 | |
| 72 | // correctness checking |
| 73 | if ( iAnd >= nRegularOps || |
| 74 | iOr >= nRegularOps || |
| 75 | iExists >= nRegularOps || |
| 76 | iForall >= nRegularOps || |
| 77 | iGE >= nRegularOps || |
| 78 | iLE >= nRegularOps ) |
| 79 | throw EFaCTPlusPlus ( "ToDo List option out of range" ); |
| 80 | |
| 81 | // inform about used rules order |
| 82 | if ( LLM.isWritable(llAlways) ) |
| 83 | LL << "\nInit " << optionName << " = " << iAnd << iOr << iExists << iForall << iLE << iGE; |
| 84 | } |
| 85 | |
| 86 | inline ToDoListIndex ToDoPriorMatrix :: getIndex ( DagTag Op, bool Sign, bool NominalNode ) const |
| 87 | { |
| 88 | switch ( Op ) |
| 89 | { |
| 90 | case dtAnd: |
| 91 | return (Sign?iAnd:iOr); |
| 92 | |
| 93 | case dtForall: |
| 94 | case dtIrr: // process local (ir-)reflexivity as a FORALL |
| 95 | return (Sign?iForall:iExists); |
| 96 | |
| 97 | case dtProj: // it should be the lowest priority but now just OR's one |
| 98 | case dtChoose: // probably should be highest branching one |
| 99 | return iOr; |
| 100 | |
| 101 | case dtLE: |
| 102 | return (Sign?(NominalNode?iNN:iLE):iGE); |
| 103 | |
| 104 | case dtDataType: |
| 105 | case dtDataValue: |
| 106 | case dtDataExpr: |
| 107 | case dtNN: |
| 108 | case dtTop: // no need to process these ops |
| 109 | return nRegularOps; |
| 110 | |
| 111 | case dtPSingleton: |
| 112 | case dtPConcept: // no need to process neg of PC |
| 113 | return (Sign?iId:nRegularOps); |
| 114 | |
| 115 | case dtNSingleton: |
| 116 | case dtNConcept: // both NC and neg NC are processed |
| 117 | return iId; |
| 118 | |
| 119 | default: // safety check |
| 120 | fpp_unreachable(); |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | #endif |