6 #include "knowrob/KnowledgeBase.h"
7 #include "knowrob/reasoner/DataDrivenReasoner.h"
8 #include "knowrob/reasoner/ReasonerManager.h"
9 #include "knowrob/integration/python/utils.h"
10 #include "knowrob/integration/python/gil.h"
11 #include "knowrob/queries/QueryParser.h"
26 return (
features_ &
static_cast<uint32_t
>(feature)) ==
static_cast<uint32_t
>(feature);
38 [](
const std::exception &exc) {
39 KB_ERROR(
"Error in reasoner update: {}", exc.what());
59 auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(
now -
lastUpdate_);
64 auto remaining = std::chrono::milliseconds(
72 std::thread timeout([&remaining,
this]() {
73 std::this_thread::sleep_for(remaining);
105 switch (event->eventType()) {
107 processAssertion(std::static_pointer_cast<reasoner::AssertionEvent>(event)->triples());
110 processRetraction(std::static_pointer_cast<reasoner::RetractionEvent>(event)->triples());
113 processReplacement(std::static_pointer_cast<reasoner::ReplacementEvent>(event)->triples());
127 auto query = std::make_shared<GraphQuery>(
term);
133 KB_WARN(
"Reasoner has no feature to invalidate itself, but still generated an invalidation event. Ignoring.");
143 for (
auto &triple: triples) {
167 std::set<TriplePtr> newTriples(triples.begin(), triples.end());
169 std::vector<TriplePtr> triplesToRemove, triplesToAdd;
171 std::set_difference(oldTriples.begin(), oldTriples.end(),
172 newTriples.begin(), newTriples.end(),
173 std::inserter(triplesToRemove, triplesToRemove.begin()));
175 std::set_difference(newTriples.begin(), newTriples.end(),
176 oldTriples.begin(), oldTriples.end(),
177 std::inserter(triplesToAdd, triplesToAdd.begin()));
179 for (
auto &triple: triplesToRemove) {
184 if (!triplesToAdd.empty()) {
187 if (!triplesToRemove.empty()) {
195 struct DataDrivenReasonerWrap :
public DataDrivenReasoner, boost::python::wrapper<DataDrivenReasoner> {
198 bool initializeReasoner(
const PropertyTree &config)
override {
199 return call_method<bool>(
self,
"initializeReasoner", config);
202 void start()
override {
207 call_method<void>(
self,
"start");
212 void stop()
override {
216 call_method<void>(
self,
"stop");
222 void update()
override { call_method<void>(
self,
"update"); }
231 enum_<DataDrivenReasoner::Feature>(
"DataDrivenReasonerFeature")
237 py::createType<reasoner::Event>();
239 class_<DataDrivenReasoner, std::shared_ptr<DataDrivenReasonerWrap>, bases<Reasoner>, boost::noncopyable>
240 (
"DataDrivenReasoner", init<>())
241 .def(
"enableFeature", &DataDrivenReasonerWrap::enableFeature)
242 .def(
"hasFeature", &DataDrivenReasonerWrap::hasFeature)
243 .def(
"emit", &DataDrivenReasonerWrap::emit)
244 .def(
"observe", +[](
DataDrivenReasoner &x,
const std::shared_ptr<GraphQuery> &query,
object &handler) {
258 .def(
"setUpdateInterval", &DataDrivenReasonerWrap::setUpdateInterval)
259 .def(
"updateInterval", &DataDrivenReasonerWrap::updateInterval)
261 .def(
"update", &DataDrivenReasonerWrap::update)
262 .def(
"start", &DataDrivenReasonerWrap::start, &DataDrivenReasonerWrap::start_default)
263 .def(
"stop", &DataDrivenReasonerWrap::stop, &DataDrivenReasonerWrap::stop_default);
void processReplacement(const std::vector< TriplePtr > &triples)
void setReasonerOrigin(const std::vector< TriplePtr > &triples)
std::shared_ptr< ThreadPool::Runner > updateRunner_
void observe(const std::shared_ptr< GraphQuery > &query, const BindingsHandler &handler)
void processAssertion(const std::vector< TriplePtr > &triples)
void setUpdateInterval(double intervalInSeconds)
void enableFeature(Feature feature)
std::chrono::duration< double > updateInterval_
bool hasFeature(Feature feature) const
void processRetraction(const std::vector< TriplePtr > &triples)
std::set< TriplePtr > inferredTriples_
void emit(const std::shared_ptr< reasoner::Event > &event)
void processInvalidation()
std::chrono::time_point< std::chrono::high_resolution_clock > lastUpdate_
static std::shared_ptr< GraphTerm > parseGraphTerm(const std::string &queryString)
ReasonerManager & reasonerManager() const
auto & reasonerName() const
auto reasonerLanguage() const
std::function< bool()> StopChecker
void createType< DataDrivenReasoner >()
std::shared_ptr< const Bindings > BindingsPtr
std::function< void(const BindingsPtr &)> BindingsHandler
std::shared_ptr< ThreadPool > DefaultThreadPool()