Skip to content

Commit

Permalink
issue with state names is fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
PimLeerkes committed Feb 4, 2025
1 parent 37f8f53 commit 980ac3f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 10 deletions.
20 changes: 11 additions & 9 deletions stormvogel/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -753,7 +753,13 @@ def new_action(self, name: str, labels: frozenset[str] | None = None) -> Action:
return action

def reassign_ids(self):
"""reassigns the ids of states, transitions and rates to be in order again"""
"""reassigns the ids of states, transitions and rates to be in order again.
Mainly useful to keep consistent with storm."""

print(
"Warning: Using this can cause problems in your code if there are existing references to states by id."
)

self.states = {
new_id: value
for new_id, (old_id, value) in enumerate(sorted(self.states.items()))
Expand All @@ -777,11 +783,6 @@ def remove_state(
):
"""Properly removes a state, it can optionally normalize the model and reassign ids automatically."""

if reassign_ids:
print(
"Warning: Using this can cause problems in your code if there are existing references to states by id."
)

if state in self.states.values():
# we remove the state from the transitions
# first we remove transitions that go into the state
Expand Down Expand Up @@ -895,15 +896,16 @@ def new_state(
self,
labels: list[str] | str | None = None,
features: dict[str, int] | None = None,
name: str | None = None,
) -> State:
"""Creates a new state and returns it."""
state_id = self.__free_state_id()
if isinstance(labels, list):
state = State(labels, features or {}, state_id, self)
state = State(labels, features or {}, state_id, self, name=name)
elif isinstance(labels, str):
state = State([labels], features or {}, state_id, self)
state = State([labels], features or {}, state_id, self, name=name)
elif labels is None:
state = State([], features or {}, state_id, self)
state = State([], features or {}, state_id, self, name=name)

self.states[state_id] = state

Expand Down
14 changes: 13 additions & 1 deletion tests/test_model_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,19 @@ def test_remove_state():

assert state0 != state1

# This should complain that names are the same:
try:
new_dtmc.new_state()
assert False
except RuntimeError:
pass

# But no longer if we do this:
try:
new_dtmc.new_state(name="new_name")
except RuntimeError:
assert False


def test_remove_transitions_between_states():
# we make a model and remove transitions between two states
Expand Down Expand Up @@ -329,7 +342,6 @@ def test_get_sub_model():
[(1 / 6, new_dtmc.new_state(f"rolled{i}", {"rolled": i})) for i in range(2)]
)
new_dtmc.normalize()

assert sub_model == new_dtmc


Expand Down

0 comments on commit 980ac3f

Please sign in to comment.