Storm
A Modern Probabilistic Model Checker
Loading...
Searching...
No Matches
spotProduct.cpp
Go to the documentation of this file.
2
3#include <deque>
4
10
12
14
15typedef std::pair<unsigned, unsigned> product_state;
16
18 size_t operator()(product_state s) const noexcept {
19#ifdef STORM_HAVE_SPOT
20 return spot::wang32_hash(s.first ^ spot::wang32_hash(s.second));
21#else
22 STORM_LOG_THROW(false, storm::exceptions::NotSupportedException, "Storm is compiled without Spot support.");
23#endif
24 }
25};
26
28typedef std::vector<std::pair<unsigned, unsigned>> product_states;
29
30std::shared_ptr<storm::automata::DeterministicAutomaton> ltl2daSpotProduct(storm::logic::MultiObjectiveFormula const& formula,
32 std::vector<uint>& acceptanceConditions) {
33#ifdef STORM_HAVE_SPOT
34 bool first = true;
35 spot::twa_graph_ptr productAutomaton;
36 spot::bdd_dict_ptr dict = spot::make_bdd_dict();
37 uint countAccept = 0;
38 // iterate over all subformulae
39 for (const std::shared_ptr<const storm::logic::Formula>& subFormula : formula.getSubformulas()) {
40 // get the formula in the right format
41 STORM_LOG_ASSERT(subFormula->isProbabilityOperatorFormula(), "subformula " << *subFormula << " has unexpected type.");
42 auto const& pathFormula = subFormula->asProbabilityOperatorFormula().getSubformula().asPathFormula();
43
44 // get map of state-expressions to propositions
45 std::shared_ptr<storm::logic::Formula> ltlFormula1 = storm::logic::ExtractMaximalStateFormulasVisitor::extract(pathFormula, extracted);
46
47 // parse the formula in spot-format
48 std::string prefixLtl = ltlFormula1->toPrefixString();
49 spot::parsed_formula spotPrefixLtl = spot::parse_prefix_ltl(prefixLtl);
50 if (!spotPrefixLtl.errors.empty()) {
51 std::ostringstream errorMsg;
52 spotPrefixLtl.format_errors(errorMsg);
53 STORM_LOG_THROW(false, storm::exceptions::ExpressionEvaluationException, "Spot could not parse formula: " << prefixLtl << ": " << errorMsg.str());
54 }
55 spot::formula spotFormula = spotPrefixLtl.f;
56
57 // Request a deterministic, complete automaton with state-based acceptance with parity-acceptance condition (should result in Streett)
58 spot::translator trans = spot::translator(dict);
59 trans.set_type(spot::postprocessor::Parity);
60 trans.set_pref(spot::postprocessor::Deterministic | spot::postprocessor::SBAcc | spot::postprocessor::Complete | spot::postprocessor::Colored);
61 // aut contains the Spot-automaton
62 auto aut = trans.run(spotFormula);
63
64 acceptanceConditions.push_back(countAccept);
65 countAccept += aut->get_acceptance().top_conjuncts().size();
66 if (first) {
67 // the first automaton does not need to be merged with the product automaton
68 productAutomaton = aut;
69 first = false;
70 continue;
71 } else {
72 // create a product of the the new automaton and the already existing product automaton
73 spot::const_twa_graph_ptr left = aut;
74 spot::const_twa_graph_ptr right = productAutomaton;
75 unsigned left_state = left->get_init_state_number();
76 unsigned right_state = right->get_init_state_number();
77
78 auto res = spot::make_twa_graph(left->get_dict());
79 res->copy_ap_of(left);
80 res->copy_ap_of(right);
81
82 auto left_num = left->num_sets();
83 auto& left_acc = left->get_acceptance();
84 auto right_acc = right->get_acceptance() << left_num;
85 right_acc &= left_acc;
86
87 res->set_acceptance(left_num + right->num_sets(), right_acc);
88
89 auto merge_acc = [&](spot::acc_cond::mark_t ml, spot::acc_cond::mark_t mr) { return ml | (mr << left_num); };
90 std::unordered_map<product_state, unsigned, product_state_hash> s2n;
91 std::deque<std::pair<product_state, unsigned>> todo;
92
93 auto v = new product_states;
94 res->set_named_prop("product-states", v);
95
96 auto new_state = [&](unsigned left_state, unsigned right_state) -> unsigned {
97 product_state x(left_state, right_state);
98 auto p = s2n.emplace(x, 0);
99 if (p.second) // This is a new state
100 {
101 p.first->second = res->new_state();
102 todo.emplace_back(x, p.first->second);
103 assert(p.first->second == v->size());
104 v->emplace_back(x);
105 }
106 return p.first->second;
107 };
108
109 res->set_init_state(new_state(left_state, right_state));
110 while (!todo.empty()) {
111 auto top = todo.front();
112 todo.pop_front();
113 for (auto& l : left->out(top.first.first))
114 for (auto& r : right->out(top.first.second)) {
115 auto cond = l.cond & r.cond;
116 if (cond == bddfalse)
117 continue;
118 auto dst = new_state(l.dst, r.dst);
119 res->new_edge(top.second, dst, cond, merge_acc(l.acc, r.acc));
120 // If right is deterministic, we can abort immediately!
121 }
122 }
123
124 if (res->acc().is_f()) {
125 assert(res->num_edges() == 0);
126 res->prop_universal(true);
127 res->prop_complete(false);
128 res->prop_stutter_invariant(true);
129 res->prop_terminal(true);
130 res->prop_state_acc(true);
131 } else {
132 // The product of two non-deterministic automata could be
133 // deterministic. Likewise for non-complete automata.
134 if (left->prop_universal() && right->prop_universal())
135 res->prop_universal(true);
136 if (left->prop_complete() && right->prop_complete())
137 res->prop_complete(true);
138 if (left->prop_stutter_invariant() && right->prop_stutter_invariant())
139 res->prop_stutter_invariant(true);
140 if (left->prop_inherently_weak() && right->prop_inherently_weak())
141 res->prop_inherently_weak(true);
142 if (left->prop_weak() && right->prop_weak())
143 res->prop_weak(true);
144 if (left->prop_terminal() && right->prop_terminal())
145 res->prop_terminal(true);
146 res->prop_state_acc(left->prop_state_acc() && right->prop_state_acc());
147 }
148 productAutomaton = res;
149 }
150 }
151 acceptanceConditions.push_back(countAccept);
152
153 if (!(productAutomaton->get_acceptance().is_cnf())) {
154 // Transform the acceptance condition in disjunctive normal form and merge all the Fin-sets of each clause
155 productAutomaton = to_generalized_streett(productAutomaton, true);
156 }
157 std::stringstream autStream;
158 // Print reachable states in HOA format, implicit edges (i), state-based acceptance (s)
159 spot::print_hoa(autStream, productAutomaton, "is");
160
161 // parse the automaton into storm-format
163
164 return da;
165#else
166 STORM_LOG_THROW(false, storm::exceptions::NotSupportedException, "Storm is compiled without Spot support.");
167 (void)formula;
168 (void)extracted;
169 (void)acceptanceConditions;
170#endif
171}
172
173} // namespace storm::modelchecker::helper::lexicographic::spothelper
std::shared_ptr< DeterministicAutomaton > ptr
static DeterministicAutomaton::ptr parse(std::istream &in)
static std::shared_ptr< Formula > extract(PathFormula const &f, ApToFormulaMap &extractedFormulas)
Finds state subformulae in f and replaces them by atomic propositions.
std::map< std::string, std::shared_ptr< Formula const > > ApToFormulaMap
std::vector< std::shared_ptr< Formula const > > const & getSubformulas() const
#define STORM_LOG_ASSERT(cond, message)
Definition macros.h:11
#define STORM_LOG_THROW(cond, exception, message)
Definition macros.h:30
std::vector< std::pair< unsigned, unsigned > > product_states
std::shared_ptr< storm::automata::DeterministicAutomaton > ltl2daSpotProduct(storm::logic::MultiObjectiveFormula const &formula, storm::logic::ExtractMaximalStateFormulasVisitor::ApToFormulaMap &extracted, std::vector< uint > &acceptanceConditions)
Function that creates a determinitistic automaton with Streett-acceptance condition.