Commit 40fec844 authored by Alejandro Suarez Hernandez's avatar Alejandro Suarez Hernandez
Browse files

removed pl support. Re-added substitutions

parent c7ff882c
......@@ -6,15 +6,13 @@ SET(headers imagine-planner.h types.h)
FIND_PACKAGE(Boost COMPONENTS system filesystem unit_test_framework REQUIRED)
# Swi-pl
include(FindPkgConfig)
pkg_check_modules(SWIPL REQUIRED swipl)
# locate the necessary dependencies
# add the necessary include directories
INCLUDE_DIRECTORIES(. ${Boost_INCLUDE_DIR} ${SWIPL_INCLUDE_DIRS})
LINK_DIRECTORIES(${SWIPL_LIBRARY_DIRS})
INCLUDE_DIRECTORIES(. ${Boost_INCLUDE_DIR})
# create the shared library
ADD_LIBRARY(imagine-planner SHARED ${sources})
# link necessary libraries
TARGET_LINK_LIBRARIES(imagine-planner ${SWIPL_LIBRARIES})
TARGET_LINK_LIBRARIES(imagine-planner)
INSTALL(TARGETS imagine-planner
RUNTIME DESTINATION bin
......
......@@ -10,10 +10,4 @@ TARGET_LINK_LIBRARIES(types_test
${Boost_SYSTEM_LIBRARY}
${Boost_UNIT_TEST_FRAMEWORK_LIBRARY})
ADD_EXECUTABLE(query_test query_test.cpp)
TARGET_LINK_LIBRARIES(query_test
imagine-planner
${Boost_FILESYSTEM_LIBRARY}
${Boost_SYSTEM_LIBRARY}
${Boost_UNIT_TEST_FRAMEWORK_LIBRARY})
#include "types.h"
#include <iostream>
using namespace imagine_planner;
#if 0 // Set to 1 if __PRETTY_FUNCTION__ macro is not available
#define __PRETTY_FUNCTION__ __func__
#endif
#if 1
#define INFO(x)\
std::cout << "\e[1m\e[32m\e[4m" << __PRETTY_FUNCTION__ << " [" << __LINE__ << "]" << "\e[24m: " << x << "\e[0m" << std::endl;
#else
#define INFO(x)
#endif
void test()
{
PlanState state{Predicate("at", "lettuce", "east"),
Predicate("at", "wolf", "east"),
Predicate("at", "bunny", "east"),
Predicate("at", "boat", "east"),
Predicate("side", "east"),
Predicate("side", "west"),
Predicate("forbidden", "wolf", "bunny"),
Predicate("forbidden", "lettuce", "bunny")};
PlCall("assertz((load_pre(Side,What):-at(boat,Side),at(What,Side),What\\==boat,side(Side),\\+loaded(_)))");
StateQuery query(state, create_term_v("Side", "What"), "load_pre");
//while (query.next_solution())
//{
//INFO("Side=" << query.get_values()[0] << ", What=" << query.get_values()[1]);
//}
}
int main(int argc, char* argv[])
{
PlEngine pl(argc, argv);
//PlTermv av(1);
//PlQuery q("current_module", av);
//while (q.next_solution())
//{
//std::cout << (char*)av[0] << std::endl;
//}
test();
}
......@@ -205,8 +205,33 @@ BOOST_AUTO_TEST_CASE(unordered_set_test)
set_test_aux<Set>();
}
BOOST_AUTO_TEST_CASE(state_query_test)
BOOST_AUTO_TEST_CASE(substitution_test)
{
Predicate p1("p", "X", "a");
Predicate p2("p", "x", "y");
Predicate p3("p", "X", "Y");
Predicate p4("q", "Y");
Predicate p5("q", "Z");
Predicate p6("r");
Substitution sigma({{"X", TermWrapper("a")}, {"Y", TermWrapper("b")}});
INFO("p1 = " << p1);
INFO("p2 = " << p2);
INFO("p3 = " << p3);
INFO("p4 = " << p4);
INFO("p5 = " << p5);
INFO("p6 = " << p6);
INFO("sigma" << sigma);
Predicate p1_ = sigma(p1);
Predicate p2_ = sigma(p2);
Predicate p3_ = sigma(p3);
Predicate p4_ = sigma(p4);
Predicate p5_ = sigma(p5);
Predicate p6_ = sigma(p6);
BOOST_CHECK_EQUAL(p1_.to_str(), "p(a,a)");
BOOST_CHECK_EQUAL(p2_.to_str(), "p(x,y)");
BOOST_CHECK_EQUAL(p3_.to_str(), "p(a,b)");
BOOST_CHECK_EQUAL(p4_.to_str(), "q(b)");
BOOST_CHECK_EQUAL(p5_.to_str(), "q(Z)");
BOOST_CHECK_EQUAL(p6_.to_str(), "r()");
}
#include "types.h"
#include <iostream>
#include <cmath>
#include <sstream>
......@@ -81,6 +80,8 @@ bool NumberTerm::operator<(const Term& term) const
TermWrapper::TermWrapper(const TermWrapper& term) : term_(term.term_->clone()) {}
TermWrapper::TermWrapper(const Term& term) : term_(term.clone()) {}
TermWrapper::TermWrapper(const std::string& literal) :
term_(new LiteralTerm(literal)) {}
......@@ -181,72 +182,89 @@ bool Predicate::operator<(const Predicate& predicate) const
return false;
}
// Implementation of PlanState
// Implementation of Substitution
PlanState::PlanState() {}
PlanState::PlanState(std::initializer_list<Predicate> il) : predicates_(il) {}
Substitution::Substitution() {}
// Implementation of StateQuery
Substitution::Substitution(const std::map<std::string, TermWrapper>& sigma)
: sigma_(sigma) {}
void add_predicate_to_pl(const Predicate& predicate)
std::string Substitution::to_str() const
{
std::size_t arity = predicate.arity();
const TermV& arguments = predicate.get_arguments();
PlTermv pl_termv(arity);
for (std::size_t idx = 0; idx < arity; ++idx)
std::ostringstream oss;
if (sigma_.empty()) oss << "{}";
else
{
if (auto casted = arguments[idx].get_literal_term())
oss << '{';
bool first = true;
for (auto entry : sigma_)
{
pl_termv[idx] = casted->get_literal().c_str();
}
else if (auto casted = arguments[idx].get_number_term())
{
pl_termv[idx] = (long)casted->get_number();
if (not first) oss << ',';
oss << "\n ";
oss << entry.first << " -> " << entry.second;
first = false;
}
oss << "\n}";
}
PlCompound pl_pred(predicate.get_name().c_str(), pl_termv);
std::cout << "Adding " << predicate << std::endl;
PlCall("assertz", pl_pred);
std::cout << "Added " << predicate << std::endl;
return oss.str();
}
StateQuery::StateQuery(const PlanState& state, const TermV& parameters,
const std::string& query) :
state_(state), parameters_(parameters), values_(parameters),
pl_values_(parameters.size()), query_(new PlQuery(query.c_str(), pl_values_))
bool Substitution::has(const std::string& varname) const
{
for (const Predicate& predicate : state_)
{
add_predicate_to_pl(predicate);
}
return (bool)sigma_.count(varname);
}
bool StateQuery::next_solution()
const Term* Substitution::get(const std::string& varname) const
{
if (query_->next_solution())
{
for (std::size_t idx = 0; idx < parameters_.size(); ++idx)
{
if (pl_values_[idx].type() == PL_ATOM)
{
values_[idx] = TermWrapper((char*)pl_values_[idx]);
}
else if (pl_values_[idx].type() == PL_INTEGER)
{
values_[idx] = TermWrapper((long)pl_values_[idx]);
}
}
return true;
}
return false;
auto it = sigma_.find(varname);
if (it != sigma_.end()) return it->second.get_term();
return nullptr;
}
StateQuery::~StateQuery()
void Substitution::put(const std::string& varname, const TermWrapper& term)
{
/* TO-DO: retract all facts */
sigma_.insert(std::make_pair(varname, term));
}
void Substitution::remove(const std::string& varname)
{
sigma_.erase(varname);
}
Predicate Substitution::operator()(const Predicate& predicate) const
{
TermV arguments;
arguments.reserve(predicate.arity());
for (const TermWrapper& term : predicate.get_arguments())
{
const Term* value = get(term.to_str());
if (not value) value = term.get_term();
arguments.push_back(TermWrapper(*value));
}
return Predicate(predicate.get_name(), arguments);
}
// Implementation of PlanState
PlanState::PlanState() {}
PlanState::PlanState(std::initializer_list<Predicate> il) : predicates_(il) {}
// Implementation of StateQuery
//StateQuery::StateQuery(const PlanState& state, const TermV& parameters,
//const std::string& query) :
//state_(state), parameters_(parameters), values_(parameters),
//{
//}
//bool StateQuery::next_solution()
//{
//return false;
//}
// Implementation of stream operator
std::ostream& operator<<(std::ostream& os, const Stringifiable& strable)
......
......@@ -12,8 +12,8 @@
#include <ostream>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <SWI-cpp.h>
namespace imagine_planner
{
......@@ -136,10 +136,14 @@ class TermWrapper : public Stringifiable,
TermWrapper(const TermWrapper& term);
TermWrapper(const Term& term);
TermWrapper(const std::string& literal);
TermWrapper(double number);
const Term* get_term() const { return term_; }
const LiteralTerm* get_literal_term() const;
const NumberTerm* get_number_term() const;
......@@ -198,8 +202,6 @@ TermV create_term_v(const First& fst, Args... args)
return term_v;
}
bool is_ground(const TermV& term_v);
class Predicate : public Stringifiable,
public Hashable
{
......@@ -234,51 +236,66 @@ class Predicate : public Stringifiable,
};
class PlanState
class Substitution : public Stringifiable
{
private:
typedef std::set<Predicate> Container;
typedef std::map<std::string,TermWrapper> Container;
Container predicates_;
Container sigma_;
public:
typedef Container::const_iterator CIter;
PlanState();
Substitution();
PlanState(std::initializer_list<Predicate> il);
Substitution(const std::map<std::string, TermWrapper>& sigma);
template <typename InputIterator>
PlanState(InputIterator begin, InputIterator end) :
predicates_(begin, end) {}
std::string to_str() const override;
CIter begin() const { return predicates_.begin(); }
bool has(const std::string& varname) const;
CIter end() const { return predicates_.end(); }
const Term* get(const std::string& varname) const;
void put(const std::string& varname, const TermWrapper& value);
void remove(const std::string& varname);
Predicate operator()(const Predicate& predicate) const;
int size() const { return sigma_.size(); }
CIter begin() const { return sigma_.begin(); }
CIter end() const { return sigma_.end(); }
};
class StateQuery
class PlanState
{
private:
typedef std::set<Predicate> Container;
const PlanState& state_;
TermV parameters_, values_;
PlTermv pl_values_;
PlQuery* query_;
Container predicates_;
public:
typedef Container::const_iterator CIter;
StateQuery(const PlanState& state, const TermV& parameters,
const std::string& query);
PlanState();
bool next_solution();
PlanState(std::initializer_list<Predicate> il);
const TermV& get_values() const { return values_; }
template <typename InputIterator>
PlanState(InputIterator begin, InputIterator end) :
predicates_(begin, end) {}
CIter begin() const { return predicates_.begin(); }
CIter end() const { return predicates_.end(); }
~StateQuery();
};
//class Operator
//{
//private:
......
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