6 #include <boost/property_tree/json_parser.hpp>
7 #include "knowrob/PropertyTree.h"
8 #include "knowrob/terms/Function.h"
9 #include "knowrob/integration/python/utils.h"
10 #include "knowrob/terms/String.h"
11 #include "knowrob/terms/ListTerm.h"
23 : ptree_(std::move(ptree)),
30 std::istringstream ss(json_str.data());
31 boost::property_tree::ptree tree;
32 boost::property_tree::read_json(ss, tree);
34 ptree_ = std::make_shared<boost::property_tree::ptree>(tree);
42 auto data_sources = ptree_->get_child_optional(
"imports");
44 data_sources = ptree_->get_child_optional(
"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);
52 dataSources_.push_back(source);
59 return get_value_recursive(*ptree_,
std::string(key));
60 }
catch (
const std::runtime_error &e) {
65 TermPtr PropertyTree::get_value_recursive(
const boost::property_tree::ptree &node,
const std::string &path) {
68 return std::make_shared<String>(node.get_value<
std::string>());
69 }
catch (
const boost::property_tree::ptree_bad_data &) {
70 throw std::runtime_error(
"The found child is not a leaf node");
75 size_t dot_pos = path.find(
'.');
76 size_t bracket_pos = path.find(
'[');
77 size_t next_pos = (dot_pos == std::string::npos) ? bracket_pos : ((bracket_pos == std::string::npos) ? dot_pos
79 dot_pos, bracket_pos));
82 std::string key = (next_pos == std::string::npos) ? path : path.substr(0, next_pos);
83 std::string remaining_path = (next_pos == std::string::npos) ?
"" : path.substr(next_pos);
86 if (!remaining_path.empty() && remaining_path[0] ==
'[') {
87 size_t end_bracket_pos = remaining_path.find(
']');
88 if (end_bracket_pos == std::string::npos) {
89 throw std::runtime_error(
"Invalid path syntax: unmatched '['");
93 std::string index_str = remaining_path.substr(1, end_bracket_pos - 1);
94 size_t index = std::stoi(index_str);
95 remaining_path = remaining_path.substr(end_bracket_pos + 1);
98 if (!remaining_path.empty() && remaining_path[0] ==
'.') {
99 remaining_path = remaining_path.substr(1);
104 const boost::property_tree::ptree &child_array = node.get_child(key);
105 auto it = child_array.begin();
106 std::advance(it, index);
107 if (it == child_array.end()) {
108 throw std::out_of_range(
"Index out of range");
112 return get_value_recursive(it->second, remaining_path);
113 }
catch (
const boost::property_tree::ptree_bad_path &) {
114 throw std::runtime_error(
"Invalid path (array): '" + key +
"'");
120 if (!remaining_path.empty() && remaining_path[0] ==
'.') {
121 remaining_path = remaining_path.substr(1);
124 const boost::property_tree::ptree &child_node = node.get_child(key);
126 return get_value_recursive(child_node, remaining_path);
127 }
catch (
const boost::property_tree::ptree_bad_path &) {
128 throw std::runtime_error(
"Invalid path (key): '" + key +
"'");
136 size_t pos_start = 0, pos_end, delim_len = delimiter_.length();
137 std::string_view token;
138 std::vector<std::string_view> res;
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;
145 last_key = std::make_shared<Function>(
Function(delimiter_, {last_key, next_key}));
149 res.push_back(token);
162 class_<PropertyTree, std::shared_ptr<PropertyTree>>(
"PropertyTree", init<>())
163 .def(init<const std::string &>())
static std::shared_ptr< knowrob::Atom > Tabled(std::string_view stringForm)
TermPtr createKeyTerm(std::string_view key) const
auto & dataSources() const
TermPtr get(std::string_view key, const TermPtr &defaultValue)
void createType< PropertyTree >()
std::shared_ptr< Term > TermPtr