knowrob  2.1.0
A Knowledge Base System for Cognition-enabled Robots
knowrob::GraphTransformation Class Referenceabstract

#include <GraphTransformation.h>

Inheritance diagram for knowrob::GraphTransformation:

Public Member Functions

 GraphTransformation ()=default
 
virtual ~GraphTransformation ()=default
 
void setOrigin (std::string_view origin)
 
auto origin () const
 
void apply (OntologySource &ontologySource, const TripleHandler &callback)
 
virtual bool configure (const boost::property_tree::ptree &config)=0
 
 GraphTransformation ()=default
 
virtual ~GraphTransformation ()=default
 
void setOrigin (std::string_view origin)
 
auto origin () const
 
void apply (OntologySource &ontologySource, const TripleHandler &callback)
 
virtual bool configure (const boost::property_tree::ptree &config)=0
 

Static Public Member Functions

static std::shared_ptr< GraphTransformationcreate (const boost::property_tree::ptree &config)
 
static std::shared_ptr< GraphTransformationcreate (const boost::property_tree::ptree &config)
 

Protected Member Functions

void initializeNext ()
 
void finalizeNext ()
 
void setNext (const std::shared_ptr< GraphTransformation > &next)
 
void setNext (const TripleHandler &next)
 
void pushOutputTriples (const TripleContainerPtr &triples)
 
virtual void pushInputTriples (const TripleContainerPtr &triples)=0
 
virtual void initializeTransformation ()=0
 
virtual void finalizeTransformation ()=0
 
void initializeNext ()
 
void finalizeNext ()
 
void setNext (const std::shared_ptr< GraphTransformation > &next)
 
void setNext (const TripleHandler &next)
 
void pushOutputTriples (const TripleContainerPtr &triples)
 
virtual void pushInputTriples (const TripleContainerPtr &triples)=0
 
virtual void initializeTransformation ()=0
 
virtual void finalizeTransformation ()=0
 

Protected Attributes

TripleHandler next_
 
std::shared_ptr< GraphTransformationnextTransformation_
 
std::string origin_
 

Detailed Description

Base class for graph transformations. Each transformation consumes input triples and produces output triples. The output triples are either passed to the next transformation or to a handler. Note that GraphTransformation currently maybe not preserve the context of input triples, and that the context of output triples is limited to reflect the triple origin.

Definition at line 22 of file GraphTransformation.h.

Constructor & Destructor Documentation

◆ GraphTransformation() [1/2]

knowrob::GraphTransformation::GraphTransformation ( )
default

◆ ~GraphTransformation() [1/2]

virtual knowrob::GraphTransformation::~GraphTransformation ( )
virtualdefault

◆ GraphTransformation() [2/2]

knowrob::GraphTransformation::GraphTransformation ( )
default

◆ ~GraphTransformation() [2/2]

virtual knowrob::GraphTransformation::~GraphTransformation ( )
virtualdefault

Member Function Documentation

◆ apply() [1/2]

void GraphTransformation::apply ( OntologySource ontologySource,
const TripleHandler callback 
)

Apply the transformation to the given ontology source.

Parameters
ontologySourcethe ontology source to apply the transformation to
callbackthe callback to handle the output triples

Definition at line 40 of file GraphTransformation.cpp.

40  {
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 }
void setNext(const std::shared_ptr< GraphTransformation > &next)
std::shared_ptr< GraphTransformation > nextTransformation_
virtual void pushInputTriples(const TripleContainerPtr &triples)=0
virtual void finalizeTransformation()=0
virtual void initializeTransformation()=0
virtual bool load(const TripleHandler &callback)=0
std::shared_ptr< TripleContainer > TripleContainerPtr

◆ apply() [2/2]

void knowrob::GraphTransformation::apply ( OntologySource ontologySource,
const TripleHandler callback 
)

Apply the transformation to the given ontology source.

Parameters
ontologySourcethe ontology source to apply the transformation to
callbackthe callback to handle the output triples

◆ configure() [1/2]

virtual bool knowrob::GraphTransformation::configure ( const boost::property_tree::ptree &  config)
pure virtual

Configure the transformation with the given options.

Parameters
configthe options
Returns
true if the configuration was successful

Implemented in knowrob::GraphRestructuring, knowrob::GraphRenaming, knowrob::GraphRestructuring, and knowrob::GraphRenaming.

◆ configure() [2/2]

virtual bool knowrob::GraphTransformation::configure ( const boost::property_tree::ptree &  config)
pure virtual

Configure the transformation with the given options.

Parameters
configthe options
Returns
true if the configuration was successful

Implemented in knowrob::GraphRestructuring, knowrob::GraphRenaming, knowrob::GraphRestructuring, and knowrob::GraphRenaming.

◆ create() [1/2]

std::shared_ptr< GraphTransformation > GraphTransformation::create ( const boost::property_tree::ptree &  config)
static

Create a new transformation from the given configuration.

Parameters
configthe configuration
Returns
the new transformation

Definition at line 61 of file GraphTransformation.cpp.

