knowrob  2.1.0
A Knowledge Base System for Cognition-enabled Robots
knowrob::GraphRestructuring Class Reference

#include <GraphRestructuring.h>

Inheritance diagram for knowrob::GraphRestructuring:
Collaboration diagram for knowrob::GraphRestructuring:

Public Member Functions

 GraphRestructuring ()
 
void addRule (std::shared_ptr< GraphTransformationRule > rule)
 
bool configure (const boost::property_tree::ptree &opts) override
 
 GraphRestructuring ()
 
void addRule (std::shared_ptr< GraphTransformationRule > rule)
 
bool configure (const boost::property_tree::ptree &opts) override
 
- Public Member Functions inherited from knowrob::GraphTransformation
 GraphTransformation ()=default
 
virtual ~GraphTransformation ()=default
 
void setOrigin (std::string_view origin)
 
auto origin () const
 
void apply (OntologySource &ontologySource, const TripleHandler &callback)
 
 GraphTransformation ()=default
 
virtual ~GraphTransformation ()=default
 
void setOrigin (std::string_view origin)
 
auto origin () const
 
void apply (OntologySource &ontologySource, const TripleHandler &callback)
 

Protected Member Functions

void doTransformation (GraphTransformationRule &rule)
 
bool readFromFile (const std::string &filename)
 
void initializeTransformation () override
 
void finalizeTransformation () override
 
void pushInputTriples (const TripleContainerPtr &triples) override
 
void doTransformation (GraphTransformationRule &rule)
 
bool readFromFile (const std::string &filename)
 
void initializeTransformation () override
 
void finalizeTransformation () override
 
void pushInputTriples (const TripleContainerPtr &triples) override
 
- Protected Member Functions inherited from knowrob::GraphTransformation
void initializeNext ()
 
void finalizeNext ()
 
void setNext (const std::shared_ptr< GraphTransformation > &next)
 
void setNext (const TripleHandler &next)
 
void pushOutputTriples (const TripleContainerPtr &triples)
 
void initializeNext ()
 
void finalizeNext ()
 
void setNext (const std::shared_ptr< GraphTransformation > &next)
 
void setNext (const TripleHandler &next)
 
void pushOutputTriples (const TripleContainerPtr &triples)
 

Static Protected Member Functions

static TriplePatternPtr readTriplePattern (const std::string &filename)
 
static TriplePatternPtr readTriplePattern (const std::string &filename)
 

Protected Attributes

std::unique_ptr< RedlandModelmodel_
 
std::vector< std::shared_ptr< GraphTransformationRule > > rules_
 
- Protected Attributes inherited from knowrob::GraphTransformation
TripleHandler next_
 
std::shared_ptr< GraphTransformationnextTransformation_
 
std::string origin_
 

Additional Inherited Members

- Static Public Member Functions inherited from knowrob::GraphTransformation
static std::shared_ptr< GraphTransformationcreate (const boost::property_tree::ptree &config)
 
static std::shared_ptr< GraphTransformationcreate (const boost::property_tree::ptree &config)
 

Detailed Description

A graph transformation that restructures and possibly renames entities in the input graph.

Definition at line 19 of file GraphRestructuring.h.

Constructor & Destructor Documentation

◆ GraphRestructuring() [1/2]

GraphRestructuring::GraphRestructuring ( )

Definition at line 15 of file GraphRestructuring.cpp.

17  model_(nullptr) {
18 }
std::unique_ptr< RedlandModel > model_

◆ GraphRestructuring() [2/2]

knowrob::GraphRestructuring::GraphRestructuring ( )

Member Function Documentation

◆ addRule() [1/2]

void GraphRestructuring::addRule ( std::shared_ptr< GraphTransformationRule rule)

Adds a rule to this transformation.

Parameters
rulea rule.

Definition at line 20 of file GraphRestructuring.cpp.

20  {
21  rules_.push_back(std::move(rule));
22 }
std::vector< std::shared_ptr< GraphTransformationRule > > rules_

◆ addRule() [2/2]

void knowrob::GraphRestructuring::addRule ( std::shared_ptr< GraphTransformationRule rule)

Adds a rule to this transformation.

Parameters
rulea rule.

◆ configure() [1/2]

bool GraphRestructuring::configure ( const boost::property_tree::ptree &  config)
overridevirtual

Configure the transformation with the given options.

Parameters
configthe options
Returns
true if the configuration was successful

Implements knowrob::GraphTransformation.

