12 :
FormulaParserGrammar::base_type(start), constManager(manager), manager(nullptr), expressionParser(*manager, keywords_, true, true), propertyCount(0) {
17 :
FormulaParserGrammar::base_type(start), constManager(manager), manager(manager), expressionParser(*manager, keywords_, true, true), propertyCount(0) {
25void FormulaParserGrammar::initialize() {
27 for (
auto variableTypePair : *constManager) {
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");
41 isPathFormula = qi::eps(qi::_r1 == FormulaKind::Path);
42 noAmbiguousNonAssociativeOperator =
43 !(qi::lit(qi::_r2)[qi::_pass = phoenix::bind(&FormulaParserGrammar::raiseAmbiguousNonAssociativeOperatorError, phoenix::ref(*
this), qi::_r1, qi::_r2)]);
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_(
'_')))]]];
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");
54 (-optimalityOperator_)[qi::_a = qi::_1] >>
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");
63 (longRunAverageRewardFormula | eventuallyFormula(qi::_r1) | discountedCumulativeRewardFormula | discountedTotalRewardFormula |
64 cumulativeRewardFormula | instantaneousRewardFormula | totalRewardFormula)) |
69 phoenix::ref(*
this), qi::_1, qi::_2, qi::_r1)];
70 operatorSubFormula.name(
"operator subformula");
72 rewardModelName.name(
"reward model name");
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");
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");
85 basicPropositionalFormula =
89 (formula(qi::_r1, qi::_r2) > qi::lit(
")")))
90 | labelFormula | negationPropositionalFormula(qi::_r1, qi::_r2) | operatorFormula |
91 (isPathFormula(qi::_r1) >> prefixOperatorPathFormula(qi::_r2))
92 | multiLexOperatorFormula
93 | multiOperatorFormula
94 | quantileFormula | gameFormula;
99 negationPropositionalFormula =
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(
"|"))
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);
121 basicPathFormula = propositionalFormula(FormulaKind::Path, qi::_r1)
122 | prefixOperatorPathFormula(
124 prefixOperatorPathFormula =
125 eventuallyFormula(qi::_r1) | nextFormula(qi::_r1) | globallyFormula(qi::_r1) | hoaPathFormula(qi::_r1) | multiBoundedPathFormula(qi::_r1);
126 basicPathFormula.name(
"basic path formula");
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,
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");
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");
157 (qi::lit(
"G") > basicPathFormula(qi::_r1))[qi::_val = phoenix::bind(&FormulaParserGrammar::createGloballyFormula, phoenix::ref(*
this), qi::_1)];
158 globallyFormula.name(
"globally formula");
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)] >
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 untilLevelPathFormula =
171 basicPathFormula(qi::_r1)[qi::_val = qi::_1] >>
172 -((qi::lit(
"U") > (-timeBounds) >
173 basicPathFormula(qi::_r1))[qi::_val = phoenix::bind(&FormulaParserGrammar::createUntilFormula, phoenix::ref(*
this), qi::_val, qi::_1, qi::_2)]) >>
174 (qi::eps > noAmbiguousNonAssociativeOperator(qi::_val, std::string(
"U")));
175 untilLevelPathFormula.name(
"until precedence level path formula");
176 pathFormula = untilLevelPathFormula(qi::_r1);
177 pathFormula.name(
"path formula");
180 discountedTotalRewardFormula =
181 (qi::lit(
"Cdiscount=") >>
182 expressionParser)[qi::_val = phoenix::bind(&FormulaParserGrammar::createDiscountedTotalRewardFormula, phoenix::ref(*
this), qi::_1)];
183 discountedTotalRewardFormula.name(
"discounted total reward formula");
184 discountedCumulativeRewardFormula =
185 (qi::lit(
"C") >> timeBounds >> qi::lit(
"discount=") >>
186 expressionParser)[qi::_val = phoenix::bind(&FormulaParserGrammar::createDiscountedCumulativeRewardFormula, phoenix::ref(*
this), qi::_2, qi::_1)];
187 discountedCumulativeRewardFormula.name(
"discounted cumulative reward formula");
188 longRunAverageRewardFormula = (qi::lit(
"LRA") | qi::lit(
"S") |
189 qi::lit(
"MP"))[qi::_val = phoenix::bind(&FormulaParserGrammar::createLongRunAverageRewardFormula, phoenix::ref(*
this))];
190 longRunAverageRewardFormula.name(
"long run average reward formula");
191 instantaneousRewardFormula =
192 (qi::lit(
"I=") > expressionParser)[qi::_val = phoenix::bind(&FormulaParserGrammar::createInstantaneousRewardFormula, phoenix::ref(*
this), qi::_1)];
193 instantaneousRewardFormula.name(
"instantaneous reward formula");
194 cumulativeRewardFormula =
195 (qi::lit(
"C") >> timeBounds)[qi::_val = phoenix::bind(&FormulaParserGrammar::createCumulativeRewardFormula, phoenix::ref(*
this), qi::_1)];
196 cumulativeRewardFormula.name(
"cumulative reward formula");
197 totalRewardFormula = (qi::lit(
"C"))[qi::_val = phoenix::bind(&FormulaParserGrammar::createTotalRewardFormula, phoenix::ref(*
this))];
198 totalRewardFormula.name(
"total reward formula");
201 playerCoalition = (-((identifier[phoenix::push_back(qi::_a, qi::_1)] | qi::uint_[phoenix::push_back(qi::_a, qi::_1)]) %
202 ','))[qi::_val = phoenix::bind(&FormulaParserGrammar::createPlayerCoalition, phoenix::ref(*
this), qi::_a)];
203 playerCoalition.name(
"player coalition");
204 gameFormula = (qi::lit(
"<<") > playerCoalition > qi::lit(
">>") >
205 operatorFormula)[qi::_val = phoenix::bind(&FormulaParserGrammar::createGameFormula, phoenix::ref(*
this), qi::_1, qi::_2)];
206 gameFormula.name(
"game formula");
209 multiOperatorFormula = (qi::lit(
"multi") > qi::lit(
"(") > (operatorFormula % qi::lit(
",")) >
210 qi::lit(
")"))[qi::_val = phoenix::bind(&FormulaParserGrammar::createMultiOperatorFormula, phoenix::ref(*
this), qi::_1,
212 multiOperatorFormula.name(
"multi-objective operator formula");
213 multiLexOperatorFormula = (qi::lit(
"multilex") > qi::lit(
"(") > (operatorFormula % qi::lit(
",")) >
214 qi::lit(
")"))[qi::_val = phoenix::bind(&FormulaParserGrammar::createMultiOperatorFormula, phoenix::ref(*
this), qi::_1,
216 multiLexOperatorFormula.name(
"multi-objective lexicographic operator formula");
217 quantileBoundVariable = (-optimalityOperator_ >> identifier >>
218 qi::lit(
","))[qi::_val = phoenix::bind(&FormulaParserGrammar::createQuantileBoundVariables, phoenix::ref(*
this), qi::_1, qi::_2)];
219 quantileBoundVariable.name(
"quantile bound variable");
220 quantileFormula = (qi::lit(
"quantile") > qi::lit(
"(") > *(quantileBoundVariable) >
221 operatorFormula[qi::_pass = phoenix::bind(&FormulaParserGrammar::isBooleanReturnType, phoenix::ref(*
this), qi::_1,
true)] >
222 qi::lit(
")"))[qi::_val = phoenix::bind(&FormulaParserGrammar::createQuantileFormula, phoenix::ref(*
this), qi::_1, qi::_2)];
223 quantileFormula.name(
"Quantile formula");
226 formula = (isPathFormula(qi::_r1) >> pathFormula(qi::_r2) | propositionalFormula(qi::_r1, qi::_r2));
227 formula.name(
"formula");
230 topLevelFormula.name(
"top-level formula");
232 formulaName = qi::lit(
"\"") >> identifier >> qi::lit(
"\"") >> qi::lit(
":");
233 formulaName.name(
"formula name");
238 identifier >> -(qi::lit(
"=") > expressionParser))[phoenix::bind(&FormulaParserGrammar::addConstant, phoenix::ref(*
this), qi::_1, qi::_a, qi::_2)];
239 constantDefinition.name(
"constant definition");
241#pragma clang diagnostic push
242#pragma clang diagnostic ignored "-Woverloaded-shift-op-parentheses"
245 (-formulaName >> qi::lit(
"filter") > qi::lit(
"(") > filterType_ > qi::lit(
",") > topLevelFormula > qi::lit(
",") >
247 qi::lit(
")"))[qi::_val = phoenix::bind(&FormulaParserGrammar::createProperty, phoenix::ref(*
this), qi::_1, qi::_2, qi::_3, qi::_4)] |
249 topLevelFormula)[qi::_val = phoenix::bind(&FormulaParserGrammar::createPropertyWithDefaultFilterTypeAndStates, phoenix::ref(*
this), qi::_1, qi::_2)];
250 filterProperty.name(
"filter property");
252#pragma clang diagnostic pop
254 start = (qi::eps >> filterProperty[phoenix::push_back(qi::_val, qi::_1)] |
255 qi::eps(phoenix::bind(&FormulaParserGrammar::areConstantDefinitionsAllowed, phoenix::ref(*
this))) >> constantDefinition | qi::eps) %
256 +(qi::char_(
"\n;")) >>
257 qi::skip(storm::spirit_encoding::space_type() | qi::lit(
"//") >> *(qi::char_ - (qi::eol | qi::eoi)))[qi::eps] >> qi::eoi;
302 qi::on_error<qi::fail>(rewardModelName, handler(qi::_1, qi::_2, qi::_3, qi::_4));
303 qi::on_error<qi::fail>(operatorFormula, handler(qi::_1, qi::_2, qi::_3, qi::_4));
304 qi::on_error<qi::fail>(labelFormula, handler(qi::_1, qi::_2, qi::_3, qi::_4));
305 qi::on_error<qi::fail>(expressionFormula, handler(qi::_1, qi::_2, qi::_3, qi::_4));
306 qi::on_error<qi::fail>(basicPropositionalFormula, handler(qi::_1, qi::_2, qi::_3, qi::_4));
307 qi::on_error<qi::fail>(negationPropositionalFormula, handler(qi::_1, qi::_2, qi::_3, qi::_4));
308 qi::on_error<qi::fail>(andLevelPropositionalFormula, handler(qi::_1, qi::_2, qi::_3, qi::_4));
309 qi::on_error<qi::fail>(orLevelPropositionalFormula, handler(qi::_1, qi::_2, qi::_3, qi::_4));
310 qi::on_error<qi::fail>(propositionalFormula, handler(qi::_1, qi::_2, qi::_3, qi::_4));
311 qi::on_error<qi::fail>(timeBoundReference, handler(qi::_1, qi::_2, qi::_3, qi::_4));
312 qi::on_error<qi::fail>(timeBound, handler(qi::_1, qi::_2, qi::_3, qi::_4));
313 qi::on_error<qi::fail>(timeBounds, handler(qi::_1, qi::_2, qi::_3, qi::_4));
314 qi::on_error<qi::fail>(eventuallyFormula, handler(qi::_1, qi::_2, qi::_3, qi::_4));
315 qi::on_error<qi::fail>(nextFormula, handler(qi::_1, qi::_2, qi::_3, qi::_4));
316 qi::on_error<qi::fail>(globallyFormula, handler(qi::_1, qi::_2, qi::_3, qi::_4));
317 qi::on_error<qi::fail>(hoaPathFormula, handler(qi::_1, qi::_2, qi::_3, qi::_4));
318 qi::on_error<qi::fail>(multiBoundedPathFormula, handler(qi::_1, qi::_2, qi::_3, qi::_4));
319 qi::on_error<qi::fail>(prefixOperatorPathFormula, handler(qi::_1, qi::_2, qi::_3, qi::_4));
320 qi::on_error<qi::fail>(basicPathFormula, handler(qi::_1, qi::_2, qi::_3, qi::_4));
321 qi::on_error<qi::fail>(untilLevelPathFormula, handler(qi::_1, qi::_2, qi::_3, qi::_4));
322 qi::on_error<qi::fail>(pathFormula, handler(qi::_1, qi::_2, qi::_3, qi::_4));
323 qi::on_error<qi::fail>(longRunAverageRewardFormula, handler(qi::_1, qi::_2, qi::_3, qi::_4));
324 qi::on_error<qi::fail>(instantaneousRewardFormula, handler(qi::_1, qi::_2, qi::_3, qi::_4));
325 qi::on_error<qi::fail>(cumulativeRewardFormula, handler(qi::_1, qi::_2, qi::_3, qi::_4));
326 qi::on_error<qi::fail>(totalRewardFormula, handler(qi::_1, qi::_2, qi::_3, qi::_4));
327 qi::on_error<qi::fail>(discountedCumulativeRewardFormula, handler(qi::_1, qi::_2, qi::_3, qi::_4));
328 qi::on_error<qi::fail>(discountedTotalRewardFormula, handler(qi::_1, qi::_2, qi::_3, qi::_4));
329 qi::on_error<qi::fail>(playerCoalition, handler(qi::_1, qi::_2, qi::_3, qi::_4));
330 qi::on_error<qi::fail>(gameFormula, handler(qi::_1, qi::_2, qi::_3, qi::_4));
331 qi::on_error<qi::fail>(multiOperatorFormula, handler(qi::_1, qi::_2, qi::_3, qi::_4));
332 qi::on_error<qi::fail>(multiLexOperatorFormula, handler(qi::_1, qi::_2, qi::_3, qi::_4));
333 qi::on_error<qi::fail>(quantileBoundVariable, handler(qi::_1, qi::_2, qi::_3, qi::_4));
334 qi::on_error<qi::fail>(quantileFormula, handler(qi::_1, qi::_2, qi::_3, qi::_4));
335 qi::on_error<qi::fail>(formula, handler(qi::_1, qi::_2, qi::_3, qi::_4));
336 qi::on_error<qi::fail>(topLevelFormula, handler(qi::_1, qi::_2, qi::_3, qi::_4));
337 qi::on_error<qi::fail>(formulaName, handler(qi::_1, qi::_2, qi::_3, qi::_4));
338 qi::on_error<qi::fail>(filterProperty, handler(qi::_1, qi::_2, qi::_3, qi::_4));
339 qi::on_error<qi::fail>(constantDefinition, handler(qi::_1, qi::_2, qi::_3, qi::_4));
340 qi::on_error<qi::fail>(start, handler(qi::_1, qi::_2, qi::_3, qi::_4));
345 "Identifier `" << identifier <<
"' coincides with a reserved keyword or operator. Property expressions using the variable or constant '"
346 << identifier <<
"' will not be parsed correctly.");
348 "Identifier `" << identifier <<
"' coincides with a reserved keyword or operator. Property expressions using the variable or constant '"
349 << identifier <<
"' might not be parsed correctly.");
350 this->identifiers_.add(identifier, expression);
353void FormulaParserGrammar::addConstant(std::string
const& name,
ConstantDataType type, boost::optional<storm::expressions::Expression>
const& expression) {
354 STORM_LOG_ASSERT(manager,
"Mutable expression manager required to define new constants.");
356 STORM_LOG_THROW(!manager->hasVariable(name), storm::exceptions::WrongFormatException,
357 "Invalid constant definition '" << name <<
"' in property: variable already exists.");
360 newVariable = manager->declareBooleanVariable(name);
362 newVariable = manager->declareIntegerVariable(name);
364 newVariable = manager->declareRationalVariable(name);
370 undefinedConstants.insert(newVariable);
375bool FormulaParserGrammar::areConstantDefinitionsAllowed()
const {
376 return static_cast<bool>(manager);
379std::shared_ptr<storm::logic::TimeBoundReference> FormulaParserGrammar::createTimeBoundReference(
storm::logic::TimeBoundType const& type,
380 boost::optional<std::string>
const& rewardModelName)
const {
382 return std::make_shared<storm::logic::TimeBoundReference>(rewardModelName);
384 return std::make_shared<storm::logic::TimeBoundReference>(type);
388std::tuple<boost::optional<storm::logic::TimeBound>, boost::optional<storm::logic::TimeBound>, std::shared_ptr<storm::logic::TimeBoundReference>>
389FormulaParserGrammar::createTimeBoundFromInterval(storm::expressions::Expression
const& lowerBound, storm::expressions::Expression
const& upperBound,
390 std::shared_ptr<storm::logic::TimeBoundReference>
const& timeBoundReference)
const {
393 storm::logic::TimeBound lower(
false, lowerBound);
394 storm::logic::TimeBound upper(
false, upperBound);
395 return std::make_tuple(lower, upper, timeBoundReference);
398std::tuple<boost::optional<storm::logic::TimeBound>, boost::optional<storm::logic::TimeBound>, std::shared_ptr<storm::logic::TimeBoundReference>>
399FormulaParserGrammar::createTimeBoundFromSingleBound(storm::expressions::Expression
const& bound,
bool upperBound,
bool strict,
400 std::shared_ptr<storm::logic::TimeBoundReference>
const& timeBoundReference)
const {
403 return std::make_tuple(boost::none, storm::logic::TimeBound(strict, bound), timeBoundReference);
405 return std::make_tuple(storm::logic::TimeBound(strict, bound), boost::none, timeBoundReference);
409std::shared_ptr<storm::logic::Formula const> FormulaParserGrammar::createInstantaneousRewardFormula(storm::expressions::Expression
const& timeBound)
const {
410 return std::shared_ptr<storm::logic::Formula const>(
new storm::logic::InstantaneousRewardFormula(timeBound));
413std::shared_ptr<storm::logic::Formula const> FormulaParserGrammar::createCumulativeRewardFormula(
414 std::vector<std::tuple<boost::optional<storm::logic::TimeBound>, boost::optional<storm::logic::TimeBound>,
415 std::shared_ptr<storm::logic::TimeBoundReference>>>
const& timeBounds)
const {
416 std::vector<storm::logic::TimeBound> bounds;
417 std::vector<storm::logic::TimeBoundReference> timeBoundReferences;
418 for (
auto const& timeBound : timeBounds) {
419 STORM_LOG_THROW(!std::get<0>(timeBound), storm::exceptions::WrongFormatException,
"Cumulative reward formulas with lower time bound are not allowed.");
420 STORM_LOG_THROW(std::get<1>(timeBound), storm::exceptions::WrongFormatException,
"Cumulative reward formulas require an upper bound.");
421 bounds.push_back(std::get<1>(timeBound).get());
422 timeBoundReferences.emplace_back(*std::get<2>(timeBound));
424 return std::shared_ptr<storm::logic::Formula const>(
new storm::logic::CumulativeRewardFormula(bounds, timeBoundReferences));
427std::shared_ptr<storm::logic::Formula const> FormulaParserGrammar::createDiscountedCumulativeRewardFormula(
428 storm::expressions::Expression
const& discountFactor,
429 std::vector<std::tuple<boost::optional<storm::logic::TimeBound>, boost::optional<storm::logic::TimeBound>,
430 std::shared_ptr<storm::logic::TimeBoundReference>>>
const& timeBounds)
const {
431 std::vector<storm::logic::TimeBound> bounds;
432 std::vector<storm::logic::TimeBoundReference> timeBoundReferences;
433 for (
auto const& timeBound : timeBounds) {
434 STORM_LOG_THROW(!std::get<0>(timeBound), storm::exceptions::WrongFormatException,
"Cumulative reward formulas with lower time bound are not allowed.");
435 STORM_LOG_THROW(std::get<1>(timeBound), storm::exceptions::WrongFormatException,
"Cumulative reward formulas require an upper bound.");
436 bounds.push_back(std::get<1>(timeBound).get());
437 timeBoundReferences.emplace_back(*std::get<2>(timeBound));
439 return std::shared_ptr<storm::logic::Formula const>(
new storm::logic::DiscountedCumulativeRewardFormula(discountFactor, bounds, timeBoundReferences));
442std::shared_ptr<storm::logic::Formula const> FormulaParserGrammar::createTotalRewardFormula()
const {
443 return std::shared_ptr<storm::logic::Formula const>(
new storm::logic::TotalRewardFormula());
446std::shared_ptr<storm::logic::Formula const> FormulaParserGrammar::createDiscountedTotalRewardFormula(
447 storm::expressions::Expression
const& discountFactor)
const {
448 return std::shared_ptr<storm::logic::Formula const>(
new storm::logic::DiscountedTotalRewardFormula(discountFactor));
451std::shared_ptr<storm::logic::Formula const> FormulaParserGrammar::createLongRunAverageRewardFormula()
const {
452 return std::shared_ptr<storm::logic::Formula const>(
new storm::logic::LongRunAverageRewardFormula());
455std::shared_ptr<storm::logic::Formula const> FormulaParserGrammar::createAtomicExpressionFormula(storm::expressions::Expression
const& expression)
const {
457 "Expected expression " + expression.
toString() +
" to be of boolean type.");
461 return std::shared_ptr<storm::logic::Formula const>(
new storm::logic::AtomicExpressionFormula(expression));
464std::shared_ptr<storm::logic::Formula const> FormulaParserGrammar::createBooleanLiteralFormula(
bool literal)
const {
465 return std::shared_ptr<storm::logic::Formula const>(
new storm::logic::BooleanLiteralFormula(literal));
468std::shared_ptr<storm::logic::Formula const> FormulaParserGrammar::createAtomicLabelFormula(std::string
const& label)
const {
469 return std::shared_ptr<storm::logic::Formula const>(
new storm::logic::AtomicLabelFormula(label));
472std::shared_ptr<storm::logic::Formula const> FormulaParserGrammar::createEventuallyFormula(
473 boost::optional<std::vector<std::tuple<boost::optional<storm::logic::TimeBound>, boost::optional<storm::logic::TimeBound>,
474 std::shared_ptr<storm::logic::TimeBoundReference>>>>
const& timeBounds,
476 if (timeBounds && !timeBounds.get().empty()) {
479 std::vector<std::optional<storm::logic::TimeBound>> lowerBounds, upperBounds;
480 std::vector<storm::logic::TimeBoundReference> timeBoundReferences;
481 for (
auto const& timeBound : timeBounds.get()) {
482 auto const& lowerBound = std::get<0>(timeBound);
483 auto const& upperBound = std::get<1>(timeBound);
485 lowerBounds.emplace_back(lowerBound.get());
487 lowerBounds.emplace_back();
490 upperBounds.emplace_back(upperBound.get());
492 upperBounds.emplace_back();
494 timeBoundReferences.emplace_back(*std::get<2>(timeBound));
496 return std::shared_ptr<storm::logic::Formula const>(
497 new storm::logic::BoundedUntilFormula(createBooleanLiteralFormula(
true), subformula, lowerBounds, upperBounds, timeBoundReferences));
499 return std::shared_ptr<storm::logic::Formula const>(
new storm::logic::EventuallyFormula(subformula, context));
503std::shared_ptr<storm::logic::Formula const> FormulaParserGrammar::createGloballyFormula(std::shared_ptr<storm::logic::Formula const>
const& subformula)
const {
504 return std::shared_ptr<storm::logic::Formula const>(
new storm::logic::GloballyFormula(subformula));
507std::shared_ptr<storm::logic::Formula const> FormulaParserGrammar::createNextFormula(std::shared_ptr<storm::logic::Formula const>
const& subformula)
const {
508 return std::shared_ptr<storm::logic::Formula const>(
new storm::logic::NextFormula(subformula));
511std::shared_ptr<storm::logic::Formula const> FormulaParserGrammar::createUntilFormula(
512 std::shared_ptr<storm::logic::Formula const>
const& leftSubformula,
513 boost::optional<std::vector<std::tuple<boost::optional<storm::logic::TimeBound>, boost::optional<storm::logic::TimeBound>,
514 std::shared_ptr<storm::logic::TimeBoundReference>>>>
const& timeBounds,
515 std::shared_ptr<storm::logic::Formula const>
const& rightSubformula) {
516 if (timeBounds && !timeBounds.get().empty()) {
519 std::vector<std::optional<storm::logic::TimeBound>> lowerBounds, upperBounds;
520 std::vector<storm::logic::TimeBoundReference> timeBoundReferences;
521 for (
auto const& timeBound : timeBounds.get()) {
522 auto const& lowerBound = std::get<0>(timeBound);
523 auto const& upperBound = std::get<1>(timeBound);
525 lowerBounds.emplace_back(lowerBound.get());
527 lowerBounds.emplace_back();
530 upperBounds.emplace_back(upperBound.get());
532 upperBounds.emplace_back();
534 timeBoundReferences.emplace_back(*std::get<2>(timeBound));
536 return std::shared_ptr<storm::logic::Formula const>(
537 new storm::logic::BoundedUntilFormula(leftSubformula, rightSubformula, lowerBounds, upperBounds, timeBoundReferences));
539 return std::shared_ptr<storm::logic::Formula const>(
new storm::logic::UntilFormula(leftSubformula, rightSubformula));
543std::shared_ptr<storm::logic::Formula const> FormulaParserGrammar::createHOAPathFormula(std::string
const& automatonFile)
const {
544 return std::shared_ptr<storm::logic::Formula const>(
new storm::logic::HOAPathFormula(automatonFile));
547void FormulaParserGrammar::addHoaAPMapping(storm::logic::Formula
const& hoaFormula,
const std::string& ap,
548 std::shared_ptr<storm::logic::Formula const>& expression)
const {
551 storm::logic::HOAPathFormula& hoaFormula_ =
static_cast<storm::logic::HOAPathFormula&
>(
const_cast<storm::logic::Formula&
>(hoaFormula));
555std::shared_ptr<storm::logic::Formula const> FormulaParserGrammar::createConditionalFormula(
556 std::shared_ptr<storm::logic::Formula const>
const& leftSubformula, boost::optional<std::shared_ptr<storm::logic::Formula const>>
const& rightSubformula,
558 if (rightSubformula) {
559 return std::shared_ptr<storm::logic::Formula const>(
new storm::logic::ConditionalFormula(leftSubformula, rightSubformula.get(), context));
562 return leftSubformula;
566storm::logic::OperatorInformation FormulaParserGrammar::createOperatorInformation(boost::optional<storm::OptimizationDirection>
const& optimizationDirection,
567 boost::optional<storm::logic::ComparisonType>
const& comparisonType,
568 boost::optional<storm::expressions::Expression>
const& threshold)
const {
569 if (comparisonType && threshold) {
570 storm::expressions::ExpressionEvaluator<storm::RationalNumber> evaluator(*constManager);
571 return storm::logic::OperatorInformation(optimizationDirection, storm::logic::Bound(comparisonType.get(), threshold.get()));
573 return storm::logic::OperatorInformation(optimizationDirection, boost::none);
578 boost::optional<std::string>
const& rewardModelName,
579 storm::logic::OperatorInformation
const& operatorInformation,
580 std::shared_ptr<storm::logic::Formula const>
const& subformula) {
583 STORM_LOG_ASSERT(!rewardModelName,
"Probability operator with reward information parsed.");
584 return createProbabilityOperatorFormula(operatorInformation, subformula);
586 return createRewardOperatorFormula(rewardModelName, operatorInformation, subformula);
588 STORM_LOG_ASSERT(!rewardModelName,
"LRA operator with reward information parsed.");
589 return createLongRunAverageOperatorFormula(operatorInformation, subformula);
591 STORM_LOG_ASSERT(!rewardModelName,
"Time operator with reward model name parsed.");
592 return createTimeOperatorFormula(operatorInformation, subformula);
594 STORM_LOG_THROW(
false, storm::exceptions::WrongFormatException,
"Unexpected formula context.");
598std::shared_ptr<storm::logic::Formula const> FormulaParserGrammar::createLongRunAverageOperatorFormula(
599 storm::logic::OperatorInformation
const& operatorInformation, std::shared_ptr<storm::logic::Formula const>
const& subformula)
const {
600 return std::shared_ptr<storm::logic::Formula const>(
new storm::logic::LongRunAverageOperatorFormula(subformula, operatorInformation));
603std::shared_ptr<storm::logic::Formula const> FormulaParserGrammar::createRewardOperatorFormula(
604 boost::optional<std::string>
const& rewardModelName, storm::logic::OperatorInformation
const& operatorInformation,
605 std::shared_ptr<storm::logic::Formula const>
const& subformula)
const {
606 return std::shared_ptr<storm::logic::Formula const>(
new storm::logic::RewardOperatorFormula(subformula, rewardModelName, operatorInformation));
609std::shared_ptr<storm::logic::Formula const> FormulaParserGrammar::createTimeOperatorFormula(
610 storm::logic::OperatorInformation
const& operatorInformation, std::shared_ptr<storm::logic::Formula const>
const& subformula)
const {
611 return std::shared_ptr<storm::logic::Formula const>(
new storm::logic::TimeOperatorFormula(subformula, operatorInformation));
614std::shared_ptr<storm::logic::Formula const> FormulaParserGrammar::createProbabilityOperatorFormula(
615 storm::logic::OperatorInformation
const& operatorInformation, std::shared_ptr<storm::logic::Formula const>
const& subformula) {
616 return std::shared_ptr<storm::logic::Formula const>(
new storm::logic::ProbabilityOperatorFormula(subformula, operatorInformation));
619std::shared_ptr<storm::logic::Formula const> FormulaParserGrammar::createBinaryBooleanStateFormula(
620 std::shared_ptr<storm::logic::Formula const>
const& leftSubformula, std::shared_ptr<storm::logic::Formula const>
const& rightSubformula,
622 return std::shared_ptr<storm::logic::Formula const>(
new storm::logic::BinaryBooleanStateFormula(operatorType, leftSubformula, rightSubformula));
625std::shared_ptr<storm::logic::Formula const> FormulaParserGrammar::createUnaryBooleanStateFormula(
626 std::shared_ptr<storm::logic::Formula const>
const& subformula, boost::optional<storm::logic::UnaryBooleanStateFormula::OperatorType>
const& operatorType) {
628 return std::shared_ptr<storm::logic::Formula const>(
new storm::logic::UnaryBooleanStateFormula(operatorType.get(), subformula));
634std::shared_ptr<storm::logic::Formula const> FormulaParserGrammar::createBinaryBooleanPathFormula(
635 std::shared_ptr<storm::logic::Formula const>
const& leftSubformula, std::shared_ptr<storm::logic::Formula const>
const& rightSubformula,
637 return std::shared_ptr<storm::logic::Formula const>(
new storm::logic::BinaryBooleanPathFormula(operatorType, leftSubformula, rightSubformula));
640std::shared_ptr<storm::logic::Formula const> FormulaParserGrammar::createUnaryBooleanPathFormula(
641 std::shared_ptr<storm::logic::Formula const>
const& subformula, boost::optional<storm::logic::UnaryBooleanPathFormula::OperatorType>
const& operatorType) {
643 return std::shared_ptr<storm::logic::Formula const>(
new storm::logic::UnaryBooleanPathFormula(operatorType.get(), subformula));
649std::shared_ptr<storm::logic::Formula const> FormulaParserGrammar::createBinaryBooleanStateOrPathFormula(
650 std::shared_ptr<storm::logic::Formula const>
const& leftSubformula, std::shared_ptr<storm::logic::Formula const>
const& rightSubformula,
652 if (leftSubformula->isStateFormula() && rightSubformula->isStateFormula()) {
653 return createBinaryBooleanStateFormula(leftSubformula, rightSubformula, operatorType);
654 }
else if (leftSubformula->isPathFormula() || rightSubformula->isPathFormula()) {
655 return createBinaryBooleanPathFormula(leftSubformula, rightSubformula, operatorType);
657 STORM_LOG_THROW(
false, storm::exceptions::WrongFormatException,
"Subformulas have unexpected type.");
660std::shared_ptr<storm::logic::Formula const> FormulaParserGrammar::createUnaryBooleanStateOrPathFormula(
661 std::shared_ptr<storm::logic::Formula const>
const& subformula, boost::optional<storm::logic::UnaryBooleanOperatorType>
const& operatorType) {
662 if (subformula->isStateFormula()) {
663 return createUnaryBooleanStateFormula(subformula, operatorType);
664 }
else if (subformula->isPathFormula()) {
665 return createUnaryBooleanPathFormula(subformula, operatorType);
667 STORM_LOG_THROW(
false, storm::exceptions::WrongFormatException,
"Subformulas have unexpected type.");
670bool FormulaParserGrammar::isValidMultiBoundedPathFormulaOperand(std::shared_ptr<storm::logic::Formula const>
const& operand) {
671 if (operand->isBoundedUntilFormula()) {
672 if (!operand->asBoundedUntilFormula().isMultiDimensional()) {
675 STORM_LOG_ERROR(
"Composition of multidimensional bounded until formula must consist of single dimension subformulas. Got '" << *operand
681std::shared_ptr<storm::logic::Formula const> FormulaParserGrammar::createMultiBoundedPathFormula(
682 std::vector<std::shared_ptr<storm::logic::Formula const>>
const& subformulas) {
683 std::vector<std::shared_ptr<storm::logic::Formula const>> leftSubformulas, rightSubformulas;
684 std::vector<std::optional<storm::logic::TimeBound>> lowerBounds, upperBounds;
685 std::vector<storm::logic::TimeBoundReference> timeBoundReferences;
686 for (
auto const& subformula : subformulas) {
687 STORM_LOG_THROW(subformula->isBoundedUntilFormula(), storm::exceptions::WrongFormatException,
688 "Multi-path formulas require bounded until (or eventually) subformulae. Got '" << *subformula <<
"' instead.");
689 auto const& f = subformula->asBoundedUntilFormula();
690 STORM_LOG_THROW(!f.isMultiDimensional(), storm::exceptions::WrongFormatException,
691 "Composition of multidimensional bounded until formula must consist of single dimension subformulas. Got '" << f <<
"' instead.");
692 leftSubformulas.push_back(f.getLeftSubformula().asSharedPointer());
693 rightSubformulas.push_back(f.getRightSubformula().asSharedPointer());
694 if (f.hasLowerBound()) {
695 lowerBounds.emplace_back(storm::logic::TimeBound(f.isLowerBoundStrict(), f.getLowerBound()));
697 lowerBounds.emplace_back();
699 if (f.hasUpperBound()) {
700 upperBounds.emplace_back(storm::logic::TimeBound(f.isUpperBoundStrict(), f.getUpperBound()));
702 upperBounds.emplace_back();
704 timeBoundReferences.push_back(f.getTimeBoundReference());
706 return std::shared_ptr<storm::logic::Formula const>(
707 new storm::logic::BoundedUntilFormula(leftSubformulas, rightSubformulas, lowerBounds, upperBounds, timeBoundReferences));
710std::shared_ptr<storm::logic::Formula const> FormulaParserGrammar::createMultiOperatorFormula(
712 return std::shared_ptr<storm::logic::Formula const>(
new storm::logic::MultiObjectiveFormula(subformulas, type));
715storm::expressions::Variable FormulaParserGrammar::createQuantileBoundVariables(boost::optional<storm::solver::OptimizationDirection>
const& dir,
716 std::string
const& variableName) {
717 STORM_LOG_ASSERT(manager,
"Mutable expression manager required to define quantile bound variable.");
718 storm::expressions::Variable var;
719 if (manager->hasVariable(variableName)) {
720 var = manager->getVariable(variableName);
721 STORM_LOG_THROW(quantileFormulaVariables.count(var) > 0, storm::exceptions::WrongFormatException,
722 "Invalid quantile variable name '" << variableName <<
"' in quantile formula: variable already exists.");
724 var = manager->declareRationalVariable(variableName);
725 quantileFormulaVariables.insert(var);
728 << dir.get() <<
"' for quantile variable " << variableName
729 <<
" is ignored. This information will be derived from the subformula of the quantile.");
734std::shared_ptr<storm::logic::Formula const> FormulaParserGrammar::createQuantileFormula(std::vector<storm::expressions::Variable>
const& boundVariables,
735 std::shared_ptr<storm::logic::Formula const>
const& subformula) {
736 return std::shared_ptr<storm::logic::Formula const>(
new storm::logic::QuantileFormula(boundVariables, subformula));
739std::set<storm::expressions::Variable> FormulaParserGrammar::getUndefinedConstants(std::shared_ptr<storm::logic::Formula const>
const& formula)
const {
740 std::set<storm::expressions::Variable> result;
741 std::set<storm::expressions::Variable> usedVariables = formula->getUsedVariables();
742 std::set_intersection(usedVariables.begin(), usedVariables.end(), undefinedConstants.begin(), undefinedConstants.end(),
743 std::inserter(result, result.begin()));
747storm::jani::Property FormulaParserGrammar::createProperty(boost::optional<std::string>
const& propertyName,
storm::modelchecker::FilterType const& filterType,
748 std::shared_ptr<storm::logic::Formula const>
const& formula,
749 std::shared_ptr<storm::logic::Formula const>
const& states) {
750 storm::jani::FilterExpression filterExpression(formula, filterType, states);
754 return storm::jani::Property(propertyName.get(), filterExpression, this->getUndefinedConstants(formula));
756 return storm::jani::Property(std::to_string(propertyCount - 1), filterExpression, this->getUndefinedConstants(formula));
760storm::jani::Property FormulaParserGrammar::createPropertyWithDefaultFilterTypeAndStates(boost::optional<std::string>
const& propertyName,
761 std::shared_ptr<storm::logic::Formula const>
const& formula) {
764 return storm::jani::Property(propertyName.get(), formula, this->getUndefinedConstants(formula));
766 return storm::jani::Property(std::to_string(propertyCount), formula, this->getUndefinedConstants(formula));
770storm::logic::PlayerCoalition FormulaParserGrammar::createPlayerCoalition(
771 std::vector<std::variant<std::string, storm::storage::PlayerIndex>>
const& playerIds)
const {
772 return storm::logic::PlayerCoalition(playerIds);
775std::shared_ptr<storm::logic::Formula const> FormulaParserGrammar::createGameFormula(storm::logic::PlayerCoalition
const& coalition,
776 std::shared_ptr<storm::logic::Formula const>
const& subformula)
const {
777 return std::shared_ptr<storm::logic::Formula const>(
new storm::logic::GameFormula(coalition, subformula));
780bool FormulaParserGrammar::isBooleanReturnType(std::shared_ptr<storm::logic::Formula const>
const& formula,
bool raiseErrorMessage) {
781 if (formula->hasQualitativeResult()) {
784 STORM_LOG_ERROR_COND(!raiseErrorMessage,
"Formula " << *formula <<
" does not have a Boolean return type.");
788bool FormulaParserGrammar::raiseAmbiguousNonAssociativeOperatorError(std::shared_ptr<storm::logic::Formula const>
const& formula, std::string
const& op) {
789 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
Contains all file parsers and helper classes.