11 :
FormulaParserGrammar::base_type(start), constManager(manager), manager(nullptr), expressionParser(*manager, keywords_, true, true), propertyCount(0) {
16 :
FormulaParserGrammar::base_type(start), constManager(manager), manager(manager), expressionParser(*manager, keywords_, true, true), propertyCount(0) {
24void FormulaParserGrammar::initialize() {
26 for (
auto variableTypePair : *constManager) {
32 keywords_.name(
"keyword");
33 nonStandardKeywords_.name(
"non-standard Storm-specific keyword");
34 relationalOperator_.name(
"relational operator");
35 optimalityOperator_.name(
"optimality operator");
36 operatorKeyword_.name(
"Operator keyword");
37 filterType_.name(
"filter type");
40 isPathFormula = qi::eps(qi::_r1 == FormulaKind::Path);
41 noAmbiguousNonAssociativeOperator =
42 !(qi::lit(qi::_r2)[qi::_pass = phoenix::bind(&FormulaParserGrammar::raiseAmbiguousNonAssociativeOperatorError, phoenix::ref(*
this), qi::_r1, qi::_r2)]);
43 noAmbiguousNonAssociativeOperator.name(
"no ambiguous non-associative operator");
44 identifier %= qi::as_string[qi::raw[qi::lexeme[((qi::alpha | qi::char_(
'_') | qi::char_(
'.')) >> *(qi::alnum | qi::char_(
'_')))]]];
45 identifier.name(
"identifier");
46 label %= qi::as_string[qi::raw[qi::lexeme[((qi::alpha | qi::char_(
'_')) >> *(qi::alnum | qi::char_(
'_')))]]];
48 quotedString %= qi::as_string[qi::lexeme[qi::omit[qi::char_(
'"')] > qi::raw[*(!qi::char_(
'"') >> qi::char_)] > qi::omit[qi::lit(
'"')]]];
49 quotedString.name(
"quoted string");
53 (-optimalityOperator_)[qi::_a = qi::_1] >>
55 qi::lit(
"?"))[qi::_val = phoenix::bind(&FormulaParserGrammar::createOperatorInformation, phoenix::ref(*
this), qi::_a, boost::none, boost::none)] |
56 (relationalOperator_ >
57 expressionParser)[qi::_val = phoenix::bind(&FormulaParserGrammar::createOperatorInformation, phoenix::ref(*
this), qi::_a, qi::_1, qi::_2)]);
58 operatorInformation.name(
"operator information");
62 (longRunAverageRewardFormula | eventuallyFormula(qi::_r1) | discountedCumulativeRewardFormula | discountedTotalRewardFormula |
63 cumulativeRewardFormula | instantaneousRewardFormula | totalRewardFormula)) |
68 phoenix::ref(*
this), qi::_1, qi::_2, qi::_r1)];
69 operatorSubFormula.name(
"operator subformula");
71 rewardModelName.name(
"reward model name");
73 (operatorKeyword_[qi::_a = qi::_1] > -rewardModelName(qi::_a) > operatorInformation > qi::lit(
"[") > operatorSubFormula(qi::_a) >
74 qi::lit(
"]"))[qi::_val = phoenix::bind(&FormulaParserGrammar::createOperatorFormula, phoenix::ref(*
this), qi::_a, qi::_2, qi::_3, qi::_4)];
75 operatorFormula.name(
"operator formula");
79 (qi::lit(
"\"") >> label >> qi::lit(
"\""))[qi::_val = phoenix::bind(&FormulaParserGrammar::createAtomicLabelFormula, phoenix::ref(*
this), qi::_1)];
80 labelFormula.name(
"label formula");
81 expressionFormula = expressionParser[qi::_val = phoenix::bind(&FormulaParserGrammar::createAtomicExpressionFormula, phoenix::ref(*
this), qi::_1)];
82 expressionFormula.name(
"expression formula");
84 basicPropositionalFormula =
88 (formula(qi::_r1, qi::_r2) > qi::lit(
")")))
89 | labelFormula | negationPropositionalFormula(qi::_r1, qi::_r2) | operatorFormula |
90 (isPathFormula(qi::_r1) >> prefixOperatorPathFormula(qi::_r2))
91 | multiOperatorFormula
92 | quantileFormula | gameFormula;
97 negationPropositionalFormula =
99 basicPropositionalFormula(qi::_r1, qi::_r2))[qi::_val = phoenix::bind(&FormulaParserGrammar::createUnaryBooleanStateOrPathFormula, phoenix::ref(*
this),
101 basicPropositionalFormula.name(
"basic propositional formula");
102 andLevelPropositionalFormula =
103 basicPropositionalFormula(qi::_r1, qi::_r2)[qi::_val = qi::_1] >>
104 *(qi::lit(
"&") > basicPropositionalFormula(
105 qi::_r1, qi::_r2)[qi::_val = phoenix::bind(&FormulaParserGrammar::createBinaryBooleanStateOrPathFormula, phoenix::ref(*
this),
106 qi::_val, qi::_1, storm::logic::BinaryBooleanStateFormula::OperatorType::And)]);
107 andLevelPropositionalFormula.name(
"and precedence level propositional formula");
108 orLevelPropositionalFormula =
109 andLevelPropositionalFormula(qi::_r1, qi::_r2)[qi::_val = qi::_1] >>
110 *((!qi::lit(
"||") >> qi::lit(
"|"))
111 > andLevelPropositionalFormula(
112 qi::_r1, qi::_r2)[qi::_val = phoenix::bind(&FormulaParserGrammar::createBinaryBooleanStateOrPathFormula, phoenix::ref(*
this), qi::_val, qi::_1,
113 storm::logic::BinaryBooleanStateFormula::OperatorType::Or)]);
114 orLevelPropositionalFormula.name(
"or precedence level propositional formula");
115 propositionalFormula = orLevelPropositionalFormula(qi::_r1, qi::_r2);
119 basicPathFormula = propositionalFormula(FormulaKind::Path, qi::_r1)
120 | prefixOperatorPathFormula(
122 prefixOperatorPathFormula =
123 eventuallyFormula(qi::_r1) | nextFormula(qi::_r1) | globallyFormula(qi::_r1) | hoaPathFormula(qi::_r1) | multiBoundedPathFormula(qi::_r1);
124 basicPathFormula.name(
"basic path formula");
136 timeBoundReference.name(
"time bound reference");
137 timeBound = ((timeBoundReference >> qi::lit(
"[")) > expressionParser > qi::lit(
",") > expressionParser >
138 qi::lit(
"]"))[qi::_val = phoenix::bind(&FormulaParserGrammar::createTimeBoundFromInterval, phoenix::ref(*
this), qi::_2, qi::_3, qi::_1)] |
139 (timeBoundReference >> (qi::lit(
"<=")[(qi::_a =
true, qi::_b =
false)] | qi::lit(
"<")[(qi::_a =
true, qi::_b =
true)] |
140 qi::lit(
">=")[(qi::_a =
false, qi::_b =
false)] | qi::lit(
">")[(qi::_a =
false, qi::_b =
true)]) >>
141 expressionParser)[qi::_val = phoenix::bind(&FormulaParserGrammar::createTimeBoundFromSingleBound, phoenix::ref(*
this), qi::_2, qi::_a, qi::_b,
143 (timeBoundReference >> qi::lit(
"=") >>
144 expressionParser)[qi::_val = phoenix::bind(&FormulaParserGrammar::createTimeBoundFromInterval, phoenix::ref(*
this), qi::_2, qi::_2, qi::_1)];
145 timeBound.name(
"time bound");
146 timeBounds = (timeBound % qi::lit(
",")) | (((-qi::lit(
"^") >> qi::lit(
"{")) >> (timeBound % qi::lit(
","))) >> qi::lit(
"}"));
147 timeBounds.name(
"time bounds");
149 (qi::lit(
"F") > (-timeBounds) >
150 basicPathFormula(qi::_r1))[qi::_val = phoenix::bind(&FormulaParserGrammar::createEventuallyFormula, phoenix::ref(*
this), qi::_1, qi::_r1, qi::_2)];
151 eventuallyFormula.name(
"eventually formula");
152 nextFormula = (qi::lit(
"X") > basicPathFormula(qi::_r1))[qi::_val = phoenix::bind(&FormulaParserGrammar::createNextFormula, phoenix::ref(*
this), qi::_1)];
153 nextFormula.name(
"next formula");
155 (qi::lit(
"G") > basicPathFormula(qi::_r1))[qi::_val = phoenix::bind(&FormulaParserGrammar::createGloballyFormula, phoenix::ref(*
this), qi::_1)];
156 globallyFormula.name(
"globally formula");
158 qi::lit(
"HOA:") > qi::lit(
"{") > quotedString[qi::_val = phoenix::bind(&FormulaParserGrammar::createHOAPathFormula, phoenix::ref(*
this), qi::_1)] >>
159 *(qi::lit(
",") > quotedString > qi::lit(
"->") >
160 formula(FormulaKind::State, qi::_r1))[phoenix::bind(&FormulaParserGrammar::addHoaAPMapping, phoenix::ref(*
this), *qi::_val, qi::_1, qi::_2)] >
162 multiBoundedPathFormulaOperand = pathFormula(
163 qi::_r1)[qi::_pass = phoenix::bind(&FormulaParserGrammar::isValidMultiBoundedPathFormulaOperand, phoenix::ref(*
this), qi::_1)][qi::_val = qi::_1];
164 multiBoundedPathFormulaOperand.name(
"multi bounded path formula operand");
165 multiBoundedPathFormula = ((qi::lit(
"multi") > qi::lit(
"(")) >> (multiBoundedPathFormulaOperand(qi::_r1) % qi::lit(
",")) >>
166 qi::lit(
")"))[qi::_val = phoenix::bind(&FormulaParserGrammar::createMultiBoundedPathFormula, phoenix::ref(*
this), qi::_1)];
167 multiBoundedPathFormula.name(
"multi bounded path formula");
168 untilLevelPathFormula =
169 basicPathFormula(qi::_r1)[qi::_val = qi::_1] >>
170 -((qi::lit(
"U") > (-timeBounds) >
171 basicPathFormula(qi::_r1))[qi::_val = phoenix::bind(&FormulaParserGrammar::createUntilFormula, phoenix::ref(*
this), qi::_val, qi::_1, qi::_2)]) >>
172 (qi::eps > noAmbiguousNonAssociativeOperator(qi::_val, std::string(
"U")));
173 untilLevelPathFormula.name(
"until precedence level path formula");
174 pathFormula = untilLevelPathFormula(qi::_r1);
175 pathFormula.name(
"path formula");
178 discountedTotalRewardFormula =
179 (qi::lit(
"Cdiscount=") >>
180 expressionParser)[qi::_val = phoenix::bind(&FormulaParserGrammar::createDiscountedTotalRewardFormula, phoenix::ref(*
this), qi::_1)];
181 discountedTotalRewardFormula.name(
"discounted total reward formula");
182 discountedCumulativeRewardFormula =
183 (qi::lit(
"C") >> timeBounds >> qi::lit(
"discount=") >>
184 expressionParser)[qi::_val = phoenix::bind(&FormulaParserGrammar::createDiscountedCumulativeRewardFormula, phoenix::ref(*
this), qi::_2, qi::_1)];
185 discountedCumulativeRewardFormula.name(
"discounted cumulative reward formula");
186 longRunAverageRewardFormula = (qi::lit(
"LRA") | qi::lit(
"S") |
187 qi::lit(
"MP"))[qi::_val = phoenix::bind(&FormulaParserGrammar::createLongRunAverageRewardFormula, phoenix::ref(*
this))];
188 longRunAverageRewardFormula.name(
"long run average reward formula");
189 instantaneousRewardFormula =
190 (qi::lit(
"I=") > expressionParser)[qi::_val = phoenix::bind(&FormulaParserGrammar::createInstantaneousRewardFormula, phoenix::ref(*
this), qi::_1)];
191 instantaneousRewardFormula.name(
"instantaneous reward formula");
192 cumulativeRewardFormula =
193 (qi::lit(
"C") >> timeBounds)[qi::_val = phoenix::bind(&FormulaParserGrammar::createCumulativeRewardFormula, phoenix::ref(*
this), qi::_1)];
194 cumulativeRewardFormula.name(
"cumulative reward formula");
195 totalRewardFormula = (qi::lit(
"C"))[qi::_val = phoenix::bind(&FormulaParserGrammar::createTotalRewardFormula, phoenix::ref(*
this))];
196 totalRewardFormula.name(
"total reward formula");
199 playerCoalition = (-((identifier[phoenix::push_back(qi::_a, qi::_1)] | qi::uint_[phoenix::push_back(qi::_a, qi::_1)]) %
200 ','))[qi::_val = phoenix::bind(&FormulaParserGrammar::createPlayerCoalition, phoenix::ref(*
this), qi::_a)];
201 playerCoalition.name(
"player coalition");
202 gameFormula = (qi::lit(
"<<") > playerCoalition > qi::lit(
">>") >
203 operatorFormula)[qi::_val = phoenix::bind(&FormulaParserGrammar::createGameFormula, phoenix::ref(*
this), qi::_1, qi::_2)];
204 gameFormula.name(
"game formula");
207 multiOperatorFormula = (qi::lit(
"multi") > qi::lit(
"(") > (operatorFormula % qi::lit(
",")) >
208 qi::lit(
")"))[qi::_val = phoenix::bind(&FormulaParserGrammar::createMultiOperatorFormula, phoenix::ref(*
this), qi::_1)];
209 multiOperatorFormula.name(
"multi-objective operator formula");
210 quantileBoundVariable = (-optimalityOperator_ >> identifier >>
211 qi::lit(
","))[qi::_val = phoenix::bind(&FormulaParserGrammar::createQuantileBoundVariables, phoenix::ref(*
this), qi::_1, qi::_2)];
212 quantileBoundVariable.name(
"quantile bound variable");
213 quantileFormula = (qi::lit(
"quantile") > qi::lit(
"(") > *(quantileBoundVariable) >
214 operatorFormula[qi::_pass = phoenix::bind(&FormulaParserGrammar::isBooleanReturnType, phoenix::ref(*
this), qi::_1,
true)] >
215 qi::lit(
")"))[qi::_val = phoenix::bind(&FormulaParserGrammar::createQuantileFormula, phoenix::ref(*
this), qi::_1, qi::_2)];
216 quantileFormula.name(
"Quantile formula");
219 formula = (isPathFormula(qi::_r1) >> pathFormula(qi::_r2) | propositionalFormula(qi::_r1, qi::_r2));
220 formula.name(
"formula");
223 topLevelFormula.name(
"top-level formula");
225 formulaName = qi::lit(
"\"") >> identifier >> qi::lit(
"\"") >> qi::lit(
":");
226 formulaName.name(
"formula name");
231 identifier >> -(qi::lit(
"=") > expressionParser))[phoenix::bind(&FormulaParserGrammar::addConstant, phoenix::ref(*
this), qi::_1, qi::_a, qi::_2)];
232 constantDefinition.name(
"constant definition");
234#pragma clang diagnostic push
235#pragma clang diagnostic ignored "-Woverloaded-shift-op-parentheses"
238 (-formulaName >> qi::lit(
"filter") > qi::lit(
"(") > filterType_ > qi::lit(
",") > topLevelFormula > qi::lit(
",") >
240 qi::lit(
")"))[qi::_val = phoenix::bind(&FormulaParserGrammar::createProperty, phoenix::ref(*
this), qi::_1, qi::_2, qi::_3, qi::_4)] |
242 topLevelFormula)[qi::_val = phoenix::bind(&FormulaParserGrammar::createPropertyWithDefaultFilterTypeAndStates, phoenix::ref(*
this), qi::_1, qi::_2)];
243 filterProperty.name(
"filter property");
245#pragma clang diagnostic pop
247 start = (qi::eps >> filterProperty[phoenix::push_back(qi::_val, qi::_1)] |
248 qi::eps(phoenix::bind(&FormulaParserGrammar::areConstantDefinitionsAllowed, phoenix::ref(*
this))) >> constantDefinition | qi::eps) %
249 +(qi::char_(
"\n;")) >>
250 qi::skip(storm::spirit_encoding::space_type() | qi::lit(
"//") >> *(qi::char_ - (qi::eol | qi::eoi)))[qi::eps] >> qi::eoi;
294 qi::on_error<qi::fail>(rewardModelName, handler(qi::_1, qi::_2, qi::_3, qi::_4));
295 qi::on_error<qi::fail>(operatorFormula, handler(qi::_1, qi::_2, qi::_3, qi::_4));
296 qi::on_error<qi::fail>(labelFormula, handler(qi::_1, qi::_2, qi::_3, qi::_4));
297 qi::on_error<qi::fail>(expressionFormula, handler(qi::_1, qi::_2, qi::_3, qi::_4));
298 qi::on_error<qi::fail>(basicPropositionalFormula, handler(qi::_1, qi::_2, qi::_3, qi::_4));
299 qi::on_error<qi::fail>(negationPropositionalFormula, handler(qi::_1, qi::_2, qi::_3, qi::_4));
300 qi::on_error<qi::fail>(andLevelPropositionalFormula, handler(qi::_1, qi::_2, qi::_3, qi::_4));
301 qi::on_error<qi::fail>(orLevelPropositionalFormula, handler(qi::_1, qi::_2, qi::_3, qi::_4));
302 qi::on_error<qi::fail>(propositionalFormula, handler(qi::_1, qi::_2, qi::_3, qi::_4));
303 qi::on_error<qi::fail>(timeBoundReference, handler(qi::_1, qi::_2, qi::_3, qi::_4));
304 qi::on_error<qi::fail>(timeBound, handler(qi::_1, qi::_2, qi::_3, qi::_4));
305 qi::on_error<qi::fail>(timeBounds, handler(qi::_1, qi::_2, qi::_3, qi::_4));
306 qi::on_error<qi::fail>(eventuallyFormula, handler(qi::_1, qi::_2, qi::_3, qi::_4));
307 qi::on_error<qi::fail>(nextFormula, handler(qi::_1, qi::_2, qi::_3, qi::_4));
308 qi::on_error<qi::fail>(globallyFormula, handler(qi::_1, qi::_2, qi::_3, qi::_4));
309 qi::on_error<qi::fail>(hoaPathFormula, handler(qi::_1, qi::_2, qi::_3, qi::_4));
310 qi::on_error<qi::fail>(multiBoundedPathFormula, handler(qi::_1, qi::_2, qi::_3, qi::_4));
311 qi::on_error<qi::fail>(prefixOperatorPathFormula, handler(qi::_1, qi::_2, qi::_3, qi::_4));
312 qi::on_error<qi::fail>(basicPathFormula, handler(qi::_1, qi::_2, qi::_3, qi::_4));
313 qi::on_error<qi::fail>(untilLevelPathFormula, handler(qi::_1, qi::_2, qi::_3, qi::_4));
314 qi::on_error<qi::fail>(pathFormula, handler(qi::_1, qi::_2, qi::_3, qi::_4));
315 qi::on_error<qi::fail>(longRunAverageRewardFormula, handler(qi::_1, qi::_2, qi::_3, qi::_4));
316 qi::on_error<qi::fail>(instantaneousRewardFormula, handler(qi::_1, qi::_2, qi::_3, qi::_4));
317 qi::on_error<qi::fail>(cumulativeRewardFormula, handler(qi::_1, qi::_2, qi::_3, qi::_4));
318 qi::on_error<qi::fail>(totalRewardFormula, handler(qi::_1, qi::_2, qi::_3, qi::_4));
319 qi::on_error<qi::fail>(discountedCumulativeRewardFormula, handler(qi::_1, qi::_2, qi::_3, qi::_4));
320 qi::on_error<qi::fail>(discountedTotalRewardFormula, handler(qi::_1, qi::_2, qi::_3, qi::_4));
321 qi::on_error<qi::fail>(playerCoalition, handler(qi::_1, qi::_2, qi::_3, qi::_4));
322 qi::on_error<qi::fail>(gameFormula, handler(qi::_1, qi::_2, qi::_3, qi::_4));
323 qi::on_error<qi::fail>(multiOperatorFormula, handler(qi::_1, qi::_2, qi::_3, qi::_4));
324 qi::on_error<qi::fail>(quantileBoundVariable, handler(qi::_1, qi::_2, qi::_3, qi::_4));
325 qi::on_error<qi::fail>(quantileFormula, handler(qi::_1, qi::_2, qi::_3, qi::_4));
326 qi::on_error<qi::fail>(formula, handler(qi::_1, qi::_2, qi::_3, qi::_4));
327 qi::on_error<qi::fail>(topLevelFormula, handler(qi::_1, qi::_2, qi::_3, qi::_4));
328 qi::on_error<qi::fail>(formulaName, handler(qi::_1, qi::_2, qi::_3, qi::_4));
329 qi::on_error<qi::fail>(filterProperty, handler(qi::_1, qi::_2, qi::_3, qi::_4));
330 qi::on_error<qi::fail>(constantDefinition, handler(qi::_1, qi::_2, qi::_3, qi::_4));
331 qi::on_error<qi::fail>(start, handler(qi::_1, qi::_2, qi::_3, qi::_4));
336 "Identifier `" << identifier <<
"' coincides with a reserved keyword or operator. Property expressions using the variable or constant '"
337 << identifier <<
"' will not be parsed correctly.");
339 "Identifier `" << identifier <<
"' coincides with a reserved keyword or operator. Property expressions using the variable or constant '"
340 << identifier <<
"' might not be parsed correctly.");
341 this->identifiers_.add(identifier, expression);
344void FormulaParserGrammar::addConstant(std::string
const& name,
ConstantDataType type, boost::optional<storm::expressions::Expression>
const& expression) {
345 STORM_LOG_ASSERT(manager,
"Mutable expression manager required to define new constants.");
347 STORM_LOG_THROW(!manager->hasVariable(name), storm::exceptions::WrongFormatException,
348 "Invalid constant definition '" << name <<
"' in property: variable already exists.");
351 newVariable = manager->declareBooleanVariable(name);
353 newVariable = manager->declareIntegerVariable(name);
355 newVariable = manager->declareRationalVariable(name);
361 undefinedConstants.insert(newVariable);
366bool FormulaParserGrammar::areConstantDefinitionsAllowed()
const {
367 return static_cast<bool>(manager);
370std::shared_ptr<storm::logic::TimeBoundReference> FormulaParserGrammar::createTimeBoundReference(
storm::logic::TimeBoundType const& type,
371 boost::optional<std::string>
const& rewardModelName)
const {
373 return std::make_shared<storm::logic::TimeBoundReference>(rewardModelName);
375 return std::make_shared<storm::logic::TimeBoundReference>(type);
379std::tuple<boost::optional<storm::logic::TimeBound>, boost::optional<storm::logic::TimeBound>, std::shared_ptr<storm::logic::TimeBoundReference>>
381 std::shared_ptr<storm::logic::TimeBoundReference>
const& timeBoundReference)
const {
386 return std::make_tuple(lower, upper, timeBoundReference);
389std::tuple<boost::optional<storm::logic::TimeBound>, boost::optional<storm::logic::TimeBound>, std::shared_ptr<storm::logic::TimeBoundReference>>
391 std::shared_ptr<storm::logic::TimeBoundReference>
const& timeBoundReference)
const {
400std::shared_ptr<storm::logic::Formula const> FormulaParserGrammar::createInstantaneousRewardFormula(
storm::expressions::Expression const& timeBound)
const {
404std::shared_ptr<storm::logic::Formula const> FormulaParserGrammar::createCumulativeRewardFormula(
405 std::vector<std::tuple<boost::optional<storm::logic::TimeBound>, boost::optional<storm::logic::TimeBound>,
406 std::shared_ptr<storm::logic::TimeBoundReference>>>
const& timeBounds)
const {
407 std::vector<storm::logic::TimeBound> bounds;
408 std::vector<storm::logic::TimeBoundReference> timeBoundReferences;
409 for (
auto const& timeBound : timeBounds) {
410 STORM_LOG_THROW(!std::get<0>(timeBound), storm::exceptions::WrongFormatException,
"Cumulative reward formulas with lower time bound are not allowed.");
411 STORM_LOG_THROW(std::get<1>(timeBound), storm::exceptions::WrongFormatException,
"Cumulative reward formulas require an upper bound.");
412 bounds.push_back(std::get<1>(timeBound).get());
413 timeBoundReferences.emplace_back(*std::get<2>(timeBound));
418std::shared_ptr<storm::logic::Formula const> FormulaParserGrammar::createDiscountedCumulativeRewardFormula(
420 std::vector<std::tuple<boost::optional<storm::logic::TimeBound>, boost::optional<storm::logic::TimeBound>,
421 std::shared_ptr<storm::logic::TimeBoundReference>>>
const& timeBounds)
const {
422 std::vector<storm::logic::TimeBound> bounds;
423 std::vector<storm::logic::TimeBoundReference> timeBoundReferences;
424 for (
auto const& timeBound : timeBounds) {
425 STORM_LOG_THROW(!std::get<0>(timeBound), storm::exceptions::WrongFormatException,
"Cumulative reward formulas with lower time bound are not allowed.");
426 STORM_LOG_THROW(std::get<1>(timeBound), storm::exceptions::WrongFormatException,
"Cumulative reward formulas require an upper bound.");
427 bounds.push_back(std::get<1>(timeBound).get());
428 timeBoundReferences.emplace_back(*std::get<2>(timeBound));
433std::shared_ptr<storm::logic::Formula const> FormulaParserGrammar::createTotalRewardFormula()
const {
437std::shared_ptr<storm::logic::Formula const> FormulaParserGrammar::createDiscountedTotalRewardFormula(
442std::shared_ptr<storm::logic::Formula const> FormulaParserGrammar::createLongRunAverageRewardFormula()
const {
446std::shared_ptr<storm::logic::Formula const> FormulaParserGrammar::createAtomicExpressionFormula(
storm::expressions::Expression const& expression)
const {
448 "Expected expression " + expression.
toString() +
" to be of boolean type.");
455std::shared_ptr<storm::logic::Formula const> FormulaParserGrammar::createBooleanLiteralFormula(
bool literal)
const {
459std::shared_ptr<storm::logic::Formula const> FormulaParserGrammar::createAtomicLabelFormula(std::string
const& label)
const {
463std::shared_ptr<storm::logic::Formula const> FormulaParserGrammar::createEventuallyFormula(
464 boost::optional<std::vector<std::tuple<boost::optional<storm::logic::TimeBound>, boost::optional<storm::logic::TimeBound>,
465 std::shared_ptr<storm::logic::TimeBoundReference>>>>
const& timeBounds,
467 if (timeBounds && !timeBounds.get().empty()) {
468 std::vector<boost::optional<storm::logic::TimeBound>> lowerBounds, upperBounds;
469 std::vector<storm::logic::TimeBoundReference> timeBoundReferences;
470 for (
auto const& timeBound : timeBounds.get()) {
471 lowerBounds.push_back(std::get<0>(timeBound));
472 upperBounds.push_back(std::get<1>(timeBound));
473 timeBoundReferences.emplace_back(*std::get<2>(timeBound));
475 return std::shared_ptr<storm::logic::Formula const>(
482std::shared_ptr<storm::logic::Formula const> FormulaParserGrammar::createGloballyFormula(std::shared_ptr<storm::logic::Formula const>
const& subformula)
const {
486std::shared_ptr<storm::logic::Formula const> FormulaParserGrammar::createNextFormula(std::shared_ptr<storm::logic::Formula const>
const& subformula)
const {
490std::shared_ptr<storm::logic::Formula const> FormulaParserGrammar::createUntilFormula(
491 std::shared_ptr<storm::logic::Formula const>
const& leftSubformula,
492 boost::optional<std::vector<std::tuple<boost::optional<storm::logic::TimeBound>, boost::optional<storm::logic::TimeBound>,
493 std::shared_ptr<storm::logic::TimeBoundReference>>>>
const& timeBounds,
494 std::shared_ptr<storm::logic::Formula const>
const& rightSubformula) {
495 if (timeBounds && !timeBounds.get().empty()) {
496 std::vector<boost::optional<storm::logic::TimeBound>> lowerBounds, upperBounds;
497 std::vector<storm::logic::TimeBoundReference> timeBoundReferences;
498 for (
auto const& timeBound : timeBounds.get()) {
499 lowerBounds.push_back(std::get<0>(timeBound));
500 upperBounds.push_back(std::get<1>(timeBound));
501 timeBoundReferences.emplace_back(*std::get<2>(timeBound));
503 return std::shared_ptr<storm::logic::Formula const>(
510std::shared_ptr<storm::logic::Formula const> FormulaParserGrammar::createHOAPathFormula(std::string
const& automatonFile)
const {
514void FormulaParserGrammar::addHoaAPMapping(
storm::logic::Formula const& hoaFormula,
const std::string& ap,
515 std::shared_ptr<storm::logic::Formula const>& expression)
const {
522std::shared_ptr<storm::logic::Formula const> FormulaParserGrammar::createConditionalFormula(
523 std::shared_ptr<storm::logic::Formula const>
const& leftSubformula, boost::optional<std::shared_ptr<storm::logic::Formula const>>
const& rightSubformula,
525 if (rightSubformula) {
529 return leftSubformula;
534 boost::optional<storm::logic::ComparisonType>
const& comparisonType,
535 boost::optional<storm::expressions::Expression>
const& threshold)
const {
536 if (comparisonType && threshold) {
545 boost::optional<std::string>
const& rewardModelName,
547 std::shared_ptr<storm::logic::Formula const>
const& subformula) {
550 STORM_LOG_ASSERT(!rewardModelName,
"Probability operator with reward information parsed");
551 return createProbabilityOperatorFormula(operatorInformation, subformula);
553 return createRewardOperatorFormula(rewardModelName, operatorInformation, subformula);
555 STORM_LOG_ASSERT(!rewardModelName,
"LRA operator with reward information parsed");
556 return createLongRunAverageOperatorFormula(operatorInformation, subformula);
558 STORM_LOG_ASSERT(!rewardModelName,
"Time operator with reward model name parsed");
559 return createTimeOperatorFormula(operatorInformation, subformula);
561 STORM_LOG_THROW(
false, storm::exceptions::WrongFormatException,
"Unexpected formula context.");
565std::shared_ptr<storm::logic::Formula const> FormulaParserGrammar::createLongRunAverageOperatorFormula(
570std::shared_ptr<storm::logic::Formula const> FormulaParserGrammar::createRewardOperatorFormula(
572 std::shared_ptr<storm::logic::Formula const>
const& subformula)
const {
576std::shared_ptr<storm::logic::Formula const> FormulaParserGrammar::createTimeOperatorFormula(
581std::shared_ptr<storm::logic::Formula const> FormulaParserGrammar::createProbabilityOperatorFormula(
586std::shared_ptr<storm::logic::Formula const> FormulaParserGrammar::createBinaryBooleanStateFormula(
587 std::shared_ptr<storm::logic::Formula const>
const& leftSubformula, std::shared_ptr<storm::logic::Formula const>
const& rightSubformula,
592std::shared_ptr<storm::logic::Formula const> FormulaParserGrammar::createUnaryBooleanStateFormula(
593 std::shared_ptr<storm::logic::Formula const>
const& subformula, boost::optional<storm::logic::UnaryBooleanStateFormula::OperatorType>
const& operatorType) {
601std::shared_ptr<storm::logic::Formula const> FormulaParserGrammar::createBinaryBooleanPathFormula(
602 std::shared_ptr<storm::logic::Formula const>
const& leftSubformula, std::shared_ptr<storm::logic::Formula const>
const& rightSubformula,
607std::shared_ptr<storm::logic::Formula const> FormulaParserGrammar::createUnaryBooleanPathFormula(
608 std::shared_ptr<storm::logic::Formula const>
const& subformula, boost::optional<storm::logic::UnaryBooleanPathFormula::OperatorType>
const& operatorType) {
616std::shared_ptr<storm::logic::Formula const> FormulaParserGrammar::createBinaryBooleanStateOrPathFormula(
617 std::shared_ptr<storm::logic::Formula const>
const& leftSubformula, std::shared_ptr<storm::logic::Formula const>
const& rightSubformula,
619 if (leftSubformula->isStateFormula() && rightSubformula->isStateFormula()) {
620 return createBinaryBooleanStateFormula(leftSubformula, rightSubformula, operatorType);
621 }
else if (leftSubformula->isPathFormula() || rightSubformula->isPathFormula()) {
622 return createBinaryBooleanPathFormula(leftSubformula, rightSubformula, operatorType);
624 STORM_LOG_THROW(
false, storm::exceptions::WrongFormatException,
"Subformulas have unexpected type.");
627std::shared_ptr<storm::logic::Formula const> FormulaParserGrammar::createUnaryBooleanStateOrPathFormula(
628 std::shared_ptr<storm::logic::Formula const>
const& subformula, boost::optional<storm::logic::UnaryBooleanOperatorType>
const& operatorType) {
629 if (subformula->isStateFormula()) {
630 return createUnaryBooleanStateFormula(subformula, operatorType);
631 }
else if (subformula->isPathFormula()) {
632 return createUnaryBooleanPathFormula(subformula, operatorType);
634 STORM_LOG_THROW(
false, storm::exceptions::WrongFormatException,
"Subformulas have unexpected type.");
637bool FormulaParserGrammar::isValidMultiBoundedPathFormulaOperand(std::shared_ptr<storm::logic::Formula const>
const& operand) {
638 if (operand->isBoundedUntilFormula()) {
639 if (!operand->asBoundedUntilFormula().isMultiDimensional()) {
642 STORM_LOG_ERROR(
"Composition of multidimensional bounded until formula must consist of single dimension subformulas. Got '" << *operand
648std::shared_ptr<storm::logic::Formula const> FormulaParserGrammar::createMultiBoundedPathFormula(
649 std::vector<std::shared_ptr<storm::logic::Formula const>>
const& subformulas) {
650 std::vector<std::shared_ptr<storm::logic::Formula const>> leftSubformulas, rightSubformulas;
651 std::vector<boost::optional<storm::logic::TimeBound>> lowerBounds, upperBounds;
652 std::vector<storm::logic::TimeBoundReference> timeBoundReferences;
653 for (
auto const& subformula : subformulas) {
654 STORM_LOG_THROW(subformula->isBoundedUntilFormula(), storm::exceptions::WrongFormatException,
655 "multi-path formulas require bounded until (or eventually) subformulae. Got '" << *subformula <<
"' instead.");
656 auto const& f = subformula->asBoundedUntilFormula();
657 STORM_LOG_THROW(!f.isMultiDimensional(), storm::exceptions::WrongFormatException,
658 "Composition of multidimensional bounded until formula must consist of single dimension subformulas. Got '" << f <<
"' instead.");
659 leftSubformulas.push_back(f.getLeftSubformula().asSharedPointer());
660 rightSubformulas.push_back(f.getRightSubformula().asSharedPointer());
661 if (f.hasLowerBound()) {
664 lowerBounds.emplace_back();
666 if (f.hasUpperBound()) {
669 upperBounds.emplace_back();
671 timeBoundReferences.push_back(f.getTimeBoundReference());
673 return std::shared_ptr<storm::logic::Formula const>(
677std::shared_ptr<storm::logic::Formula const> FormulaParserGrammar::createMultiOperatorFormula(
678 std::vector<std::shared_ptr<storm::logic::Formula const>>
const& subformulas) {
682storm::expressions::Variable FormulaParserGrammar::createQuantileBoundVariables(boost::optional<storm::solver::OptimizationDirection>
const& dir,
683 std::string
const& variableName) {
684 STORM_LOG_ASSERT(manager,
"Mutable expression manager required to define quantile bound variable.");
686 if (manager->hasVariable(variableName)) {
687 var = manager->getVariable(variableName);
688 STORM_LOG_THROW(quantileFormulaVariables.count(var) > 0, storm::exceptions::WrongFormatException,
689 "Invalid quantile variable name '" << variableName <<
"' in quantile formula: variable already exists.");
691 var = manager->declareRationalVariable(variableName);
692 quantileFormulaVariables.insert(var);
695 << dir.get() <<
"' for quantile variable " << variableName
696 <<
" is ignored. This information will be derived from the subformula of the quantile.");
701std::shared_ptr<storm::logic::Formula const> FormulaParserGrammar::createQuantileFormula(std::vector<storm::expressions::Variable>
const& boundVariables,
702 std::shared_ptr<storm::logic::Formula const>
const& subformula) {
706std::set<storm::expressions::Variable> FormulaParserGrammar::getUndefinedConstants(std::shared_ptr<storm::logic::Formula const>
const& formula)
const {
707 std::set<storm::expressions::Variable> result;
708 std::set<storm::expressions::Variable> usedVariables = formula->getUsedVariables();
709 std::set_intersection(usedVariables.begin(), usedVariables.end(), undefinedConstants.begin(), undefinedConstants.end(),
710 std::inserter(result, result.begin()));
715 std::shared_ptr<storm::logic::Formula const>
const& formula,
716 std::shared_ptr<storm::logic::Formula const>
const& states) {
721 return storm::jani::Property(propertyName.get(), filterExpression, this->getUndefinedConstants(formula));
723 return storm::jani::Property(std::to_string(propertyCount - 1), filterExpression, this->getUndefinedConstants(formula));
727storm::jani::Property FormulaParserGrammar::createPropertyWithDefaultFilterTypeAndStates(boost::optional<std::string>
const& propertyName,
728 std::shared_ptr<storm::logic::Formula const>
const& formula) {
733 return storm::jani::Property(std::to_string(propertyCount), formula, this->getUndefinedConstants(formula));
738 std::vector<std::variant<std::string, storm::storage::PlayerIndex>>
const& playerIds)
const {
743 std::shared_ptr<storm::logic::Formula const>
const& subformula)
const {
747bool FormulaParserGrammar::isBooleanReturnType(std::shared_ptr<storm::logic::Formula const>
const& formula,
bool raiseErrorMessage) {
748 if (formula->hasQualitativeResult()) {
751 STORM_LOG_ERROR_COND(!raiseErrorMessage,
"Formula " << *formula <<
" does not have a Boolean return type.");
755bool FormulaParserGrammar::raiseAmbiguousNonAssociativeOperatorError(std::shared_ptr<storm::logic::Formula const>
const& formula, std::string
const& op) {
756 STORM_LOG_ERROR(
"Ambiguous use of non-associative operator '" << op <<
"' in formula '" << *formula <<
" U ... '");
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.
void setIdentifierMapping(qi::symbols< char, storm::expressions::Expression > const *identifiers_)
Sets an identifier mapping that is used to determine valid variables in the expression.
#define STORM_LOG_ERROR(message)
#define STORM_LOG_ASSERT(cond, message)
#define STORM_LOG_WARN_COND(cond, message)
#define STORM_LOG_ERROR_COND(cond, message)
#define STORM_LOG_THROW(cond, exception, message)
BinaryBooleanOperatorType