Skip to content
Snippets Groups Projects
Commit f441b825 authored by Alejandro Suarez Hernandez's avatar Alejandro Suarez Hernandez
Browse files

substitution operators

parent 40fec844
No related branches found
No related tags found
No related merge requests found
...@@ -214,6 +214,7 @@ BOOST_AUTO_TEST_CASE(substitution_test) ...@@ -214,6 +214,7 @@ BOOST_AUTO_TEST_CASE(substitution_test)
Predicate p5("q", "Z"); Predicate p5("q", "Z");
Predicate p6("r"); Predicate p6("r");
Substitution sigma({{"X", TermWrapper("a")}, {"Y", TermWrapper("b")}}); Substitution sigma({{"X", TermWrapper("a")}, {"Y", TermWrapper("b")}});
Substitution sigma_({{"W", TermWrapper("c")}});
INFO("p1 = " << p1); INFO("p1 = " << p1);
INFO("p2 = " << p2); INFO("p2 = " << p2);
INFO("p3 = " << p3); INFO("p3 = " << p3);
...@@ -233,5 +234,13 @@ BOOST_AUTO_TEST_CASE(substitution_test) ...@@ -233,5 +234,13 @@ BOOST_AUTO_TEST_CASE(substitution_test)
BOOST_CHECK_EQUAL(p4_.to_str(), "q(b)"); BOOST_CHECK_EQUAL(p4_.to_str(), "q(b)");
BOOST_CHECK_EQUAL(p5_.to_str(), "q(Z)"); BOOST_CHECK_EQUAL(p5_.to_str(), "q(Z)");
BOOST_CHECK_EQUAL(p6_.to_str(), "r()"); BOOST_CHECK_EQUAL(p6_.to_str(), "r()");
Substitution sigma_x = sigma + sigma_;
BOOST_CHECK_EQUAL(sigma_x.to_str(), "{\n W -> c,\n X -> a,\n Y -> b\n}");
}
BOOST_AUTO_TEST_CASE(planstate_test)
{
PlanState state({Predicate("p", "x", "y"), Predicate("q", "y"), Predicate("r")});
INFO(state);
} }
...@@ -231,6 +231,36 @@ void Substitution::remove(const std::string& varname) ...@@ -231,6 +231,36 @@ void Substitution::remove(const std::string& varname)
sigma_.erase(varname); sigma_.erase(varname);
} }
void Substitution::operator+=(const Substitution& other)
{
for (auto entry : other)
{
sigma_.insert(entry);
}
}
void Substitution::operator-=(const Substitution& other)
{
for (auto entry : other)
{
sigma_.erase(entry.first);
}
}
Substitution Substitution::operator+(const Substitution& other) const
{
Substitution ret(*this);
ret += other;
return ret;
}
Substitution Substitution::operator-(const Substitution& other) const
{
Substitution ret;
ret -= other;
return ret;
}
Predicate Substitution::operator()(const Predicate& predicate) const Predicate Substitution::operator()(const Predicate& predicate) const
{ {
TermV arguments; TermV arguments;
...@@ -248,22 +278,44 @@ Predicate Substitution::operator()(const Predicate& predicate) const ...@@ -248,22 +278,44 @@ Predicate Substitution::operator()(const Predicate& predicate) const
PlanState::PlanState() {} PlanState::PlanState() {}
PlanState::PlanState(std::initializer_list<Predicate> il) : predicates_(il) {} PlanState::PlanState(const std::set<Predicate>& predicates) :
predicates_(predicates) {}
// Implementation of StateQuery
std::string PlanState::to_str() const
{
std::ostringstream oss;
bool first = true;
oss << '{';
for (const Predicate& pred : predicates_)
{
if (not first) oss << ',';
oss << pred;
first = false;
}
oss << '}';
return oss.str();
}
//StateQuery::StateQuery(const PlanState& state, const TermV& parameters, std::size_t PlanState::hash() const
//const std::string& query) : {
//state_(state), parameters_(parameters), values_(parameters), std::size_t acc = 0;
//{ for (const Predicate& pred : predicates_)
{
acc += pred.hash();
}
return acc;
}
//}
//bool StateQuery::next_solution() bool PlanState::subset_of(const PlanState& other) const
//{ {
//return false; if (predicates_.size() > other.predicates_.size()) return false;
//} for (const Predicate& pred : predicates_)
{
if (not other.has(pred)) return false;
}
return true;
}
// Implementation of stream operator // Implementation of stream operator
......
...@@ -261,6 +261,14 @@ class Substitution : public Stringifiable ...@@ -261,6 +261,14 @@ class Substitution : public Stringifiable
void remove(const std::string& varname); void remove(const std::string& varname);
void operator+=(const Substitution& other);
void operator-=(const Substitution& other);
Substitution operator+(const Substitution& other) const;
Substitution operator-(const Substitution& other) const;
Predicate operator()(const Predicate& predicate) const; Predicate operator()(const Predicate& predicate) const;
int size() const { return sigma_.size(); } int size() const { return sigma_.size(); }
...@@ -271,49 +279,54 @@ class Substitution : public Stringifiable ...@@ -271,49 +279,54 @@ class Substitution : public Stringifiable
}; };
class PlanState class PlanState : public Stringifiable,
public Hashable
{ {
private: private:
typedef std::set<Predicate> Container; std::set<Predicate> predicates_;
Container predicates_;
public: public:
typedef Container::const_iterator CIter; typedef std::set<Predicate>::const_iterator CIter;
PlanState(); PlanState();
PlanState(std::initializer_list<Predicate> il); PlanState(const std::set<Predicate>& predicates);
template <typename InputIterator>
PlanState(InputIterator begin, InputIterator end) :
predicates_(begin, end) {}
CIter begin() const { return predicates_.begin(); }
CIter end() const { return predicates_.end(); } virtual std::string to_str() const override;
}; virtual std::size_t hash() const override;
bool operator==(const PlanState& other) const
{
return predicates_ == other.predicates_;
}
//class Operator bool subset_of(const PlanState& other) const;
//{
//private:
//TermV parameters_, values_; bool has(const Predicate& predicate) const
//std::string name_, pre_, post_; {
return (bool)predicates_.count(predicate);
}
//public: void put(const Predicate& predicate)
{
predicates_.insert(predicate);
}
//Operator(TermV parameters_, const std::string& pre, void remove(const Predicate& predicate)
//const std::string& post); {
predicates_.erase(predicate);
}
//bool is_ground() const; template <typename InputIterator>
PlanState(InputIterator begin, InputIterator end) :
predicates_(begin, end) {}
//int arity() const { return parameters_.size(); } CIter begin() const { return predicates_.begin(); }
//}; CIter end() const { return predicates_.end(); }
};
/** /**
* @brief Stream operator that conveniently puts a Stringifiable object into * @brief Stream operator that conveniently puts a Stringifiable object into
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment