| 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 TBRANCHINGCONTEXT_H |
| 21 | #define TBRANCHINGCONTEXT_H |
| 22 | |
| 23 | #include "ConceptWithDep.h" |
| 24 | |
| 25 | class DlCompletionTree; |
| 26 | |
| 27 | /// class for saving branching context of a Reasoner |
| 28 | class BranchingContext |
| 29 | { |
| 30 | public: // members |
| 31 | /// currently processed node |
| 32 | DlCompletionTree* curNode = nullptr; |
| 33 | /// currently processed concept |
| 34 | ConceptWDep curConcept {bpINVALID}; |
| 35 | /// positions of the Used members |
| 36 | size_t pUsedIndex = 0, nUsedIndex = 0; |
| 37 | /// size of a session GCIs vector |
| 38 | size_t SGsize = 0; |
| 39 | /// dependencies for branching clashes |
| 40 | DepSet branchDep; |
| 41 | |
| 42 | public: // interface |
| 43 | /// empty c'tor |
| 44 | BranchingContext() = default; |
| 45 | /// no copy c'tor |
| 46 | BranchingContext ( const BranchingContext& ) = delete; |
| 47 | /// no assignment |
| 48 | BranchingContext& operator = ( const BranchingContext& ) = delete; |
| 49 | /// empty d'tor |
| 50 | virtual ~BranchingContext() = default; |
| 51 | |
| 52 | /// init indices (if necessary) |
| 53 | virtual void init ( void ) {} |
| 54 | /// give the next branching alternative |
| 55 | virtual void nextOption ( void ) {} |
| 56 | }; // BranchingContext |
| 57 | |
| 58 | /// branching context for the OR operations |
| 59 | class BCOr: public BranchingContext |
| 60 | { |
| 61 | public: // types |
| 62 | /// short OR indexes |
| 63 | typedef std::vector<BipolarPointer> OrIndex; |
| 64 | /// short OR index iterator |
| 65 | typedef OrIndex::const_iterator or_iterator; |
| 66 | |
| 67 | public: // members |
| 68 | /// relevant disjuncts (ready to add) |
| 69 | OrIndex applicableOrEntries; |
| 70 | /// current branching index |
| 71 | size_t branchIndex = 0; |
| 72 | |
| 73 | public: // interface |
| 74 | /// init branch index |
| 75 | void init ( void ) override { branchIndex = 0; } |
| 76 | /// give the next branching alternative |
| 77 | void nextOption ( void ) override { ++branchIndex; } |
| 78 | |
| 79 | // access to the fields |
| 80 | |
| 81 | /// check if the current processing OR entry is the last one |
| 82 | bool isLastOrEntry ( void ) const { return applicableOrEntries.size() == branchIndex+1; } |
| 83 | /// 1st element of OrIndex |
| 84 | or_iterator orBeg ( void ) const { return applicableOrEntries.begin(); } |
| 85 | /// current element of OrIndex |
| 86 | or_iterator orCur ( void ) const { return orBeg() + branchIndex; } |
| 87 | }; // BCOr |
| 88 | |
| 89 | /// branching context for the Choose-rule |
| 90 | class BCChoose: public BranchingContext |
| 91 | { |
| 92 | }; // BCChoose |
| 93 | |
| 94 | /// branching context for the NN-rule |
| 95 | class BCNN: public BranchingContext |
| 96 | { |
| 97 | public: // members |
| 98 | /// the value of M used in the NN rule |
| 99 | unsigned int value = 0; |
| 100 | |
| 101 | public: // interface |
| 102 | /// init value |
| 103 | void init ( void ) override { value = 1; } |
| 104 | /// give the next branching alternative |
| 105 | void nextOption ( void ) override { ++value; } |
| 106 | |
| 107 | // access to the fields |
| 108 | |
| 109 | /// check if the NN has no option to process |
| 110 | bool noMoreNNOptions ( unsigned int n ) const { return value > n; } |
| 111 | }; // BCNN |
| 112 | |
| 113 | /// branching context for the LE operations |
| 114 | template <typename T> |
| 115 | class BCLE: public BranchingContext |
| 116 | { |
| 117 | public: // types |
| 118 | /// Cardinality Restriction index type |
| 119 | // TODO: make it 8-bit or so |
| 120 | typedef unsigned short int CRIndex; |
| 121 | /// vector of edges |
| 122 | typedef std::vector<T*> EdgeVector; |
| 123 | |
| 124 | public: // members |
| 125 | /// vector of edges to be merged |
| 126 | EdgeVector ItemsToMerge; |
| 127 | /// index of a edge into which the merge is performing |
| 128 | CRIndex toIndex = 0; |
| 129 | /// index of a merge candidate |
| 130 | CRIndex fromIndex = 0; |
| 131 | |
| 132 | public: // interface |
| 133 | /// init indices |
| 134 | void init ( void ) override |
| 135 | { |
| 136 | toIndex = 0; |
| 137 | fromIndex = 0; |
| 138 | } |
| 139 | /// correct fromIndex after changing |
| 140 | void resetMCI ( void ) { fromIndex = static_cast<CRIndex>(ItemsToMerge.size() - 1); } |
| 141 | /// give the next branching alternative |
| 142 | void nextOption ( void ) override |
| 143 | { |
| 144 | --fromIndex; // get new merge candidate |
| 145 | if ( fromIndex == toIndex ) // nothing more can be mergeable to BI node |
| 146 | { |
| 147 | ++toIndex; // change the candidate to merge to |
| 148 | resetMCI(); |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | // access to the fields |
| 153 | |
| 154 | /// get FROM pointer to merge |
| 155 | T* getFrom ( void ) const { return ItemsToMerge[fromIndex]; } |
| 156 | /// get FROM pointer to merge |
| 157 | T* getTo ( void ) const { return ItemsToMerge[toIndex]; } |
| 158 | /// check if the LE has no option to process |
| 159 | bool noMoreLEOptions ( void ) const { return fromIndex <= toIndex; } |
| 160 | }; // BCLE |
| 161 | |
| 162 | /// branching context for the barrier |
| 163 | class BCBarrier: public BranchingContext |
| 164 | { |
| 165 | }; // BCBarrier |
| 166 | |
| 167 | #endif |