knowrob  2.1.0
A Knowledge Base System for Cognition-enabled Robots
GraphRenaming.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/GraphRenaming.h"
7 #include "knowrob/Logger.h"
8 #include <utility>
9 #include <fstream>
10 
11 using namespace knowrob;
12 
13 #define GRAPH_RENAMING_SETTING_FILE "file"
14 
16  : renaming_(std::move(renaming)) {
17 }
18 
19 void GraphRenaming::addRenaming(std::string_view from, std::string_view to) {
20  renaming_.emplace(std::string(from), std::string(to));
21 }
22 
23 bool GraphRenaming::configure(const boost::property_tree::ptree &opts) {
24  auto o_user = opts.get_optional<std::string>(GRAPH_RENAMING_SETTING_FILE);
25  bool status = true;
26  if (o_user) {
27  try {
28  status = readFromFile(*o_user);
29  } catch (const std::exception &e) {
30  KB_WARN("Error reading renaming from file {}: {}", *o_user, e.what());
31  status = false;
32  }
33  }
34  return status;
35 }
36 
38  std::ifstream file(filename);
39  std::string line;
40 
41  while (std::getline(file, line)) {
42  std::istringstream iss(line);
43  std::string key, value;
44 
45  if (!(iss >> key >> value)) {
46  KB_WARN("Error reading renaming from file {}: Unexpected file format.", filename);
47  return false;
48  }
49 
50  renaming_[key] = value;
51  }
52  return true;
53 }
54 
55 std::string_view GraphRenaming::rename(const std::string_view &entity) {
56  auto it = renaming_.find(entity);
57  if (it != renaming_.end()) {
58  return it->second;
59  }
60  return entity;
61 }
62 
64  triple.setSubject(rename(triple.subject()));
65  triple.setPredicate(rename(triple.predicate()));
66  if (triple.isObjectIRI()) {
67  triple.setObjectIRI(rename(triple.valueAsString()));
68  }
69 }
70 
73 }
74 
76  finalizeNext();
77 }
78 
80  if (triples->isMutable()) {
81  auto mutableTriples = std::static_pointer_cast<MutableTripleContainer>(triples);
82  for (auto it = mutableTriples->begin(); it != mutableTriples->end(); ++it) {
83  rename(**it);
84  }
85  } else {
86  KB_WARN("Input triples are not mutable which is currently required for renaming.");
87  }
88  pushOutputTriples(triples);
89 }
#define GRAPH_RENAMING_SETTING_FILE
#define KB_WARN
Definition: Logger.h:27
bool readFromFile(const std::string &filename)
void pushInputTriples(const TripleContainerPtr &triples) override
void finalizeTransformation() override
void initializeTransformation() override
std::string_view rename(const std::string_view &entity)
bool configure(const boost::property_tree::ptree &opts) override
GraphRenamingMap renaming_
Definition: GraphRenaming.h:57
void addRenaming(std::string_view from, std::string_view to)
void pushOutputTriples(const TripleContainerPtr &triples)
virtual std::string_view valueAsString() const =0
virtual void setSubject(std::string_view subject)=0
virtual std::string_view subject() const =0
virtual std::string_view predicate() const =0
virtual void setObjectIRI(std::string_view object)=0
bool isObjectIRI() const
Definition: Triple.h:54
virtual void setPredicate(std::string_view predicate)=0
TermRule & string()
Definition: terms.cpp:63
std::shared_ptr< TripleContainer > TripleContainerPtr
std::map< std::string, std::string, std::less<> > GraphRenamingMap
Definition: GraphRenaming.h:18