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

#include <PropertyTree.h>

Public Member Functions

 PropertyTree ()
 
 PropertyTree (std::shared_ptr< const boost::property_tree::ptree > ptree)
 
 PropertyTree (const std::string_view json_str)
 
void init ()
 
auto operator-> () const
 
TermPtr get (std::string_view key, const TermPtr &defaultValue)
 
TermPtr createKeyTerm (std::string_view key) const
 
auto begin () const
 
auto end () const
 
auto & dataSources () const
 
auto ptree () const
 
 PropertyTree ()
 
 PropertyTree (std::shared_ptr< const boost::property_tree::ptree > ptree)
 
 PropertyTree (const std::string_view json_str)
 
void init ()
 
auto operator-> () const
 
TermPtr get (std::string_view key, const TermPtr &defaultValue)
 
TermPtr createKeyTerm (std::string_view key) const
 
auto begin () const
 
auto end () const
 
auto & dataSources () const
 
auto ptree () const
 

Detailed Description

Properties of a reasoner.

Definition at line 21 of file PropertyTree.h.

Constructor & Destructor Documentation

◆ PropertyTree() [1/6]

PropertyTree::PropertyTree ( )

Definition at line 18 of file PropertyTree.cpp.

19  : ptree_(nullptr),
20  delimiter_(".") {}

◆ PropertyTree() [2/6]

PropertyTree::PropertyTree ( std::shared_ptr< const boost::property_tree::ptree >  ptree)
explicit

Load a reasoner configuration from a property tree.

Parameters
ptreea property tree.

Definition at line 22 of file PropertyTree.cpp.

23  : ptree_(std::move(ptree)),
24  delimiter_(".") {
25  init();
26 }
auto ptree() const
Definition: PropertyTree.h:88

◆ PropertyTree() [3/6]

PropertyTree::PropertyTree ( const std::string_view  json_str)
explicit

Load a reasoner configuration from a JSON string.

Parameters
json_stra JSON string.

Definition at line 28 of file PropertyTree.cpp.

29  : PropertyTree() {
30  std::istringstream ss(json_str.data());
31  boost::property_tree::ptree tree;
32  boost::property_tree::read_json(ss, tree);
33  // assign the variable with a new memory allocation as shared_ptr
34  ptree_ = std::make_shared<boost::property_tree::ptree>(tree);
35  init();
36 }

◆ PropertyTree() [4/6]

knowrob::PropertyTree::PropertyTree ( )

◆ PropertyTree() [5/6]

knowrob::PropertyTree::PropertyTree ( std::shared_ptr< const boost::property_tree::ptree >  ptree)
explicit

Load a reasoner configuration from a property tree.

Parameters
ptreea property tree.

◆ PropertyTree() [6/6]

knowrob::PropertyTree::PropertyTree ( const std::string_view  json_str)
explicit

Load a reasoner configuration from a JSON string.

Parameters
json_stra JSON string.

Member Function Documentation

◆ begin() [1/2]

auto knowrob::PropertyTree::begin ( ) const
inline
Returns
the begin iterator of the key-value pairs.

Definition at line 73 of file PropertyTree.h.

73 { return properties_.begin(); }

◆ begin() [2/2]

auto knowrob::PropertyTree::begin ( ) const
inline
Returns
the begin iterator of the key-value pairs.

Definition at line 73 of file PropertyTree.h.

73 { return properties_.begin(); }

◆ createKeyTerm() [1/2]

TermPtr PropertyTree::createKeyTerm ( std::string_view  key) const

Generate a term from a key string.

Parameters
keya key
Returns
a term representing the key.

Definition at line 133 of file PropertyTree.cpp.

133  {
134  TermPtr last_key, next_key;
135 
136  size_t pos_start = 0, pos_end, delim_len = delimiter_.length();
137  std::string_view token;
138  std::vector<std::string_view> res;
139 
140  while ((pos_end = key.find(delimiter_, pos_start)) != std::string::npos) {
141  token = key.substr(pos_start, pos_end - pos_start);
142  pos_start = pos_end + delim_len;
143  next_key = Atom::Tabled(token);
144  if (last_key) {
145  last_key = std::make_shared<Function>(Function(delimiter_, {last_key, next_key}));
146  } else {
147  last_key = next_key;
148  }
149  res.push_back(token);
150  }
151  if (!last_key) {
152  last_key = Atom::Tabled(key);
153  }
154 
155  return last_key;
156 }
static std::shared_ptr< knowrob::Atom > Tabled(std::string_view stringForm)
Definition: Atom.cpp:40
std::shared_ptr< Term > TermPtr
Definition: Term.h:117

