6 #include "knowrob/terms/Bindings.h"
7 #include "knowrob/terms/Unifier.h"
8 #include "knowrob/knowrob.h"
9 #include "knowrob/formulas/CompoundFormula.h"
10 #include "knowrob/formulas/ModalFormula.h"
11 #include "knowrob/formulas/Implication.h"
12 #include "knowrob/formulas/Negation.h"
13 #include "knowrob/formulas/Conjunction.h"
14 #include "knowrob/formulas/Disjunction.h"
15 #include "knowrob/terms/Function.h"
16 #include "knowrob/integration/python/utils.h"
17 #include "knowrob/integration/python/converter/dict.h"
18 #include "knowrob/Logger.h"
23 for (
const auto &pair: mapping) {
24 set(pair.first, pair.second);
34 auto &val1 = x.second.second;
35 auto &val2 = other.
get(x.first);
37 if (!(*val1 == *val2))
return false;
38 }
else if (val1 || val2) {
48 set(x.second.first, x.second.second);
65 return it->second.second;
72 static const std::shared_ptr<Atomic> null_term;
76 return std::static_pointer_cast<Atomic>(
term);
83 auto seed =
static_cast<size_t>(0);
87 hashCombine(seed, std::hash<std::string_view>{}(item.first));
89 auto value_hash =
static_cast<size_t>(0);
90 if (item.second.second) {
91 value_hash = item.second.second->hash();
100 static const auto empty = std::make_shared<Bindings>();
105 const Map *map_small;
106 const Map *map_large;
115 for (
const auto &pair: *map_small) {
116 auto it = map_large->find(pair.first);
117 if (it != map_large->end()) {
118 auto t0 = pair.second.second;
119 auto t1 = it->second.second;
131 for (
const auto &pair: other.
mapping_) {
132 auto it =
mapping_.find(pair.first);
138 TermPtr t0 = it->second.second;
139 TermPtr t1 = pair.second.second;
145 it->second.second = sigma.
apply();
160 if (i++ > 0) os <<
',';
161 os << pair.first <<
':' <<
' ' << (*pair.second.second);
168 const std::shared_ptr<T> &phi,
170 std::vector<FormulaPtr> formulae;
171 bool hasNewFormula =
false;
172 for (
const auto &f: phi->formulae()) {
175 hasNewFormula =
true;
179 if (!hasNewFormula) {
182 return std::make_shared<T>(formulae);
188 if (phi->isGround()) {
191 switch (phi->type()) {
193 auto modal = std::static_pointer_cast<ModalFormula>(phi);
195 if (inner == modal->modalFormula()) {
198 return std::make_shared<ModalFormula>(modal->modalOperator(), inner);
202 auto implication = std::static_pointer_cast<Implication>(phi);
203 auto antecedent =
applyBindings(implication->antecedent(), bindings);
204 auto consequent =
applyBindings(implication->consequent(), bindings);
205 if (antecedent == implication->antecedent() && consequent == implication->consequent()) {
208 return std::make_shared<Implication>(antecedent, consequent);
212 auto negation = std::static_pointer_cast<Negation>(phi);
213 auto negated =
applyBindings(negation->negatedFormula(), bindings);
214 if (negated == negation->negatedFormula()) {
217 return std::make_shared<Negation>(negated);
221 return applyCompoundBindings<Conjunction>(std::static_pointer_cast<Conjunction>(phi), bindings);
223 return applyCompoundBindings<Disjunction>(std::static_pointer_cast<Disjunction>(phi), bindings);
225 auto predicate = std::static_pointer_cast<Predicate>(phi);
227 auto newArgs = std::vector<TermPtr>(args.size());
228 auto hasNewArg =
false;
229 for (uint32_t i = 0; i < args.size(); i++) {
231 if (arg != args[i]) {
239 return std::make_shared<Predicate>(
predicate->functor(), newArgs);
250 switch (t->termType()) {
254 auto var = std::static_pointer_cast<Variable>(t);
263 auto function = std::static_pointer_cast<Function>(t);
264 auto args =
function->arguments();
265 auto newArgs = std::vector<TermPtr>(args.size());
266 auto hasNewArg =
false;
267 for (uint32_t i = 0; i < args.size(); i++) {
269 if (arg != args[i]) {
277 return std::make_shared<Function>(
function->functor(), newArgs);
298 class_<Bindings, std::shared_ptr<Bindings>>(
"Bindings", init<>())
299 .def(init<std::map<VariablePtr, TermPtr>>())
300 .def(
"__eq__", &Bindings::operator==)
306 .def(
"get", &
Bindings::get, return_value_policy<copy_const_reference>())
315 register_ptr_to_python<std::shared_ptr<const Bindings> >();
316 implicitly_convertible<std::shared_ptr<Bindings>, std::shared_ptr<const Bindings> >();
std::shared_ptr< T > applyCompoundBindings(const std::shared_ptr< T > &phi, const Bindings &bindings)
bool contains(std::string_view varName) const
std::shared_ptr< Atomic > getAtomic(std::string_view varName) const
const TermPtr & get(std::string_view varName) const
bool isConsistentWith(const Bindings &other) const
void set(const std::shared_ptr< Variable > &var, const TermPtr &term)
static std::shared_ptr< const Bindings > emptyBindings()
void write(std::ostream &os) const override
bool unifyWith(const Bindings &other)
void operator+=(const Bindings &other)
bool operator==(const Bindings &other) const
std::map< std::string_view, std::pair< std::shared_ptr< Variable >, TermPtr > > Map
FunctionRule & function()
FormulaPtr applyBindings_phi(const FormulaPtr &phi, const Bindings &bindings)
TermPtr applyBindings_t(const TermPtr &t, const Bindings &bindings)
void createType< Bindings >()
std::shared_ptr< Term > TermPtr
std::shared_ptr< Formula > FormulaPtr
void hashCombine(std::size_t &seed, const std::size_t &v)
FirstOrderLiteralPtr applyBindings(const FirstOrderLiteralPtr &lit, const Bindings &bindings)