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.

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