FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
knowrob/src/integration/InterfaceUtils.cpp at dev · knowrob/knowrob · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
knowrob
/
knowrob
Public
Notifications
You must be signed in to change notification settings
Fork
112
Star
159
Code
Issues
4
Pull requests
2
Discussions
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Discussions
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
knowrob
/
src
/
integration
/
InterfaceUtils.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
212 lines (190 loc) · 6.7 KB
Breadcrumbs
knowrob
/
src
/
integration
/
InterfaceUtils.cpp
Copy path
File metadata and controls
212 lines (190 loc) · 6.7 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/*
* Copyright (c) 2023, Sascha Jongebloed
* All rights reserved.
*
* This file is part of KnowRob, please consult
* https://github.com/knowrob/knowrob for license details.
*/
#
include
<
unordered_map
>
#
include
<
boost/any.hpp
>
#
include
<
boost/property_tree/json_parser.hpp
>
#
include
"
knowrob/integration/InterfaceUtils.h
"
#
include
"
knowrob/formulas/ModalFormula.h
"
#
include
"
knowrob/semweb/Triple.h
"
#
include
"
knowrob/queries/QueryTree.h
"
#
include
"
knowrob/queries/QueryError.h
"
#
include
"
knowrob/KnowledgeBase.h
"
using
namespace
knowrob
;
//
enum for epistemicOperator
enum
EpistemicOperator {
KNOWLEDGE
=
0
,
BELIEF
=
1
};
//
enum for temporalOperator
enum
TemporalOperator {
CURRENTLY
=
0
,
ALL_PAST
=
1
,
SOME_PAST
=
2
};
boost::property_tree::ptree
InterfaceUtils::loadSettings
() {
//
Check for settings file
std::string config_path =
"
default.json
"
;
if
(
std::getenv
(
"
KNOWROB_SETTINGS
"
)) {
config_path =
std::getenv
(
"
KNOWROB_SETTINGS
"
);
}
//
read the settings
boost::property_tree::ptree config;
boost::property_tree::read_json
(
config_path,
config);
return
config;
}
bool
InterfaceUtils::assertStatements
(
const
KnowledgeBasePtr &kb_,
const
std::vector<FormulaPtr> &args) {
//
Create the query trees for each formula and calculate the number of nodes of type Predicate
//
first create the list of query trees
std::vector<std::unique_ptr<QueryTree>> queryTrees;
for
(
auto
&phi : args) {
auto
qt = std::make_unique<QueryTree>(phi);
if
(qt->
numPaths
() >
1
) {
throw
QueryError
(
"
Disjunctions are not allowed in assertions.
"
"
Appears in statement {}.
"
, *phi);
}
else
if
(qt->
numPaths
() ==
0
) {
throw
QueryError
(
"
Invalid assertion: '{}'
"
, *phi);
}
queryTrees.
push_back
(
std::move
(qt));
//
✔️ move the unique_ptr
}
//
create a counter for the number of nodes of type Predicate
int
numPredicates =
0
;
for
(
auto
&qt: queryTrees) {
for
(
auto
&psi: qt->
begin
()->
nodes
()) {
if
(psi->
type
() == knowrob::FormulaType::
PREDICATE
) {
numPredicates +=
1
;
}
}
}
//
create a vector of TriplePtr and TriplePatternPtr
std::vector<TriplePtr>
data
(numPredicates);
std::vector<TriplePatternPtr>
buf
(numPredicates);
uint32_t
dataIndex =
0
;
//
iterate over the query trees and create the TriplePattern objects
for
(
auto
&qt: queryTrees) {
for
(
auto
&psi: qt->
begin
()->
nodes
()) {
switch
(psi->
type
()) {
case
knowrob::FormulaType::
PREDICATE
:
buf[dataIndex] = std::make_shared<TriplePattern>(
std::static_pointer_cast<Predicate>(psi),
false
);
data[dataIndex].
ptr
=
new
TripleCopy
();
data[dataIndex].
owned
=
true
;
buf[dataIndex]->
instantiateInto
(*data[dataIndex].
ptr
);
dataIndex +=
1
;
break
;
default
:
throw
QueryError
(
"
Invalid assertion: '{}'
"
, *psi);
}
}
}
if
(kb_->
insertAll
(data)) {
std::cout <<
"
success,
"
<< dataIndex <<
"
statement(s) were asserted.
"
<<
"
\n
"
;
return
true
;
}
else
{
std::cout <<
"
assertion failed.
"
<<
"
\n
"
;
return
false
;
}
}
FormulaPtr
InterfaceUtils::applyModality
(
const
std::unordered_map<std::string, boost::any> &options,
FormulaPtr phi) {
FormulaPtr
mFormula
=
std::move
(phi);
//
Retrieve epistemicOperator and check if it is "BELIEF"
auto
epistemicOperator = boost::any_cast<
int
>(options.
at
(
"
epistemicOperator
"
));
if
(epistemicOperator == EpistemicOperator::
BELIEF
) {
//
Retrieve aboutAgentIRI and confidence
auto
aboutAgentIRI = boost::any_cast<std::string>(options.
at
(
"
aboutAgentIRI
"
));
auto
confidence = boost::any_cast<
double
>(options.
at
(
"
confidence
"
));
if
(!aboutAgentIRI.
empty
()) {
if
(confidence !=
1.0
) {
mFormula
= std::make_shared<ModalFormula>(
modals::B
(aboutAgentIRI, confidence),
mFormula
);
}
else
{
mFormula
= std::make_shared<ModalFormula>(
modals::B
(aboutAgentIRI),
mFormula
);
}
}
}
else
if
(epistemicOperator == EpistemicOperator::
KNOWLEDGE
) {
//
Retrieve aboutAgentIRI
auto
aboutAgentIRI = boost::any_cast<std::string>(options.
at
(
"
aboutAgentIRI
"
));
if
(!aboutAgentIRI.
empty
()) {
mFormula
= std::make_shared<ModalFormula>(
modals::K
(aboutAgentIRI),
mFormula
);
}
}
//
Retrieve temporalOperator
auto
temporalOperator = boost::any_cast<
int
>(options.
at
(
"
temporalOperator
"
));
//
Retrieve minPastTimestamp and maxPastTimestamp
auto
minPastTimestamp = boost::any_cast<
double
>(options.
at
(
"
minPastTimestamp
"
));
auto
maxPastTimestamp = boost::any_cast<
double
>(options.
at
(
"
maxPastTimestamp
"
));
auto
minPastTimePoint =
minPastTimestamp != -
1
? std::optional<TimePoint>(
knowrob::time::fromSeconds
(minPastTimestamp))
: std::
nullopt
;
auto
maxPastTimePoint =
maxPastTimestamp != -
1
? std::optional<TimePoint>(
knowrob::time::fromSeconds
(maxPastTimestamp))
: std::
nullopt
;
if
(temporalOperator == TemporalOperator::
SOME_PAST
) {
if
(minPastTimestamp != -
1
|| maxPastTimestamp != -
1
) {
if
(minPastTimestamp == -
1
) {
mFormula
= std::make_shared<ModalFormula>(
modals::P
(
TimeInterval
(std::
nullopt
,
maxPastTimePoint)),
mFormula
);
}
else
if
(maxPastTimestamp == -
1
) {
mFormula
= std::make_shared<ModalFormula>(
modals::P
(
TimeInterval
(minPastTimePoint,
std::
nullopt
)),
mFormula
);
}
else
{
mFormula
= std::make_shared<ModalFormula>(
modals::P
(
TimeInterval
(minPastTimePoint,
maxPastTimePoint)),
mFormula
);
}
}
else
{
mFormula
= std::make_shared<ModalFormula>(
modals::P
(),
mFormula
);
}
}
else
if
(temporalOperator == TemporalOperator::
ALL_PAST
) {
if
(minPastTimestamp != -
1
|| maxPastTimestamp != -
1
) {
if
(minPastTimestamp == -
1
) {
mFormula
= std::make_shared<ModalFormula>(
modals::H
(
TimeInterval
(std::
nullopt
,
maxPastTimePoint)),
mFormula
);
}
else
if
(maxPastTimestamp == -
1
) {
mFormula
= std::make_shared<ModalFormula>(
modals::H
(
TimeInterval
(minPastTimePoint,
std::
nullopt
)),
mFormula
);
}
else
{
mFormula
= std::make_shared<ModalFormula>(
modals::H
(
TimeInterval
(minPastTimePoint,
maxPastTimePoint)),
mFormula
);
}
}
else
{
mFormula
= std::make_shared<ModalFormula>(
modals::H
(),
mFormula
);
}
}
return
mFormula
;
}
namespace
knowrob
::py {
template
<>
void
createType<InterfaceUtils>() {
using
namespace
boost
::python
;
//
Expose InterfaceUtils class and its methods
class_<InterfaceUtils>(
"
InterfaceUtils
"
)
.
def
(
"
assertStatements
"
, &InterfaceUtils::assertStatements).
staticmethod
(
"
assertStatements
"
)
.
def
(
"
applyModality
"
, &InterfaceUtils::applyModality).
staticmethod
(
"
applyModality
"
);
//
Expose enums
enum_<EpistemicOperator>(
"
EpistemicOperator
"
)
.
value
(
"
KNOWLEDGE
"
,
KNOWLEDGE
)
.
value
(
"
BELIEF
"
,
BELIEF
);
enum_<TemporalOperator>(
"
TemporalOperator
"
)
.
value
(
"
CURRENTLY
"
,
CURRENTLY
)
.
value
(
"
ALL_PAST
"
,
ALL_PAST
)
.
value
(
"
SOME_PAST
"
,
SOME_PAST
);
}
}
Back
|
FazBrowse Home
|
New Git URL