knowrob  2.1.0
A Knowledge Base System for Cognition-enabled Robots
InterfaceUtils.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2023, Sascha Jongebloed
3  * All rights reserved.
4  *
5  * This file is part of KnowRob, please consult
6  * https://github.com/knowrob/knowrob for license details.
7  */
8 
9 #include <unordered_map>
10 #include <boost/any.hpp>
11 #include <boost/property_tree/json_parser.hpp>
12 #include "knowrob/integration/InterfaceUtils.h"
13 #include "knowrob/formulas/ModalFormula.h"
14 #include "knowrob/semweb/Triple.h"
15 #include "knowrob/queries/QueryTree.h"
16 #include "knowrob/queries/QueryError.h"
17 #include "knowrob/KnowledgeBase.h"
18 
19 
20 using namespace knowrob;
21 
22 // enum for epistemicOperator
24  KNOWLEDGE = 0,
25  BELIEF = 1
26 };
27 
28 // enum for temporalOperator
30  CURRENTLY = 0,
31  ALL_PAST = 1,
32  SOME_PAST = 2
33 };
34 
35 boost::property_tree::ptree InterfaceUtils::loadSettings() {
36  // Check for settings file
37  std::string config_path = "default.json";
38  if (std::getenv("KNOWROB_SETTINGS")) {
39  config_path = std::getenv("KNOWROB_SETTINGS");
40  }
41 
42  // read the settings
43  boost::property_tree::ptree config;
44  boost::property_tree::read_json(
45  config_path,
46  config);
47 
48  return config;
49 
50 }
51 
52 bool InterfaceUtils::assertStatements(const KnowledgeBasePtr &kb_, const std::vector<FormulaPtr> &args) {
53  std::vector<TriplePtr> data(args.size());
54  std::vector<TriplePatternPtr> buf(args.size());
55  uint32_t dataIndex = 0;
56 
57  for (auto &phi: args) {
58  const QueryTree qt(phi);
59  if (qt.numPaths() > 1) {
60  throw QueryError("Disjunctions are not allowed in assertions. "
61  "Appears in statement {}.", *phi);
62  } else if (qt.numPaths() == 0) {
63  throw QueryError("Invalid assertion: '{}'", *phi);
64  }
65  for (auto &psi: qt.begin()->nodes()) {
66  switch (psi->type()) {
68  buf[dataIndex] = std::make_shared<TriplePattern>(
69  std::static_pointer_cast<Predicate>(psi), false);
70  data[dataIndex].ptr = new TripleCopy();
71  data[dataIndex].owned = true;
72  buf[dataIndex]->instantiateInto(*data[dataIndex].ptr);
73  dataIndex += 1;
74  break;
75  default:
76  throw QueryError("Invalid assertion: '{}'", *phi);
77  }
78  }
79  }
80  if (kb_->insertAll(data)) {
81  std::cout << "success, " << dataIndex << " statement(s) were asserted." << "\n";
82  return true;
83  } else {
84  std::cout << "assertion failed." << "\n";
85  return false;
86  }
87 }
88 
90 InterfaceUtils::applyModality(const std::unordered_map<std::string, boost::any> &options,
91  FormulaPtr phi) {
92  FormulaPtr mFormula = std::move(phi);
93 
94  // Retrieve epistemicOperator and check if it is "BELIEF"
95  auto epistemicOperator = boost::any_cast<int>(options.at("epistemicOperator"));
96  if (epistemicOperator == EpistemicOperator::BELIEF) {
97  // Retrieve aboutAgentIRI and confidence
98  auto aboutAgentIRI = boost::any_cast<std::string>(options.at("aboutAgentIRI"));
99  auto confidence = boost::any_cast<double>(options.at("confidence"));
100  if (!aboutAgentIRI.empty()) {
101  if (confidence != 1.0) {
102  mFormula = std::make_shared<ModalFormula>(
103  modals::B(aboutAgentIRI, confidence), mFormula);
104  } else {
105  mFormula = std::make_shared<ModalFormula>(
106  modals::B(aboutAgentIRI), mFormula);
107  }
108  }
109  } else if (epistemicOperator == EpistemicOperator::KNOWLEDGE) {
110  // Retrieve aboutAgentIRI
111  auto aboutAgentIRI = boost::any_cast<std::string>(options.at("aboutAgentIRI"));
112  if (!aboutAgentIRI.empty()) {
113  mFormula = std::make_shared<ModalFormula>(
114  modals::K(aboutAgentIRI), mFormula);
115  }
116  }
117  // Retrieve temporalOperator
118  auto temporalOperator = boost::any_cast<int>(options.at("temporalOperator"));
119 
120  // Retrieve minPastTimestamp and maxPastTimestamp
121  auto minPastTimestamp = boost::any_cast<double>(options.at("minPastTimestamp"));
122  auto maxPastTimestamp = boost::any_cast<double>(options.at("maxPastTimestamp"));
123 
124  auto minPastTimePoint =
125  minPastTimestamp != -1 ? std::optional<TimePoint>(knowrob::time::fromSeconds(minPastTimestamp))
126  : std::nullopt;
127  auto maxPastTimePoint =
128  maxPastTimestamp != -1 ? std::optional<TimePoint>(knowrob::time::fromSeconds(maxPastTimestamp))
129  : std::nullopt;
130 
131  if (temporalOperator == TemporalOperator::SOME_PAST) {
132  if (minPastTimestamp != -1 || maxPastTimestamp != -1) {
133  if (minPastTimestamp == -1) {
134  mFormula = std::make_shared<ModalFormula>(
135  modals::P(TimeInterval(std::nullopt,
136  maxPastTimePoint)), mFormula);
137  } else if (maxPastTimestamp == -1) {
138  mFormula = std::make_shared<ModalFormula>(
139  modals::P(TimeInterval(minPastTimePoint,
140  std::nullopt)), mFormula);
141  } else {
142  mFormula = std::make_shared<ModalFormula>(
143  modals::P(TimeInterval(minPastTimePoint,
144  maxPastTimePoint)), mFormula);
145  }
146  } else {
147  mFormula = std::make_shared<ModalFormula>(
148  modals::P(), mFormula);
149  }
150  } else if (temporalOperator == TemporalOperator::ALL_PAST) {
151  if (minPastTimestamp != -1 || maxPastTimestamp != -1) {
152  if (minPastTimestamp == -1) {
153  mFormula = std::make_shared<ModalFormula>(
154  modals::H(TimeInterval(std::nullopt,
155  maxPastTimePoint)), mFormula);
156  } else if (maxPastTimestamp == -1) {
157  mFormula = std::make_shared<ModalFormula>(
158  modals::H(TimeInterval(minPastTimePoint,
159  std::nullopt)), mFormula);
160  } else {
161  mFormula = std::make_shared<ModalFormula>(
162  modals::H(TimeInterval(minPastTimePoint,
163  maxPastTimePoint)), mFormula);
164  }
165  } else {
166  mFormula = std::make_shared<ModalFormula>(
167  modals::H(), mFormula);
168  }
169  }
170  return mFormula;
171 }
172 
173 namespace knowrob::py {
174  template<>
176  using namespace boost::python;
177 
178  // Expose InterfaceUtils class and its methods
179  class_<InterfaceUtils>("InterfaceUtils")
180  .def("assertStatements", &InterfaceUtils::assertStatements).staticmethod("assertStatements")
181  .def("applyModality", &InterfaceUtils::applyModality).staticmethod("applyModality");
182 
183  // Expose enums
184  enum_<EpistemicOperator>("EpistemicOperator")
185  .value("KNOWLEDGE", KNOWLEDGE)
186  .value("BELIEF", BELIEF);
187  enum_<TemporalOperator>("TemporalOperator")
188  .value("CURRENTLY", CURRENTLY)
189  .value("ALL_PAST", ALL_PAST)
190  .value("SOME_PAST", SOME_PAST);
191  }
192 }
TemporalOperator
@ SOME_PAST
@ CURRENTLY
@ ALL_PAST
EpistemicOperator
@ KNOWLEDGE
@ BELIEF
static boost::property_tree::ptree loadSettings()
static FormulaPtr applyModality(const std::unordered_map< std::string, boost::any > &options, FormulaPtr phi)
static bool assertStatements(const KnowledgeBasePtr &kb, const std::vector< FormulaPtr > &args)
auto begin() const
Definition: QueryTree.h:45
auto numPaths() const
Definition: QueryTree.h:35
std::shared_ptr< ModalFormula > K(const FormulaPtr &phi)
std::shared_ptr< ModalFormula > P(const FormulaPtr &phi)
std::shared_ptr< ModalFormula > B(const FormulaPtr &phi)
std::shared_ptr< ModalFormula > H(const FormulaPtr &phi)
TermRule & string()
Definition: terms.cpp:63
TermRule & options()
Definition: terms.cpp:114
void createType< InterfaceUtils >()
TimePoint fromSeconds(double seconds)
Definition: TimePoint.cpp:17
std::shared_ptr< KnowledgeBase > KnowledgeBasePtr
TripleTemplate< std::string > TripleCopy
Definition: Triple.h:577
std::shared_ptr< Formula > FormulaPtr
Definition: Formula.h:99