forked from RGNC/plingua
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserialization.hpp
More file actions
457 lines (372 loc) · 13.3 KB
/
serialization.hpp
File metadata and controls
457 lines (372 loc) · 13.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
#ifndef _SERIALIZATION_HPP_
#define _SERIALIZATION_HPP_
#include <iostream>
#include <algorithm>
#include <cstring>
#include "cereal/types/vector.hpp"
#include "cereal/types/map.hpp"
#include "cereal/types/set.hpp"
#include "cereal/types/string.hpp"
namespace plingua {
// VALUE CLASS
class Value {
public:
enum Type {CHAR, UCHAR, SHORT, USHORT, INT, UINT, LONG, DOUBLE, STRING};
Value() : type_(INT) {this->operator =(0);}
Value(long value) : type_(INT) {this->operator =(value);}
Value(double value) : type_(INT) {this->operator =(value);}
Value(const char* value) : type_(INT) {this->operator =(value);}
Value(const std::string& value) : type_(INT) {this->operator =(value);}
Value(const Value& value): type_(INT) {this->operator =(value);}
~Value() {clear();}
Type type() const {return type_;}
char as_char() const {return value_.charValue;}
unsigned char as_uchar() const {return value_.ucharValue;}
short as_short() const {return value_.shortValue;}
unsigned short as_ushort() const {return value_.ushortValue;}
int as_int() const {return value_.intValue;}
unsigned int as_uint() const {return value_.uintValue;}
long as_long() const {return value_.longValue;}
double as_double() const {return value_.doubleValue;}
const char* as_string() const {return value_.stringValue;}
long cast_long() const {
switch(type_) {
case CHAR: return (long)(value_.charValue);
case UCHAR: return (long)(value_.ucharValue);
case SHORT: return (long)(value_.shortValue);
case USHORT: return (long)(value_.ushortValue);
case INT: return (long)(value_.intValue);
case UINT: return (long)(value_.uintValue);
case LONG: return value_.longValue;
default: return 0;
}
}
Value& operator =(char value);
Value& operator =(unsigned char value);
Value& operator =(short value);
Value& operator =(unsigned short value);
Value& operator =(int value);
Value& operator =(unsigned int value);
Value& operator =(long value);
Value& operator =(double value);
Value& operator =(const char* value);
Value& operator =(const std::string& value);
Value& operator =(const Value& other);
template<class A> void save(A& archive) const;
template<class A> void load(A& archive);
private:
void clear();
Type type_;
union {
char charValue;
unsigned char ucharValue;
short shortValue;
unsigned short ushortValue;
int intValue;
unsigned int uintValue;
long longValue;
double doubleValue;
char* stringValue;
} value_;
};
class Multiplicity {
public:
Multiplicity();
Multiplicity(std::size_t multiplicity);
Multiplicity& operator =(std::size_t multiplicity);
Multiplicity operator ++(int);
Multiplicity& operator ++();
Multiplicity& operator +=(const Multiplicity& other);
bool operator ==(const Multiplicity& other) const;
bool operator <(const Multiplicity& other) const;
std::size_t raw() const;
template<class A> void save(A& archive) const;
template<class A> void load(A& archive);
private:
std::size_t multiplicity;
};
class String
{
public:
String() {}
String(const std::string& str) : str_(str) {}
String(const char* s) : str_(s) {}
virtual ~String() {}
String& operator =(const String& other) {str_ = other.str_; return *this;}
String& operator =(const std::string& other) {str_ = other; return *this;}
String& operator =(const char* s) {str_ = s; return *this;}
const std::string& str() const {return str_;}
std::string& str() {return str_;}
virtual bool operator ==(const String& other) const {return str_ == other.str_;}
virtual bool operator !=(const String& other) const {return str_ != other.str_;}
virtual bool operator <(const String& other) const {return str_ < other.str_;}
template<class A> void save(A& archive) const;
template<class A> void load(A& archive);
private:
std::string str_;
};
class LabelString : public String
{
public:
LabelString() {}
LabelString(const std::string& str) : String(str) {}
LabelString(const char* s) : String(s) {}
virtual ~LabelString() {}
template<class A> void save(A& archive) const;
template<class A> void load(A& archive);
};
class ObjectString : public String
{
public:
ObjectString() {}
ObjectString(const std::string& str) : String(str) {}
ObjectString(const char* s) : String(s) {}
virtual ~ObjectString() {}
template<class A> void save(A& archive) const;
template<class A> void load(A& archive);
};
class FeatureString : public String
{
public:
FeatureString() {}
FeatureString(const std::string& str) : String(str) {}
FeatureString(const char* s) : String(s) {}
virtual ~FeatureString() {}
template<class A> void save(A& archive) const;
template<class A> void load(A& archive);
};
typedef std::vector<LabelString> Label;
typedef std::map<ObjectString,Multiplicity> Multiset;
typedef std::map<FeatureString,Value> Features;
// EXTENDED VECTOR CLASS
template<class T>
class ExtendedVector {
public:
std::vector<T> data;
bool operator==(const ExtendedVector<T>& other) const;
bool operator<(const ExtendedVector<T>& other) const;
};
// LEAF MEMBRANE CLASS
class LeafMembrane {
public:
char charge; // charge < 0; charge == 0; charge > 0;
Label label;
LeafMembrane();
const char* getChargeSymbol() const;
bool operator==(const LeafMembrane& other) const;
bool operator<(const LeafMembrane& other) const;
template<class A> void serialize(A& archive);
};
// MEMBRANE CLASS
class Membrane : public LeafMembrane , public ExtendedVector<Membrane> {
public:
bool operator==(const Membrane& other) const;
bool operator<(const Membrane& other) const;
template<class A> void serialize(A& archive);
};
// INNER MEMBRANE CLASS
class IMembrane : public LeafMembrane {
public:
Multiset multiset;
bool operator ==(const IMembrane& other) const;
bool operator <(const IMembrane& other) const;
template<class A> void serialize(A& archive);
};
// OUTER MEMBRANE CLASS (ONLY FOR RULES)
class OMembrane : public IMembrane, public ExtendedVector<IMembrane> {
public:
bool operator==(const OMembrane& other) const;
bool operator<(const OMembrane& other) const;
template<class A> void serialize(A& archive);
};
// HAND RULE CLASS
class HR {
public:
Multiset multiset;
bool operator ==(const HR& other) const;
bool operator <(const HR& other) const;
template<class A> void serialize(A& archive);
};
// LEFT-HAND RULE CLASS
class LHR : public HR {
public:
OMembrane membrane;
bool operator==(const LHR& other) const;
bool operator<(const LHR& other) const;
template<class A> void serialize(A& archive);
};
// RIGHT-HAND RULE CLASS
class RHR : public HR, public ExtendedVector<OMembrane> {
public:
bool operator==(const RHR& other) const;
bool operator<(const RHR& other) const;
template<class A> void serialize(A& archive);
};
// RULE CLASS
class Rule {
public:
LHR lhr;
char arrow; // 0 is --> ; 1 is <--> ;
RHR rhr;
Features features; // extension features (probability, priority, regular expression, etc...)
Rule () ;
bool operator==(const Rule& other) const;
bool operator<(const Rule& other) const;
template<class A> void serialize(A& archive);
};
// SEMANTICS CLASS
class Semantics{
public:
std::size_t value;
bool inf;
std::set<String> patterns;
std::vector<Semantics> children;
Semantics() : value(0),inf(true) {}
void getAllPatterns(std::set<String>& allPatterns) const;
bool operator==(const Semantics& other) const;
bool operator<(const Semantics& other) const;
template<class A> void serialize(A& archive);
};
// CONFIGURATION MEMBRANE
class CMembrane : public IMembrane
{
public:
int parent; // index of the parent membrane (-1 if root membrane)
std::vector<int> children; // indexes of the child membranes
long priorityLevel;
Semantics semantics;
CMembrane() : parent(-1), priorityLevel(0) {}
template<class A> void serialize(A& archive);
};
// P SYSTEM CONFIGURATION
class Configuration
{
public:
unsigned long time; // current time stamp, 0 for initial configuration.
Multiset environment; // environment multiset
std::vector<CMembrane> membranes; // All the membranes in the configuration
Configuration() : time(0) {}
void clear();
template<class A> void serialize(A& archive);
};
// P SYSTEM CLASS
class Psystem {
public:
String model; // model
Membrane structure; // initial membrane structure
std::map<Label, Multiset> multisets; // initial multisets
std::set<Rule> rules; // rules
Semantics semantics; // semantics
Features features; // extension features (multienvironment, confluent, etc...)
template<class A> void save(A& archive) const;
template<class A> void load(A& archive);
};
// P SYSTEM FILE CLASS
class File {
public:
std::string header;
std::string version;
Psystem psystem;
template<class A> void serialize(A& archive);
};
// UID CLASS
class UId {
public:
UId();
template<class A> UId(A& archive, std::size_t max);
UId(std::size_t id, std::size_t max);
UId& operator =(std::size_t id);
void set(std::size_t id, std::size_t max);
std::size_t getMax() const {return max;}
std::size_t getId() const {return id;}
bool operator ==(const UId& other) const;
bool operator <(const UId& other) const;
template<class A> void save(A& archive) const;
private:
std::size_t max;
std::size_t id;
};
// ALPHABET CLASS
class Alphabet {
public:
Alphabet(Alphabet const&) = delete;
void operator=(Alphabet const&) = delete;
virtual ~Alphabet() {}
static Alphabet& getInstance() {
static Alphabet singleton;
return singleton;
}
#define ALPHABET Alphabet::getInstance()
const UId& getObjectId(const std::string& object) const;
const UId& getLabelId(const std::string& label) const;
const UId& getFeatureId(const std::string& feature) const;
const UId& getStringId(const std::string& str) const;
const std::string& getObject(std::size_t id) const;
const std::string& getLabel(std::size_t id) const;
const std::string& getFeature(std::size_t id) const;
const std::string& getString(std::size_t id) const;
std::size_t getObjectAlphabetSize() const;
std::size_t getLabelAlphabetSize() const;
std::size_t getFeatureAlphabetSize() const;
std::size_t getStringsAlphabetSize() const;
std::size_t getMaxMultiplicity() const;
template<class A> void save(A& archive) const;
template<class A> void load(A& archive);
void load(const Psystem& psystem);
private:
Alphabet() {}
void addMultiset(const Multiset& multiset);
void addRule(const Rule& rule);
void addLabel(const Label& label);
void addMembrane(const Membrane& membrane);
void sort();
std::map<std::string,UId> objects;
std::vector<std::string> objects_array;
std::map<std::string,UId> labels;
std::vector<std::string> labels_array;
std::map<std::string,UId> features;
std::vector<std::string> features_array;
std::map<std::string,UId> strings;
std::vector<std::string> strings_array;
std::size_t maxMultiplicity;
};
template<class T>
void loadFromJsonFile(const std::string& path, T& data, const std::string& root = "file");
template<class T>
void loadFromXmlFile(const std::string& path, T& data, const std::string& root = "file");
template<class T>
void loadFromBinaryFile(const std::string& path, T& data, const std::string& root = "file");
template<class T>
void loadFromPortableBinaryFile(const std::string& path, T& data, const std::string& root = "file");
template<class T>
void loadFromFile(const std::string& path, T& data, const std::string& root = "file");
template<class T>
void saveToJsonFile(const std::string& path, const T& data, const std::string& root = "file");
template<class T>
void saveToXmlFile(const std::string& path, const T& data, const std::string& root = "file");
template<class T>
void saveToBinaryFile(const std::string& path, const T& data, const std::string& root = "file");
template<class T>
void saveToPortableBinaryFile(const std::string& path, const T&data, const std::string& root = "file");
template<class T>
void saveToFile(const std::string& path, const T& data, const std::string& root = "file");
}
std::ostream& operator <<(std::ostream& os, const plingua::Multiplicity& arg);
std::ostream& operator <<(std::ostream& os, const plingua::String& arg);
std::ostream& operator <<(std::ostream& os, const plingua::Value& arg);
std::ostream& operator <<(std::ostream& os, const plingua::Label& arg);
std::ostream& operator <<(std::ostream& os, const plingua::Multiset& arg);
std::ostream& operator <<(std::ostream& os, const plingua::LeafMembrane& arg);
std::ostream& operator <<(std::ostream& os, const plingua::Membrane& arg);
std::ostream& operator <<(std::ostream& os, const plingua::IMembrane& arg);
std::ostream& operator <<(std::ostream& os, const plingua::OMembrane& arg);
std::ostream& operator <<(std::ostream& os, const plingua::HR& arg);
std::ostream& operator <<(std::ostream& os, const plingua::LHR& arg);
std::ostream& operator <<(std::ostream& os, const plingua::RHR& arg);
std::ostream& operator <<(std::ostream& os, const plingua::Rule& arg);
std::ostream& operator <<(std::ostream& os, const plingua::Semantics& arg);
std::ostream& operator <<(std::ostream& os, const plingua::Psystem& arg);
std::ostream& operator <<(std::ostream& os, const plingua::File& arg);
std::ostream& operator <<(std::ostream& os, const plingua::Configuration& arg);
#include "serialization.tcc"
#endif