◆ createKeyTerm() [2/2]

TermPtr knowrob::PropertyTree::createKeyTerm ( std::string_view  key) const

Generate a term from a key string.

Parameters
keya key
Returns
a term representing the key.

◆ dataSources() [1/2]

auto& knowrob::PropertyTree::dataSources ( ) const
inline
Returns
the list of data sources that should be imported into the reasoner backend.

Definition at line 83 of file PropertyTree.h.

83 { return dataSources_; }

◆ dataSources() [2/2]

auto& knowrob::PropertyTree::dataSources ( ) const
inline
Returns
the list of data sources that should be imported into the reasoner backend.

Definition at line 83 of file PropertyTree.h.

83 { return dataSources_; }

◆ end() [1/2]

auto knowrob::PropertyTree::end ( ) const
inline
Returns
the end iterator of the key-value pairs.

Definition at line 78 of file PropertyTree.h.

78 { return properties_.end(); }

◆ end() [2/2]

auto knowrob::PropertyTree::end ( ) const
inline
Returns
the end iterator of the key-value pairs.

Definition at line 78 of file PropertyTree.h.

78 { return properties_.end(); }

◆ get() [1/2]

TermPtr PropertyTree::get ( std::string_view  key,
const TermPtr defaultValue 
)

Get a value from the property tree. The keys can contain dots to access nested values and square brackets to access array elements. E.g. a call could have this form:

get("key1.key2[0].key3", defaultValue).

The result needs to be a leaf node in the property tree, else (or in case no value is found) an exception is thrown.

Parameters
keya key.
defaultValuea default value.
Returns
the value associated with the key.

Definition at line 57 of file PropertyTree.cpp.

57  {
58  try {
59  return get_value_recursive(*ptree_, std::string(key));
60  } catch (const std::runtime_error &e) {
61  return defaultValue;
62  }
63 }
TermRule & string()
Definition: terms.cpp:63

◆ get() [2/2]

TermPtr knowrob::PropertyTree::get ( std::string_view  key,
const TermPtr defaultValue 
)

Get a value from the property tree. The keys can contain dots to access nested values and square brackets to access array elements. E.g. a call could have this form:

get("key1.key2[0].key3", defaultValue).

The result needs to be a leaf node in the property tree, else (or in case no value is found) an exception is thrown.

Parameters
keya key.
defaultValuea default value.
Returns
the value associated with the key.

◆ init() [1/2]

void PropertyTree::init ( )

Initialize the property tree.

Definition at line 38 of file PropertyTree.cpp.

38  {
39  static const std::string formatDefault = {};
40 
41  // process list of data sources that should be imported into the reasoner backend.
42  auto data_sources = ptree_->get_child_optional("imports");
43  if (!data_sources) {
44  data_sources = ptree_->get_child_optional("data-sources");
45  }
46  if (data_sources) {
47  for (const auto &pair: data_sources.value()) {
48  auto &subtree = pair.second;
49  URI dataSourceURI(subtree);
50  auto dataFormat = subtree.get("format", formatDefault);
51  auto source = std::make_shared<DataSource>(dataSourceURI, dataFormat, DataSourceType::UNSPECIFIED);
52  dataSources_.push_back(source);
53  }
54  }
55 }

◆ init() [2/2]

void knowrob::PropertyTree::init ( )

Initialize the property tree.

◆ operator->() [1/2]

auto knowrob::PropertyTree::operator-> ( ) const
inline

Access the underlying boost property tree.

Returns
the underlying property tree.

Definition at line 46 of file PropertyTree.h.

46 { return ptree_; }

◆ operator->() [2/2]

auto knowrob::PropertyTree::operator-> ( ) const
inline

Access the underlying boost property tree.

Returns
the underlying property tree.

Definition at line 46 of file PropertyTree.h.

46 { return ptree_; }

◆ ptree() [1/2]

auto knowrob::PropertyTree::ptree ( ) const
inline
Returns
the property tree used to generate this configuration.

Definition at line 88 of file PropertyTree.h.

88 { return ptree_; }

◆ ptree() [2/2]

auto knowrob::PropertyTree::ptree ( ) const
inline
Returns
the property tree used to generate this configuration.

Definition at line 88 of file PropertyTree.h.

88 { return ptree_; }

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