Commit 18bd3c05 authored by Alejandro Suarez Hernandez's avatar Alejandro Suarez Hernandez
Browse files

substitution op

parent d1c3a3a5
...@@ -231,10 +231,10 @@ std::size_t Predicate::hash() const ...@@ -231,10 +231,10 @@ std::size_t Predicate::hash() const
Substitution::Substitution(std::initializer_list<Entry> il) : map_(il) {} Substitution::Substitution(std::initializer_list<Entry> il) : map_(il) {}
Term::Ptr Substitution::get(const std::string& key) const Term::Ptr Substitution::get(const std::string& key, Term::Ptr default_) const
{ {
auto it = map_.find(key); auto it = map_.find(key);
return it == map_.end()? nullptr : it->second; return it == map_.end()? default_ : it->second;
} }
void Substitution::put(const std::string& key, Term::Ptr term) void Substitution::put(const std::string& key, Term::Ptr term)
...@@ -247,6 +247,23 @@ void Substitution::remove(const std::string& key) ...@@ -247,6 +247,23 @@ void Substitution::remove(const std::string& key)
map_.erase(key); map_.erase(key);
} }
Predicate::Ptr Substitution::operator()(Predicate::Ptr input) const
{
std::vector<Term::Ptr> arguments;
for (auto term : input->arguments())
{
if (auto var = std::dynamic_pointer_cast<const Variable>(term))
{
arguments.push_back(get(var->name(), var));
}
else
{
arguments.push_back(term);
}
}
return Predicate::Ptr(new Predicate(input->name(), arguments));
}
// Implementation of stream operator // Implementation of stream operator
std::ostream& operator<<(std::ostream& os, const Stringifiable& strable) std::ostream& operator<<(std::ostream& os, const Stringifiable& strable)
......
...@@ -458,7 +458,7 @@ class Substitution : public Stringifiable ...@@ -458,7 +458,7 @@ class Substitution : public Stringifiable
Substitution(std::initializer_list<Entry> il); Substitution(std::initializer_list<Entry> il);
Term::Ptr get(const std::string& key) const; Term::Ptr get(const std::string& key, Term::Ptr default_=nullptr) const;
void put(const std::string& key, Term::Ptr term); void put(const std::string& key, Term::Ptr term);
...@@ -466,8 +466,6 @@ class Substitution : public Stringifiable ...@@ -466,8 +466,6 @@ class Substitution : public Stringifiable
Predicate::Ptr operator()(Predicate::Ptr input) const; Predicate::Ptr operator()(Predicate::Ptr input) const;
TermV::Ptr operator()(TermV::Ptr input) const;
int size() const { return map_.size(); } int size() const { return map_.size(); }
Map::const_iterator begin() const { return map_.cbegin(); } Map::const_iterator begin() const { return map_.cbegin(); }
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment