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.

259 lines
9.8 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/properties/actions/FormulaAction.h"
  12. #include "src/properties/actions/InvertAction.h"
  13. #include "src/properties/actions/SortAction.h"
  14. #include "src/properties/actions/RangeAction.h"
  15. #include "src/properties/actions/BoundAction.h"
  16. namespace csl = storm::properties::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. ASSERT_TRUE(formula->getChild()->isPropositional());
  26. ASSERT_FALSE(formula->getChild()->isProbEventuallyAP());
  27. // The input was parsed correctly.
  28. ASSERT_EQ(input, formula->toString());
  29. }
  30. TEST(CslParserTest, parsePropositionalFormulaTest) {
  31. std::string input = "!(a & b) | a & ! c";
  32. std::shared_ptr<csl::CslFilter<double>> formula(nullptr);
  33. ASSERT_NO_THROW(
  34. formula = storm::parser::CslParser::parseCslFormula(input);
  35. );
  36. // The parser did not falsely recognize the input as a comment.
  37. ASSERT_NE(formula.get(), nullptr);
  38. ASSERT_TRUE(formula->getChild()->isPropositional());
  39. ASSERT_FALSE(formula->getChild()->isProbEventuallyAP());
  40. // The input was parsed correctly.
  41. ASSERT_EQ("(!(a & b) | (a & !c))", formula->toString());
  42. }
  43. TEST(CslParserTest, parsePathFormulaTest) {
  44. std::string input = "X( P<0.9 (a U b))";
  45. std::shared_ptr<csl::CslFilter<double>> formula(nullptr);
  46. ASSERT_NO_THROW(
  47. formula = storm::parser::CslParser::parseCslFormula(input)
  48. );
  49. // The parser did not falsely recognize the input as a comment.
  50. ASSERT_NE(formula, nullptr);
  51. // The input was parsed correctly.
  52. ASSERT_NE(std::dynamic_pointer_cast<csl::Next<double>>(formula->getChild()).get(), nullptr);
  53. auto nextFormula = std::dynamic_pointer_cast<csl::Next<double>>(formula->getChild());
  54. ASSERT_FALSE(nextFormula->isPropositional());
  55. ASSERT_FALSE(nextFormula->isProbEventuallyAP());
  56. ASSERT_NE(std::dynamic_pointer_cast<csl::ProbabilisticBoundOperator<double>>(nextFormula->getChild()).get(), nullptr);
  57. auto probBoundFormula = std::dynamic_pointer_cast<csl::ProbabilisticBoundOperator<double>>(nextFormula->getChild());
  58. ASSERT_EQ(0.9, probBoundFormula->getBound());
  59. ASSERT_EQ(storm::properties::LESS, probBoundFormula->getComparisonOperator());
  60. ASSERT_FALSE(probBoundFormula->isPropositional());
  61. ASSERT_TRUE(probBoundFormula->isProbEventuallyAP());
  62. ASSERT_NE(std::dynamic_pointer_cast<csl::Until<double>>(probBoundFormula->getChild()).get(), nullptr);
  63. auto untilFormula = std::dynamic_pointer_cast<csl::Until<double>>(probBoundFormula->getChild());
  64. ASSERT_FALSE(untilFormula->isPropositional());
  65. ASSERT_FALSE(untilFormula->isProbEventuallyAP());
  66. ASSERT_NE(std::dynamic_pointer_cast<csl::Ap<double>>(untilFormula->getLeft()).get(), nullptr);
  67. ASSERT_NE(std::dynamic_pointer_cast<csl::Ap<double>>(untilFormula->getRight()).get(), nullptr);
  68. ASSERT_EQ("a", std::dynamic_pointer_cast<csl::Ap<double>>(untilFormula->getLeft())->getAp());
  69. ASSERT_EQ("b", std::dynamic_pointer_cast<csl::Ap<double>>(untilFormula->getRight())->getAp());
  70. // The string representation is also correct.
  71. ASSERT_EQ("P = ? (X P < 0.900000 (a U b))", formula->toString());
  72. }
  73. TEST(CslParserTest, parseProbabilisticFormulaTest) {
  74. std::string input = "P > 0.5 [ F a ]";
  75. std::shared_ptr<csl::CslFilter<double>> formula(nullptr);
  76. ASSERT_NO_THROW(
  77. formula = storm::parser::CslParser::parseCslFormula(input);
  78. );
  79. // The parser did not falsely recognize the input as a comment.
  80. ASSERT_NE(formula.get(), nullptr);
  81. auto op = std::dynamic_pointer_cast<storm::properties::csl::ProbabilisticBoundOperator<double>>(formula->getChild());
  82. ASSERT_NE(op.get(), nullptr);
  83. ASSERT_EQ(storm::properties::GREATER, op->getComparisonOperator());
  84. ASSERT_EQ(0.5, op->getBound());
  85. ASSERT_FALSE(op->isPropositional());
  86. ASSERT_TRUE(op->isProbEventuallyAP());
  87. // Test the string representation for the parsed formula.
  88. ASSERT_EQ("P > 0.500000 (F a)", formula->toString());
  89. }
  90. TEST(CslParserTest, parseSteadyStateBoundFormulaTest) {
  91. std::string input = "S >= 15 [ P < 0.2 [ a U<=3 b ] ]";
  92. std::shared_ptr<csl::CslFilter<double>> formula(nullptr);
  93. ASSERT_NO_THROW(
  94. formula = storm::parser::CslParser::parseCslFormula(input);
  95. );
  96. // The parser did not falsely recognize the input as a comment.
  97. ASSERT_NE(formula.get(), nullptr);
  98. auto op = std::dynamic_pointer_cast<storm::properties::csl::SteadyStateBoundOperator<double>>(formula->getChild());
  99. ASSERT_NE(op.get(), nullptr);
  100. ASSERT_EQ(storm::properties::GREATER_EQUAL, op->getComparisonOperator());
  101. ASSERT_EQ(15.0, op->getBound());
  102. ASSERT_FALSE(op->isPropositional());
  103. ASSERT_FALSE(op->isProbEventuallyAP());
  104. // Test the string representation for the parsed formula.
  105. ASSERT_EQ("S >= 15.000000 (P < 0.200000 (a U[0.000000,3.000000] b))", formula->toString());
  106. }
  107. TEST(CslParserTest, parseSteadyStateNoBoundFormulaTest) {
  108. std::string input = "S = ? [ P <= 0.5 [ F<=3 a ] ]";
  109. std::shared_ptr<csl::CslFilter<double>> formula(nullptr);
  110. ASSERT_NO_THROW(
  111. formula = storm::parser::CslParser::parseCslFormula(input);
  112. );
  113. // The parser did not falsely recognize the input as a comment.
  114. ASSERT_NE(formula.get(), nullptr);
  115. ASSERT_FALSE(formula->getChild()->isPropositional());
  116. ASSERT_FALSE(formula->getChild()->isProbEventuallyAP());
  117. // The input was parsed correctly.
  118. ASSERT_EQ("S = ? (P <= 0.500000 (F[0.000000,3.000000] a))", formula->toString());
  119. }
  120. TEST(CslParserTest, parseProbabilisticNoBoundFormulaTest) {
  121. std::string input = "P = ? [ a U [3,4] b & (!c) ]";
  122. std::shared_ptr<csl::CslFilter<double>> formula(nullptr);
  123. ASSERT_NO_THROW(
  124. formula = storm::parser::CslParser::parseCslFormula(input);
  125. );
  126. // The parser did not falsely recognize the input as a comment.
  127. ASSERT_NE(formula.get(), nullptr);
  128. ASSERT_FALSE(formula->getChild()->isPropositional());
  129. ASSERT_FALSE(formula->getChild()->isProbEventuallyAP());
  130. // The input was parsed correctly.
  131. ASSERT_EQ("P = ? (a U[3.000000,4.000000] (b & !c))", formula->toString());
  132. }
  133. TEST(CslParserTest, parseComplexFormulaTest) {
  134. 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";
  135. std::shared_ptr<csl::CslFilter<double>> formula(nullptr);
  136. ASSERT_NO_THROW(
  137. formula = storm::parser::CslParser::parseCslFormula(input);
  138. );
  139. // The parser did not falsely recognize the input as a comment.
  140. ASSERT_NE(formula.get(), nullptr);
  141. ASSERT_FALSE(formula->getChild()->isPropositional());
  142. ASSERT_FALSE(formula->getChild()->isProbEventuallyAP());
  143. // The input was parsed correctly.
  144. 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());
  145. }
  146. TEST(CslParserTest, parseCslFilterTest) {
  147. std::string input = "filter[max; formula(b); invert; bound(<, 0.5); sort(value); range(0,3)](F a)";
  148. std::shared_ptr<csl::CslFilter<double>> formula(nullptr);
  149. ASSERT_NO_THROW(
  150. formula = storm::parser::CslParser::parseCslFormula(input)
  151. );
  152. // The parser did not falsely recognize the input as a comment.
  153. ASSERT_NE(formula, nullptr);
  154. ASSERT_EQ(storm::properties::MAXIMIZE, formula->getOptimizingOperator());
  155. ASSERT_EQ(5, formula->getActionCount());
  156. ASSERT_NE(std::dynamic_pointer_cast<storm::properties::action::FormulaAction<double>>(formula->getAction(0)).get(), nullptr);
  157. ASSERT_NE(std::dynamic_pointer_cast<storm::properties::action::InvertAction<double>>(formula->getAction(1)).get(), nullptr);
  158. ASSERT_NE(std::dynamic_pointer_cast<storm::properties::action::BoundAction<double>>(formula->getAction(2)).get(), nullptr);
  159. ASSERT_NE(std::dynamic_pointer_cast<storm::properties::action::SortAction<double>>(formula->getAction(3)).get(), nullptr);
  160. ASSERT_NE(std::dynamic_pointer_cast<storm::properties::action::RangeAction<double>>(formula->getAction(4)).get(), nullptr);
  161. ASSERT_FALSE(formula->getChild()->isPropositional());
  162. ASSERT_FALSE(formula->getChild()->isProbEventuallyAP());
  163. // The input was parsed correctly.
  164. ASSERT_EQ("filter[max; formula(b); invert; bound(<, 0.500000); sort(value, ascending); range(0, 3)](F a)", formula->toString());
  165. }
  166. TEST(CslParserTest, commentTest) {
  167. std::string input = "// This is a comment. And this is a commented out formula: P<=0.5 [ X a ]";
  168. std::shared_ptr<csl::CslFilter<double>> formula(nullptr);
  169. ASSERT_NO_THROW(
  170. formula = storm::parser::CslParser::parseCslFormula(input)
  171. );
  172. // The parser recognized the input as a comment.
  173. ASSERT_NE(nullptr, formula.get());
  174. // Test if the parser recognizes the comment at the end of a line.
  175. input = "P<=0.5 [ X a ] // This is a comment.";
  176. ASSERT_NO_THROW(
  177. formula = storm::parser::CslParser::parseCslFormula(input)
  178. );
  179. ASSERT_FALSE(formula->getChild()->isPropositional());
  180. ASSERT_FALSE(formula->getChild()->isProbEventuallyAP());
  181. ASSERT_EQ("P <= 0.500000 (X a)", formula->toString());
  182. }
  183. TEST(CslParserTest, wrongProbabilisticFormulaTest) {
  184. std::string input = "P > 0.5 [ a ]";
  185. std::shared_ptr<csl::CslFilter<double>> formula(nullptr);
  186. ASSERT_THROW(
  187. formula = storm::parser::CslParser::parseCslFormula(input),
  188. storm::exceptions::WrongFormatException
  189. );
  190. }
  191. TEST(CslParserTest, wrongFormulaTest) {
  192. std::string input = "(a | b) & +";
  193. std::shared_ptr<csl::CslFilter<double>> formula(nullptr);
  194. ASSERT_THROW(
  195. formula = storm::parser::CslParser::parseCslFormula(input),
  196. storm::exceptions::WrongFormatException
  197. );
  198. }
  199. TEST(CslParserTest, wrongFormulaTest2) {
  200. std::string input = "P>0 [ F & a ]";
  201. std::shared_ptr<csl::CslFilter<double>> formula(nullptr);
  202. ASSERT_THROW(
  203. formula = storm::parser::CslParser::parseCslFormula(input),
  204. storm::exceptions::WrongFormatException
  205. );
  206. }