FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
knowrob/src/KnowledgeBase.cpp at dev · daniel86/knowrob · GitHub
daniel86
/
knowrob
Public
forked from
knowrob/knowrob
Notifications
You must be signed in to change notification settings
Fork
5
Star
1
Code
Issues
0
Pull requests
2
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
knowrob
/
src
/
KnowledgeBase.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
710 lines (637 loc) · 26.1 KB
Breadcrumbs
knowrob
/
src
/
KnowledgeBase.cpp
Copy path
File metadata and controls
710 lines (637 loc) · 26.1 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
/*
* This file is part of KnowRob, please consult
* https://github.com/knowrob/knowrob for license details.
*/
#
include
<
thread
>
#
include
<
boost/property_tree/json_parser.hpp
>
#
include
<
knowrob/Logger.h
>
#
include
<
knowrob/KnowledgeBase.h
>
#
include
<
knowrob/URI.h
>
#
include
<
boost/python/suite/indexing/vector_indexing_suite.hpp
>
#
include
"
knowrob/queries/QueryPipeline.h
"
#
include
"
knowrob/semweb/PrefixRegistry.h
"
#
include
"
knowrob/semweb/rdf.h
"
#
include
"
knowrob/semweb/owl.h
"
#
include
"
knowrob/semweb/rdfs.h
"
#
include
"
knowrob/semweb/OntologyLanguage.h
"
#
include
"
knowrob/reasoner/ReasonerManager.h
"
#
include
"
knowrob/ontologies/OntologyFile.h
"
#
include
"
knowrob/ontologies/GraphTransformation.h
"
#
include
"
knowrob/ontologies/TransformedOntology.h
"
#
include
"
knowrob/integration/python/utils.h
"
#
include
"
knowrob/integration/python/with.h
"
#
define
KB_SETTING_REASONER
"
reasoner
"
#
define
KB_SETTING_DATA_BACKENDS
"
data-backends
"
#
define
KB_SETTING_DATA_SOURCES
"
data-sources
"
#
define
KB_SETTING_DATA_TRANSFORMATION
"
transformation
"
#
define
KB_SETTING_SEMWEB
"
semantic-web
"
#
define
KB_SETTING_PREFIXES
"
prefixes
"
#
define
KB_SETTING_PREFIX_ALIAS
"
alias
"
#
define
KB_SETTING_PREFIX_URI
"
uri
"
using
namespace
knowrob
;
KnowledgeBase::KnowledgeBase
()
: isInitialized_(
false
) {
vocabulary_ = std::make_shared<Vocabulary>();
//
use "system" as default origin until initialization completed
vocabulary_->
importHierarchy
()->
setDefaultGraph
(ImportHierarchy::
ORIGIN_SYSTEM
);
backendManager_ = std::make_shared<StorageManager>(vocabulary_);
reasonerManager_ = std::make_shared<ReasonerManager>(
this
, backendManager_);
edb_ = std::make_shared<StorageInterface>(backendManager_);
}
KnowledgeBase::KnowledgeBase
(
const
boost::property_tree::ptree &config) : KnowledgeBase() {
configure
(config);
init
();
}
KnowledgeBase::KnowledgeBase
(std::string_view configFile) : KnowledgeBase() {
boost::property_tree::ptree config;
//
Test if string is a JSON string or a file path
if
(configFile.
find_first_of
(
'
{
'
) ==
0
) {
std::istringstream
json_stream
(configFile.
data
());
boost::property_tree::read_json
(json_stream, config);
}
else
{
boost::property_tree::read_json
(
URI::resolve
(configFile), config);
}
configure
(config);
init
();
}
KnowledgeBase::~KnowledgeBase
() {
stopReasoner
();
if
(observerManager_) {
observerManager_->
stop
();
observerManager_ =
nullptr
;
}
edb_ =
nullptr
;
backendManager_ =
nullptr
;
reasonerManager_ =
nullptr
;
vocabulary_ =
nullptr
;
}
std::shared_ptr<KnowledgeBase>
KnowledgeBase::create
(
const
boost::property_tree::ptree &config) {
return
std::shared_ptr<KnowledgeBase>(
new
KnowledgeBase
(config));
}
std::shared_ptr<KnowledgeBase>
KnowledgeBase::create
(std::string_view config) {
return
std::shared_ptr<KnowledgeBase>(
new
KnowledgeBase
(config));
}
std::shared_ptr<KnowledgeBase>
KnowledgeBase::create
() {
return
std::shared_ptr<KnowledgeBase>(
new
KnowledgeBase
());
}
void
KnowledgeBase::init
() {
isInitialized_ =
true
;
vocabulary_->
importHierarchy
()->
setDefaultGraph
(ImportHierarchy::
ORIGIN_USER
);
initBackends
();
synchronizeBackends
();
initVocabulary
();
observerManager_ = std::make_shared<ObserverManager>(
getBackendForQuery
());
startReasoner
();
}
void
KnowledgeBase::initBackends
() {
for
(
auto
&pair: backendManager_->
plugins
()) {
auto
definedBackend = pair.
second
;
definedBackend->
value
()->
setVocabulary
(vocabulary_);
}
}
void
KnowledgeBase::synchronizeBackends
() {
//
find all non-persistent backends, which we assume to be empty at this point
std::vector<std::shared_ptr<NamedBackend>> nonPersistent;
for
(
auto
&it: backendManager_->
plugins
()) {
auto
backend = it.
second
->
value
();
auto
queryable = backendManager_->
queryable
().
find
(it.
first
);
if
(queryable == backendManager_->
queryable
().
end
() || !queryable->
second
->
isPersistent
()) {
nonPersistent.
push_back
(it.
second
);
}
}
//
synchronize persistent backends with each other
if
(backendManager_->
persistent
().
size
() >
1
) {
//
find versions of persisted origins
using
BackendOriginVersion = std::pair<std::shared_ptr<QueryableStorage>, VersionedOriginPtr>;
std::map<std::string_view, std::vector<BackendOriginVersion>> origins;
for
(
auto
&it: backendManager_->
persistent
()) {
auto
persistentBackend = it.
second
;
for
(
auto
&origin: persistentBackend->
getOrigins
()) {
origins[origin->
value
()].
emplace_back
(it.
second
, origin);
}
}
//
drop all persisted origins with an outdated version
for
(
auto
&origin_pair: origins) {
auto
&v = origin_pair.
second
;
if
(v.
size
() <
2
)
continue
;
//
find the maximum version
std::string_view maxVersion;
for
(
auto
&version: v) {
if
(!maxVersion.
empty
() && version.
second
->
version
() > maxVersion) {
maxVersion = version.
second
->
version
();
}
}
//
drop origin for backends with outdated version, also remove such backends from "origins" array
v.
erase
(
std::remove_if
(v.
begin
(), v.
end
(),
[&maxVersion](
const
BackendOriginVersion &bov) {
if
(bov.
second
->
version
() != maxVersion) {
bov.
first
->
removeAllWithOrigin
(bov.
second
->
value
());
return
true
;
}
else
{
return
false
;
}
}), v.
end
());
}
//
At this point, origins contains only the backends with the latest version.
//
So data can be copied from any of them into all other persistent backends that do
//
not have the data yet.
for
(
auto
&origin_pair: origins) {
//
First find all persistent backends that do not appear in origin_pair.
//
These are the ones without the data.
std::vector<std::shared_ptr<NamedBackend>> included;
for
(
auto
&it: backendManager_->
persistent
()) {
auto
&persistentBackend = it.
second
;
if
(
std::find_if
(origin_pair.
second
.
begin
(), origin_pair.
second
.
end
(),
[&persistentBackend](
const
BackendOriginVersion &bov) {
return
bov.
first
== persistentBackend;
}) == origin_pair.
second
.
end
()) {
included.
push_back
(backendManager_->
getPluginWithID
(it.
first
));
}
}
if
(included.
empty
())
continue
;
//
Now copy the data from one of the backends in origin_pair to all backends in included.
auto
&persistedBackend = origin_pair.
second
.
begin
()->
first
;
auto
transaction = edb_->
createTransaction
(
persistedBackend,
StorageInterface::Insert,
StorageInterface::Including,
included);
persistedBackend->
batchOrigin
(origin_pair.
first
, [&](
const
TripleContainerPtr &triples) {
transaction->
commit
(triples);
});
}
}
//
insert from first persistent backend into all non-persistent backends.
//
persistent backends are synchronized before, so we can just take the first one.
if
(!backendManager_->
persistent
().
empty
() && !nonPersistent.
empty
()) {
KB_DEBUG
(
"
Synchronizing persistent triples into {} non-persistent backends.
"
, nonPersistent.
size
());
auto
&persistedBackend = *backendManager_->
persistent
().
begin
();
auto
transaction = edb_->
createTransaction
(
getBackendForQuery
(),
StorageInterface::Insert,
StorageInterface::Including,
nonPersistent);
persistedBackend.
second
->
batch
([&](
const
TripleContainerPtr &triples) { transaction->
commit
(triples); });
}
}
void
KnowledgeBase::initVocabulary
() {
auto
v_s = std::make_shared<Variable>(
"
?s
"
);
auto
v_o = std::make_shared<Variable>(
"
?o
"
);
for
(
auto
&it: backendManager_->
persistent
()) {
auto
backend = it.
second
;
//
initialize the import hierarchy
for
(
auto
&origin: backend->
getOrigins
()) {
vocabulary_->
importHierarchy
()->
addDirectImport
(vocabulary_->
importHierarchy
()->
ORIGIN_SYSTEM
,
origin->
value
());
}
//
iterate over all rdf:type assertions and add them to the vocabulary
backend->
match
(
FramedTriplePattern
(v_s, rdf::type, v_o),
[
this
](
const
FramedTriplePtr &triple) {
vocabulary_->
addResourceType
(triple->
subject
(), triple->
valueAsString
());
vocabulary_->
increaseFrequency
(rdf::type->
stringForm
());
});
//
iterate over all rdfs::subClassOf assertions and add them to the vocabulary
backend->
match
(
FramedTriplePattern
(v_s, rdfs::subClassOf, v_o),
[
this
](
const
FramedTriplePtr &triple) {
vocabulary_->
addSubClassOf
(triple->
subject
(), triple->
valueAsString
(), triple->
graph
());
vocabulary_->
increaseFrequency
(rdfs::subClassOf->
stringForm
());
});
//
iterate over all rdfs::subPropertyOf assertions and add them to the vocabulary
backend->
match
(
FramedTriplePattern
(v_s, rdfs::subPropertyOf, v_o),
[
this
](
const
FramedTriplePtr &triple) {
vocabulary_->
addSubPropertyOf
(triple->
subject
(), triple->
valueAsString
(), triple->
graph
());
vocabulary_->
increaseFrequency
(rdfs::subPropertyOf->
stringForm
());
});
//
iterate over all owl::inverseOf assertions and add them to the vocabulary
backend->
match
(
FramedTriplePattern
(v_s, owl::inverseOf, v_o),
[
this
](
const
FramedTriplePtr &triple) {
vocabulary_->
setInverseOf
(triple->
subject
(), triple->
valueAsString
());
vocabulary_->
increaseFrequency
(owl::inverseOf->
stringForm
());
});
//
query number of assertions of each property/class.
//
this is useful information for optimizing the query planner.
std::vector<semweb::PropertyPtr> reifiedProperties;
backend->
count
([
this
, &reifiedProperties](std::string_view resource,
uint64_t
count) {
//
special handling for reified relations: they are concepts, but do also increase the relation counter
auto
reifiedProperty = vocabulary_->
getDefinedReification
(resource);
if
(reifiedProperty) reifiedProperties.
push_back
(reifiedProperty);
vocabulary_->
setFrequency
(resource, count);
});
for
(
auto
&p: reifiedProperties) {
vocabulary_->
increaseFrequency
(p->
iri
());
}
}
}
void
KnowledgeBase::configure
(
const
boost::property_tree::ptree &config) {
configurePrefixes
(config);
//
initialize data backends from configuration
configureBackends
(config);
//
share vocabulary and import hierarchy with backends
initBackends
();
//
load common ontologies
loadCommon
();
//
load the "global" data sources.
//
these are data sources that are loaded into all backends, however
//
the backends may decide to ignore some of the data sources.
configureDataSources
(config);
//
load reasoners from configuration
configureReasoner
(config);
}
void
KnowledgeBase::configurePrefixes
(
const
boost::property_tree::ptree &config) {
auto
semwebTree = config.
get_child_optional
(
KB_SETTING_SEMWEB
);
if
(semwebTree) {
//
load RDF URI aliases
auto
prefixesList = semwebTree.
value
().
get_child_optional
(
KB_SETTING_PREFIXES
);
for
(
const
auto
&pair: prefixesList.
value
()) {
auto
alias = pair.
second
.
get
(
KB_SETTING_PREFIX_ALIAS
,
"
"
);
auto
uri = pair.
second
.
get
(
KB_SETTING_PREFIX_URI
,
"
"
);
if
(!alias.
empty
() && !uri.
empty
()) {
PrefixRegistry::registerPrefix
(alias, uri);
}
else
{
KB_WARN
(
"
Invalid entry in semantic-web::prefixes, 'alias' and 'uri' must be defined.
"
);
}
}
}
}
void
KnowledgeBase::configureBackends
(
const
boost::property_tree::ptree &config) {
auto
backendList = config.
get_child_optional
(
KB_SETTING_DATA_BACKENDS
);
if
(backendList) {
for
(
const
auto
&pair: backendList.
value
()) {
KB_LOGGED_TRY_CATCH
(pair.
first
,
"
load
"
, { backendManager_->
loadPlugin
(pair.
second
); });
}
}
else
{
KB_ERROR
(
"
configuration has no 'backends' key.
"
);
}
}
void
KnowledgeBase::configureReasoner
(
const
boost::property_tree::ptree &config) {
auto
reasonerList = config.
get_child_optional
(
KB_SETTING_REASONER
);
if
(reasonerList) {
for
(
const
auto
&pair: reasonerList.
value
()) {
KB_LOGGED_TRY_CATCH
(pair.
first
,
"
load
"
, {
auto
definedReasoner = reasonerManager_->
loadPlugin
(pair.
second
);
//
if reasoner implements DataBackend class, add it to the backend manager
auto
reasonerBackend = std::dynamic_pointer_cast<Storage>(definedReasoner->
value
());
if
(reasonerBackend) {
backendManager_->
addPlugin
(definedReasoner->
name
(), definedReasoner->
language
(), reasonerBackend);
}
});
}
}
else
{
KB_ERROR
(
"
configuration has no 'reasoner' key.
"
);
}
}
void
KnowledgeBase::loadCommon
() {
for
(
auto
&ontoPath: {
"
owl/rdf-schema.xml
"
,
"
owl/owl.rdf
"
}) {
loadDataSource
(std::make_shared<OntologyFile>(vocabulary_,
URI
(ontoPath),
"
rdf-xml
"
));
}
}
static
void
do_startReasoner
(
const
std::shared_ptr<DataDrivenReasoner> &reasoner) {
if
(reasoner->
reasonerLanguage
() == PluginLanguage::
PYTHON
) {
py::gil_lock lock;
reasoner->
start
();
}
else
{
reasoner->
start
();
}
}
void
KnowledgeBase::startReasoner
() {
std::vector<std::string_view> failedToStartReasoner;
for
(
auto
&pair: reasonerManager_->
dataDriven
()) {
KB_LOGGED_TRY_EXCEPT
(pair.
first
.
data
(),
"
start
"
,
{
do_startReasoner
(pair.
second
); },
{ failedToStartReasoner.
push_back
(pair.
first
); });
}
//
remove reasoner that failed to start
for
(
auto
&reasonerName: failedToStartReasoner) {
reasonerManager_->
removePlugin
(reasonerName);
}
}
void
KnowledgeBase::stopReasoner
() {
for
(
auto
&pair: reasonerManager_->
dataDriven
()) {
KB_LOGGED_TRY_CATCH
(pair.
first
.
data
(),
"
stop
"
, { pair.
second
->
stop
(); });
}
}
QueryableBackendPtr
KnowledgeBase::getBackendForQuery
()
const
{
auto
&queryable = backendManager_->
queryable
();
if
(queryable.
empty
()) {
KB_WARN
(
"
No queryable backends available.
"
);
return
nullptr
;
}
else
{
return
queryable.
begin
()->
second
;
}
}
TokenBufferPtr
KnowledgeBase::submitQuery
(
const
FirstOrderLiteralPtr &literal,
const
QueryContextPtr &ctx) {
return
submitQuery
(std::make_shared<ConjunctiveQuery>(
ConjunctiveQuery
({literal}, ctx)));
}
TokenBufferPtr
KnowledgeBase::submitQuery
(
const
ConjunctiveQueryPtr &conjunctiveQuery) {
auto
pipeline = std::make_shared<QueryPipeline>(
shared_from_this
(), conjunctiveQuery);
//
Wrap output into AnswerBuffer_WithReference object.
//
Note that the AnswerBuffer_WithReference object is used such that the caller can
//
destroy the whole pipeline by de-referencing the returned AnswerBufferPtr.
auto
out = std::make_shared<AnswerBuffer_WithReference>(pipeline);
*pipeline >> out;
pipeline->
stopBuffering
();
return
out;
}
TokenBufferPtr
KnowledgeBase::submitQuery
(
const
FormulaPtr &phi,
const
QueryContextPtr &ctx) {
auto
pipeline = std::make_shared<QueryPipeline>(
shared_from_this
(), phi, ctx);
auto
out = std::make_shared<AnswerBuffer_WithReference>(pipeline);
*pipeline >> out;
pipeline->
stopBuffering
();
return
out;
}
ObserverPtr
KnowledgeBase::observe
(
const
GraphQueryPtr &query,
const
BindingsHandler &callback) {
return
observerManager_->
observe
(query, callback);
}
void
KnowledgeBase::synchronizeObservers
() {
observerManager_->
synchronize
();
}
bool
KnowledgeBase::insertOne
(
const
FramedTriple &triple) {
auto
sourceBackend =
findSourceBackend
(triple);
auto
transaction = edb_->
createTransaction
(
getBackendForQuery
(),
StorageInterface::Insert,
StorageInterface::Excluding,
{sourceBackend});
if
(transaction->
commit
(triple)) {
auto
tripleCopy =
new
FramedTripleCopy
(triple);
std::vector<FramedTriplePtr> triples;
triples.
emplace_back
(tripleCopy);
auto
container = std::make_shared<ProxyTripleContainer>(triples);
observerManager_->
insert
(container);
return
true
;
}
else
{
return
false
;
}
}
bool
KnowledgeBase::insertAll
(
const
TripleContainerPtr &triples) {
auto
sourceBackend =
findSourceBackend
(**triples->
begin
());
auto
transaction = edb_->
createTransaction
(
getBackendForQuery
(),
StorageInterface::Insert,
StorageInterface::Excluding,
{sourceBackend});
if
(transaction->
commit
(triples)) {
observerManager_->
insert
(triples);
return
true
;
}
else
{
return
false
;
}
}
bool
KnowledgeBase::removeOne
(
const
FramedTriple &triple) {
auto
sourceBackend =
findSourceBackend
(triple);
auto
transaction = edb_->
createTransaction
(
getBackendForQuery
(),
StorageInterface::Remove,
StorageInterface::Excluding,
{sourceBackend});
if
(transaction->
commit
(triple)) {
auto
tripleCopy =
new
FramedTripleCopy
(triple);
std::vector<FramedTriplePtr> triples;
triples.
emplace_back
(tripleCopy);
auto
container = std::make_shared<ProxyTripleContainer>(triples);
observerManager_->
remove
(container);
return
true
;
}
else
{
return
false
;
}
}
bool
KnowledgeBase::removeAll
(
const
TripleContainerPtr &triples) {
auto
sourceBackend =
findSourceBackend
(**triples->
begin
());
auto
transaction = edb_->
createTransaction
(
getBackendForQuery
(),
StorageInterface::Remove,
StorageInterface::Excluding,
{sourceBackend});
if
(transaction->
commit
(triples)) {
observerManager_->
remove
(triples);
return
true
;
}
else
{
return
false
;
}
}
bool
KnowledgeBase::insertAll
(
const
std::vector<FramedTriplePtr> &triples) {
//
Note: insertAll blocks until the triples are inserted, so it is safe to use the triples vector as a pointer.
return
insertAll
(std::make_shared<ProxyTripleContainer>(&triples));
}
bool
KnowledgeBase::removeAll
(
const
std::vector<FramedTriplePtr> &triples) {
return
removeAll
(std::make_shared<ProxyTripleContainer>(&triples));
}
bool
KnowledgeBase::removeAllWithOrigin
(std::string_view origin) {
return
edb_->
removeAllWithOrigin
(origin);
}
std::shared_ptr<NamedBackend>
KnowledgeBase::findSourceBackend
(
const
FramedTriple &triple) {
if
(!triple.
graph
())
return
{};
auto
definedBackend_withID = backendManager_->
getPluginWithID
(triple.
graph
().
value
());
if
(definedBackend_withID)
return
definedBackend_withID;
auto
definedReasoner = reasonerManager_->
getPluginWithID
(triple.
graph
().
value
());
if
(definedReasoner) {
auto
reasonerBackend = reasonerManager_->
getReasonerStorage
(definedReasoner);
if
(reasonerBackend) {
for
(
auto
&it: backendManager_->
plugins
()) {
auto
&definedBackend_ofReasoner = it.
second
;
if
(definedBackend_ofReasoner->
value
() == reasonerBackend) {
return
definedBackend_ofReasoner;
}
}
}
}
return
{};
}
void
KnowledgeBase::configureDataSources
(
const
boost::property_tree::ptree &config) {
auto
dataSourcesList = config.
get_child_optional
(
KB_SETTING_DATA_SOURCES
);
if
(!dataSourcesList)
return
;
for
(
const
auto
&pair: dataSourcesList.
value
()) {
auto
&subtree = pair.
second
;
auto
dataSource =
DataSource::create
(vocabulary_, subtree);
if
(!dataSource) {
KB_ERROR
(
"
Failed to create data source
\"
{}
\"
.
"
, pair.
first
);
continue
;
}
bool
has_error;
auto
transformationConfig = config.
get_child_optional
(
KB_SETTING_DATA_TRANSFORMATION
);
if
(transformationConfig) {
if
(dataSource->
dataSourceType
() != DataSourceType::
ONTOLOGY
) {
KB_ERROR
(
"
Transformations can only be applied on ontology data sources.
"
);
continue
;
}
auto
ontology = std::static_pointer_cast<OntologySource>(dataSource);
//
apply a transformation to the data source if "transformation" key is present
auto
transformation =
GraphTransformation::create
(transformationConfig.
value
());
auto
transformed = std::make_shared<TransformedOntology>(
URI
(ontology->
uri
()), ontology->
format
());
transformation->
apply
(*ontology, [&transformed](
const
TripleContainerPtr &triples) {
transformed->
storage
()->
insertAll
(triples);
});
has_error = !
loadDataSource
(transformed);
}
else
{
has_error = !
loadDataSource
(dataSource);
}
if
(has_error) {
KB_ERROR
(
"
Failed to load data source from
\"
{}
\"
.
"
, dataSource->
uri
());
}
}
}
bool
KnowledgeBase::loadDataSource
(
const
DataSourcePtr &source) {
switch
(source->
dataSourceType
()) {
case
DataSourceType::
ONTOLOGY
:
return
loadOntologySource
(std::static_pointer_cast<OntologySource>(source));
case
DataSourceType::
UNSPECIFIED
:
return
loadNonOntologySource
(source);
}
return
false
;
}
std::optional<std::string>
KnowledgeBase::getVersionOfOrigin
(
const
std::shared_ptr<NamedBackend> &definedBackend, std::string_view origin)
const
{
//
check if the origin was loaded before in this session
auto
runtimeVersion = definedBackend->
value
()->
getVersionOfOrigin
(origin);
if
(runtimeVersion)
return
runtimeVersion;
//
otherwise check if the backend is persistent and if so, ask the persistent backend
auto
persistentBackend = backendManager_->
persistent
().
find
(definedBackend->
name
());
if
(persistentBackend != backendManager_->
persistent
().
end
()) {
return
persistentBackend->
second
->
getVersionOfOrigin
(origin);
}
return
{};
}
std::vector<std::shared_ptr<NamedBackend>>
KnowledgeBase::prepareLoad
(std::string_view origin, std::string_view newVersion)
const
{
std::vector<std::shared_ptr<NamedBackend>> backendsToLoad;
for
(
auto
&it: backendManager_->
plugins
()) {
//
check if the ontology is already loaded by the backend,
//
and if so whether it has the right version.
auto
definedBackend = it.
second
;
auto
currentVersion =
getVersionOfOrigin
(definedBackend, origin);
if
(currentVersion.
has_value
()) {
if
(currentVersion.
value
() != newVersion) {
backendsToLoad.
emplace_back
(it.
second
);
definedBackend->
value
()->
removeAllWithOrigin
(origin);
}
}
else
{
backendsToLoad.
emplace_back
(it.
second
);
}
}
return
backendsToLoad;
}
void
KnowledgeBase::finishLoad
(
const
std::shared_ptr<OntologySource> &source, std::string_view origin,
std::string_view newVersion) {
//
update the version triple
for
(
auto
&it: backendManager_->
plugins
()) {
it.
second
->
value
()->
setVersionOfOrigin
(origin, newVersion);
}
for
(
auto
&it: backendManager_->
persistent
()) {
auto
persistentBackend = it.
second
;
persistentBackend->
setVersionOfOrigin
(origin, newVersion);
}
//
add direct import
if
(source->
parentOrigin
().
has_value
()) {
vocabulary_->
importHierarchy
()->
addDirectImport
(source->
parentOrigin
().
value
(), origin);
}
else
{
vocabulary_->
importHierarchy
()->
addDirectImport
(vocabulary_->
importHierarchy
()->
defaultGraph
(), origin);
}
}
bool
KnowledgeBase::loadOntologySource
(
const
std::shared_ptr<OntologySource> &source) {
//
NOLINT(misc-no-recursion)
auto
uri =
URI::resolve
(source->
uri
());
//
Some ontologies may encode version in the URI which we try to extract
//
below. Otherwise, we just use the current day as version causing a re-load every day.
auto
newVersion =
DataSource::getVersionFromURI
(uri);
//
get all backends that do not have the data loaded yet
auto
backendsToLoad =
prepareLoad
(source->
origin
(), newVersion);
if
(backendsToLoad.
empty
()) {
//
data is already loaded
KB_DEBUG
(
"
Ontology at
\"
{}
\"
already loaded.
"
, uri);
return
true
;
}
auto
result = source->
load
([
this
, &backendsToLoad](
const
TripleContainerPtr &triples) {
auto
transaction = edb_->
createTransaction
(
getBackendForQuery
(),
StorageInterface::Insert,
StorageInterface::Including,
backendsToLoad);
transaction->
commit
(triples);
});
if
(!result) {
KB_WARN
(
"
Failed to load ontology
\"
{}
\"
.
"
, uri);
return
false
;
}
finishLoad
(source, source->
origin
(), newVersion);
for
(
auto
&imported: source->
imports
()) {
auto
importedSource = std::make_shared<OntologyFile>(vocabulary_,
URI
(imported), source->
format
());
if
(!
loadOntologySource
(importedSource)) {
KB_WARN
(
"
Failed to load imported ontology
\"
{}
\"
.
"
, imported);
return
false
;
}
}
return
true
;
}
bool
KnowledgeBase::loadNonOntologySource
(
const
DataSourcePtr &source)
const
{
bool
hasHandler =
false
;
bool
allSucceeded =
true
;
for
(
auto
&kg_pair: backendManager_->
plugins
()) {
auto
backend = kg_pair.
second
->
value
();
if
(backend->
hasDataHandler
(source)) {
if
(!backend->
loadDataSource
(source)) {
allSucceeded =
false
;
KB_WARN
(
"
backend '{}' failed to load data source '{}'
"
, kg_pair.
first
, source->
uri
());
}
hasHandler =
true
;
}
}
if
(!hasHandler) {
KB_WARN
(
"
no data handler for data source format '{}'
"
, source->
format
());
}
return
hasHandler && allSucceeded;
}
void
KnowledgeBase::setDefaultGraph
(std::string_view origin) {
vocabulary_->
importHierarchy
()->
setDefaultGraph
(origin);
}
static
std::shared_ptr<KnowledgeBase>
makeKB1
() {
return
KnowledgeBase::create
();
}
static
std::shared_ptr<KnowledgeBase>
makeKB2
(std::string_view settingsFile) {
return
KnowledgeBase::create
(settingsFile);
}
static
std::shared_ptr<KnowledgeBase>
makeKB3
(
const
boost::property_tree::ptree &settingsTree) {
return
KnowledgeBase::create
(settingsTree);
}
namespace
knowrob
::py {
template
<>
void
createType<KnowledgeBase>() {
using
namespace
boost
::python
;
//
these typedefs are necessary to resolve functions with duplicate names which is
//
fine in C++ but not in Python, so they need special handling here.
//
The typedefs are used to explicitly select the mapped method.
using
QueryPredicate =
TokenBufferPtr
(KnowledgeBase::*)(
const
FirstOrderLiteralPtr &,
const
QueryContextPtr &);
using
QueryFormula =
TokenBufferPtr
(KnowledgeBase::*)(
const
FormulaPtr &,
const
QueryContextPtr &);
using
QueryGraph =
TokenBufferPtr
(KnowledgeBase::*)(
const
ConjunctiveQueryPtr &);
using
ContainerAction =
bool
(KnowledgeBase::*)(
const
TripleContainerPtr &);
using
ListAction =
bool
(KnowledgeBase::*)(
const
std::vector<FramedTriplePtr> &);
createType<Vocabulary>();
createType<GraphQuery>();
class_<KnowledgeBase, std::shared_ptr<KnowledgeBase>, boost::noncopyable>
(
"
KnowledgeBase
"
, no_init)
//
hide the "create" functions in Python
.
def
(
"
__init__
"
,
make_constructor
(&makeKB1))
.
def
(
"
__init__
"
,
make_constructor
(&makeKB2))
.
def
(
"
__init__
"
,
make_constructor
(&makeKB3))
.
def
(
"
setDefaultGraph
"
, &KnowledgeBase::setDefaultGraph)
.
def
(
"
vocabulary
"
, &KnowledgeBase::vocabulary, return_value_policy<copy_const_reference>())
.
def
(
"
loadCommon
"
, with<no_gil>(&KnowledgeBase::loadCommon))
.
def
(
"
loadDataSource
"
, with<no_gil>(&KnowledgeBase::loadDataSource))
.
def
(
"
submitQuery
"
, with<no_gil>(
static_cast
<QueryFormula>(&KnowledgeBase::submitQuery)))
.
def
(
"
submitQuery
"
, with<no_gil>(
static_cast
<QueryPredicate>(&KnowledgeBase::submitQuery)))
.
def
(
"
submitQuery
"
, with<no_gil>(
static_cast
<QueryGraph>(&KnowledgeBase::submitQuery)))
.
def
(
"
observe
"
, +[](KnowledgeBase &kb,
const
GraphQueryPtr &query, object &fn) {
no_gil unlock;
return
kb.
observe
(query, [fn](
const
BindingsPtr &bindings) {
//
make sure to lock the GIL before calling the Python function
gil_lock lock;
fn
(bindings);
});
})
.
def
(
"
insertOne
"
, with<no_gil>(&KnowledgeBase::insertOne))
.
def
(
"
insertAll
"
, with<no_gil>(
static_cast
<ContainerAction>(&KnowledgeBase::insertAll)))
.
def
(
"
insertAll
"
, with<no_gil>(
static_cast
<ListAction>(&KnowledgeBase::insertAll)))
.
def
(
"
removeOne
"
, with<no_gil>(&KnowledgeBase::removeOne))
.
def
(
"
removeAll
"
, with<no_gil>(
static_cast
<ContainerAction>(&KnowledgeBase::removeAll)))
.
def
(
"
removeAll
"
, with<no_gil>(
static_cast
<ListAction>(&KnowledgeBase::removeAll)))
.
def
(
"
removeAllWithOrigin
"
, with<no_gil>(&KnowledgeBase::removeAllWithOrigin));
}
}
Back
|
FazBrowse Home
|
New Git URL