knowrob  2.1.0
A Knowledge Base System for Cognition-enabled Robots
ReificationContainer.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of KnowRob, please consult
3  * https://github.com/knowrob/knowrob for license details.
4  */
5 
6 #include <utility>
7 
8 #include "knowrob/storage/ReificationContainer.h"
9 #include "knowrob/storage/ReifiedTriple.h"
10 
11 using namespace knowrob;
12 
14  VocabularyPtr vocabulary,
15  ReifiedNames reifiedNames)
16  : originalTriples_(std::move(originalTriples)),
17  vocabulary_(std::move(vocabulary)),
18  reifiedNames_(std::move(reifiedNames)) {
19 }
20 
23  const VocabularyPtr &vocabulary,
24  const ReifiedNames &reifiedNames,
25  uint32_t tripleIndex) {
26  std::shared_ptr<ReifiedTriple> reified;
27  if (reifiedNames && !reifiedNames->empty()) {
28  reified = std::make_shared<ReifiedTriple>(triple, vocabulary, (*reifiedNames)[tripleIndex - 1]);
29  } else {
30  reified = std::make_shared<ReifiedTriple>(triple, vocabulary);
31  }
32  return [reified, it = reified->begin()]() mutable -> const TriplePtr * {
33  if (it == reified->end()) return nullptr;
34  return &*it++;
35  };
36 }
37 
38 namespace knowrob::reification {
39  struct IterationData {
40  IterationData(VocabularyPtr vocabulary, const TripleContainerPtr &originalTriples,
41  ReifiedNames reifiedNames)
42  : vocabulary(std::move(vocabulary)),
43  reifiedGen(nullptr),
44  it(originalTriples->begin()),
45  end(originalTriples->end()),
46  reifiedNames(std::move(reifiedNames)),
47  tripleIndex(0) {
48  }
49 
50  VocabularyPtr vocabulary;
54  ReifiedNames reifiedNames;
55  uint32_t tripleIndex;
56  };
57 }
58 
60  auto data = std::make_shared<reification::IterationData>(
62 
63  return [data]() mutable -> const TriplePtr * {
64  if (data->reifiedGen) {
65  // if a reified triple is available, return it
66  const TriplePtr *nextReified = data->reifiedGen();
67  if (nextReified) return nextReified;
68  else data->reifiedGen = nullptr;
69  }
70  // else process the next triple from the original container
71  if (data->it == data->end) return nullptr;
72  data->tripleIndex += 1;
73  const TriplePtr *next = &*data->it++;
74  if (ReifiedTriple::isReifiable(*next->ptr)) {
75  data->reifiedGen = getReifiedGenerator(*next->ptr, data->vocabulary, data->reifiedNames, data->tripleIndex);
76  return data->reifiedGen();
77  } else {
78  return next;
79  }
80  };
81 }
TripleContainer::ConstGenerator getReifiedGenerator(const Triple &triple, const VocabularyPtr &vocabulary, const ReifiedNames &reifiedNames, uint32_t tripleIndex)
ReificationContainer(TripleContainerPtr originalTriples, VocabularyPtr vocabulary, ReifiedNames reifiedNames)
ConstGenerator cgenerator() const override
static bool isReifiable(const Triple &triple)
std::function< const TriplePtr *()> ConstGenerator
std::shared_ptr< TripleContainer > TripleContainerPtr
std::shared_ptr< std::vector< IRIAtomPtr > > ReifiedNames
std::shared_ptr< Vocabulary > VocabularyPtr
Definition: Vocabulary.h:233