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.

149 lines
6.4 KiB

  1. #include "gtest/gtest.h"
  2. #include "storm-config.h"
  3. #include "src/parser/FormulaParser.h"
  4. #include "src/exceptions/WrongFormatException.h"
  5. TEST(FormulaParserTest, LabelTest) {
  6. storm::parser::FormulaParser formulaParser;
  7. std::string input = "\"label\"";
  8. std::shared_ptr<storm::logic::Formula> formula(nullptr);
  9. ASSERT_NO_THROW(formula = formulaParser.parseSingleFormulaFromString(input));
  10. EXPECT_TRUE(formula->isAtomicLabelFormula());
  11. }
  12. TEST(FormulaParserTest, ComplexLabelTest) {
  13. storm::parser::FormulaParser formulaParser;
  14. std::string input = "!(\"a\" & \"b\") | \"a\" & !\"c\"";
  15. std::shared_ptr<storm::logic::Formula> formula(nullptr);
  16. ASSERT_NO_THROW(formula = formulaParser.parseSingleFormulaFromString(input));
  17. EXPECT_TRUE(formula->isPropositionalFormula());
  18. EXPECT_TRUE(formula->isBinaryBooleanStateFormula());
  19. }
  20. TEST(FormulaParserTest, ExpressionTest) {
  21. std::shared_ptr<storm::expressions::ExpressionManager> manager(new storm::expressions::ExpressionManager());
  22. manager->declareBooleanVariable("x");
  23. manager->declareIntegerVariable("y");
  24. storm::parser::FormulaParser formulaParser(manager);
  25. std::string input = "!(x | y > 3)";
  26. std::shared_ptr<storm::logic::Formula> formula(nullptr);
  27. ASSERT_NO_THROW(formula = formulaParser.parseSingleFormulaFromString(input));
  28. EXPECT_TRUE(formula->isPropositionalFormula());
  29. EXPECT_TRUE(formula->isUnaryBooleanStateFormula());
  30. }
  31. TEST(FormulaParserTest, LabelAndExpressionTest) {
  32. std::shared_ptr<storm::expressions::ExpressionManager> manager(new storm::expressions::ExpressionManager());
  33. manager->declareBooleanVariable("x");
  34. manager->declareIntegerVariable("y");
  35. storm::parser::FormulaParser formulaParser(manager);
  36. std::string input = "!\"a\" | x | y > 3";
  37. std::shared_ptr<storm::logic::Formula> formula(nullptr);
  38. ASSERT_NO_THROW(formula = formulaParser.parseSingleFormulaFromString(input));
  39. EXPECT_TRUE(formula->isPropositionalFormula());
  40. input = "x | y > 3 | !\"a\"";
  41. ASSERT_NO_THROW(formula = formulaParser.parseSingleFormulaFromString(input));
  42. EXPECT_TRUE(formula->isPropositionalFormula());
  43. }
  44. TEST(FormulaParserTest, ProbabilityOperatorTest) {
  45. storm::parser::FormulaParser formulaParser;
  46. std::string input = "P<0.9 [\"a\" U \"b\"]";
  47. std::shared_ptr<storm::logic::Formula> formula(nullptr);
  48. ASSERT_NO_THROW(formula = formulaParser.parseSingleFormulaFromString(input));
  49. EXPECT_TRUE(formula->isProbabilityOperatorFormula());
  50. EXPECT_TRUE(formula->asProbabilityOperatorFormula().hasBound());
  51. EXPECT_FALSE(formula->asProbabilityOperatorFormula().hasOptimalityType());
  52. }
  53. TEST(FormulaParserTest, RewardOperatorTest) {
  54. storm::parser::FormulaParser formulaParser;
  55. std::string input = "Rmin<0.9 [F \"a\"]";
  56. std::shared_ptr<storm::logic::Formula> formula(nullptr);
  57. ASSERT_NO_THROW(formula = formulaParser.parseSingleFormulaFromString(input));
  58. EXPECT_TRUE(formula->isRewardOperatorFormula());
  59. EXPECT_TRUE(formula->asRewardOperatorFormula().hasBound());
  60. EXPECT_TRUE(formula->asRewardOperatorFormula().hasOptimalityType());
  61. input = "R=? [I=10]";
  62. ASSERT_NO_THROW(formula = formulaParser.parseSingleFormulaFromString(input));
  63. EXPECT_TRUE(formula->isRewardOperatorFormula());
  64. EXPECT_FALSE(formula->asRewardOperatorFormula().hasBound());
  65. EXPECT_FALSE(formula->asRewardOperatorFormula().hasOptimalityType());
  66. EXPECT_TRUE(formula->asRewardOperatorFormula().getSubformula().isInstantaneousRewardFormula());
  67. }
  68. TEST(FormulaParserTest, ConditionalProbabilityTest) {
  69. storm::parser::FormulaParser formulaParser;
  70. std::string input = "P<0.9 [F \"a\" || F \"b\"]";
  71. std::shared_ptr<storm::logic::Formula> formula(nullptr);
  72. ASSERT_NO_THROW(formula = formulaParser.parseSingleFormulaFromString(input));
  73. EXPECT_TRUE(formula->isProbabilityOperatorFormula());
  74. storm::logic::ProbabilityOperatorFormula const& probFormula = formula->asProbabilityOperatorFormula();
  75. EXPECT_TRUE(probFormula.getSubformula().isConditionalPathFormula());
  76. }
  77. TEST(FormulaParserTest, NestedPathFormulaTest) {
  78. storm::parser::FormulaParser formulaParser;
  79. std::string input = "P<0.9 [F X \"a\"]";
  80. std::shared_ptr<storm::logic::Formula> formula(nullptr);
  81. ASSERT_NO_THROW(formula = formulaParser.parseSingleFormulaFromString(input));
  82. EXPECT_TRUE(formula->isProbabilityOperatorFormula());
  83. ASSERT_TRUE(formula->asProbabilityOperatorFormula().getSubformula().isEventuallyFormula());
  84. ASSERT_TRUE(formula->asProbabilityOperatorFormula().getSubformula().asEventuallyFormula().getSubformula().isNextFormula());
  85. }
  86. TEST(FormulaParserTest, CommentTest) {
  87. storm::parser::FormulaParser formulaParser;
  88. std::string input = "// This is a comment. And this is a commented out formula: P<=0.5 [ F \"a\" ] The next line contains the actual formula. \n P<=0.5 [ X \"b\" ] // Another comment \n // And again: another comment.";
  89. std::shared_ptr<storm::logic::Formula> formula(nullptr);
  90. ASSERT_NO_THROW(formula = formulaParser.parseSingleFormulaFromString(input));
  91. EXPECT_TRUE(formula->isProbabilityOperatorFormula());
  92. ASSERT_TRUE(formula->asProbabilityOperatorFormula().getSubformula().isNextFormula());
  93. ASSERT_TRUE(formula->asProbabilityOperatorFormula().getSubformula().asNextFormula().getSubformula().isAtomicLabelFormula());
  94. }
  95. TEST(FormulaParserTest, WrongFormatTest) {
  96. std::shared_ptr<storm::expressions::ExpressionManager> manager(new storm::expressions::ExpressionManager());
  97. manager->declareBooleanVariable("x");
  98. manager->declareIntegerVariable("y");
  99. storm::parser::FormulaParser formulaParser(manager);
  100. std::string input = "P>0.5 [ a ]";
  101. std::shared_ptr<storm::logic::Formula> formula(nullptr);
  102. EXPECT_THROW(formula = formulaParser.parseSingleFormulaFromString(input), storm::exceptions::WrongFormatException);
  103. input = "P=0.5 [F \"a\"]";
  104. EXPECT_THROW(formula = formulaParser.parseSingleFormulaFromString(input), storm::exceptions::WrongFormatException);
  105. input = "P>0.5 [F !(x = 0)]";
  106. EXPECT_THROW(formula = formulaParser.parseSingleFormulaFromString(input), storm::exceptions::WrongFormatException);
  107. input = "P>0.5 [F !y]";
  108. EXPECT_THROW(formula = formulaParser.parseSingleFormulaFromString(input), storm::exceptions::WrongFormatException);
  109. input = "P>0.5 [F y!=0)]";
  110. EXPECT_THROW(formula = formulaParser.parseSingleFormulaFromString(input), storm::exceptions::WrongFormatException);
  111. }