|
|
@ -35,31 +35,43 @@ namespace qi = boost::spirit::qi; |
|
|
|
namespace phoenix = boost::phoenix; |
|
|
|
|
|
|
|
namespace storm { |
|
|
|
namespace parser { |
|
|
|
namespace prism { |
|
|
|
namespace parser { |
|
|
|
namespace prism { |
|
|
|
|
|
|
|
std::shared_ptr<BaseExpression> PrismGrammar::addIntegerConstant(std::string const& name, std::shared_ptr<BaseExpression> const& value) { |
|
|
|
this->state->integerConstants_.add(name, value); |
|
|
|
this->state->allConstantNames_.add(name, name); |
|
|
|
return value; |
|
|
|
} |
|
|
|
|
|
|
|
void PrismGrammar::addLabel(std::string const& name, std::shared_ptr<BaseExpression> const& value, std::map<std::string, std::shared_ptr<BaseExpression>>& nameToExpressionMap) { |
|
|
|
void PrismGrammar::addLabel(std::string const& name, std::shared_ptr<BaseExpression> const& value, std::map<std::string, std::unique_ptr<BaseExpression>>& nameToExpressionMap) { |
|
|
|
this->state->labelNames_.add(name, name); |
|
|
|
nameToExpressionMap[name] = value; |
|
|
|
} |
|
|
|
nameToExpressionMap[name] = value->clone(); |
|
|
|
} |
|
|
|
|
|
|
|
void PrismGrammar::addIntegerAssignment(std::string const& variable, std::shared_ptr<BaseExpression> const& value, std::map<std::string, Assignment>& variableToAssignmentMap) { |
|
|
|
void PrismGrammar::addIntegerAssignment(std::string const& variable, std::shared_ptr<BaseExpression> const& value, std::map<std::string, Assignment>& variableToAssignmentMap) { |
|
|
|
this->state->assignedIntegerVariables_.add(variable, variable); |
|
|
|
variableToAssignmentMap[variable] = Assignment(variable, value); |
|
|
|
} |
|
|
|
variableToAssignmentMap[variable] = Assignment(variable, value->clone()); |
|
|
|
} |
|
|
|
|
|
|
|
void PrismGrammar::addBooleanAssignment(std::string const& variable, std::shared_ptr<BaseExpression> const& value, std::map<std::string, Assignment>& variableToAssigmentMap) { |
|
|
|
void PrismGrammar::addBooleanAssignment(std::string const& variable, std::shared_ptr<BaseExpression> const& value, std::map<std::string, Assignment>& variableToAssigmentMap) { |
|
|
|
this->state->assignedBooleanVariables_.add(variable, variable); |
|
|
|
variableToAssigmentMap[variable] = Assignment(variable, value); |
|
|
|
} |
|
|
|
variableToAssigmentMap[variable] = Assignment(variable, value->clone()); |
|
|
|
} |
|
|
|
|
|
|
|
void PrismGrammar::addUndefinedBooleanConstant(std::string const& name, std::map<std::string, std::unique_ptr<BooleanConstantExpression>>& nameToExpressionMap) { |
|
|
|
this->state->booleanConstants_.add(name, std::shared_ptr<BaseExpression>(new BooleanConstantExpression(name))); |
|
|
|
this->state->allConstantNames_.add(name, name); |
|
|
|
nameToExpressionMap.emplace(name, std::unique_ptr<BooleanConstantExpression>(new BooleanConstantExpression(dynamic_cast<BooleanConstantExpression&>(*this->state->booleanConstants_.at(name))))); |
|
|
|
} |
|
|
|
|
|
|
|
void PrismGrammar::addUndefinedIntegerConstant(std::string const& name, std::map<std::string, std::unique_ptr<IntegerConstantExpression>>& nameToExpressionMap) { |
|
|
|
this->state->integerConstants_.add(name, std::shared_ptr<BaseExpression>(new IntegerConstantExpression(name))); |
|
|
|
this->state->allConstantNames_.add(name, name); |
|
|
|
nameToExpressionMap.emplace(name, std::unique_ptr<IntegerConstantExpression>(new IntegerConstantExpression(dynamic_cast<IntegerConstantExpression&>(*this->state->integerConstants_.at(name))))); |
|
|
|
} |
|
|
|
|
|
|
|
void PrismGrammar::addUndefinedDoubleConstant(std::string const& name, std::map<std::string, std::unique_ptr<DoubleConstantExpression>>& nameToExpressionMap) { |
|
|
|
this->state->doubleConstants_.add(name, std::shared_ptr<BaseExpression>(new DoubleConstantExpression(name))); |
|
|
|
this->state->allConstantNames_.add(name, name); |
|
|
|
nameToExpressionMap.emplace(name, std::unique_ptr<DoubleConstantExpression>(new DoubleConstantExpression(dynamic_cast<DoubleConstantExpression&>(*this->state->doubleConstants_.at(name))))); |
|
|
|
} |
|
|
|
|
|
|
|
Module PrismGrammar::renameModule(std::string const& newName, std::string const& oldName, std::map<std::string, std::string> const& renaming) { |
|
|
|
Module PrismGrammar::renameModule(std::string const& newName, std::string const& oldName, std::map<std::string, std::string> const& renaming) { |
|
|
|
this->state->moduleNames_.add(newName, newName); |
|
|
|
Module* old = this->moduleMap_.find(oldName); |
|
|
|
if (old == nullptr) { |
|
|
@ -69,72 +81,72 @@ Module PrismGrammar::renameModule(std::string const& newName, std::string const& |
|
|
|
Module res(*old, newName, renaming, *this->state); |
|
|
|
this->moduleMap_.at(newName) = res; |
|
|
|
return res; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
Module PrismGrammar::createModule(std::string const& name, std::vector<BooleanVariable> const& bools, std::vector<IntegerVariable> const& ints, std::map<std::string, uint_fast64_t> const& boolids, std::map<std::string, uint_fast64_t> const& intids, std::vector<storm::ir::Command> const& commands) { |
|
|
|
Module PrismGrammar::createModule(std::string const& name, std::vector<BooleanVariable> const& bools, std::vector<IntegerVariable> const& ints, std::map<std::string, uint_fast64_t> const& boolids, std::map<std::string, uint_fast64_t> const& intids, std::vector<storm::ir::Command> const& commands) { |
|
|
|
this->state->moduleNames_.add(name, name); |
|
|
|
Module res(name, bools, ints, boolids, intids, commands); |
|
|
|
this->moduleMap_.at(name) = res; |
|
|
|
return res; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
void PrismGrammar::createIntegerVariable(std::string const& name, std::shared_ptr<BaseExpression> const& lower, std::shared_ptr<BaseExpression> const& upper, std::shared_ptr<BaseExpression> const& init, std::vector<IntegerVariable>& vars, std::map<std::string, uint_fast64_t>& varids, bool isGlobalVariable) { |
|
|
|
void PrismGrammar::createIntegerVariable(std::string const& name, std::shared_ptr<BaseExpression> const& lower, std::shared_ptr<BaseExpression> const& upper, std::shared_ptr<BaseExpression> const& init, std::vector<IntegerVariable>& vars, std::map<std::string, uint_fast64_t>& varids, bool isGlobalVariable) { |
|
|
|
uint_fast64_t id = this->state->addIntegerVariable(name); |
|
|
|
uint_fast64_t newLocalIndex = this->state->nextLocalIntegerVariableIndex; |
|
|
|
vars.emplace_back(newLocalIndex, id, name, lower, upper, init); |
|
|
|
vars.emplace_back(newLocalIndex, id, name, lower->clone(), upper->clone(), init->clone()); |
|
|
|
varids[name] = newLocalIndex; |
|
|
|
++this->state->nextLocalIntegerVariableIndex; |
|
|
|
this->state->localIntegerVariables_.add(name, name); |
|
|
|
if (isGlobalVariable) { |
|
|
|
this->state->globalIntegerVariables_.add(name, name); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
void PrismGrammar::createBooleanVariable(std::string const& name, std::shared_ptr<BaseExpression> const& init, std::vector<BooleanVariable>& vars, std::map<std::string, uint_fast64_t>& varids, bool isGlobalVariable) { |
|
|
|
void PrismGrammar::createBooleanVariable(std::string const& name, std::shared_ptr<BaseExpression> const& init, std::vector<BooleanVariable>& vars, std::map<std::string, uint_fast64_t>& varids, bool isGlobalVariable) { |
|
|
|
uint_fast64_t id = this->state->addBooleanVariable(name); |
|
|
|
uint_fast64_t newLocalIndex = this->state->nextLocalBooleanVariableIndex; |
|
|
|
vars.emplace_back(newLocalIndex, id, name, init); |
|
|
|
vars.emplace_back(newLocalIndex, id, name, init->clone()); |
|
|
|
varids[name] = newLocalIndex; |
|
|
|
++this->state->nextLocalBooleanVariableIndex; |
|
|
|
this->state->localBooleanVariables_.add(name, name); |
|
|
|
if (isGlobalVariable) { |
|
|
|
this->state->globalBooleanVariables_.add(name, name); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
StateReward createStateReward(std::shared_ptr<BaseExpression> guard, std::shared_ptr<BaseExpression> reward) { |
|
|
|
return StateReward(guard, reward); |
|
|
|
} |
|
|
|
TransitionReward createTransitionReward(std::string const& label, std::shared_ptr<BaseExpression> guard, std::shared_ptr<BaseExpression> reward) { |
|
|
|
return TransitionReward(label, guard, reward); |
|
|
|
} |
|
|
|
void createRewardModel(std::string const& name, std::vector<StateReward>& stateRewards, std::vector<TransitionReward>& transitionRewards, std::map<std::string, RewardModel>& mapping) { |
|
|
|
} |
|
|
|
|
|
|
|
StateReward createStateReward(std::shared_ptr<BaseExpression> const& guard, std::shared_ptr<BaseExpression> const& reward) { |
|
|
|
return StateReward(guard->clone(), reward->clone()); |
|
|
|
} |
|
|
|
TransitionReward createTransitionReward(std::string const& label, std::shared_ptr<BaseExpression> const& guard, std::shared_ptr<BaseExpression> const& reward) { |
|
|
|
return TransitionReward(label, guard->clone(), reward->clone()); |
|
|
|
} |
|
|
|
void createRewardModel(std::string const& name, std::vector<StateReward>& stateRewards, std::vector<TransitionReward>& transitionRewards, std::map<std::string, RewardModel>& mapping) { |
|
|
|
mapping[name] = RewardModel(name, stateRewards, transitionRewards); |
|
|
|
} |
|
|
|
Update PrismGrammar::createUpdate(std::shared_ptr<BaseExpression> likelihood, std::map<std::string, Assignment> const& bools, std::map<std::string, Assignment> const& ints) { |
|
|
|
} |
|
|
|
Update PrismGrammar::createUpdate(std::shared_ptr<BaseExpression> const& likelihood, std::map<std::string, Assignment> const& bools, std::map<std::string, Assignment> const& ints) { |
|
|
|
this->state->nextGlobalUpdateIndex++; |
|
|
|
return Update(this->state->getNextGlobalUpdateIndex() - 1, likelihood, bools, ints); |
|
|
|
} |
|
|
|
Command PrismGrammar::createCommand(std::string const& label, std::shared_ptr<BaseExpression> guard, std::vector<Update> const& updates) { |
|
|
|
return Update(this->state->getNextGlobalUpdateIndex() - 1, likelihood->clone(), bools, ints); |
|
|
|
} |
|
|
|
Command PrismGrammar::createCommand(std::string const& label, std::shared_ptr<BaseExpression> const& guard, std::vector<Update> const& updates) { |
|
|
|
this->state->nextGlobalCommandIndex++; |
|
|
|
return Command(this->state->getNextGlobalCommandIndex() - 1, label, guard, updates); |
|
|
|
} |
|
|
|
Program createProgram( |
|
|
|
return Command(this->state->getNextGlobalCommandIndex() - 1, label, guard->clone(), updates); |
|
|
|
} |
|
|
|
Program createProgram( |
|
|
|
Program::ModelType modelType, |
|
|
|
std::map<std::string, std::shared_ptr<BooleanConstantExpression>> const& undefBoolConst, |
|
|
|
std::map<std::string, std::shared_ptr<IntegerConstantExpression>> const& undefIntConst, |
|
|
|
std::map<std::string, std::shared_ptr<DoubleConstantExpression>> const& undefDoubleConst, |
|
|
|
std::map<std::string, std::unique_ptr<BooleanConstantExpression>> const& undefBoolConst, |
|
|
|
std::map<std::string, std::unique_ptr<IntegerConstantExpression>> const& undefIntConst, |
|
|
|
std::map<std::string, std::unique_ptr<DoubleConstantExpression>> const& undefDoubleConst, |
|
|
|
GlobalVariableInformation const& globalVariableInformation, |
|
|
|
std::vector<Module> const& modules, |
|
|
|
std::map<std::string, RewardModel> const& rewards, |
|
|
|
std::map<std::string, std::shared_ptr<BaseExpression>> const& labels) { |
|
|
|
std::map<std::string, std::unique_ptr<BaseExpression>> const& labels) { |
|
|
|
return Program(modelType, undefBoolConst, undefIntConst, undefDoubleConst, |
|
|
|
globalVariableInformation.booleanVariables, globalVariableInformation.integerVariables, |
|
|
|
globalVariableInformation.booleanVariableToIndexMap, |
|
|
|
globalVariableInformation.integerVariableToIndexMap, modules, rewards, labels); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
PrismGrammar::PrismGrammar() : PrismGrammar::base_type(start), state(new VariableState()) { |
|
|
|
PrismGrammar::PrismGrammar() : PrismGrammar::base_type(start), state(new VariableState()) { |
|
|
|
|
|
|
|
labelDefinition = (qi::lit("label") >> -qi::lit("\"") >> FreeIdentifierGrammar::instance(this->state) >> -qi::lit("\"") >> qi::lit("=") >> BooleanExpressionGrammar::instance(this->state) >> qi::lit(";")) |
|
|
|
[phoenix::bind(&PrismGrammar::addLabel, this, qi::_1, qi::_2, qi::_r1)]; |
|
|
@ -211,18 +223,15 @@ PrismGrammar::PrismGrammar() : PrismGrammar::base_type(start), state(new Variabl |
|
|
|
// This block defines all entities that are needed for parsing constant definitions.
|
|
|
|
definedBooleanConstantDefinition = (qi::lit("const") >> qi::lit("bool") >> FreeIdentifierGrammar::instance(this->state) >> qi::lit("=") > ConstBooleanExpressionGrammar::instance(this->state) > qi::lit(";"))[phoenix::bind(this->state->booleanConstants_.add, qi::_1, qi::_2), phoenix::bind(this->state->allConstantNames_.add, qi::_1, qi::_1), qi::_val = qi::_2]; |
|
|
|
definedBooleanConstantDefinition.name("defined boolean constant declaration"); |
|
|
|
definedIntegerConstantDefinition = ( |
|
|
|
qi::lit("const") >> qi::lit("int") >> FreeIdentifierGrammar::instance(this->state) >> qi::lit("=") >> |
|
|
|
ConstIntegerExpressionGrammar::instance(this->state) >> qi::lit(";") |
|
|
|
)[ qi::_val = phoenix::bind(&PrismGrammar::addIntegerConstant, this, qi::_1, qi::_2) ]; |
|
|
|
definedIntegerConstantDefinition = (qi::lit("const") >> qi::lit("int") >> FreeIdentifierGrammar::instance(this->state) >> qi::lit("=") >> ConstIntegerExpressionGrammar::instance(this->state) >> qi::lit(";"))[phoenix::bind(this->state->integerConstants_.add, qi::_1, qi::_2), phoenix::bind(this->state->allConstantNames_.add, qi::_1, qi::_1), qi::_val = qi::_2]; |
|
|
|
definedIntegerConstantDefinition.name("defined integer constant declaration"); |
|
|
|
definedDoubleConstantDefinition = (qi::lit("const") >> qi::lit("double") >> FreeIdentifierGrammar::instance(this->state) >> qi::lit("=") > ConstDoubleExpressionGrammar::instance(this->state) > qi::lit(";"))[phoenix::bind(this->state->doubleConstants_.add, qi::_1, qi::_2), phoenix::bind(this->state->allConstantNames_.add, qi::_1, qi::_1), qi::_val = qi::_2]; |
|
|
|
definedDoubleConstantDefinition.name("defined double constant declaration"); |
|
|
|
undefinedBooleanConstantDefinition = (qi::lit("const") >> qi::lit("bool") > FreeIdentifierGrammar::instance(this->state) > qi::lit(";"))[qi::_a = phoenix::construct<std::shared_ptr<BooleanConstantExpression>>(phoenix::new_<BooleanConstantExpression>(qi::_1)), phoenix::insert(qi::_r1, phoenix::construct<std::pair<std::string, std::shared_ptr<BooleanConstantExpression>>>(qi::_1, qi::_a)), phoenix::bind(this->state->booleanConstants_.add, qi::_1, qi::_a), phoenix::bind(this->state->allConstantNames_.add, qi::_1, qi::_1)]; |
|
|
|
undefinedBooleanConstantDefinition = (qi::lit("const") >> qi::lit("bool") > FreeIdentifierGrammar::instance(this->state) > qi::lit(";"))[phoenix::bind(&PrismGrammar::addUndefinedBooleanConstant, this, qi::_1, qi::_r1)]; |
|
|
|
undefinedBooleanConstantDefinition.name("undefined boolean constant declaration"); |
|
|
|
undefinedIntegerConstantDefinition = (qi::lit("const") >> qi::lit("int") > FreeIdentifierGrammar::instance(this->state) > qi::lit(";"))[qi::_a = phoenix::construct<std::shared_ptr<IntegerConstantExpression>>(phoenix::new_<IntegerConstantExpression>(qi::_1)), phoenix::insert(qi::_r1, phoenix::construct<std::pair<std::string, std::shared_ptr<IntegerConstantExpression>>>(qi::_1, qi::_a)), phoenix::bind(this->state->integerConstants_.add, qi::_1, qi::_a), phoenix::bind(this->state->allConstantNames_.add, qi::_1, qi::_1)]; |
|
|
|
undefinedIntegerConstantDefinition = (qi::lit("const") >> qi::lit("int") > FreeIdentifierGrammar::instance(this->state) > qi::lit(";"))[phoenix::bind(&PrismGrammar::addUndefinedIntegerConstant, this, qi::_1, qi::_r1)]; |
|
|
|
undefinedIntegerConstantDefinition.name("undefined integer constant declaration"); |
|
|
|
undefinedDoubleConstantDefinition = (qi::lit("const") >> qi::lit("double") > FreeIdentifierGrammar::instance(this->state) > qi::lit(";"))[qi::_a = phoenix::construct<std::shared_ptr<DoubleConstantExpression>>(phoenix::new_<DoubleConstantExpression>(qi::_1)), phoenix::insert(qi::_r1, phoenix::construct<std::pair<std::string, std::shared_ptr<DoubleConstantExpression>>>(qi::_1, qi::_a)), phoenix::bind(this->state->doubleConstants_.add, qi::_1, qi::_a), phoenix::bind(this->state->allConstantNames_.add, qi::_1, qi::_1)]; |
|
|
|
undefinedDoubleConstantDefinition = (qi::lit("const") >> qi::lit("double") > FreeIdentifierGrammar::instance(this->state) > qi::lit(";"))[phoenix::bind(&PrismGrammar::addUndefinedDoubleConstant, this, qi::_1, qi::_r1)]; |
|
|
|
undefinedDoubleConstantDefinition.name("undefined double constant declaration"); |
|
|
|
definedConstantDefinition %= (definedBooleanConstantDefinition | definedIntegerConstantDefinition | definedDoubleConstantDefinition); |
|
|
|
definedConstantDefinition.name("defined constant declaration"); |
|
|
@ -242,9 +251,9 @@ PrismGrammar::PrismGrammar() : PrismGrammar::base_type(start), state(new Variabl |
|
|
|
rewardDefinitionList(qi::_e) > |
|
|
|
labelDefinitionList(qi::_f))[qi::_val = phoenix::bind(&createProgram, qi::_1, qi::_a, qi::_b, qi::_c, qi::_d, qi::_2, qi::_e, qi::_f)]; |
|
|
|
start.name("probabilistic program declaration"); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
void PrismGrammar::prepareForSecondRun() { |
|
|
|
void PrismGrammar::prepareForSecondRun() { |
|
|
|
LOG4CPLUS_INFO(logger, "Preparing parser for second run."); |
|
|
|
this->state->prepareForSecondRun(); |
|
|
|
BooleanExpressionGrammar::secondRun(); |
|
|
@ -252,17 +261,17 @@ void PrismGrammar::prepareForSecondRun() { |
|
|
|
ConstDoubleExpressionGrammar::secondRun(); |
|
|
|
ConstIntegerExpressionGrammar::secondRun(); |
|
|
|
IntegerExpressionGrammar::secondRun(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
void PrismGrammar::resetGrammars() { |
|
|
|
void PrismGrammar::resetGrammars() { |
|
|
|
LOG4CPLUS_INFO(logger, "Resetting grammars."); |
|
|
|
BooleanExpressionGrammar::resetInstance(); |
|
|
|
ConstBooleanExpressionGrammar::resetInstance(); |
|
|
|
ConstDoubleExpressionGrammar::resetInstance(); |
|
|
|
ConstIntegerExpressionGrammar::resetInstance(); |
|
|
|
IntegerExpressionGrammar::resetInstance(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} // namespace prism
|
|
|
|
} // namespace parser
|
|
|
|
} // namespace prism
|
|
|
|
} // namespace parser
|
|
|
|
} // namespace storm
|
xxxxxxxxxx