-
Notifications
You must be signed in to change notification settings - Fork 0
/
rml_extractor.cpp
645 lines (560 loc) · 21.8 KB
/
rml_extractor.cpp
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
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
#include "rml_extractor.h"
#include <stdexcept>
#include "rml_uris.h"
// Predefined list of valid primary language subtags
// https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes
std::unordered_set<std::string> valid_language_subtags = {
"en", // English
"es", // Spanish
"fr", // French
"de", // German
"zh", // Chinese
"it", // Italian
"ja", // Japanese
"ko", // Korean
"no", // Norwegian
"pt", // Portuguese
"ru", // Russian
"ar", // Arabic
"cs", // Czech
"da", // Danish
"nl", // Dutch
"fi", // Finnish
"el", // Modern Greek
"hi", // Hindi
"hu", // Hungarian
"ro", // Romanian
};
/**
* @brief Retrieves subject map data from a vector of NTriple objects.
*
* @param rml_triples Vector of NTriple objects containing the RDF triples to be
* searched.
* @param tripleMap_node String representing the node in the RDF triples to be
* matched.
* @return SubjectMapInfo struct containing all found information.
*
* @note This function will enter an infinite loop if any inconsistencies are
* found.
*/
SubjectMapInfo extract_rml_info_of_subjectMap(
const std::vector<NTriple>& rml_triples,
const std::string& tripleMap_node) {
// -> Each triples map has exactly 1 subjectMap
SubjectMapInfo subjectMapInfo;
std::vector<std::string> temp_result;
std::string node_uri; // Uri of SubjectMap Object
// Initialize with default
subjectMapInfo.termType = IRI_TERM_TYPE;
subjectMapInfo.constant = "";
subjectMapInfo.reference = "";
subjectMapInfo.template_str = "";
subjectMapInfo.graph_template = "";
subjectMapInfo.graph_constant = "";
subjectMapInfo.graph_termType = IRI_TERM_TYPE;
subjectMapInfo.name_triplesMap_node = tripleMap_node;
// Get blank node of subjectMap
temp_result =
find_matching_object(rml_triples, tripleMap_node, RML_SUBJECT_MAP);
if (temp_result.empty()) {
throw std::runtime_error(
"Runtime error occurred. Error: No subjectMap blank node found!");
} else if (temp_result.size() > 1) {
throw std::runtime_error(
"Runtime error occurred. More than one subjectMap blank node found!");
} else {
// Assign the found source to source
node_uri = temp_result[0];
}
// Get subjectMap constant
temp_result = find_matching_object(rml_triples, node_uri, RML_CONSTANT);
if (temp_result.size() == 1) {
subjectMapInfo.constant = temp_result[0];
}
// Get subjectMap reference
temp_result = find_matching_object(rml_triples, node_uri, RML_REFERENCE);
if (temp_result.size() == 1) {
subjectMapInfo.reference = temp_result[0];
}
// Get subjectMap template
temp_result = find_matching_object(rml_triples, node_uri, RML_TEMPLATE);
if (temp_result.size() == 1) {
subjectMapInfo.template_str = temp_result[0];
}
// Get subjectMap termType
temp_result = find_matching_object(rml_triples, node_uri, RML_TERM_TYPE);
// If match is found store it -> else default is used
if (!temp_result.empty()) {
subjectMapInfo.termType = temp_result[0];
}
// Get subjectMap graphMap
// Can contain rr:constant, rr:template anbd rr:termType
// Get subjectMap graph blank node
temp_result = find_matching_object(rml_triples, node_uri, RML_GRAPH_MAP);
// Only if blank node is found a graph is specified
if (!temp_result.empty()) {
std::string subjectMap_graph_blank_node = temp_result[0];
// Get graph constant
temp_result = find_matching_object(rml_triples, subjectMap_graph_blank_node,
RML_CONSTANT);
if (!temp_result.empty()) {
subjectMapInfo.graph_constant = temp_result[0];
}
// Get graph template
temp_result = find_matching_object(rml_triples, subjectMap_graph_blank_node,
RML_TEMPLATE);
if (!temp_result.empty()) {
subjectMapInfo.graph_template = temp_result[0];
}
// Get graph termType
temp_result = find_matching_object(rml_triples, subjectMap_graph_blank_node,
RML_TERM_TYPE);
if (!temp_result.empty()) {
subjectMapInfo.graph_termType = temp_result[0];
}
}
return subjectMapInfo;
}
/**
* @brief Retrieves predicate map data from a vector of NTriple objects.
*
* @param rml_triples Vector of NTriple objects containing the RDF triples to be
* searched.
* @param predicateObjectMap_uri String representing the node in the RDF triples
* to be matched.
* @return PredicateMapInfo struct containing all found information.
*
* @note This function will enter an infinite loop if any inconsistencies are
* found.
*/
PredicateMapInfo extract_rml_info_of_predicateMap(
const std::vector<NTriple>& rml_triples,
const std::string& predicateObjectMap_uri) {
std::vector<std::string> temp_result;
//// Handle predicateMap ////
PredicateMapInfo predicateMapInfo;
// Initialize with default
predicateMapInfo.constant = "";
predicateMapInfo.template_str = "";
predicateMapInfo.reference = "";
// Get Uri of predicateMap
std::string node_uri;
temp_result = find_matching_object(rml_triples, predicateObjectMap_uri,
RML_PREDICATE_MAP);
if (temp_result.empty()) {
throw std::runtime_error(
"Runtime error occurred. No predicateMap uri/blank node found!");
} else if (temp_result.size() > 1) {
throw std::runtime_error(
"Runtime error occurred. More than one predicateMap uri/blank node "
"found!");
} else {
// Assign the found source to source
node_uri = temp_result[0];
}
// Get predicateMap constant
temp_result = find_matching_object(rml_triples, node_uri, RML_CONSTANT);
if (temp_result.size() == 1) {
predicateMapInfo.constant = temp_result[0];
}
// Get predicateMap template
temp_result = find_matching_object(rml_triples, node_uri, RML_TEMPLATE);
if (temp_result.size() == 1) {
predicateMapInfo.template_str = temp_result[0];
}
// Get predicateMap reference
temp_result = find_matching_object(rml_triples, node_uri, RML_REFERENCE);
if (temp_result.size() == 1) {
predicateMapInfo.reference = temp_result[0];
}
return predicateMapInfo;
}
PredicateObjectMapInfo extract_rml_info_of_predicateObjectMap(
const std::vector<NTriple>& rml_triples,
const std::string& predicateObjectMap_uri) {
std::vector<std::string> temp_result;
//// Handle predicateMap ////
PredicateObjectMapInfo predicateObjectMapInfo;
// Initialize with default
predicateObjectMapInfo.graph_constant = "";
predicateObjectMapInfo.graph_template = "";
predicateObjectMapInfo.graph_termType = IRI_TERM_TYPE;
// Get predicateObjectMap graphMap
// Can contain rr:constant, rr:template anbd rr:termType
// Get predicateObjectMap graph blank node
temp_result =
find_matching_object(rml_triples, predicateObjectMap_uri, RML_GRAPH_MAP);
// Only if blank node is found a graph is specified
if (!temp_result.empty()) {
std::string subjectMap_graph_blank_node = temp_result[0];
// Get graph constant
temp_result = find_matching_object(rml_triples, subjectMap_graph_blank_node,
RML_CONSTANT);
if (!temp_result.empty()) {
predicateObjectMapInfo.graph_constant = temp_result[0];
}
// Get graph template
temp_result = find_matching_object(rml_triples, subjectMap_graph_blank_node,
RML_TEMPLATE);
if (!temp_result.empty()) {
predicateObjectMapInfo.graph_template = temp_result[0];
}
// Get graph termType
temp_result = find_matching_object(rml_triples, subjectMap_graph_blank_node,
RML_TERM_TYPE);
if (!temp_result.empty()) {
predicateObjectMapInfo.graph_termType = temp_result[0];
}
}
return predicateObjectMapInfo;
}
/**
* @brief Retrieves object map data from a vector of NTriple objects.
*
* @param rml_triples Vector of NTriple objects containing the RDF triples to be
* searched.
* @param objectObjectMap_uri String representing the node in the RDF triples to
* be matched.
* @return PredicateMapInfo struct containing all found information.
*
*/
ObjectMapInfo extract_rml_info_of_objectMap(
const std::vector<NTriple>& rml_triples,
const std::string& objectObjectMap_uri) {
std::vector<std::string> temp_result;
//// Handle objectMap ////
ObjectMapInfo objectMapInfo;
// Initialize with default
objectMapInfo.constant = "";
objectMapInfo.template_str = "";
objectMapInfo.termType = "";
objectMapInfo.reference = "";
objectMapInfo.language = "";
objectMapInfo.parentRef = "";
objectMapInfo.parentSource = "";
objectMapInfo.parent = "";
objectMapInfo.child = "";
objectMapInfo.dataType = "";
objectMapInfo.dataType_template = "";
objectMapInfo.dataType_child = ""; // TODO: Extract child datatype
objectMapInfo.join_reference_condition_available = "";
// Get Uri of objectMap
std::string node_uri;
temp_result =
find_matching_object(rml_triples, objectObjectMap_uri, RML_OBJECT_MAP);
if (temp_result.empty()) {
throw std::runtime_error(
"Runtime error occurred. No objectMap uri/blank node found!");
} else if (temp_result.size() > 1) {
throw std::runtime_error(
"Runtime error occurred. More than one objectMap uri/blank node "
"found!");
} else {
// Assign the found source to source
node_uri = temp_result[0];
}
// Get objectMap constant
temp_result = find_matching_object(rml_triples, node_uri, RML_CONSTANT);
if (temp_result.size() == 1) {
objectMapInfo.constant = temp_result[0];
}
// Get objectMap template
temp_result = find_matching_object(rml_triples, node_uri, RML_TEMPLATE);
if (temp_result.size() == 1) {
objectMapInfo.template_str = temp_result[0];
}
// Get objectMap reference
temp_result = find_matching_object(rml_triples, node_uri, RML_REFERENCE);
if (temp_result.size() == 1) {
objectMapInfo.reference = temp_result[0];
}
// Get objectMap parentSource
temp_result = find_matching_object(rml_triples, node_uri, PARENT_SOURCE);
if (temp_result.size() == 1) {
// Check if source is a blanknode
std::vector<std::string> temp_result2 =
find_matching_object(rml_triples, temp_result[0], SD_NAME);
if (temp_result2.empty()) {
// Assign the found source to source
objectMapInfo.parentSource = temp_result[0];
objectMapInfo.parent_in_memory_name = "";
} else {
// Handle in-memory structure
objectMapInfo.parent_in_memory_name = temp_result2[0];
objectMapInfo.parentSource = "";
}
}
// Get objectMap parentSourceReferrence
temp_result =
find_matching_object(rml_triples, node_uri, PARENT_REFERENCE_FORMULATION);
if (temp_result.size() == 1) {
objectMapInfo.parentRef = temp_result[0];
}
// Get objectMap parent key
temp_result = find_matching_object(rml_triples, node_uri, RML_PARENT);
if (temp_result.size() == 1) {
objectMapInfo.parent = temp_result[0];
}
// Get objectMap child key
temp_result = find_matching_object(rml_triples, node_uri, RML_CHILD);
if (temp_result.size() == 1) {
objectMapInfo.child = temp_result[0];
}
// Get objectMap join_reference_condition
temp_result =
find_matching_object(rml_triples, node_uri, JOIN_REFERENCE_CONDITION);
if (temp_result.size() == 1) {
objectMapInfo.join_reference_condition_available = temp_result[0];
}
// Get objectMap data type
temp_result = find_matching_object(rml_triples, node_uri, RML_DATA_TYPE_MAP);
if (temp_result.size() == 1) {
std::vector<std::string> query_result;
// Get constant
query_result =
find_matching_object(rml_triples, temp_result[0], RML_CONSTANT);
if (query_result.size() == 1) {
objectMapInfo.dataType = query_result[0];
} else {
// Get Template
query_result =
find_matching_object(rml_triples, temp_result[0], RML_TEMPLATE);
if (query_result.size() == 1) {
objectMapInfo.dataType_template = query_result[0];
}
}
}
// Get objectMap language
temp_result = find_matching_object(rml_triples, node_uri, RML_LANGUAGE_MAP);
if (temp_result.size() == 1) {
// Query for constant -> temp_result changes here!
temp_result =
find_matching_object(rml_triples, temp_result[0], RML_CONSTANT);
if (temp_result.size() == 1) {
std::string lang_tag = temp_result[0];
// split en-US -> en
std::size_t pos = temp_result[0].find_first_of('-');
if (pos != std::string::npos) {
lang_tag = temp_result[0].substr(0, pos);
}
// Check if value is supported
if (valid_language_subtags.find(lang_tag) ==
valid_language_subtags.end()) {
throw std::runtime_error(
"Runtime error occurred. Language tag is not supported!");
}
objectMapInfo.language = lang_tag;
}
}
// Set default value for object termType
// TODO: Handle following logic:
// If objectMap is reference based -> literal
if (objectMapInfo.reference != "") {
objectMapInfo.termType = LITERAL_TERM_TYPE;
}
// If objectMap is constant it can be IRI or literal
else if (objectMapInfo.constant != "") {
// Check if objectMapInfo.constant starts with "http"
if (objectMapInfo.constant.substr(0, 4) == "http") {
objectMapInfo.termType = IRI_TERM_TYPE;
} else {
objectMapInfo.termType = LITERAL_TERM_TYPE;
}
}
// If objectMap has rml:languageMap and/or rr:language -> literal
// If objectMap has rr:datatype -> literal
// else -> IRI
else {
objectMapInfo.termType = IRI_TERM_TYPE;
}
// Get objectMap termType and overwrite default value
temp_result = find_matching_object(rml_triples, node_uri, RML_TERM_TYPE);
if (temp_result.size() == 1) {
objectMapInfo.termType = temp_result[0];
}
return objectMapInfo;
}
/**
* @brief Retrieves logical source data including reference formulation and
* source from a vector of NTriple objects.
*
* @param rml_triples Vector of NTriple objects containing the RDF triples to be
* searched.
* @param tripleMap_node String representing the node in the RDF triples to be
* matched.
* @return LogicalSourceInfo struct containing all found information.
* and the source as the second element.
*
* @note This function will enter an infinite loop if any inconsistencies are
* found or if an unsupported reference formulation is identified.
*/
std::string extract_file_path(const std::string& input) {
std::string prefix = "<file://";
std::string suffix = ">";
// Check if the string starts with the prefix and ends with the suffix
if (input.substr(0, prefix.size()) == prefix &&
input.substr(input.size() - suffix.size()) == suffix) {
// Remove the prefix and suffix
return input.substr(prefix.size(),
input.size() - prefix.size() - suffix.size());
} else {
throw std::runtime_error(
"Runtime error occurred. String has unsupported format.");
return "";
}
}
LogicalSourceInfo extract_rml_info_of_source_data(
const std::vector<NTriple>& rml_triples,
const std::string& tripleMap_node) {
std::vector<std::string>
temp_result; // Temporary vector to store intermediate results
LogicalSourceInfo temp_logicalSourceInfo; // Store gained information
std::string logicalSource_uri;
std::string reference_formulation;
// Find matching object for logical source
temp_result =
find_matching_object(rml_triples, tripleMap_node, RML_LOGICAL_SOURCE);
if (temp_result.empty()) {
throw std::runtime_error(
"Runtime error occurred. No logical source found!");
} else if (temp_result.size() > 1) {
throw std::runtime_error(
"Runtime error occurred. More than one logical source found!");
}
logicalSource_uri = temp_result[0];
// Find matching object for source
temp_result =
find_matching_object(rml_triples, logicalSource_uri, RML_SOURCE);
if (temp_result.empty()) {
throw std::runtime_error("Runtime error occurred. No source specified!");
} else if (temp_result.size() > 1) {
throw std::runtime_error(
"Runtime error occurred. More than one source definition found!");
}
std::string rml_source_object_node = temp_result[0];
// Get type
temp_result =
find_matching_object(rml_triples, rml_source_object_node, RDF_TYPE);
if (temp_result.empty()) {
throw std::runtime_error("Runtime error occurred. No type found!");
} else if (temp_result.size() > 1) {
throw std::runtime_error(
"Runtime error occurred. More than one type found!");
}
std::string rml_path_type = temp_result[0];
// Get root
temp_result =
find_matching_object(rml_triples, rml_source_object_node, RML_ROOT);
if (temp_result.empty()) {
throw std::runtime_error("Runtime error occurred. No root found!");
} else if (temp_result.size() > 1) {
throw std::runtime_error(
"Runtime error occurred. More than one root found!");
}
std::string rml_root_uri = temp_result[0];
// Get path
temp_result =
find_matching_object(rml_triples, rml_source_object_node, RML_PATH);
if (temp_result.empty()) {
throw std::runtime_error("Runtime error occurred. No path found!");
} else if (temp_result.size() > 1) {
throw std::runtime_error(
"Runtime error occurred. More than one path found!");
}
std::string rml_path_uri = temp_result[0];
// Build path
if (rml_path_type == "http://w3id.org/rml/RelativePathSource") {
if (rml_root_uri == "http://w3id.org/rml/MappingDirectory") {
temp_logicalSourceInfo.source_path = "./" + rml_path_uri;
}
}
// Find matching object for reference formulation
temp_result = find_matching_object(rml_triples, logicalSource_uri,
RML_REFERENCE_FORMULATION);
if (temp_result.empty()) {
throw std::runtime_error(
"Runtime error occurred. No reference_formulation found!");
} else if (temp_result.size() > 1) {
throw std::runtime_error(
"Runtime error occurred. More than one reference_formulation found!");
} else {
temp_logicalSourceInfo.reference_formulation = temp_result[0];
}
// Return the reference_formulation and source as a pair
return temp_logicalSourceInfo;
}
void parse_rml_rules(
const std::vector<NTriple>& rml_triple,
const std::vector<std::string>& tripleMap_nodes,
const std::string& base_uri,
std::vector<LogicalSourceInfo>& logicalSourceInfo_of_tripleMaps,
std::vector<SubjectMapInfo>& subjectMapInfo_of_tripleMaps,
std::vector<std::vector<PredicateMapInfo>>& predicateMapInfo_of_tripleMaps,
std::vector<std::vector<ObjectMapInfo>>& objectMapInfo_of_tripleMaps,
std::vector<std::vector<PredicateObjectMapInfo>>&
predicateObjectMapInfo_of_tripleMaps) {
// Handle all tripleMaps
for (size_t i = 0; i < tripleMap_nodes.size(); i++) {
// Initialize current tripleMap_node
std::string tripleMap_node = tripleMap_nodes[i];
///////////////////////////////////////////////
// Get information about the subject mapping
///////////////////////////////////////////////
SubjectMapInfo subjectMapInfo =
extract_rml_info_of_subjectMap(rml_triple, tripleMap_node);
// Add base uri
subjectMapInfo.base_uri = base_uri;
subjectMapInfo_of_tripleMaps.push_back(subjectMapInfo);
//////////////////////////
//// Handle Predicate ////
//////////////////////////
// Get all predicateObjectMap Uris of current tripleMap_node
std::vector<std::string> predicateObjectMap_uris = find_matching_object(
rml_triple, tripleMap_node, RML_PREDICATE_OBJECT_MAP);
// Stores all extracted inforation of all predicateMaps in current tripleMap
std::vector<PredicateMapInfo> predicateMapInfos;
// Iterate over all found predicateObjectMap_uris and extract predicate
for (const std::string& predicateObjectMap_uri : predicateObjectMap_uris) {
PredicateMapInfo predicateMapInfo =
extract_rml_info_of_predicateMap(rml_triple, predicateObjectMap_uri);
predicateMapInfos.push_back(predicateMapInfo);
}
// Add generated vector of predicateMaps in current tripleMap to vector
// outside
predicateMapInfo_of_tripleMaps.push_back(predicateMapInfos);
//////////////////////////////////////
// Handle PredicateObjectMap graph //
/////////////////////////////////////
std::vector<PredicateObjectMapInfo> predicateObjectMapInfos;
// Iterate over all found predicateObjectMap_uris and extract predicate
for (const std::string& predicateObjectMap_uri : predicateObjectMap_uris) {
PredicateObjectMapInfo predicateObjectMapInfo =
extract_rml_info_of_predicateObjectMap(rml_triple,
predicateObjectMap_uri);
predicateObjectMapInfos.push_back(predicateObjectMapInfo);
}
// Add generated vector of predicateMaps in current tripleMap to vector
// outside
predicateObjectMapInfo_of_tripleMaps.push_back(predicateObjectMapInfos);
///////////////////////
//// Handle Object ////
///////////////////////
// Stores all extracted inforation of all objectMaps in current tripleMap
std::vector<ObjectMapInfo> objectMapInfos;
// Iterate over all found objectObjectMap_uris and extract predicate
for (const std::string& predicateObjectMap_uri : predicateObjectMap_uris) {
ObjectMapInfo objectMapInfo =
extract_rml_info_of_objectMap(rml_triple, predicateObjectMap_uri);
objectMapInfo.base_uri = base_uri; // base_uri
objectMapInfos.push_back(objectMapInfo);
}
// Add generated vector of objectMaps in current tripleMap to vector outside
objectMapInfo_of_tripleMaps.push_back(objectMapInfos);
///////////////////////////////////////////////
// Get meta data of mapping
///////////////////////////////////////////////
LogicalSourceInfo logicalSourceInfo =
extract_rml_info_of_source_data(rml_triple, tripleMap_node);
logicalSourceInfo_of_tripleMaps.push_back(logicalSourceInfo);
}
}