| 1 | /* This file is part of the FaCT++ DL reasoner |
| 2 | Copyright (C) 2013-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 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 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 SAVELOADMANAGER_H |
| 21 | #define SAVELOADMANAGER_H |
| 22 | |
| 23 | #include <string> |
| 24 | #include <iostream> |
| 25 | #include <vector> |
| 26 | #include <map> |
| 27 | |
| 28 | #include "globaldef.h" |
| 29 | #include "eFPPSaveLoad.h" |
| 30 | |
| 31 | class TNamedEntity; |
| 32 | class TNamedEntry; |
| 33 | class TaxonomyVertex; |
| 34 | |
| 35 | class SaveLoadManager |
| 36 | { |
| 37 | protected: // types |
| 38 | // maps pointers and numbers |
| 39 | template <typename T> |
| 40 | class PointerMap |
| 41 | { |
| 42 | protected: // types |
| 43 | /// map int->pointer type |
| 44 | typedef std::vector<const T*> I2PMap; |
| 45 | /// map pointer->int type |
| 46 | typedef std::map<const T*, unsigned int> P2IMap; |
| 47 | |
| 48 | protected: // members |
| 49 | /// map i -> pointer |
| 50 | I2PMap i2p; |
| 51 | /// map pointer -> i |
| 52 | P2IMap p2i; |
| 53 | /// ID of the last recorded NE |
| 54 | unsigned int last = 0; |
| 55 | |
| 56 | protected: // methods |
| 57 | /// @return true if given pointer present in the map |
| 58 | bool in ( const T* p ) const { return p2i.find(p) != p2i.end(); } |
| 59 | /// @return true if given index present in the map |
| 60 | bool in ( unsigned int i ) const { return i < last; } |
| 61 | /// @throw an exception if P is not registered |
| 62 | void ensure ( const T* p ) const { if ( !in(p) ) throw EFPPSaveLoad("Cannot save unregistered pointer"); } |
| 63 | /// @throw an exception if I is not registered |
| 64 | void ensure ( unsigned int i ) const { if ( !in(i) ) throw EFPPSaveLoad("Cannot load unregistered index"); } |
| 65 | |
| 66 | public: // interface |
| 67 | /// clear the maps |
| 68 | void clear ( void ) |
| 69 | { |
| 70 | i2p.clear(); |
| 71 | p2i.clear(); |
| 72 | last = 0; |
| 73 | } |
| 74 | |
| 75 | // populate the map |
| 76 | |
| 77 | /// add an entry |
| 78 | void add ( T* p ) |
| 79 | { |
| 80 | if ( in(p) ) |
| 81 | return; |
| 82 | i2p.push_back(p); |
| 83 | p2i[p] = last++; |
| 84 | } |
| 85 | |
| 86 | // access to the mapped element |
| 87 | |
| 88 | /// get the NE by index I |
| 89 | T* getP ( unsigned int i ) { ensure(i); return const_cast<T*>(i2p[i]); } |
| 90 | /// get the index by NE P |
| 91 | unsigned int getI ( const T* p ) { ensure(p); return p2i[p]; } |
| 92 | }; // PointerMap |
| 93 | |
| 94 | protected: // members |
| 95 | /// name of S/L dir |
| 96 | std::string dirname; |
| 97 | /// file name |
| 98 | std::string filename; |
| 99 | /// input stream pointer |
| 100 | std::istream* ip = nullptr; |
| 101 | /// output stream pointer |
| 102 | std::ostream* op = nullptr; |
| 103 | |
| 104 | // uint <-> named entity map for the current taxonomy |
| 105 | PointerMap<TNamedEntity> eMap; |
| 106 | // uint <-> named entry map for the current taxonomy |
| 107 | PointerMap<TNamedEntry> neMap; |
| 108 | // uint <-> TaxonomyVertex map to update the taxonomy |
| 109 | PointerMap<TaxonomyVertex> tvMap; |
| 110 | |
| 111 | public: // methods |
| 112 | /// init c'tor: remember the S/L name |
| 113 | explicit SaveLoadManager ( const std::string& name ) : dirname(name) { filename = name+".fpp.state"; } |
| 114 | /// empty d'tor |
| 115 | ~SaveLoadManager() |
| 116 | { |
| 117 | delete ip; |
| 118 | delete op; |
| 119 | } |
| 120 | |
| 121 | // context information |
| 122 | |
| 123 | /// @return true if there is some S/L content |
| 124 | bool existsContent ( void ) const; |
| 125 | /// clear all the content corresponding to the manager |
| 126 | void clearContent ( void ) const; |
| 127 | |
| 128 | // set up stream |
| 129 | |
| 130 | /// prepare stream according to INPUT value |
| 131 | void prepare ( bool input ); |
| 132 | /// get an input stream |
| 133 | std::istream& i ( void ) { return *ip; } |
| 134 | /// get an output stream |
| 135 | std::ostream& o ( void ) { return *op; } |
| 136 | /// check whether stream is in a good shape |
| 137 | void checkStream ( void ) const |
| 138 | { |
| 139 | if ( ip && unlikely(!ip->good()) ) |
| 140 | throw EFPPSaveLoad ( filename, /*save=*/false); |
| 141 | if ( op && unlikely(!op->good()) ) |
| 142 | throw EFPPSaveLoad ( filename, /*save=*/true); |
| 143 | } |
| 144 | |
| 145 | // save/load primitives |
| 146 | |
| 147 | /// load a single char from input, throw an exception if it is not a given one |
| 148 | inline void expectChar ( const char C ) |
| 149 | { |
| 150 | char c; |
| 151 | i() >> c; |
| 152 | if ( c != C ) |
| 153 | throw EFPPSaveLoad(C); |
| 154 | } |
| 155 | |
| 156 | // save/load integers |
| 157 | |
| 158 | /// save unsigned integer |
| 159 | inline void saveUInt ( unsigned int n ) { o() << "(" << n << ")"; } |
| 160 | /// save signed integer |
| 161 | inline void saveSInt ( int n ) { o() << "(" << n << ")"; } |
| 162 | /// load unsigned integer |
| 163 | inline unsigned int loadUInt ( void ) |
| 164 | { |
| 165 | unsigned int ret; |
| 166 | expectChar('('); |
| 167 | i() >> ret; |
| 168 | expectChar(')'); |
| 169 | return ret; |
| 170 | } |
| 171 | /// load signed integer |
| 172 | inline int loadSInt ( void ) |
| 173 | { |
| 174 | int ret; |
| 175 | expectChar('('); |
| 176 | i() >> ret; |
| 177 | expectChar(')'); |
| 178 | return ret; |
| 179 | } |
| 180 | |
| 181 | // pointer <-> int related methods |
| 182 | |
| 183 | /// clear all maps |
| 184 | void clearPointerMaps ( void ) |
| 185 | { |
| 186 | neMap.clear(); |
| 187 | eMap.clear(); |
| 188 | tvMap.clear(); |
| 189 | } |
| 190 | /// register named entry together with entity (if available) |
| 191 | void registerE ( const TNamedEntry* p ); |
| 192 | /// register taxonomy vertex |
| 193 | void registerV ( TaxonomyVertex* v ) { tvMap.add(v); } |
| 194 | |
| 195 | /// save Entry pointer |
| 196 | void savePointer ( const TNamedEntry* p ) { saveUInt(neMap.getI(p)); } |
| 197 | /// save Entity pointer |
| 198 | void savePointer ( const TNamedEntity* p ) { saveUInt(eMap.getI(const_cast<TNamedEntity*>(p))); } |
| 199 | /// save Vertex pointer |
| 200 | void savePointer ( const TaxonomyVertex* p ) { saveUInt(tvMap.getI(const_cast<TaxonomyVertex*>(p))); } |
| 201 | |
| 202 | /// load Entry pointer |
| 203 | TNamedEntry* loadEntry ( void ) { return neMap.getP(loadUInt()); } |
| 204 | /// load Entity pointer |
| 205 | TNamedEntity* loadEntity ( void ) { return eMap.getP(loadUInt()); } |
| 206 | /// load Vertex pointer |
| 207 | TaxonomyVertex* loadVertex ( void ) { return tvMap.getP(loadUInt()); } |
| 208 | }; // SaveLoadManager |
| 209 | |
| 210 | #endif |