knowrob  2.1.0
A Knowledge Base System for Cognition-enabled Robots
terms.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/terms.h"
8 #include "knowrob/queries/parsers/strings.h"
9 #include "knowrob/queries/parsers/common.h"
10 #include "knowrob/terms/String.h"
11 #include "knowrob/terms/Numeric.h"
12 #include "knowrob/terms/Variable.h"
13 #include "knowrob/terms/ListTerm.h"
14 #include "knowrob/terms/Function.h"
15 #include "knowrob/terms/IRIAtom.h"
16 #include "knowrob/terms/Blank.h"
17 
18 #define RETURN_TERM_RULE(expr) static TermRule r(expr); return r
19 #define RETURN_ATOM_RULE(expr) static AtomRule r(expr); return r
20 #define RETURN_VAR_RULE(expr) static VariableRule r(expr); return r
21 #define RETURN_FUNCTION_RULE(expr) static FunctionRule r(expr); return r
22 
23 static knowrob::AtomPtr makeAtom(std::string_view stringForm) {
24  switch (knowrob::rdfNodeTypeGuess(stringForm)) {
26  return knowrob::IRIAtom::Tabled(stringForm);
28  return knowrob::Blank::Tabled(stringForm);
30  break;
31  }
32  return knowrob::Atom::Tabled(stringForm);
33 }
34 
35 static knowrob::AtomPtr makeIRI(std::string_view stringForm) {
36  return knowrob::IRIAtom::Tabled(stringForm);
37 }
38 
39 static knowrob::TermPtr makeXSD(std::string_view value, const knowrob::AtomPtr &typeIRI) {
40  return knowrob::XSDAtomic::create(value, typeIRI->stringForm());
41 }
42 
43 static std::vector<knowrob::TermPtr> createTermVector2(const knowrob::TermPtr &a, const knowrob::TermPtr &b) {
44  return {a, b};
45 }
46 
47 namespace knowrob::parsers::terms {
48  using namespace knowrob::parsers::str;
49  namespace qi = boost::spirit::qi;
50 
52  RETURN_ATOM_RULE(str::iri()[qi::_val = boost::phoenix::bind(&makeIRI, qi::_1)]);
53  }
54 
56  RETURN_ATOM_RULE(str::atom()[qi::_val = boost::phoenix::bind(&makeAtom, qi::_1)]);
57  }
58 
61  }
62 
64  RETURN_TERM_RULE(double_quotes()[qi::_val = ptr_<String>()(qi::_1)]);
65  }
66 
68  RETURN_TERM_RULE(qi::double_[qi::_val = ptr_<Double>()(qi::_1)]);
69  }
70 
72  RETURN_TERM_RULE(str::blank()[qi::_val = ptr_<String>()(qi::_1)]);
73  }
74 
76  RETURN_TERM_RULE((xsd_value() >> "^^" >> iri())[qi::_val = boost::phoenix::bind(&makeXSD, qi::_1, qi::_2)]);
77  }
78 
80  RETURN_TERM_RULE(atom() | string() | xsd() | number());
81  }
82 
84  RETURN_VAR_RULE(str::upper_prefix()[qi::_val = ptr_<Variable>()(qi::_1)]);
85  }
86 
88  RETURN_VAR_RULE('?' >> str::lower_prefix()[qi::_val = ptr_<Variable>()(qi::_1)]);
89  }
90 
93  }
94 
96  RETURN_TERM_RULE(('[' >> (atomic() % ',') >> ']')[qi::_val = ptr_<ListTerm>()(qi::_1)]);
97  }
98 
100  RETURN_TERM_RULE(qi::attr(ListTerm::nil()));
101  }
102 
104  static auto equalFunctor = Atom::Tabled("=");
105  RETURN_TERM_RULE((atom() >> '=' >> atomic())
106  [qi::_val = ptr_<Function>()(equalFunctor,
107  boost::phoenix::bind(&createTermVector2, qi::_1, qi::_2))]);
108  }
109 
112  }
113 
115  RETURN_TERM_RULE(('[' >> (option() % ',') >> ']')[qi::_val = ptr_<ListTerm>()(qi::_1)]);
116  }
117 
120  }
121 
122  struct term_and_function_parsers {
123  term_and_function_parsers() {
124  function = ((atom() >> '(' >> (term % ',') >> ')') [qi::_val = ptr_<Function>()(qi::_1, qi::_2)]);
125  term %= function | atomic() | var() | atomic_list();
126  }
127  TermRule term;
128  FunctionRule function;
129  };
130 
132  static term_and_function_parsers p;
133  return p;
134  }
135 
138  }
139 
140  FunctionRule &function() {
142  }
143 }
static std::shared_ptr< knowrob::Atom > Tabled(std::string_view stringForm)
Definition: Atom.cpp:40
static std::shared_ptr< Blank > Tabled(std::string_view stringForm)
Definition: Blank.cpp:12
static std::shared_ptr< IRIAtom > Tabled(std::string_view stringForm)
Definition: IRIAtom.cpp:25
static std::shared_ptr< ListTerm > nil()
Definition: ListTerm.cpp:22
static std::shared_ptr< XSDAtomic > create(std::string_view lexicalForm, std::string_view xsdTypeIRI)
Definition: XSDAtomic.cpp:17
const AtomPtr equalFunctor
Definition: GraphBuiltin.h:73
StringRule & iri()
Definition: strings.cpp:49
StringRule & upper_prefix()
Definition: strings.cpp:37
StringRule & blank()
Definition: strings.cpp:62
StringRule & lower_prefix()
Definition: strings.cpp:33
StringRule & double_quotes()
Definition: strings.cpp:29
StringRule & xsd_value()
Definition: strings.cpp:70
StringRule & atom()
Definition: strings.cpp:54
VariableRule & var()
Definition: terms.cpp:91
VariableRule & var_question()
Definition: terms.cpp:87
TermRule & key_value_pair()
Definition: terms.cpp:103
boost::spirit::qi::rule< std::string::const_iterator, std::shared_ptr< Function >(), boost::spirit::ascii::space_type > FunctionRule
Definition: terms.h:19
AtomRule & atom_regular()
Definition: terms.cpp:55
TermRule & number()
Definition: terms.cpp:67
VariableRule & var_upper()
Definition: terms.cpp:83
boost::spirit::qi::rule< std::string::const_iterator, std::shared_ptr< Variable >(), boost::spirit::ascii::space_type > VariableRule
Definition: terms.h:17
TermRule & blank()
Definition: terms.cpp:71
TermRule & nil()
Definition: terms.cpp:99
TermRule & option()
Definition: terms.cpp:110
AtomRule & iri()
Definition: terms.cpp:51
TermRule & options_or_nil()
Definition: terms.cpp:118
TermRule & string()
Definition: terms.cpp:63
TermRule & xsd()
Definition: terms.cpp:75
AtomRule & atom()
Definition: terms.cpp:59
TermRule & atomic_list()
Definition: terms.cpp:95
TermRule & atomic()
Definition: terms.cpp:79
boost::spirit::qi::rule< std::string::const_iterator, std::shared_ptr< Atom >(), boost::spirit::ascii::space_type > AtomRule
Definition: terms.h:18
TermRule & term()
Definition: terms.cpp:136
boost::spirit::qi::rule< std::string::const_iterator, std::shared_ptr< Term >(), boost::spirit::ascii::space_type > TermRule
Definition: terms.h:16
TermRule & options()
Definition: terms.cpp:114
auto & term_and_function()
Definition: terms.cpp:131
boost::phoenix::function< make_shared_f< T > > ptr_
Definition: common.h:29
RDFNodeType rdfNodeTypeGuess(std::string_view str)
Definition: RDFNode.cpp:11
std::shared_ptr< Term > TermPtr
Definition: Term.h:117
std::shared_ptr< Atom > AtomPtr
Definition: Atom.h:69
#define RETURN_FUNCTION_RULE(expr)
Definition: terms.cpp:21
#define RETURN_ATOM_RULE(expr)
Definition: terms.cpp:19
#define RETURN_VAR_RULE(expr)
Definition: terms.cpp:20
#define RETURN_TERM_RULE(expr)
Definition: terms.cpp:18