knowrob  2.1.0
A Knowledge Base System for Cognition-enabled Robots
DataDrivenReasoner.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_DATA_DRIVEN_REASONER_H
7 #define KNOWROB_DATA_DRIVEN_REASONER_H
8 
9 #include "Reasoner.h"
10 #include "ReasonerEvent.h"
11 
12 namespace knowrob {
20  class DataDrivenReasoner : public Reasoner {
21  public:
25  enum Feature {
26  // The reasoner does not have any special features.
27  NothingSpecial = 1 << 0,
28  // The reasoner updates itself, thus `update` does not need to be called externally.
29  UpdatesItself = 1 << 1,
30  // The reasoner invalidates itself, thus `update` does not need to be called periodically
31  // in case no invalidation happens.
32  InvalidatesItself = 1 << 2
33  };
34 
36 
41  void enableFeature(Feature feature);
42 
46  bool hasFeature(Feature feature) const;
47 
53  void setUpdateInterval(double intervalInSeconds);
54 
58  auto updateInterval() const { return updateInterval_; }
59 
66  void emit(const std::shared_ptr<reasoner::Event> &event);
67 
74  void observe(const std::shared_ptr<GraphQuery> &query, const BindingsHandler &handler);
75 
82  void observe(const std::string &queryString, const BindingsHandler &handler);
83 
89  virtual void update() = 0;
90 
94  virtual void start();
95 
99  virtual void stop();
100 
101  protected:
102  bool isUpdateQueued_ = false;
103  bool isRunning_ = false;
104  bool isInvalidated_ = true;
105  std::chrono::duration<double> updateInterval_ = std::chrono::seconds(1);
106  uint32_t features_ = NothingSpecial;
107  std::set<TriplePtr> inferredTriples_;
108  std::shared_ptr<ThreadPool::Runner> updateRunner_;
109  std::chrono::time_point<std::chrono::high_resolution_clock> lastUpdate_;
110 
111  void processAssertion(const std::vector<TriplePtr> &triples);
112 
113  void processRetraction(const std::vector<TriplePtr> &triples);
114 
115  void processReplacement(const std::vector<TriplePtr> &triples);
116 
118 
119  void setReasonerOrigin(const std::vector<TriplePtr> &triples);
120 
121  void doUpdate();
122 
123  void queueUpdate();
124  };
125 
126  using DataDrivenReasonerPtr = std::shared_ptr<DataDrivenReasoner>;
127 }
128 
129 #endif //KNOWROB_DATA_DRIVEN_REASONER_H
void processRetraction(const std::vector< TriplePtr > &triples)
bool hasFeature(Feature feature) const
void setUpdateInterval(double intervalInSeconds)
void emit(const std::shared_ptr< reasoner::Event > &event)
void observe(const std::string &queryString, const BindingsHandler &handler)
std::shared_ptr< ThreadPool::Runner > updateRunner_
void enableFeature(Feature feature)
void processAssertion(const std::vector< TriplePtr > &triples)
std::chrono::duration< double > updateInterval_
std::set< TriplePtr > inferredTriples_
void observe(const std::shared_ptr< GraphQuery > &query, const BindingsHandler &handler)
void setReasonerOrigin(const std::vector< TriplePtr > &triples)
std::chrono::time_point< std::chrono::high_resolution_clock > lastUpdate_
void processReplacement(const std::vector< TriplePtr > &triples)
TermRule & string()
Definition: terms.cpp:63
std::function< void(const BindingsPtr &)> BindingsHandler
Definition: Bindings.h:152
std::shared_ptr< DataDrivenReasoner > DataDrivenReasonerPtr