knowrob  2.1.0
A Knowledge Base System for Cognition-enabled Robots
GraphTransformation.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 "knowrob/semweb/GraphTransformation.h"
7 #include "knowrob/Logger.h"
8 #include "knowrob/semweb/GraphRenaming.h"
9 #include "knowrob/semweb/GraphRestructuring.h"
10 
11 #define TRANSFORMATION_SETTING_KEY_TYPE "type"
12 #define TRANSFORMATION_SETTING_TYPE_RENAMING "renaming"
13 #define TRANSFORMATION_SETTING_TYPE_RESTRUCTURING "restructuring"
14 #define TRANSFORMATION_SETTING_TYPE_RULES "rules"
15 
16 using namespace knowrob;
17 
19  if (nextTransformation_) {
20  nextTransformation_->pushInputTriples(triples);
21  } else if (next_) {
22  next_(triples);
23  } else {
24  KB_WARN("No next transformation or handler set");
25  }
26 }
27 
29  if (nextTransformation_) {
30  nextTransformation_->initializeTransformation();
31  }
32 }
33 
35  if (nextTransformation_) {
36  nextTransformation_->finalizeTransformation();
37  }
38 }
39 
40 void GraphTransformation::apply(OntologySource &ontologySource, const TripleHandler &callback) {
41  // set the callback for the last transformation in the chain
42  std::shared_ptr<GraphTransformation> last;
43  GraphTransformation *current = this;
44  while (current->nextTransformation_) {
45  last = current->nextTransformation_;
46  current = last.get();
47  }
48  if (last) {
49  last->setNext(callback);
50  } else {
51  setNext(callback);
52  }
53 
55  ontologySource.load([this](const TripleContainerPtr &triples) {
56  pushInputTriples(triples);
57  });
59 }
60 
61 std::shared_ptr<GraphTransformation> GraphTransformation::create(const boost::property_tree::ptree &config) {
62  std::shared_ptr<GraphTransformation> last, first;
63 
64  // iterate over list of transformation options
65  for (const auto &elem_pair: config) {
66  auto &elem = elem_pair.second;
67 
68  auto type_opt = elem.get_optional<std::string>(TRANSFORMATION_SETTING_KEY_TYPE);
69  if (!type_opt) {
70  KB_ERROR("No \"{}\" key specified in graph transformation settings.", TRANSFORMATION_SETTING_KEY_TYPE);
71  continue;
72  }
73  auto type_name = *type_opt;
74 
75  std::shared_ptr<GraphTransformation> next;
76  if (type_name == TRANSFORMATION_SETTING_TYPE_RULES ||
78  next = std::make_shared<GraphRestructuring>();
79  } else if (type_name == TRANSFORMATION_SETTING_TYPE_RENAMING) {
80  next = std::make_shared<GraphRenaming>();
81  } else {
82  KB_ERROR("Unknown transformation type \"{}\"", type_name);
83  continue;
84  }
85  if (!next->configure(config)) {
86  KB_ERROR("Failed to configure transformation");
87  continue;
88  }
89  if (!first) {
90  first = next;
91  } else {
92  last->setNext(next);
93  }
94  last = next;
95  }
96 
97  if (!first) {
98  KB_ERROR("No transformations created");
99  return nullptr;
100  }
101  return first;
102 }
#define TRANSFORMATION_SETTING_TYPE_RULES
#define TRANSFORMATION_SETTING_TYPE_RESTRUCTURING
#define TRANSFORMATION_SETTING_KEY_TYPE
#define TRANSFORMATION_SETTING_TYPE_RENAMING
#define KB_ERROR
Definition: Logger.h:28
#define KB_WARN
Definition: Logger.h:27
void setNext(const std::shared_ptr< GraphTransformation > &next)
std::shared_ptr< GraphTransformation > nextTransformation_
static std::shared_ptr< GraphTransformation > create(const boost::property_tree::ptree &config)
void apply(OntologySource &ontologySource, const TripleHandler &callback)
virtual void pushInputTriples(const TripleContainerPtr &triples)=0
virtual void finalizeTransformation()=0
void pushOutputTriples(const TripleContainerPtr &triples)
virtual void initializeTransformation()=0
virtual bool load(const TripleHandler &callback)=0
TermRule & string()
Definition: terms.cpp:63
std::shared_ptr< TripleContainer > TripleContainerPtr
std::function< void(const TripleContainerPtr &)> TripleHandler