Made output stream a member of the SAS translator for convenience.

This commit is contained in:
Patrick Lühne 2016-06-12 22:35:31 +02:00
parent b70e62ff3b
commit 49002ac52d
3 changed files with 108 additions and 106 deletions

View File

@ -112,8 +112,8 @@ int main(int argc, char **argv)
else if (language == plasp::Language::Type::SAS) else if (language == plasp::Language::Type::SAS)
{ {
const auto description = plasp::sas::Description::fromParser(std::move(parser)); const auto description = plasp::sas::Description::fromParser(std::move(parser));
const auto translator = plasp::sas::TranslatorASP(description); const auto translator = plasp::sas::TranslatorASP(description, std::cout);
translator.translate(std::cout); translator.translate();
} }
} }
catch (const plasp::utils::ParserException &e) catch (const plasp::utils::ParserException &e)

View File

@ -19,20 +19,21 @@ namespace sas
class TranslatorASP class TranslatorASP
{ {
public: public:
explicit TranslatorASP(const Description &description); explicit TranslatorASP(const Description &description, std::ostream &ostream);
void translate(std::ostream &ostream) const; void translate() const;
private: private:
void translateRequirements(std::ostream &ostream) const; void translateRequirements() const;
void translateInitialState(std::ostream &ostream) const; void translateInitialState() const;
void translateGoal(std::ostream &ostream) const; void translateGoal() const;
void translateVariables(std::ostream &ostream) const; void translateVariables() const;
void translateActions(std::ostream &ostream) const; void translateActions() const;
void translateMutexes(std::ostream &ostream) const; void translateMutexes() const;
void translateAxiomRules(std::ostream &ostream) const; void translateAxiomRules() const;
const Description &m_description; const Description &m_description;
std::ostream &m_ostream;
}; };
//////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////

View File

