knowrob  2.1.0
A Knowledge Base System for Cognition-enabled Robots
QueryParser.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/qi.hpp>
7 #include "knowrob/queries/QueryParser.h"
8 #include "knowrob/queries/QueryError.h"
9 #include "knowrob/queries/parsers/strings.h"
10 #include "knowrob/queries/parsers/terms.h"
11 #include "knowrob/queries/parsers/formula.h"
12 #include "knowrob/integration/python/utils.h"
13 #include "knowrob/formulas/ModalFormula.h"
14 #include "knowrob/queries/parsers/graph.h"
15 
16 using namespace knowrob;
17 
18 template<typename ResultType, typename RuleType>
19 static inline ResultType parse_(const std::string &queryString, const RuleType &rule) {
20  auto first = queryString.begin();
21  auto last = queryString.end();
22 
23  ResultType result;
24  bool r = boost::spirit::qi::phrase_parse(first, last, rule, boost::spirit::ascii::space, result);
25 
26  if (first == last && r) {
27  return result;
28  } else {
29  throw QueryError("Query string ({}) has invalid syntax.", queryString);
30  }
31 }
32 
34  return parse_<FormulaPtr, knowrob::parsers::formula::FormulaRule>(queryString,
36 }
37 
39  return parse_<PredicatePtr, knowrob::parsers::formula::PredicateRule>(queryString,
41 }
42 
43 std::shared_ptr<GraphTerm> QueryParser::parseGraphTerm(const std::string &queryString) {
44  return parse_<std::shared_ptr<GraphTerm>, knowrob::parsers::graph::GraphTermRule>(queryString,
46 }
47 
49  return parse_<FunctionPtr, knowrob::parsers::terms::FunctionRule>(queryString, knowrob::parsers::terms::function());
50 }
51 
53  return parse_<TermPtr, knowrob::parsers::terms::TermRule>(queryString, knowrob::parsers::terms::atomic());
54 }
55 
57  return parse_<std::string, knowrob::parsers::str::StringRule>(queryString, knowrob::parsers::str::atom_or_iri());
58 }
59 
60 namespace knowrob::py {
61  template<>
63  using namespace boost::python;
64  class_<QueryParser, boost::noncopyable>
65  ("QueryParser", no_init)
66  .def("parse", &QueryParser::parse).staticmethod("parse")
67  .def("parsePredicate", &QueryParser::parsePredicate).staticmethod("parsePredicate")
68  .def("parseGraphTerm", &QueryParser::parseGraphTerm).staticmethod("parseGraphTerm")
69  .def("parseFunction", &QueryParser::parseFunction).staticmethod("parseFunction")
70  .def("parseConstant", &QueryParser::parseConstant).staticmethod("parseConstant")
71  .def("parseRawAtom", &QueryParser::parseRawAtom).staticmethod("parseRawAtom");
72  }
73 }
static TermPtr parseConstant(const std::string &queryString)
Definition: QueryParser.cpp:52
static std::string parseRawAtom(const std::string &queryString)
Definition: QueryParser.cpp:56
static FunctionPtr parseFunction(const std::string &queryString)
Definition: QueryParser.cpp:48
static std::shared_ptr< GraphTerm > parseGraphTerm(const std::string &queryString)
Definition: QueryParser.cpp:43
static FormulaPtr parse(const std::string &queryString)
Definition: QueryParser.cpp:33
static PredicatePtr parsePredicate(const std::string &queryString)
Definition: QueryParser.cpp:38
PredicateRule & predicate()
Definition: formula.cpp:221
FormulaRule & formula()
Definition: formula.cpp:283
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
StringRule & atom_or_iri()
Definition: strings.cpp:58
FunctionRule & function()
Definition: terms.cpp:140
TermRule & string()
Definition: terms.cpp:63
TermRule & atomic()
Definition: terms.cpp:79
void createType< QueryParser >()
Definition: QueryParser.cpp:62
std::shared_ptr< Term > TermPtr
Definition: Term.h:117
std::shared_ptr< Formula > FormulaPtr
Definition: Formula.h:99
std::shared_ptr< Predicate > PredicatePtr
Definition: Predicate.h:77
std::shared_ptr< Function > FunctionPtr
Definition: Function.h:73