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.

269 lines
12 KiB

  1. /*
  2. * NondeterministicSparseTransitionParserTest.cpp
  3. *
  4. * Created on: Feb 26, 2014
  5. * Author: Manuel Sascha Weiand
  6. */
  7. #include "gtest/gtest.h"
  8. #include "storm-config.h"
  9. #include "src/parser/NondeterministicSparseTransitionParser.h"
  10. #include "src/storage/SparseMatrix.h"
  11. #include "src/settings/InternalOptionMemento.h"
  12. #include "src/exceptions/FileIoException.h"
  13. #include "src/exceptions/WrongFormatException.h"
  14. TEST(NondeterministicSparseTransitionParserTest, NonExistingFile) {
  15. // No matter what happens, please do NOT create a file with the name "nonExistingFile.not"!
  16. ASSERT_THROW(storm::parser::NondeterministicSparseTransitionParser::parseNondeterministicTransitions(STORM_CPP_TESTS_BASE_PATH "/nonExistingFile.not"), storm::exceptions::FileIoException);
  17. storm::storage::SparseMatrix<double> nullInformation;
  18. ASSERT_THROW(storm::parser::NondeterministicSparseTransitionParser::parseNondeterministicTransitionRewards(STORM_CPP_TESTS_BASE_PATH "/nonExistingFile.not", nullInformation), storm::exceptions::FileIoException);
  19. }
  20. TEST(NondeterministicSparseTransitionParserTest, BasicTransitionsParsing) {
  21. // Parse a nondeterministic transitions file and test the result.
  22. storm::storage::SparseMatrix<double> result(storm::parser::NondeterministicSparseTransitionParser::parseNondeterministicTransitions(STORM_CPP_TESTS_BASE_PATH "/functional/parser/tra_files/mdp_general.tra"));
  23. // Test the row mapping, i.e. at which row which state starts.
  24. ASSERT_EQ(6, result.getRowGroupCount());
  25. ASSERT_EQ(7, result.getRowGroupIndices().size());
  26. ASSERT_EQ(0, result.getRowGroupIndices()[0]);
  27. ASSERT_EQ(4, result.getRowGroupIndices()[1]);
  28. ASSERT_EQ(5, result.getRowGroupIndices()[2]);
  29. ASSERT_EQ(7, result.getRowGroupIndices()[3]);
  30. ASSERT_EQ(8, result.getRowGroupIndices()[4]);
  31. ASSERT_EQ(9, result.getRowGroupIndices()[5]);
  32. ASSERT_EQ(11, result.getRowGroupIndices()[6]);
  33. // Test the transition matrix.
  34. ASSERT_EQ(6, result.getColumnCount());
  35. ASSERT_EQ(11, result.getRowCount());
  36. ASSERT_EQ(22, result.getEntryCount());
  37. // Test every entry of the matrix.
  38. storm::storage::SparseMatrix<double>::const_iterator cIter = result.begin(0);
  39. ASSERT_EQ(0, cIter->getColumn());
  40. ASSERT_EQ(0.9, cIter->getValue());
  41. cIter++;
  42. ASSERT_EQ(1, cIter->getColumn());
  43. ASSERT_EQ(0.1, cIter->getValue());
  44. cIter++;
  45. ASSERT_EQ(1, cIter->getColumn());
  46. ASSERT_EQ(0.2, cIter->getValue());
  47. cIter++;
  48. ASSERT_EQ(2, cIter->getColumn());
  49. ASSERT_EQ(0.2, cIter->getValue());
  50. cIter++;
  51. ASSERT_EQ(3, cIter->getColumn());
  52. ASSERT_EQ(0.2, cIter->getValue());
  53. cIter++;
  54. ASSERT_EQ(4, cIter->getColumn());
  55. ASSERT_EQ(0.2, cIter->getValue());
  56. cIter++;
  57. ASSERT_EQ(5, cIter->getColumn());
  58. ASSERT_EQ(0.2, cIter->getValue());
  59. cIter++;
  60. ASSERT_EQ(5, cIter->getColumn());
  61. ASSERT_EQ(1, cIter->getValue());
  62. cIter++;
  63. ASSERT_EQ(0, cIter->getColumn());
  64. ASSERT_EQ(0.1, cIter->getValue());
  65. cIter++;
  66. ASSERT_EQ(4, cIter->getColumn());
  67. ASSERT_EQ(0.9, cIter->getValue());
  68. cIter++;
  69. ASSERT_EQ(2, cIter->getColumn());
  70. ASSERT_EQ(1, cIter->getValue());
  71. cIter++;
  72. ASSERT_EQ(2, cIter->getColumn());
  73. ASSERT_EQ(0.5, cIter->getValue());
  74. cIter++;
  75. ASSERT_EQ(3, cIter->getColumn());
  76. ASSERT_EQ(0.5, cIter->getValue());
  77. cIter++;
  78. ASSERT_EQ(2, cIter->getColumn());
  79. ASSERT_EQ(1, cIter->getValue());
  80. cIter++;
  81. ASSERT_EQ(2, cIter->getColumn());
  82. ASSERT_EQ(0.001, cIter->getValue());
  83. cIter++;
  84. ASSERT_EQ(3, cIter->getColumn());
  85. ASSERT_EQ(0.999, cIter->getValue());
  86. cIter++;
  87. ASSERT_EQ(1, cIter->getColumn());
  88. ASSERT_EQ(0.7, cIter->getValue());
  89. cIter++;
  90. ASSERT_EQ(4, cIter->getColumn());
  91. ASSERT_EQ(0.3, cIter->getValue());
  92. cIter++;
  93. ASSERT_EQ(1, cIter->getColumn());
  94. ASSERT_EQ(0.2, cIter->getValue());
  95. cIter++;
  96. ASSERT_EQ(4, cIter->getColumn());
  97. ASSERT_EQ(0.2, cIter->getValue());
  98. cIter++;
  99. ASSERT_EQ(5, cIter->getColumn());
  100. ASSERT_EQ(0.6, cIter->getValue());
  101. cIter++;
  102. ASSERT_EQ(5, cIter->getColumn());
  103. ASSERT_EQ(1, cIter->getValue());
  104. }
  105. TEST(NondeterministicSparseTransitionParserTest, BasicTransitionsRewardsParsing) {
  106. // Parse a nondeterministic transitions file and test the result.
  107. storm::storage::SparseMatrix<double> modelInformation(storm::parser::NondeterministicSparseTransitionParser::parseNondeterministicTransitions(STORM_CPP_TESTS_BASE_PATH "/functional/parser/tra_files/mdp_general.tra"));
  108. storm::storage::SparseMatrix<double> result(storm::parser::NondeterministicSparseTransitionParser::parseNondeterministicTransitionRewards(STORM_CPP_TESTS_BASE_PATH "/functional/parser/rew_files/mdp_general.trans.rew", modelInformation));
  109. // Test the transition matrix.
  110. ASSERT_EQ(6, result.getColumnCount());
  111. ASSERT_EQ(11, result.getRowCount());
  112. ASSERT_EQ(17, result.getEntryCount());
  113. // Test every entry of the matrix.
  114. storm::storage::SparseMatrix<double>::const_iterator cIter = result.begin(0);
  115. ASSERT_EQ(0, cIter->getColumn());
  116. ASSERT_EQ(1, cIter->getValue());
  117. cIter++;
  118. ASSERT_EQ(1, cIter->getColumn());
  119. ASSERT_EQ(30, cIter->getValue());
  120. cIter++;
  121. ASSERT_EQ(1, cIter->getColumn());
  122. ASSERT_EQ(15.2, cIter->getValue());
  123. cIter++;
  124. ASSERT_EQ(2, cIter->getColumn());
  125. ASSERT_EQ(75, cIter->getValue());
  126. cIter++;
  127. ASSERT_EQ(5, cIter->getColumn());
  128. ASSERT_EQ(2.45, cIter->getValue());
  129. cIter++;
  130. ASSERT_EQ(5, cIter->getColumn());
  131. ASSERT_EQ(1, cIter->getValue());
  132. cIter++;
  133. ASSERT_EQ(0, cIter->getColumn());
  134. ASSERT_EQ(0.114, cIter->getValue());
  135. cIter++;
  136. ASSERT_EQ(4, cIter->getColumn());
  137. ASSERT_EQ(90, cIter->getValue());
  138. cIter++;
  139. ASSERT_EQ(2, cIter->getColumn());
  140. ASSERT_EQ(1, cIter->getValue());
  141. cIter++;
  142. ASSERT_EQ(2, cIter->getColumn());
  143. ASSERT_EQ(55, cIter->getValue());
  144. cIter++;
  145. ASSERT_EQ(3, cIter->getColumn());
  146. ASSERT_EQ(87, cIter->getValue());
  147. cIter++;
  148. ASSERT_EQ(2, cIter->getColumn());
  149. ASSERT_EQ(13, cIter->getValue());
  150. cIter++;
  151. ASSERT_EQ(3, cIter->getColumn());
  152. ASSERT_EQ(999, cIter->getValue());
  153. cIter++;
  154. ASSERT_EQ(1, cIter->getColumn());
  155. ASSERT_EQ(0.7, cIter->getValue());
  156. cIter++;
  157. ASSERT_EQ(4, cIter->getColumn());
  158. ASSERT_EQ(0.3, cIter->getValue());
  159. cIter++;
  160. ASSERT_EQ(1, cIter->getColumn());
  161. ASSERT_EQ(0.1, cIter->getValue());
  162. cIter++;
  163. ASSERT_EQ(5, cIter->getColumn());
  164. ASSERT_EQ(6, cIter->getValue());
  165. }
  166. TEST(NondeterministicSparseTransitionParserTest, Whitespaces) {
  167. // Test the resilience of the parser against whitespaces.
  168. // Do so by comparing the hashes of the transition matices and the rowMapping vectors element by element.
  169. storm::storage::SparseMatrix<double> correctResult(storm::parser::NondeterministicSparseTransitionParser::parseNondeterministicTransitions(STORM_CPP_TESTS_BASE_PATH "/functional/parser/tra_files/mdp_general.tra"));
  170. storm::storage::SparseMatrix<double> whitespaceResult = storm::parser::NondeterministicSparseTransitionParser::parseNondeterministicTransitions(STORM_CPP_TESTS_BASE_PATH "/functional/parser/tra_files/mdp_whitespaces.tra");
  171. ASSERT_EQ(correctResult.hash(), whitespaceResult.hash());
  172. ASSERT_EQ(correctResult.getRowGroupIndices().size(), whitespaceResult.getRowGroupIndices().size());
  173. for(uint_fast64_t i = 0; i < correctResult.getRowGroupIndices().size(); i++) {
  174. ASSERT_EQ(correctResult.getRowGroupIndices()[i], whitespaceResult.getRowGroupIndices()[i]);
  175. }
  176. // Do the same (minus the unused rowMapping) for the corresponding transition rewards file (with and without whitespaces)
  177. uint_fast64_t correctHash = storm::parser::NondeterministicSparseTransitionParser::parseNondeterministicTransitionRewards(STORM_CPP_TESTS_BASE_PATH "/functional/parser/rew_files/mdp_general.trans.rew", correctResult).hash();
  178. ASSERT_EQ(correctHash, storm::parser::NondeterministicSparseTransitionParser::parseNondeterministicTransitionRewards(STORM_CPP_TESTS_BASE_PATH "/functional/parser/rew_files/mdp_whitespaces.trans.rew", whitespaceResult).hash());
  179. }
  180. TEST(NondeterministicSparseTransitionParserTest, MixedTransitionOrder) {
  181. // Since the MatrixBuilder needs sequential input of new elements reordering of transitions or states should throw an exception.
  182. ASSERT_THROW(storm::parser::NondeterministicSparseTransitionParser::parseNondeterministicTransitions(STORM_CPP_TESTS_BASE_PATH "/functional/parser/tra_files/mdp_mixedTransitionOrder.tra"), storm::exceptions::InvalidArgumentException);
  183. ASSERT_THROW(storm::parser::NondeterministicSparseTransitionParser::parseNondeterministicTransitions(STORM_CPP_TESTS_BASE_PATH "/functional/parser/tra_files/mdp_mixedStateOrder.tra"), storm::exceptions::InvalidArgumentException);
  184. storm::storage::SparseMatrix<double> modelInformation = storm::parser::NondeterministicSparseTransitionParser::parseNondeterministicTransitions(STORM_CPP_TESTS_BASE_PATH "/functional/parser/tra_files/mdp_general.tra");
  185. ASSERT_THROW(storm::parser::NondeterministicSparseTransitionParser::parseNondeterministicTransitionRewards(STORM_CPP_TESTS_BASE_PATH "/functional/parser/rew_files/mdp_mixedTransitionOrder.trans.rew", modelInformation), storm::exceptions::InvalidArgumentException);
  186. ASSERT_THROW(storm::parser::NondeterministicSparseTransitionParser::parseNondeterministicTransitionRewards(STORM_CPP_TESTS_BASE_PATH "/functional/parser/rew_files/mdp_mixedStateOrder.trans.rew", modelInformation), storm::exceptions::InvalidArgumentException);
  187. }
  188. TEST(NondeterministicSparseTransitionParserTest, FixDeadlocks) {
  189. // Set the fixDeadlocks flag temporarily. It is set to its old value once the deadlockOption object is destructed.
  190. storm::settings::InternalOptionMemento setDeadlockOption("fixDeadlocks", true);
  191. // Parse a transitions file with the fixDeadlocks Flag set and test if it works.
  192. storm::storage::SparseMatrix<double> result(storm::parser::NondeterministicSparseTransitionParser::parseNondeterministicTransitions(STORM_CPP_TESTS_BASE_PATH "/functional/parser/tra_files/mdp_deadlock.tra"));
  193. ASSERT_EQ(8, result.getRowGroupIndices().size());
  194. ASSERT_EQ(9, result.getRowGroupIndices()[5]);
  195. ASSERT_EQ(10, result.getRowGroupIndices()[6]);
  196. ASSERT_EQ(12, result.getRowGroupIndices()[7]);
  197. ASSERT_EQ(7, result.getColumnCount());
  198. ASSERT_EQ(12, result.getRowCount());
  199. ASSERT_EQ(23, result.getEntryCount());
  200. storm::storage::SparseMatrix<double>::const_iterator cIter = result.begin(8);
  201. ASSERT_EQ(1, cIter->getColumn());
  202. ASSERT_EQ(0.7, cIter->getValue());
  203. cIter++;
  204. ASSERT_EQ(4, cIter->getColumn());
  205. ASSERT_EQ(0.3, cIter->getValue());
  206. cIter++;
  207. ASSERT_EQ(5, cIter->getColumn());
  208. ASSERT_EQ(1, cIter->getValue());
  209. cIter++;
  210. ASSERT_EQ(1, cIter->getColumn());
  211. ASSERT_EQ(0.2, cIter->getValue());
  212. cIter++;
  213. ASSERT_EQ(4, cIter->getColumn());
  214. ASSERT_EQ(0.2, cIter->getValue());
  215. cIter++;
  216. ASSERT_EQ(5, cIter->getColumn());
  217. ASSERT_EQ(0.6, cIter->getValue());
  218. cIter++;
  219. ASSERT_EQ(5, cIter->getColumn());
  220. ASSERT_EQ(1, cIter->getValue());
  221. }
  222. TEST(NondeterministicSparseTransitionParserTest, DontFixDeadlocks) {
  223. // Try to parse a transitions file containing a deadlock state with the fixDeadlocksFlag unset. This should throw an exception.
  224. storm::settings::InternalOptionMemento unsetDeadlockOption("fixDeadlocks", false);
  225. ASSERT_THROW(storm::parser::NondeterministicSparseTransitionParser::parseNondeterministicTransitions(STORM_CPP_TESTS_BASE_PATH "/functional/parser/tra_files/mdp_deadlock.tra"), storm::exceptions::WrongFormatException);
  226. }
  227. TEST(NondeterministicSparseTransitionParserTest, DoubledLines) {
  228. // There is a redundant line in the transition file. As the transition already exists this should throw an exception.
  229. ASSERT_THROW(storm::parser::NondeterministicSparseTransitionParser::parseNondeterministicTransitions(STORM_CPP_TESTS_BASE_PATH "/functional/parser/tra_files/mdp_doubledLines.tra"), storm::exceptions::InvalidArgumentException);
  230. }
  231. TEST(NondeterministicSparseTransitionParserTest, RewardForNonExistentTransition) {
  232. // First parse a transition file. Then parse a transition reward file for the resulting transition matrix.
  233. storm::storage::SparseMatrix<double> transitionResult = storm::parser::NondeterministicSparseTransitionParser::parseNondeterministicTransitions(STORM_CPP_TESTS_BASE_PATH "/functional/parser/tra_files/mdp_general.tra");
  234. // There is a reward for a transition that does not exist in the transition matrix.
  235. ASSERT_THROW(storm::parser::NondeterministicSparseTransitionParser::parseNondeterministicTransitionRewards(STORM_CPP_TESTS_BASE_PATH "/functional/parser/rew_files/mdp_rewardForNonExTrans.trans.rew", transitionResult), storm::exceptions::WrongFormatException);
  236. }