| 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 TODOLIST_H |
| 21 | #define TODOLIST_H |
| 22 | |
| 23 | #include <vector> |
| 24 | |
| 25 | #include "globaldef.h" |
| 26 | #include "fpp_assert.h" |
| 27 | #include "PriorityMatrix.h" |
| 28 | #include "tRareSaveStack.h" |
| 29 | |
| 30 | /// the entry of TODO table |
| 31 | struct ToDoEntry |
| 32 | { |
| 33 | /// node to include concept |
| 34 | DlCompletionTree* Node = nullptr; |
| 35 | /// offset of included concept in Node's label |
| 36 | // (it's not possible to use pointers because |
| 37 | // std::vector invalidates them) |
| 38 | int offset = 0; |
| 39 | |
| 40 | /// empty c'tor |
| 41 | ToDoEntry() = default; |
| 42 | /// init c'tor |
| 43 | ToDoEntry ( DlCompletionTree* n, int off ) : Node(n), offset(off) {} |
| 44 | }; // ToDoEntry |
| 45 | |
| 46 | /// All-in-one version of arrayToDoTable |
| 47 | class ToDoList |
| 48 | { |
| 49 | protected: // classes |
| 50 | /// class for saving/restoring ToDoQueue |
| 51 | class QueueSaveState |
| 52 | { |
| 53 | public: // members |
| 54 | /// save start point of queue of entries |
| 55 | size_t sp = 0; |
| 56 | /// save end point of queue of entries |
| 57 | size_t ep = 0; |
| 58 | }; // QueueSaveState |
| 59 | //-------------------------------------------------------------------------- |
| 60 | |
| 61 | /// class to represent single queue |
| 62 | class arrayQueue |
| 63 | { |
| 64 | protected: // members |
| 65 | /// waiting ops queue |
| 66 | std::vector<ToDoEntry> Wait; |
| 67 | /// start pointer; points to the 1st element in the queue |
| 68 | size_t sPointer = 0; |
| 69 | |
| 70 | protected: // types |
| 71 | /// type for restore the whole queue |
| 72 | class QueueRestorer: public TRestorer |
| 73 | { |
| 74 | protected: // members |
| 75 | /// copy of a queue |
| 76 | std::vector<ToDoEntry> Wait; |
| 77 | /// pointer to a queue to restore |
| 78 | arrayQueue* queue; |
| 79 | /// start pointer |
| 80 | size_t sp; |
| 81 | |
| 82 | public: // interface |
| 83 | /// init c'tor |
| 84 | explicit QueueRestorer ( arrayQueue* q ) : Wait(q->Wait), queue(q), sp(q->sPointer) {} |
| 85 | /// restore: copy the queue back, adjust pointers |
| 86 | void restore () override { std::swap(queue->Wait, Wait); queue->sPointer = sp; } |
| 87 | }; |
| 88 | |
| 89 | public: // interface |
| 90 | /// c'tor: init queue with proper size and reset it |
| 91 | arrayQueue() |
| 92 | { |
| 93 | Wait.reserve(50); // initial size |
| 94 | Wait.clear(); |
| 95 | } |
| 96 | |
| 97 | /// add entry to a queue |
| 98 | virtual void add ( DlCompletionTree* node, int offset ) { Wait.emplace_back(node,offset); } |
| 99 | /// clear queue |
| 100 | void clear ( void ) { sPointer = 0; Wait.clear(); } |
| 101 | /// check if queue empty |
| 102 | bool empty ( void ) const { return sPointer == Wait.size(); } |
| 103 | /// get next entry from the queue; works for non-empty queues |
| 104 | const ToDoEntry* get ( void ) { return &(Wait[sPointer++]); } |
| 105 | |
| 106 | /// save queue content to the given entry |
| 107 | void save ( QueueSaveState& tss ) const |
| 108 | { |
| 109 | tss.sp = sPointer; |
| 110 | tss.ep = Wait.size(); |
| 111 | } |
| 112 | /// restore queue content from the given entry |
| 113 | void restore ( const QueueSaveState& tss ) |
| 114 | { |
| 115 | sPointer = tss.sp; |
| 116 | Wait.resize(tss.ep); |
| 117 | } |
| 118 | }; // arrayQueue |
| 119 | //-------------------------------------------------------------------------- |
| 120 | |
| 121 | /// class to represent single priority queue |
| 122 | class queueQueue: public arrayQueue |
| 123 | { |
| 124 | protected: // members |
| 125 | /// stack to save states for the overwritten queue |
| 126 | TRareSaveStack* stack; |
| 127 | |
| 128 | public: // interface |
| 129 | /// c'tor: make an empty queue |
| 130 | explicit queueQueue ( TRareSaveStack* s ) : arrayQueue(), stack(s) {} |
| 131 | |
| 132 | /// add entry to a queue |
| 133 | void add ( DlCompletionTree* Node, int offset ) override |
| 134 | { |
| 135 | auto nominalLevel = Node->getNominalLevel(); |
| 136 | auto greaterNominalLevel = [=] (const ToDoEntry& entry) { return entry.Node->getNominalLevel() > nominalLevel; }; |
| 137 | if ( empty() || // no problems with empty queue and if no priority clashes |
| 138 | !greaterNominalLevel(Wait.back()) ) |
| 139 | { |
| 140 | Wait.emplace_back(Node,offset); |
| 141 | return; |
| 142 | } |
| 143 | |
| 144 | // here we need to put new entry into a proper place |
| 145 | // this will invalidate the array so save it |
| 146 | stack->push(new QueueRestorer(this)); |
| 147 | // find a place for the new entry |
| 148 | auto i = std::find_if ( Wait.begin()+sPointer, Wait.end(), greaterNominalLevel ); |
| 149 | Wait.emplace(i,Node,offset); |
| 150 | } |
| 151 | }; // queueQueue |
| 152 | //-------------------------------------------------------------------------- |
| 153 | |
| 154 | protected: // internal typedefs |
| 155 | /// typedef for NN-queue (which should support complete S/R) |
| 156 | typedef queueQueue NNQueue; |
| 157 | |
| 158 | protected: // classes |
| 159 | /// class for saving/restoring array TODO table |
| 160 | class SaveState |
| 161 | { |
| 162 | public: // members |
| 163 | /// save state for queueID |
| 164 | QueueSaveState backupID; |
| 165 | /// save state for queueNN |
| 166 | QueueSaveState backupNN; |
| 167 | /// save state of all regular queues |
| 168 | QueueSaveState backup[nRegularOps]; |
| 169 | /// save number-of-entries to do |
| 170 | unsigned int noe = 0; |
| 171 | }; // SaveState |
| 172 | //-------------------------------------------------------------------------- |
| 173 | |
| 174 | protected: // members |
| 175 | /// waiting ops queue for IDs |
| 176 | arrayQueue queueID; |
| 177 | /// waiting ops queue for <= ops in nominal nodes |
| 178 | NNQueue queueNN; |
| 179 | /// waiting ops queues |
| 180 | arrayQueue Wait[nRegularOps]; |
| 181 | /// stack of saved states |
| 182 | TSaveStack<SaveState> SaveStack; |
| 183 | /// priority matrix |
| 184 | const ToDoPriorMatrix& Matrix; |
| 185 | /// number of un-processed entries |
| 186 | unsigned int noe = 0; |
| 187 | |
| 188 | protected: // methods |
| 189 | /// save current TODO table content to given saveState entry |
| 190 | void saveState ( SaveState* tss ) |
| 191 | { |
| 192 | queueID.save(tss->backupID); |
| 193 | queueNN.save(tss->backupNN); |
| 194 | for ( auto i = 0; i < nRegularOps; ++i ) |
| 195 | Wait[i].save(tss->backup[i]); |
| 196 | |
| 197 | tss->noe = noe; |
| 198 | } |
| 199 | /// restore TODO table content from given saveState entry |
| 200 | void restoreState ( const SaveState* tss ) |
| 201 | { |
| 202 | queueID.restore(tss->backupID); |
| 203 | queueNN.restore(tss->backupNN); |
| 204 | for ( auto i = 0; i < nRegularOps; ++i ) |
| 205 | Wait[i].restore(tss->backup[i]); |
| 206 | |
| 207 | noe = tss->noe; |
| 208 | } |
| 209 | |
| 210 | public: |
| 211 | /// init c'tor |
| 212 | ToDoList( const ToDoPriorMatrix& matrix, TRareSaveStack* stack ) : queueNN(stack), Matrix(matrix) {} |
| 213 | /// no copy c'tor |
| 214 | ToDoList( const ToDoList& ) = delete; |
| 215 | /// no assignment |
| 216 | ToDoList& operator = ( const ToDoList& ) = delete; |
| 217 | /// d'tor: delete all entries |
| 218 | ~ToDoList() { clear(); } |
| 219 | |
| 220 | // global methods |
| 221 | |
| 222 | /// clear TODO table |
| 223 | void clear ( void ) |
| 224 | { |
| 225 | queueID.clear(); |
| 226 | queueNN.clear(); |
| 227 | for ( auto& queue: Wait ) |
| 228 | queue.clear(); |
| 229 | |
| 230 | SaveStack.clear(); |
| 231 | noe = 0; |
| 232 | } |
| 233 | /// check if TODO table is empty |
| 234 | bool empty ( void ) const { return !noe; } |
| 235 | |
| 236 | // work with entries |
| 237 | |
| 238 | /// add entry with given NODE and CONCEPT with given OFFSET to the TODO table |
| 239 | void addEntry ( DlCompletionTree* node, DagTag type, BipolarPointer bp, int offset ) |
| 240 | { |
| 241 | auto index = Matrix.getIndex ( type, isPositive(bp), node->isNominalNode() ); |
| 242 | switch ( index ) |
| 243 | { |
| 244 | case nRegularOps: // unused entry |
| 245 | return; |
| 246 | case iId: // ID |
| 247 | queueID.add(node,offset); break; |
| 248 | case iNN: // NN |
| 249 | queueNN.add(node,offset); break; |
| 250 | default: // regular queue |
| 251 | Wait[index].add(node,offset); break; |
| 252 | } |
| 253 | ++noe; |
| 254 | } |
| 255 | /// add entry with given NODE and CONCEPT of a TYPE to the ToDo table |
| 256 | void addEntry ( DlCompletionTree* node, DagTag type, BipolarPointer bp ) |
| 257 | { addEntry ( node, type, bp, node->label().getLast(type) ); } |
| 258 | /// get the next TODO entry. @return NULL if the table is empty |
| 259 | const ToDoEntry* getNextEntry ( void ); |
| 260 | |
| 261 | // save/restore methods |
| 262 | |
| 263 | /// save current state using internal stack |
| 264 | void save ( void ) { saveState(SaveStack.push()); } |
| 265 | /// restore state using internal stack |
| 266 | void restore ( void ) { fpp_assert ( !SaveStack.empty() ); restoreState(SaveStack.pop()); } |
| 267 | /// restore state to the given level using internal stack |
| 268 | void restore ( unsigned int level ) { restoreState(SaveStack.pop(level)); } |
| 269 | }; // ToDoList |
| 270 | |
| 271 | inline const ToDoEntry* ToDoList :: getNextEntry ( void ) |
| 272 | { |
| 273 | #ifdef ENABLE_CHECKING |
| 274 | fpp_assert ( !empty () ); // safety check |
| 275 | #endif |
| 276 | |
| 277 | // decrease amount of elements-to-process |
| 278 | --noe; |
| 279 | |
| 280 | // check ID queue |
| 281 | if ( !queueID.empty() ) |
| 282 | return queueID.get(); |
| 283 | |
| 284 | // check NN queue |
| 285 | if ( !queueNN.empty() ) |
| 286 | return queueNN.get(); |
| 287 | |
| 288 | // check regular queues |
| 289 | for ( auto& queue: Wait ) |
| 290 | if ( !queue.empty() ) |
| 291 | return queue.get(); |
| 292 | |
| 293 | // that's impossible, but still... |
| 294 | return nullptr; |
| 295 | } |
| 296 | |
| 297 | #endif |