knowrob  2.1.0
A Knowledge Base System for Cognition-enabled Robots
Bindings.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_BINDINGS_H_
7 #define KNOWROB_BINDINGS_H_
8 
9 #include <queue>
10 #include <functional>
11 #include <map>
12 #include <memory>
13 #include <ostream>
14 #include "Term.h"
15 #include "Variable.h"
16 #include "knowrob/formulas/Formula.h"
17 #include "Atomic.h"
18 #include "knowrob/Printable.h"
19 
20 namespace knowrob {
28  class Bindings : public Printable {
29  public:
33  using Map = std::map<std::string_view, std::pair<std::shared_ptr<Variable>, TermPtr>>;
34  using VarMap = std::map<std::shared_ptr<Variable>, TermPtr>;
35 
36  Bindings() = default;
37 
41  explicit Bindings(const std::map<std::shared_ptr<Variable>, TermPtr> &mapping);
42 
43  ~Bindings() override = default;
44 
49  bool operator==(const Bindings &other) const;
50 
55  void operator+=(const Bindings &other);
56 
60  bool empty() const { return mapping_.empty(); }
61 
65  auto size() const { return mapping_.size(); }
66 
70  auto begin() const { return mapping_.begin(); }
71 
75  auto end() const { return mapping_.end(); }
76 
80  auto find(std::string_view varName) const { return mapping_.find(varName); }
81 
87  void set(const std::shared_ptr<Variable> &var, const TermPtr &term);
88 
97  const TermPtr &get(std::string_view varName) const;
98 
107  std::shared_ptr<Atomic> getAtomic(std::string_view varName) const;
108 
114  bool contains(std::string_view varName) const;
115 
123  bool unifyWith(const Bindings &other);
124 
131  bool isConsistentWith(const Bindings &other) const;
132 
136  size_t hash() const;
137 
141  static std::shared_ptr<const Bindings> emptyBindings();
142 
143  // Printable interface
144  void write(std::ostream &os) const override;
145 
146  protected:
147  Map mapping_;
148  };
149 
150  // alias declaration
151  using BindingsPtr = std::shared_ptr<const Bindings>;
152  using BindingsHandler = std::function<void(const BindingsPtr &)>;
153 
160  TermPtr applyBindings(const TermPtr &term, const Bindings &bindings);
161 
168  FormulaPtr applyBindings(const FormulaPtr &phi, const Bindings &bindings);
169 }
170 
171 #endif //KNOWROB_BINDINGS_H_
std::shared_ptr< Atomic > getAtomic(std::string_view varName) const
auto begin() const
Definition: Bindings.h:70
bool unifyWith(const Bindings &other)
void operator+=(const Bindings &other)
bool isConsistentWith(const Bindings &other) const
auto end() const
Definition: Bindings.h:75
~Bindings() override=default
void write(std::ostream &os) const override
bool contains(std::string_view varName) const
size_t hash() const
void set(const std::shared_ptr< Variable > &var, const TermPtr &term)
static std::shared_ptr< const Bindings > emptyBindings()
bool operator==(const Bindings &other) const
const TermPtr & get(std::string_view varName) const
auto size() const
Definition: Bindings.h:65
Bindings()=default
bool empty() const
Definition: Bindings.h:60
Bindings(const std::map< std::shared_ptr< Variable >, TermPtr > &mapping)
auto find(std::string_view varName) const
Definition: Bindings.h:80
std::map< std::shared_ptr< Variable >, TermPtr > VarMap
Definition: Bindings.h:34
std::map< std::string_view, std::pair< std::shared_ptr< Variable >, TermPtr > > Map
Definition: Bindings.h:33
VariableRule & var()
Definition: terms.cpp:91
FunctionRule & function()
Definition: terms.cpp:140
TermRule & term()
Definition: terms.cpp:136
std::shared_ptr< const Bindings > BindingsPtr
Definition: Bindings.h:151
std::shared_ptr< Term > TermPtr
Definition: Term.h:117
std::shared_ptr< Formula > FormulaPtr
Definition: Formula.h:99
std::function< void(const BindingsPtr &)> BindingsHandler
Definition: Bindings.h:152
FirstOrderLiteralPtr applyBindings(const FirstOrderLiteralPtr &lit, const Bindings &bindings)