Added constraints derived from SAS variables to ASP output and fixed mutex group constraints.

This commit is contained in:
Patrick Lühne 2016-05-22 16:35:57 +02:00
parent 97eaff0535
commit 4258dfcfd0
3 changed files with 54 additions and 17 deletions

View File

@ -41,6 +41,7 @@ struct Value
public: public:
void printAsSAS(std::ostream &ostream) const; void printAsSAS(std::ostream &ostream) const;
void printAsASP(std::ostream &ostream) const; void printAsASP(std::ostream &ostream) const;
void printAsASPCommaSeparated(std::ostream &ostream) const;
Sign sign() const; Sign sign() const;
const std::string &name() const; const std::string &name() const;

View File

@ -105,7 +105,7 @@ void TranslatorASP::translate(std::ostream &ostream) const
[&](const auto &fact) [&](const auto &fact)
{ {
ostream << "init("; ostream << "init(";
fact.value().printAsASP(ostream); fact.value().printAsASPCommaSeparated(ostream);
ostream << ")." << std::endl; ostream << ")." << std::endl;
}); });
@ -118,7 +118,7 @@ void TranslatorASP::translate(std::ostream &ostream) const
[&](const auto &fact) [&](const auto &fact)
{ {
ostream << "goal("; ostream << "goal(";
fact.value().printAsASP(ostream); fact.value().printAsASPCommaSeparated(ostream);
ostream << ")." << std::endl; ostream << ")." << std::endl;
}); });
@ -141,9 +141,9 @@ void TranslatorASP::translate(std::ostream &ostream) const
{ {
ostream << "precondition("; ostream << "precondition(";
operator_.predicate().printAsASP(ostream); operator_.predicate().printAsASP(ostream);
ostream << ", " << precondition.value().name() ostream << ", ";
<< ", " << (precondition.value().sign() == Value::Sign::Positive ? "true" : "false") precondition.value().printAsASPCommaSeparated(ostream);
<< ")." << std::endl; ostream << ")." << std::endl;
}); });
const auto &effects = operator_.effects(); const auto &effects = operator_.effects();
@ -153,31 +153,60 @@ void TranslatorASP::translate(std::ostream &ostream) const
{ {
ostream << "postcondition("; ostream << "postcondition(";
operator_.predicate().printAsASP(ostream); operator_.predicate().printAsASP(ostream);
ostream << ", " << effect.postcondition().value().name() ostream << ", ";
<< ", " << (effect.postcondition().value().sign() == Value::Sign::Positive ? "true" : "false") effect.postcondition().value().printAsASPCommaSeparated(ostream);
<< ")." << std::endl; ostream << ")." << std::endl;
}); });
}); });
ostream << std::endl; ostream << std::endl;
ostream << "% mutex groups" << std::endl; ostream << "% constraints derived from SAS variables" << std::endl;
std::for_each(variables.cbegin(), variables.cend(),
[&](const auto &variable)
{
const auto &values = variable.values();
// Skip trivial constraints of the form :- x, not x.
if (values.size() == 2 && values[0].name() == values[1].name())
return;
for (auto i = values.cbegin(); i != values.cend(); i++)
for (auto j = i + 1; j != values.cend(); j++)
{
const auto &value1 = *i;
const auto &value2 = *j;
ostream << ":- time(T), holds(";
value1.printAsASPCommaSeparated(ostream);
ostream << ", T), holds(";
value2.printAsASPCommaSeparated(ostream);
ostream << ", T)." << std::endl;
}
});
ostream << std::endl;
ostream << "% constraints derived from SAS mutex groups" << std::endl;
const auto &mutexGroups = m_description.mutexGroups(); const auto &mutexGroups = m_description.mutexGroups();
std::for_each(mutexGroups.cbegin(), mutexGroups.cend(), std::for_each(mutexGroups.cbegin(), mutexGroups.cend(),
[&](const auto &mutexGroup) [&](const auto &mutexGroup)
{ {
ostream << ":- time(T)"; const auto &facts = mutexGroup.facts();
std::for_each(mutexGroup.facts().cbegin(), mutexGroup.facts().cend(), for (auto i = facts.cbegin(); i != facts.cend(); i++)
[&](const auto &fact) for (auto j = i + 1; j != facts.cend(); j++)
{ {
ostream << ", holds("; const auto &value1 = i->value();
fact.value().printAsASP(ostream); const auto &value2 = j->value();
ostream << ", T)";
});
ostream << "." << std::endl; ostream << ":- time(T), holds(";
value1.printAsASPCommaSeparated(ostream);
ostream << ", T), holds(";
value2.printAsASPCommaSeparated(ostream);
ostream << ", T)." << std::endl;
}
}); });
} }

View File

@ -113,6 +113,13 @@ void Value::printAsASP(std::ostream &ostream) const
//////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////
void Value::printAsASPCommaSeparated(std::ostream &ostream) const
{
ostream << m_name << ", " << (m_sign == Sign::Positive ? "true" : "false");
}
////////////////////////////////////////////////////////////////////////////////////////////////////
void Value::printAsSAS(std::ostream &ostream) const void Value::printAsSAS(std::ostream &ostream) const
{ {
if (m_sign == Value::Sign::Positive) if (m_sign == Value::Sign::Positive)