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.

118 lines
5.4 KiB

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