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

#include <GraphRenaming.h>

Inheritance diagram for knowrob::GraphRenaming:
Collaboration diagram for knowrob::GraphRenaming:

Public Member Functions

 GraphRenaming ()=default
 
 GraphRenaming (GraphRenamingMap renaming)
 
std::string_view rename (const std::string_view &entity)
 
void rename (Triple &triple)
 
void addRenaming (std::string_view from, std::string_view to)
 
bool configure (const boost::property_tree::ptree &opts) override
 
 GraphRenaming ()=default
 
 GraphRenaming (GraphRenamingMap renaming)
 
std::string_view rename (const std::string_view &entity)
 
void rename (Triple &triple)
 
void addRenaming (std::string_view from, std::string_view to)
 
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

bool readFromFile (const std::string &filename)
 
void initializeTransformation () override
 
void finalizeTransformation () override
 
void pushInputTriples (const TripleContainerPtr &triples) override
 
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)
 

Protected Attributes

GraphRenamingMap renaming_
 
- 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 transformation that only renames entities in the input graph.

Definition at line 23 of file GraphRenaming.h.

Constructor & Destructor Documentation

◆ GraphRenaming() [1/4]

knowrob::GraphRenaming::GraphRenaming ( )
default

◆ GraphRenaming() [2/4]

GraphRenaming::GraphRenaming ( GraphRenamingMap  renaming)
explicit

Create a new renaming transformation with the given renaming map.

Parameters
renamingthe renaming map

Definition at line 15 of file GraphRenaming.cpp.

16  : renaming_(std::move(renaming)) {
17 }
GraphRenamingMap renaming_
Definition: GraphRenaming.h:57

◆ GraphRenaming() [3/4]

knowrob::GraphRenaming::GraphRenaming ( )
default

◆ GraphRenaming() [4/4]

knowrob::GraphRenaming::GraphRenaming ( GraphRenamingMap  renaming)
explicit

Create a new renaming transformation with the given renaming map.

Parameters
renamingthe renaming map

Member Function Documentation

◆ addRenaming() [1/2]

void GraphRenaming::addRenaming ( std::string_view  from,
std::string_view  to 
)

Add a renaming rule to the renaming map.

Parameters
fromthe original entity name
tothe new entity name

Definition at line 19 of file GraphRenaming.cpp.

19  {
20  renaming_.emplace(std::string(from), std::string(to));
21 }
TermRule & string()
Definition: terms.cpp:63

◆ addRenaming() [2/2]

void knowrob::GraphRenaming::addRenaming ( std::string_view  from,
std::string_view  to 
)

Add a renaming rule to the renaming map.

Parameters
fromthe original entity name
tothe new entity name

◆ configure() [1/2]

bool GraphRenaming::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 23 of file GraphRenaming.cpp.

23  {
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 }
#define GRAPH_RENAMING_SETTING_FILE
#define KB_WARN
Definition: Logger.h:27
bool readFromFile(const std::string &filename)

◆ configure() [2/2]

bool knowrob::GraphRenaming::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.

◆ finalizeTransformation() [1/2]

void GraphRenaming::finalizeTransformation ( )
overrideprotectedvirtual

Finalize the transformation.

Implements knowrob::GraphTransformation.

Definition at line 75 of file GraphRenaming.cpp.

75  {
76  finalizeNext();
77 }

◆ finalizeTransformation() [2/2]

void knowrob::GraphRenaming::finalizeTransformation ( )
overrideprotectedvirtual

Finalize the transformation.

Implements knowrob::GraphTransformation.

◆ initializeTransformation() [1/2]

void GraphRenaming::initializeTransformation ( )
overrideprotectedvirtual

Initialize the transformation.

Implements knowrob::GraphTransformation.

Definition at line 71 of file GraphRenaming.cpp.

71  {
73 }

◆ initializeTransformation() [2/2]

void knowrob::GraphRenaming::initializeTransformation ( )
overrideprotectedvirtual

Initialize the transformation.

Implements knowrob::GraphTransformation.

◆ pushInputTriples() [1/2]

void GraphRenaming::pushInputTriples ( const TripleContainerPtr triples)
overrideprotectedvirtual

Push input triples to the transformation.

Parameters
triplesthe input triples

Implements knowrob::GraphTransformation.

Definition at line 79 of file GraphRenaming.cpp.

79  {
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 }
std::string_view rename(const std::string_view &entity)
void pushOutputTriples(const TripleContainerPtr &triples)

◆ pushInputTriples() [2/2]

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

Push input triples to the transformation.

Parameters
triplesthe input triples

Implements knowrob::GraphTransformation.

◆ readFromFile() [1/2]

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

Definition at line 37 of file GraphRenaming.cpp.

37  {
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 }

◆ readFromFile() [2/2]

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

◆ rename() [1/4]

std::string_view GraphRenaming::rename ( const std::string_view &  entity)

Apply renaming mapping to an entity.

Parameters
entitythe entity to rename
Returns
the renamed entity

Definition at line 55 of file GraphRenaming.cpp.

55  {
56  auto it = renaming_.find(entity);
57  if (it != renaming_.end()) {
58  return it->second;
59  }
60  return entity;
61 }

◆ rename() [2/4]

std::string_view knowrob::GraphRenaming::rename ( const std::string_view &  entity)

Apply renaming mapping to an entity.

Parameters
entitythe entity to rename
Returns
the renamed entity

◆ rename() [3/4]

void GraphRenaming::rename ( Triple triple)

Rename the entities in the given triple.

Parameters
triplethe triple to rename

Definition at line 63 of file GraphRenaming.cpp.

63  {
64  triple.setSubject(rename(triple.subject()));
65  triple.setPredicate(rename(triple.predicate()));
66  if (triple.isObjectIRI()) {
67  triple.setObjectIRI(rename(triple.valueAsString()));
68  }
69 }
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

◆ rename() [4/4]

void knowrob::GraphRenaming::rename ( Triple triple)

Rename the entities in the given triple.

Parameters
triplethe triple to rename

Member Data Documentation

◆ renaming_

GraphRenamingMap knowrob::GraphRenaming::renaming_
protected

Definition at line 57 of file GraphRenaming.h.


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