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.

222 lines
8.4 KiB

  1. /*
  2. * CslParserTest.cpp
  3. *
  4. * Created on: 09.04.2013
  5. * Author: Thomas Heinemann
  6. */
  7. #include "gtest/gtest.h"
  8. #include "storm-config.h"
  9. #include "src/parser/CslParser.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 csl = storm::property::csl;
  17. TEST(CslParserTest, parseApOnlyTest) {
  18. std::string input = "ap";
  19. std::shared_ptr<csl::CslFilter<double>> formula(nullptr);
  20. ASSERT_NO_THROW(
  21. formula = storm::parser::CslParser::parseCslFormula(input);
  22. );
  23. // The parser did not falsely recognize the input as a comment.
  24. ASSERT_NE(formula.get(), nullptr);
  25. // The input was parsed correctly.
  26. ASSERT_EQ(input, formula->toString());
  27. }
  28. TEST(CslParserTest, parsePropositionalFormulaTest) {
  29. std::string input = "!(a & b) | a & ! c";
  30. std::shared_ptr<csl::CslFilter<double>> formula(nullptr);
  31. ASSERT_NO_THROW(
  32. formula = storm::parser::CslParser::parseCslFormula(input);
  33. );
  34. // The parser did not falsely recognize the input as a comment.
  35. ASSERT_NE(formula.get(), nullptr);
  36. // The input was parsed correctly.
  37. ASSERT_EQ("(!(a & b) | (a & !c))", formula->toString());
  38. }
  39. TEST(CslParserTest, parsePathFormulaTest) {
  40. std::string input = "X( P<0.9 (a U b))";
  41. std::shared_ptr<csl::CslFilter<double>> formula(nullptr);
  42. ASSERT_NO_THROW(
  43. formula = storm::parser::CslParser::parseCslFormula(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<csl::Next<double>>(formula->getChild()).get(), nullptr);
  49. auto nextFormula = std::dynamic_pointer_cast<csl::Next<double>>(formula->getChild());
  50. ASSERT_NE(std::dynamic_pointer_cast<csl::ProbabilisticBoundOperator<double>>(nextFormula->getChild()).get(), nullptr);
  51. auto probBoundFormula = std::dynamic_pointer_cast<csl::ProbabilisticBoundOperator<double>>(nextFormula->getChild());
  52. ASSERT_NE(std::dynamic_pointer_cast<csl::Until<double>>(probBoundFormula->getChild()).get(), nullptr);
  53. auto untilFormula = std::dynamic_pointer_cast<csl::Until<double>>(probBoundFormula->getChild());
  54. ASSERT_NE(std::dynamic_pointer_cast<csl::Ap<double>>(untilFormula->getLeft()).get(), nullptr);
  55. ASSERT_NE(std::dynamic_pointer_cast<csl::Ap<double>>(untilFormula->getRight()).get(), nullptr);
  56. ASSERT_EQ("a", std::dynamic_pointer_cast<csl::Ap<double>>(untilFormula->getLeft())->getAp());
  57. ASSERT_EQ("b", std::dynamic_pointer_cast<csl::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(CslParserTest, parseProbabilisticFormulaTest) {
  64. std::string input = "P > 0.5 [ F a ]";
  65. std::shared_ptr<csl::CslFilter<double>> formula(nullptr);
  66. ASSERT_NO_THROW(
  67. formula = storm::parser::CslParser::parseCslFormula(input);
  68. );
  69. // The parser did not falsely recognize the input as a comment.
  70. ASSERT_NE(formula.get(), nullptr);
  71. auto op = std::dynamic_pointer_cast<storm::property::csl::ProbabilisticBoundOperator<double>>(formula->getChild());
  72. ASSERT_NE(op.get(), nullptr);
  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(CslParserTest, parseSteadyStateBoundFormulaTest) {
  79. std::string input = "S >= 15 [ P < 0.2 [ a U<=3 b ] ]";
  80. std::shared_ptr<csl::CslFilter<double>> formula(nullptr);
  81. ASSERT_NO_THROW(
  82. formula = storm::parser::CslParser::parseCslFormula(input);
  83. );
  84. // The parser did not falsely recognize the input as a comment.
  85. ASSERT_NE(formula.get(), nullptr);
  86. auto op = std::dynamic_pointer_cast<storm::property::csl::SteadyStateBoundOperator<double>>(formula->getChild());
  87. ASSERT_NE(op.get(), nullptr);
  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("S >= 15.000000 (P < 0.200000 (a U[0.000000,3.000000] b))", formula->toString());
  92. }
  93. TEST(CslParserTest, parseSteadyStateNoBoundFormulaTest) {
  94. std::string input = "S = ? [ P <= 0.5 [ F<=3 a ] ]";
  95. std::shared_ptr<csl::CslFilter<double>> formula(nullptr);
  96. ASSERT_NO_THROW(
  97. formula = storm::parser::CslParser::parseCslFormula(input);
  98. );
  99. // The parser did not falsely recognize the input as a comment.
  100. ASSERT_NE(formula.get(), nullptr);
  101. // The input was parsed correctly.
  102. ASSERT_EQ("S = ? (P <= 0.500000 (F[0.000000,3.000000] a))", formula->toString());
  103. }
  104. TEST(CslParserTest, parseProbabilisticNoBoundFormulaTest) {
  105. std::string input = "P = ? [ a U [3,4] b & (!c) ]";
  106. std::shared_ptr<csl::CslFilter<double>> formula(nullptr);
  107. ASSERT_NO_THROW(
  108. formula = storm::parser::CslParser::parseCslFormula(input);
  109. );
  110. // The parser did not falsely recognize the input as a comment.
  111. ASSERT_NE(formula.get(), nullptr);
  112. // The input was parsed correctly.
  113. ASSERT_EQ("P = ? (a U[3.000000,4.000000] (b & !c))", formula->toString());
  114. }
  115. TEST(CslParserTest, parseComplexFormulaTest) {
  116. std::string input = "S<=0.5 [ P <= 0.5 [ a U c ] ] & (P > 0.5 [ G b] | !P < 0.4 [ G P>0.9 [F >=7 a & b] ]) //and a comment";
  117. std::shared_ptr<csl::CslFilter<double>> formula(nullptr);
  118. ASSERT_NO_THROW(
  119. formula = storm::parser::CslParser::parseCslFormula(input);
  120. );
  121. // The parser did not falsely recognize the input as a comment.
  122. ASSERT_NE(formula.get(), nullptr);
  123. // The input was parsed correctly.
  124. ASSERT_EQ("S <= 0.500000 ((P <= 0.500000 (a U c) & (P > 0.500000 (G b) | !P < 0.400000 (G P > 0.900000 (F>=7.000000 (a & b))))))", formula->toString());
  125. }
  126. TEST(CslParserTest, parseCslFilterTest) {
  127. std::string input = "filter[formula(b); invert; bound(<, 0.5); sort(value); range(0,3)](F a)";
  128. std::shared_ptr<csl::CslFilter<double>> formula(nullptr);
  129. ASSERT_NO_THROW(
  130. formula = storm::parser::CslParser::parseCslFormula(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(CslParserTest, commentTest) {
  144. std::string input = "// This is a comment. And this is a commented out formula: P = ? [ F a ]";
  145. std::shared_ptr<csl::CslFilter<double>> formula(nullptr);
  146. ASSERT_NO_THROW(
  147. formula = storm::parser::CslParser::parseCslFormula(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 = "P = ? [ F a ] // This is a comment.";
  153. ASSERT_NO_THROW(
  154. formula = storm::parser::CslParser::parseCslFormula(input)
  155. );
  156. ASSERT_EQ("P = ? (F a)", formula->toString());
  157. }
  158. TEST(CslParserTest, wrongProbabilisticFormulaTest) {
  159. std::string input = "P > 0.5 [ a ]";
  160. std::shared_ptr<csl::CslFilter<double>> formula(nullptr);
  161. ASSERT_THROW(
  162. formula = storm::parser::CslParser::parseCslFormula(input),
  163. storm::exceptions::WrongFormatException
  164. );
  165. }
  166. TEST(CslParserTest, wrongFormulaTest) {
  167. std::string input = "(a | b) & +";
  168. std::shared_ptr<csl::CslFilter<double>> formula(nullptr);
  169. ASSERT_THROW(
  170. formula = storm::parser::CslParser::parseCslFormula(input),
  171. storm::exceptions::WrongFormatException
  172. );
  173. }
  174. TEST(CslParserTest, wrongFormulaTest2) {
  175. std::string input = "P>0 [ F & a ]";
  176. std::shared_ptr<csl::CslFilter<double>> formula(nullptr);
  177. ASSERT_THROW(
  178. formula = storm::parser::CslParser::parseCslFormula(input),
  179. storm::exceptions::WrongFormatException
  180. );
  181. }