Definition at line 24 of file GraphRestructuring.cpp.

24  {
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 }
#define GRAPH_RESTRUCTURING_SETTING_FILE
#define KB_WARN
Definition: Logger.h:27
bool readFromFile(const std::string &filename)
TermRule & string()
Definition: terms.cpp:63

◆ configure() [2/2]

bool knowrob::GraphRestructuring::configure ( const boost::property_tree::ptree &  config)
overridevirtual

Configure the transformation with the given options.

Parameters
configthe options
Returns
true if the configuration was successful

Implements knowrob::GraphTransformation.

◆ doTransformation() [1/2]

void GraphRestructuring::doTransformation ( GraphTransformationRule rule)
protected

Definition at line 129 of file GraphRestructuring.cpp.

129  {
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 }
const std::vector< TriplePatternPtr > & from() const
std::shared_ptr< const Bindings > BindingsPtr
Definition: Bindings.h:151

◆ doTransformation() [2/2]

void knowrob::GraphRestructuring::doTransformation ( GraphTransformationRule rule)
protected

◆ finalizeTransformation() [1/2]

void GraphRestructuring::finalizeTransformation ( )
overrideprotectedvirtual

Finalize the transformation.

Implements knowrob::GraphTransformation.

Definition at line 111 of file GraphRestructuring.cpp.

111  {
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 }
void doTransformation(GraphTransformationRule &rule)
void pushOutputTriples(const TripleContainerPtr &triples)
std::shared_ptr< TripleContainer > TripleContainerPtr

◆ finalizeTransformation() [2/2]

void knowrob::GraphRestructuring::finalizeTransformation ( )
overrideprotectedvirtual

Finalize the transformation.

Implements knowrob::GraphTransformation.

◆ initializeTransformation() [1/2]

void GraphRestructuring::initializeTransformation ( )
overrideprotectedvirtual

Initialize the transformation.

Implements knowrob::GraphTransformation.

Definition at line 105 of file GraphRestructuring.cpp.

105  {
106  model_ = std::make_unique<RedlandModel>();
107  model_->setStorageType(RedlandStorageType::MEMORY);
108  model_->setOrigin(origin_);
109 }

◆ initializeTransformation() [2/2]

void knowrob::GraphRestructuring::initializeTransformation ( )
overrideprotectedvirtual

Initialize the transformation.

Implements knowrob::GraphTransformation.

◆ pushInputTriples() [1/2]

void GraphRestructuring::pushInputTriples ( const TripleContainerPtr triples)
overrideprotectedvirtual

Push input triples to the transformation.

Parameters
triplesthe input triples

Implements knowrob::GraphTransformation.

Definition at line 125 of file GraphRestructuring.cpp.

125  {
126  model_->insertAll(triples);
127 }

◆ pushInputTriples() [2/2]

void knowrob::GraphRestructuring::pushInputTriples ( const TripleContainerPtr triples)
overrideprotectedvirtual

Push input triples to the transformation.

Parameters
triplesthe input triples

Implements knowrob::GraphTransformation.

◆ readFromFile() [1/2]

bool GraphRestructuring::readFromFile ( const std::string &  filename)
protected

Definition at line 38 of file GraphRestructuring.cpp.

38  {
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 }
static TriplePatternPtr readTriplePattern(const std::string &filename)
void addRule(std::shared_ptr< GraphTransformationRule > rule)

◆ readFromFile() [2/2]

bool knowrob::GraphRestructuring::readFromFile ( const std::string &  filename)
protected

◆ readTriplePattern() [1/2]

TriplePatternPtr GraphRestructuring::readTriplePattern ( const std::string &  filename)
staticprotected

Definition at line 93 of file GraphRestructuring.cpp.

93  {
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 }
static PredicatePtr parsePredicate(const std::string &queryString)
Definition: QueryParser.cpp:38
static std::shared_ptr< Predicate > getRDFPredicate(const PredicatePtr &predicate)

◆ readTriplePattern() [2/2]

static TriplePatternPtr knowrob::GraphRestructuring::readTriplePattern ( const std::string &  filename)
staticprotected

Member Data Documentation

◆ model_

std::unique_ptr< RedlandModel > knowrob::GraphRestructuring::model_
protected

Definition at line 33 of file GraphRestructuring.h.

◆ rules_

std::vector< std::shared_ptr< GraphTransformationRule > > knowrob::GraphRestructuring::rules_
protected

Definition at line 34 of file GraphRestructuring.h.


The documentation for this class was generated from the following files: