knowrob  2.1.0
A Knowledge Base System for Cognition-enabled Robots
GraphQuery.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/semweb/GraphQuery.h"
7 #include "knowrob/semweb/GraphPattern.h"
8 #include "knowrob/semweb/GraphSequence.h"
9 #include "knowrob/formulas/Negation.h"
10 #include "knowrob/formulas/Disjunction.h"
11 #include "knowrob/semweb/GraphUnion.h"
12 #include "knowrob/formulas/Conjunction.h"
13 #include "knowrob/integration/python/utils.h"
14 
15 using namespace knowrob;
16 
17 static QueryContextPtr createQueryContext(const TriplePatternPtr &query) {
18  auto ctx = std::make_shared<QueryContext>();
19  ctx->queryFlags = QUERY_FLAG_ALL_SOLUTIONS;
20  query->getTripleFrame(ctx->selector);
21  return ctx;
22 }
23 
24 GraphQuery::GraphQuery(const std::shared_ptr<GraphTerm> &queryTerm, const QueryContextPtr &ctx)
25  : Query(ctx),
26  term_(queryTerm) {
27 }
28 
30  : Query(createQueryContext(query)),
31  term_(std::make_shared<GraphPattern>(query)) {
32 }
33 
35  : Query(ctx),
36  term_(std::make_shared<GraphPattern>(query)) {
37 }
38 
39 GraphQuery::GraphQuery(const std::vector<TriplePatternPtr> &query, const QueryContextPtr &ctx)
40  : Query(ctx),
41  term_(std::make_shared<GraphSequence>()) {
42  auto sequence = std::static_pointer_cast<GraphSequence>(term_);
43  for (auto &p: query) {
44  sequence->addPattern(p);
45  }
46 }
47 
48 static FormulaPtr toFormula_recursive(GraphTerm *term) { //NOLINT
49  switch (term->termType()) {
51  auto pattern = ((GraphPattern *) term)->value();
52  if (pattern->isNegated()) {
53  return std::make_shared<Negation>(pattern->predicate());
54  } else {
55  return pattern->predicate();
56  }
57  }
58  case GraphTermType::Union: {
59  auto &terms = ((GraphUnion *) term)->terms();
60  std::vector<FormulaPtr> formulae;
61  for (const auto &t: terms) {
62  auto phi = toFormula_recursive(t.get());
63  if (phi) {
64  formulae.push_back(phi);
65  }
66  }
67  return std::make_shared<Disjunction>(formulae);
68  }
70  auto &terms = ((GraphSequence *) term)->terms();
71  std::vector<FormulaPtr> formulae;
72  for (const auto &t: terms) {
73  auto phi = toFormula_recursive(t.get());
74  if (phi) {
75  formulae.push_back(phi);
76  }
77  }
78  return std::make_shared<Conjunction>(formulae);
79  }
81  break;
82  }
83  return nullptr;
84 }
85 
87  return toFormula_recursive(term_.get());
88 }
89 
90 void GraphQuery::write(std::ostream &os) const {
91  os << *term_;
92 }
93 
94 namespace knowrob::py {
95  template<>
97  using namespace boost::python;
98 
100 
101  class_<GraphQuery, std::shared_ptr<GraphQuery>, boost::noncopyable>
102  ("GraphQuery", init<const std::shared_ptr<GraphTerm> &>())
103  .def(init<const std::shared_ptr<GraphTerm> &, const QueryContextPtr &>())
104  .def(init<const TriplePatternPtr &>())
105  .def(init<const TriplePatternPtr &, const QueryContextPtr &>())
106  .def(init<const std::vector<TriplePatternPtr> &, const QueryContextPtr &>())
107  .def("term", &GraphQuery::term)
108  .def("toFormula", &GraphQuery::toFormula);
109  }
110 }
auto term() const
Definition: GraphQuery.h:48
FormulaPtr toFormula() const
Definition: GraphQuery.cpp:86
std::shared_ptr< GraphTerm > term_
Definition: GraphQuery.h:57
GraphQuery(const std::shared_ptr< GraphTerm > &query, const QueryContextPtr &ctx=DefaultQueryContext())
Definition: GraphQuery.cpp:24
void write(std::ostream &os) const override
Definition: GraphQuery.cpp:90
GraphTermRule & pattern()
Definition: graph.cpp:23
TermRule & term()
Definition: terms.cpp:136
void createType< GraphQuery >()
Definition: GraphQuery.cpp:96
void createType< GraphTerm >()
Definition: GraphTerm.cpp:90
std::shared_ptr< TriplePattern > TriplePatternPtr
@ QUERY_FLAG_ALL_SOLUTIONS
Definition: QueryFlag.h:15
std::shared_ptr< Formula > FormulaPtr
Definition: Formula.h:99
std::shared_ptr< const QueryContext > QueryContextPtr
Definition: QueryContext.h:41