@ -11,89 +11,90 @@ namespace sas
// //
//////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////
TranslatorASP::TranslatorASP(const Description &description) TranslatorASP::TranslatorASP(const Description &description, std::ostream &ostream)
: m_description(description) : m_description(description),
m_ostream(ostream)
{ {
} }
//////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////
void TranslatorASP::translate(std::ostream &ostream) const void TranslatorASP::translate() const
{ {
translateRequirements(ostream); translateRequirements();
translateInitialState(ostream); translateInitialState();
translateGoal(ostream); translateGoal();
translateVariables(ostream); translateVariables();
translateActions(ostream); translateActions();
translateMutexes(ostream); translateMutexes();
translateAxiomRules(ostream); translateAxiomRules();
} }
//////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////
void TranslatorASP::translateRequirements(std::ostream &ostream) const void TranslatorASP::translateRequirements() const
{ {
ostream << "% feature requirements" << std::endl; m_ostream << "% feature requirements" << std::endl;
if (m_description.usesActionCosts()) if (m_description.usesActionCosts())
ostream << "requiresFeature(actionCosts)." << std::endl; m_ostream << "requiresFeature(actionCosts)." << std::endl;
if (m_description.usesAxiomRules()) if (m_description.usesAxiomRules())
ostream << "requiresFeature(axiomRules)." << std::endl; m_ostream << "requiresFeature(axiomRules)." << std::endl;
if (m_description.usesConditionalEffects()) if (m_description.usesConditionalEffects())
ostream << "requiresFeature(conditionalEffects)." << std::endl; m_ostream << "requiresFeature(conditionalEffects)." << std::endl;
ostream << std::endl; m_ostream << std::endl;
} }
//////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////
void TranslatorASP::translateInitialState(std::ostream &ostream) const void TranslatorASP::translateInitialState() const
{ {
ostream << "% initial state" << std::endl; m_ostream << "% initial state" << std::endl;
const auto &initialStateFacts = m_description.initialState().facts(); const auto &initialStateFacts = m_description.initialState().facts();
std::for_each(initialStateFacts.cbegin(), initialStateFacts.cend(), std::for_each(initialStateFacts.cbegin(), initialStateFacts.cend(),
[&](const auto &fact) [&](const auto &fact)
{ {
ostream << "initialState("; m_ostream << "initialState(";
fact.variable().printNameAsASPPredicate(ostream); fact.variable().printNameAsASPPredicate(m_ostream);
ostream << ", "; m_ostream << ", ";
fact.value().printAsASPPredicate(ostream); fact.value().printAsASPPredicate(m_ostream);
ostream << ")." << std::endl; m_ostream << ")." << std::endl;
}); });
ostream << std::endl; m_ostream << std::endl;
} }
//////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////
void TranslatorASP::translateGoal(std::ostream &ostream) const void TranslatorASP::translateGoal() const
{ {
ostream << "% goal" << std::endl; m_ostream << "% goal" << std::endl;
const auto &goalFacts = m_description.goal().facts(); const auto &goalFacts = m_description.goal().facts();
std::for_each(goalFacts.cbegin(), goalFacts.cend(), std::for_each(goalFacts.cbegin(), goalFacts.cend(),
[&](const auto &fact) [&](const auto &fact)
{ {
ostream << "goal("; m_ostream << "goal(";
fact.variable().printNameAsASPPredicate(ostream); fact.variable().printNameAsASPPredicate(m_ostream);
ostream << ", "; m_ostream << ", ";
fact.value().printAsASPPredicate(ostream); fact.value().printAsASPPredicate(m_ostream);
ostream << ")." << std::endl; m_ostream << ")." << std::endl;
}); });
ostream << std::endl; m_ostream << std::endl;
} }
//////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////
void TranslatorASP::translateVariables(std::ostream &ostream) const void TranslatorASP::translateVariables() const
{ {
ostream << "% variables"; m_ostream << "% variables";
const auto &variables = m_description.variables(); const auto &variables = m_description.variables();
@ -104,51 +105,51 @@ void TranslatorASP::translateVariables(std::ostream &ostream) const
BOOST_ASSERT(!values.empty()); BOOST_ASSERT(!values.empty());
ostream << std::endl; m_ostream << std::endl;
variable.printNameAsASPPredicate(ostream); variable.printNameAsASPPredicate(m_ostream);
ostream << "." << std::endl; m_ostream << "." << std::endl;
std::for_each(values.cbegin(), values.cend(), std::for_each(values.cbegin(), values.cend(),
[&](const auto &value) [&](const auto &value)
{ {
ostream << "contains("; m_ostream << "contains(";
variable.printNameAsASPPredicate(ostream); variable.printNameAsASPPredicate(m_ostream);
ostream << ", "; m_ostream << ", ";
value.printAsASPPredicate(ostream); value.printAsASPPredicate(m_ostream);
ostream << ")." << std::endl; m_ostream << ")." << std::endl;
}); });
}); });
ostream << std::endl; m_ostream << std::endl;
} }
//////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////
void TranslatorASP::translateActions(std::ostream &ostream) const void TranslatorASP::translateActions() const
{ {
ostream << "% actions"; m_ostream << "% actions";
const auto &operators = m_description.operators(); const auto &operators = m_description.operators();
std::for_each(operators.cbegin(), operators.cend(), std::for_each(operators.cbegin(), operators.cend(),
[&](const auto &operator_) [&](const auto &operator_)
{ {
ostream << std::endl; m_ostream << std::endl;
operator_.printPredicateAsASP(ostream); operator_.printPredicateAsASP(m_ostream);
ostream << "." << std::endl; m_ostream << "." << std::endl;
const auto &preconditions = operator_.preconditions(); const auto &preconditions = operator_.preconditions();
std::for_each(preconditions.cbegin(), preconditions.cend(), std::for_each(preconditions.cbegin(), preconditions.cend(),
[&](const auto &precondition) [&](const auto &precondition)
{ {
ostream << "precondition("; m_ostream << "precondition(";
operator_.printPredicateAsASP(ostream); operator_.printPredicateAsASP(m_ostream);
ostream << ", "; m_ostream << ", ";
precondition.variable().printNameAsASPPredicate(ostream); precondition.variable().printNameAsASPPredicate(m_ostream);
ostream << ", "; m_ostream << ", ";
precondition.value().printAsASPPredicate(ostream); precondition.value().printAsASPPredicate(m_ostream);
ostream << ")." << std::endl; m_ostream << ")." << std::endl;
}); });
const auto &effects = operator_.effects(); const auto &effects = operator_.effects();
@ -163,39 +164,39 @@ void TranslatorASP::translateActions(std::ostream &ostream) const
std::for_each(conditions.cbegin(), conditions.cend(), std::for_each(conditions.cbegin(), conditions.cend(),
[&](const auto &condition) [&](const auto &condition)
{ {
ostream << "effectCondition("; m_ostream << "effectCondition(";
operator_.printPredicateAsASP(ostream); operator_.printPredicateAsASP(m_ostream);
ostream << ", effect(" << currentEffectID << "), "; m_ostream << ", effect(" << currentEffectID << "), ";
condition.variable().printNameAsASPPredicate(ostream); condition.variable().printNameAsASPPredicate(m_ostream);
ostream << ", "; m_ostream << ", ";
condition.value().printAsASPPredicate(ostream); condition.value().printAsASPPredicate(m_ostream);
ostream << ")." << std::endl; m_ostream << ")." << std::endl;
}); });
ostream << "postcondition("; m_ostream << "postcondition(";
operator_.printPredicateAsASP(ostream); operator_.printPredicateAsASP(m_ostream);
ostream << ", effect(" << currentEffectID << "), "; m_ostream << ", effect(" << currentEffectID << "), ";
effect.postcondition().variable().printNameAsASPPredicate(ostream); effect.postcondition().variable().printNameAsASPPredicate(m_ostream);
ostream << ", "; m_ostream << ", ";
effect.postcondition().value().printAsASPPredicate(ostream); effect.postcondition().value().printAsASPPredicate(m_ostream);
ostream << ")." << std::endl; m_ostream << ")." << std::endl;
currentEffectID++; currentEffectID++;
}); });
ostream << "costs("; m_ostream << "costs(";
operator_.printPredicateAsASP(ostream); operator_.printPredicateAsASP(m_ostream);
ostream << ", " << operator_.costs() << ")." << std::endl; m_ostream << ", " << operator_.costs() << ")." << std::endl;
}); });
ostream << std::endl; m_ostream << std::endl;
} }
//////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////
void TranslatorASP::translateMutexes(std::ostream &ostream) const void TranslatorASP::translateMutexes() const
{ {
ostream << "% mutex groups"; m_ostream << "% mutex groups";
const auto &mutexGroups = m_description.mutexGroups(); const auto &mutexGroups = m_description.mutexGroups();
@ -207,31 +208,31 @@ void TranslatorASP::translateMutexes(std::ostream &ostream) const
const auto mutexGroupID = std::to_string(currentMutexGroupID); const auto mutexGroupID = std::to_string(currentMutexGroupID);
currentMutexGroupID++; currentMutexGroupID++;
ostream << std::endl << "mutexGroup(" << mutexGroupID << ")." << std::endl; m_ostream << std::endl << "mutexGroup(" << mutexGroupID << ")." << std::endl;
const auto &facts = mutexGroup.facts(); const auto &facts = mutexGroup.facts();
std::for_each(facts.cbegin(), facts.cend(), std::for_each(facts.cbegin(), facts.cend(),
[&](const auto &fact) [&](const auto &fact)
{ {
ostream << "contains(mutexGroup(" << mutexGroupID << "), "; m_ostream << "contains(mutexGroup(" << mutexGroupID << "), ";
fact.variable().printNameAsASPPredicate(ostream); fact.variable().printNameAsASPPredicate(m_ostream);
ostream << ", "; m_ostream << ", ";
fact.value().printAsASPPredicate(ostream); fact.value().printAsASPPredicate(m_ostream);
ostream << ")." << std::endl; m_ostream << ")." << std::endl;
}); });
}); });
} }
//////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////
void TranslatorASP::translateAxiomRules(std::ostream &ostream) const void TranslatorASP::translateAxiomRules() const
{ {
if (!m_description.usesActionCosts()) if (!m_description.usesActionCosts())
return; return;
ostream << std::endl; m_ostream << std::endl;
ostream << "% axiom rules"; m_ostream << "% axiom rules";
const auto &axiomRules = m_description.axiomRules(); const auto &axiomRules = m_description.axiomRules();
@ -243,27 +244,27 @@ void TranslatorASP::translateAxiomRules(std::ostream &ostream) const
const auto axiomRuleID = std::to_string(currentAxiomRuleID); const auto axiomRuleID = std::to_string(currentAxiomRuleID);
currentAxiomRuleID++; currentAxiomRuleID++;
ostream << std::endl << "axiomRule(" << axiomRuleID << ")." << std::endl; m_ostream << std::endl << "axiomRule(" << axiomRuleID << ")." << std::endl;
const auto &conditions = axiomRule.conditions(); const auto &conditions = axiomRule.conditions();
std::for_each(conditions.cbegin(), conditions.cend(), std::for_each(conditions.cbegin(), conditions.cend(),
[&](const auto &condition) [&](const auto &condition)
{ {
ostream << "condition(axiomRule(" << axiomRuleID << "), "; m_ostream << "condition(axiomRule(" << axiomRuleID << "), ";
condition.variable().printNameAsASPPredicate(ostream); condition.variable().printNameAsASPPredicate(m_ostream);
ostream << ", "; m_ostream << ", ";
condition.value().printAsASPPredicate(ostream); condition.value().printAsASPPredicate(m_ostream);
ostream << ")." << std::endl; m_ostream << ")." << std::endl;
}); });
const auto &postcondition = axiomRule.postcondition(); const auto &postcondition = axiomRule.postcondition();
ostream << "postcondition(axiomRule(axiomRule" << axiomRuleID << "), "; m_ostream << "postcondition(axiomRule(axiomRule" << axiomRuleID << "), ";
postcondition.variable().printNameAsASPPredicate(ostream); postcondition.variable().printNameAsASPPredicate(m_ostream);
ostream << ", "; m_ostream << ", ";
postcondition.value().printAsASPPredicate(ostream); postcondition.value().printAsASPPredicate(m_ostream);
ostream << ")." << std::endl; m_ostream << ")." << std::endl;
}); });
} }