knowrob  2.1.0
A Knowledge Base System for Cognition-enabled Robots
ModalOperator.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of KnowRob, please consult
3  * https://github.com/knowrob/knowrob for license details.
4  */
5 
6 #include "knowrob/formulas/ModalOperator.h"
7 #include "knowrob/Logger.h"
8 #include "knowrob/integration/python/utils.h"
9 #include "knowrob/terms/Numeric.h"
10 
11 using namespace knowrob;
12 
14  : modalType_(modalType) {
15 }
16 
17 const char *ModalOperator::symbol() const {
18  switch (modalType_) {
20  return "K";
21  case ModalType::BELIEF:
22  return "B";
23  case ModalType::ALWAYS:
24  return "H";
26  return "P";
27  }
28  return "K";
29 }
30 
31 void ModalOperator::write(std::ostream &os) const {
32  os << symbol();
33 
34  if (!parameters_.empty()) {
35  os << '[';
36  int paramIndex = 0;
37  for (auto &pair: parameters_) {
38  if (paramIndex++ > 0) os << ", ";
39  os << pair.first << '=' << *pair.second;
40  }
41  os << ']';
42  }
43 }
44 
45 bool ModalOperator::operator==(const ModalOperator &other) const {
46  if (modalType_ != other.modalType_) {
47  return false;
48  }
49  if (parameters_.size() != other.parameters_.size()) {
50  return false;
51  }
52  for (auto &pair: parameters_) {
53  auto it = other.parameters_.find(pair.first);
54  if (it == other.parameters_.end() || *it->second != *pair.second) {
55  return false;
56  }
57  }
58  return true;
59 }
60 
62  switch (modalType_) {
64  case ModalType::ALWAYS:
65  return true;
67  case ModalType::BELIEF:
68  return false;
69  }
70  return true;
71 }
72 
74  return !isModalNecessity();
75 }
76 
77 void ModalOperator::setParameter(std::string_view key, const TermPtr &value) {
78  parameters_[key.data()] = value;
79 }
80 
81 std::optional<TermPtr> ModalOperator::parameter(std::string_view key) const {
82  auto it = parameters_.find(key.data());
83  if (it == parameters_.end()) {
84  return std::nullopt;
85  }
86  return it->second;
87 }
88 
89 std::optional<PerspectivePtr> ModalOperator::perspective() const {
90  auto it = parameters_.find(KEY_PERSPECTIVE);
91  if (it != parameters_.end() && it->second->isAtom()) {
92  return Perspective::get(std::static_pointer_cast<Atomic>(it->second)->stringForm());
93  }
94  return std::nullopt;
95 }
96 
97 void ModalOperator::setPerspective(const std::string_view &agent) {
99 }
100 
101 std::optional<double> ModalOperator::confidence() const {
102  auto it = parameters_.find(KEY_CONFIDENCE);
103  if (it != parameters_.end() && it->second->isNumeric()) {
104  return std::static_pointer_cast<Numeric>(it->second)->asDouble();
105  }
106  return std::nullopt;
107 }
108 
109 void ModalOperator::setConfidence(double confidence) {
110  parameters_[KEY_CONFIDENCE] = std::make_shared<Double>(confidence);
111 }
112 
113 std::optional<double> ModalOperator::begin() const {
114  auto it = parameters_.find(KEY_BEGIN);
115  if (it != parameters_.end() && it->second->isNumeric()) {
116  return std::static_pointer_cast<Numeric>(it->second)->asDouble();
117  }
118  return std::nullopt;
119 }
120 
121 void ModalOperator::setBegin(double begin) {
122  parameters_[KEY_BEGIN] = std::make_shared<Double>(begin);
123 }
124 
125 std::optional<double> ModalOperator::end() const {
126  auto it = parameters_.find(KEY_END);
127  if (it != parameters_.end() && it->second->isNumeric()) {
128  return std::static_pointer_cast<Numeric>(it->second)->asDouble();
129  }
130  return std::nullopt;
131 }
132 
133 void ModalOperator::setEnd(double end) {
134  parameters_[KEY_END] = std::make_shared<Double>(end);
135 }
136 
137 void ModalOperator::setTimeInterval(const TimeInterval &timeInterval) {
138  if (timeInterval.since()) {
139  setBegin(time::toSeconds(*timeInterval.since()));
140  }
141  if (timeInterval.until()) {
142  setEnd(time::toSeconds(*timeInterval.until()));
143  }
144 }
145 
147  static ModalIteration empty;
148  return empty;
149 }
150 
151 bool ModalIteration::operator==(const ModalIteration &other) const {
152  if (modalitySequence_.size() != other.modalitySequence_.size()) {
153  return false;
154  }
155  auto it = modalitySequence_.begin();
156  auto jt = other.modalitySequence_.begin();
157  while (it != modalitySequence_.end()) {
158  if (!(*it == *jt)) {
159  return false;
160  }
161  ++it;
162  ++jt;
163  }
164  return true;
165 }
166 
168  ModalIteration it(*this);
169  it += modalOperator;
170  return it;
171 }
172 
174  modalitySequence_.push_back(next);
175 }
176 
177 namespace knowrob::py {
178  template<>
180  using namespace boost::python;
181  enum_<ModalType>("ModalType")
182  .value("KNOWLEDGE", ModalType::KNOWLEDGE)
183  .value("BELIEF", ModalType::BELIEF)
184  .value("ALWAYS", ModalType::ALWAYS)
185  .value("SOMETIMES", ModalType::SOMETIMES)
186  .export_values();
187  class_<ModalOperator, std::shared_ptr<ModalOperator>>
188  ("ModalOperator", init<ModalType>())
189  .def("isModalNecessity", &ModalOperator::isModalNecessity)
190  .def("isModalPossibility", &ModalOperator::isModalPossibility)
191  .def("modalType", &ModalOperator::modalType)
192  .def("symbol", &ModalOperator::symbol);
193  class_<ModalIteration, std::shared_ptr<ModalIteration>>
194  ("ModalIteration", init<>())
195  .def("__eq__", &ModalIteration::operator==)
196  .def("__iter__", range(&ModalIteration::begin, &ModalIteration::end))
197  .def("numOperators", &ModalIteration::numOperators)
198  .def("emptyIteration", &ModalIteration::emptyIteration, return_value_policy<copy_const_reference>());
199  }
200 }
static std::shared_ptr< knowrob::Atom > Tabled(std::string_view stringForm)
Definition: Atom.cpp:40
ModalIteration operator+(const ModalOperatorPtr &modalOperator) const
static const ModalIteration & emptyIteration()
void operator+=(const ModalOperatorPtr &modalOperator)
bool operator==(const ModalIteration &other) const
std::list< ModalOperatorPtr > modalitySequence_
std::optional< PerspectivePtr > perspective() const
void write(std::ostream &os) const
const char * symbol() const
void setBegin(double begin)
auto modalType() const
Definition: ModalOperator.h:62
std::map< std::string, TermPtr > parameters_
std::optional< double > end() const
void setPerspective(const std::string_view &agent)
const ModalType modalType_
bool isModalPossibility() const
static constexpr const char * KEY_END
Definition: ModalOperator.h:36
bool operator==(const ModalOperator &other) const
void setEnd(double end)
static constexpr const char * KEY_CONFIDENCE
Definition: ModalOperator.h:34
void setConfidence(double confidence)
void setParameter(std::string_view key, const TermPtr &value)
std::optional< double > begin() const
std::optional< TermPtr > parameter(std::string_view key) const
ModalOperator(ModalType modalType)
void setTimeInterval(const TimeInterval &timeInterval)
bool isModalNecessity() const
static constexpr const char * KEY_BEGIN
Definition: ModalOperator.h:35
static constexpr const char * KEY_PERSPECTIVE
Definition: ModalOperator.h:33
std::optional< double > confidence() const
static std::shared_ptr< Perspective > get(std::string_view iri)
Definition: Perspective.cpp:31
const auto & since() const
Definition: TimeInterval.h:59
const auto & until() const
Definition: TimeInterval.h:64
void createType< ModalOperator >()
const IRIAtomPtr range
Definition: rdfs.h:20
double toSeconds(const TimePoint &timestamp)
Definition: TimePoint.cpp:23
std::shared_ptr< Term > TermPtr
Definition: Term.h:117
std::shared_ptr< const ModalOperator > ModalOperatorPtr