61  {
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
TermRule & string()
Definition: terms.cpp:63

◆ create() [2/2]

static std::shared_ptr<GraphTransformation> knowrob::GraphTransformation::create ( const boost::property_tree::ptree &  config)
static

Create a new transformation from the given configuration.

Parameters
configthe configuration
Returns
the new transformation

◆ finalizeNext() [1/2]

void GraphTransformation::finalizeNext ( )
protected

Definition at line 34 of file GraphTransformation.cpp.

34  {
35  if (nextTransformation_) {
36  nextTransformation_->finalizeTransformation();
37  }
38 }

◆ finalizeNext() [2/2]

void knowrob::GraphTransformation::finalizeNext ( )
protected

◆ finalizeTransformation() [1/2]

virtual void knowrob::GraphTransformation::finalizeTransformation ( )
protectedpure virtual

◆ finalizeTransformation() [2/2]

virtual void knowrob::GraphTransformation::finalizeTransformation ( )
protectedpure virtual

◆ initializeNext() [1/2]

void GraphTransformation::initializeNext ( )
protected

Definition at line 28 of file GraphTransformation.cpp.

28  {
29  if (nextTransformation_) {
30  nextTransformation_->initializeTransformation();
31  }
32 }

◆ initializeNext() [2/2]

void knowrob::GraphTransformation::initializeNext ( )
protected

◆ initializeTransformation() [1/2]

virtual void knowrob::GraphTransformation::initializeTransformation ( )
protectedpure virtual

◆ initializeTransformation() [2/2]

virtual void knowrob::GraphTransformation::initializeTransformation ( )
protectedpure virtual

◆ origin() [1/2]

auto knowrob::GraphTransformation::origin ( ) const
inline

Get the origin of the triples.

Returns
the origin of the input triples

Definition at line 40 of file GraphTransformation.h.

40 { return origin_; }

◆ origin() [2/2]

auto knowrob::GraphTransformation::origin ( ) const
inline

Get the origin of the triples.

Returns
the origin of the input triples

Definition at line 40 of file GraphTransformation.h.

40 { return origin_; }

◆ pushInputTriples() [1/2]

virtual void knowrob::GraphTransformation::pushInputTriples ( const TripleContainerPtr triples)
protectedpure virtual

Push input triples to the transformation.

Parameters
triplesthe input triples

Implemented in knowrob::GraphRestructuring, knowrob::GraphRenaming, knowrob::GraphRestructuring, and knowrob::GraphRenaming.

◆ pushInputTriples() [2/2]

virtual void knowrob::GraphTransformation::pushInputTriples ( const TripleContainerPtr triples)
protectedpure virtual

Push input triples to the transformation.

Parameters
triplesthe input triples

Implemented in knowrob::GraphRestructuring, knowrob::GraphRenaming, knowrob::GraphRestructuring, and knowrob::GraphRenaming.

◆ pushOutputTriples() [1/2]

void GraphTransformation::pushOutputTriples ( const TripleContainerPtr triples)
protected

Push output triples to the next transformation or handler.

Parameters
triplesthe output triples

Definition at line 18 of file GraphTransformation.cpp.

18  {
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 }
#define KB_WARN
Definition: Logger.h:27

◆ pushOutputTriples() [2/2]

void knowrob::GraphTransformation::pushOutputTriples ( const TripleContainerPtr triples)
protected

Push output triples to the next transformation or handler.

Parameters
triplesthe output triples

◆ setNext() [1/4]

void knowrob::GraphTransformation::setNext ( const std::shared_ptr< GraphTransformation > &  next)
inlineprotected

Set the next transformation to be called after this one.

Parameters
nextthe next transformation

Definition at line 76 of file GraphTransformation.h.

76 { nextTransformation_ = next; }

◆ setNext() [2/4]

void knowrob::GraphTransformation::setNext ( const std::shared_ptr< GraphTransformation > &  next)
inlineprotected

Set the next transformation to be called after this one.

Parameters
nextthe next transformation

Definition at line 76 of file GraphTransformation.h.

76 { nextTransformation_ = next; }

◆ setNext() [3/4]

void knowrob::GraphTransformation::setNext ( const TripleHandler next)
inlineprotected

Set the next handler to be called after this transformation.

Parameters
nextthe next handler

Definition at line 82 of file GraphTransformation.h.

82 { next_ = next; }

◆ setNext() [4/4]

void knowrob::GraphTransformation::setNext ( const TripleHandler next)
inlineprotected

Set the next handler to be called after this transformation.

Parameters
nextthe next handler

Definition at line 82 of file GraphTransformation.h.

82 { next_ = next; }

◆ setOrigin() [1/2]

void knowrob::GraphTransformation::setOrigin ( std::string_view  origin)
inline

Set the origin of the triples. Transformations maybe do not preserve context of input triples, so the origin of the input triples must be specified.

Parameters
originthe origin of the input triples

Definition at line 34 of file GraphTransformation.h.

◆ setOrigin() [2/2]

void knowrob::GraphTransformation::setOrigin ( std::string_view  origin)
inline

Set the origin of the triples. Transformations maybe do not preserve context of input triples, so the origin of the input triples must be specified.

Parameters
originthe origin of the input triples

Definition at line 34 of file GraphTransformation.h.

34 { origin_ = origin; }

Member Data Documentation

◆ next_

TripleHandler knowrob::GraphTransformation::next_
protected

Definition at line 64 of file GraphTransformation.h.

◆ nextTransformation_

std::shared_ptr< GraphTransformation > knowrob::GraphTransformation::nextTransformation_
protected

Definition at line 65 of file GraphTransformation.h.

◆ origin_

std::string knowrob::GraphTransformation::origin_
protected

Definition at line 66 of file GraphTransformation.h.


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