You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

223 lines
8.5 KiB

  1. /*
  2. * PrctlParserTest.cpp
  3. *
  4. * Created on: 01.02.2013
  5. * Author: Thomas Heinemann
  6. */
  7. #include "gtest/gtest.h"
  8. #include "storm-config.h"
  9. #include "src/parser/PrctlParser.h"
  10. #include "src/exceptions/WrongFormatException.h"
  11. #include "src/formula/actions/FormulaAction.h"
  12. #include "src/formula/actions/InvertAction.h"
  13. #include "src/formula/actions/SortAction.h"
  14. #include "src/formula/actions/RangeAction.h"
  15. #include "src/formula/actions/BoundAction.h"
  16. namespace prctl = storm::property::prctl;
  17. TEST(PrctlParserTest, parseApOnlyTest) {
  18. std::string input = "ap";
  19. std::shared_ptr<prctl::PrctlFilter<double>> formula(nullptr);
  20. ASSERT_NO_THROW(
  21. formula = storm::parser::PrctlParser::parsePrctlFormula(input)
  22. );
  23. // The parser did not falsely recognize the input as a comment.
  24. ASSERT_NE(formula, nullptr);
  25. // The input was parsed correctly.
  26. ASSERT_EQ(input, formula->toString());
  27. }
  28. TEST(PrctlParserTest, parsePropositionalFormulaTest) {
  29. std::string input = "!(a & b) | a & ! c";
  30. std::shared_ptr<prctl::PrctlFilter<double>> formula(nullptr);
  31. ASSERT_NO_THROW(
  32. formula = storm::parser::PrctlParser::parsePrctlFormula(input)
  33. );
  34. // The parser did not falsely recognize the input as a comment.
  35. ASSERT_NE(formula, nullptr);
  36. // The input was parsed correctly.
  37. ASSERT_EQ("(!(a & b) | (a & !c))", formula->toString());
  38. }
  39. TEST(PrctlParserTest, parsePathFormulaTest) {
  40. std::string input = "X( P<0.9 (a U b))";
  41. std::shared_ptr<prctl::PrctlFilter<double>> formula(nullptr);
  42. ASSERT_NO_THROW(
  43. formula = storm::parser::PrctlParser::parsePrctlFormula(input)
  44. );
  45. // The parser did not falsely recognize the input as a comment.
  46. ASSERT_NE(formula, nullptr);
  47. // The input was parsed correctly.
  48. ASSERT_NE(std::dynamic_pointer_cast<prctl::Next<double>>(formula->getChild()).get(), nullptr);
  49. auto nextFormula = std::dynamic_pointer_cast<prctl::Next<double>>(formula->getChild());
  50. ASSERT_NE(std::dynamic_pointer_cast<prctl::ProbabilisticBoundOperator<double>>(nextFormula->getChild()).get(), nullptr);
  51. auto probBoundFormula = std::dynamic_pointer_cast<prctl::ProbabilisticBoundOperator<double>>(nextFormula->getChild());
  52. ASSERT_NE(std::dynamic_pointer_cast<prctl::Until<double>>(probBoundFormula->getChild()).get(), nullptr);
  53. auto untilFormula = std::dynamic_pointer_cast<prctl::Until<double>>(probBoundFormula->getChild());
  54. ASSERT_NE(std::dynamic_pointer_cast<prctl::Ap<double>>(untilFormula->getLeft()).get(), nullptr);
  55. ASSERT_NE(std::dynamic_pointer_cast<prctl::Ap<double>>(untilFormula->getRight()).get(), nullptr);
  56. ASSERT_EQ("a", std::dynamic_pointer_cast<prctl::Ap<double>>(untilFormula->getLeft())->getAp());
  57. ASSERT_EQ("b", std::dynamic_pointer_cast<prctl::Ap<double>>(untilFormula->getRight())->getAp());
  58. ASSERT_EQ(0.9, probBoundFormula->getBound());
  59. ASSERT_EQ(storm::property::LESS, probBoundFormula->getComparisonOperator());
  60. // The string representation is also correct.
  61. ASSERT_EQ("P = ? (X P < 0.900000 (a U b))", formula->toString());
  62. }
  63. TEST(PrctlParserTest, parseProbabilisticFormulaTest) {
  64. std::string input = "P > 0.5 [ F a ]";
  65. std::shared_ptr<prctl::PrctlFilter<double>> formula(nullptr);
  66. ASSERT_NO_THROW(
  67. formula = storm::parser::PrctlParser::parsePrctlFormula(input)
  68. );
  69. // The parser did not falsely recognize the input as a comment.
  70. ASSERT_NE(formula, nullptr);
  71. ASSERT_NE(std::dynamic_pointer_cast<prctl::ProbabilisticBoundOperator<double>>(formula->getChild()).get(), nullptr);
  72. std::shared_ptr<prctl::ProbabilisticBoundOperator<double>> op = std::static_pointer_cast<prctl::ProbabilisticBoundOperator<double>>(formula->getChild());
  73. ASSERT_EQ(storm::property::GREATER, op->getComparisonOperator());
  74. ASSERT_EQ(0.5, op->getBound());
  75. // Test the string representation for the parsed formula.
  76. ASSERT_EQ("P > 0.500000 (F a)", formula->toString());
  77. }
  78. TEST(PrctlParserTest, parseRewardFormulaTest) {
  79. std::string input = "R >= 15 [ I=5 ]";
  80. std::shared_ptr<prctl::PrctlFilter<double>>formula(nullptr);
  81. ASSERT_NO_THROW(
  82. formula = storm::parser::PrctlParser::parsePrctlFormula(input);
  83. );
  84. // The parser did not falsely recognize the input as a comment.
  85. ASSERT_NE(formula, nullptr);
  86. ASSERT_NE(std::dynamic_pointer_cast<prctl::RewardBoundOperator<double>>(formula->getChild()).get(), nullptr);
  87. std::shared_ptr<prctl::RewardBoundOperator<double>> op = std::static_pointer_cast<prctl::RewardBoundOperator<double>>(formula->getChild());
  88. ASSERT_EQ(storm::property::GREATER_EQUAL, op->getComparisonOperator());
  89. ASSERT_EQ(15.0, op->getBound());
  90. // Test the string representation for the parsed formula.
  91. ASSERT_EQ("R >= 15.000000 (I=5)", formula->toString());
  92. }
  93. TEST(PrctlParserTest, parseRewardNoBoundFormulaTest) {
  94. std::string input = "R = ? [ F a ]";
  95. std::shared_ptr<prctl::PrctlFilter<double>> formula(nullptr);
  96. ASSERT_NO_THROW(
  97. formula = storm::parser::PrctlParser::parsePrctlFormula(input)
  98. );
  99. // The parser did not falsely recognize the input as a comment.
  100. ASSERT_NE(formula, nullptr);
  101. // The input was parsed correctly.
  102. ASSERT_EQ("R = ? (F a)", formula->toString());
  103. }
  104. TEST(PrctlParserTest, parseProbabilisticNoBoundFormulaTest) {
  105. std::string input = "P = ? [ a U <= 4 b & (!c) ]";
  106. std::shared_ptr<prctl::PrctlFilter<double>> formula(nullptr);
  107. ASSERT_NO_THROW(
  108. formula = storm::parser::PrctlParser::parsePrctlFormula(input)
  109. );
  110. // The parser did not falsely recognize the input as a comment.
  111. ASSERT_NE(formula, nullptr);
  112. // The input was parsed correctly.
  113. ASSERT_EQ("P = ? (a U<=4 (b & !c))", formula->toString());
  114. }
  115. TEST(PrctlParserTest, parseComplexFormulaTest) {
  116. std::string input = "R<=0.5 [ S ] & (R > 15 [ C<=0.5 ] | !P < 0.4 [ G P>0.9 [F<=7 a & b] ])";
  117. std::shared_ptr<prctl::PrctlFilter<double>> formula(nullptr);
  118. ASSERT_NO_THROW(
  119. formula = storm::parser::PrctlParser::parsePrctlFormula(input)
  120. );
  121. // The parser did not falsely recognize the input as a comment.
  122. ASSERT_NE(formula, nullptr);
  123. // The input was parsed correctly.
  124. ASSERT_EQ("(R <= 0.500000 (S) & (R > 15.000000 (C <= 0.500000) | !P < 0.400000 (G P > 0.900000 (F<=7 (a & b)))))", formula->toString());
  125. }
  126. TEST(PrctlParserTest, parsePrctlFilterTest) {
  127. std::string input = "filter[formula(b); invert; bound(<, 0.5); sort(value); range(0,3)](F a)";
  128. std::shared_ptr<prctl::PrctlFilter<double>> formula(nullptr);
  129. ASSERT_NO_THROW(
  130. formula = storm::parser::PrctlParser::parsePrctlFormula(input)
  131. );
  132. // The parser did not falsely recognize the input as a comment.
  133. ASSERT_NE(formula, nullptr);
  134. ASSERT_EQ(5, formula->getActionCount());
  135. ASSERT_NE(std::dynamic_pointer_cast<storm::property::action::FormulaAction<double>>(formula->getAction(0)).get(), nullptr);
  136. ASSERT_NE(std::dynamic_pointer_cast<storm::property::action::InvertAction<double>>(formula->getAction(1)).get(), nullptr);
  137. ASSERT_NE(std::dynamic_pointer_cast<storm::property::action::BoundAction<double>>(formula->getAction(2)).get(), nullptr);
  138. ASSERT_NE(std::dynamic_pointer_cast<storm::property::action::SortAction<double>>(formula->getAction(3)).get(), nullptr);
  139. ASSERT_NE(std::dynamic_pointer_cast<storm::property::action::RangeAction<double>>(formula->getAction(4)).get(), nullptr);
  140. // The input was parsed correctly.
  141. ASSERT_EQ("filter[formula(b); invert; bound(<, 0.500000); sort(value, ascending); range(0, 3)](F a)", formula->toString());
  142. }
  143. TEST(PrctlParserTest, commentTest) {
  144. std::string input = "// This is a comment. And this is a commented out formula: R = ? [ F a ]";
  145. std::shared_ptr<prctl::PrctlFilter<double>> formula(nullptr);
  146. ASSERT_NO_THROW(
  147. formula = storm::parser::PrctlParser::parsePrctlFormula(input)
  148. );
  149. // The parser recognized the input as a comment.
  150. ASSERT_NE(nullptr, formula.get());
  151. // Test if the parser recognizes the comment at the end of a line.
  152. input = "R = ? [ F a ] // This is a comment.";
  153. ASSERT_NO_THROW(
  154. formula = storm::parser::PrctlParser::parsePrctlFormula(input)
  155. );
  156. ASSERT_EQ("R = ? (F a)", formula->toString());
  157. }
  158. TEST(PrctlParserTest, wrongProbabilisticFormulaTest) {
  159. std::shared_ptr<prctl::PrctlFilter<double>> formula(nullptr);
  160. ASSERT_THROW(
  161. formula = storm::parser::PrctlParser::parsePrctlFormula("P > 0.5 [ a ]"),
  162. storm::exceptions::WrongFormatException
  163. );
  164. }
  165. TEST(PrctlParserTest, wrongFormulaTest) {
  166. std::shared_ptr<prctl::PrctlFilter<double>> formula(nullptr);
  167. ASSERT_THROW(
  168. formula = storm::parser::PrctlParser::parsePrctlFormula("(a | b) & +"),
  169. storm::exceptions::WrongFormatException
  170. );
  171. }
  172. TEST(PrctlParserTest, wrongFormulaTest2) {
  173. std::shared_ptr<prctl::PrctlFilter<double>> formula(nullptr);
  174. ASSERT_THROW(
  175. formula = storm::parser::PrctlParser::parsePrctlFormula("P>0 [ F & a ]"),
  176. storm::exceptions::WrongFormatException
  177. );
  178. }