knowrob  2.1.0
A Knowledge Base System for Cognition-enabled Robots
graph.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/spirit/include/phoenix.hpp>
7 #include "knowrob/queries/parsers/common.h"
8 #include "knowrob/queries/parsers/graph.h"
9 #include "knowrob/queries/parsers/terms.h"
10 #include "knowrob/queries/parsers/strings.h"
11 #include "knowrob/Logger.h"
12 #include "knowrob/semweb/GraphPattern.h"
13 #include "knowrob/semweb/GraphBuiltin.h"
14 
15 #define RETURN_GRAPH_PAT_RULE(expr) static GraphTermRule r(expr); return r
16 
17 using namespace knowrob;
18 
19 namespace knowrob::parsers::graph {
20  using namespace knowrob::parsers::terms;
21  namespace qi = boost::spirit::qi;
22 
24  RETURN_GRAPH_PAT_RULE((iri() >> '(' >> term() >> ',' >> term() >> ')')
25  [qi::_val = ptr_<GraphPattern>()(qi::_2, qi::_1, qi::_3)]);
26  }
27 
28  struct graph_parsers_struct {
29  graph_parsers_struct() {
30  using namespace boost::phoenix;
31 
32  term %= disjunction | brackets;
33  brackets %= ('(' >>term >> ')');
34 
35  disjunction = (((conjunction | brackets)
36  >> (qi::char_(';') | qi::char_('|'))
37  >> (disjunction | brackets))
38  [qi::_val = (qi::_1 | qi::_3)]
39  | conjunction[qi::_val = qi::_1]);
40  conjunction = (((atomic | brackets)
41  >> (qi::char_(',') | qi::char_('&'))
42  >> (conjunction | brackets))
43  [qi::_val = (qi::_1 & qi::_3)]
44  | atomic[qi::_val = qi::_1]);
45 
46  atomic %= pattern() | builtin;
47  builtin %= less | greater | leq | geq | eq | neq | bindCmd | min | max;
48  auto &t = terms::term();
49  less = (t >> '<' >> t) [qi::_val = bind(&GraphBuiltin::less, qi::_1, qi::_2)];
50  greater = (t >> '>' >> t) [qi::_val = bind(&GraphBuiltin::greater, qi::_1, qi::_2)];
51  leq = (t >> "<=" >> t) [qi::_val = bind(&GraphBuiltin::lessOrEqual, qi::_1, qi::_2)];
52  geq = (t >> ">=" >> t) [qi::_val = bind(&GraphBuiltin::greaterOrEqual, qi::_1, qi::_2)];
53  eq = (t >> '=' >> t) [qi::_val = bind(&GraphBuiltin::equal, qi::_1, qi::_2)];
54  neq = (t >> "!=" >> t) [qi::_val = bind(&GraphBuiltin::notEqual, qi::_1, qi::_2)];
55 
56  min = ((qi::string("min") >> '(' >> t >> ',' >> t >> ')' >> "as" >> terms::var())
57  [qi::_val = bind(&GraphBuiltin::min, qi::_4, qi::_2, qi::_3)]);
58  max = ((qi::string("max") >> '(' >> t >> ',' >> t >> ')' >> "as" >> terms::var())
59  [qi::_val = bind(&GraphBuiltin::max, qi::_4, qi::_2, qi::_3)]);
60  bindCmd = ((qi::string("bind") >> '(' >> t >> ')' >> "as" >> terms::var())
61  [qi::_val = bind(&GraphBuiltin::bind, qi::_3, qi::_2)]);
62  }
63 
65  GraphTermRule brackets;
66  GraphTermRule disjunction, conjunction;
68  GraphTermRule builtin;
69  GraphTermRule less, greater, leq, geq, eq, neq, bindCmd, min, max;
70  };
71 
72  auto &parsers() {
73  static graph_parsers_struct p;
74  return p;
75  }
76 
78  return parsers().term;
79  }
80 }
static BuiltinPtr greaterOrEqual(const TermPtr &a, const TermPtr &b)
Definition: GraphBuiltin.h:210
static BuiltinPtr max(const VariablePtr &var, const TermPtr &a, const TermPtr &b)
Definition: GraphBuiltin.h:157
static BuiltinPtr lessOrEqual(const TermPtr &a, const TermPtr &b)
Definition: GraphBuiltin.h:184
static BuiltinPtr equal(const TermPtr &a, const TermPtr &b)
Definition: GraphBuiltin.h:223
static BuiltinPtr notEqual(const TermPtr &a, const TermPtr &b)
Definition: GraphBuiltin.h:236
static BuiltinPtr less(const TermPtr &a, const TermPtr &b)
Definition: GraphBuiltin.h:171
static BuiltinPtr bind(const VariablePtr &var, const TermPtr &val)
Definition: GraphBuiltin.h:127
static BuiltinPtr greater(const TermPtr &a, const TermPtr &b)
Definition: GraphBuiltin.h:197
static BuiltinPtr min(const VariablePtr &var, const TermPtr &a, const TermPtr &b)
Definition: GraphBuiltin.h:142
#define RETURN_GRAPH_PAT_RULE(expr)
Definition: graph.cpp:15
boost::spirit::qi::rule< std::string::const_iterator, std::shared_ptr< GraphTerm >(), boost::spirit::ascii::space_type > GraphTermRule
Definition: graph.h:14
GraphTermRule & graphTerm()
Definition: graph.cpp:77
GraphTermRule & pattern()
Definition: graph.cpp:23
VariableRule & var()
Definition: terms.cpp:91
TermRule & string()
Definition: terms.cpp:63
TermRule & atomic()
Definition: terms.cpp:79
TermRule & term()
Definition: terms.cpp:136
boost::phoenix::function< make_shared_f< T > > ptr_
Definition: common.h:29
IRIAtomPtr iri(std::string_view ns, std::string_view name)
Definition: IRIAtom.cpp:62