knowrob  2.1.0
A Knowledge Base System for Cognition-enabled Robots
PrologEngine.h
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 #ifndef KNOWROB_PROLOG_THREAD_POOL_H_
7 #define KNOWROB_PROLOG_THREAD_POOL_H_
8 
9 #include "filesystem"
10 #include "knowrob/ThreadPool.h"
11 #include "PrologTerm.h"
12 
13 namespace knowrob {
22  class PrologEngine : public ThreadPool {
23  public:
26  using Solution = std::map<Variable, TermPtr, std::less<>>;
27 
31  explicit PrologEngine(uint32_t maxNumThreads = 0);
32 
36  static void initializeProlog();
37 
41  static void finalizeProlog();
42 
46  static bool isPrologInitialized() { return isPrologInitialized_; }
47 
53  static bool eval(const GoalFactory &goalFactory);
54 
60  static std::optional<Solution> oneSolution(const GoalFactory &goalFactory);
61 
67  static std::vector<Solution> allSolutions(const GoalFactory &goalFactory);
68 
74  static void query(const GoalFactory &goalFactory, const BindingsHandler &callback);
75 
84  static bool consult(const std::filesystem::path &uri, const char *module = {});
85 
91  static void pushGoal(const std::shared_ptr<ThreadPool::Runner> &goal, const ErrorHandler &errHandler);
92 
98  static void pushGoalAndJoin(const std::shared_ptr<ThreadPool::Runner> &goal);
99 
106  static std::filesystem::path getPrologPath(const std::filesystem::path &filename);
107 
114  static std::filesystem::path getResourcePath(const std::filesystem::path &filename);
115 
116  protected:
117  static bool isPrologInitialized_;
118  static std::vector<std::string> arguments_;
119  static std::optional<PrologEngine> self_;
120 
121  // Override ThreadPool
122  bool initializeWorker() override;
123 
124  static void expandSearchPaths();
125  };
126 }
127 
128 #define PROLOG_ENGINE_EVAL(term) PrologEngine::eval([&]() { return term; })
129 #define PROLOG_ENGINE_ONE_SOL(term) PrologEngine::oneSolution([&]() { return term; })
130 #define PROLOG_ENGINE_ALL_SOL(term) PrologEngine::allSolutions([&]() { return term; })
131 #define PROLOG_ENGINE_QUERY(term, callback) PrologEngine::query([&]() { return term; }, callback)
132 
133 #endif //KNOWROB_PROLOG_THREAD_POOL_H_
static bool eval(const GoalFactory &goalFactory)
static std::filesystem::path getPrologPath(const std::filesystem::path &filename)
static std::vector< std::string > arguments_
Definition: PrologEngine.h:118
std::map< Variable, TermPtr, std::less<> > Solution
Definition: PrologEngine.h:26
static void pushGoalAndJoin(const std::shared_ptr< ThreadPool::Runner > &goal)
PrologEngine(uint32_t maxNumThreads=0)
static std::filesystem::path getResourcePath(const std::filesystem::path &filename)
static void pushGoal(const std::shared_ptr< ThreadPool::Runner > &goal, const ErrorHandler &errHandler)
bool initializeWorker() override
static bool isPrologInitialized()
Definition: PrologEngine.h:46
static void expandSearchPaths()
static std::optional< Solution > oneSolution(const GoalFactory &goalFactory)
static void query(const GoalFactory &goalFactory, const BindingsHandler &callback)
static bool consult(const std::filesystem::path &uri, const char *module={})
static std::optional< PrologEngine > self_
Definition: PrologEngine.h:119
static bool isPrologInitialized_
Definition: PrologEngine.h:117
std::function< PrologTerm()> GoalFactory
Definition: PrologEngine.h:25
ThreadPool::ExceptionHandler ErrorHandler
Definition: PrologEngine.h:24
static void finalizeProlog()
static std::vector< Solution > allSolutions(const GoalFactory &goalFactory)
static void initializeProlog()
std::function< void(const std::exception &)> ExceptionHandler
Definition: ThreadPool.h:32
FunctionRule & function()
Definition: terms.cpp:140
std::function< void(const BindingsPtr &)> BindingsHandler
Definition: Bindings.h:152