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

Some basic types for being used with the planning engine

parent d7f86588
bin/
doc/html
doc/images
......
......@@ -18,6 +18,8 @@ IF (NOT CMAKE_BUILD_TYPE)
SET(CMAKE_BUILD_TYPE "DEBUG")
ENDIF (NOT CMAKE_BUILD_TYPE)
SET(CMAKE_CXX_FLAGS "-std=c++11 ${CMAKE_CXX_FLAGS}")
SET(CMAKE_CXX_FLAGS_DEBUG "-g -Wall -D_REENTRANT")
SET(CMAKE_CXX_FLAGS_RELEASE "-O3 -D_REENTRANT")
......
# driver source files
SET(sources imagine-planner.cpp)
SET(sources imagine-planner.cpp types.cpp)
# application header files
SET(headers imagine-planner.h)
SET(headers imagine-planner.h types.h)
# locate the necessary dependencies
# add the necessary include directories
INCLUDE_DIRECTORIES(.)
......
ADD_EXECUTABLE(types_test types_test.cpp)
TARGET_LINK_LIBRARIES(types_test imagine-planner)
# create an example application
ADD_EXECUTABLE(imagine-planner_test imagine-planner_test.cpp)
# link necessary libraries
......
#include <iostream>
#include "types.h"
using namespace imagine_planner;
const char* b2s(bool b)
{
return b? "true" : "false";
}
int main(int argc, char* argv[])
{
std::hash<Term> h;
Term* atom = new Atom("my_atom");
Term* 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;
}
#include "imagine-planner.h"
CImagine-Planner::CImagine-Planner()
CImaginePlanner::CImaginePlanner()
{
}
CImagine-Planner::~CImagine-Planner()
CImaginePlanner::~CImaginePlanner()
{
}
#ifndef _IMAGINE-PLANNER_H
#define _IMAGINE-PLANNER_H
#ifndef _IMAGINE_PLANNER_H_
#define _IMAGINE_PLANNER_H_
class CImagine-Planner
class CImaginePlanner
{
public:
CImagine-Planner();
~CImagine-Planner();
CImaginePlanner();
~CImaginePlanner();
};
#endif
#include "types.h"
#include <cmath>
namespace imagine_planner
{
// Implementation of Atom's methods
Atom::Atom(const std::string& name) : name_(name) {}
bool Atom::operator==(const Term& other) const
{
if (const Atom* c_other = dynamic_cast<const Atom*>(&other))
{
return c_other->name_ == this->name_;
}
return false;
}
std::size_t Atom::hash() const
{
std::hash<std::string> h;
return h(name_);
}
// Implementation of Number's methods
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 false;
}
std::size_t Number::hash() const
{
std::hash<int> h;
return is_int()? h(int(number_)) : 0; // Return 0 for non-integer values to
// avoid problems in maps and sets
}
bool Number::is_int() const
{
return std::fabs(number_ - int(number_)) < 1E-9;
}
// Implementation of stream operator
std::ostream& operator<<(std::ostream& os, const Term& term)
{
os << term.to_str();
return os;
}
} /* end imagine_planner namespace */
namespace std
{
std::size_t hash<imagine_planner::Term>::operator()(const imagine_planner::Term& term) const
{
return term.hash();
}
}
#ifndef _LIBIMAGINE_PLANNER_TYPES_H_
#define _LIBIMAGINE_PLANNER_TYPES_H_
#include <functional>
#include <ostream>
#include <string>
#include <vector>
namespace imagine_planner
{
/**
* @brief Generic Term class.
*/
class Term
{
public:
virtual std::string to_str() const =0;
virtual bool is_ground() const =0;
virtual bool is_atomic() const =0;
virtual bool operator==(const Term& other) const =0;
virtual std::size_t hash() const =0;
virtual ~Term() {}
};
/**
* @brief
*/
class Atom : public Term
{
private:
std::string name_;
public:
Atom(const std::string& name);
virtual std::string to_str() const override { return name_; }
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 std::size_t hash() const override;
virtual std::string name() const { return name_; }
};
class Number : public Term
{
private:
double number_;
public:
Number(double number);
virtual std::string to_str() const override { return std::to_string(number_); }
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 std::size_t hash() const override;
bool is_int() const;
double number() const { return number_; }
};
class Variable : public Term
{
};
class TermV : public Term
{
private:
std::vector<Term> term_v_;
public:
TermV(const std::vector<Term>& term_v);
TermV(std::initializer_list<Term> il);
};
class Compound : public Term
{
};
/**
* @brief Stream operator that conveniently puts a Term printable version into
* an output stream.
*
* @param os Output stream
* @param term Term to be written into the stream
*
* @return os after the insertion
*/
std::ostream& operator<<(std::ostream& os, const Term& term);
} /* end imagine_planner namespace */
namespace std
{
/**
* @brief hash struct specialized to Term instances, so they can be used in
* unordered sets or maps.
*/
template<>
struct hash<imagine_planner::Term>
{
std::size_t operator()(const imagine_planner::Term& term) const;
};
}
#endif
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