Storm 2.0.0.3
A Modern Probabilistic Model Checker
Loading...
Searching...
No Matches
FormulaParserGrammar.cpp
Go to the documentation of this file.
2
3#include <memory>
4#include <optional>
5
7
8namespace storm {
9namespace parser {
10
11FormulaParserGrammar::FormulaParserGrammar(std::shared_ptr<storm::expressions::ExpressionManager const> const& manager)
12 : FormulaParserGrammar::base_type(start), constManager(manager), manager(nullptr), expressionParser(*manager, keywords_, true, true), propertyCount(0) {
13 initialize();
14}
15
16FormulaParserGrammar::FormulaParserGrammar(std::shared_ptr<storm::expressions::ExpressionManager> const& manager)
17 : FormulaParserGrammar::base_type(start), constManager(manager), manager(manager), expressionParser(*manager, keywords_, true, true), propertyCount(0) {
18 initialize();
19}
20
21qi::symbols<char, storm::expressions::Expression> const& FormulaParserGrammar::getIdentifiers() const {
22 return identifiers_;
23}
24
25void FormulaParserGrammar::initialize() {
26 // Register all variables so we can parse them in the expressions.
27 for (auto variableTypePair : *constManager) {
28 addIdentifierExpression(variableTypePair.first.getName(), variableTypePair.first);
29 }
30 // Set the identifier mapping to actually generate expressions.
31 expressionParser.setIdentifierMapping(&identifiers_);
32
33 keywords_.name("keyword");
34 nonStandardKeywords_.name("non-standard Storm-specific keyword");
35 relationalOperator_.name("relational operator");
36 optimalityOperator_.name("optimality operator");
37 operatorKeyword_.name("Operator keyword");
38 filterType_.name("filter type");
39
40 // Auxiliary helpers
41 isPathFormula = qi::eps(qi::_r1 == FormulaKind::Path);
42 noAmbiguousNonAssociativeOperator = !(
43 binaryPathOperator_[qi::_pass = phoenix::bind(&FormulaParserGrammar::raiseAmbiguousNonAssociativeOperatorError, phoenix::ref(*this), qi::_r1, qi::_1)]);
44 noAmbiguousNonAssociativeOperator.name("no ambiguous non-associative operator");
45 identifier %= qi::as_string[qi::raw[qi::lexeme[((qi::alpha | qi::char_('_') | qi::char_('.')) >> *(qi::alnum | qi::char_('_')))]]];
46 identifier.name("identifier");
47 label %= qi::as_string[qi::raw[qi::lexeme[((qi::alpha | qi::char_('_')) >> *(qi::alnum | qi::char_('_')))]]];
48 label.name("label");
49 quotedString %= qi::as_string[qi::lexeme[qi::omit[qi::char_('"')] > qi::raw[*(!qi::char_('"') >> qi::char_)] > qi::omit[qi::lit('"')]]];
50 quotedString.name("quoted string");
51
52 // PCTL-like Operator Formulas
53 operatorInformation =
54 (-optimalityOperator_)[qi::_a = qi::_1] >>
55 ((qi::lit("=") >
56 qi::lit("?"))[qi::_val = phoenix::bind(&FormulaParserGrammar::createOperatorInformation, phoenix::ref(*this), qi::_a, boost::none, boost::none)] |
57 (relationalOperator_ >
58 expressionParser)[qi::_val = phoenix::bind(&FormulaParserGrammar::createOperatorInformation, phoenix::ref(*this), qi::_a, qi::_1, qi::_2)]);
59 operatorInformation.name("operator information");
60 operatorSubFormula =
61 (((qi::eps(qi::_r1 == storm::logic::FormulaContext::Probability) > formula(FormulaKind::Path, qi::_r1)) |
62 (qi::eps(qi::_r1 == storm::logic::FormulaContext::Reward) >
63 (longRunAverageRewardFormula | eventuallyFormula(qi::_r1) | discountedCumulativeRewardFormula | discountedTotalRewardFormula |
64 cumulativeRewardFormula | instantaneousRewardFormula | totalRewardFormula)) |
65 (qi::eps(qi::_r1 == storm::logic::FormulaContext::Time) > eventuallyFormula(qi::_r1)) |
66 (qi::eps(qi::_r1 == storm::logic::FormulaContext::LongRunAverage) > formula(FormulaKind::State, storm::logic::FormulaContext::LongRunAverage))) >>
67 -(qi::lit("||") >
68 formula(FormulaKind::Path, storm::logic::FormulaContext::Probability)))[qi::_val = phoenix::bind(&FormulaParserGrammar::createConditionalFormula,
69 phoenix::ref(*this), qi::_1, qi::_2, qi::_r1)];
70 operatorSubFormula.name("operator subformula");
71 rewardModelName = qi::eps(qi::_r1 == storm::logic::FormulaContext::Reward) >> (qi::lit("{\"") > label > qi::lit("\"}"));
72 rewardModelName.name("reward model name");
73 operatorFormula =
74 (operatorKeyword_[qi::_a = qi::_1] > -rewardModelName(qi::_a) > operatorInformation > qi::lit("[") > operatorSubFormula(qi::_a) >
75 qi::lit("]"))[qi::_val = phoenix::bind(&FormulaParserGrammar::createOperatorFormula, phoenix::ref(*this), qi::_a, qi::_2, qi::_3, qi::_4)];
76 operatorFormula.name("operator formula");
77
78 // Atomic propositions
79 labelFormula =
80 (qi::lit("\"") >> label >> qi::lit("\""))[qi::_val = phoenix::bind(&FormulaParserGrammar::createAtomicLabelFormula, phoenix::ref(*this), qi::_1)];
81 labelFormula.name("label formula");
82 expressionFormula = expressionParser[qi::_val = phoenix::bind(&FormulaParserGrammar::createAtomicExpressionFormula, phoenix::ref(*this), qi::_1)];
83 expressionFormula.name("expression formula");
84
85 basicPropositionalFormula =
86 expressionFormula // try as expression first, e.g. '(false)=false' is an atomic expression formula. Also should be checked before operator formulas and
87 // others. Otherwise, e.g. variable "Random" would be parsed as reward operator 'R' (followed by andom)
88 | (qi::lit("(") >>
89 (formula(qi::_r1, qi::_r2) > qi::lit(")"))) // If we're starting with '(' but this is not an expression, we must have a formula inside the brackets
90 | labelFormula | negationPropositionalFormula(qi::_r1, qi::_r2) | operatorFormula |
91 (isPathFormula(qi::_r1) >> prefixOperatorPathFormula(qi::_r2)) // Needed for e.g. F "a" & X "a" = F ("a" & (X "a"))
92 | multiLexOperatorFormula // Has to come after prefixOperatorPathFormula to avoid confusion with multiBoundedPathFormula
93 | multiOperatorFormula // Has to come after multilex to avoid failing to parse multilex
94 | quantileFormula | gameFormula;
95
96 // Propositional Logic operators
97 // To correctly parse the operator precedences (! binds stronger than & binds stronger than |), we run through different "precedence levels" starting with
98 // the strongest binding operator.
99 negationPropositionalFormula =
100 (qi::lit("!") >
101 basicPropositionalFormula(qi::_r1, qi::_r2))[qi::_val = phoenix::bind(&FormulaParserGrammar::createUnaryBooleanStateOrPathFormula, phoenix::ref(*this),
103 basicPropositionalFormula.name("basic propositional formula");
104 andLevelPropositionalFormula =
105 basicPropositionalFormula(qi::_r1, qi::_r2)[qi::_val = qi::_1] >>
106 *(qi::lit("&") > basicPropositionalFormula(
107 qi::_r1, qi::_r2)[qi::_val = phoenix::bind(&FormulaParserGrammar::createBinaryBooleanStateOrPathFormula, phoenix::ref(*this),
108 qi::_val, qi::_1, storm::logic::BinaryBooleanStateFormula::OperatorType::And)]);
109 andLevelPropositionalFormula.name("and precedence level propositional formula");
110 orLevelPropositionalFormula =
111 andLevelPropositionalFormula(qi::_r1, qi::_r2)[qi::_val = qi::_1] >>
112 *((!qi::lit("||") >> qi::lit("|")) // Make sure to not confuse with conditional operator "||"
113 > andLevelPropositionalFormula(
114 qi::_r1, qi::_r2)[qi::_val = phoenix::bind(&FormulaParserGrammar::createBinaryBooleanStateOrPathFormula, phoenix::ref(*this), qi::_val, qi::_1,
115 storm::logic::BinaryBooleanStateFormula::OperatorType::Or)]);
116 orLevelPropositionalFormula.name("or precedence level propositional formula");
117 propositionalFormula = orLevelPropositionalFormula(qi::_r1, qi::_r2);
118
119 // Path operators
120 // Again need to parse precedences correctly. Propositional formulae bind stronger than temporal operators.
121 basicPathFormula = propositionalFormula(FormulaKind::Path, qi::_r1) // Bracketed case is handled here as well
122 | prefixOperatorPathFormula(
123 qi::_r1); // Needs to be checked *after* atomic expression formulas. Otherwise e.g. variable Fail would be parsed as "F (ail)"
124 prefixOperatorPathFormula =
125 eventuallyFormula(qi::_r1) | nextFormula(qi::_r1) | globallyFormula(qi::_r1) | hoaPathFormula(qi::_r1) | multiBoundedPathFormula(qi::_r1);
126 basicPathFormula.name("basic path formula");
127 timeBoundReference =
128 (-qi::lit("rew") >>
129 rewardModelName(storm::logic::FormulaContext::Reward))[qi::_val = phoenix::bind(&FormulaParserGrammar::createTimeBoundReference, phoenix::ref(*this),
131 (qi::lit("rew") >>
132 -rewardModelName(storm::logic::FormulaContext::Reward))[qi::_val = phoenix::bind(&FormulaParserGrammar::createTimeBoundReference, phoenix::ref(*this),
134 (qi::lit("steps"))[qi::_val = phoenix::bind(&FormulaParserGrammar::createTimeBoundReference, phoenix::ref(*this), storm::logic::TimeBoundType::Steps,
135 boost::none)] |
136 (-qi::lit("time"))[qi::_val = phoenix::bind(&FormulaParserGrammar::createTimeBoundReference, phoenix::ref(*this), storm::logic::TimeBoundType::Time,
137 boost::none)];
138 timeBoundReference.name("time bound reference");
139 timeBound = ((timeBoundReference >> qi::lit("[")) > expressionParser > qi::lit(",") > expressionParser >
140 qi::lit("]"))[qi::_val = phoenix::bind(&FormulaParserGrammar::createTimeBoundFromInterval, phoenix::ref(*this), qi::_2, qi::_3, qi::_1)] |
141 (timeBoundReference >> (qi::lit("<=")[(qi::_a = true, qi::_b = false)] | qi::lit("<")[(qi::_a = true, qi::_b = true)] |
142 qi::lit(">=")[(qi::_a = false, qi::_b = false)] | qi::lit(">")[(qi::_a = false, qi::_b = true)]) >>
143 expressionParser)[qi::_val = phoenix::bind(&FormulaParserGrammar::createTimeBoundFromSingleBound, phoenix::ref(*this), qi::_2, qi::_a, qi::_b,
144 qi::_1)] |
145 (timeBoundReference >> qi::lit("=") >>
146 expressionParser)[qi::_val = phoenix::bind(&FormulaParserGrammar::createTimeBoundFromInterval, phoenix::ref(*this), qi::_2, qi::_2, qi::_1)];
147 timeBound.name("time bound");
148 timeBounds = (timeBound % qi::lit(",")) | (((-qi::lit("^") >> qi::lit("{")) >> (timeBound % qi::lit(","))) >> qi::lit("}"));
149 timeBounds.name("time bounds");
150 eventuallyFormula =
151 (qi::lit("F") > (-timeBounds) >
152 basicPathFormula(qi::_r1))[qi::_val = phoenix::bind(&FormulaParserGrammar::createEventuallyFormula, phoenix::ref(*this), qi::_1, qi::_r1, qi::_2)];
153 eventuallyFormula.name("eventually formula");
154 nextFormula = (qi::lit("X") > basicPathFormula(qi::_r1))[qi::_val = phoenix::bind(&FormulaParserGrammar::createNextFormula, phoenix::ref(*this), qi::_1)];
155 nextFormula.name("next formula");
156 globallyFormula =
157 (qi::lit("G") > basicPathFormula(qi::_r1))[qi::_val = phoenix::bind(&FormulaParserGrammar::createGloballyFormula, phoenix::ref(*this), qi::_1)];
158 globallyFormula.name("globally formula");
159 hoaPathFormula =
160 qi::lit("HOA:") > qi::lit("{") > quotedString[qi::_val = phoenix::bind(&FormulaParserGrammar::createHOAPathFormula, phoenix::ref(*this), qi::_1)] >>
161 *(qi::lit(",") > quotedString > qi::lit("->") >
162 formula(FormulaKind::State, qi::_r1))[phoenix::bind(&FormulaParserGrammar::addHoaAPMapping, phoenix::ref(*this), *qi::_val, qi::_1, qi::_2)] >
163 qi::lit("}");
164 multiBoundedPathFormulaOperand = pathFormula(
165 qi::_r1)[qi::_pass = phoenix::bind(&FormulaParserGrammar::isValidMultiBoundedPathFormulaOperand, phoenix::ref(*this), qi::_1)][qi::_val = qi::_1];
166 multiBoundedPathFormulaOperand.name("multi bounded path formula operand");
167 multiBoundedPathFormula = ((qi::lit("multi") > qi::lit("(")) >> (multiBoundedPathFormulaOperand(qi::_r1) % qi::lit(",")) >>
168 qi::lit(")"))[qi::_val = phoenix::bind(&FormulaParserGrammar::createMultiBoundedPathFormula, phoenix::ref(*this), qi::_1)];
169 multiBoundedPathFormula.name("multi bounded path formula");
170 // U, W, R go through here
171 binaryLevelPathFormula =
172 basicPathFormula(qi::_r1)[qi::_val = qi::_1] >>
173 -((binaryPathOperator_ > (-timeBounds) >
174 basicPathFormula(
175 qi::_r1))[qi::_val = phoenix::bind(&FormulaParserGrammar::createBinaryPathFormula, phoenix::ref(*this), qi::_val, qi::_1, qi::_2, qi::_3)]) >>
176 (qi::eps > noAmbiguousNonAssociativeOperator(qi::_val)); // Do not parse a U b U c
177 binaryLevelPathFormula.name("binary path operator precedence level path formula");
178 pathFormula = binaryLevelPathFormula(qi::_r1);
179 pathFormula.name("path formula");
180
181 // Quantitative path formulae (reward)
182 discountedTotalRewardFormula =
183 (qi::lit("Cdiscount=") >>
184 expressionParser)[qi::_val = phoenix::bind(&FormulaParserGrammar::createDiscountedTotalRewardFormula, phoenix::ref(*this), qi::_1)];
185 discountedTotalRewardFormula.name("discounted total reward formula");
186 discountedCumulativeRewardFormula =
187 (qi::lit("C") >> timeBounds >> qi::lit("discount=") >>
188 expressionParser)[qi::_val = phoenix::bind(&FormulaParserGrammar::createDiscountedCumulativeRewardFormula, phoenix::ref(*this), qi::_2, qi::_1)];
189 discountedCumulativeRewardFormula.name("discounted cumulative reward formula");
190 longRunAverageRewardFormula = (qi::lit("LRA") | qi::lit("S") |
191 qi::lit("MP"))[qi::_val = phoenix::bind(&FormulaParserGrammar::createLongRunAverageRewardFormula, phoenix::ref(*this))];
192 longRunAverageRewardFormula.name("long run average reward formula");
193 instantaneousRewardFormula =
194 (qi::lit("I=") > expressionParser)[qi::_val = phoenix::bind(&FormulaParserGrammar::createInstantaneousRewardFormula, phoenix::ref(*this), qi::_1)];
195 instantaneousRewardFormula.name("instantaneous reward formula");
196 cumulativeRewardFormula =
197 (qi::lit("C") >> timeBounds)[qi::_val = phoenix::bind(&FormulaParserGrammar::createCumulativeRewardFormula, phoenix::ref(*this), qi::_1)];
198 cumulativeRewardFormula.name("cumulative reward formula");
199 totalRewardFormula = (qi::lit("C"))[qi::_val = phoenix::bind(&FormulaParserGrammar::createTotalRewardFormula, phoenix::ref(*this))];
200 totalRewardFormula.name("total reward formula");
201
202 // Game Formulae
203 playerCoalition = (-((identifier[phoenix::push_back(qi::_a, qi::_1)] | qi::uint_[phoenix::push_back(qi::_a, qi::_1)]) %
204 ','))[qi::_val = phoenix::bind(&FormulaParserGrammar::createPlayerCoalition, phoenix::ref(*this), qi::_a)];
205 playerCoalition.name("player coalition");
206 gameFormula = (qi::lit("<<") > playerCoalition > qi::lit(">>") >
207 operatorFormula)[qi::_val = phoenix::bind(&FormulaParserGrammar::createGameFormula, phoenix::ref(*this), qi::_1, qi::_2)];
208 gameFormula.name("game formula");
209
210 // Multi-objective, quantiles
211 multiOperatorFormula = (qi::lit("multi") > qi::lit("(") > (operatorFormula % qi::lit(",")) >
212 qi::lit(")"))[qi::_val = phoenix::bind(&FormulaParserGrammar::createMultiOperatorFormula, phoenix::ref(*this), qi::_1,
214 multiOperatorFormula.name("multi-objective operator formula");
215 multiLexOperatorFormula = (qi::lit("multilex") > qi::lit("(") > (operatorFormula % qi::lit(",")) >
216 qi::lit(")"))[qi::_val = phoenix::bind(&FormulaParserGrammar::createMultiOperatorFormula, phoenix::ref(*this), qi::_1,
218 multiLexOperatorFormula.name("multi-objective lexicographic operator formula");
219 quantileBoundVariable = (-optimalityOperator_ >> identifier >>
220 qi::lit(","))[qi::_val = phoenix::bind(&FormulaParserGrammar::createQuantileBoundVariables, phoenix::ref(*this), qi::_1, qi::_2)];
221 quantileBoundVariable.name("quantile bound variable");
222 quantileFormula = (qi::lit("quantile") > qi::lit("(") > *(quantileBoundVariable) >
223 operatorFormula[qi::_pass = phoenix::bind(&FormulaParserGrammar::isBooleanReturnType, phoenix::ref(*this), qi::_1, true)] >
224 qi::lit(")"))[qi::_val = phoenix::bind(&FormulaParserGrammar::createQuantileFormula, phoenix::ref(*this), qi::_1, qi::_2)];
225 quantileFormula.name("Quantile formula");
226
227 // General formulae
228 formula = (isPathFormula(qi::_r1) >> pathFormula(qi::_r2) | propositionalFormula(qi::_r1, qi::_r2));
229 formula.name("formula");
230
231 topLevelFormula = formula(FormulaKind::State, storm::logic::FormulaContext::Undefined);
232 topLevelFormula.name("top-level formula");
233
234 formulaName = qi::lit("\"") >> identifier >> qi::lit("\"") >> qi::lit(":");
235 formulaName.name("formula name");
236
237 constantDefinition =
238 (qi::lit("const") > -(qi::lit("int")[qi::_a = ConstantDataType::Integer] | qi::lit("bool")[qi::_a = ConstantDataType::Bool] |
239 qi::lit("double")[qi::_a = ConstantDataType::Rational]) >>
240 identifier >> -(qi::lit("=") > expressionParser))[phoenix::bind(&FormulaParserGrammar::addConstant, phoenix::ref(*this), qi::_1, qi::_a, qi::_2)];
241 constantDefinition.name("constant definition");
242
243#pragma clang diagnostic push
244#pragma clang diagnostic ignored "-Woverloaded-shift-op-parentheses"
245
246 filterProperty =
247 (-formulaName >> qi::lit("filter") > qi::lit("(") > filterType_ > qi::lit(",") > topLevelFormula > qi::lit(",") >
248 formula(FormulaKind::State, storm::logic::FormulaContext::Undefined) >
249 qi::lit(")"))[qi::_val = phoenix::bind(&FormulaParserGrammar::createProperty, phoenix::ref(*this), qi::_1, qi::_2, qi::_3, qi::_4)] |
250 (-formulaName >>
251 topLevelFormula)[qi::_val = phoenix::bind(&FormulaParserGrammar::createPropertyWithDefaultFilterTypeAndStates, phoenix::ref(*this), qi::_1, qi::_2)];
252 filterProperty.name("filter property");
253
254#pragma clang diagnostic pop
255
256 start = (qi::eps >> filterProperty[phoenix::push_back(qi::_val, qi::_1)] |
257 qi::eps(phoenix::bind(&FormulaParserGrammar::areConstantDefinitionsAllowed, phoenix::ref(*this))) >> constantDefinition | qi::eps) %
258 +(qi::char_("\n;")) >>
259 qi::skip(storm::spirit_encoding::space_type() | qi::lit("//") >> *(qi::char_ - (qi::eol | qi::eoi)))[qi::eps] >> qi::eoi;
260 start.name("start");
261
262 // Enable the following lines to print debug output for most the rules.
263 // debug(rewardModelName)
264 // debug(operatorFormula)
265 // debug(labelFormula)
266 // debug(expressionFormula)
267 // debug(basicPropositionalFormula)
268 // debug(negationPropositionalFormula)
269 // debug(andLevelPropositionalFormula)
270 // debug(orLevelPropositionalFormula)
271 // debug(propositionalFormula)
272 // debug(timeBoundReference)
273 // debug(timeBound)
274 // debug(timeBounds)
275 // debug(eventuallyFormula)
276 // debug(nextFormula)
277 // debug(globallyFormula)
278 // debug(hoaPathFormula)
279 // debug(multiBoundedPathFormula)
280 // debug(prefixOperatorPathFormula)
281 // debug(basicPathFormula)
282 // debug(binaryLevelPathFormula)
283 // debug(pathFormula)
284 // debug(longRunAverageRewardFormula)
285 // debug(instantaneousRewardFormula)
286 // debug(cumulativeRewardFormula)
287 // debug(totalRewardFormula)
288 // debug(playerCoalition)
289 // debug(gameFormula)
290 // debug(multiOperatorFormula)
291 // debug(multiLexOperatorFormula)
292 // debug(quantileBoundVariable)
293 // debug(quantileFormula)
294 // debug(formula)
295 // debug(topLevelFormula)
296 // debug(formulaName)
297 // debug(filterProperty)
298 // debug(constantDefinition )
299 // debug(start)
300 // debug(discountedCumulativeRewardFormula)
301 // debug(discountedTotalRewardFormula)
302
303 // Enable error reporting.
304 qi::on_error<qi::fail>(rewardModelName, handler(qi::_1, qi::_2, qi::_3, qi::_4));
305 qi::on_error<qi::fail>(operatorFormula, handler(qi::_1, qi::_2, qi::_3, qi::_4));
306 qi::on_error<qi::fail>(labelFormula, handler(qi::_1, qi::_2, qi::_3, qi::_4));
307 qi::on_error<qi::fail>(expressionFormula, handler(qi::_1, qi::_2, qi::_3, qi::_4));
308 qi::on_error<qi::fail>(basicPropositionalFormula, handler(qi::_1, qi::_2, qi::_3, qi::_4));
309 qi::on_error<qi::fail>(negationPropositionalFormula, handler(qi::_1, qi::_2, qi::_3, qi::_4));
310 qi::on_error<qi::fail>(andLevelPropositionalFormula, handler(qi::_1, qi::_2, qi::_3, qi::_4));
311 qi::on_error<qi::fail>(orLevelPropositionalFormula, handler(qi::_1, qi::_2, qi::_3, qi::_4));
312 qi::on_error<qi::fail>(propositionalFormula, handler(qi::_1, qi::_2, qi::_3, qi::_4));
313 qi::on_error<qi::fail>(timeBoundReference, handler(qi::_1, qi::_2, qi::_3, qi::_4));
314 qi::on_error<qi::fail>(timeBound, handler(qi::_1, qi::_2, qi::_3, qi::_4));
315 qi::on_error<qi::fail>(timeBounds, handler(qi::_1, qi::_2, qi::_3, qi::_4));
316 qi::on_error<qi::fail>(eventuallyFormula, handler(qi::_1, qi::_2, qi::_3, qi::_4));
317 qi::on_error<qi::fail>(nextFormula, handler(qi::_1, qi::_2, qi::_3, qi::_4));
318 qi::on_error<qi::fail>(globallyFormula, handler(qi::_1, qi::_2, qi::_3, qi::_4));
319 qi::on_error<qi::fail>(hoaPathFormula, handler(qi::_1, qi::_2, qi::_3, qi::_4));
320 qi::on_error<qi::fail>(multiBoundedPathFormula, handler(qi::_1, qi::_2, qi::_3, qi::_4));
321 qi::on_error<qi::fail>(prefixOperatorPathFormula, handler(qi::_1, qi::_2, qi::_3, qi::_4));
322 qi::on_error<qi::fail>(basicPathFormula, handler(qi::_1, qi::_2, qi::_3, qi::_4));
323 qi::on_error<qi::fail>(binaryLevelPathFormula, handler(qi::_1, qi::_2, qi::_3, qi::_4));
324 qi::on_error<qi::fail>(pathFormula, handler(qi::_1, qi::_2, qi::_3, qi::_4));
325 qi::on_error<qi::fail>(longRunAverageRewardFormula, handler(qi::_1, qi::_2, qi::_3, qi::_4));
326 qi::on_error<qi::fail>(instantaneousRewardFormula, handler(qi::_1, qi::_2, qi::_3, qi::_4));
327 qi::on_error<qi::fail>(cumulativeRewardFormula, handler(qi::_1, qi::_2, qi::_3, qi::_4));
328 qi::on_error<qi::fail>(totalRewardFormula, handler(qi::_1, qi::_2, qi::_3, qi::_4));
329 qi::on_error<qi::fail>(discountedCumulativeRewardFormula, handler(qi::_1, qi::_2, qi::_3, qi::_4));
330 qi::on_error<qi::fail>(discountedTotalRewardFormula, handler(qi::_1, qi::_2, qi::_3, qi::_4));
331 qi::on_error<qi::fail>(playerCoalition, handler(qi::_1, qi::_2, qi::_3, qi::_4));
332 qi::on_error<qi::fail>(gameFormula, handler(qi::_1, qi::_2, qi::_3, qi::_4));
333 qi::on_error<qi::fail>(multiOperatorFormula, handler(qi::_1, qi::_2, qi::_3, qi::_4));
334 qi::on_error<qi::fail>(multiLexOperatorFormula, handler(qi::_1, qi::_2, qi::_3, qi::_4));
335 qi::on_error<qi::fail>(quantileBoundVariable, handler(qi::_1, qi::_2, qi::_3, qi::_4));
336 qi::on_error<qi::fail>(quantileFormula, handler(qi::_1, qi::_2, qi::_3, qi::_4));
337 qi::on_error<qi::fail>(formula, handler(qi::_1, qi::_2, qi::_3, qi::_4));
338 qi::on_error<qi::fail>(topLevelFormula, handler(qi::_1, qi::_2, qi::_3, qi::_4));
339 qi::on_error<qi::fail>(formulaName, handler(qi::_1, qi::_2, qi::_3, qi::_4));
340 qi::on_error<qi::fail>(filterProperty, handler(qi::_1, qi::_2, qi::_3, qi::_4));
341 qi::on_error<qi::fail>(constantDefinition, handler(qi::_1, qi::_2, qi::_3, qi::_4));
342 qi::on_error<qi::fail>(start, handler(qi::_1, qi::_2, qi::_3, qi::_4));
343}
344
345void FormulaParserGrammar::addIdentifierExpression(std::string const& identifier, storm::expressions::Expression const& expression) {
346 STORM_LOG_WARN_COND(keywords_.find(identifier) == nullptr,
347 "Identifier `" << identifier << "' coincides with a reserved keyword or operator. Property expressions using the variable or constant '"
348 << identifier << "' will not be parsed correctly.");
349 STORM_LOG_WARN_COND(nonStandardKeywords_.find(identifier) == nullptr,
350 "Identifier `" << identifier << "' coincides with a reserved keyword or operator. Property expressions using the variable or constant '"
351 << identifier << "' might not be parsed correctly.");
352 this->identifiers_.add(identifier, expression);
353}
354
355void FormulaParserGrammar::addConstant(std::string const& name, ConstantDataType type, boost::optional<storm::expressions::Expression> const& expression) {
356 STORM_LOG_ASSERT(manager, "Mutable expression manager required to define new constants.");
358 STORM_LOG_THROW(!manager->hasVariable(name), storm::exceptions::WrongFormatException,
359 "Invalid constant definition '" << name << "' in property: variable already exists.");
360
361 if (type == ConstantDataType::Bool) {
362 newVariable = manager->declareBooleanVariable(name);
363 } else if (type == ConstantDataType::Integer) {
364 newVariable = manager->declareIntegerVariable(name);
365 } else {
366 newVariable = manager->declareRationalVariable(name);
367 }
368
369 if (expression) {
370 addIdentifierExpression(name, expression.get());
371 } else {
372 undefinedConstants.insert(newVariable);
373 addIdentifierExpression(name, newVariable);
374 }
375}
376
377bool FormulaParserGrammar::areConstantDefinitionsAllowed() const {
378 return static_cast<bool>(manager);
379}
380
381std::shared_ptr<storm::logic::TimeBoundReference> FormulaParserGrammar::createTimeBoundReference(storm::logic::TimeBoundType const& type,
382 boost::optional<std::string> const& rewardModelName) const {
384 return std::make_shared<storm::logic::TimeBoundReference>(rewardModelName);
385 } else {
386 return std::make_shared<storm::logic::TimeBoundReference>(type);
387 }
388}
389
390std::tuple<boost::optional<storm::logic::TimeBound>, boost::optional<storm::logic::TimeBound>, std::shared_ptr<storm::logic::TimeBoundReference>>
391FormulaParserGrammar::createTimeBoundFromInterval(storm::expressions::Expression const& lowerBound, storm::expressions::Expression const& upperBound,
392 std::shared_ptr<storm::logic::TimeBoundReference> const& timeBoundReference) const {
393 // As soon as it somehow does not break everything anymore, I will change return types here.
394
395 storm::logic::TimeBound lower(false, lowerBound);
396 storm::logic::TimeBound upper(false, upperBound);
397 return std::make_tuple(lower, upper, timeBoundReference);
398}
399
400std::tuple<boost::optional<storm::logic::TimeBound>, boost::optional<storm::logic::TimeBound>, std::shared_ptr<storm::logic::TimeBoundReference>>
401FormulaParserGrammar::createTimeBoundFromSingleBound(storm::expressions::Expression const& bound, bool upperBound, bool strict,
402 std::shared_ptr<storm::logic::TimeBoundReference> const& timeBoundReference) const {
403 // As soon as it somehow does not break everything anymore, I will change return types here.
404 if (upperBound) {
405 return std::make_tuple(boost::none, storm::logic::TimeBound(strict, bound), timeBoundReference);
406 } else {
407 return std::make_tuple(storm::logic::TimeBound(strict, bound), boost::none, timeBoundReference);
408 }
409}
410
411std::shared_ptr<storm::logic::Formula const> FormulaParserGrammar::createInstantaneousRewardFormula(storm::expressions::Expression const& timeBound) const {
412 return std::shared_ptr<storm::logic::Formula const>(new storm::logic::InstantaneousRewardFormula(timeBound));
413}
414
415std::shared_ptr<storm::logic::Formula const> FormulaParserGrammar::createCumulativeRewardFormula(
416 std::vector<std::tuple<boost::optional<storm::logic::TimeBound>, boost::optional<storm::logic::TimeBound>,
417 std::shared_ptr<storm::logic::TimeBoundReference>>> const& timeBounds) const {
418 std::vector<storm::logic::TimeBound> bounds;
419 std::vector<storm::logic::TimeBoundReference> timeBoundReferences;
420 for (auto const& timeBound : timeBounds) {
421 STORM_LOG_THROW(!std::get<0>(timeBound), storm::exceptions::WrongFormatException, "Cumulative reward formulas with lower time bound are not allowed.");
422 STORM_LOG_THROW(std::get<1>(timeBound), storm::exceptions::WrongFormatException, "Cumulative reward formulas require an upper bound.");
423 bounds.push_back(std::get<1>(timeBound).get());
424 timeBoundReferences.emplace_back(*std::get<2>(timeBound));
425 }
426 return std::shared_ptr<storm::logic::Formula const>(new storm::logic::CumulativeRewardFormula(bounds, timeBoundReferences));
427}
428
429std::shared_ptr<storm::logic::Formula const> FormulaParserGrammar::createDiscountedCumulativeRewardFormula(
430 storm::expressions::Expression const& discountFactor,
431 std::vector<std::tuple<boost::optional<storm::logic::TimeBound>, boost::optional<storm::logic::TimeBound>,
432 std::shared_ptr<storm::logic::TimeBoundReference>>> const& timeBounds) const {
433 std::vector<storm::logic::TimeBound> bounds;
434 std::vector<storm::logic::TimeBoundReference> timeBoundReferences;
435 for (auto const& timeBound : timeBounds) {
436 STORM_LOG_THROW(!std::get<0>(timeBound), storm::exceptions::WrongFormatException, "Cumulative reward formulas with lower time bound are not allowed.");
437 STORM_LOG_THROW(std::get<1>(timeBound), storm::exceptions::WrongFormatException, "Cumulative reward formulas require an upper bound.");
438 bounds.push_back(std::get<1>(timeBound).get());
439 timeBoundReferences.emplace_back(*std::get<2>(timeBound));
440 }
441 return std::shared_ptr<storm::logic::Formula const>(new storm::logic::DiscountedCumulativeRewardFormula(discountFactor, bounds, timeBoundReferences));
442}
443
444std::shared_ptr<storm::logic::Formula const> FormulaParserGrammar::createTotalRewardFormula() const {
445 return std::shared_ptr<storm::logic::Formula const>(new storm::logic::TotalRewardFormula());
446}
447
448std::shared_ptr<storm::logic::Formula const> FormulaParserGrammar::createDiscountedTotalRewardFormula(
449 storm::expressions::Expression const& discountFactor) const {
450 return std::shared_ptr<storm::logic::Formula const>(new storm::logic::DiscountedTotalRewardFormula(discountFactor));
451}
452
453std::shared_ptr<storm::logic::Formula const> FormulaParserGrammar::createLongRunAverageRewardFormula() const {
454 return std::shared_ptr<storm::logic::Formula const>(new storm::logic::LongRunAverageRewardFormula());
455}
456
457std::shared_ptr<storm::logic::Formula const> FormulaParserGrammar::createAtomicExpressionFormula(storm::expressions::Expression const& expression) const {
458 STORM_LOG_THROW(expression.hasBooleanType(), storm::exceptions::WrongFormatException,
459 "Expected expression " + expression.toString() + " to be of boolean type.");
460 if (expression.isLiteral()) {
461 return createBooleanLiteralFormula(expression.evaluateAsBool());
462 }
463 return std::shared_ptr<storm::logic::Formula const>(new storm::logic::AtomicExpressionFormula(expression));
464}
465
466std::shared_ptr<storm::logic::Formula const> FormulaParserGrammar::createBooleanLiteralFormula(bool literal) const {
467 return std::shared_ptr<storm::logic::Formula const>(new storm::logic::BooleanLiteralFormula(literal));
468}
469
470std::shared_ptr<storm::logic::Formula const> FormulaParserGrammar::createAtomicLabelFormula(std::string const& label) const {
471 return std::shared_ptr<storm::logic::Formula const>(new storm::logic::AtomicLabelFormula(label));
472}
473
474std::shared_ptr<storm::logic::Formula const> FormulaParserGrammar::createEventuallyFormula(
475 boost::optional<std::vector<std::tuple<boost::optional<storm::logic::TimeBound>, boost::optional<storm::logic::TimeBound>,
476 std::shared_ptr<storm::logic::TimeBoundReference>>>> const& timeBounds,
477 storm::logic::FormulaContext context, std::shared_ptr<storm::logic::Formula const> const& subformula) const {
478 if (timeBounds && !timeBounds.get().empty()) {
479 // Conversion of boost::optional to std::optional
480 // This can be simplified if the input is changed to already use std::optional
481 std::vector<std::optional<storm::logic::TimeBound>> lowerBounds, upperBounds;
482 std::vector<storm::logic::TimeBoundReference> timeBoundReferences;
483 for (auto const& timeBound : timeBounds.get()) {
484 auto const& lowerBound = std::get<0>(timeBound);
485 auto const& upperBound = std::get<1>(timeBound);
486 if (lowerBound) {
487 lowerBounds.emplace_back(lowerBound.get());
488 } else {
489 lowerBounds.emplace_back();
490 }
491 if (upperBound) {
492 upperBounds.emplace_back(upperBound.get());
493 } else {
494 upperBounds.emplace_back();
495 }
496 timeBoundReferences.emplace_back(*std::get<2>(timeBound));
497 }
498 return std::shared_ptr<storm::logic::Formula const>(
499 new storm::logic::BoundedUntilFormula(createBooleanLiteralFormula(true), subformula, lowerBounds, upperBounds, timeBoundReferences));
500 } else {
501 return std::shared_ptr<storm::logic::Formula const>(new storm::logic::EventuallyFormula(subformula, context));
502 }
503}
504
505std::shared_ptr<storm::logic::Formula const> FormulaParserGrammar::createGloballyFormula(std::shared_ptr<storm::logic::Formula const> const& subformula) const {
506 return std::shared_ptr<storm::logic::Formula const>(new storm::logic::GloballyFormula(subformula));
507}
508
509std::shared_ptr<storm::logic::Formula const> FormulaParserGrammar::createNextFormula(std::shared_ptr<storm::logic::Formula const> const& subformula) const {
510 return std::shared_ptr<storm::logic::Formula const>(new storm::logic::NextFormula(subformula));
511}
512
513std::string FormulaParserGrammar::toString(BinaryPathOperator op) {
514 switch (op) {
515 case BinaryPathOperator::Until:
516 return "U";
517 case BinaryPathOperator::WeakUntil:
518 return "W";
519 case BinaryPathOperator::Release:
520 return "R";
521 }
522 STORM_LOG_THROW_UNCONDITIONALLY(storm::exceptions::IllegalArgumentException, "Unknown BinaryPathOperator.");
523}
524
525std::shared_ptr<storm::logic::Formula const> FormulaParserGrammar::createBinaryPathFormula(
526 std::shared_ptr<storm::logic::Formula const> const& leftSubformula, BinaryPathOperator op,
527 boost::optional<std::vector<std::tuple<boost::optional<storm::logic::TimeBound>, boost::optional<storm::logic::TimeBound>,
528 std::shared_ptr<storm::logic::TimeBoundReference>>>> const& timeBounds,
529 std::shared_ptr<storm::logic::Formula const> const& rightSubformula) const {
530 bool const hasTimeBounds = timeBounds && !timeBounds.get().empty();
531 if (op != BinaryPathOperator::Until) {
532 // Only until operator has a bounded variant.
533 STORM_LOG_THROW(!hasTimeBounds, storm::exceptions::WrongFormatException, "Bounded '" << toString(op) << "' formulas are not supported.");
534 if (op == BinaryPathOperator::WeakUntil) {
535 return std::shared_ptr<storm::logic::Formula const>(new storm::logic::WeakUntilFormula(leftSubformula, rightSubformula));
536 }
537 return std::shared_ptr<storm::logic::Formula const>(new storm::logic::ReleaseFormula(leftSubformula, rightSubformula));
538 }
539 if (!hasTimeBounds) {
540 return std::shared_ptr<storm::logic::Formula const>(new storm::logic::UntilFormula(leftSubformula, rightSubformula));
541 }
542 // Conversion of boost::optional to std::optional
543 // This can be simplified if the input is changed to already use std::optional
544 std::vector<std::optional<storm::logic::TimeBound>> lowerBounds, upperBounds;
545 std::vector<storm::logic::TimeBoundReference> timeBoundReferences;
546 for (auto const& timeBound : timeBounds.get()) {
547 auto const& lowerBound = std::get<0>(timeBound);
548 auto const& upperBound = std::get<1>(timeBound);
549 if (lowerBound) {
550 lowerBounds.emplace_back(lowerBound.get());
551 } else {
552 lowerBounds.emplace_back();
553 }
554 if (upperBound) {
555 upperBounds.emplace_back(upperBound.get());
556 } else {
557 upperBounds.emplace_back();
558 }
559 timeBoundReferences.emplace_back(*std::get<2>(timeBound));
560 }
561 return std::shared_ptr<storm::logic::Formula const>(
562 new storm::logic::BoundedUntilFormula(leftSubformula, rightSubformula, lowerBounds, upperBounds, timeBoundReferences));
563}
564
565std::shared_ptr<storm::logic::Formula const> FormulaParserGrammar::createHOAPathFormula(std::string const& automatonFile) const {
566 return std::shared_ptr<storm::logic::Formula const>(new storm::logic::HOAPathFormula(automatonFile));
567}
568
569void FormulaParserGrammar::addHoaAPMapping(storm::logic::Formula const& hoaFormula, const std::string& ap,
570 std::shared_ptr<storm::logic::Formula const>& expression) const {
571 // taking a const Formula reference and doing static_ and const_cast from Formula to allow non-const access to
572 // qi::_val of the hoaPathFormula rule
573 storm::logic::HOAPathFormula& hoaFormula_ = static_cast<storm::logic::HOAPathFormula&>(const_cast<storm::logic::Formula&>(hoaFormula));
574 hoaFormula_.addAPMapping(ap, expression);
575}
576
577std::shared_ptr<storm::logic::Formula const> FormulaParserGrammar::createConditionalFormula(
578 std::shared_ptr<storm::logic::Formula const> const& leftSubformula, boost::optional<std::shared_ptr<storm::logic::Formula const>> const& rightSubformula,
579 storm::logic::FormulaContext context) const {
580 if (rightSubformula) {
581 return std::shared_ptr<storm::logic::Formula const>(new storm::logic::ConditionalFormula(leftSubformula, rightSubformula.get(), context));
582 } else {
583 // If there is no rhs, just return the lhs
584 return leftSubformula;
585 }
586}
587
588storm::logic::OperatorInformation FormulaParserGrammar::createOperatorInformation(boost::optional<storm::OptimizationDirection> const& optimizationDirection,
589 boost::optional<storm::logic::ComparisonType> const& comparisonType,
590 boost::optional<storm::expressions::Expression> const& threshold) const {
591 if (comparisonType && threshold) {
592 storm::expressions::ExpressionEvaluator<storm::RationalNumber> evaluator(*constManager);
593 return storm::logic::OperatorInformation(optimizationDirection, storm::logic::Bound(comparisonType.get(), threshold.get()));
594 } else {
595 return storm::logic::OperatorInformation(optimizationDirection, boost::none);
596 }
597}
598
599std::shared_ptr<storm::logic::Formula const> FormulaParserGrammar::createOperatorFormula(storm::logic::FormulaContext const& context,
600 boost::optional<std::string> const& rewardModelName,
601 storm::logic::OperatorInformation const& operatorInformation,
602 std::shared_ptr<storm::logic::Formula const> const& subformula) {
603 switch (context) {
605 STORM_LOG_ASSERT(!rewardModelName, "Probability operator with reward information parsed.");
606 return createProbabilityOperatorFormula(operatorInformation, subformula);
608 return createRewardOperatorFormula(rewardModelName, operatorInformation, subformula);
610 STORM_LOG_ASSERT(!rewardModelName, "LRA operator with reward information parsed.");
611 return createLongRunAverageOperatorFormula(operatorInformation, subformula);
613 STORM_LOG_ASSERT(!rewardModelName, "Time operator with reward model name parsed.");
614 return createTimeOperatorFormula(operatorInformation, subformula);
615 default:
616 STORM_LOG_THROW(false, storm::exceptions::WrongFormatException, "Unexpected formula context.");
617 }
618}
619
620std::shared_ptr<storm::logic::Formula const> FormulaParserGrammar::createLongRunAverageOperatorFormula(
621 storm::logic::OperatorInformation const& operatorInformation, std::shared_ptr<storm::logic::Formula const> const& subformula) const {
622 return std::shared_ptr<storm::logic::Formula const>(new storm::logic::LongRunAverageOperatorFormula(subformula, operatorInformation));
623}
624
625std::shared_ptr<storm::logic::Formula const> FormulaParserGrammar::createRewardOperatorFormula(
626 boost::optional<std::string> const& rewardModelName, storm::logic::OperatorInformation const& operatorInformation,
627 std::shared_ptr<storm::logic::Formula const> const& subformula) const {
628 return std::shared_ptr<storm::logic::Formula const>(new storm::logic::RewardOperatorFormula(subformula, rewardModelName, operatorInformation));
629}
630
631std::shared_ptr<storm::logic::Formula const> FormulaParserGrammar::createTimeOperatorFormula(
632 storm::logic::OperatorInformation const& operatorInformation, std::shared_ptr<storm::logic::Formula const> const& subformula) const {
633 return std::shared_ptr<storm::logic::Formula const>(new storm::logic::TimeOperatorFormula(subformula, operatorInformation));
634}
635
636std::shared_ptr<storm::logic::Formula const> FormulaParserGrammar::createProbabilityOperatorFormula(
637 storm::logic::OperatorInformation const& operatorInformation, std::shared_ptr<storm::logic::Formula const> const& subformula) {
638 return std::shared_ptr<storm::logic::Formula const>(new storm::logic::ProbabilityOperatorFormula(subformula, operatorInformation));
639}
640
641std::shared_ptr<storm::logic::Formula const> FormulaParserGrammar::createBinaryBooleanStateFormula(
642 std::shared_ptr<storm::logic::Formula const> const& leftSubformula, std::shared_ptr<storm::logic::Formula const> const& rightSubformula,
644 return std::shared_ptr<storm::logic::Formula const>(new storm::logic::BinaryBooleanStateFormula(operatorType, leftSubformula, rightSubformula));
645}
646
647std::shared_ptr<storm::logic::Formula const> FormulaParserGrammar::createUnaryBooleanStateFormula(
648 std::shared_ptr<storm::logic::Formula const> const& subformula, boost::optional<storm::logic::UnaryBooleanStateFormula::OperatorType> const& operatorType) {
649 if (operatorType) {
650 return std::shared_ptr<storm::logic::Formula const>(new storm::logic::UnaryBooleanStateFormula(operatorType.get(), subformula));
651 } else {
652 return subformula;
653 }
654}
655
656std::shared_ptr<storm::logic::Formula const> FormulaParserGrammar::createBinaryBooleanPathFormula(
657 std::shared_ptr<storm::logic::Formula const> const& leftSubformula, std::shared_ptr<storm::logic::Formula const> const& rightSubformula,
659 return std::shared_ptr<storm::logic::Formula const>(new storm::logic::BinaryBooleanPathFormula(operatorType, leftSubformula, rightSubformula));
660}
661
662std::shared_ptr<storm::logic::Formula const> FormulaParserGrammar::createUnaryBooleanPathFormula(
663 std::shared_ptr<storm::logic::Formula const> const& subformula, boost::optional<storm::logic::UnaryBooleanPathFormula::OperatorType> const& operatorType) {
664 if (operatorType) {
665 return std::shared_ptr<storm::logic::Formula const>(new storm::logic::UnaryBooleanPathFormula(operatorType.get(), subformula));
666 } else {
667 return subformula;
668 }
669}
670
671std::shared_ptr<storm::logic::Formula const> FormulaParserGrammar::createBinaryBooleanStateOrPathFormula(
672 std::shared_ptr<storm::logic::Formula const> const& leftSubformula, std::shared_ptr<storm::logic::Formula const> const& rightSubformula,
674 if (leftSubformula->isStateFormula() && rightSubformula->isStateFormula()) {
675 return createBinaryBooleanStateFormula(leftSubformula, rightSubformula, operatorType);
676 } else if (leftSubformula->isPathFormula() || rightSubformula->isPathFormula()) {
677 return createBinaryBooleanPathFormula(leftSubformula, rightSubformula, operatorType);
678 }
679 STORM_LOG_THROW(false, storm::exceptions::WrongFormatException, "Subformulas have unexpected type.");
680}
681
682std::shared_ptr<storm::logic::Formula const> FormulaParserGrammar::createUnaryBooleanStateOrPathFormula(
683 std::shared_ptr<storm::logic::Formula const> const& subformula, boost::optional<storm::logic::UnaryBooleanOperatorType> const& operatorType) {
684 if (subformula->isStateFormula()) {
685 return createUnaryBooleanStateFormula(subformula, operatorType);
686 } else if (subformula->isPathFormula()) {
687 return createUnaryBooleanPathFormula(subformula, operatorType);
688 }
689 STORM_LOG_THROW(false, storm::exceptions::WrongFormatException, "Subformulas have unexpected type.");
690}
691
692bool FormulaParserGrammar::isValidMultiBoundedPathFormulaOperand(std::shared_ptr<storm::logic::Formula const> const& operand) {
693 if (operand->isBoundedUntilFormula()) {
694 if (!operand->asBoundedUntilFormula().isMultiDimensional()) {
695 return true;
696 }
697 STORM_LOG_ERROR("Composition of multidimensional bounded until formula must consist of single dimension subformulas. Got '" << *operand
698 << "' instead.");
699 }
700 return false;
701}
702
703std::shared_ptr<storm::logic::Formula const> FormulaParserGrammar::createMultiBoundedPathFormula(
704 std::vector<std::shared_ptr<storm::logic::Formula const>> const& subformulas) {
705 std::vector<std::shared_ptr<storm::logic::Formula const>> leftSubformulas, rightSubformulas;
706 std::vector<std::optional<storm::logic::TimeBound>> lowerBounds, upperBounds;
707 std::vector<storm::logic::TimeBoundReference> timeBoundReferences;
708 for (auto const& subformula : subformulas) {
709 STORM_LOG_THROW(subformula->isBoundedUntilFormula(), storm::exceptions::WrongFormatException,
710 "Multi-path formulas require bounded until (or eventually) subformulae. Got '" << *subformula << "' instead.");
711 auto const& f = subformula->asBoundedUntilFormula();
712 STORM_LOG_THROW(!f.isMultiDimensional(), storm::exceptions::WrongFormatException,
713 "Composition of multidimensional bounded until formula must consist of single dimension subformulas. Got '" << f << "' instead.");
714 leftSubformulas.push_back(f.getLeftSubformula().asSharedPointer());
715 rightSubformulas.push_back(f.getRightSubformula().asSharedPointer());
716 if (f.hasLowerBound()) {
717 lowerBounds.emplace_back(storm::logic::TimeBound(f.isLowerBoundStrict(), f.getLowerBound()));
718 } else {
719 lowerBounds.emplace_back();
720 }
721 if (f.hasUpperBound()) {
722 upperBounds.emplace_back(storm::logic::TimeBound(f.isUpperBoundStrict(), f.getUpperBound()));
723 } else {
724 upperBounds.emplace_back();
725 }
726 timeBoundReferences.push_back(f.getTimeBoundReference());
727 }
728 return std::shared_ptr<storm::logic::Formula const>(
729 new storm::logic::BoundedUntilFormula(leftSubformulas, rightSubformulas, lowerBounds, upperBounds, timeBoundReferences));
730}
731
732std::shared_ptr<storm::logic::Formula const> FormulaParserGrammar::createMultiOperatorFormula(
733 std::vector<std::shared_ptr<storm::logic::Formula const>> const& subformulas, storm::logic::MultiObjectiveFormula::Type type) {
734 return std::shared_ptr<storm::logic::Formula const>(new storm::logic::MultiObjectiveFormula(subformulas, type));
735}
736
737storm::expressions::Variable FormulaParserGrammar::createQuantileBoundVariables(boost::optional<storm::solver::OptimizationDirection> const& dir,
738 std::string const& variableName) {
739 STORM_LOG_ASSERT(manager, "Mutable expression manager required to define quantile bound variable.");
740 storm::expressions::Variable var;
741 if (manager->hasVariable(variableName)) {
742 var = manager->getVariable(variableName);
743 STORM_LOG_THROW(quantileFormulaVariables.count(var) > 0, storm::exceptions::WrongFormatException,
744 "Invalid quantile variable name '" << variableName << "' in quantile formula: variable already exists.");
745 } else {
746 var = manager->declareRationalVariable(variableName);
747 quantileFormulaVariables.insert(var);
748 }
749 STORM_LOG_WARN_COND(!dir.is_initialized(), "Optimization direction '"
750 << dir.get() << "' for quantile variable " << variableName
751 << " is ignored. This information will be derived from the subformula of the quantile.");
752 addIdentifierExpression(variableName, var);
753 return var;
754}
755
756std::shared_ptr<storm::logic::Formula const> FormulaParserGrammar::createQuantileFormula(std::vector<storm::expressions::Variable> const& boundVariables,
757 std::shared_ptr<storm::logic::Formula const> const& subformula) {
758 return std::shared_ptr<storm::logic::Formula const>(new storm::logic::QuantileFormula(boundVariables, subformula));
759}
760
761std::set<storm::expressions::Variable> FormulaParserGrammar::getUndefinedConstants(std::shared_ptr<storm::logic::Formula const> const& formula) const {
762 std::set<storm::expressions::Variable> result;
763 std::set<storm::expressions::Variable> usedVariables = formula->getUsedVariables();
764 std::set_intersection(usedVariables.begin(), usedVariables.end(), undefinedConstants.begin(), undefinedConstants.end(),
765 std::inserter(result, result.begin()));
766 return result;
767}
768
769storm::jani::Property FormulaParserGrammar::createProperty(boost::optional<std::string> const& propertyName, storm::modelchecker::FilterType const& filterType,
770 std::shared_ptr<storm::logic::Formula const> const& formula,
771 std::shared_ptr<storm::logic::Formula const> const& states) {
772 storm::jani::FilterExpression filterExpression(formula, filterType, states);
773
774 ++propertyCount;
775 if (propertyName) {
776 return storm::jani::Property(propertyName.get(), filterExpression, this->getUndefinedConstants(formula));
777 } else {
778 return storm::jani::Property(std::to_string(propertyCount - 1), filterExpression, this->getUndefinedConstants(formula));
779 }
780}
781
782storm::jani::Property FormulaParserGrammar::createPropertyWithDefaultFilterTypeAndStates(boost::optional<std::string> const& propertyName,
783 std::shared_ptr<storm::logic::Formula const> const& formula) {
784 ++propertyCount;
785 if (propertyName) {
786 return storm::jani::Property(propertyName.get(), formula, this->getUndefinedConstants(formula));
787 } else {
788 return storm::jani::Property(std::to_string(propertyCount), formula, this->getUndefinedConstants(formula));
789 }
790}
791
792storm::logic::PlayerCoalition FormulaParserGrammar::createPlayerCoalition(
793 std::vector<std::variant<std::string, storm::storage::PlayerIndex>> const& playerIds) const {
794 return storm::logic::PlayerCoalition(playerIds);
795}
796
797std::shared_ptr<storm::logic::Formula const> FormulaParserGrammar::createGameFormula(storm::logic::PlayerCoalition const& coalition,
798 std::shared_ptr<storm::logic::Formula const> const& subformula) const {
799 return std::shared_ptr<storm::logic::Formula const>(new storm::logic::GameFormula(coalition, subformula));
800}
801
802bool FormulaParserGrammar::isBooleanReturnType(std::shared_ptr<storm::logic::Formula const> const& formula, bool raiseErrorMessage) {
803 if (formula->hasQualitativeResult()) {
804 return true;
805 }
806 STORM_LOG_ERROR_COND(!raiseErrorMessage, "Formula " << *formula << " does not have a Boolean return type.");
807 return false;
808}
809
810bool FormulaParserGrammar::raiseAmbiguousNonAssociativeOperatorError(std::shared_ptr<storm::logic::Formula const> const& formula, BinaryPathOperator op) const {
811 STORM_LOG_ERROR("Ambiguous use of non-associative operator '" << toString(op) << "' in formula '" << *formula << " " << toString(op) << " ... '");
812 return true;
813}
814
815} // namespace parser
816} // namespace storm
bool evaluateAsBool(Valuation const *valuation=nullptr) const
Evaluates the expression under the valuation of variables given by the valuation and returns the resu...
bool hasBooleanType() const
Retrieves whether the expression has a boolean return type.
bool isLiteral() const
Retrieves whether the expression is a literal.
std::string toString() const
Converts the expression into a string.
storm::logic::BinaryBooleanOperatorType OperatorType
storm::logic::BinaryBooleanOperatorType OperatorType
void addAPMapping(const std::string &ap, const std::shared_ptr< Formula const > &formula)
void setIdentifierMapping(qi::symbols< char, storm::expressions::Expression > const *identifiers_)
Sets an identifier mapping that is used to determine valid variables in the expression.
qi::symbols< char, storm::expressions::Expression > const & getIdentifiers() const
FormulaParserGrammar(std::shared_ptr< storm::expressions::ExpressionManager const > const &manager)
void addIdentifierExpression(std::string const &identifier, storm::expressions::Expression const &expression)
Adds an identifier and the expression it is supposed to be replaced with.
#define STORM_LOG_ERROR(message)
Definition logging.h:36
#define STORM_LOG_THROW_UNCONDITIONALLY(exception, message)
Definition macros.h:48
#define STORM_LOG_ASSERT(cond, message)
Definition macros.h:9
#define STORM_LOG_WARN_COND(cond, message)
Definition macros.h:54
#define STORM_LOG_ERROR_COND(cond, message)
Definition macros.h:68
#define STORM_LOG_THROW(cond, exception, message)
Definition macros.h:39
Contains all file parsers and helper classes.
The contract of a DD backend.