From 1878962dea3eb99bbf35b34dae8b5a7359dc60a5 Mon Sep 17 00:00:00 2001 From: gereon Date: Tue, 23 Apr 2013 17:24:13 +0200 Subject: [PATCH] Fixed another nullptr, removed shared_ptr for Update and Command objects. --- src/adapters/ExplicitModelAdapter.cpp | 48 +++++++++++++-------------- src/adapters/ExplicitModelAdapter.h | 4 +-- src/ir/Command.cpp | 10 +++--- src/ir/Command.h | 6 ++-- src/ir/Module.cpp | 13 ++++---- src/ir/Module.h | 6 ++-- src/parser/PrismParser.cpp | 10 +++--- src/parser/PrismParser.h | 8 ++--- 8 files changed, 52 insertions(+), 53 deletions(-) diff --git a/src/adapters/ExplicitModelAdapter.cpp b/src/adapters/ExplicitModelAdapter.cpp index dd5fb247e..39d7bfe8b 100644 --- a/src/adapters/ExplicitModelAdapter.cpp +++ b/src/adapters/ExplicitModelAdapter.cpp @@ -160,20 +160,20 @@ ExplicitModelAdapter::~ExplicitModelAdapter() { * @param action Action label. * @return Active commands. */ - std::unique_ptr>>> ExplicitModelAdapter::getActiveCommandsByAction(StateType const * state, std::string& action) { - std::unique_ptr>>> res = std::unique_ptr>>>(new std::list>>()); + std::unique_ptr>> ExplicitModelAdapter::getActiveCommandsByAction(StateType const * state, std::string& action) { + std::unique_ptr>> res = std::unique_ptr>>(new std::list>()); // Iterate over all modules. for (uint_fast64_t i = 0; i < this->program->getNumberOfModules(); ++i) { std::shared_ptr const module = this->program->getModule(i); std::shared_ptr> ids = module->getCommandsByAction(action); - std::list> commands; + std::list commands; // Look up commands by their id. Add, if guard holds. for (uint_fast64_t id : *ids) { - std::shared_ptr cmd = module->getCommand(id); - if (cmd->getGuard()->getValueAsBool(state)) { + storm::ir::Command cmd = module->getCommand(id); + if (cmd.getGuard()->getValueAsBool(state)) { commands.push_back(module->getCommand(id)); } } @@ -181,7 +181,7 @@ ExplicitModelAdapter::~ExplicitModelAdapter() { } // Sort the result in the vague hope that having small lists at the beginning will speed up the expanding. // This is how lambdas may look like in C++... - res->sort([](const std::list>& a, const std::list>& b){ return a.size() < b.size(); }); + res->sort([](const std::list& a, const std::list& b){ return a.size() < b.size(); }); return res; } @@ -191,12 +191,12 @@ ExplicitModelAdapter::~ExplicitModelAdapter() { * @params update Update to be applied. * @return Resulting state. */ - StateType* ExplicitModelAdapter::applyUpdate(StateType const * const state, std::shared_ptr const update) const { + StateType* ExplicitModelAdapter::applyUpdate(StateType const * const state, storm::ir::Update const& update) const { StateType* newState = new StateType(*state); - for (auto assignedVariable : update->getBooleanAssignments()) { + for (auto assignedVariable : update.getBooleanAssignments()) { setValue(newState, this->booleanVariableToIndexMap.at(assignedVariable.first), assignedVariable.second.getExpression()->getValueAsBool(state)); } - for (auto assignedVariable : update->getIntegerAssignments()) { + for (auto assignedVariable : update.getIntegerAssignments()) { setValue(newState, this->integerVariableToIndexMap.at(assignedVariable.first), assignedVariable.second.getExpression()->getValueAsInt(state)); } return newState; @@ -297,29 +297,29 @@ ExplicitModelAdapter::~ExplicitModelAdapter() { std::shared_ptr const module = program->getModule(i); // Iterate over all commands. for (uint_fast64_t j = 0; j < module->getNumberOfCommands(); ++j) { - std::shared_ptr const command = module->getCommand(j); + storm::ir::Command const& command = module->getCommand(j); // Only consider unlabeled commands. - if (command->getActionName() != "") continue; + if (command.getActionName() != "") continue; // Omit, if command is not active. - if (!command->getGuard()->getValueAsBool(state)) continue; + if (!command.getGuard()->getValueAsBool(state)) continue; // Add a new map and get pointer. res.emplace_back(); std::map* states = &res.back().second; // Iterate over all updates. - for (uint_fast64_t k = 0; k < command->getNumberOfUpdates(); ++k) { + for (uint_fast64_t k = 0; k < command.getNumberOfUpdates(); ++k) { // Obtain new state id. - std::shared_ptr const update = command->getUpdate(k); + storm::ir::Update const& update = command.getUpdate(k); uint_fast64_t newStateId = this->getOrAddStateId(this->applyUpdate(state, update)); // Check, if we already know this state, add up probabilities for every state. auto stateIt = states->find(newStateId); if (stateIt == states->end()) { - (*states)[newStateId] = update->getLikelihoodExpression()->getValueAsDouble(state); + (*states)[newStateId] = update.getLikelihoodExpression()->getValueAsDouble(state); this->numberOfTransitions++; } else { - (*states)[newStateId] += update->getLikelihoodExpression()->getValueAsDouble(state); + (*states)[newStateId] += update.getLikelihoodExpression()->getValueAsDouble(state); } } } @@ -336,21 +336,21 @@ ExplicitModelAdapter::~ExplicitModelAdapter() { // Create a copy of the current state, as we will free intermediate states... for (std::string action : this->program->getActions()) { StateType* state = new StateType(*this->allStates[stateID]); - std::unique_ptr>>> cmds = this->getActiveCommandsByAction(state, action); + std::unique_ptr>> cmds = this->getActiveCommandsByAction(state, action); // Start with current state std::unordered_map resultStates; resultStates[state] = 1.0; - for (std::list> module : *cmds) { + for (std::list module : *cmds) { if (resultStates.size() == 0) break; std::unordered_map newStates; // Iterate over all commands within this module. - for (std::shared_ptr command : module) { + for (storm::ir::Command command : module) { // Iterate over all updates of this command. - for (uint_fast64_t k = 0; k < command->getNumberOfUpdates(); ++k) { - std::shared_ptr const update = command->getUpdate(k); + for (uint_fast64_t k = 0; k < command.getNumberOfUpdates(); ++k) { + storm::ir::Update const update = command.getUpdate(k); // Iterate over all resultStates. for (auto it : resultStates) { @@ -360,9 +360,9 @@ ExplicitModelAdapter::~ExplicitModelAdapter() { // Take care of calculation of likelihood, combine identical states. auto s = newStates.find(newState); if (s == newStates.end()) { - newStates[newState] = it.second * update->getLikelihoodExpression()->getValueAsDouble(it.first); + newStates[newState] = it.second * update.getLikelihoodExpression()->getValueAsDouble(it.first); } else { - newStates[newState] += it.second * update->getLikelihoodExpression()->getValueAsDouble(it.first); + newStates[newState] += it.second * update.getLikelihoodExpression()->getValueAsDouble(it.first); } } } @@ -461,7 +461,7 @@ ExplicitModelAdapter::~ExplicitModelAdapter() { LOG4CPLUS_DEBUG(logger, "Building nondeterministic transition matrix with " << this->numberOfChoices << " choices and " << this->numberOfTransitions << " transitions now."); std::shared_ptr> result(new storm::storage::SparseMatrix(allStates.size(), this->numberOfChoices)); result->initialize(this->numberOfTransitions); - if (this->rewardModel->hasTransitionRewards()) { + if ((this->rewardModel != nullptr) && (this->rewardModel->hasTransitionRewards())) { this->transitionRewards = std::shared_ptr>(new storm::storage::SparseMatrix(allStates.size(), this->numberOfChoices)); this->transitionRewards->initialize(this->numberOfTransitions); } diff --git a/src/adapters/ExplicitModelAdapter.h b/src/adapters/ExplicitModelAdapter.h index 962bfa6d8..73a705e33 100644 --- a/src/adapters/ExplicitModelAdapter.h +++ b/src/adapters/ExplicitModelAdapter.h @@ -83,7 +83,7 @@ private: * @params update Update to be applied. * @return Resulting state. */ - StateType* applyUpdate(StateType const * const state, std::shared_ptr const update) const; + StateType* applyUpdate(StateType const * const state, storm::ir::Update const& update) const; /*! * Reads and combines variables from all program modules and stores them. @@ -107,7 +107,7 @@ private: * @param action Action label. * @return Active commands. */ - std::unique_ptr>>> getActiveCommandsByAction(StateType const * state, std::string& action); + std::unique_ptr>> getActiveCommandsByAction(StateType const * state, std::string& action); /*! * Generates all initial states and adds them to allStates. diff --git a/src/ir/Command.cpp b/src/ir/Command.cpp index 673a1bd14..05fe1084c 100644 --- a/src/ir/Command.cpp +++ b/src/ir/Command.cpp @@ -20,7 +20,7 @@ Command::Command() : actionName(), guardExpression(), updates() { } // Initializes all members according to the given values. -Command::Command(std::string actionName, std::shared_ptr guardExpression, std::vector> updates) +Command::Command(std::string actionName, std::shared_ptr guardExpression, std::vector updates) : actionName(actionName), guardExpression(guardExpression), updates(updates) { // Nothing to do here. } @@ -31,8 +31,8 @@ Command::Command(const Command& cmd, const std::map& r this->actionName = renaming.at(this->actionName); } this->updates.reserve(cmd.updates.size()); - for (std::shared_ptr u : cmd.updates) { - this->updates.emplace_back(new Update(*u, renaming, bools, ints)); + for (Update u : cmd.updates) { + this->updates.emplace_back(u, renaming, bools, ints); } } @@ -52,7 +52,7 @@ uint_fast64_t Command::getNumberOfUpdates() const { } // Return the requested update. -std::shared_ptr const& Command::getUpdate(uint_fast64_t index) const { +storm::ir::Update const& Command::getUpdate(uint_fast64_t index) const { return this->updates[index]; } @@ -61,7 +61,7 @@ std::string Command::toString() const { std::stringstream result; result << "[" << actionName << "] " << guardExpression->toString() << " -> "; for (uint_fast64_t i = 0; i < updates.size(); ++i) { - result << updates[i]->toString(); + result << updates[i].toString(); if (i < updates.size() - 1) { result << " + "; } diff --git a/src/ir/Command.h b/src/ir/Command.h index 4e3da7f65..b4049307c 100644 --- a/src/ir/Command.h +++ b/src/ir/Command.h @@ -34,7 +34,7 @@ public: * @param actionName the action name of the command. * @param guardExpression the expression that defines the guard of the command. */ - Command(std::string actionName, std::shared_ptr guardExpression, std::vector> updates); + Command(std::string actionName, std::shared_ptr guardExpression, std::vector updates); Command(const Command& cmd, const std::map& renaming, const std::map& bools, const std::map& ints); /*! @@ -59,7 +59,7 @@ public: * Retrieves a reference to the update with the given index. * @returns a reference to the update with the given index. */ - std::shared_ptr const& getUpdate(uint_fast64_t index) const; + storm::ir::Update const& getUpdate(uint_fast64_t index) const; /*! * Retrieves a string representation of this command. @@ -75,7 +75,7 @@ private: std::shared_ptr guardExpression; // The list of updates of the command. - std::vector> updates; + std::vector updates; }; } // namespace ir diff --git a/src/ir/Module.cpp b/src/ir/Module.cpp index 2d1482846..6bb8aa1aa 100644 --- a/src/ir/Module.cpp +++ b/src/ir/Module.cpp @@ -28,7 +28,7 @@ Module::Module(std::string moduleName, std::vector integerVariables, std::map booleanVariableToIndexMap, std::map integerVariableToIndexMap, - std::vector> commands) + std::vector commands) : moduleName(moduleName), booleanVariables(booleanVariables), integerVariables(integerVariables), booleanVariablesToIndexMap(booleanVariableToIndexMap), integerVariablesToIndexMap(integerVariableToIndexMap), commands(commands), actions(), actionsToCommandIndexMap() { @@ -68,9 +68,8 @@ Module::Module(const Module& module, const std::string& moduleName, const std::m } this->commands.reserve(module.commands.size()); - for (std::shared_ptr cmd: module.commands) { - Command* c = new Command(*cmd, renaming, this->booleanVariablesToIndexMap, this->integerVariablesToIndexMap); - this->commands.emplace_back(c); + for (Command cmd: module.commands) { + this->commands.emplace_back(cmd, renaming, this->booleanVariablesToIndexMap, this->integerVariablesToIndexMap); } this->collectActions(); } @@ -121,7 +120,7 @@ uint_fast64_t Module::getIntegerVariableIndex(std::string variableName) const { } // Return the requested command. -std::shared_ptr const Module::getCommand(uint_fast64_t index) const { +storm::ir::Command const Module::getCommand(uint_fast64_t index) const { return this->commands[index]; } @@ -136,7 +135,7 @@ std::string Module::toString() const { result << "\t" << variable.toString() << std::endl; } for (auto command : commands) { - result << "\t" << command->toString() << std::endl; + result << "\t" << command.toString() << std::endl; } result << "endmodule" << std::endl; return result.str(); @@ -159,7 +158,7 @@ std::shared_ptr> const Module::getCommandsByAction(std:: void Module::collectActions() { for (unsigned int id = 0; id < this->commands.size(); id++) { - std::string action = this->commands[id]->getActionName(); + std::string action = this->commands[id].getActionName(); if (action != "") { if (this->actionsToCommandIndexMap.count(action) == 0) { this->actionsToCommandIndexMap[action] = std::shared_ptr>(new std::set()); diff --git a/src/ir/Module.h b/src/ir/Module.h index 2a97a2ab3..8bc465b44 100644 --- a/src/ir/Module.h +++ b/src/ir/Module.h @@ -48,7 +48,7 @@ public: std::vector integerVariables, std::map booleanVariableToIndexMap, std::map integerVariableToIndexMap, - std::vector> commands); + std::vector commands); typedef uint_fast64_t (*addIntegerVariablePtr)(const std::string& name, const std::shared_ptr lower, const std::shared_ptr upper, const std::shared_ptr init); typedef uint_fast64_t (*addBooleanVariablePtr)(const std::string& name, const std::shared_ptr init); @@ -110,7 +110,7 @@ public: * Retrieves a reference to the command with the given index. * @returns a reference to the command with the given index. */ - std::shared_ptr const getCommand(uint_fast64_t index) const; + storm::ir::Command const getCommand(uint_fast64_t index) const; /*! * Retrieves a string representation of this variable. @@ -152,7 +152,7 @@ private: std::map integerVariablesToIndexMap; // The commands associated with the module. - std::vector> commands; + std::vector commands; // The set of actions present in this module. std::set actions; diff --git a/src/parser/PrismParser.cpp b/src/parser/PrismParser.cpp index 5b29913bc..7340d9d3a 100644 --- a/src/parser/PrismParser.cpp +++ b/src/parser/PrismParser.cpp @@ -69,7 +69,7 @@ namespace parser { this->state->moduleMap_.add(name, res); return res; } - std::shared_ptr PrismParser::PrismGrammar::createModule(const std::string name, std::vector& bools, std::vector& ints, std::map& boolids, std::map intids, std::vector> commands) { + std::shared_ptr PrismParser::PrismGrammar::createModule(const std::string name, std::vector& bools, std::vector& ints, std::map& boolids, std::map intids, std::vector commands) { this->state->moduleNames_.add(name, name); std::shared_ptr res = std::shared_ptr(new Module(name, bools, ints, boolids, intids, commands)); this->state->moduleMap_.add(name, res); @@ -85,11 +85,11 @@ namespace parser { void createRewardModel(std::string name, std::vector>& stateRewards, std::vector>& transitionRewards, std::map>& mapping) { mapping[name] = std::shared_ptr(new RewardModel(name, stateRewards, transitionRewards)); } - std::shared_ptr createUpdate(std::shared_ptr likelihood, std::map& bools, std::map ints) { - return std::shared_ptr(new Update(likelihood, bools, ints)); + Update createUpdate(std::shared_ptr likelihood, std::map& bools, std::map ints) { + return Update(likelihood, bools, ints); } - std::shared_ptr createCommand(std::string& label, std::shared_ptr guard, std::vector>& updates) { - return std::shared_ptr(new Command(label, guard, updates)); + Command createCommand(std::string& label, std::shared_ptr guard, std::vector& updates) { + return Command(label, guard, updates); } std::shared_ptr createProgram( Program::ModelType modelType, diff --git a/src/parser/PrismParser.h b/src/parser/PrismParser.h index b08777c65..0da0e0821 100644 --- a/src/parser/PrismParser.h +++ b/src/parser/PrismParser.h @@ -84,9 +84,9 @@ public: qi::rule&, std::map&), qi::locals>, Skipper> integerVariableDefinition; // Rules for command definitions. - qi::rule(), qi::locals, Skipper> commandDefinition; - qi::rule>(), Skipper> updateListDefinition; - qi::rule(), qi::locals, std::map>, Skipper> updateDefinition; + qi::rule, Skipper> commandDefinition; + qi::rule(), Skipper> updateListDefinition; + qi::rule, std::map>, Skipper> updateDefinition; qi::rule&, std::map&), Skipper> assignmentDefinitionList; qi::rule&, std::map&), Skipper> assignmentDefinition; @@ -129,7 +129,7 @@ public: void addBoolAssignment(const std::string& variable, std::shared_ptr value, std::map& mapping); void addIntAssignment(const std::string& variable, std::shared_ptr value, std::map& mapping); std::shared_ptr renameModule(const std::string& name, const std::string& oldname, std::map& mapping); - std::shared_ptr createModule(const std::string name, std::vector& bools, std::vector& ints, std::map& boolids, std::map intids, std::vector> commands); + std::shared_ptr createModule(const std::string name, std::vector& bools, std::vector& ints, std::map& boolids, std::map intids, std::vector commands); };