Menu

[r12]: / trunk / libtop_engine / tpl_encoder.h  Maximize  Restore  History

Download this file

521 lines (416 with data), 20.7 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
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
#ifndef TPL_ENCODER_
#define TPL_ENCODER_
#include "rdf_rule_core.h"
#include "rdf_date_time.h"
#include "rdf_graph.h"
#include <istream>
#include <sstream>
#include <fstream>
#include <utility>
#include <cstdlib>
#include <boost/algorithm/string/trim.hpp>
#include <boost/algorithm/string/predicate.hpp>
namespace parser {
class tpl_encoder;
typedef std::tr1::shared_ptr<tpl_encoder> tpl_encoder_ptr_type;
class tpl_encoder_model;
typedef std::tr1::shared_ptr<tpl_encoder_model> tpl_encoder_model_ptr_type;
// map of (tpl_encoder_model::name, tpl_encoder_model)
typedef std::tr1::unordered_map<std::string, tpl_encoder_model_ptr_type> tpl_encoder_model_map_type;
namespace internal {
class tpl_encoder_helper;
typedef std::tr1::shared_ptr<tpl_encoder_helper> tpl_encoder_helper_ptr_type;
// data structure used internally between the helper and the public classes
typedef std::tr1::unordered_multimap<std::string, std::string> po_map_t;
typedef std::tr1::unordered_map<std::string, po_map_t> spo_map_t;
/////////////////////////////////////////////////////////////////////////////////////////
// struct model_version
/////////////////////////////////////////////////////////////////////////////////////////
struct model_version
{
model_version():
name("TOP_ENGINE_MODEL"),
version(0),
next_key(0)
{};
std::string name;
unsigned int version;
unsigned int next_key;
};
/////////////////////////////////////////////////////////////////////////////////////////
// class tpl_encoder_helper
//
// Class hold common resources to make encoded buffers of graph smaller.
/////////////////////////////////////////////////////////////////////////////////////////
class tpl_encoder_helper
{
public:
typedef std::tr1::unordered_map<std::string, std::string> str_pool_map_t;
typedef std::tr1::unordered_map<std::string, rdf::index_type> key_index_map_t;
typedef std::tr1::unordered_map<rdf::index_type, std::string> index_key_map_t;
tpl_encoder_helper(
tpl_encoder_model_ptr_type model_p,
rdf::rdf_graph_ptr_type & graph_p,
bool verbose):
m_model_p(model_p),
m_graph_p(graph_p),
m_next_key(0),
m_key_str_map(),
m_str_key_map(),
m_key_index_map(),
m_index_key_map(),
m_key_encoded_index_map(),
m_has_model(true),
m_verbose(verbose)
{};
tpl_encoder_helper(
rdf::rdf_graph_ptr_type & graph_p,
bool verbose):
m_model_p(),
m_graph_p(graph_p),
m_next_key(0),
m_key_str_map(),
m_str_key_map(),
m_key_index_map(),
m_index_key_map(),
m_key_encoded_index_map(),
m_has_model(false),
m_verbose(verbose)
{};
~tpl_encoder_helper()
{};
/////////////////////////////////////////////////////////////////////////////////////////
// set_next_key
/////////////////////////////////////////////////////////////////////////////////////////
void
set_next_key(unsigned int next_key)
{
m_next_key = next_key;
};
/////////////////////////////////////////////////////////////////////////////////////////
// get_next_key
/////////////////////////////////////////////////////////////////////////////////////////
unsigned int
get_last_key()const
{
return m_next_key;
};
/////////////////////////////////////////////////////////////////////////////////////////
// parse_version
/////////////////////////////////////////////////////////////////////////////////////////
std::string::size_type
parse_version(model_version & version, unsigned int & last_key, std::string const& buffer, std::string::size_type ipos)const;
/////////////////////////////////////////////////////////////////////////////////////////
// encode_version
/////////////////////////////////////////////////////////////////////////////////////////
void
encode_version(model_version const& version, std::stringstream & sbuf, unsigned int last_key)const;
/////////////////////////////////////////////////////////////////////////////////////////
// parse_string_pool
/////////////////////////////////////////////////////////////////////////////////////////
std::string::size_type
parse_string_pool(std::string const& buffer, std::string::size_type ipos);
/////////////////////////////////////////////////////////////////////////////////////////
// encode_string_pool
/////////////////////////////////////////////////////////////////////////////////////////
void
encode_string_pool(std::stringstream & sbuf);
/////////////////////////////////////////////////////////////////////////////////////////
// parse_resource_pool
/////////////////////////////////////////////////////////////////////////////////////////
std::string::size_type
parse_resource_pool(std::string const& buffer, std::string::size_type ipos);
/////////////////////////////////////////////////////////////////////////////////////////
// encode_resource_pool
/////////////////////////////////////////////////////////////////////////////////////////
void
encode_resource_pool(std::stringstream & sbuf);
/////////////////////////////////////////////////////////////////////////////////////////
// parse_triples
/////////////////////////////////////////////////////////////////////////////////////////
std::string::size_type
parse_triples(rdf::rdf_graph_ptr_type & graph_p, std::string const& buffer, std::string::size_type ipos);
/////////////////////////////////////////////////////////////////////////////////////////
// encode_triples
/////////////////////////////////////////////////////////////////////////////////////////
void
encode_triples(std::stringstream & sbuf, spo_map_t const& spo_map);
/////////////////////////////////////////////////////////////////////////////////////////
// get_index
//
// returns NULL if key is not found
/////////////////////////////////////////////////////////////////////////////////////////
rdf::index_type
get_index(std::string const& key)const;
/////////////////////////////////////////////////////////////////////////////////////////
// get_str
//
// returns empty str if key not found
/////////////////////////////////////////////////////////////////////////////////////////
std::string
get_str(std::string const& key)const;
/////////////////////////////////////////////////////////////////////////////////////////
// get_key_for
/////////////////////////////////////////////////////////////////////////////////////////
std::string const&
get_key_for(std::string const& str);
std::string const&
get_key_for(rdf::index_type index);
/////////////////////////////////////////////////////////////////////////////////////////
// encode_resource
//
// Encode the index argument and returns the encoded key.
// Add the (key, encoded-resource) pair into m_key_encoded_index_map
/////////////////////////////////////////////////////////////////////////////////////////
std::string const&
encode_resource(rdf::index_type index);
/////////////////////////////////////////////////////////////////////////////////////////
// create_index
/////////////////////////////////////////////////////////////////////////////////////////
rdf::index_type
create_index(unsigned int rb_key, std::string const& name, std::string const& type, std::string const& content);
private:
/////////////////////////////////////////////////////////////////////////////////////////
// get_encoded_name
/////////////////////////////////////////////////////////////////////////////////////////
std::string
get_encoded_name(rdf::index_type index)
{
std::string name = "*";
if(!index->has_local_name()) {
std::string const& iname = index->get_name();
std::string::size_type ipos = iname.find('#');
if(ipos!=std::string::npos && ipos>0) {
std::string const& base_key = get_key_for(iname.substr(0, ipos));
name = base_key + iname.substr(ipos);
} else {
name = get_key_for(index->get_name());
}
}
return name;
};
tpl_encoder_model_ptr_type m_model_p;
rdf::rdf_graph_ptr_type m_graph_p;
unsigned int m_next_key;
str_pool_map_t m_key_str_map;
str_pool_map_t m_str_key_map;
key_index_map_t m_key_index_map;
index_key_map_t m_index_key_map;
str_pool_map_t m_key_encoded_index_map;
bool m_has_model; // is true if helper to tpl_encoder_model, false helper to tpl_encoder
bool m_verbose;
};
}; /* internal namespace */
/////////////////////////////////////////////////////////////////////////////////////////
// class tpl_encoder
//
// Class to encode / decode triples into a string buffer.
// This uses an encoder model (tpl_encoder_model) where common resources, such as predicate,
// classes and commonly used individuals (usually from model) are defined to reduce the
// size of the buffer.
//
// This class is a single use class to encode / decode from / to a graph to / from a string buffer
/////////////////////////////////////////////////////////////////////////////////////////
class tpl_encoder
{
public:
tpl_encoder(
tpl_encoder_model_ptr_type const& tpl_encoder_model_p,
rdf::rdf_graph_ptr_type & graph_p,
bool verbose):
m_tpl_encoder_model_p(tpl_encoder_model_p),
m_helper_p(new internal::tpl_encoder_helper(tpl_encoder_model_p, graph_p, verbose)),
m_graph_p(graph_p),
m_verbose(verbose)
{};
public:
~tpl_encoder()
{};
/////////////////////////////////////////////////////////////////////////////////////////
// get_helper
/////////////////////////////////////////////////////////////////////////////////////////
internal::tpl_encoder_helper_ptr_type
get_helper_ptr() const
{
return m_helper_p;
};
/////////////////////////////////////////////////////////////////////////////////////////
// is_verbose
/////////////////////////////////////////////////////////////////////////////////////////
bool
is_verbose()const
{
return m_verbose;
};
/////////////////////////////////////////////////////////////////////////////////////////
// set_verbose
/////////////////////////////////////////////////////////////////////////////////////////
void
set_verbose(bool verbose)
{
m_verbose = verbose;
};
/////////////////////////////////////////////////////////////////////////////////////////
// decode
//
// Decode all triples from input buffer and assert them into m_graph_p
/////////////////////////////////////////////////////////////////////////////////////////
void
decode(std::string const& buffer);
/////////////////////////////////////////////////////////////////////////////////////////
// encode
//
// Encode all triples in m_graph_p into a string buffer using tpl_encoder_model_name
/////////////////////////////////////////////////////////////////////////////////////////
std::string
encode();
protected:
/////////////////////////////////////////////////////////////////////////////////////////
// add_triple
/////////////////////////////////////////////////////////////////////////////////////////
void
add_triple(internal::spo_map_t & triples, std::string const& skey, std::string const& pkey, std::string const& okey)
{
internal::po_map_t & po_map = triples[skey];
po_map.insert(std::make_pair(pkey, okey));
};
private:
friend class tpl_encoder_model;
tpl_encoder_model_ptr_type m_tpl_encoder_model_p;
internal::tpl_encoder_helper_ptr_type m_helper_p;
rdf::rdf_graph_ptr_type m_graph_p;
bool m_verbose;
};
/////////////////////////////////////////////////////////////////////////////////////////
// class tpl_encoder_model
//
// Class hold common resources to make encoded buffers of graph smaller - hold the encoded metadata graph
/////////////////////////////////////////////////////////////////////////////////////////
class tpl_encoder_model
{
public:
tpl_encoder_model(
rdf::rdf_graph_ptr_type & graph_p,
bool verbose):
m_helper_p(new internal::tpl_encoder_helper(graph_p, verbose)),
m_model_version(),
m_graph_p(graph_p),
m_verbose(verbose)
{};
~tpl_encoder_model()
{};
/////////////////////////////////////////////////////////////////////////////////////////
// is_verbose
/////////////////////////////////////////////////////////////////////////////////////////
bool
is_verbose()const
{
return m_verbose;
};
/////////////////////////////////////////////////////////////////////////////////////////
// set_verbose
/////////////////////////////////////////////////////////////////////////////////////////
void
set_verbose(bool verbose)
{
m_verbose = verbose;
};
/////////////////////////////////////////////////////////////////////////////////////////
// set_model_version
/////////////////////////////////////////////////////////////////////////////////////////
void
set_version(std::string const& name, unsigned int v)
{
m_model_version.name = name;
m_model_version.version = v;
};
/////////////////////////////////////////////////////////////////////////////////////////
// get_version
/////////////////////////////////////////////////////////////////////////////////////////
internal::model_version const&
get_version() const
{
return m_model_version;
};
/////////////////////////////////////////////////////////////////////////////////////////
// get_version_name
/////////////////////////////////////////////////////////////////////////////////////////
std::string const&
get_version_name() const
{
return m_model_version.name;
};
/////////////////////////////////////////////////////////////////////////////////////////
// get_version_number
/////////////////////////////////////////////////////////////////////////////////////////
unsigned int
get_version_number() const
{
return m_model_version.version;
};
/////////////////////////////////////////////////////////////////////////////////////////
// get_helper
/////////////////////////////////////////////////////////////////////////////////////////
internal::tpl_encoder_helper_ptr_type
get_helper_ptr() const
{
return m_helper_p;
};
/////////////////////////////////////////////////////////////////////////////////////////
// decode
//
// Decode string and resource pools from buffer and metadata graph triples
/////////////////////////////////////////////////////////////////////////////////////////
void
decode(std::string const& buffer);
/////////////////////////////////////////////////////////////////////////////////////////
// encode
//
// Encode all resources defined in m_graph and metadata triples from m_graph
// if(graph_p) then use graph_p for the triples, this will create a new version
// NOTE: graph_p must use the same resource_base_map as m_graph_p
/////////////////////////////////////////////////////////////////////////////////////////
std::string
encode(rdf::rdf_graph_ptr_type & graph_p);
std::string
encode()
{
// creating a new helper
m_helper_p = internal::tpl_encoder_helper_ptr_type(new internal::tpl_encoder_helper(m_graph_p, m_verbose));
rdf::rdf_graph_ptr_type graph_p;
return encode(graph_p);
};
protected:
/////////////////////////////////////////////////////////////////////////////////////////
// add_triple
/////////////////////////////////////////////////////////////////////////////////////////
void
add_triple(internal::spo_map_t & triples, std::string const& skey, std::string const& pkey, std::string const& okey)
{
internal::po_map_t & po_map = triples[skey];
po_map.insert(std::make_pair(pkey, okey));
};
private:
internal::tpl_encoder_helper_ptr_type m_helper_p;
internal::model_version m_model_version;
rdf::rdf_graph_ptr_type m_graph_p;
bool m_verbose;
};
/////////////////////////////////////////////////////////////////////////////////////////
// create_tpl_encoder_model
/////////////////////////////////////////////////////////////////////////////////////////
inline tpl_encoder_model_ptr_type
create_tpl_encoder_model(rdf::rdf_graph_ptr_type & graph_p, bool verbose)
{
return tpl_encoder_model_ptr_type(new tpl_encoder_model(graph_p, verbose));
};
/////////////////////////////////////////////////////////////////////////////////////////
// create_tpl_encoder
/////////////////////////////////////////////////////////////////////////////////////////
inline tpl_encoder_ptr_type
create_tpl_encoder(tpl_encoder_model_ptr_type const& encoder_model_p, rdf::rdf_graph_ptr_type & graph_p, bool verbose)
{
return tpl_encoder_ptr_type(new tpl_encoder(encoder_model_p, graph_p, verbose));
};
}; /* parser namespace */
#endif /*TPL_ENCODER_*/
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.