Commit c6ca06dd authored by Alejandro Suarez Hernandez's avatar Alejandro Suarez Hernandez
Browse files

list and variable types

parent ef220ce9
......@@ -10,11 +10,29 @@ const char* b2s(bool b)
int main(int argc, char* argv[])
{
std::hash<Term> h;
Term* atom = new Atom("my_atom");
Term* atom_ = new Atom("another_atom");
Term::Ptr atom(new Atom("my_atom"));
Term::Ptr atom_(new Atom("another_atom"));
std::cout << "Testing stream operator for Atom: " << *atom << std::endl;
std::cout << "Testing hash function for Atom: " << h(*atom) << std::endl;
std::cout << "Testing == operator (equal): " << b2s(*atom == *atom) << std::endl;
std::cout << "Testing == operator (not equal): " << b2s(*atom == *atom_) << std::endl;
std::cout << "type(atom): " << atom->type() << std::endl;
std::cout << "Testing < operator (equal): " << b2s(*atom < *atom) << std::endl;
std::cout << "Testing < operator (lt): " << b2s(*atom_ < *atom) << std::endl;
std::cout << "Testing clone() method: " << *(atom->clone()) << std::endl;
Term::Ptr term_v(new TermV{Term::Ptr(new Atom("a1")), Term::Ptr(new Atom("a2"))});
Term::Ptr term_v_(new TermV{Term::Ptr(new Atom("a2")), Term::Ptr(new Atom("a3"))});
std::cout << "Testing stream operator for TermV: " << *term_v << std::endl;
std::cout << "Testing hash function for TermV: " << h(*term_v) << std::endl;
std::cout << "Testing == operator (equal): " << b2s(*term_v == *term_v) << std::endl;
std::cout << "Testing == operator (not equal): " << b2s(*term_v == *atom_) << std::endl;
std::cout << "Testing == operator (not equal): " << b2s(*term_v == *term_v_) << std::endl;
std::cout << "type(atom): " << term_v->type() << std::endl;
std::cout << "Testing < operator (equal): " << b2s(*term_v < *term_v) << std::endl;
std::cout << "Testing < operator (lt): " << b2s(*term_v < *term_v_) << std::endl;
std::cout << "Testing < operator (gt): " << b2s(*term_v < *atom_) << std::endl;
std::cout << "Testing < operator (lt): " << b2s(*atom_ < *term_v) << std::endl;
std::cout << "Testing clone() method: " << *(term_v->clone()) << std::endl;
}
#include "types.h"
#include <cmath>
#include <sstream>
namespace imagine_planner
{
......@@ -12,11 +13,20 @@ bool Atom::operator==(const Term& other) const
{
if (const Atom* c_other = dynamic_cast<const Atom*>(&other))
{
return c_other->name_ == this->name_;
return this->name_ == c_other->name_;
}
return false;
}
bool Atom::operator<(const Term& other) const
{
if (const Atom* c_other = dynamic_cast<const Atom*>(&other))
{
return this->name_ < c_other->name_;
}
return this->type() < other.type();
}
std::size_t Atom::hash() const
{
std::hash<std::string> h;
......@@ -25,17 +35,28 @@ std::size_t Atom::hash() const
// Implementation of Number's methods
double Number::EPSILON_ = 1E-9;
Number::Number(double number) : number_(number) {}
bool Number::operator==(const Term& other) const
{
if (const Number* c_other = dynamic_cast<const Number*>(&other))
{
return std::fabs(number_ - c_other->number_) < 1E-9;
return std::fabs(this->number_ - c_other->number_) < EPSILON_;
}
return false;
}
bool Number::operator<(const Term& other) const
{
if (const Number* c_other = dynamic_cast<const Number*>(&other))
{
return c_other->number_ - this->number_ >= EPSILON_;
}
return this->type() < other.type();
}
std::size_t Number::hash() const
{
std::hash<int> h;
......@@ -45,7 +66,129 @@ std::size_t Number::hash() const
bool Number::is_int() const
{
return std::fabs(number_ - int(number_)) < 1E-9;
return std::fabs(number_ - int(number_)) < EPSILON_;
}
// Implementation of Variable's methods
Variable::Variable(const std::string& name) : name_(name), value_(nullptr) {}
Variable::Variable(const std::string& name, const Term& value) : name_(name), value_(value.clone()) {}
void Variable::reset(const std::string& name, Term::ConstPtr value)
{
value_ = (name.empty() or name == name_)? value : value_;
}
bool Variable::operator==(const Term& other) const
{
if (const Variable* c_other = dynamic_cast<const Variable*>(&other))
{
return this->name_ == c_other->name_;
}
return false;
}
bool Variable::operator<(const Term& other) const
{
if (const Variable* c_other = dynamic_cast<const Variable*>(&other))
{
return c_other->name_ < c_other->name_;
}
return this->type() < other.type();
}
std::size_t Variable::hash() const
{
std::hash<std::string> h;
return h(name_);
}
// Implementation of TermV's methods
#define TERMV_CONSTRUCTOR(Container, PtrType)\
TermV::TermV(const Container<Term::PtrType>& term_v)\
{\
for (auto term : term_v) term_v_.push_back(term->clone());\
}
TERMV_CONSTRUCTOR(std::vector, Ptr)
TERMV_CONSTRUCTOR(std::vector, ConstPtr)
TERMV_CONSTRUCTOR(std::initializer_list, Ptr)
TERMV_CONSTRUCTOR(std::initializer_list, ConstPtr)
std::string TermV::to_str() const
{
std::ostringstream oss;
bool first = true;
for (auto term : term_v_)
{
if (not first) oss << ',';
oss << *term;
first = false;
}
return oss.str();
}
void TermV::reset(const std::string& name, Term::ConstPtr value)
{
for (auto term : term_v_)
{
term->reset(name, value);
}
}
bool TermV::is_ground() const
{
for (auto term : term_v_)
{
if (not term->is_ground()) return false;
}
return true;
}
bool TermV::operator==(const Term& other) const
{
if (const TermV* c_other = dynamic_cast<const TermV*>(&other))
{
if (this->size() != c_other->size()) return false;
for (int idx = 0; idx < this->size(); ++idx)
{
if (this->at(idx) != c_other->at(idx)) return false;
}
return true;
}
return false;
}
bool TermV::operator<(const Term& other) const
{
if (const TermV* c_other = dynamic_cast<const TermV*>(&other))
{
if (this->size() < c_other->size()) return true;
if (this->size() > c_other->size()) return false;
for (int idx = 0; idx < this->size(); ++idx)
{
if (*(this->at(idx)) < *(c_other->at(idx))) return true;
if (*(c_other->at(idx)) < *(this->at(idx))) return false;
}
return false;
}
return this->type() < other.type();
}
std::size_t TermV::hash() const
{
size_t ret = 0;
std::hash<Term> h;
for (auto term : term_v_)
{
ret += h(*term) + 0x9e3779b9 + (ret << 6) + (ret >> 2);
}
return ret;
}
// Implementation of stream operator
......
#ifndef _LIBIMAGINE_PLANNER_TYPES_H_
#define _LIBIMAGINE_PLANNER_TYPES_H_
#define TYPES_COMMON(Class)\
typedef std::shared_ptr<Class> Ptr;\
typedef std::shared_ptr<const Class> ConstPtr;\
virtual std::string type() const override { return #Class; }\
virtual Term::Ptr clone() const override { return Term::Ptr(new Class(*this)); }\
#include <functional>
#include <memory>
#include <ostream>
#include <string>
#include <vector>
......@@ -16,7 +23,12 @@ class Term
{
public:
virtual std::string to_str() const =0;
typedef std::shared_ptr<Term> Ptr;
typedef std::shared_ptr<const Term> ConstPtr;
virtual std::string to_str() const =0;
virtual void reset(const std::string& name="", Term::ConstPtr value=nullptr) =0;
virtual bool is_ground() const =0;
......@@ -24,8 +36,16 @@ class Term
virtual bool operator==(const Term& other) const =0;
virtual bool operator!=(const Term& other) const { return not(*this == other); }
virtual bool operator<(const Term& other) const =0;
virtual std::size_t hash() const =0;
virtual std::string type() const =0;
virtual Term::Ptr clone() const =0;
virtual ~Term() {}
};
......@@ -41,16 +61,22 @@ class Atom : public Term
public:
TYPES_COMMON(Atom);
Atom(const std::string& name);
virtual std::string to_str() const override { return name_; }
virtual void reset(const std::string& name="", Term::ConstPtr value=nullptr) override {}
virtual bool is_ground() const override { return true; }
virtual bool is_atomic() const override { return true; }
virtual bool operator==(const Term& other) const override;
virtual bool operator<(const Term& other) const override;
virtual std::size_t hash() const override;
virtual std::string name() const { return name_; }
......@@ -62,20 +88,28 @@ class Number : public Term
{
private:
static double EPSILON_;
double number_;
public:
TYPES_COMMON(Number);
Number(double number);
virtual std::string to_str() const override { return std::to_string(number_); }
virtual void reset(const std::string& name="", Term::ConstPtr value=nullptr) override {}
virtual bool is_ground() const override { return true; }
virtual bool is_atomic() const override { return true; }
virtual bool operator==(const Term& other) const override;
virtual bool operator<(const Term& other) const override;
virtual std::size_t hash() const override;
bool is_int() const;
......@@ -85,23 +119,86 @@ class Number : public Term
};
/**
* @brief
*/
class Variable : public Term
{
private:
};
std::string name_;
Term::ConstPtr value_;
public:
TYPES_COMMON(Variable);
Variable(const std::string& name);
Variable(const std::string& name, const Term& value);
virtual std::string to_str() const override { return value_? value_->to_str() : name_; }
virtual void reset(const std::string& name="", Term::ConstPtr value=nullptr) override;
virtual bool is_ground() const override { return bool(value_); }
virtual bool is_atomic() const override { return false; }
virtual bool operator==(const Term& other) const override;
virtual bool operator<(const Term& other) const override;
virtual std::size_t hash() const override;
const std::string& name() const { return name_; }
Term::ConstPtr value() const { return value_; }
};
class TermV : public Term
{
private:
std::vector<Term> term_v_;
std::vector<Term::Ptr> term_v_;
public:
TermV(const std::vector<Term>& term_v);
typedef std::vector<Term::Ptr>::const_iterator CIter;
TYPES_COMMON(TermV);
TermV(const std::vector<Term::Ptr>& term_v);
TermV(const std::vector<Term::ConstPtr>& term_v);
TermV(const std::initializer_list<Term::Ptr>& il);
TermV(const std::initializer_list<Term::ConstPtr>& il);
virtual std::string to_str() const override;
virtual void reset(const std::string& name="", Term::ConstPtr value=nullptr) override;
virtual bool is_ground() const override;
virtual bool is_atomic() const override { return false; }
virtual bool operator==(const Term& other) const override;
virtual bool operator<(const Term& other) const override;
virtual std::size_t hash() const override;
int size() const { return term_v_.size(); }
const Term::ConstPtr at(int idx) const { return term_v_.at(idx); }
//CIter cbegin() const { return term_v_.cbegin(); }
TermV(std::initializer_list<Term> il);
//CIter cend() const { return term_v_.cend(); }
};
......
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