Browse Source

Fixed another nullptr, removed shared_ptr for Update and Command objects.

main
gereon 12 years ago
parent
commit
1878962dea
  1. 48
      src/adapters/ExplicitModelAdapter.cpp
  2. 4
      src/adapters/ExplicitModelAdapter.h
  3. 10
      src/ir/Command.cpp
  4. 6
      src/ir/Command.h
  5. 13
      src/ir/Module.cpp
  6. 6
      src/ir/Module.h
  7. 10
      src/parser/PrismParser.cpp
  8. 8
      src/parser/PrismParser.h

48
src/adapters/ExplicitModelAdapter.cpp

@ -160,20 +160,20 @@ ExplicitModelAdapter::~ExplicitModelAdapter() {
* @param action Action label.
* @return Active commands.
*/
std::unique_ptr<std::list<std::list<std::shared_ptr<storm::ir::Command>>>> ExplicitModelAdapter::getActiveCommandsByAction(StateType const * state, std::string& action) {
std::unique_ptr<std::list<std::list<std::shared_ptr<storm::ir::Command>>>> res = std::unique_ptr<std::list<std::list<std::shared_ptr<storm::ir::Command>>>>(new std::list<std::list<std::shared_ptr<storm::ir::Command>>>());
std::unique_ptr<std::list<std::list<storm::ir::Command>>> ExplicitModelAdapter::getActiveCommandsByAction(StateType const * state, std::string& action) {
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);
std::shared_ptr<std::set<uint_fast64_t>> ids = module->getCommandsByAction(action);
std::list<std::shared_ptr<storm::ir::Command>> commands;
std::list<storm::ir::Command> commands;
// Look up commands by their id. Add, if guard holds.
for (uint_fast64_t id : *ids) {
std::shared_ptr<storm::ir::Command> 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<std::shared_ptr<storm::ir::Command>>& a, const std::list<std::shared_ptr<storm::ir::Command>>& b){ return a.size() < b.size(); });
res->sort([](const std::list<storm::ir::Command>& a, const std::list<storm::ir::Command>& 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<storm::ir::Update> 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<storm::ir::Module> const module = program->getModule(i);
// Iterate over all commands.
for (uint_fast64_t j = 0; j < module->getNumberOfCommands(); ++j) {
std::shared_ptr<storm::ir::Command> 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<uint_fast64_t, double>* 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<storm::ir::Update> 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<std::list<std::list<std::shared_ptr<storm::ir::Command>>>> cmds = this->getActiveCommandsByAction(state, action);
std::unique_ptr<std::list<std::list<storm::ir::Command>>> cmds = this->getActiveCommandsByAction(state, action);
// Start with current state
std::unordered_map<StateType*, double, StateHash, StateCompare> resultStates;
resultStates[state] = 1.0;
for (std::list<std::shared_ptr<storm::ir::Command>> module : *cmds) {
for (std::list<storm::ir::Command> module : *cmds) {
if (resultStates.size() == 0) break;
std::unordered_map<StateType*, double, StateHash, StateCompare> newStates;
// Iterate over all commands within this module.
for (std::shared_ptr<storm::ir::Command> 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<storm::ir::Update> 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<storm::storage::SparseMatrix<double>> result(new storm::storage::SparseMatrix<double>(allStates.size(), this->numberOfChoices));
result->initialize(this->numberOfTransitions);
if (this->rewardModel->hasTransitionRewards()) {
if ((this->rewardModel != nullptr) && (this->rewardModel->hasTransitionRewards())) {
this->transitionRewards = std::shared_ptr<storm::storage::SparseMatrix<double>>(new storm::storage::SparseMatrix<double>(allStates.size(), this->numberOfChoices));
this->transitionRewards->initialize(this->numberOfTransitions);
}

4
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<storm::ir::Update> 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<std::list<std::list<std::shared_ptr<storm::ir::Command>>>> getActiveCommandsByAction(StateType const * state, std::string& action);
std::unique_ptr<std::list<std::list<storm::ir::Command>>> getActiveCommandsByAction(StateType const * state, std::string& action);
/*!
* Generates all initial states and adds them to allStates.

10
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<storm::ir::expressions::BaseExpression> guardExpression, std::vector<std::shared_ptr<storm::ir::Update>> updates)
Command::Command(std::string actionName, std::shared_ptr<storm::ir::expressions::BaseExpression> guardExpression, std::vector<storm::ir::Update> updates)
: actionName(actionName), guardExpression(guardExpression), updates(updates) {
// Nothing to do here.
}
@ -31,8 +31,8 @@ Command::Command(const Command& cmd, const std::map<std::string, std::string>& r
this->actionName = renaming.at(this->actionName);
}
this->updates.reserve(cmd.updates.size());
for (std::shared_ptr<Update> 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<storm::ir::Update> 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 << " + ";
}

6
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<storm::ir::expressions::BaseExpression> guardExpression, std::vector<std::shared_ptr<storm::ir::Update>> updates);
Command(std::string actionName, std::shared_ptr<storm::ir::expressions::BaseExpression> guardExpression, std::vector<storm::ir::Update> updates);
Command(const Command& cmd, const std::map<std::string, std::string>& renaming, const std::map<std::string,uint_fast64_t>& bools, const std::map<std::string,uint_fast64_t>& 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<storm::ir::Update> 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<storm::ir::expressions::BaseExpression> guardExpression;
// The list of updates of the command.
std::vector<std::shared_ptr<storm::ir::Update>> updates;
std::vector<storm::ir::Update> updates;
};
} // namespace ir

13
src/ir/Module.cpp

@ -28,7 +28,7 @@ Module::Module(std::string moduleName,
std::vector<storm::ir::IntegerVariable> integerVariables,
std::map<std::string, uint_fast64_t> booleanVariableToIndexMap,
std::map<std::string, uint_fast64_t> integerVariableToIndexMap,
std::vector<std::shared_ptr<storm::ir::Command>> commands)
std::vector<storm::ir::Command> 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<Command> 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<storm::ir::Command> 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<std::set<uint_fast64_t>> 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<std::set<uint_fast64_t>>(new std::set<uint_fast64_t>());

6
src/ir/Module.h

@ -48,7 +48,7 @@ public:
std::vector<storm::ir::IntegerVariable> integerVariables,
std::map<std::string, uint_fast64_t> booleanVariableToIndexMap,
std::map<std::string, uint_fast64_t> integerVariableToIndexMap,
std::vector<std::shared_ptr<storm::ir::Command>> commands);
std::vector<storm::ir::Command> commands);
typedef uint_fast64_t (*addIntegerVariablePtr)(const std::string& name, const std::shared_ptr<storm::ir::expressions::BaseExpression> lower, const std::shared_ptr<storm::ir::expressions::BaseExpression> upper, const std::shared_ptr<storm::ir::expressions::BaseExpression> init);
typedef uint_fast64_t (*addBooleanVariablePtr)(const std::string& name, const std::shared_ptr<storm::ir::expressions::BaseExpression> 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<storm::ir::Command> 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<std::string, uint_fast64_t> integerVariablesToIndexMap;
// The commands associated with the module.
std::vector<std::shared_ptr<storm::ir::Command>> commands;
std::vector<storm::ir::Command> commands;
// The set of actions present in this module.
std::set<std::string> actions;

10
src/parser/PrismParser.cpp

@ -69,7 +69,7 @@ namespace parser {
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<std::shared_ptr<storm::ir::Command>> commands) {
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) {
this->state->moduleNames_.add(name, name);
std::shared_ptr<Module> res = std::shared_ptr<Module>(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<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));
}
std::shared_ptr<Update> createUpdate(std::shared_ptr<BaseExpression> likelihood, std::map<std::string, Assignment>& bools, std::map<std::string, Assignment> ints) {
return std::shared_ptr<Update>(new Update(likelihood, bools, 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);
}
std::shared_ptr<Command> createCommand(std::string& label, std::shared_ptr<BaseExpression> guard, std::vector<std::shared_ptr<Update>>& updates) {
return std::shared_ptr<Command>(new Command(label, guard, updates));
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::ModelType modelType,

8
src/parser/PrismParser.h

@ -84,9 +84,9 @@ public:
qi::rule<Iterator, qi::unused_type(std::vector<IntegerVariable>&, std::map<std::string, uint_fast64_t>&), qi::locals<uint_fast64_t, std::shared_ptr<BaseExpression>>, Skipper> integerVariableDefinition;
// Rules for command definitions.
qi::rule<Iterator, std::shared_ptr<Command>(), qi::locals<std::string>, Skipper> commandDefinition;
qi::rule<Iterator, std::vector<std::shared_ptr<Update>>(), Skipper> updateListDefinition;
qi::rule<Iterator, std::shared_ptr<Update>(), qi::locals<std::map<std::string, Assignment>, std::map<std::string, Assignment>>, Skipper> updateDefinition;
qi::rule<Iterator, Command(), qi::locals<std::string>, Skipper> commandDefinition;
qi::rule<Iterator, std::vector<Update>(), Skipper> updateListDefinition;
qi::rule<Iterator, Update(), qi::locals<std::map<std::string, Assignment>, std::map<std::string, Assignment>>, Skipper> updateDefinition;
qi::rule<Iterator, qi::unused_type(std::map<std::string, Assignment>&, std::map<std::string, Assignment>&), Skipper> assignmentDefinitionList;
qi::rule<Iterator, qi::unused_type(std::map<std::string, Assignment>&, std::map<std::string, Assignment>&), Skipper> assignmentDefinition;
@ -129,7 +129,7 @@ public:
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<std::shared_ptr<storm::ir::Command>> commands);
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);
};

Loading…
Cancel
Save