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