Repository: pytransitions/transitions

Issue:
GraphMachine markup should update if machine structure changes
I am experimenting with modifying HierarchicalGraphMachine for my particular use case: using `remove_transition` and/or removing states. I noticed that the GraphMachine family does not update its markup when structural changes occur.

Here is some example code I was messing with...

```python
from transitions.extensions import HierarchicalGraphMachine as HGM
states = ['a','b','c']
initial = 'a'
machine = HGM(states=states, initial='a', auto_transitions=False)
machine.add_transition(trigger='t_ab', source='a',dest='b')
machine.add_transition(trigger='t_bc', source='b',dest='c')
machine.add_transition(trigger='t_ca', source='c',dest='a')
machine.get_graph().draw('machine1.png', format="png", prog='dot')
```
And this produces:
![machine1](https://user-images.githubusercontent.com/1869285/210273838-96cb7c18-febe-41c6-bba9-3a28d8bab30b.png)

Then I take a couple steps:
```python
machine.t_ab()
machine.t_bc()
```
![machine3](https://user-images.githubusercontent.com/1869285/210273906-79376a45-da20-4c55-96fc-932dfbe5ae0b.png)
Now I remove a transition for fun:
```python
machine.remove_transition(trigger='t_ca')
```
The transition is removed correctly (no more `t_ca`), but the markup still has transition as evidenced in this final graph drawing:

![machine4](https://user-images.githubusercontent.com/1869285/210274121-eb9e06a0-a1b7-41e2-8b88-4f16843d6116.png)
and by looking at the markup:
```python
machine.markup['transitions']
```
`[{'source': 'a', 'dest': 'b', 'trigger': 't_ab'}, {'source': 'b', 'dest': 'c', 'trigger': 't_bc'}, {'source': 'c', 'dest': 'a', 'trigger': 't_ca'}]`

It could be I am doing this wrong, but I would expect that removing a transition or state should update the markup that is used to drive the graph output. If this seems reasonable, where would such a feature be implemented? Thanks!



Discussion:
Hello @drpjm,

very well presented issue. Many thanks for that. Markup as well as diagrams should update when a transition is removed. Changes in [5c89a24](https://github.com/pytransitions/transitions/commit/5c89a24c71130130bf959c4d1b9a12ceb9fcf26f) should address this. However, regeneration of graphs will remove the styling of previous transitions. This information is not stored anywhere and just 'applied' to a model's graph when a state changes. Consequently, your code example would end up with a graph like this:

![machine2](https://user-images.githubusercontent.com/205986/210333983-b1e17465-7a8a-4739-9318-aed215095e8c.png)

Great! The current state is still indicated, which is just fine. Would it make sense for there to be a method for removal of states too and that would be reflected in the markup? I did not find a `remove_state` function, so I have been doing something like this (based on example above):
```python
del machine.states['a']
```
This operates on the `OrderedDict` directly. Would it make sense for there to be a wrapping function similar to `remove_transition`, but it safely removes the state? Or is there a better way?
A briefly scanned the code and it seems like deleting states dynamically has not been a use case so far. But since removing transitions is supported, supporting the removal of states is not far-fetched at all. I don't see any particular reason why this should not be supported.

> [...] so I have been doing something like this (based on example above): del machine.states['a']

I haven't tested in but it looks okay-ish. It will not remove convenience functions previously attached to models like "mode.is_a()" though.
Thanks for the fix and discussion. I will play with cleanly removing states dynamically and let you know if I come up with a way to do it that would be good for the library.
> I will play with cleanly removing states dynamically and let you know if I come up with a way to do it that would be good for the library.

sounds good. adding this feature is quite straight forward I guess. `remove_transition`, `add_state` and `add_model_to_state` are the methods to have a look at. We usually decorate the model via `setattr` as far as I remember.
In case it helps, I've found that calling `machine.get_graph(force_new=True)` to obtain the graph means it will pick up anything that has changed.



Patch:
diff --git a/Changelog.md b/Changelog.md
index 48b68a27..348fe5a6 100644
--- a/Changelog.md
+++ b/Changelog.md
@@ -11,6 +11,7 @@
 - PR #633: Remove surrounding whitespace from docstrings (thanks @artofhuman)
 - PR #665: Add `on_final` to `Machine` and `NestedState` constructor and `final` to `State`. Callbacks passed to `on_final` will be executed when a State with `final=True` is entered or all children of a parallel state are final.
 - Bug #626: Process exceptions with `Machine.on_exception` in may_<trigger> as well (thanks @match1)
+- PR #666: Improved handling of removing transitions for `GraphMachine` and `HiearachicalMachine` (thanks @drpjm)
 - Typing:
   + Made state, event and machine property mandatory in (Nested)EventData
   + Transition.dest may be None
diff --git a/transitions/extensions/diagrams.py b/transitions/extensions/diagrams.py
index cbee76eb..28badbd0 100644
--- a/transitions/extensions/diagrams.py
+++ b/transitions/extensions/diagrams.py
@@ -260,8 +260,9 @@ def add_transition(self, trigger, source, dest, conditions=None, unless=None, be
 
     def remove_transition(self, trigger, source="*", dest="*"):
         super(GraphMachine, self).remove_transition(trigger, source, dest)
+        # update all model graphs since some transitions might be gone
         for model in self.models:
-            model.get_graph(force_new=True)
+            _ = model.get_graph(force_new=True)
 
 
 class NestedGraphTransition(TransitionGraphSupport, NestedTransition):
diff --git a/transitions/extensions/markup.pyi b/transitions/extensions/markup.pyi
index 080c0003..c4ea6efa 100644
--- a/transitions/extensions/markup.pyi
+++ b/transitions/extensions/markup.pyi
@@ -8,7 +8,7 @@ from enum import Enum
 
 # mypy does not support recursive definitions (yet), we need to use Any instead of 'MarkupConfig'
 class MarkupConfig(TypedDict):
-    transitions: List[TransitionConfig]
+    transitions: List[Dict[str, Any]]
 
 class MarkupMachine(Machine):
     state_attributes: List[str]
diff --git a/transitions/extensions/nesting.py b/transitions/extensions/nesting.py
index d060ed16..06fe3ed5 100644
--- a/transitions/extensions/nesting.py
+++ b/transitions/extensions/nesting.py
@@ -705,12 +705,57 @@ def get_transitions(self, trigger="", source="*", dest="*", delegate=False):
                 return matches
             source_path.pop()
             while source_path:
-                matches.extend(self.get_transitions(trigger,
-                                                    source=self.state_cls.separator.join(source_path),
-                                                    dest=dest))
+                matches.extend(self.get_nested_transitions(trigger,
+                                                           src_path=source_path,
+                                                           dest_path=dest_path))
                 source_path.pop()
             return matches
 
+    def _remove_nested_transitions(self, trigger, src_path, dest_path):
+        """Remove transitions from nested states.
+        Args:
+            trigger (str): Trigger name of the transition to be removed.
+            src_path (list(str)): If empty, all transitions that match dest_path and trigger will be removed.
+            dest_path (list(str)): If empty, all transitions that match src_path and trigger will be removed.
+        """
+        cur_src = self.state_cls.separator.join(src_path)
+        cur_dst = self.state_cls.separator.join(dest_path)
+        if trigger in self.scoped.events:
+            evt = self.scoped.events[trigger]
+            for src, transitions in evt.transitions.items():
+                evt.transitions[src] = [trans for trans in transitions
+                                        if (src_path and trans.source != cur_src) or (cur_dst and trans.dest != cur_dst)]
+        for state_name in self.scoped.states:
+            with self(state_name):
+                if state_name in [cur_src, cur_dst]:
+                    continue
+                self._remove_nested_transitions(trigger,
+                                                src_path if not src_path or state_name != src_path[0] else src_path[1:],
+                                                dest_path if not dest_path or state_name != dest_path[0] else dest_path[1:])
+
+    def remove_transition(self, trigger, source="*", dest="*"):
+        """Removes transitions matching the passed criteria.
+        Args:
+            trigger (str): Trigger name of the transition.
+            source (str, State or Enum): Limits list to transitions from a certain state.
+            dest (str, State or Enum): Limits list to transitions to a certain state.
+        """
+        with self():
+            source_path = [] if source == "*" \
+                else source.split(self.state_cls.separator) if isinstance(source, string_types) \
+                else self._get_enum_path(source) if isinstance(source, Enum) \
+                else self._get_state_path(source)
+            dest_path = [] if dest == "*" \
+                else dest.split(self.state_cls.separator) if isinstance(dest, string_types) \
+                else self._get_enum_path(dest) if isinstance(dest, Enum) \
+                else self._get_state_path(dest)
+            self._remove_nested_transitions(trigger, source_path, dest_path)
+
+        # remove trigger from models if no transition is left for trigger
+        if not self.get_transitions(trigger):
+            for model in self.models:
+                delattr(model, trigger)
+
     def _can_trigger(self, model, trigger, *args, **kwargs):
         state_tree = self.build_state_tree(getattr(model, self.model_attribute), self.state_cls.separator)
         ordered_states = resolve_order(state_tree)


Test patch:
diff --git a/tests/test_graphviz.py b/tests/test_graphviz.py
index 3244a790..c1b4564c 100644
--- a/tests/test_graphviz.py
+++ b/tests/test_graphviz.py
@@ -289,6 +289,16 @@ def test_function_callbacks_annotation(self):
         _, nodes, edges = self.parse_dot(m.get_graph())
         self.assertIn("[is_state(A", edges[0])
 
+    def test_update_on_remove_transition(self):
+        m = self.machine_cls(states=self.states, transitions=self.transitions, initial='A',
+                             use_pygraphviz=self.use_pygraphviz, show_state_attributes=True)
+        _, _, edges = self.parse_dot(m.get_graph())
+        assert "[label=walk]" in edges
+        m.remove_transition(trigger="walk", source="A", dest="B")
+        _, _, edges = self.parse_dot(m.get_graph())
+        assert not any("walk" == t["trigger"] for t in m.markup["transitions"])
+        assert "[label=walk]" not in edges
+
 
 @skipIf(pgv is None, 'Graph diagram test requires graphviz')
 class TestDiagramsLocked(TestDiagrams):
diff --git a/tests/test_nesting.py b/tests/test_nesting.py
index 62001ed2..4ce25521 100644
--- a/tests/test_nesting.py
+++ b/tests/test_nesting.py
@@ -973,6 +973,25 @@ def test_machine_may_transitions(self):
         assert m.may_sprint()
         m.run_fast()
 
+    def test_remove_nested_transition(self):
+        separator = self.state_cls.separator
+        state = {
+            'name': 'B',
+            'children': ['1', '2'],
+            'transitions': [['jo', '1', '2']],
+            'initial': '2'
+        }
+        m = self.stuff.machine_cls(initial='A', states=['A', state],
+                                   transitions=[['go', 'A', 'B'], ['go', 'B{0}2'.format(separator),
+                                                                   'B{0}1'.format(separator)]])
+        m.remove_transition("go", "A", "B")
+        assert m.get_transitions("go")
+        m.remove_transition("go")
+        assert not m.get_transitions("go")
+        assert m.get_transitions("jo")
+        m.remove_transition("jo")
+        assert not m.get_transitions("jo")
+
 
 class TestSeparatorsSlash(TestSeparatorsBase):
     separator = '/'
diff --git a/tests/test_pygraphviz.py b/tests/test_pygraphviz.py
index da7984d6..051a1579 100644
--- a/tests/test_pygraphviz.py
+++ b/tests/test_pygraphviz.py
@@ -97,7 +97,20 @@ class CustomMachine(self.machine_cls):  # type: ignore
         self.states[0] = {'name': 'A', 'tags': ['new', 'polling'], 'timeout': 5, 'on_enter': 'say_hello',
                           'on_exit': 'say_goodbye', 'on_timeout': 'do_something'}
         m = CustomMachine(states=self.states, transitions=self.transitions, initial='A', show_state_attributes=True)
-        g = m.get_graph(show_roi=True)
+        _ = m.get_graph(show_roi=True)
+
+    def test_update_on_remove_transition(self):
+
+        m = self.machine_cls(states=self.states, transitions=self.transitions, initial='A', show_state_attributes=True)
+        g = m.get_graph()
+        e = g.get_edge("A", "B")
+        assert e in g.edges()
+        m.remove_transition(trigger="walk", source="A", dest="B")
+        assert not any("walk" == t["trigger"] for t in m.markup["transitions"])
+        g = m.get_graph()
+        with self.assertRaises(KeyError):
+            _ = g.get_edge("A", "B")
+        assert e not in g.edges()
 
 
 @skipIf(pgv is None, 'NestedGraph diagram requires pygraphviz')