6 #include <boost/python.hpp>
7 #include <knowrob/knowrob.h>
8 #include <knowrob/Logger.h>
9 #include <knowrob/ThreadPool.h>
12 #include <boost/uuid/uuid_generators.hpp>
13 #include <boost/uuid/uuid_io.hpp>
14 #include "knowrob/integration/python/PythonError.h"
15 #include "knowrob/integration/prolog/PrologEngine.h"
20 IRIAtom::Tabled(
"http://knowrob.org/kb/knowrob.owl#Self");
22 #define KNOWROB_EXECUTABLE_NAME "knowrob"
24 static bool initialized =
false;
34 static char noExec[] =
"<no-executable>";
40 static const auto GOLDEN_RATIO_HASH =
static_cast<size_t>(0x9e3779b9);
41 seed ^= v + GOLDEN_RATIO_HASH + (seed << 6) + (seed >> 2);
45 static boost::uuids::random_generator generator;
46 std::hash<std::string> str_hash;
47 os << std::setfill(
'0') << std::setw(8) << std::hex <<
48 str_hash(to_string(generator()));
52 std::stringstream pythonPath;
53 auto oldPath = std::getenv(
"PYTHONPATH");
55 pythonPath << oldPath <<
":";
58 (std::filesystem::path(KNOWROB_INSTALL_PREFIX) /
"knowrob").
string() <<
":"
59 << KNOWROB_SOURCE_DIR <<
":"
61 auto pythonPathStr = pythonPath.str();
62 KB_DEBUG(
"[KnowRob] using python path: {}", pythonPathStr);
63 setenv(
"PYTHONPATH", pythonPathStr.c_str(), 1);
67 if (initialized)
return;
73 std::cout.imbue(std::locale::classic());
89 KB_INFO(
"[KnowRob] static initialization done.");
90 KB_DEBUG(
"[KnowRob] source directory: {}", KNOWROB_SOURCE_DIR);
91 KB_DEBUG(
"[KnowRob] install prefix: {}", KNOWROB_INSTALL_PREFIX);
92 KB_DEBUG(
"[KnowRob] build directory: {}", KNOWROB_BUILD_DIR);
105 static void py_InitKnowRob1(boost::python::list py_argv) {
107 auto argc = boost::python::len(py_argv);
109 auto extracted = boost::python::extract<std::string>(py_argv[0]);
110 if (extracted.check() && !extracted().empty()) {
111 nameOfExecutable = extracted();
117 static void py_InitKnowRob2() {
118 auto sys = boost::python::import(
"sys");
119 auto py_argv = boost::python::extract<boost::python::list>(sys.attr(
"argv"));
120 py_InitKnowRob1(py_argv);
129 KB_INFO(
"[KnowRob] shutdown complete.");
140 def(
"InitKnowRobWithArgs", &py_InitKnowRob1,
"Initialize the Knowledge Base with arguments.");
141 def(
"InitKnowRob", &py_InitKnowRob2,
"Initialize the Knowledge Base using sys.argv.");
static uint32_t batchSize_
static IRIAtomPtr egoIRI_
static void finalizeProlog()
#define KNOWROB_EXECUTABLE_NAME
void staticKnowRobModuleInit()
char * NAME_OF_EXECUTABLE
std::shared_ptr< ThreadPool > DefaultThreadPool()
void hashCombine(std::size_t &seed, const std::size_t &v)
void InitKnowRob(int argc, char **argv, bool initPython=true)
char * getNameOfExecutable()
std::shared_ptr< IRIAtom > IRIAtomPtr
void insertUnique(std::ostream &os)