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.

217 lines
8.9 KiB

  1. /*
  2. * MarkovAutomatonParserTest.cpp
  3. *
  4. * Created on: 03.12.2013
  5. * Author: Manuel Sascha Weiand
  6. */
  7. #include "gtest/gtest.h"
  8. #include "storm-config.h"
  9. #include "src/settings/Settings.h"
  10. #include <cmath>
  11. #include <vector>
  12. #include "src/parser/MarkovAutomatonSparseTransitionParser.h"
  13. #include "src/parser/SparseStateRewardParser.h"
  14. #include "src/parser/Parser.h"
  15. #include "src/parser/MarkovAutomatonParser.h"
  16. #define STATE_COUNT 6
  17. #define CHOICE_COUNT 7
  18. TEST(MarkovAutomatonSparseTransitionParserTest, BasicParseTest) {
  19. // The file that will be used for the test.
  20. std::string filename = STORM_CPP_TESTS_BASE_PATH "/functional/parser/tra_files/ma_general_input_01.tra";
  21. storm::parser::MarkovAutomatonSparseTransitionParser::ResultType result = storm::parser::MarkovAutomatonSparseTransitionParser::parseMarkovAutomatonTransitions(filename);
  22. // Test all sizes and counts.
  23. ASSERT_EQ(result.transitionMatrix.getColumnCount(), STATE_COUNT);
  24. ASSERT_EQ(result.transitionMatrix.getRowCount(), CHOICE_COUNT);
  25. ASSERT_EQ(result.transitionMatrix.getNonZeroEntryCount(), 12);
  26. ASSERT_EQ(result.markovianChoices.getSize(), CHOICE_COUNT);
  27. ASSERT_EQ(result.markovianStates.getSize(), STATE_COUNT);
  28. ASSERT_EQ(result.markovianStates.getNumberOfSetBits(), 2);
  29. ASSERT_EQ(result.exitRates.size(), STATE_COUNT);
  30. ASSERT_EQ(result.nondeterministicChoiceIndices.size(), 7);
  31. // Test the general structure of the transition system (that will be an Markov automaton).
  32. // Test the mapping between states and transition matrix rows.
  33. ASSERT_EQ(result.nondeterministicChoiceIndices[0], 0);
  34. ASSERT_EQ(result.nondeterministicChoiceIndices[1], 1);
  35. ASSERT_EQ(result.nondeterministicChoiceIndices[2], 2);
  36. ASSERT_EQ(result.nondeterministicChoiceIndices[3], 3);
  37. ASSERT_EQ(result.nondeterministicChoiceIndices[4], 4);
  38. ASSERT_EQ(result.nondeterministicChoiceIndices[5], 6);
  39. ASSERT_EQ(result.nondeterministicChoiceIndices[6], 7);
  40. // Test the Markovian states.
  41. ASSERT_TRUE(result.markovianStates.get(0));
  42. ASSERT_FALSE(result.markovianStates.get(1));
  43. ASSERT_TRUE(result.markovianStates.get(2));
  44. ASSERT_FALSE(result.markovianStates.get(3));
  45. ASSERT_FALSE(result.markovianStates.get(4));
  46. ASSERT_FALSE(result.markovianStates.get(5));
  47. // Test the exit rates. These have to be 0 for all non-Markovian states.
  48. ASSERT_EQ(result.exitRates[0], 2);
  49. ASSERT_EQ(result.exitRates[1], 0);
  50. ASSERT_EQ(result.exitRates[2], 15);
  51. ASSERT_EQ(result.exitRates[3], 0);
  52. ASSERT_EQ(result.exitRates[4], 0);
  53. ASSERT_EQ(result.exitRates[5], 0);
  54. // Finally, test the transition matrix itself.
  55. ASSERT_EQ(result.transitionMatrix.getValue(0,1), 2);
  56. ASSERT_EQ(result.transitionMatrix.getValue(1,2), 1);
  57. ASSERT_EQ(result.transitionMatrix.getValue(2,0), 1);
  58. ASSERT_EQ(result.transitionMatrix.getValue(2,1), 2);
  59. ASSERT_EQ(result.transitionMatrix.getValue(2,3), 4);
  60. ASSERT_EQ(result.transitionMatrix.getValue(2,4), 8);
  61. ASSERT_EQ(result.transitionMatrix.getValue(3,2), 0.5);
  62. ASSERT_EQ(result.transitionMatrix.getValue(3,3), 0.5);
  63. ASSERT_EQ(result.transitionMatrix.getValue(4,3), 1);
  64. ASSERT_EQ(result.transitionMatrix.getValue(5,1), 0.5);
  65. ASSERT_EQ(result.transitionMatrix.getValue(5,5), 0.5);
  66. ASSERT_EQ(result.transitionMatrix.getValue(6,5), 1);
  67. }
  68. TEST(MarkovAutomatonSparseTransitionParserTest, WhiteSpaceTest) {
  69. // The file that will be used for the test.
  70. std::string filename = STORM_CPP_TESTS_BASE_PATH "/functional/parser/tra_files/ma_whitespace_input_01.tra";
  71. storm::parser::MarkovAutomatonSparseTransitionParser::ResultType result = storm::parser::MarkovAutomatonSparseTransitionParser::parseMarkovAutomatonTransitions(filename);
  72. // Test all sizes and counts.
  73. ASSERT_EQ(result.transitionMatrix.getColumnCount(), STATE_COUNT);
  74. ASSERT_EQ(result.transitionMatrix.getRowCount(), CHOICE_COUNT);
  75. ASSERT_EQ(result.transitionMatrix.getNonZeroEntryCount(), 12);
  76. ASSERT_EQ(result.markovianChoices.getSize(), CHOICE_COUNT);
  77. ASSERT_EQ(result.markovianStates.getSize(), STATE_COUNT);
  78. ASSERT_EQ(result.markovianStates.getNumberOfSetBits(), 2);
  79. ASSERT_EQ(result.exitRates.size(), STATE_COUNT);
  80. ASSERT_EQ(result.nondeterministicChoiceIndices.size(), 7);
  81. // Test the general structure of the transition system (that will be an Markov automaton).
  82. // Test the mapping between states and transition matrix rows.
  83. ASSERT_EQ(result.nondeterministicChoiceIndices[0], 0);
  84. ASSERT_EQ(result.nondeterministicChoiceIndices[1], 1);
  85. ASSERT_EQ(result.nondeterministicChoiceIndices[2], 2);
  86. ASSERT_EQ(result.nondeterministicChoiceIndices[3], 3);
  87. ASSERT_EQ(result.nondeterministicChoiceIndices[4], 4);
  88. ASSERT_EQ(result.nondeterministicChoiceIndices[5], 6);
  89. ASSERT_EQ(result.nondeterministicChoiceIndices[6], 7);
  90. // Test the Markovian states.
  91. ASSERT_TRUE(result.markovianStates.get(0));
  92. ASSERT_FALSE(result.markovianStates.get(1));
  93. ASSERT_TRUE(result.markovianStates.get(2));
  94. ASSERT_FALSE(result.markovianStates.get(3));
  95. ASSERT_FALSE(result.markovianStates.get(4));
  96. ASSERT_FALSE(result.markovianStates.get(5));
  97. // Test the exit rates. These have to be 0 for all non-Markovian states.
  98. ASSERT_EQ(result.exitRates[0], 2);
  99. ASSERT_EQ(result.exitRates[1], 0);
  100. ASSERT_EQ(result.exitRates[2], 15);
  101. ASSERT_EQ(result.exitRates[3], 0);
  102. ASSERT_EQ(result.exitRates[4], 0);
  103. ASSERT_EQ(result.exitRates[5], 0);
  104. // Finally, test the transition matrix itself.
  105. ASSERT_EQ(result.transitionMatrix.getValue(0,1), 2);
  106. ASSERT_EQ(result.transitionMatrix.getValue(1,2), 1);
  107. ASSERT_EQ(result.transitionMatrix.getValue(2,0), 1);
  108. ASSERT_EQ(result.transitionMatrix.getValue(2,1), 2);
  109. ASSERT_EQ(result.transitionMatrix.getValue(2,3), 4);
  110. ASSERT_EQ(result.transitionMatrix.getValue(2,4), 8);
  111. ASSERT_EQ(result.transitionMatrix.getValue(3,2), 0.5);
  112. ASSERT_EQ(result.transitionMatrix.getValue(3,3), 0.5);
  113. ASSERT_EQ(result.transitionMatrix.getValue(4,3), 1);
  114. ASSERT_EQ(result.transitionMatrix.getValue(5,1), 0.5);
  115. ASSERT_EQ(result.transitionMatrix.getValue(5,5), 0.5);
  116. ASSERT_EQ(result.transitionMatrix.getValue(6,5), 1);
  117. }
  118. //TODO: Deadlock Test. I am quite sure that the deadlock state handling does not behave quite right.
  119. // Find a way to test this by manipulating the fixDeadlocks flag of the settings.
  120. /*
  121. TEST(MarkovAutomatonSparseTransitionParserTest, DeadlockTest) {
  122. // Save the fixDeadlocks flag, since it will be manipulated during the test.
  123. bool fixDeadlocks = storm::settings::Settings::getInstance()->isSet("fixDeadlocks");
  124. storm::settings::Settings::getInstance()->set("fixDeadlocks");
  125. // The file that will be used for the test.
  126. std::string filename = "/functional/parser/tra_files/ma_general_input_01.tra";
  127. storm::parser::MarkovAutomatonSparseTransitionParser::ResultType result = storm::parser::MarkovAutomatonSparseTransitionParser::parseMarkovAutomatonTransitions(filename);
  128. // Test if the result of the first pass has been transfered correctly
  129. ASSERT_EQ(result.transitionMatrix.colCount, 7);
  130. ASSERT_EQ(result.transitionMatrix.nonZeroEntryCount, 8);
  131. ASSERT_EQ(result.markovianChoices.getNumberOfSetBits(), 2);
  132. // Test the general structure of the transition system (that will be an Markov automaton).
  133. //TODO
  134. //Do the test again but this time without the fixDeadlock flag. This should throw an exception.
  135. storm::settings::Settings::getInstance()->unset("fixDeadlocks");
  136. bool thrown = false;
  137. try {
  138. // Parse the file, again.
  139. result = storm::parser::MarkovAutomatonSparseTransitionParser::parseMarkovAutomatonTransitions(filename);
  140. } catch(Exception &exception) {
  141. // Print the exception and remember that it was thrown.
  142. exception.print(std::cout);
  143. thrown = true;
  144. }
  145. ASSERT_TRUE(thrown);
  146. // Reset the fixDeadlocks flag to its original value.
  147. if(fixDeadlocks) {
  148. storm::settings::Settings::getInstance()->set("fixDeadlocks");
  149. }
  150. }*/
  151. double round(double val, int precision)
  152. {
  153. std::stringstream s;
  154. s << std::setprecision(precision) << std::setiosflags(std::ios_base::fixed) << val;
  155. s >> val;
  156. return val;
  157. }
  158. TEST(SparseStateRewardParserTest, BasicParseTest) {
  159. // Get the parsing result.
  160. std::vector<double> result = storm::parser::SparseStateRewardParser::parseSparseStateReward(100, STORM_CPP_TESTS_BASE_PATH "/functional/parser/rew_files/state_reward_parser_basic.state.rew");
  161. // Now test if the correct value were parsed.
  162. for(int i = 0; i < 100; i++) {
  163. ASSERT_EQ(std::round(result[i]) , std::round(2*i + 15/13*i*i - 1.5/(i+0.1) + 15.7));
  164. }
  165. }
  166. TEST(MarkovAutomatonParserTest, BasicParseTest) {
  167. // Get the parsing result.
  168. storm::models::MarkovAutomaton<double> result = storm::parser::MarkovAutomatonParser::parseMarkovAutomaton(STORM_CPP_TESTS_BASE_PATH "/functional/parser/tra_files/ma_general_input_01.tra", STORM_CPP_TESTS_BASE_PATH "/functional/parser/lab_files/ma_general_input_01.lab", STORM_CPP_TESTS_BASE_PATH "/functional/parser/rew_files/ma_general_input_01.state.rew", "");
  169. // Test sizes and counts.
  170. ASSERT_EQ(result.getNumberOfStates(), STATE_COUNT);
  171. ASSERT_EQ(result.getNumberOfChoices(), CHOICE_COUNT);
  172. ASSERT_EQ(result.getNumberOfTransitions(), 12);
  173. // Test
  174. std::vector<double> rates = result.getExitRates();
  175. ASSERT_EQ(rates[0], 2);
  176. }