2016-05-30 15:44:13 +02:00
|
|
|
#include <gtest/gtest.h>
|
|
|
|
|
|
|
|
#include <fstream>
|
|
|
|
#include <iostream>
|
|
|
|
#include <sstream>
|
|
|
|
#include <stdexcept>
|
|
|
|
|
|
|
|
#include <plasp/pddl/Description.h>
|
2016-06-07 14:00:29 +02:00
|
|
|
#include <plasp/pddl/expressions/And.h>
|
2016-06-03 17:12:39 +02:00
|
|
|
#include <plasp/pddl/expressions/Either.h>
|
2016-06-07 14:00:29 +02:00
|
|
|
#include <plasp/pddl/expressions/Not.h>
|
|
|
|
#include <plasp/pddl/expressions/Predicate.h>
|
2016-06-04 14:46:05 +02:00
|
|
|
#include <plasp/pddl/expressions/PrimitiveType.h>
|
|
|
|
#include <plasp/pddl/expressions/Reference.h>
|
2016-05-30 15:44:13 +02:00
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
class PDDLParserTests : public ::testing::Test
|
|
|
|
{
|
|
|
|
protected:
|
|
|
|
PDDLParserTests()
|
2016-05-30 20:43:36 +02:00
|
|
|
: m_blocksworldDomainFile(readFile("data/blocksworld-domain.pddl")),
|
2016-06-01 16:47:34 +02:00
|
|
|
m_storageDomainFile(readFile("data/storage-domain.pddl")),
|
2016-06-02 23:41:40 +02:00
|
|
|
m_whiteSpaceTestFile(readFile("data/white-space-test.pddl")),
|
2016-06-01 16:47:34 +02:00
|
|
|
m_woodworkingDomainFile(readFile("data/woodworking-domain.pddl"))
|
2016-05-30 15:44:13 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
static std::stringstream readFile(const std::string &path)
|
|
|
|
{
|
|
|
|
std::ifstream fileStream(path, std::ios::in);
|
|
|
|
|
|
|
|
std::stringstream outputStream;
|
|
|
|
|
|
|
|
if (!fileStream.is_open())
|
|
|
|
throw std::runtime_error("Could not open file \"" + path + "\"");
|
|
|
|
|
|
|
|
outputStream << fileStream.rdbuf();
|
|
|
|
|
|
|
|
return outputStream;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::stringstream m_blocksworldDomainFile;
|
2016-05-30 20:43:36 +02:00
|
|
|
std::stringstream m_storageDomainFile;
|
2016-06-02 23:41:40 +02:00
|
|
|
std::stringstream m_whiteSpaceTestFile;
|
2016-06-01 16:47:34 +02:00
|
|
|
std::stringstream m_woodworkingDomainFile;
|
2016-05-30 15:44:13 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2016-05-30 20:43:36 +02:00
|
|
|
TEST_F(PDDLParserTests, ParseBlocksWorldDomain)
|
2016-05-30 15:44:13 +02:00
|
|
|
{
|
2016-06-04 14:46:05 +02:00
|
|
|
using namespace plasp::pddl;
|
|
|
|
|
2016-05-30 15:44:13 +02:00
|
|
|
try
|
|
|
|
{
|
2016-06-04 14:46:05 +02:00
|
|
|
const auto description = Description::fromStream(m_blocksworldDomainFile);
|
2016-05-30 15:44:13 +02:00
|
|
|
|
|
|
|
ASSERT_NO_THROW(description.domain());
|
|
|
|
|
|
|
|
const auto &domain = description.domain();
|
|
|
|
|
2016-06-01 14:11:13 +02:00
|
|
|
// Name
|
2016-06-04 18:28:43 +02:00
|
|
|
ASSERT_EQ(domain.name(), "blocks");
|
2016-05-30 20:43:36 +02:00
|
|
|
|
2016-06-01 14:11:13 +02:00
|
|
|
// Requirements
|
2016-05-30 15:44:13 +02:00
|
|
|
ASSERT_EQ(domain.requirements().size(), 2u);
|
2016-06-04 14:46:05 +02:00
|
|
|
ASSERT_EQ(domain.requirements()[0].type(), Requirement::Type::STRIPS);
|
|
|
|
ASSERT_EQ(domain.requirements()[1].type(), Requirement::Type::Typing);
|
2016-05-30 20:43:36 +02:00
|
|
|
|
2016-06-01 14:11:13 +02:00
|
|
|
// Types
|
2016-05-30 20:43:36 +02:00
|
|
|
ASSERT_EQ(domain.types().size(), 1u);
|
|
|
|
|
2016-06-01 14:11:13 +02:00
|
|
|
const auto &block = *domain.types()[0];
|
2016-05-30 20:43:36 +02:00
|
|
|
|
2016-06-01 01:29:46 +02:00
|
|
|
ASSERT_EQ(block.name(), "block");
|
|
|
|
ASSERT_EQ(block.parentTypes().size(), 0u);
|
2016-05-31 16:43:25 +02:00
|
|
|
|
2016-06-01 14:11:13 +02:00
|
|
|
// Predicates
|
2016-05-31 16:43:25 +02:00
|
|
|
ASSERT_EQ(domain.predicates().size(), 5u);
|
|
|
|
|
2016-06-01 14:11:13 +02:00
|
|
|
const auto &on = *domain.predicates()[0];
|
|
|
|
|
|
|
|
ASSERT_EQ(on.name(), "on");
|
|
|
|
ASSERT_EQ(on.arguments().size(), 2u);
|
2016-06-03 13:37:23 +02:00
|
|
|
ASSERT_EQ(on.arguments()[0]->name(), "x");
|
2016-06-04 14:46:05 +02:00
|
|
|
const auto *onArgument0Type = dynamic_cast<const expressions::PrimitiveType *>(on.arguments()[0]->type());
|
2016-06-01 14:11:13 +02:00
|
|
|
ASSERT_EQ(onArgument0Type, &block);
|
2016-06-03 13:37:23 +02:00
|
|
|
ASSERT_EQ(on.arguments()[1]->name(), "y");
|
2016-06-04 14:46:05 +02:00
|
|
|
const auto onArgument1Type = dynamic_cast<const expressions::PrimitiveType *>(on.arguments()[1]->type());
|
2016-06-01 14:11:13 +02:00
|
|
|
ASSERT_EQ(onArgument1Type, &block);
|
|
|
|
|
|
|
|
const auto &handempty = *domain.predicates()[3];
|
|
|
|
|
|
|
|
ASSERT_EQ(handempty.name(), "handempty");
|
|
|
|
ASSERT_TRUE(handempty.arguments().empty());
|
2016-06-07 14:00:29 +02:00
|
|
|
|
|
|
|
// Actions
|
|
|
|
ASSERT_EQ(domain.actions().size(), 4u);
|
|
|
|
|
|
|
|
const auto &pickUp = *domain.actions()[0];
|
|
|
|
|
|
|
|
ASSERT_EQ(pickUp.name(), "pick-up");
|
|
|
|
ASSERT_EQ(pickUp.parameters().size(), 1u);
|
|
|
|
ASSERT_EQ(pickUp.parameters()[0]->name(), "x");
|
|
|
|
ASSERT_EQ(pickUp.parameters()[0]->type(), &block);
|
|
|
|
|
|
|
|
const auto &pickUpPre = dynamic_cast<const expressions::And &>(pickUp.precondition());
|
|
|
|
ASSERT_EQ(pickUpPre.arguments().size(), 3u);
|
|
|
|
const auto &pickUpPre0 = dynamic_cast<const expressions::Predicate &>(*pickUpPre.arguments()[0]);
|
|
|
|
ASSERT_EQ(pickUpPre0.name(), "clear");
|
|
|
|
ASSERT_EQ(pickUpPre0.arguments().size(), 1u);
|
|
|
|
const auto &pickUpPre00 = *dynamic_cast<const expressions::Reference<expressions::Variable> &>(*pickUpPre0.arguments()[0]).value();
|
|
|
|
ASSERT_EQ(pickUpPre00.name(), "x");
|
|
|
|
ASSERT_EQ(pickUpPre00.type(), &block);
|
|
|
|
ASSERT_EQ(&pickUpPre00, pickUp.parameters()[0].get());
|
|
|
|
const auto &pickUpPre2 = dynamic_cast<const expressions::Predicate &>(*pickUpPre.arguments()[2]);
|
|
|
|
ASSERT_EQ(pickUpPre2.name(), "handempty");
|
|
|
|
ASSERT_EQ(pickUpPre2.arguments().size(), 0u);
|
|
|
|
|
|
|
|
const auto &pickUpEff = dynamic_cast<const expressions::And &>(pickUp.effect());
|
|
|
|
ASSERT_EQ(pickUpEff.arguments().size(), 4u);
|
|
|
|
const auto &pickUpEff0 = dynamic_cast<const expressions::Not &>(*pickUpEff.arguments()[0]);
|
|
|
|
const auto &pickUpEff00 = dynamic_cast<const expressions::Predicate &>(pickUpEff0.argument());
|
|
|
|
ASSERT_EQ(pickUpEff00.name(), "ontable");
|
|
|
|
ASSERT_EQ(pickUpEff00.arguments().size(), 1u);
|
|
|
|
const auto &pickUpEff000 = *dynamic_cast<const expressions::Reference<expressions::Variable> &>(*pickUpEff00.arguments()[0]).value();
|
|
|
|
ASSERT_EQ(pickUpEff000.name(), "x");
|
|
|
|
ASSERT_EQ(pickUpEff000.type(), &block);
|
2016-05-30 20:43:36 +02:00
|
|
|
}
|
|
|
|
catch (const std::exception &e)
|
|
|
|
{
|
|
|
|
FAIL() << e.what();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
TEST_F(PDDLParserTests, ParseStorageDomain)
|
|
|
|
{
|
2016-06-04 14:46:05 +02:00
|
|
|
using namespace plasp::pddl;
|
|
|
|
|
2016-05-30 20:43:36 +02:00
|
|
|
try
|
|
|
|
{
|
|
|
|
const auto description = plasp::pddl::Description::fromStream(m_storageDomainFile);
|
|
|
|
|
|
|
|
ASSERT_NO_THROW(description.domain());
|
|
|
|
|
|
|
|
const auto &domain = description.domain();
|
|
|
|
|
2016-06-01 14:11:13 +02:00
|
|
|
// Name
|
2016-06-04 18:28:43 +02:00
|
|
|
ASSERT_EQ(domain.name(), "storage-propositional");
|
2016-05-30 20:43:36 +02:00
|
|
|
|
2016-06-01 14:11:13 +02:00
|
|
|
// Requirements
|
2016-05-30 20:43:36 +02:00
|
|
|
ASSERT_EQ(domain.requirements().size(), 1u);
|
2016-06-04 14:46:05 +02:00
|
|
|
ASSERT_EQ(domain.requirements()[0].type(), Requirement::Type::Typing);
|
2016-05-30 20:43:36 +02:00
|
|
|
|
2016-06-01 14:11:13 +02:00
|
|
|
// Types
|
|
|
|
ASSERT_EQ(domain.types().size(), 10u);
|
|
|
|
|
|
|
|
const auto &hoist = *domain.types()[0];
|
|
|
|
const auto &surface = *domain.types()[1];
|
|
|
|
const auto &area = *domain.types()[3];
|
|
|
|
const auto &object = *domain.types()[4];
|
|
|
|
const auto &storearea = *domain.types()[7];
|
|
|
|
const auto &crate = *domain.types()[9];
|
2016-06-01 01:29:46 +02:00
|
|
|
|
|
|
|
const auto &hoistParents = hoist.parentTypes();
|
2016-05-30 20:43:36 +02:00
|
|
|
ASSERT_EQ(hoistParents.size(), 1u);
|
2016-06-01 01:29:46 +02:00
|
|
|
ASSERT_TRUE(std::find(hoistParents.cbegin(), hoistParents.cend(), &object) != hoistParents.cend());
|
2016-05-30 20:43:36 +02:00
|
|
|
|
2016-06-01 01:29:46 +02:00
|
|
|
const auto &areaParents = area.parentTypes();
|
2016-05-30 20:43:36 +02:00
|
|
|
ASSERT_EQ(areaParents.size(), 2u);
|
2016-06-01 01:29:46 +02:00
|
|
|
ASSERT_TRUE(std::find(areaParents.cbegin(), areaParents.cend(), &object) != areaParents.cend());
|
|
|
|
ASSERT_TRUE(std::find(areaParents.cbegin(), areaParents.cend(), &surface) != areaParents.cend());
|
2016-06-01 14:11:13 +02:00
|
|
|
|
|
|
|
// Predicates
|
|
|
|
ASSERT_EQ(domain.predicates().size(), 8u);
|
|
|
|
|
|
|
|
const auto &on = *domain.predicates()[5];
|
|
|
|
|
|
|
|
ASSERT_EQ(on.name(), "on");
|
|
|
|
ASSERT_EQ(on.arguments().size(), 2u);
|
2016-06-03 13:37:23 +02:00
|
|
|
ASSERT_EQ(on.arguments()[0]->name(), "c");
|
2016-06-04 14:46:05 +02:00
|
|
|
const auto onArgument0Type = dynamic_cast<const expressions::PrimitiveType *>(on.arguments()[0]->type());
|
2016-06-01 14:11:13 +02:00
|
|
|
ASSERT_EQ(onArgument0Type, &crate);
|
2016-06-03 13:37:23 +02:00
|
|
|
ASSERT_EQ(on.arguments()[1]->name(), "s");
|
2016-06-04 14:46:05 +02:00
|
|
|
const auto onArgument1Type = dynamic_cast<const expressions::PrimitiveType *>(on.arguments()[1]->type());
|
2016-06-01 14:11:13 +02:00
|
|
|
ASSERT_EQ(onArgument1Type, &storearea);
|
|
|
|
|
|
|
|
const auto &in = *domain.predicates()[1];
|
|
|
|
ASSERT_EQ(in.name(), "in");
|
|
|
|
ASSERT_EQ(in.arguments().size(), 2u);
|
2016-06-03 13:37:23 +02:00
|
|
|
ASSERT_EQ(in.arguments()[0]->name(), "x");
|
2016-06-04 14:46:05 +02:00
|
|
|
const auto inArgument0Type = dynamic_cast<const expressions::Either *>(in.arguments()[0]->type());
|
2016-06-03 17:12:39 +02:00
|
|
|
ASSERT_EQ(inArgument0Type->arguments().size(), 2u);
|
2016-06-07 13:39:54 +02:00
|
|
|
const auto inArgument0Type0 = dynamic_cast<const expressions::Reference<expressions::PrimitiveType> *>(inArgument0Type->arguments()[0].get())->value();
|
|
|
|
ASSERT_EQ(inArgument0Type0, &storearea);
|
|
|
|
const auto inArgument0Type1 = dynamic_cast<const expressions::Reference<expressions::PrimitiveType> *>(inArgument0Type->arguments()[1].get())->value();
|
|
|
|
ASSERT_EQ(inArgument0Type1, &crate);
|
2016-06-07 14:07:17 +02:00
|
|
|
|
|
|
|
// Actions
|
|
|
|
ASSERT_EQ(domain.actions().size(), 5u);
|
|
|
|
|
|
|
|
const auto &drop = *domain.actions()[1];
|
|
|
|
|
|
|
|
ASSERT_EQ(drop.name(), "drop");
|
|
|
|
ASSERT_EQ(drop.parameters().size(), 5u);
|
|
|
|
ASSERT_EQ(drop.parameters()[3]->name(), "a2");
|
|
|
|
ASSERT_EQ(drop.parameters()[3]->type(), &area);
|
|
|
|
|
|
|
|
const auto &dropPre = dynamic_cast<const expressions::And &>(drop.precondition());
|
|
|
|
ASSERT_EQ(dropPre.arguments().size(), 5u);
|
|
|
|
const auto &dropPre2 = dynamic_cast<const expressions::Predicate &>(*dropPre.arguments()[2]);
|
|
|
|
ASSERT_EQ(dropPre2.name(), "lifting");
|
|
|
|
ASSERT_EQ(dropPre2.arguments().size(), 2u);
|
|
|
|
const auto &dropPre21 = *dynamic_cast<const expressions::Reference<expressions::Variable> &>(*dropPre2.arguments()[1]).value();
|
|
|
|
ASSERT_EQ(dropPre21.name(), "c");
|
|
|
|
ASSERT_EQ(dropPre21.type(), &crate);
|
|
|
|
|
|
|
|
const auto &dropEff = dynamic_cast<const expressions::And &>(drop.effect());
|
|
|
|
ASSERT_EQ(dropEff.arguments().size(), 5u);
|
|
|
|
const auto &dropEff2 = dynamic_cast<const expressions::Not &>(*dropEff.arguments()[2]);
|
|
|
|
const auto &dropEff20 = dynamic_cast<const expressions::Predicate &>(dropEff2.argument());
|
|
|
|
ASSERT_EQ(dropEff20.name(), "clear");
|
|
|
|
ASSERT_EQ(dropEff20.arguments().size(), 1u);
|
|
|
|
const auto &dropEff200 = *dynamic_cast<const expressions::Reference<expressions::Variable> &>(*dropEff20.arguments()[0]).value();
|
|
|
|
ASSERT_EQ(dropEff200.name(), "a1");
|
|
|
|
ASSERT_EQ(dropEff200.type(), &storearea);
|
2016-05-30 15:44:13 +02:00
|
|
|
}
|
|
|
|
catch (const std::exception &e)
|
|
|
|
{
|
|
|
|
FAIL() << e.what();
|
|
|
|
}
|
|
|
|
}
|
2016-06-01 16:47:34 +02:00
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
TEST_F(PDDLParserTests, ParseConstants)
|
|
|
|
{
|
2016-06-04 14:46:05 +02:00
|
|
|
using namespace plasp::pddl;
|
|
|
|
|
2016-06-01 16:47:34 +02:00
|
|
|
try
|
|
|
|
{
|
2016-06-04 14:46:05 +02:00
|
|
|
const auto description = Description::fromStream(m_woodworkingDomainFile);
|
2016-06-01 16:47:34 +02:00
|
|
|
|
|
|
|
ASSERT_NO_THROW(description.domain());
|
|
|
|
|
|
|
|
const auto &domain = description.domain();
|
|
|
|
|
|
|
|
// Name
|
|
|
|
ASSERT_EQ(domain.name(), "woodworking");
|
|
|
|
|
|
|
|
// Types
|
|
|
|
const auto &acolour = *domain.types()[0];
|
|
|
|
const auto &surface = *domain.types()[4];
|
|
|
|
const auto &treatmentstatus = *domain.types()[5];
|
|
|
|
|
|
|
|
// Constants
|
|
|
|
ASSERT_EQ(domain.constants().size(), 8u);
|
|
|
|
ASSERT_EQ(domain.constants()[0]->type(), &surface);
|
|
|
|
ASSERT_EQ(domain.constants()[2]->type(), &surface);
|
|
|
|
ASSERT_EQ(domain.constants()[3]->type(), &treatmentstatus);
|
|
|
|
ASSERT_EQ(domain.constants()[6]->type(), &treatmentstatus);
|
|
|
|
ASSERT_EQ(domain.constants()[7]->type(), &acolour);
|
2016-06-07 14:07:17 +02:00
|
|
|
|
|
|
|
// TODO: add test with constants in predicates
|
2016-06-01 16:47:34 +02:00
|
|
|
}
|
|
|
|
catch (const std::exception &e)
|
|
|
|
{
|
|
|
|
FAIL() << e.what();
|
|
|
|
}
|
|
|
|
}
|
2016-06-02 23:41:40 +02:00
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
TEST_F(PDDLParserTests, ParseWithWhiteSpace)
|
|
|
|
{
|
2016-06-04 14:46:05 +02:00
|
|
|
using namespace plasp::pddl;
|
|
|
|
|
2016-06-02 23:41:40 +02:00
|
|
|
try
|
|
|
|
{
|
2016-06-04 14:46:05 +02:00
|
|
|
ASSERT_NO_THROW(Description::fromStream(m_whiteSpaceTestFile));
|
2016-06-02 23:41:40 +02:00
|
|
|
}
|
|
|
|
catch (const std::exception &e)
|
|
|
|
{
|
|
|
|
FAIL() << e.what();
|
|
|
|
}
|
|
|
|
}
|