diff --git a/src/adapters/ExplicitModelAdapter.cpp b/src/adapters/ExplicitModelAdapter.cpp index 39d7bfe8b..d5f9be117 100644 --- a/src/adapters/ExplicitModelAdapter.cpp +++ b/src/adapters/ExplicitModelAdapter.cpp @@ -24,7 +24,7 @@ namespace storm { 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(), allStates(), stateToIndexMap(), numberOfTransitions(0), numberOfChoices(0), transitionRewards(nullptr), transitionMap() { this->initializeVariables(); @@ -39,12 +39,12 @@ ExplicitModelAdapter::~ExplicitModelAdapter() { 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; this->rewardModel = nullptr; 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->hasStateRewards()) { 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: { @@ -93,11 +93,11 @@ ExplicitModelAdapter::~ExplicitModelAdapter() { 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())); for (uint_fast64_t index = 0; index < this->allStates.size(); index++) { for (auto reward: rewards) { - (*results)[index] = reward->getReward(this->allStates[index]); + (*results)[index] = reward.getReward(this->allStates[index]); } } return results; @@ -121,9 +121,9 @@ ExplicitModelAdapter::~ExplicitModelAdapter() { void ExplicitModelAdapter::initializeVariables() { uint_fast64_t numberOfIntegerVariables = 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); @@ -131,17 +131,17 @@ ExplicitModelAdapter::~ExplicitModelAdapter() { uint_fast64_t nextBooleanVariableIndex = 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; } - 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; } } @@ -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>>()); // 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; // Look up commands by their id. Add, if guard holds. 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)) { - commands.push_back(module->getCommand(id)); + commands.push_back(module.getCommand(id)); } } 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) { const StateType* state = this->allStates[stateID]; // 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. - 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. if (command.getActionName() != "") continue; // 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) { // 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]); 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; if ((this->rewardModel != nullptr) && (this->rewardModel->hasTransitionRewards())) { 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())) { double rewardValue = 0; 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); } diff --git a/src/adapters/ExplicitModelAdapter.h b/src/adapters/ExplicitModelAdapter.h index 73a705e33..f97f76c5e 100644 --- a/src/adapters/ExplicitModelAdapter.h +++ b/src/adapters/ExplicitModelAdapter.h @@ -53,7 +53,7 @@ public: class ExplicitModelAdapter { public: - ExplicitModelAdapter(std::shared_ptr<storm::ir::Program> program); + ExplicitModelAdapter(storm::ir::Program program); ~ExplicitModelAdapter(); std::shared_ptr<storm::models::AbstractModel> getModel(std::string const & rewardModelName = ""); @@ -91,7 +91,7 @@ private: */ 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); /*! @@ -166,14 +166,14 @@ private: void clearInternalState(); // 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::IntegerVariable> integerVariables; std::map<std::string, uint_fast64_t> booleanVariableToIndexMap; std::map<std::string, uint_fast64_t> integerVariableToIndexMap; // 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::unordered_map<StateType*, uint_fast64_t, StateHash, StateCompare> stateToIndexMap; uint_fast64_t numberOfTransitions; diff --git a/src/ir/Program.cpp b/src/ir/Program.cpp index d153a1bc6..fd75d9b3e 100644 --- a/src/ir/Program.cpp +++ b/src/ir/Program.cpp @@ -25,15 +25,15 @@ Program::Program() : modelType(UNDEFINED), booleanUndefinedConstantExpressions() } // 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() { // Build actionsToModuleIndexMap 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) { - 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); } } @@ -67,11 +67,11 @@ std::string Program::toString() const { result << std::endl; for (auto module : modules) { - result << module->toString() << std::endl; + result << module.toString() << std::endl; } for (auto rewardModel : rewards) { - result << rewardModel.first << ": " << rewardModel.second->toString() << std::endl; + result << rewardModel.first << ": " << rewardModel.second.toString() << std::endl; } for (auto label : labels) { @@ -85,7 +85,7 @@ uint_fast64_t Program::getNumberOfModules() const { 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]; } @@ -95,20 +95,20 @@ std::set<std::string> const& Program::getActions() const { } // 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); 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 { 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); if (it == this->rewards.end()) { LOG4CPLUS_ERROR(logger, "The given reward model \"" << name << "\" does not exist. We will proceed without rewards."); - return nullptr; + throw "Rewardmodel does not exist."; } else { return it->second; } diff --git a/src/ir/Program.h b/src/ir/Program.h index d51ebeb0e..a181bbcc3 100644 --- a/src/ir/Program.h +++ b/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::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::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); /*! @@ -72,7 +72,7 @@ public: * Retrieves a reference to the module with the given index. * @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. @@ -98,14 +98,14 @@ public: * @param action Name of the action. * @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. * @param name Name of the reward model. * @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. @@ -127,10 +127,10 @@ private: std::map<std::string, std::shared_ptr<storm::ir::expressions::DoubleConstantExpression>> doubleUndefinedConstantExpressions; // 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. - 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. std::map<std::string, std::shared_ptr<storm::ir::expressions::BaseExpression>> labels; @@ -139,7 +139,7 @@ private: std::set<std::string> actions; // 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 diff --git a/src/ir/RewardModel.cpp b/src/ir/RewardModel.cpp index 352c8614c..d6611e4ec 100644 --- a/src/ir/RewardModel.cpp +++ b/src/ir/RewardModel.cpp @@ -19,7 +19,7 @@ RewardModel::RewardModel() : rewardModelName(), stateRewards(), transitionReward } // 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. } @@ -28,10 +28,10 @@ std::string RewardModel::toString() const { std::stringstream result; result << "rewards \"" << rewardModelName << "\"" << std::endl; for (auto reward : stateRewards) { - result << reward->toString() << std::endl; + result << reward.toString() << std::endl; } for (auto reward : transitionRewards) { - result << reward->toString() << std::endl; + result << reward.toString() << std::endl; } result << "endrewards" << std::endl; return result.str(); @@ -41,7 +41,7 @@ bool RewardModel::hasStateRewards() const { 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; } @@ -49,7 +49,7 @@ bool RewardModel::hasTransitionRewards() const { 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; } diff --git a/src/ir/RewardModel.h b/src/ir/RewardModel.h index 29029a4db..cb538f173 100644 --- a/src/ir/RewardModel.h +++ b/src/ir/RewardModel.h @@ -34,7 +34,7 @@ public: * @param stateRewards A vector of state-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. @@ -52,7 +52,7 @@ public: * Retrieve 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. @@ -64,17 +64,17 @@ public: * Retrieve transition rewards. * @return Transition rewards. */ - std::vector<std::shared_ptr<storm::ir::TransitionReward>> getTransitionRewards() const; + std::vector<storm::ir::TransitionReward> getTransitionRewards() const; private: // The name of the reward model. std::string rewardModelName; // 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. - std::vector<std::shared_ptr<storm::ir::TransitionReward>> transitionRewards; + std::vector<storm::ir::TransitionReward> transitionRewards; }; } // namespace ir diff --git a/src/parser/PrismParser.cpp b/src/parser/PrismParser.cpp index 7340d9d3a..980853300 100644 --- a/src/parser/PrismParser.cpp +++ b/src/parser/PrismParser.cpp @@ -58,32 +58,32 @@ namespace parser { this->state->assignedLocalBooleanVariables_.add(variable, variable); 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); - std::shared_ptr<Module> old = this->state->moduleMap_.at(oldname); + Module* old = this->state->moduleMap_.find(oldname); if (old == nullptr) { 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); 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); - 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); 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) { 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) { return Command(label, guard, updates); } - std::shared_ptr<Program> createProgram( + Program createProgram( Program::ModelType modelType, 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<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) { - 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()) { @@ -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 * 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. 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. try { - result = std::shared_ptr<storm::ir::Program>(parse(inputFileStream, filename)); + result = parse(inputFileStream, filename); } catch(std::exception& e) { // In case of an exception properly close the file before passing exception. 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 * 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. // 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 @@ -268,7 +268,7 @@ std::shared_ptr<storm::ir::Program> PrismParser::parse(std::istream& inputStream PositionIteratorType positionIteratorEnd; // 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. // 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. qi::phrase_parse(positionIteratorBegin, positionIteratorEnd, grammar, boost::spirit::ascii::space | qi::lit("//") >> *(qi::char_ - qi::eol) >> qi::eol, result); 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; // Second run. 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) { // If the parser expected content different than the one provided, display information // about the location of the error. diff --git a/src/parser/PrismParser.h b/src/parser/PrismParser.h index 0da0e0821..10cdeb895 100644 --- a/src/parser/PrismParser.h +++ b/src/parser/PrismParser.h @@ -1,5 +1,4 @@ -/* - * PrismParser.h +/* * PrismParser.h * * Created on: Jan 3, 2013 * Author: Christian Dehnert @@ -42,7 +41,7 @@ public: * @param filename the name of the file to parse. * @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 @@ -50,11 +49,15 @@ public: */ class PrismGrammar : public qi::grammar< Iterator, - std::shared_ptr<Program>(), + 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>>>, Skipper> { + std::map<std::string, std::shared_ptr<DoubleConstantExpression>>, + std::map<std::string, RewardModel>, + std::map<std::string, std::shared_ptr<BaseExpression>> + >, + Skipper> { public: PrismGrammar(); void prepareForSecondRun(); @@ -66,16 +69,22 @@ public: // The starting point of the grammar. qi::rule< 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; 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, std::vector<std::shared_ptr<Module>>(), Skipper> moduleDefinitionList; + qi::rule<Iterator, std::vector<Module>(), Skipper> moduleDefinitionList; // 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. qi::rule<Iterator, std::shared_ptr<BaseExpression>(), Skipper> integerLiteralExpression; @@ -96,10 +105,10 @@ public: qi::rule<Iterator, std::string(), Skipper> unassignedLocalIntegerVariableName; // 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. 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 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); - 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. * @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 diff --git a/src/parser/PrismParser/VariableState.h b/src/parser/PrismParser/VariableState.h index 6b2c9a9b3..58a716fd1 100644 --- a/src/parser/PrismParser/VariableState.h +++ b/src/parser/PrismParser/VariableState.h @@ -38,7 +38,7 @@ public: // the intermediate representation. 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<Module>> moduleMap_; + struct qi::symbols<char, Module> moduleMap_; // A structure representing the identity function over identifier names. struct variableNamesStruct : qi::symbols<char, std::string> { } integerVariableNames_, booleanVariableNames_, commandNames_, labelNames_, allConstantNames_, moduleNames_,