knowrob  2.1.0
A Knowledge Base System for Cognition-enabled Robots
GraphSelector.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 "iomanip"
7 #include "knowrob/semweb/GraphSelector.h"
8 #include "knowrob/knowrob.h"
9 #include "knowrob/integration/python/utils.h"
10 
11 using namespace knowrob;
12 
14  uncertain = uncertain || other.uncertain;
15  occasional = occasional || other.occasional;
16 
17  // graph selector changes to wildcard if graph of the other selector is different
18  if (!graph || !other.graph || *graph != *other.graph) {
19  graph = nullptr;
20  }
21 
22  // agent cannot be changed in merge operation.
23  // So GraphSelector can only be merged within a modal in which both are embedded.
24  if (perspective) {
25  if (other.perspective) {
26  if (perspective->iri() != other.perspective->iri()) {
27  return false;
28  }
29  } else {
30  return false;
31  }
32  } else if (other.perspective) {
33  return false;
34  }
35 
36  // later begin time is more restrictive
37  if (other.begin.has_value() && (!begin.has_value() || other.begin.value() > begin.value())) {
38  begin = other.begin;
39  }
40 
41  // earlier end time is more restrictive
42  if (other.end.has_value() && (!end.has_value() || other.end.value() > end.value())) {
43  end = other.end;
44  }
45 
46  // smaller confidence is more restrictive
47  if (other.confidence.has_value() && (!confidence.has_value() || other.confidence.value() < confidence.value())) {
48  confidence = other.confidence;
49  }
50 
51  return true;
52 }
53 
54 size_t GraphSelector::hash() const {
55  size_t val = 0;
56  if (graph) {
57  hashCombine(val, graph->hash());
58  } else {
59  hashCombine(val, 0);
60  }
61  if (perspective) {
62  hashCombine(val, std::hash<std::string_view>{}(perspective->iri()));
63  } else {
64  hashCombine(val, 0);
65  }
66  hashCombine(val, std::hash<bool>{}(occasional));
67  hashCombine(val, std::hash<bool>{}(uncertain));
68  hashCombine(val, std::hash<std::optional<double>>{}(end));
69  hashCombine(val, std::hash<std::optional<double>>{}(begin));
70  hashCombine(val, std::hash<std::optional<double>>{}(confidence));
71  return val;
72 }
73 
74 void GraphSelector::write(std::ostream &os) const {
75  os << std::setprecision(1);
76 
77  bool hasEpistemicOperator = false;
78  if (confidence.has_value()) {
79  hasEpistemicOperator = true;
80  if (confidence.value() > 0.999) {
81  os << 'K';
82  } else {
83  os << "B[" << confidence.value() << "]";
84  }
85  } else if (uncertain) {
86  hasEpistemicOperator = true;
87  os << 'B';
88  }
90  if (!hasEpistemicOperator) {
91  os << 'K';
92  }
93  os << '[' << perspective->iri() << ']';
94  }
95 
96  bool hasTemporalOperator = false;
97  if (occasional) {
98  hasTemporalOperator = true;
99  os << 'P';
100  }
101  if (begin.has_value() || end.has_value()) {
102  if (!hasTemporalOperator) {
103  os << 'H';
104  }
105  os << '[';
106  if (begin.has_value()) {
107  os << begin.value();
108  }
109  os << '-';
110  if (end.has_value()) {
111  os << end.value();
112  }
113  os << ']';
114  }
115 }
116 
117 void GraphSelector::set(const boost::property_tree::ptree &config) {
118  uncertain = config.get<bool>("uncertain", false);
119  occasional = config.get<bool>("occasional", false);
120  if (config.count("graph")) {
121  graph = Atom::Tabled(config.get<std::string>("graph"));
122  }
123  if (config.count("perspective")) {
124  perspective = std::make_shared<Perspective>(config.get<std::string>("perspective"));
125  }
126  if (config.count("begin")) {
127  begin = config.get<double>("begin");
128  }
129  if (config.count("end")) {
130  end = config.get<double>("end");
131  }
132  if (config.count("confidence")) {
133  confidence = config.get<double>("confidence");
134  }
135 }
136 
137 namespace knowrob {
139  static auto defaultSelector = std::make_shared<const GraphSelector>();
140  return defaultSelector;
141  }
142 }
143 
144 // optional member must be added with add_property
145 #define BOOST_PYTHON_ADD_OPTIONAL(X, Y) add_property(X, \
146  make_getter(Y, return_value_policy<return_by_value>()), \
147  make_setter(Y, return_value_policy<return_by_value>()))
148 
149 namespace knowrob::py {
150  template<>
152  using namespace boost::python;
153  class_<GraphSelector, std::shared_ptr<GraphSelector>>
154  ("GraphSelector", init<>())
155  .def_readwrite("graph", &GraphSelector::graph)
156  .def_readwrite("uncertain", &GraphSelector::uncertain)
157  .def_readwrite("occasional", &GraphSelector::occasional)
158  .BOOST_PYTHON_ADD_OPTIONAL("perspective", &GraphSelector::perspective)
159  .BOOST_PYTHON_ADD_OPTIONAL("begin", &GraphSelector::begin)
160  .BOOST_PYTHON_ADD_OPTIONAL("end", &GraphSelector::end)
161  .BOOST_PYTHON_ADD_OPTIONAL("confidence", &GraphSelector::confidence);
162  }
163 }
static std::shared_ptr< knowrob::Atom > Tabled(std::string_view stringForm)
Definition: Atom.cpp:40
static bool isEgoPerspective(std::string_view iri)
Definition: Perspective.cpp:27
TermRule & string()
Definition: terms.cpp:63
void createType< GraphSelector >()
GraphSelectorPtr DefaultGraphSelector()
std::shared_ptr< const GraphSelector > GraphSelectorPtr
Definition: GraphSelector.h:76
void hashCombine(std::size_t &seed, const std::size_t &v)
Definition: knowrob.cpp:39
void write(std::ostream &os) const override
std::optional< double > confidence
Definition: GraphSelector.h:50
std::optional< double > end
Definition: GraphSelector.h:46
PerspectivePtr perspective
Definition: GraphSelector.h:30
void set(const boost::property_tree::ptree &config)
std::optional< double > begin
Definition: GraphSelector.h:42
bool mergeWith(const GraphSelector &other)