knowrob  2.1.0
A Knowledge Base System for Cognition-enabled Robots
Token.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 "sstream"
7 #include "knowrob/queries/Token.h"
8 #include "knowrob/knowrob.h"
9 #include "knowrob/integration/python/utils.h"
10 #include "knowrob/queries/EndOfEvaluation.h"
11 #include "knowrob/queries/Answer.h"
12 
13 using namespace knowrob;
14 
15 size_t Token::hash() const {
16  size_t val = 0;
17  switch (tokenType()) {
19  // note currently there is only one control token type, so nothing needs to be done here for the moment.
20  hashCombine(val, uint8_t(tokenType()));
21  break;
23  hashCombine(val, ((const Answer *) this)->hashOfAnswer());
24  break;
25  }
26  return val;
27 }
28 
30  switch (tokenType()) {
32  // note currently there is only one control token type, so nothing needs to be done here for the moment.
33  return "EndOfEvaluation";
35  return ((const Answer *) this)->stringFormOfAnswer();
36  }
37  return "UnknownToken";
38 }
39 
40 namespace knowrob::py {
41  template<>
43  using namespace boost::python;
44  enum_<TokenType>("TokenType")
45  .value("CONTROL_TOKEN", TokenType::CONTROL_TOKEN)
46  .value("ANSWER_TOKEN", TokenType::ANSWER_TOKEN)
47  .export_values();
48  class_<Token, std::shared_ptr<Token>, boost::noncopyable>
49  ("Token", no_init)
50  .def("__repr__", &Token::stringForm)
51  .def("__hash__", &Token::hash)
52  .def("tokenType", &Token::tokenType)
53  .def("indicatesEndOfEvaluation", &Token::indicatesEndOfEvaluation);
54  class_<EndOfEvaluation, bases<Token>, std::shared_ptr<EndOfEvaluation>>
55  ("EndOfEvaluation", no_init)
56  .def("get", &EndOfEvaluation::get, return_value_policy<reference_existing_object>())
57  .staticmethod("get");
58  // allow implicit conversion from shared_ptr<Token> to shared_ptr<const Token>
59  register_ptr_to_python<std::shared_ptr<const Token> >();
60  implicitly_convertible<std::shared_ptr<Token>, std::shared_ptr<const Token> >();
61  // create subtypes
63  }
64 }
TokenType tokenType() const
Definition: Token.h:38
bool indicatesEndOfEvaluation() const
Definition: Token.h:63
size_t hash() const
Definition: Token.cpp:15
std::string stringForm() const
Definition: Token.cpp:29
TermRule & string()
Definition: terms.cpp:63
void createType< Token >()
Definition: Token.cpp:42
void createType< Answer >()
Definition: Answer.cpp:163
void hashCombine(std::size_t &seed, const std::size_t &v)
Definition: knowrob.cpp:39