knowrob  2.1.0
A Knowledge Base System for Cognition-enabled Robots
GraphRestructuring.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/GraphRestructuring.h"
7 #include "knowrob/semweb/SPARQLQuery.h"
8 #include "knowrob/queries/QueryParser.h"
9 #include <fstream>
10 
11 using namespace knowrob;
12 
13 #define GRAPH_RESTRUCTURING_SETTING_FILE "file"
14 
17  model_(nullptr) {
18 }
19 
20 void GraphRestructuring::addRule(std::shared_ptr<GraphTransformationRule> rule) {
21  rules_.push_back(std::move(rule));
22 }
23 
24 bool GraphRestructuring::configure(const boost::property_tree::ptree &opts) {
25  auto o_user = opts.get_optional<std::string>(GRAPH_RESTRUCTURING_SETTING_FILE);
26  bool status = true;
27  if (o_user) {
28  try {
29  status = readFromFile(*o_user);
30  } catch (const std::exception &e) {
31  KB_WARN("Error reading renaming from file {}: {}", *o_user, e.what());
32  status = false;
33  }
34  }
35  return status;
36 }
37 
39  std::ifstream file(filename);
40  std::string line, entry;
41 
42  while (std::getline(file, line, '.')) {
43  entry += line;
44  if (file.peek() != '\n') continue;
45 
46  file.ignore();
47  std::stringstream ss(entry);
48  std::string part1, part2;
49 
50  // split the entry into two parts at the '<-' separator
51  std::getline(ss, part1, '<');
52  ss.ignore(2); // ignore the '-' and ' '
53  std::getline(ss, part2);
54  if (part1.size() < 5 || part2.size() < 5) {
55  KB_WARN("Error reading transformation from file {}: Invalid Syntax.", filename);
56  return false;
57  }
58 
59  // remove the brackets
60  part1 = part1.substr(1, part1.size() - 2);
61  part2 = part2.substr(1, part2.size() - 2);
62 
63  std::vector<TriplePatternPtr> x, y;
64  std::stringstream ss1(part1), ss2(part2);
65  std::string item;
66 
67  while (std::getline(ss1, item, ',')) {
68  auto pat = readTriplePattern(item);
69  if (pat) {
70  x.push_back(pat);
71  } else {
72  KB_WARN("Error reading transformation from file {}: Invalid triple pattern \"{}\".", filename, item);
73  return false;
74  }
75  }
76  while (std::getline(ss2, item, ',')) {
77  auto pat = readTriplePattern(item);
78  if (pat) {
79  y.push_back(pat);
80  } else {
81  KB_WARN("Error reading transformation from file {}: Invalid triple pattern \"{}\".", filename, item);
82  return false;
83  }
84  }
85  addRule(std::make_shared<GraphTransformationRule>(x, y));
86 
87  entry.clear();
88  }
89 
90  return true;
91 }
92 
94  auto p = QueryParser::parsePredicate(stringForm);
95  if (!p) return nullptr;
96  try {
97  auto p_rdf = TriplePattern::getRDFPredicate(p);
98  return std::make_shared<TriplePattern>(p_rdf);
99  } catch (const std::exception &e) {
100  KB_WARN("Error reading transformation from file: {}", e.what());
101  return nullptr;
102  }
103 }
104 
106  model_ = std::make_unique<RedlandModel>();
107  model_->setStorageType(RedlandStorageType::MEMORY);
108  model_->setOrigin(origin_);
109 }
110 
112  // do the transformation after last triple has been received
113  for (auto &rule: rules_) {
114  doTransformation(*rule);
115  }
116  // push into next stage
117  initializeNext();
118  model_->batch([this](const TripleContainerPtr &triples) {
119  pushOutputTriples(triples);
120  });
121  finalizeNext();
122  model_ = nullptr;
123 }
124 
126  model_->insertAll(triples);
127 }
128 
130  // collect matching originals, and apply the transformation by finding and applying
131  // a substitution mapping to the pattern predicates.
132  auto originals = std::make_shared<TriplePatternContainer>();
133  auto transformed = std::make_shared<TriplePatternContainer>();
134  // perform query and record transformations
135  model_->query(rule.getSPARQLQuery(), [&](const BindingsPtr &bindings) {
136  // apply the substitution mapping to the pattern term
137  for (auto &p: rule.to()) {
138  auto x = applyBindings(p, *bindings);
139  x->setGraphName(origin_);
140  transformed->push_back(x);
141  }
142  // apply the substitution mapping to the query term
143  for (auto &p: rule.from()) {
144  originals->push_back(applyBindings(p, *bindings));
145  }
146  });
147  // update the model
148  model_->removeAll(originals);
149  model_->insertAll(transformed);
150 }
#define GRAPH_RESTRUCTURING_SETTING_FILE
#define KB_WARN
Definition: Logger.h:27
void doTransformation(GraphTransformationRule &rule)
void pushInputTriples(const TripleContainerPtr &triples) override
std::unique_ptr< RedlandModel > model_
bool configure(const boost::property_tree::ptree &opts) override
static TriplePatternPtr readTriplePattern(const std::string &filename)
std::vector< std::shared_ptr< GraphTransformationRule > > rules_
void addRule(std::shared_ptr< GraphTransformationRule > rule)
bool readFromFile(const std::string &filename)
void pushOutputTriples(const TripleContainerPtr &triples)
const std::vector< TriplePatternPtr > & from() const
static PredicatePtr parsePredicate(const std::string &queryString)
Definition: QueryParser.cpp:38
static std::shared_ptr< Predicate > getRDFPredicate(const PredicatePtr &predicate)
TermRule & string()
Definition: terms.cpp:63
std::shared_ptr< TriplePattern > TriplePatternPtr
std::shared_ptr< TripleContainer > TripleContainerPtr
std::shared_ptr< const Bindings > BindingsPtr
Definition: Bindings.h:151