2016-10-08 16:03:14 +02:00
|
|
|
#include <catch.hpp>
|
2016-05-20 18:46:37 +02:00
|
|
|
|
|
|
|
#include <fstream>
|
|
|
|
#include <iostream>
|
|
|
|
#include <sstream>
|
|
|
|
#include <stdexcept>
|
|
|
|
|
|
|
|
#include <plasp/sas/Description.h>
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2016-10-08 16:03:14 +02:00
|
|
|
class SASParserTestsFixture
|
2016-05-20 18:46:37 +02:00
|
|
|
{
|
|
|
|
protected:
|
2016-10-08 16:03:14 +02:00
|
|
|
SASParserTestsFixture()
|
2016-05-23 00:47:30 +02:00
|
|
|
: m_blocksworldTestFile(readFile("data/blocksworld.sas")),
|
2016-05-24 02:03:13 +02:00
|
|
|
m_cavedivingTestFile(readFile("data/cavediving.sas")),
|
2016-05-23 01:27:28 +02:00
|
|
|
m_freecellTestFile(readFile("data/freecell.sas")),
|
2016-05-23 00:47:30 +02:00
|
|
|
m_philosophersTestFile(readFile("data/philosophers.sas"))
|
2016-05-20 18:46:37 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
static std::stringstream readFile(const std::string &path)
|
|
|
|
{
|
|
|
|
std::ifstream fileStream(path, std::ios::in);
|
|
|
|
|
|
|
|
std::stringstream outputStream;
|
|
|
|
|
|
|
|
if (!fileStream.is_open())
|
2016-06-14 12:47:39 +02:00
|
|
|
throw std::runtime_error("Could not open file “" + path + "”");
|
2016-05-20 18:46:37 +02:00
|
|
|
|
|
|
|
outputStream << fileStream.rdbuf();
|
|
|
|
|
|
|
|
return outputStream;
|
|
|
|
}
|
|
|
|
|
2016-05-23 00:47:30 +02:00
|
|
|
std::stringstream m_blocksworldTestFile;
|
2016-05-24 02:03:13 +02:00
|
|
|
std::stringstream m_cavedivingTestFile;
|
2016-05-23 01:27:28 +02:00
|
|
|
std::stringstream m_freecellTestFile;
|
2016-05-20 18:46:37 +02:00
|
|
|
std::stringstream m_philosophersTestFile;
|
|
|
|
};
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2016-10-08 16:03:14 +02:00
|
|
|
TEST_CASE_METHOD(SASParserTestsFixture, "[SAS parser] A valid SAS file is parsed correctly", "[SAS parser]")
|
2016-05-20 18:46:37 +02:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
const auto description = plasp::sas::Description::fromStream(m_philosophersTestFile);
|
|
|
|
|
2016-10-08 16:03:14 +02:00
|
|
|
REQUIRE_FALSE(description.usesActionCosts());
|
|
|
|
|
|
|
|
REQUIRE(description.variables().size() == 37u);
|
|
|
|
REQUIRE(description.variables()[0].axiomLayer() == -1);
|
|
|
|
REQUIRE(description.variables()[0].values()[0].sign() == plasp::sas::Value::Sign::Positive);
|
|
|
|
REQUIRE(description.variables()[0].values()[0].name() == "activate(philosopher-0, forks--pid-rfork)");
|
|
|
|
REQUIRE(description.variables()[36].axiomLayer() == -1);
|
|
|
|
REQUIRE(description.variables()[36].values()[1].sign() == plasp::sas::Value::Sign::Negative);
|
|
|
|
REQUIRE(description.variables()[36].values()[1].name() == "queue-tail-msg(forks-1-, fork)");
|
|
|
|
|
|
|
|
REQUIRE(description.mutexGroups().size() == 8u);
|
|
|
|
REQUIRE(description.mutexGroups()[0].facts().size() == 9u);
|
|
|
|
REQUIRE(&description.mutexGroups()[0].facts()[0].value() == &description.variables()[0].values()[0]);
|
|
|
|
REQUIRE(description.mutexGroups()[7].facts().size() == 2u);
|
|
|
|
REQUIRE(&description.mutexGroups()[7].facts()[1].value() == &description.variables()[34].values()[1]);
|
|
|
|
|
|
|
|
REQUIRE(description.initialState().facts().size() == 37u);
|
|
|
|
REQUIRE(&description.initialState().facts()[0].value() == &description.variables()[0].values()[8]);
|
|
|
|
REQUIRE(&description.initialState().facts()[36].value() == &description.variables()[36].values()[1]);
|
|
|
|
|
|
|
|
REQUIRE(description.goal().facts().size() == 2u);
|
|
|
|
REQUIRE(&description.goal().facts()[0].value() == &description.variables()[6].values()[0]);
|
|
|
|
REQUIRE(&description.goal().facts()[1].value() == &description.variables()[7].values()[0]);
|
|
|
|
|
|
|
|
REQUIRE(description.operators().size() == 34u);
|
|
|
|
REQUIRE(description.operators()[0].predicate().name() == "activate-trans");
|
|
|
|
REQUIRE(description.operators()[0].predicate().arguments().size() == 5u);
|
|
|
|
REQUIRE(description.operators()[0].predicate().arguments()[0] == "philosopher-0");
|
|
|
|
REQUIRE(description.operators()[0].predicate().arguments()[4] == "state-3");
|
|
|
|
REQUIRE(description.operators()[0].preconditions().size() == 3u);
|
|
|
|
REQUIRE(&description.operators()[0].preconditions()[0].value() == &description.variables()[4].values()[4]);
|
|
|
|
REQUIRE(&description.operators()[0].preconditions()[1].value() == &description.variables()[16].values()[1]);
|
|
|
|
REQUIRE(&description.operators()[0].preconditions()[2].value() == &description.variables()[0].values()[8]);
|
|
|
|
REQUIRE(description.operators()[0].effects().size() == 1u);
|
|
|
|
REQUIRE(description.operators()[0].effects()[0].conditions().size() == 0u);
|
|
|
|
REQUIRE(&description.operators()[0].effects()[0].postcondition().value() == &description.variables()[0].values()[0]);
|
|
|
|
REQUIRE(description.operators()[33].predicate().name() == "queue-write");
|
|
|
|
REQUIRE(description.operators()[33].predicate().arguments().size() == 4u);
|
|
|
|
REQUIRE(description.operators()[33].predicate().arguments()[0] == "philosopher-1");
|
|
|
|
REQUIRE(description.operators()[33].predicate().arguments()[3] == "fork");
|
|
|
|
REQUIRE(description.operators()[33].preconditions().size() == 2u);
|
|
|
|
REQUIRE(&description.operators()[33].preconditions()[0].value() == &description.variables()[1].values()[3]);
|
|
|
|
REQUIRE(&description.operators()[33].preconditions()[1].value() == &description.variables()[2].values()[2]);
|
|
|
|
REQUIRE(description.operators()[33].effects().size() == 3u);
|
|
|
|
REQUIRE(description.operators()[33].effects()[0].conditions().size() == 0u);
|
|
|
|
REQUIRE(&description.operators()[33].effects()[0].postcondition().value() == &description.variables()[1].values()[7]);
|
|
|
|
REQUIRE(&description.operators()[33].effects()[2].postcondition().value() == &description.variables()[35].values()[0]);
|
|
|
|
|
|
|
|
REQUIRE(description.axiomRules().size() == 33u);
|
|
|
|
REQUIRE(description.axiomRules()[0].conditions().size() == 4u);
|
|
|
|
REQUIRE(&description.axiomRules()[0].conditions()[0].value() == &description.variables()[0].values()[0]);
|
|
|
|
REQUIRE(&description.axiomRules()[0].conditions()[2].value() == &description.variables()[27].values()[0]);
|
|
|
|
REQUIRE(&description.axiomRules()[0].conditions()[3].value() == &description.variables()[8].values()[1]);
|
|
|
|
REQUIRE(&description.axiomRules()[0].postcondition().value() == &description.variables()[8].values()[0]);
|
|
|
|
REQUIRE(description.axiomRules()[32].conditions().size() == 2u);
|
|
|
|
REQUIRE(&description.axiomRules()[32].conditions()[0].value() == &description.variables()[15].values()[0]);
|
|
|
|
REQUIRE(&description.axiomRules()[32].conditions()[1].value() == &description.variables()[25].values()[0]);
|
|
|
|
REQUIRE(&description.axiomRules()[32].postcondition().value() == &description.variables()[25].values()[1]);
|
2016-05-20 18:46:37 +02:00
|
|
|
}
|
|
|
|
catch (const std::exception &e)
|
|
|
|
{
|
2016-10-08 16:03:14 +02:00
|
|
|
FAIL(e.what());
|
2016-05-20 18:46:37 +02:00
|
|
|
}
|
2016-08-30 13:01:22 +02:00
|
|
|
|
|
|
|
// TODO: add whitespace test
|
2016-05-20 18:46:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2016-10-08 16:03:14 +02:00
|
|
|
TEST_CASE_METHOD(SASParserTestsFixture, "[SAS parser] Trailing empty parentheses are removed", "[SAS parser]")
|
2016-05-23 00:47:30 +02:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
const auto description = plasp::sas::Description::fromStream(m_blocksworldTestFile);
|
|
|
|
|
2016-10-08 16:03:14 +02:00
|
|
|
REQUIRE(description.variables()[4].values()[0].name() == "handempty");
|
|
|
|
REQUIRE(description.variables()[5].values()[0].name() == "holding(a)");
|
2016-05-23 00:47:30 +02:00
|
|
|
}
|
|
|
|
catch (const std::exception &e)
|
|
|
|
{
|
2016-10-08 16:03:14 +02:00
|
|
|
FAIL(e.what());
|
2016-05-23 00:47:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2016-10-08 16:03:14 +02:00
|
|
|
TEST_CASE_METHOD(SASParserTestsFixture, "[SAS parser] “none” values are correctly parsed", "[SAS parser]")
|
2016-05-23 01:27:28 +02:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
const auto description = plasp::sas::Description::fromStream(m_freecellTestFile);
|
|
|
|
|
|
|
|
// TODO: compare by identity, not value
|
2016-10-08 16:03:14 +02:00
|
|
|
REQUIRE(description.variables()[0].values()[3] == plasp::sas::Value::None);
|
|
|
|
REQUIRE(description.variables()[5].values()[6] == plasp::sas::Value::None);
|
2016-05-23 01:27:28 +02:00
|
|
|
}
|
|
|
|
catch (const std::exception &e)
|
|
|
|
{
|
2016-10-08 16:03:14 +02:00
|
|
|
FAIL(e.what());
|
2016-05-24 02:03:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2016-10-08 16:03:14 +02:00
|
|
|
TEST_CASE_METHOD(SASParserTestsFixture, "[SAS parser] SAS requirements are parsed correctly", "[SAS parser]")
|
2016-05-24 02:03:13 +02:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
const auto description = plasp::sas::Description::fromStream(m_cavedivingTestFile);
|
|
|
|
|
2016-10-08 16:03:14 +02:00
|
|
|
REQUIRE(description.usesActionCosts());
|
|
|
|
REQUIRE(description.usesConditionalEffects());
|
|
|
|
REQUIRE_FALSE(description.usesAxiomRules());
|
2016-05-24 02:13:21 +02:00
|
|
|
|
2016-10-08 16:03:14 +02:00
|
|
|
REQUIRE(description.operators().size() == 496u);
|
2016-05-24 02:13:21 +02:00
|
|
|
|
2016-10-08 16:03:14 +02:00
|
|
|
REQUIRE(description.operators()[0].costs() == 1u);
|
|
|
|
REQUIRE(description.operators()[172].costs() == 10u);
|
|
|
|
REQUIRE(description.operators()[173].costs() == 63u);
|
2016-05-24 02:13:21 +02:00
|
|
|
|
2016-10-08 16:03:14 +02:00
|
|
|
REQUIRE(description.operators()[172].effects().size() == 3u);
|
|
|
|
REQUIRE(description.operators()[172].effects()[1].conditions().size() == 1u);
|
|
|
|
REQUIRE(&description.operators()[172].effects()[1].conditions()[0].value() == &description.variables()[1].values()[4]);
|
2016-05-24 02:03:13 +02:00
|
|
|
}
|
|
|
|
catch (const std::exception &e)
|
|
|
|
{
|
2016-10-08 16:03:14 +02:00
|
|
|
FAIL(e.what());
|
2016-05-23 01:27:28 +02:00
|
|
|
}
|
|
|
|
}
|
2016-08-30 18:47:12 +02:00
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2016-10-08 16:03:14 +02:00
|
|
|
TEST_CASE("[SAS parser] Former issues are fixed", "[SAS parser]")
|
2016-08-30 18:47:12 +02:00
|
|
|
{
|
|
|
|
// Check issue where unexpected whitespaces in SAS files led to a parsing error
|
2016-10-08 16:03:14 +02:00
|
|
|
REQUIRE_NOTHROW(plasp::sas::Description::fromFile("data/issues/issue-6.sas"));
|
2016-08-30 18:47:12 +02:00
|
|
|
}
|