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