pythongraph

"add_transition() argument after ** must be a mapping, not str" : transitions python library


I just try make simple graph with transitions library :

from transitions.extensions.diagrams import HierarchicalGraphMachine
from IPython.display import display, Markdown

states = ['engoff' , 'poweron' , 'engon' , 'FCCActions' #'emgstatus' , "whevent" , 'new data receive'
              ,{'name' : 'cores',
                'final': True,
                'parallel' : [{ 'name' : 'mapeng', 'children': ['maploaded', {"name": "update", "final": True}],
                                'initial' : 'maploaded',
                                'transitions': [['delay', 'maploaded', "update"]]},
                    {
                     'name' : 'EAA' , 'children': ['newdata', {"name": "done!", "final": True}], #environment analayser
                     'initial' : 'newdata', 
                     'transitions': [['Analaysing', 'newdata', 'done!']]
                  },{
                     'name' : 'FAI' , 'children': ['newdata', {"name": "done!", "final": True}],
                     'initial' : 'newdata',
                     'transitions': ['CalculateandLearning', 'newdata', 'done!']
                  }]
              }
            ]


transitions = [['flightcommand', 'engon', 'FCCActions'],
                   ['poweroff-command', 'engon', 'engoff'],
                   ['init', 'engoff', 'poweron'],
                   ['engstart-command', 'poweron', 'engon'],
                   ['startservice', 'poweron', 'cores']]

m = HierarchicalGraphMachine(states=states, transitions=transitions, initial="engoff", show_conditions=True,
                             title="Mermaid", auto_transitions=False)
m.init()

I just make some change in this example but I got Error :

Traceback (most recent call last):
  File ".../3.transitions/test.py", line 29, in <module>
    m = HierarchicalGraphMachine(states=states, transitions=transitions, initial="engoff", show_conditions=True,
  File "...\Python\Python38\lib\site-packages\transitions\extensions\diagrams.py", line 137, in __init__
    super(GraphMachine, self).__init__(
  File "...\Python\Python38\lib\site-packages\transitions\extensions\markup.py", line 61, in __init__
    super(MarkupMachine, self).__init__(
  File "...\Python\Python38\lib\site-packages\transitions\extensions\nesting.py", line 407, in __init__
    super(HierarchicalMachine, self).__init__(
  File "...\Python\Python38\lib\site-packages\transitions\core.py", line 601, in __init__
    self.add_states(states)
  File "...\Python\Python38\lib\site-packages\transitions\extensions\diagrams.py", line 230, in add_states
    super(GraphMachine, self).add_states(
  File "...\Python\Python38\lib\site-packages\transitions\extensions\markup.py", line 126, in add_states
    super(MarkupMachine, self).add_states(states, on_enter=on_enter, on_exit=on_exit,
  File "...\Python\Python38\lib\site-packages\transitions\extensions\nesting.py", line 521, in add_states
    self._add_dict_state(state, ignore, remap, **kwargs)
  File "...\Python\Python38\lib\site-packages\transitions\extensions\nesting.py", line 978, in _add_dict_state
    self.add_states(state_children, remap=remap, **kwargs)
  File "...\Python\Python38\lib\site-packages\transitions\extensions\diagrams.py", line 230, in add_states
    super(GraphMachine, self).add_states(
  File "...\Python\Python38\lib\site-packages\transitions\extensions\markup.py", line 126, in add_states
    super(MarkupMachine, self).add_states(states, on_enter=on_enter, on_exit=on_exit,
  File "...\Python\Python38\lib\site-packages\transitions\extensions\nesting.py", line 521, in add_states
    self._add_dict_state(state, ignore, remap, **kwargs)
  File "...\Python\Python38\lib\site-packages\transitions\extensions\nesting.py", line 980, in _add_dict_state
    self.add_transitions(transitions)
  File "...\Python\Python38\lib\site-packages\transitions\core.py", line 1032, in add_transitions
    self.add_transition(**trans)
TypeError: add_transition() argument after ** must be a mapping, not str

how can I fix this?


Solution

  • This value:

        'transitions': ['CalculateandLearning', 'newdata', 'done!']
    

    should be wrapped in another list:

        'transitions': [['CalculateandLearning', 'newdata', 'done!']]
    

    Full source code:

    from transitions.extensions.diagrams import HierarchicalGraphMachine
    from IPython.display import display, Markdown
    
    states = ['engoff' , 'poweron' , 'engon' , 'FCCActions' #'emgstatus' , "whevent" , 'new data receive'
                  ,{'name' : 'cores',
                    'final': True,
                    'parallel' : [{ 'name' : 'mapeng', 'children': ['maploaded', {"name": "update", "final": True}],
                                    'initial' : 'maploaded',
                                    'transitions': [['delay', 'maploaded', "update"]]},
                        {
                         'name' : 'EAA' , 'children': ['newdata', {"name": "done!", "final": True}], #environment analayser
                         'initial' : 'newdata', 
                         'transitions': [['Analaysing', 'newdata', 'done!']]
                      },{
                         'name' : 'FAI' , 'children': ['newdata', {"name": "done!", "final": True}],
                         'initial' : 'newdata',
                         'transitions': [['CalculateandLearning', 'newdata', 'done!']]
                      }]
                  }
                ]
    
    
    transitions = [['flightcommand', 'engon', 'FCCActions'],
                       ['poweroff-command', 'engon', 'engoff'],
                       ['init', 'engoff', 'poweron'],
                       ['engstart-command', 'poweron', 'engon'],
                       ['startservice', 'poweron', 'cores']]
    
    m = HierarchicalGraphMachine(states=states, transitions=transitions, initial="engoff", show_conditions=True,
                                 title="Mermaid", auto_transitions=False)
    m.init()
    

    I found the issue by editing the library code and adding these print functions:

    transitions/core.py

        def add_transitions(self, transitions):
            """Add several transitions.
    
            Args:
                transitions (list): A list of transitions.
    
            """
            print(f'{transitions=}') # Added here.
            for trans in listify(transitions):
                print(f'{trans=}') # And here.
                if isinstance(trans, list):
                    self.add_transition(*trans)
                else:
                    self.add_transition(**trans)
    

    Which showed me that the error happened on this case:

    transitions=['CalculateandLearning', 'newdata', 'done!']
    trans='CalculateandLearning'