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.

129 lines
5.9 KiB

3 months ago
  1. import stormpy
  2. from helpers.helper import get_example_path
  3. class TestState:
  4. def test_states_dtmc(self):
  5. model = stormpy.build_sparse_model_from_explicit(get_example_path("dtmc", "die.tra"),
  6. get_example_path("dtmc", "die.lab"))
  7. i = 0
  8. states = model.states
  9. assert len(states) == 13
  10. for state in states:
  11. assert state.id == i
  12. i += 1
  13. assert i == model.nr_states
  14. state = model.states[2]
  15. assert state.id == 2
  16. state = states[5]
  17. assert state.id == 5
  18. def test_states_mdp(self):
  19. model = stormpy.build_sparse_model_from_explicit(get_example_path("mdp", "two_dice.tra"),
  20. get_example_path("mdp", "two_dice.lab"))
  21. i = 0
  22. assert len(model.states) == 169
  23. for state in model.states:
  24. assert state.id == i
  25. i += 1
  26. assert i == model.nr_states
  27. states = model.states
  28. state = model.states[6]
  29. assert state.id == 6
  30. state = states[1]
  31. assert state.id == 1
  32. def test_labels_dtmc(self):
  33. model = stormpy.build_sparse_model_from_explicit(get_example_path("dtmc", "die.tra"),
  34. get_example_path("dtmc", "die.lab"))
  35. labelsOrig = [["init"], [], [], [], [], [], [], ["one", "done"], ["two", "done"], ["three", "done"],
  36. ["four", "done"], ["five", "done"], ["six", "done"]]
  37. i = 0
  38. for state in model.states:
  39. labels = state.labels
  40. for label in labelsOrig[i]:
  41. assert label in labels
  42. i += 1
  43. def test_actions_dtmc(self):
  44. model = stormpy.build_sparse_model_from_explicit(get_example_path("dtmc", "die.tra"),
  45. get_example_path("dtmc", "die.lab"))
  46. for state in model.states:
  47. assert len(state.actions) == 1
  48. for action in state.actions:
  49. assert action.id == 0
  50. def test_actions_mdp(self):
  51. model = stormpy.build_sparse_model_from_explicit(get_example_path("mdp", "two_dice.tra"),
  52. get_example_path("mdp", "two_dice.lab"))
  53. for state in model.states:
  54. assert len(state.actions) == 1 or len(state.actions) == 2
  55. for action in state.actions:
  56. assert action.id == 0 or action.id == 1
  57. def test_transitions_dtmc(self):
  58. transitions_orig = [(0, 1, 0.5), (0, 2, 0.5), (1, 3, 0.5), (1, 4, 0.5),
  59. (2, 5, 0.5), (2, 6, 0.5), (3, 1, 0.5), (3, 7, 0.5),
  60. (4, 8, 0.5), (4, 9, 0.5), (5, 10, 0.5), (5, 11, 0.5),
  61. (6, 2, 0.5), (6, 12, 0.5), (7, 7, 1), (8, 8, 1),
  62. (9, 9, 1), (10, 10, 1), (11, 11, 1), (12, 12, 1)
  63. ]
  64. model = stormpy.build_sparse_model_from_explicit(get_example_path("dtmc", "die.tra"),
  65. get_example_path("dtmc", "die.lab"))
  66. i = 0
  67. for state in model.states:
  68. for action in state.actions:
  69. assert (state.id < 7 and len(action.transitions) == 2) or (
  70. state.id >= 7 and len(action.transitions) == 1)
  71. for transition in action.transitions:
  72. transition_orig = transitions_orig[i]
  73. i += 1
  74. assert state.id == transition_orig[0]
  75. assert transition.column == transition_orig[1]
  76. assert transition.value() == transition_orig[2]
  77. def test_transitions_mdp(self):
  78. model = stormpy.build_sparse_model_from_explicit(get_example_path("mdp", "two_dice.tra"),
  79. get_example_path("mdp", "two_dice.lab"))
  80. assert model.states[1].id == 1
  81. for state in model.states:
  82. i = 0
  83. for action in state.actions:
  84. i += 1
  85. for transition in action.transitions:
  86. assert transition.value() == 0.5 or transition.value() == 1
  87. assert i == 1 or i == 2
  88. def test_row_iterator(self):
  89. transitions_orig = [(0, 1, 0.5), (0, 2, 0.5), (1, 3, 0.5), (1, 4, 0.5),
  90. (2, 5, 0.5), (2, 6, 0.5), (3, 1, 0.5), (3, 7, 0.5),
  91. (4, 8, 0.5), (4, 9, 0.5), (5, 10, 0.5), (5, 11, 0.5),
  92. (6, 2, 0.5), (6, 12, 0.5), (7, 7, 1), (8, 8, 1),
  93. (9, 9, 1), (10, 10, 1), (11, 11, 1), (12, 12, 1)
  94. ]
  95. model = stormpy.build_sparse_model_from_explicit(get_example_path("dtmc", "die.tra"),
  96. get_example_path("dtmc", "die.lab"))
  97. i = 0
  98. for state in model.states:
  99. for transition in model.transition_matrix.row_iter(state.id, state.id):
  100. transition_orig = transitions_orig[i]
  101. i += 1
  102. assert state.id == transition_orig[0]
  103. assert transition.column == transition_orig[1]
  104. assert transition.value() == transition_orig[2]
  105. def test_parametric_transitions(self):
  106. program = stormpy.parse_prism_program(get_example_path("pmdp", "two_dice.nm"))
  107. model = stormpy.build_parametric_model(program)
  108. assert model.states[1].id == 1
  109. one = stormpy.FactorizedPolynomial(stormpy.RationalRF(1))
  110. i = 0
  111. for state in model.states:
  112. assert state.id == i
  113. i += 1
  114. j = 0
  115. for action in state.actions:
  116. assert j == 0 or j == 1
  117. j += 1
  118. for transition in action.transitions:
  119. assert transition.value().denominator == one