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.

150 lines
6.6 KiB

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