kDB: Knowledge DataBase
Loading...
Searching...
No Matches
CompareRDFTriples.h
1#include <QDebug>
2#include <QList>
3
4#include <clog_print>
5#include <clog_qt>
6
7#include <knowRDF/BlankNode.h>
8#include <knowRDF/Object.h>
9#include <knowRDF/Subject.h>
10#include <knowRDF/Triple.h>
11
12namespace
13{
14 // This comparison function cheat, it should check if it is the same blank node by comparing other
15 // connection. However it is a close enough approximation and cover most cases.
16 // TODO use a better algorithm for that (ie "Matching RDF Graphs", Jeremy J. Carroll, LNCS, volume
17 // 2342
18 bool compare_triple(const knowRDF::Triple& _triple1, const knowRDF::Triple& _triple2)
19 {
20 return _triple1.predicate() == _triple2.predicate()
21 and (_triple1.subject() == _triple2.subject()
22 or (_triple1.subject().type() == knowRDF::Subject::Type::BlankNode
23 and _triple2.subject().type() == knowRDF::Subject::Type::BlankNode))
24 and (_triple1.object() == _triple2.object()
25 or (_triple1.object().type() == knowRDF::Object::Type::BlankNode
26 and _triple2.object().type() == knowRDF::Object::Type::BlankNode));
27 }
28 bool compare_triples(const QList<knowRDF::Triple>& _triples1,
29 const QList<knowRDF::Triple>& _triples2)
30 {
31 if(_triples1.size() != _triples2.size())
32 {
33 clog_error("Different number of triples: {} != {}", _triples1.size(), _triples2.size());
34 return false;
35 }
36
37 QVector<bool> matched(_triples2.size(), false);
38
39 for(const knowRDF::Triple& triple : _triples1)
40 {
41 bool found_one = false;
42 for(int j = 0; j < _triples2.size(); ++j)
43 {
44 if(not matched[j] and compare_triple(triple, _triples2[j]))
45 {
46 matched[j] = true;
47 found_one = true;
48 }
49 }
50 if(not found_one)
51 {
52 clog_error("No match found for triple: {}", triple);
53 clog_print<clog_print_flag::blue>("Possible reference triples were");
54 for(int j = 0; j < _triples2.size(); ++j)
55 {
56 clog_print<clog_print_flag::bold>("{}th: {}", j, _triples2[j]);
57 }
58 clog_print<clog_print_flag::red>("Tested triples were");
59 for(int j = 0; j < _triples1.size(); ++j)
60 {
61 clog_print<clog_print_flag::bold>("{}th: {}", j, _triples1[j]);
62 }
63 return false;
64 }
65 }
66
67 return true;
68 }
69} // namespace
Definition Revision.h:9
Definition Triple.h:19