FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
knowrob/src/knowrob.cpp at dev · knowrob/knowrob · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
knowrob
/
knowrob
Public
Notifications
You must be signed in to change notification settings
Fork
112
Star
159
Code
Issues
4
Pull requests
2
Discussions
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Discussions
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
knowrob
/
src
/
knowrob.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
144 lines (127 loc) · 4.73 KB
Breadcrumbs
knowrob
/
src
/
knowrob.cpp
Copy path
File metadata and controls
144 lines (127 loc) · 4.73 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*
* This file is part of KnowRob, please consult
* https://github.com/knowrob/knowrob for license details.
*/
#
include
<
boost/python.hpp
>
#
include
<
knowrob/knowrob.h
>
#
include
<
knowrob/Logger.h
>
#
include
<
knowrob/ThreadPool.h
>
#
include
<
filesystem
>
#
include
<
iostream
>
#
include
<
boost/uuid/uuid_generators.hpp
>
#
include
<
boost/uuid/uuid_io.hpp
>
#
include
"
knowrob/integration/python/PythonError.h
"
#
include
"
knowrob/integration/prolog/PrologEngine.h
"
uint32_t
knowrob::GlobalSettings::batchSize_ =
500u
;
knowrob::IRIAtomPtr knowrob::GlobalSettings::egoIRI_ =
IRIAtom::Tabled
(
"
http://knowrob.org/kb/knowrob.owl#Self
"
);
#
define
KNOWROB_EXECUTABLE_NAME
"
knowrob
"
static
bool
initialized =
false
;
namespace
knowrob
{
//
stores the name of the executable as provided in argv[0]
char
*
NAME_OF_EXECUTABLE
=
nullptr
;
char
*
getNameOfExecutable
() {
if
(
NAME_OF_EXECUTABLE
) {
return
knowrob::
NAME_OF_EXECUTABLE
;
}
else
{
static
char
noExec[] =
"
<no-executable>
"
;
return
noExec;
}
}
void
hashCombine
(std::
size_t
&seed,
const
std::
size_t
&v) {
static
const
auto
GOLDEN_RATIO_HASH
=
static_cast
<
size_t
>(
0x9e3779b9
);
seed ^= v +
GOLDEN_RATIO_HASH
+ (seed <<
6
) + (seed >>
2
);
}
void
insertUnique
(std::ostream &os) {
static
boost::uuids::random_generator generator;
std::hash<std::string> str_hash;
os <<
std::setfill
(
'
0
'
) <<
std::setw
(
8
) << std::hex <<
str_hash
(
to_string
(
generator
()));
}
void
InitPythonPath
() {
std::stringstream pythonPath;
auto
oldPath =
std::getenv
(
"
PYTHONPATH
"
);
if
(oldPath) {
pythonPath << oldPath <<
"
:
"
;
}
pythonPath <<
(
std::filesystem::path
(
KNOWROB_INSTALL_PREFIX
) /
"
knowrob
"
).
string
() <<
"
:
"
<<
KNOWROB_SOURCE_DIR
<<
"
:
"
<<
KNOWROB_BUILD_DIR
;
auto
pythonPathStr = pythonPath.
str
();
KB_DEBUG
(
"
[KnowRob] using python path: {}
"
, pythonPathStr);
setenv
(
"
PYTHONPATH
"
, pythonPathStr.
c_str
(),
1
);
}
void
InitKnowRob
(
char
*nameOfExecutable,
bool
initPython) {
if
(initialized)
return
;
//
remember the program name.
//
it is assumed here that argv stays valid during program execution.
knowrob::
NAME_OF_EXECUTABLE
= nameOfExecutable;
//
set the locale to classic to avoid problems with number formatting,
//
especially regarding use of dot or comma as decimal separator.
std::cout.
imbue
(
std::locale::classic
());
//
configure the logger
Logger::initialize
();
//
Allow Python to load modules KnowRob-related directories.
InitPythonPath
();
if
(initPython) {
//
Start a Python interpreter if it is not already initialized
Py_Initialize
();
//
Release the GIL which is acquired by Py_Initialize.
//
If we do not release it, then no other thread would be able
//
to run Python code.
//
So instead we always need to acquire the GIL in C++ code sections
//
that interact with Python (except of when the C++ code is launched
//
within Python in which case it actually already has the GIL).
PyEval_SaveThread
();
}
KB_INFO
(
"
[KnowRob] static initialization done.
"
);
KB_DEBUG
(
"
[KnowRob] source directory: {}
"
,
KNOWROB_SOURCE_DIR
);
KB_DEBUG
(
"
[KnowRob] install prefix: {}
"
,
KNOWROB_INSTALL_PREFIX
);
KB_DEBUG
(
"
[KnowRob] build directory: {}
"
,
KNOWROB_BUILD_DIR
);
initialized =
true
;
}
void
InitKnowRob
(
int
argc,
char
**argv,
bool
initPython) {
if
(argc >
0
) {
InitKnowRob
(argv[
0
], initPython);
}
else
{
static
std::string nameOfExecutable =
KNOWROB_EXECUTABLE_NAME
;
InitKnowRob
(nameOfExecutable.
data
(), initPython);
}
}
static
void
py_InitKnowRob1
(boost::python::list py_argv) {
static
std::string nameOfExecutable =
KNOWROB_EXECUTABLE_NAME
;
auto
argc =
boost::python::len
(py_argv);
if
(argc >
0
) {
auto
extracted = boost::python::extract<std::string>(py_argv[
0
]);
if
(extracted.
check
() && !
extracted
().
empty
()) {
nameOfExecutable =
extracted
();
}
}
knowrob::InitKnowRob
(nameOfExecutable.
data
(),
false
);
}
static
void
py_InitKnowRob2
() {
auto
sys =
boost::python::import
(
"
sys
"
);
auto
py_argv = boost::python::extract<boost::python::list>(sys.
attr
(
"
argv
"
));
py_InitKnowRob1
(py_argv);
}
void
ShutdownKnowRob
() {
//
NOTE: Py_Finalize() should not be called when using boost python according to docs.
//
Py_Finalize();
//
stop the thread pool, join all remaining threads
DefaultThreadPool
()->
shutdown
();
PrologEngine::finalizeProlog
();
KB_INFO
(
"
[KnowRob] shutdown complete.
"
);
}
}
namespace
knowrob
::py {
void
staticKnowRobModuleInit
() {
using
namespace
boost
::python
;
using
namespace
knowrob
;
//
///////////////////////////////////////////////////
//
mappings for static functions
def
(
"
InitKnowRobWithArgs
"
, &py_InitKnowRob1,
"
Initialize the Knowledge Base with arguments.
"
);
def
(
"
InitKnowRob
"
, &py_InitKnowRob2,
"
Initialize the Knowledge Base using sys.argv.
"
);
def
(
"
ShutdownKnowRob
"
, &ShutdownKnowRob);
}
}
Back
|
FazBrowse Home
|
New Git URL