FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
knowrob/src/PropertyTree.cpp at dev · daniel86/knowrob · GitHub
daniel86
/
knowrob
Public
forked from
knowrob/knowrob
Notifications
You must be signed in to change notification settings
Fork
5
Star
1
Code
Issues
0
Pull requests
2
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
knowrob
/
src
/
PropertyTree.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
164 lines (142 loc) · 5.37 KB
Breadcrumbs
knowrob
/
src
/
PropertyTree.cpp
Copy path
File metadata and controls
164 lines (142 loc) · 5.37 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*
* This file is part of KnowRob, please consult
* https://github.com/knowrob/knowrob for license details.
*/
#
include
<
boost/property_tree/json_parser.hpp
>
#
include
"
knowrob/PropertyTree.h
"
#
include
"
knowrob/terms/Function.h
"
#
include
"
knowrob/integration/python/utils.h
"
#
include
"
knowrob/terms/String.h
"
#
include
"
knowrob/terms/ListTerm.h
"
#
include
<
iostream
>
#
include
<
utility
>
using
namespace
knowrob
;
PropertyTree::PropertyTree
()
: ptree_(
nullptr
),
delimiter_(
"
.
"
) {}
PropertyTree::PropertyTree
(std::shared_ptr<
const
boost::property_tree::ptree> ptree)
: ptree_(std::move(ptree)),
delimiter_(
"
.
"
) {
init
();
}
PropertyTree::PropertyTree
(
const
std::string_view json_str)
: PropertyTree() {
std::istringstream
ss
(json_str.
data
());
boost::property_tree::ptree tree;
boost::property_tree::read_json
(ss, tree);
//
assign the variable with a new memory allocation as shared_ptr
ptree_ = std::make_shared<boost::property_tree::ptree>(tree);
init
();
}
void
PropertyTree::init
() {
static
const
std::string formatDefault = {};
//
process list of data sources that should be imported into the reasoner backend.
auto
data_sources = ptree_->
get_child_optional
(
"
imports
"
);
if
(!data_sources) {
data_sources = ptree_->
get_child_optional
(
"
data-sources
"
);
}
if
(data_sources) {
for
(
const
auto
&pair: data_sources.
value
()) {
auto
&subtree = pair.
second
;
URI
dataSourceURI
(subtree);
auto
dataFormat = subtree.
get
(
"
format
"
, formatDefault);
auto
source = std::make_shared<DataSource>(dataSourceURI, dataFormat, DataSourceType::
UNSPECIFIED
);
dataSources_.
push_back
(source);
}
}
}
TermPtr
PropertyTree::get
(std::string_view key,
const
TermPtr &defaultValue) {
return
get_value_recursive
(*ptree_,
std::string
(key));
}
TermPtr
PropertyTree::get_value_recursive
(
const
boost::property_tree::ptree &node,
const
std::string &path) {
if
(path.
empty
()) {
try
{
return
std::make_shared<String>(node.
get_value
<std::string>());
}
catch
(
const
boost::property_tree::ptree_bad_data &) {
throw
std::runtime_error
(
"
The found child is not a leaf node
"
);
}
}
//
Find the position of the first dot or bracket
size_t
dot_pos = path.
find
(
'
.
'
);
size_t
bracket_pos = path.
find
(
'
[
'
);
size_t
next_pos = (dot_pos == std::string::npos) ? bracket_pos : ((bracket_pos == std::string::npos) ? dot_pos
:
std::min
(
dot_pos, bracket_pos));
//
Extract the current key
std::string key = (next_pos == std::string::npos) ? path : path.
substr
(
0
, next_pos);
std::string remaining_path = (next_pos == std::string::npos) ?
"
"
: path.
substr
(next_pos);
//
Check for array indexing
if
(!remaining_path.
empty
() && remaining_path[
0
] ==
'
[
'
) {
size_t
end_bracket_pos = remaining_path.
find
(
'
]
'
);
if
(end_bracket_pos == std::string::npos) {
throw
std::runtime_error
(
"
Invalid path syntax: unmatched '['
"
);
}
//
Extract the index inside the brackets
std::string index_str = remaining_path.
substr
(
1
, end_bracket_pos -
1
);
size_t
index =
std::stoi
(index_str);
remaining_path = remaining_path.
substr
(end_bracket_pos +
1
);
//
Remove leading dot if it exists
if
(!remaining_path.
empty
() && remaining_path[
0
] ==
'
.
'
) {
remaining_path = remaining_path.
substr
(
1
);
}
//
Find the child array and access the element at the specified index
try
{
const
boost::property_tree::ptree &child_array = node.
get_child
(key);
auto
it = child_array.
begin
();
std::advance
(it, index);
if
(it == child_array.
end
()) {
throw
std::out_of_range
(
"
Index out of range
"
);
}
//
Recursively call with the selected child and remaining path
return
get_value_recursive
(it->
second
, remaining_path);
}
catch
(
const
boost::property_tree::ptree_bad_path &) {
throw
std::runtime_error
(
"
Invalid path (array): '
"
+ key +
"
'
"
);
}
}
else
{
//
Normal key, recursively call with the next child
try
{
//
Remove leading dot if it exists
if
(!remaining_path.
empty
() && remaining_path[
0
] ==
'
.
'
) {
remaining_path = remaining_path.
substr
(
1
);
}
//
Attempt to fetch the child node
const
boost::property_tree::ptree &child_node = node.
get_child
(key);
//
Recursively call with the selected child and remaining path
return
get_value_recursive
(child_node, remaining_path);
}
catch
(
const
boost::property_tree::ptree_bad_path &) {
throw
std::runtime_error
(
"
Invalid path (key): '
"
+ key +
"
'
"
);
}
}
}
TermPtr
PropertyTree::createKeyTerm
(std::string_view key)
const
{
TermPtr last_key, next_key;
size_t
pos_start =
0
, pos_end, delim_len = delimiter_.
length
();
std::string_view token;
std::vector<std::string_view> res;
while
((pos_end = key.
find
(delimiter_, pos_start)) != std::string::npos) {
token = key.
substr
(pos_start, pos_end - pos_start);
pos_start = pos_end + delim_len;
next_key =
Atom::Tabled
(token);
if
(last_key) {
last_key = std::make_shared<Function>(
Function
(delimiter_, {last_key, next_key}));
}
else
{
last_key = next_key;
}
res.
push_back
(token);
}
if
(!last_key) {
last_key =
Atom::Tabled
(key);
}
return
last_key;
}
namespace
knowrob
::py {
template
<>
void
createType<PropertyTree>() {
using
namespace
boost
::python
;
class_<PropertyTree, std::shared_ptr<PropertyTree>>(
"
PropertyTree
"
, init<>())
.
def
(init<
const
std::string &>())
.
def
(
"
__iter__
"
,
range
(&PropertyTree::begin, &PropertyTree::end))
.
def
(
"
get
"
, &PropertyTree::get)
.
def
(
"
dataSources
"
, &PropertyTree::dataSources, return_value_policy<copy_const_reference>());
}
}
Back
|
FazBrowse Home
|
New Git URL