Storm
A Modern Probabilistic Model Checker
Loading...
Searching...
No Matches
RebuildWithoutUnreachableAction.cpp
Go to the documentation of this file.
3
5
6namespace storm {
7namespace jani {
8namespace elimination_actions {
10
12 return "RebuildWithoutUnreachableAction";
13}
14
16 STORM_LOG_TRACE("Rebuilding model without unreachable locations");
17 for (const auto &oldAutomaton : session.getModel().getAutomata()) {
18 Automaton newAutomaton(oldAutomaton.getName(), oldAutomaton.getLocationExpressionVariable());
19
20 std::map<Variable const *, std::reference_wrapper<Variable const>> variableRemapping;
21
22 for (auto const &localVariable : oldAutomaton.getVariables()) {
23 newAutomaton.addVariable(localVariable);
24 std::reference_wrapper<Variable const> ref_w = std::cref(newAutomaton.getVariables().getVariable(localVariable.getName()));
25 variableRemapping.insert(std::pair<Variable const *, std::reference_wrapper<Variable const>>(&localVariable, ref_w));
26 }
27
28 newAutomaton.setInitialStatesRestriction(oldAutomaton.getInitialStatesRestriction());
29
30 std::unordered_set<const Edge *> satisfiableEdges;
31
32 for (auto &oldEdge : oldAutomaton.getEdges()) {
33 if (!oldEdge.getGuard().containsVariables() && !oldEdge.getGuard().evaluateAsBool())
34 continue;
35 satisfiableEdges.emplace(&oldEdge);
36 }
37 STORM_LOG_TRACE("\t" + std::to_string(satisfiableEdges.size()) + " of " + std::to_string(oldAutomaton.getEdges().size()) + " edges are satisfiable.");
38
39 std::unordered_set<uint64_t> reachableLocs;
40 std::unordered_set<uint64_t> reachableLocsOpen;
41
42 for (auto initialLocIndex : oldAutomaton.getInitialLocationIndices()) {
43 reachableLocs.emplace(initialLocIndex);
44 reachableLocsOpen.emplace(initialLocIndex);
45 }
46
47 while (!reachableLocsOpen.empty()) {
48 uint64_t current = *reachableLocsOpen.begin();
49 reachableLocsOpen.erase(current);
50
51 for (auto &edge : oldAutomaton.getEdgesFromLocation(current)) {
52 if (satisfiableEdges.count(&edge) == 1) {
53 for (auto const &dest : edge.getDestinations()) {
54 uint64_t target = dest.getLocationIndex();
55 if (reachableLocs.count(target) == 0) {
56 reachableLocs.emplace(target);
57 reachableLocsOpen.emplace(target);
58 }
59 }
60 }
61 }
62 }
63 STORM_LOG_TRACE("\t" + std::to_string(reachableLocs.size()) + " of " + std::to_string(oldAutomaton.getLocations().size()) +
64 " locations are reachable.");
65
66 // Because the session keeps track of which variables might satisfy the property, we need to update
67 // those values (because we're changing the indices of locations). As a first step, we store which
68 // (old) locations potentially satisfy the property.
69 std::set<uint64_t> oldIsPartOfProp;
70 for (auto const &oldLoc : oldAutomaton.getLocations()) {
71 uint64_t oldLocationIndex = oldAutomaton.getLocationIndex(oldLoc.getName());
72 if (session.isPartOfProp(oldAutomaton.getName(), oldLocationIndex)) {
73 oldIsPartOfProp.insert(oldLocationIndex);
74 }
75 }
76
77 std::map<uint64_t, uint64_t> oldToNewLocationIndices;
78
79 for (auto const &oldLoc : oldAutomaton.getLocations()) {
80 uint64_t oldLocationIndex = oldAutomaton.getLocationIndex(oldLoc.getName());
81 if (reachableLocs.count(oldLocationIndex) == 0)
82 continue;
83
84 Location newLoc(oldLoc.getName(), oldLoc.getAssignments());
85 newAutomaton.addLocation(newLoc);
86
87 uint64_t newLocationIndex = newAutomaton.getLocationIndex(newLoc.getName());
88 oldToNewLocationIndices.insert(std::pair<uint64_t, uint64_t>(oldLocationIndex, newLocationIndex));
89 }
90
91 for (auto initialLocIndex : oldAutomaton.getInitialLocationIndices()) {
92 newAutomaton.addInitialLocation(oldToNewLocationIndices[initialLocIndex]);
93 }
94
95 for (auto &oldEdge : oldAutomaton.getEdges()) {
96 uint64_t oldSource = oldEdge.getSourceLocationIndex();
97 if (reachableLocs.count(oldSource) == 0)
98 continue;
99
100 if (satisfiableEdges.count(&oldEdge) == 0)
101 continue;
102
103 oldEdge.getDestination(0).getOrderedAssignments().clone();
104
105 std::shared_ptr<storm::jani::TemplateEdge> templateEdge = std::make_shared<storm::jani::TemplateEdge>(oldEdge.getGuard());
106 oldEdge.getDestination(0).getOrderedAssignments().clone();
107
108 STORM_LOG_THROW(oldEdge.getAssignments().empty(), storm::exceptions::NotImplementedException, "Support for oldEdge-assignments is not implemented");
109
110 std::vector<std::pair<uint64_t, storm::expressions::Expression>> destinationLocationsAndProbabilities;
111 for (auto const &destination : oldEdge.getDestinations()) {
112 uint64_t newTarget = oldToNewLocationIndices[destination.getLocationIndex()];
113
114 OrderedAssignments oa(destination.getOrderedAssignments().clone());
116 templateEdge->addDestination(ted);
117 destinationLocationsAndProbabilities.emplace_back(newTarget, destination.getProbability());
118 }
119
120 uint64_t newSource = oldToNewLocationIndices[oldEdge.getSourceLocationIndex()];
121 newAutomaton.addEdge(storm::jani::Edge(newSource, oldEdge.getActionIndex(),
122 oldEdge.hasRate() ? boost::optional<storm::expressions::Expression>(oldEdge.getRate()) : boost::none,
123 templateEdge, destinationLocationsAndProbabilities));
124 newAutomaton.registerTemplateEdge(templateEdge);
125 }
126
127 newAutomaton.changeAssignmentVariables(variableRemapping);
128
129 // We now update which locations might satisfy the property (based on which old locations did and
130 // the old-to-new map.
131 session.clearIsPartOfProp(oldAutomaton.getName());
132 for (uint64_t oldLocationIndex : oldIsPartOfProp) {
133 session.setPartOfProp(oldAutomaton.getName(), oldToNewLocationIndices[oldLocationIndex], true);
134 }
135
136 auto &automatonInfo = session.getAutomatonInfo(oldAutomaton.getName());
137 if (automatonInfo.hasSink) {
138 if (reachableLocs.count(automatonInfo.sinkIndex) == 0) {
139 automatonInfo.hasSink = false;
140 STORM_LOG_TRACE("\tThe sink was eliminated.");
141 } else {
142 automatonInfo.sinkIndex = oldToNewLocationIndices[automatonInfo.sinkIndex];
143 }
144 }
145 STORM_LOG_TRACE("\tNew automaton has " + std::to_string(newAutomaton.getEdges().size()) + " edges.");
146
147 session.getModel().replaceAutomaton(session.getModel().getAutomatonIndex(oldAutomaton.getName()), newAutomaton);
148 }
149}
150} // namespace elimination_actions
151} // namespace jani
152} // namespace storm
VariableSet & getVariables()
Retrieves the variables of this automaton.
Definition Automaton.cpp:59
void addEdge(Edge const &edge)
Adds an edge to the automaton.
void registerTemplateEdge(std::shared_ptr< TemplateEdge > const &)
Adds the template edge to the list of edges.
void setInitialStatesRestriction(storm::expressions::Expression const &initialStatesRestriction)
Sets the expression restricting the legal initial values of the automaton's variables.
Variable const & addVariable(Variable const &variable)
Adds the given variable to this automaton.
Definition Automaton.cpp:51
void addInitialLocation(std::string const &name)
Adds the location with the given name to the initial locations.
uint64_t addLocation(Location const &location)
Adds the given location to the automaton.
void changeAssignmentVariables(std::map< Variable const *, std::reference_wrapper< Variable const > > const &remapping)
Changes all variables in assignments based on the given mapping.
std::vector< Edge > & getEdges()
Retrieves the edges of the automaton.
uint64_t getLocationIndex(std::string const &name) const
void setPartOfProp(const std::string &automatonName, const std::string &locationName, bool isPartOfProp)
bool isPartOfProp(const std::string &automatonName, std::string const &locationName)
AutomatonInfo & getAutomatonInfo(const std::string &name)
void clearIsPartOfProp(const std::string &automatonName)
Jani Location:
Definition Location.h:15
std::string const & getName() const
Retrieves the name of the location.
Definition Location.cpp:19
void replaceAutomaton(uint64_t index, Automaton const &newAutomaton)
Replaces the automaton at index with a new automaton.
Definition Model.cpp:884
std::vector< Automaton > & getAutomata()
Retrieves the automata of the model.
Definition Model.cpp:872
uint64_t getAutomatonIndex(std::string const &name) const
Retrieves the index of the given automaton.
Definition Model.cpp:908
Variable const & getVariable(std::string const &name) const
Retrieves the variable with the given name.
#define STORM_LOG_TRACE(message)
Definition logging.h:17
#define STORM_LOG_THROW(cond, exception, message)
Definition macros.h:30
LabParser.cpp.
Definition cli.cpp:18