Browse Source

Removed shared_ptr for module, program and rewardmodel objects.

main
gereon 12 years ago
parent
commit
9613d099bb
  1. 58
      src/adapters/ExplicitModelAdapter.cpp
  2. 8
      src/adapters/ExplicitModelAdapter.h
  3. 22
      src/ir/Program.cpp
  4. 16
      src/ir/Program.h
  5. 10
      src/ir/RewardModel.cpp
  6. 10
      src/ir/RewardModel.h
  7. 46
      src/parser/PrismParser.cpp
  8. 43
      src/parser/PrismParser.h
  9. 2
      src/parser/PrismParser/VariableState.h

58
src/adapters/ExplicitModelAdapter.cpp

@ -24,7 +24,7 @@ namespace storm {
namespace adapters { namespace adapters {
ExplicitModelAdapter::ExplicitModelAdapter(std::shared_ptr<storm::ir::Program> program) : program(program),
ExplicitModelAdapter::ExplicitModelAdapter(storm::ir::Program program) : program(program),
booleanVariables(), integerVariables(), booleanVariableToIndexMap(), integerVariableToIndexMap(), booleanVariables(), integerVariables(), booleanVariableToIndexMap(), integerVariableToIndexMap(),
allStates(), stateToIndexMap(), numberOfTransitions(0), numberOfChoices(0), transitionRewards(nullptr), transitionMap() { allStates(), stateToIndexMap(), numberOfTransitions(0), numberOfChoices(0), transitionRewards(nullptr), transitionMap() {
this->initializeVariables(); this->initializeVariables();
@ -39,12 +39,12 @@ ExplicitModelAdapter::~ExplicitModelAdapter() {
this->buildTransitionMap(); this->buildTransitionMap();
std::shared_ptr<storm::models::AtomicPropositionsLabeling> stateLabeling = this->getStateLabeling(this->program->getLabels());
std::shared_ptr<storm::models::AtomicPropositionsLabeling> stateLabeling = this->getStateLabeling(this->program.getLabels());
std::shared_ptr<std::vector<double>> stateRewards = nullptr; std::shared_ptr<std::vector<double>> stateRewards = nullptr;
this->rewardModel = nullptr; this->rewardModel = nullptr;
if (rewardModelName != "") { if (rewardModelName != "") {
this->rewardModel = this->program->getRewardModel(rewardModelName);
this->rewardModel = std::unique_ptr<storm::ir::RewardModel>(new storm::ir::RewardModel(this->program.getRewardModel(rewardModelName)));;
if (this->rewardModel != nullptr) { if (this->rewardModel != nullptr) {
if (this->rewardModel->hasStateRewards()) { if (this->rewardModel->hasStateRewards()) {
stateRewards = this->getStateRewards(this->rewardModel->getStateRewards()); stateRewards = this->getStateRewards(this->rewardModel->getStateRewards());
@ -52,7 +52,7 @@ ExplicitModelAdapter::~ExplicitModelAdapter() {
} }
} }
switch (this->program->getModelType())
switch (this->program.getModelType())
{ {
case storm::ir::Program::DTMC: case storm::ir::Program::DTMC:
{ {
@ -93,11 +93,11 @@ ExplicitModelAdapter::~ExplicitModelAdapter() {
std::get<1>(*state)[index] = value; std::get<1>(*state)[index] = value;
} }
std::shared_ptr<std::vector<double>> ExplicitModelAdapter::getStateRewards(std::vector<std::shared_ptr<storm::ir::StateReward>> const & rewards) {
std::shared_ptr<std::vector<double>> ExplicitModelAdapter::getStateRewards(std::vector<storm::ir::StateReward> const & rewards) {
std::shared_ptr<std::vector<double>> results(new std::vector<double>(this->allStates.size())); std::shared_ptr<std::vector<double>> results(new std::vector<double>(this->allStates.size()));
for (uint_fast64_t index = 0; index < this->allStates.size(); index++) { for (uint_fast64_t index = 0; index < this->allStates.size(); index++) {
for (auto reward: rewards) { for (auto reward: rewards) {
(*results)[index] = reward->getReward(this->allStates[index]);
(*results)[index] = reward.getReward(this->allStates[index]);
} }
} }
return results; return results;
@ -121,9 +121,9 @@ ExplicitModelAdapter::~ExplicitModelAdapter() {
void ExplicitModelAdapter::initializeVariables() { void ExplicitModelAdapter::initializeVariables() {
uint_fast64_t numberOfIntegerVariables = 0; uint_fast64_t numberOfIntegerVariables = 0;
uint_fast64_t numberOfBooleanVariables = 0; uint_fast64_t numberOfBooleanVariables = 0;
for (uint_fast64_t i = 0; i < program->getNumberOfModules(); ++i) {
numberOfIntegerVariables += program->getModule(i)->getNumberOfIntegerVariables();
numberOfBooleanVariables += program->getModule(i)->getNumberOfBooleanVariables();
for (uint_fast64_t i = 0; i < program.getNumberOfModules(); ++i) {
numberOfIntegerVariables += program.getModule(i).getNumberOfIntegerVariables();
numberOfBooleanVariables += program.getModule(i).getNumberOfBooleanVariables();
} }
this->booleanVariables.resize(numberOfBooleanVariables); this->booleanVariables.resize(numberOfBooleanVariables);
@ -131,17 +131,17 @@ ExplicitModelAdapter::~ExplicitModelAdapter() {
uint_fast64_t nextBooleanVariableIndex = 0; uint_fast64_t nextBooleanVariableIndex = 0;
uint_fast64_t nextIntegerVariableIndex = 0; uint_fast64_t nextIntegerVariableIndex = 0;
for (uint_fast64_t i = 0; i < program->getNumberOfModules(); ++i) {
std::shared_ptr<storm::ir::Module> const module = program->getModule(i);
for (uint_fast64_t i = 0; i < program.getNumberOfModules(); ++i) {
storm::ir::Module const& module = program.getModule(i);
for (uint_fast64_t j = 0; j < module->getNumberOfBooleanVariables(); ++j) {
this->booleanVariables[nextBooleanVariableIndex] = module->getBooleanVariable(j);
this->booleanVariableToIndexMap[module->getBooleanVariable(j).getName()] = nextBooleanVariableIndex;
for (uint_fast64_t j = 0; j < module.getNumberOfBooleanVariables(); ++j) {
this->booleanVariables[nextBooleanVariableIndex] = module.getBooleanVariable(j);
this->booleanVariableToIndexMap[module.getBooleanVariable(j).getName()] = nextBooleanVariableIndex;
++nextBooleanVariableIndex; ++nextBooleanVariableIndex;
} }
for (uint_fast64_t j = 0; j < module->getNumberOfIntegerVariables(); ++j) {
this->integerVariables[nextIntegerVariableIndex] = module->getIntegerVariable(j);
this->integerVariableToIndexMap[module->getIntegerVariable(j).getName()] = nextIntegerVariableIndex;
for (uint_fast64_t j = 0; j < module.getNumberOfIntegerVariables(); ++j) {
this->integerVariables[nextIntegerVariableIndex] = module.getIntegerVariable(j);
this->integerVariableToIndexMap[module.getIntegerVariable(j).getName()] = nextIntegerVariableIndex;
++nextIntegerVariableIndex; ++nextIntegerVariableIndex;
} }
} }
@ -164,17 +164,17 @@ ExplicitModelAdapter::~ExplicitModelAdapter() {
std::unique_ptr<std::list<std::list<storm::ir::Command>>> res = std::unique_ptr<std::list<std::list<storm::ir::Command>>>(new std::list<std::list<storm::ir::Command>>()); std::unique_ptr<std::list<std::list<storm::ir::Command>>> res = std::unique_ptr<std::list<std::list<storm::ir::Command>>>(new std::list<std::list<storm::ir::Command>>());
// Iterate over all modules. // Iterate over all modules.
for (uint_fast64_t i = 0; i < this->program->getNumberOfModules(); ++i) {
std::shared_ptr<storm::ir::Module> const module = this->program->getModule(i);
for (uint_fast64_t i = 0; i < this->program.getNumberOfModules(); ++i) {
storm::ir::Module const& module = this->program.getModule(i);
std::shared_ptr<std::set<uint_fast64_t>> ids = module->getCommandsByAction(action);
std::shared_ptr<std::set<uint_fast64_t>> ids = module.getCommandsByAction(action);
std::list<storm::ir::Command> commands; std::list<storm::ir::Command> commands;
// Look up commands by their id. Add, if guard holds. // Look up commands by their id. Add, if guard holds.
for (uint_fast64_t id : *ids) { for (uint_fast64_t id : *ids) {
storm::ir::Command cmd = module->getCommand(id);
storm::ir::Command cmd = module.getCommand(id);
if (cmd.getGuard()->getValueAsBool(state)) { if (cmd.getGuard()->getValueAsBool(state)) {
commands.push_back(module->getCommand(id));
commands.push_back(module.getCommand(id));
} }
} }
res->push_back(commands); res->push_back(commands);
@ -293,11 +293,11 @@ ExplicitModelAdapter::~ExplicitModelAdapter() {
void ExplicitModelAdapter::addUnlabeledTransitions(const uint_fast64_t stateID, std::list<std::pair<std::string, std::map<uint_fast64_t, double>>>& res) { void ExplicitModelAdapter::addUnlabeledTransitions(const uint_fast64_t stateID, std::list<std::pair<std::string, std::map<uint_fast64_t, double>>>& res) {
const StateType* state = this->allStates[stateID]; const StateType* state = this->allStates[stateID];
// Iterate over all modules. // Iterate over all modules.
for (uint_fast64_t i = 0; i < program->getNumberOfModules(); ++i) {
std::shared_ptr<storm::ir::Module> const module = program->getModule(i);
for (uint_fast64_t i = 0; i < program.getNumberOfModules(); ++i) {
storm::ir::Module const& module = program.getModule(i);
// Iterate over all commands. // Iterate over all commands.
for (uint_fast64_t j = 0; j < module->getNumberOfCommands(); ++j) {
storm::ir::Command const& command = module->getCommand(j);
for (uint_fast64_t j = 0; j < module.getNumberOfCommands(); ++j) {
storm::ir::Command const& command = module.getCommand(j);
// Only consider unlabeled commands. // Only consider unlabeled commands.
if (command.getActionName() != "") continue; if (command.getActionName() != "") continue;
// Omit, if command is not active. // Omit, if command is not active.
@ -334,7 +334,7 @@ ExplicitModelAdapter::~ExplicitModelAdapter() {
*/ */
void ExplicitModelAdapter::addLabeledTransitions(const uint_fast64_t stateID, std::list<std::pair<std::string, std::map<uint_fast64_t, double>>>& res) { void ExplicitModelAdapter::addLabeledTransitions(const uint_fast64_t stateID, std::list<std::pair<std::string, std::map<uint_fast64_t, double>>>& res) {
// Create a copy of the current state, as we will free intermediate states... // Create a copy of the current state, as we will free intermediate states...
for (std::string action : this->program->getActions()) {
for (std::string action : this->program.getActions()) {
StateType* state = new StateType(*this->allStates[stateID]); StateType* state = new StateType(*this->allStates[stateID]);
std::unique_ptr<std::list<std::list<storm::ir::Command>>> cmds = this->getActiveCommandsByAction(state, action); std::unique_ptr<std::list<std::list<storm::ir::Command>>> cmds = this->getActiveCommandsByAction(state, action);
@ -432,7 +432,7 @@ ExplicitModelAdapter::~ExplicitModelAdapter() {
map[elem.first] += elem.second; map[elem.first] += elem.second;
if ((this->rewardModel != nullptr) && (this->rewardModel->hasTransitionRewards())) { if ((this->rewardModel != nullptr) && (this->rewardModel->hasTransitionRewards())) {
for (auto reward : this->rewardModel->getTransitionRewards()) { for (auto reward : this->rewardModel->getTransitionRewards()) {
rewardMap[elem.first] += reward->getReward(choice.first, this->allStates[state]);
rewardMap[elem.first] += reward.getReward(choice.first, this->allStates[state]);
} }
} }
} }
@ -474,7 +474,7 @@ ExplicitModelAdapter::~ExplicitModelAdapter() {
if ((this->rewardModel != nullptr) && (this->rewardModel->hasTransitionRewards())) { if ((this->rewardModel != nullptr) && (this->rewardModel->hasTransitionRewards())) {
double rewardValue = 0; double rewardValue = 0;
for (auto reward : this->rewardModel->getTransitionRewards()) { for (auto reward : this->rewardModel->getTransitionRewards()) {
rewardValue = reward->getReward(choice.first, this->allStates[state]);
rewardValue = reward.getReward(choice.first, this->allStates[state]);
} }
this->transitionRewards->addNextValue(nextRow, it.first, rewardValue); this->transitionRewards->addNextValue(nextRow, it.first, rewardValue);
} }

8
src/adapters/ExplicitModelAdapter.h

@ -53,7 +53,7 @@ public:
class ExplicitModelAdapter { class ExplicitModelAdapter {
public: public:
ExplicitModelAdapter(std::shared_ptr<storm::ir::Program> program);
ExplicitModelAdapter(storm::ir::Program program);
~ExplicitModelAdapter(); ~ExplicitModelAdapter();
std::shared_ptr<storm::models::AbstractModel> getModel(std::string const & rewardModelName = ""); std::shared_ptr<storm::models::AbstractModel> getModel(std::string const & rewardModelName = "");
@ -91,7 +91,7 @@ private:
*/ */
void initializeVariables(); void initializeVariables();
std::shared_ptr<std::vector<double>> getStateRewards(std::vector<std::shared_ptr<storm::ir::StateReward>> const & rewards);
std::shared_ptr<std::vector<double>> getStateRewards(std::vector<storm::ir::StateReward> const & rewards);
std::shared_ptr<storm::models::AtomicPropositionsLabeling> getStateLabeling(std::map<std::string, std::shared_ptr<storm::ir::expressions::BaseExpression>> labels); std::shared_ptr<storm::models::AtomicPropositionsLabeling> getStateLabeling(std::map<std::string, std::shared_ptr<storm::ir::expressions::BaseExpression>> labels);
/*! /*!
@ -166,14 +166,14 @@ private:
void clearInternalState(); void clearInternalState();
// Program that should be converted. // Program that should be converted.
std::shared_ptr<storm::ir::Program> program;
storm::ir::Program program;
std::vector<storm::ir::BooleanVariable> booleanVariables; std::vector<storm::ir::BooleanVariable> booleanVariables;
std::vector<storm::ir::IntegerVariable> integerVariables; std::vector<storm::ir::IntegerVariable> integerVariables;
std::map<std::string, uint_fast64_t> booleanVariableToIndexMap; std::map<std::string, uint_fast64_t> booleanVariableToIndexMap;
std::map<std::string, uint_fast64_t> integerVariableToIndexMap; std::map<std::string, uint_fast64_t> integerVariableToIndexMap;
// Members that are filled during the conversion. // Members that are filled during the conversion.
std::shared_ptr<storm::ir::RewardModel> rewardModel;
std::unique_ptr<storm::ir::RewardModel> rewardModel;
std::vector<StateType*> allStates; std::vector<StateType*> allStates;
std::unordered_map<StateType*, uint_fast64_t, StateHash, StateCompare> stateToIndexMap; std::unordered_map<StateType*, uint_fast64_t, StateHash, StateCompare> stateToIndexMap;
uint_fast64_t numberOfTransitions; uint_fast64_t numberOfTransitions;

22
src/ir/Program.cpp

@ -25,15 +25,15 @@ Program::Program() : modelType(UNDEFINED), booleanUndefinedConstantExpressions()
} }
// Initializes all members according to the given values. // Initializes all members according to the given values.
Program::Program(ModelType modelType, std::map<std::string, std::shared_ptr<storm::ir::expressions::BooleanConstantExpression>> booleanUndefinedConstantExpressions, std::map<std::string, std::shared_ptr<storm::ir::expressions::IntegerConstantExpression>> integerUndefinedConstantExpressions, std::map<std::string, std::shared_ptr<storm::ir::expressions::DoubleConstantExpression>> doubleUndefinedConstantExpressions, std::vector<std::shared_ptr<storm::ir::Module>> modules, std::map<std::string, std::shared_ptr<storm::ir::RewardModel>> rewards, std::map<std::string, std::shared_ptr<storm::ir::expressions::BaseExpression>> labels)
Program::Program(ModelType modelType, std::map<std::string, std::shared_ptr<storm::ir::expressions::BooleanConstantExpression>> booleanUndefinedConstantExpressions, std::map<std::string, std::shared_ptr<storm::ir::expressions::IntegerConstantExpression>> integerUndefinedConstantExpressions, std::map<std::string, std::shared_ptr<storm::ir::expressions::DoubleConstantExpression>> doubleUndefinedConstantExpressions, std::vector<storm::ir::Module> modules, std::map<std::string, storm::ir::RewardModel> rewards, std::map<std::string, std::shared_ptr<storm::ir::expressions::BaseExpression>> labels)
: modelType(modelType), booleanUndefinedConstantExpressions(booleanUndefinedConstantExpressions), integerUndefinedConstantExpressions(integerUndefinedConstantExpressions), doubleUndefinedConstantExpressions(doubleUndefinedConstantExpressions), modules(modules), rewards(rewards), labels(labels), actionsToModuleIndexMap() { : modelType(modelType), booleanUndefinedConstantExpressions(booleanUndefinedConstantExpressions), integerUndefinedConstantExpressions(integerUndefinedConstantExpressions), doubleUndefinedConstantExpressions(doubleUndefinedConstantExpressions), modules(modules), rewards(rewards), labels(labels), actionsToModuleIndexMap() {
// Build actionsToModuleIndexMap // Build actionsToModuleIndexMap
for (unsigned int id = 0; id < this->modules.size(); id++) { for (unsigned int id = 0; id < this->modules.size(); id++) {
for (auto action : this->modules[id]->getActions()) {
for (auto action : this->modules[id].getActions()) {
if (this->actionsToModuleIndexMap.count(action) == 0) { if (this->actionsToModuleIndexMap.count(action) == 0) {
this->actionsToModuleIndexMap[action] = std::shared_ptr<std::set<uint_fast64_t>>(new std::set<uint_fast64_t>());
this->actionsToModuleIndexMap[action] = std::set<uint_fast64_t>();
} }
this->actionsToModuleIndexMap[action]->insert(id);
this->actionsToModuleIndexMap[action].insert(id);
this->actions.insert(action); this->actions.insert(action);
} }
} }
@ -67,11 +67,11 @@ std::string Program::toString() const {
result << std::endl; result << std::endl;
for (auto module : modules) { for (auto module : modules) {
result << module->toString() << std::endl;
result << module.toString() << std::endl;
} }
for (auto rewardModel : rewards) { for (auto rewardModel : rewards) {
result << rewardModel.first << ": " << rewardModel.second->toString() << std::endl;
result << rewardModel.first << ": " << rewardModel.second.toString() << std::endl;
} }
for (auto label : labels) { for (auto label : labels) {
@ -85,7 +85,7 @@ uint_fast64_t Program::getNumberOfModules() const {
return this->modules.size(); return this->modules.size();
} }
std::shared_ptr<storm::ir::Module> const& Program::getModule(uint_fast64_t index) const {
storm::ir::Module const& Program::getModule(uint_fast64_t index) const {
return this->modules[index]; return this->modules[index];
} }
@ -95,20 +95,20 @@ std::set<std::string> const& Program::getActions() const {
} }
// Return modules with given action. // Return modules with given action.
std::shared_ptr<std::set<uint_fast64_t>> const Program::getModulesByAction(std::string const& action) const {
std::set<uint_fast64_t> const Program::getModulesByAction(std::string const& action) const {
auto res = this->actionsToModuleIndexMap.find(action); auto res = this->actionsToModuleIndexMap.find(action);
if (res == this->actionsToModuleIndexMap.end()) { if (res == this->actionsToModuleIndexMap.end()) {
return std::shared_ptr<std::set<uint_fast64_t>>(new std::set<uint_fast64_t>());
return std::set<uint_fast64_t>();
} else { } else {
return res->second; return res->second;
} }
} }
std::shared_ptr<storm::ir::RewardModel> Program::getRewardModel(std::string const & name) const {
storm::ir::RewardModel Program::getRewardModel(std::string const & name) const {
auto it = this->rewards.find(name); auto it = this->rewards.find(name);
if (it == this->rewards.end()) { if (it == this->rewards.end()) {
LOG4CPLUS_ERROR(logger, "The given reward model \"" << name << "\" does not exist. We will proceed without rewards."); LOG4CPLUS_ERROR(logger, "The given reward model \"" << name << "\" does not exist. We will proceed without rewards.");
return nullptr;
throw "Rewardmodel does not exist.";
} else { } else {
return it->second; return it->second;
} }

16
src/ir/Program.h

@ -58,8 +58,8 @@ public:
std::map<std::string, std::shared_ptr<storm::ir::expressions::BooleanConstantExpression>> booleanUndefinedConstantExpressions, std::map<std::string, std::shared_ptr<storm::ir::expressions::BooleanConstantExpression>> booleanUndefinedConstantExpressions,
std::map<std::string, std::shared_ptr<storm::ir::expressions::IntegerConstantExpression>> integerUndefinedConstantExpressions, std::map<std::string, std::shared_ptr<storm::ir::expressions::IntegerConstantExpression>> integerUndefinedConstantExpressions,
std::map<std::string, std::shared_ptr<storm::ir::expressions::DoubleConstantExpression>> doubleUndefinedConstantExpressions, std::map<std::string, std::shared_ptr<storm::ir::expressions::DoubleConstantExpression>> doubleUndefinedConstantExpressions,
std::vector<std::shared_ptr<storm::ir::Module>> modules,
std::map<std::string, std::shared_ptr<storm::ir::RewardModel>> rewards,
std::vector<storm::ir::Module> modules,
std::map<std::string, storm::ir::RewardModel> rewards,
std::map<std::string, std::shared_ptr<storm::ir::expressions::BaseExpression>> labels); std::map<std::string, std::shared_ptr<storm::ir::expressions::BaseExpression>> labels);
/*! /*!
@ -72,7 +72,7 @@ public:
* Retrieves a reference to the module with the given index. * Retrieves a reference to the module with the given index.
* @param index the index of the module to retrieve. * @param index the index of the module to retrieve.
*/ */
std::shared_ptr<storm::ir::Module> const& getModule(uint_fast64_t index) const;
storm::ir::Module const& getModule(uint_fast64_t index) const;
/*! /*!
* Retrieves the model type of the model. * Retrieves the model type of the model.
@ -98,14 +98,14 @@ public:
* @param action Name of the action. * @param action Name of the action.
* @returns Indices of all matching modules. * @returns Indices of all matching modules.
*/ */
std::shared_ptr<std::set<uint_fast64_t>> const getModulesByAction(std::string const& action) const;
std::set<uint_fast64_t> const getModulesByAction(std::string const& action) const;
/*! /*!
* Retrieve reward model with given name. * Retrieve reward model with given name.
* @param name Name of the reward model. * @param name Name of the reward model.
* @return Reward model with given name. * @return Reward model with given name.
*/ */
std::shared_ptr<storm::ir::RewardModel> getRewardModel(std::string const & name) const;
storm::ir::RewardModel getRewardModel(std::string const & name) const;
/*! /*!
* Retrieves all labels. * Retrieves all labels.
@ -127,10 +127,10 @@ private:
std::map<std::string, std::shared_ptr<storm::ir::expressions::DoubleConstantExpression>> doubleUndefinedConstantExpressions; std::map<std::string, std::shared_ptr<storm::ir::expressions::DoubleConstantExpression>> doubleUndefinedConstantExpressions;
// The modules associated with the program. // The modules associated with the program.
std::vector<std::shared_ptr<storm::ir::Module>> modules;
std::vector<storm::ir::Module> modules;
// The reward models associated with the program. // The reward models associated with the program.
std::map<std::string, std::shared_ptr<storm::ir::RewardModel>> rewards;
std::map<std::string, storm::ir::RewardModel> rewards;
// The labels that are defined for this model. // The labels that are defined for this model.
std::map<std::string, std::shared_ptr<storm::ir::expressions::BaseExpression>> labels; std::map<std::string, std::shared_ptr<storm::ir::expressions::BaseExpression>> labels;
@ -139,7 +139,7 @@ private:
std::set<std::string> actions; std::set<std::string> actions;
// A map of actions to the set of modules containing commands labelled with this action. // A map of actions to the set of modules containing commands labelled with this action.
std::map<std::string, std::shared_ptr<std::set<uint_fast64_t>>> actionsToModuleIndexMap;
std::map<std::string, std::set<uint_fast64_t>> actionsToModuleIndexMap;
}; };
} // namespace ir } // namespace ir

10
src/ir/RewardModel.cpp

@ -19,7 +19,7 @@ RewardModel::RewardModel() : rewardModelName(), stateRewards(), transitionReward
} }
// Initializes all members according to the given values. // Initializes all members according to the given values.
RewardModel::RewardModel(std::string rewardModelName, std::vector<std::shared_ptr<storm::ir::StateReward>> stateRewards, std::vector<std::shared_ptr<storm::ir::TransitionReward>> transitionRewards) : rewardModelName(rewardModelName), stateRewards(stateRewards), transitionRewards(transitionRewards) {
RewardModel::RewardModel(std::string rewardModelName, std::vector<storm::ir::StateReward> stateRewards, std::vector<storm::ir::TransitionReward> transitionRewards) : rewardModelName(rewardModelName), stateRewards(stateRewards), transitionRewards(transitionRewards) {
// Nothing to do here. // Nothing to do here.
} }
@ -28,10 +28,10 @@ std::string RewardModel::toString() const {
std::stringstream result; std::stringstream result;
result << "rewards \"" << rewardModelName << "\"" << std::endl; result << "rewards \"" << rewardModelName << "\"" << std::endl;
for (auto reward : stateRewards) { for (auto reward : stateRewards) {
result << reward->toString() << std::endl;
result << reward.toString() << std::endl;
} }
for (auto reward : transitionRewards) { for (auto reward : transitionRewards) {
result << reward->toString() << std::endl;
result << reward.toString() << std::endl;
} }
result << "endrewards" << std::endl; result << "endrewards" << std::endl;
return result.str(); return result.str();
@ -41,7 +41,7 @@ bool RewardModel::hasStateRewards() const {
return this->stateRewards.size() > 0; return this->stateRewards.size() > 0;
} }
std::vector<std::shared_ptr<storm::ir::StateReward>> RewardModel::getStateRewards() const {
std::vector<storm::ir::StateReward> RewardModel::getStateRewards() const {
return this->stateRewards; return this->stateRewards;
} }
@ -49,7 +49,7 @@ bool RewardModel::hasTransitionRewards() const {
return this->transitionRewards.size() > 0; return this->transitionRewards.size() > 0;
} }
std::vector<std::shared_ptr<storm::ir::TransitionReward>> RewardModel::getTransitionRewards() const {
std::vector<storm::ir::TransitionReward> RewardModel::getTransitionRewards() const {
return this->transitionRewards; return this->transitionRewards;
} }

10
src/ir/RewardModel.h

@ -34,7 +34,7 @@ public:
* @param stateRewards A vector of state-based reward. * @param stateRewards A vector of state-based reward.
* @param transitionRewards A vector of transition-based reward. * @param transitionRewards A vector of transition-based reward.
*/ */
RewardModel(std::string rewardModelName, std::vector<std::shared_ptr<storm::ir::StateReward>> stateRewards, std::vector<std::shared_ptr<storm::ir::TransitionReward>> transitionRewards);
RewardModel(std::string rewardModelName, std::vector<storm::ir::StateReward> stateRewards, std::vector<storm::ir::TransitionReward> transitionRewards);
/*! /*!
* Retrieves a string representation of this variable. * Retrieves a string representation of this variable.
@ -52,7 +52,7 @@ public:
* Retrieve state rewards. * Retrieve state rewards.
* @return State rewards. * @return State rewards.
*/ */
std::vector<std::shared_ptr<storm::ir::StateReward>> getStateRewards() const;
std::vector<storm::ir::StateReward> getStateRewards() const;
/*! /*!
* Check, if there are any transition rewards. * Check, if there are any transition rewards.
@ -64,17 +64,17 @@ public:
* Retrieve transition rewards. * Retrieve transition rewards.
* @return Transition rewards. * @return Transition rewards.
*/ */
std::vector<std::shared_ptr<storm::ir::TransitionReward>> getTransitionRewards() const;
std::vector<storm::ir::TransitionReward> getTransitionRewards() const;
private: private:
// The name of the reward model. // The name of the reward model.
std::string rewardModelName; std::string rewardModelName;
// The state-based rewards associated with this reward model. // The state-based rewards associated with this reward model.
std::vector<std::shared_ptr<storm::ir::StateReward>> stateRewards;
std::vector<storm::ir::StateReward> stateRewards;
// The transition-based rewards associated with this reward model. // The transition-based rewards associated with this reward model.
std::vector<std::shared_ptr<storm::ir::TransitionReward>> transitionRewards;
std::vector<storm::ir::TransitionReward> transitionRewards;
}; };
} // namespace ir } // namespace ir

46
src/parser/PrismParser.cpp

@ -58,32 +58,32 @@ namespace parser {
this->state->assignedLocalBooleanVariables_.add(variable, variable); this->state->assignedLocalBooleanVariables_.add(variable, variable);
mapping[variable] = Assignment(variable, value); mapping[variable] = Assignment(variable, value);
} }
std::shared_ptr<Module> PrismParser::PrismGrammar::renameModule(const std::string& name, const std::string& oldname, std::map<std::string, std::string>& mapping) {
Module PrismParser::PrismGrammar::renameModule(const std::string& name, const std::string& oldname, std::map<std::string, std::string>& mapping) {
this->state->moduleNames_.add(name, name); this->state->moduleNames_.add(name, name);
std::shared_ptr<Module> old = this->state->moduleMap_.at(oldname);
Module* old = this->state->moduleMap_.find(oldname);
if (old == nullptr) { if (old == nullptr) {
std::cerr << "Renaming module failed: module " << oldname << " does not exist!" << std::endl; std::cerr << "Renaming module failed: module " << oldname << " does not exist!" << std::endl;
return nullptr;
throw "Renaming module failed";
} }
std::shared_ptr<Module> res = std::shared_ptr<Module>(new Module(*old, name, mapping, this->state));
Module res(*old, name, mapping, this->state);
this->state->moduleMap_.add(name, res); this->state->moduleMap_.add(name, res);
return res; return res;
} }
std::shared_ptr<Module> PrismParser::PrismGrammar::createModule(const std::string name, std::vector<BooleanVariable>& bools, std::vector<IntegerVariable>& ints, std::map<std::string, uint_fast64_t>& boolids, std::map<std::string, uint_fast64_t> intids, std::vector<storm::ir::Command> commands) {
Module PrismParser::PrismGrammar::createModule(const std::string name, std::vector<BooleanVariable>& bools, std::vector<IntegerVariable>& ints, std::map<std::string, uint_fast64_t>& boolids, std::map<std::string, uint_fast64_t> intids, std::vector<storm::ir::Command> commands) {
this->state->moduleNames_.add(name, name); this->state->moduleNames_.add(name, name);
std::shared_ptr<Module> res = std::shared_ptr<Module>(new Module(name, bools, ints, boolids, intids, commands));
Module res(name, bools, ints, boolids, intids, commands);
this->state->moduleMap_.add(name, res); this->state->moduleMap_.add(name, res);
return res; return res;
} }
std::shared_ptr<StateReward> createStateReward(std::shared_ptr<BaseExpression> guard, std::shared_ptr<BaseExpression> reward) {
return std::shared_ptr<StateReward>(new StateReward(guard, reward));
StateReward createStateReward(std::shared_ptr<BaseExpression> guard, std::shared_ptr<BaseExpression> reward) {
return StateReward(guard, reward);
} }
std::shared_ptr<TransitionReward> createTransitionReward(std::string label, std::shared_ptr<BaseExpression> guard, std::shared_ptr<BaseExpression> reward) {
return std::shared_ptr<TransitionReward>(new TransitionReward(label, guard, reward));
TransitionReward createTransitionReward(std::string label, std::shared_ptr<BaseExpression> guard, std::shared_ptr<BaseExpression> reward) {
return TransitionReward(label, guard, reward);
} }
void createRewardModel(std::string name, std::vector<std::shared_ptr<StateReward>>& stateRewards, std::vector<std::shared_ptr<TransitionReward>>& transitionRewards, std::map<std::string, std::shared_ptr<RewardModel>>& mapping) {
mapping[name] = std::shared_ptr<RewardModel>(new RewardModel(name, stateRewards, transitionRewards));
void createRewardModel(std::string name, std::vector<StateReward>& stateRewards, std::vector<TransitionReward>& transitionRewards, std::map<std::string, RewardModel>& mapping) {
mapping[name] = RewardModel(name, stateRewards, transitionRewards);
} }
Update createUpdate(std::shared_ptr<BaseExpression> likelihood, std::map<std::string, Assignment>& bools, std::map<std::string, Assignment> ints) { Update createUpdate(std::shared_ptr<BaseExpression> likelihood, std::map<std::string, Assignment>& bools, std::map<std::string, Assignment> ints) {
return Update(likelihood, bools, ints); return Update(likelihood, bools, ints);
@ -91,15 +91,15 @@ namespace parser {
Command createCommand(std::string& label, std::shared_ptr<BaseExpression> guard, std::vector<Update>& updates) { Command createCommand(std::string& label, std::shared_ptr<BaseExpression> guard, std::vector<Update>& updates) {
return Command(label, guard, updates); return Command(label, guard, updates);
} }
std::shared_ptr<Program> createProgram(
Program createProgram(
Program::ModelType modelType, Program::ModelType modelType,
std::map<std::string, std::shared_ptr<BooleanConstantExpression>> undefBoolConst, std::map<std::string, std::shared_ptr<BooleanConstantExpression>> undefBoolConst,
std::map<std::string, std::shared_ptr<IntegerConstantExpression>> undefIntConst, std::map<std::string, std::shared_ptr<IntegerConstantExpression>> undefIntConst,
std::map<std::string, std::shared_ptr<DoubleConstantExpression>> undefDoubleConst, std::map<std::string, std::shared_ptr<DoubleConstantExpression>> undefDoubleConst,
std::vector<std::shared_ptr<Module>> modules,
std::map<std::string, std::shared_ptr<RewardModel>> rewards,
std::vector<Module> modules,
std::map<std::string, RewardModel> rewards,
std::map<std::string, std::shared_ptr<BaseExpression>> labels) { std::map<std::string, std::shared_ptr<BaseExpression>> labels) {
return std::shared_ptr<Program>(new Program(modelType, undefBoolConst, undefIntConst, undefDoubleConst, modules, rewards, labels));
return Program(modelType, undefBoolConst, undefIntConst, undefDoubleConst, modules, rewards, labels);
} }
PrismParser::PrismGrammar::PrismGrammar() : PrismParser::PrismGrammar::base_type(start), state(new storm::parser::prism::VariableState()) { PrismParser::PrismGrammar::PrismGrammar() : PrismParser::PrismGrammar::base_type(start), state(new storm::parser::prism::VariableState()) {
@ -230,14 +230,14 @@ PrismParser::PrismGrammar::PrismGrammar() : PrismParser::PrismGrammar::base_type
* closes the file properly, even if an exception is thrown in the parser. In this case, the * closes the file properly, even if an exception is thrown in the parser. In this case, the
* exception is passed on to the caller. * exception is passed on to the caller.
*/ */
std::shared_ptr<storm::ir::Program> PrismParser::parseFile(std::string const& filename) const {
storm::ir::Program PrismParser::parseFile(std::string const& filename) const {
// Open file and initialize result. // Open file and initialize result.
std::ifstream inputFileStream(filename, std::ios::in); std::ifstream inputFileStream(filename, std::ios::in);
std::shared_ptr<storm::ir::Program> result(nullptr);
storm::ir::Program result;
// Now try to parse the contents of the file. // Now try to parse the contents of the file.
try { try {
result = std::shared_ptr<storm::ir::Program>(parse(inputFileStream, filename));
result = parse(inputFileStream, filename);
} catch(std::exception& e) { } catch(std::exception& e) {
// In case of an exception properly close the file before passing exception. // In case of an exception properly close the file before passing exception.
inputFileStream.close(); inputFileStream.close();
@ -254,7 +254,7 @@ std::shared_ptr<storm::ir::Program> PrismParser::parseFile(std::string const& fi
* If the parser throws an expectation failure exception, i.e. expected input different than the one * If the parser throws an expectation failure exception, i.e. expected input different than the one
* provided, this is caught and displayed properly before the exception is passed on. * provided, this is caught and displayed properly before the exception is passed on.
*/ */
std::shared_ptr<storm::ir::Program> PrismParser::parse(std::istream& inputStream, std::string const& filename) const {
storm::ir::Program PrismParser::parse(std::istream& inputStream, std::string const& filename) const {
// Prepare iterators to input. // Prepare iterators to input.
// TODO: Right now, this parses the whole contents of the file into a string first. // TODO: Right now, this parses the whole contents of the file into a string first.
// While this is usually not necessary, because there exist adapters that make an input stream // While this is usually not necessary, because there exist adapters that make an input stream
@ -268,7 +268,7 @@ std::shared_ptr<storm::ir::Program> PrismParser::parse(std::istream& inputStream
PositionIteratorType positionIteratorEnd; PositionIteratorType positionIteratorEnd;
// Prepare resulting intermediate representation of input. // Prepare resulting intermediate representation of input.
std::shared_ptr<storm::ir::Program> result(new storm::ir::Program());
storm::ir::Program result;
// In order to instantiate the grammar, we have to pass the type of the skipping parser. // In order to instantiate the grammar, we have to pass the type of the skipping parser.
// As this is more complex, we let Boost figure out the actual type for us. // As this is more complex, we let Boost figure out the actual type for us.
@ -278,11 +278,11 @@ std::shared_ptr<storm::ir::Program> PrismParser::parse(std::istream& inputStream
// First run. // First run.
qi::phrase_parse(positionIteratorBegin, positionIteratorEnd, grammar, boost::spirit::ascii::space | qi::lit("//") >> *(qi::char_ - qi::eol) >> qi::eol, result); qi::phrase_parse(positionIteratorBegin, positionIteratorEnd, grammar, boost::spirit::ascii::space | qi::lit("//") >> *(qi::char_ - qi::eol) >> qi::eol, result);
grammar.prepareForSecondRun(); grammar.prepareForSecondRun();
result = std::shared_ptr<storm::ir::Program>(new storm::ir::Program());
result = storm::ir::Program();
std::cout << "Now we start the second run..." << std::endl; std::cout << "Now we start the second run..." << std::endl;
// Second run. // Second run.
qi::phrase_parse(positionIteratorBegin2, positionIteratorEnd, grammar, boost::spirit::ascii::space | qi::lit("//") >> *(qi::char_ - qi::eol) >> qi::eol, result); qi::phrase_parse(positionIteratorBegin2, positionIteratorEnd, grammar, boost::spirit::ascii::space | qi::lit("//") >> *(qi::char_ - qi::eol) >> qi::eol, result);
std::cout << "Here is the parsed grammar: " << std::endl << result->toString() << std::endl;
std::cout << "Here is the parsed grammar: " << std::endl << result.toString() << std::endl;
} catch(const qi::expectation_failure<PositionIteratorType>& e) { } catch(const qi::expectation_failure<PositionIteratorType>& e) {
// If the parser expected content different than the one provided, display information // If the parser expected content different than the one provided, display information
// about the location of the error. // about the location of the error.

43
src/parser/PrismParser.h

@ -1,5 +1,4 @@
/*
* PrismParser.h
/* * PrismParser.h
* *
* Created on: Jan 3, 2013 * Created on: Jan 3, 2013
* Author: Christian Dehnert * Author: Christian Dehnert
@ -42,7 +41,7 @@ public:
* @param filename the name of the file to parse. * @param filename the name of the file to parse.
* @return a shared pointer to the intermediate representation of the PRISM file. * @return a shared pointer to the intermediate representation of the PRISM file.
*/ */
std::shared_ptr<storm::ir::Program> parseFile(std::string const& filename) const;
storm::ir::Program parseFile(std::string const& filename) const;
/*! /*!
* The Boost spirit grammar for the PRISM language. Returns the intermediate representation of * The Boost spirit grammar for the PRISM language. Returns the intermediate representation of
@ -50,11 +49,15 @@ public:
*/ */
class PrismGrammar : public qi::grammar< class PrismGrammar : public qi::grammar<
Iterator, Iterator,
std::shared_ptr<Program>(),
Program(),
qi::locals< qi::locals<
std::map<std::string, std::shared_ptr<BooleanConstantExpression>>, std::map<std::string, std::shared_ptr<BooleanConstantExpression>>,
std::map<std::string, std::shared_ptr<IntegerConstantExpression>>, std::map<std::string, std::shared_ptr<IntegerConstantExpression>>,
std::map<std::string, std::shared_ptr<DoubleConstantExpression>>, std::map<std::string, std::shared_ptr<RewardModel>>, std::map<std::string, std::shared_ptr<BaseExpression>>>, Skipper> {
std::map<std::string, std::shared_ptr<DoubleConstantExpression>>,
std::map<std::string, RewardModel>,
std::map<std::string, std::shared_ptr<BaseExpression>>
>,
Skipper> {
public: public:
PrismGrammar(); PrismGrammar();
void prepareForSecondRun(); void prepareForSecondRun();
@ -66,16 +69,22 @@ public:
// The starting point of the grammar. // The starting point of the grammar.
qi::rule< qi::rule<
Iterator, Iterator,
std::shared_ptr<Program>(),
qi::locals<std::map<std::string, std::shared_ptr<BooleanConstantExpression>>, std::map<std::string, std::shared_ptr<IntegerConstantExpression>>, std::map<std::string, std::shared_ptr<DoubleConstantExpression>>, std::map<std::string, std::shared_ptr<RewardModel>>, std::map<std::string, std::shared_ptr<BaseExpression>>>,
Program(),
qi::locals<
std::map<std::string, std::shared_ptr<BooleanConstantExpression>>,
std::map<std::string, std::shared_ptr<IntegerConstantExpression>>,
std::map<std::string, std::shared_ptr<DoubleConstantExpression>>,
std::map<std::string, RewardModel>,
std::map<std::string, std::shared_ptr<BaseExpression>>
>,
Skipper> start; Skipper> start;
qi::rule<Iterator, Program::ModelType(), Skipper> modelTypeDefinition; qi::rule<Iterator, Program::ModelType(), Skipper> modelTypeDefinition;
qi::rule<Iterator, qi::unused_type(std::map<std::string, std::shared_ptr<BooleanConstantExpression>>&, std::map<std::string, std::shared_ptr<IntegerConstantExpression>>&, std::map<std::string, std::shared_ptr<DoubleConstantExpression>>&), Skipper> constantDefinitionList; qi::rule<Iterator, qi::unused_type(std::map<std::string, std::shared_ptr<BooleanConstantExpression>>&, std::map<std::string, std::shared_ptr<IntegerConstantExpression>>&, std::map<std::string, std::shared_ptr<DoubleConstantExpression>>&), Skipper> constantDefinitionList;
qi::rule<Iterator, std::vector<std::shared_ptr<Module>>(), Skipper> moduleDefinitionList;
qi::rule<Iterator, std::vector<Module>(), Skipper> moduleDefinitionList;
// Rules for module definition. // Rules for module definition.
qi::rule<Iterator, std::shared_ptr<Module>(), qi::locals<std::vector<BooleanVariable>, std::vector<IntegerVariable>, std::map<std::string, uint_fast64_t>, std::map<std::string, uint_fast64_t>>, Skipper> moduleDefinition;
qi::rule<Iterator, std::shared_ptr<Module>(), qi::locals<std::map<std::string, std::string>>, Skipper> moduleRenaming;
qi::rule<Iterator, Module(), qi::locals<std::vector<BooleanVariable>, std::vector<IntegerVariable>, std::map<std::string, uint_fast64_t>, std::map<std::string, uint_fast64_t>>, Skipper> moduleDefinition;
qi::rule<Iterator, Module(), qi::locals<std::map<std::string, std::string>>, Skipper> moduleRenaming;
// Rules for variable definitions. // Rules for variable definitions.
qi::rule<Iterator, std::shared_ptr<BaseExpression>(), Skipper> integerLiteralExpression; qi::rule<Iterator, std::shared_ptr<BaseExpression>(), Skipper> integerLiteralExpression;
@ -96,10 +105,10 @@ public:
qi::rule<Iterator, std::string(), Skipper> unassignedLocalIntegerVariableName; qi::rule<Iterator, std::string(), Skipper> unassignedLocalIntegerVariableName;
// Rules for reward definitions. // Rules for reward definitions.
qi::rule<Iterator, qi::unused_type(std::map<std::string, std::shared_ptr<RewardModel>>&), Skipper> rewardDefinitionList;
qi::rule<Iterator, qi::unused_type(std::map<std::string, std::shared_ptr<RewardModel>>&), qi::locals<std::vector<std::shared_ptr<StateReward>>, std::vector<std::shared_ptr<TransitionReward>>>, Skipper> rewardDefinition;
qi::rule<Iterator, std::shared_ptr<StateReward>(), Skipper> stateRewardDefinition;
qi::rule<Iterator, std::shared_ptr<TransitionReward>(), qi::locals<std::string>, Skipper> transitionRewardDefinition;
qi::rule<Iterator, qi::unused_type(std::map<std::string, RewardModel>&), Skipper> rewardDefinitionList;
qi::rule<Iterator, qi::unused_type(std::map<std::string, RewardModel>&), qi::locals<std::vector<StateReward>, std::vector<TransitionReward>>, Skipper> rewardDefinition;
qi::rule<Iterator, StateReward(), Skipper> stateRewardDefinition;
qi::rule<Iterator, TransitionReward(), qi::locals<std::string>, Skipper> transitionRewardDefinition;
// Rules for label definitions. // Rules for label definitions.
qi::rule<Iterator, qi::unused_type(std::map<std::string, std::shared_ptr<BaseExpression>>&), Skipper> labelDefinitionList; qi::rule<Iterator, qi::unused_type(std::map<std::string, std::shared_ptr<BaseExpression>>&), Skipper> labelDefinitionList;
@ -128,8 +137,8 @@ public:
void addLabel(const std::string& name, std::shared_ptr<BaseExpression> value, std::map<std::string, std::shared_ptr<BaseExpression>>& mapping); void addLabel(const std::string& name, std::shared_ptr<BaseExpression> value, std::map<std::string, std::shared_ptr<BaseExpression>>& mapping);
void addBoolAssignment(const std::string& variable, std::shared_ptr<BaseExpression> value, std::map<std::string, Assignment>& mapping); void addBoolAssignment(const std::string& variable, std::shared_ptr<BaseExpression> value, std::map<std::string, Assignment>& mapping);
void addIntAssignment(const std::string& variable, std::shared_ptr<BaseExpression> value, std::map<std::string, Assignment>& mapping); void addIntAssignment(const std::string& variable, std::shared_ptr<BaseExpression> value, std::map<std::string, Assignment>& mapping);
std::shared_ptr<Module> renameModule(const std::string& name, const std::string& oldname, std::map<std::string, std::string>& mapping);
std::shared_ptr<Module> createModule(const std::string name, std::vector<BooleanVariable>& bools, std::vector<IntegerVariable>& ints, std::map<std::string, uint_fast64_t>& boolids, std::map<std::string, uint_fast64_t> intids, std::vector<storm::ir::Command> commands);
Module renameModule(const std::string& name, const std::string& oldname, std::map<std::string, std::string>& mapping);
Module createModule(const std::string name, std::vector<BooleanVariable>& bools, std::vector<IntegerVariable>& ints, std::map<std::string, uint_fast64_t>& boolids, std::map<std::string, uint_fast64_t> intids, std::vector<storm::ir::Command> commands);
}; };
@ -141,7 +150,7 @@ private:
* @param filename the name of the file the input stream belongs to. Used for diagnostics. * @param filename the name of the file the input stream belongs to. Used for diagnostics.
* @return a shared pointer to the intermediate representation of the PRISM file. * @return a shared pointer to the intermediate representation of the PRISM file.
*/ */
std::shared_ptr<storm::ir::Program> parse(std::istream& inputStream, std::string const& filename) const;
storm::ir::Program parse(std::istream& inputStream, std::string const& filename) const;
}; };
} // namespace parser } // namespace parser

2
src/parser/PrismParser/VariableState.h

@ -38,7 +38,7 @@ public:
// the intermediate representation. // the intermediate representation.
struct qi::symbols<char, std::shared_ptr<VariableExpression>> integerVariables_, booleanVariables_; struct qi::symbols<char, std::shared_ptr<VariableExpression>> integerVariables_, booleanVariables_;
struct qi::symbols<char, std::shared_ptr<BaseExpression>> integerConstants_, booleanConstants_, doubleConstants_; struct qi::symbols<char, std::shared_ptr<BaseExpression>> integerConstants_, booleanConstants_, doubleConstants_;
struct qi::symbols<char, std::shared_ptr<Module>> moduleMap_;
struct qi::symbols<char, Module> moduleMap_;
// A structure representing the identity function over identifier names. // A structure representing the identity function over identifier names.
struct variableNamesStruct : qi::symbols<char, std::string> { } integerVariableNames_, booleanVariableNames_, commandNames_, labelNames_, allConstantNames_, moduleNames_, struct variableNamesStruct : qi::symbols<char, std::string> { } integerVariableNames_, booleanVariableNames_, commandNames_, labelNames_, allConstantNames_, moduleNames_,

Loading…
Cancel
Save