knowrob  2.1.0
A Knowledge Base System for Cognition-enabled Robots
GraphTerm.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 <boost/python/suite/indexing/vector_indexing_suite.hpp>
7 #include "knowrob/semweb/GraphTerm.h"
8 #include "knowrob/integration/python/utils.h"
9 #include "knowrob/semweb/GraphSequence.h"
10 #include "knowrob/semweb/GraphUnion.h"
11 #include "knowrob/semweb/GraphPattern.h"
12 #include "knowrob/integration/python/converter/vector.h"
13 #include "knowrob/semweb/GraphBuiltin.h"
14 
15 using namespace knowrob;
16 
17 namespace knowrob {
18  std::shared_ptr<GraphTerm> applyBindings(const std::shared_ptr<GraphTerm> &term, const Bindings &bindings) {
19  switch (term->termType()) {
21  auto pattern = std::static_pointer_cast<GraphPattern>(term);
22  auto orig = pattern->value();
23  auto substituted = applyBindings(orig, bindings);
24  if (orig != substituted) {
25  return std::make_shared<GraphPattern>(substituted);
26  }
27  break;
28  }
30  auto builtin = std::static_pointer_cast<GraphBuiltin>(term);
31  auto args = builtin->arguments();
32  std::vector<TermPtr> newArgs;
33  for (const auto &arg: args) {
34  newArgs.push_back(applyBindings(arg, bindings));
35  }
36  return std::make_shared<GraphBuiltin>(builtin->builtinType(),
37  builtin->functor(),
38  newArgs,
39  builtin->bindVar());
40  }
42  auto sequence = std::static_pointer_cast<GraphSequence>(term);
43  std::vector<std::shared_ptr<GraphTerm>> newTerms;
44  for (const auto &subTerm: sequence->terms()) {
45  newTerms.push_back(applyBindings(subTerm, bindings));
46  }
47  return std::make_shared<GraphSequence>(newTerms);
48  }
49  case GraphTermType::Union: {
50  auto unionTerm = std::static_pointer_cast<GraphUnion>(term);
51  std::vector<std::shared_ptr<GraphTerm>> newTerms;
52  for (const auto &subTerm: unionTerm->terms()) {
53  newTerms.push_back(applyBindings(subTerm, bindings));
54  }
55  return std::make_shared<GraphUnion>(newTerms);
56  }
57  }
58  return term;
59  }
60 
61  std::shared_ptr<GraphTerm> operator&(const std::shared_ptr<GraphTerm> &a, const std::shared_ptr<GraphTerm> &b) {
62  if (a->termType() == GraphTermType::Sequence) {
63  auto sequence = std::static_pointer_cast<GraphSequence>(a);
64  auto terms = sequence->terms();
65  terms.push_back(b);
66  return std::make_shared<GraphSequence>(terms);
67  } else {
68  return std::make_shared<GraphSequence>(std::vector<std::shared_ptr<GraphTerm>>{a, b});
69  }
70  }
71 
72  std::shared_ptr<GraphTerm> operator|(const std::shared_ptr<GraphTerm> &a, const std::shared_ptr<GraphTerm> &b) {
73  if (a->termType() == GraphTermType::Union) {
74  auto unionTerm = std::static_pointer_cast<GraphUnion>(a);
75  auto terms = unionTerm->terms();
76  terms.push_back(b);
77  return std::make_shared<GraphUnion>(terms);
78  } else {
79  return std::make_shared<GraphUnion>(std::vector<std::shared_ptr<GraphTerm>>{a, b});
80  }
81  }
82 }
83 
84 namespace knowrob::py {
85  std::shared_ptr<GraphTerm> applyBindings_graph(const std::shared_ptr<GraphTerm> &term, const Bindings &bindings) {
86  return applyBindings(term, bindings);
87  }
88 
89  template<>
91  using namespace boost::python;
92 
93  enum_<GraphTermType>("GraphTermType")
94  .value("Sequence", GraphTermType::Sequence)
95  .value("Union", GraphTermType::Union)
96  .value("Pattern", GraphTermType::Pattern)
97  .value("Builtin", GraphTermType::Builtin)
98  .export_values();
99 
100  class_<GraphTerm, std::shared_ptr<GraphTerm>, boost::noncopyable>
101  ("GraphTerm", no_init)
102  .def("isPattern", &GraphTerm::isPattern)
103  .def("isBuiltin", &GraphTerm::isBuiltin)
104  .def("termType", &GraphTerm::termType);
105 
106  class_<GraphPattern, bases<GraphTerm>, std::shared_ptr<GraphPattern>, boost::noncopyable>
107  ("GraphPattern", init<TriplePatternPtr>())
108  .def(init<const TermPtr &, const TermPtr &, const TermPtr &>())
109  .def("value", &GraphPattern::value, return_value_policy<copy_const_reference>());
110 
114 
115  def("applyBindings", applyBindings_graph);
116  // allow conversion between std::vector and python::list for FirstOrderLiteral objects.
117  typedef std::vector<std::shared_ptr<GraphTerm>> GraphTermList;
119  boost::python::class_<GraphTermList>("GraphTermList").def(
120  boost::python::vector_indexing_suite<GraphTermList, true>());
121  }
122 }
const auto & value() const
Definition: GraphPattern.h:29
bool isPattern() const
Definition: GraphTerm.h:33
bool isBuiltin() const
Definition: GraphTerm.h:38
auto termType() const
Definition: GraphTerm.h:43
GraphTermRule & pattern()
Definition: graph.cpp:23
TermRule & term()
Definition: terms.cpp:136
void createType< GraphUnion >()
Definition: GraphUnion.cpp:24
std::shared_ptr< GraphTerm > applyBindings_graph(const std::shared_ptr< GraphTerm > &term, const Bindings &bindings)
Definition: GraphTerm.cpp:85
void createType< GraphTerm >()
Definition: GraphTerm.cpp:90
void createType< GraphBuiltin >()
void createType< GraphSequence >()
FormulaPtr operator|(const FormulaPtr &phi, const FormulaPtr &psi)
Definition: Disjunction.cpp:55
FirstOrderLiteralPtr applyBindings(const FirstOrderLiteralPtr &lit, const Bindings &bindings)
FormulaPtr operator&(const FormulaPtr &phi, const FormulaPtr &psi)
Definition: Conjunction.cpp:34