knowrob  2.1.0
A Knowledge Base System for Cognition-enabled Robots
knowrob_module.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 <iostream>
7 #include <functional>
8 #include <future>
9 #include <memory>
10 #include <boost/python.hpp>
11 #include <boost/python/suite/indexing/vector_indexing_suite.hpp>
12 
13 #include "knowrob/Logger.h"
14 #include "knowrob/terms/ListTerm.h"
15 #include "knowrob/terms/Numeric.h"
16 #include "knowrob/terms/Blank.h"
17 #include "knowrob/terms/String.h"
18 #include "knowrob/terms/IRIAtom.h"
19 #include "knowrob/formulas/Formula.h"
20 #include "knowrob/queries/QueryContext.h"
21 #include "knowrob/queries/QueryParser.h"
22 #include "knowrob/reasoner/Reasoner.h"
23 #include "knowrob/KnowledgeBase.h"
24 #include "knowrob/integration/python/converter.h"
25 #include "knowrob/integration/python/utils.h"
26 #include "knowrob/knowrob.h"
27 #include "knowrob/integration/InterfaceUtils.h"
28 #include "knowrob/storage/redland/RedlandModel.h"
29 #include "knowrob/integration/prolog/PrologBackend.h"
30 #include "knowrob/storage/mongo/MongoKnowledgeGraph.h"
31 
32 #ifndef MODULENAME
33 #define MODULENAME knowrob
34 #endif
35 
36 using namespace knowrob;
37 
38 static inline void register_common_types() {
43 }
44 
45 static inline void register_term_types() {
59  // allow conversion between std::vector and python::list for Term objects.
60  typedef std::vector<TermPtr> TermList;
62  boost::python::class_<TermList>("TermList").def(boost::python::vector_indexing_suite<TermList, true>());
63 }
64 
65 static inline void register_formula_types() {
67  // allow conversion between std::vector and python::list for Formula objects.
68  typedef std::vector<FormulaPtr> FormulaList;
70  boost::python::class_<FormulaList>("FormulaList").def(boost::python::vector_indexing_suite<FormulaList, true>());
71  // allow conversion between std::vector and python::list for FirstOrderLiteral objects.
72  typedef std::vector<std::shared_ptr<FirstOrderLiteral>> LiteralList;
74  boost::python::class_<LiteralList>("LiteralList").def(boost::python::vector_indexing_suite<LiteralList, true>());
75 }
76 
77 static inline void register_triple_types() {
82  // allow conversion between std::vector and python::list for Triple objects.
83  typedef std::vector<std::shared_ptr<Triple>> TripleList;
85  boost::python::class_<TripleList>("TripleList").def(boost::python::vector_indexing_suite<TripleList, true>());
86 }
87 
89  using namespace boost::python;
90  using namespace knowrob::py;
91 
92  // convert std::string_view to python::str and vice versa.
97 
99  // mappings for KnowRob types
101  register_common_types();
102  register_term_types();
103  register_formula_types();
104  register_triple_types();
105 
116 
117  // register builtin storage types.
118  // without it seems the to-python conversion is not mapping to QueryableStorage but only Storage type!
119  class_<RedlandModel, std::shared_ptr<RedlandModel>, bases<QueryableStorage>, boost::noncopyable>
120  ("RedlandModel", init<>());
121  class_<PrologBackend, std::shared_ptr<PrologBackend>, bases<QueryableStorage>, boost::noncopyable>
122  ("PrologBackend", init<>());
123  class_<MongoKnowledgeGraph, std::shared_ptr<MongoKnowledgeGraph>, bases<QueryableStorage>, boost::noncopyable>
124  ("MongoKnowledgeGraph", init<>());
125 
127  // mappings for optionals used in the structs above
129  // Note: At the moment each optional must be listed individually in the module declaration.
130  // It would be nice if this could be avoided...
135 }
#define MODULENAME
BOOST_PYTHON_MODULE(MODULENAME)
void createType< ModalOperator >()
void createType< Blank >()
Definition: Blank.cpp:35
void createType< KnowledgeBase >()
void createType< Variable >()
Definition: Variable.cpp:37
void register_pair_converter()
Definition: pair.h:24
void register_string_view_converter()
Definition: string_view.h:33
void createType< String >()
Definition: String.cpp:21
void createType< GraphSelector >()
void createType< QueryContext >()
void createType< TriplePattern >()
void createType< Atom >()
Definition: Atom.cpp:63
void createType< InterfaceUtils >()
void createType< Storage >()
Definition: Storage.cpp:49
void createType< PropertyTree >()
void createType< Perspective >()
Definition: Perspective.cpp:44
void createType< Atomic >()
Definition: Atomic.cpp:68
void createType< TokenStream >()
void createType< Logger >()
Definition: Logger.cpp:164
void staticKnowRobModuleInit()
Definition: knowrob.cpp:134
void createType< Function >()
Definition: Function.cpp:69
void createType< Numeric >()
Definition: Numeric.cpp:27
void createType< ListTerm >()
Definition: ListTerm.cpp:45
void register_dict_to_map_converter()
Definition: dict.h:59
void createType< Term >()
Definition: Term.cpp:82
void createType< Reasoner >()
Definition: Reasoner.cpp:65
void createType< Triple >()
Definition: Triple.cpp:395
void createType< DataSourceHandler >()
void createType< IRIAtom >()
Definition: IRIAtom.cpp:76
void createType< TripleContainer >()
void createType< RDFNode >()
Definition: RDFNode.cpp:25
void createType< XSDAtomic >()
Definition: XSDAtomic.cpp:97
void createType< Bindings >()
Definition: Bindings.cpp:295
void createType< Formula >()
Definition: Formula.cpp:48
void createType< DataSource >()
Definition: DataSource.cpp:144
void register_list_converter()
Definition: list.h:29
void createType< QueryParser >()
Definition: QueryParser.cpp:62