Coverage for flowr / domain / mermaid.py: 100%

16 statements  

« prev     ^ index     » next       coverage.py v7.13.5, created at 2026-05-02 18:23 +0000

1"""Mermaid stateDiagram-v2 conversion for flow definitions.""" 

2 

3from flowr.domain.flow_definition import Flow 

4 

5 

6def to_mermaid(flow: Flow) -> str: 

7 """Convert a flow definition to a Mermaid stateDiagram-v2 string.""" 

8 lines = ["stateDiagram-v2"] 

9 for state in flow.states: 

10 if state.flow is not None: 

11 lines.extend( 

12 [ 

13 f" {state.id} --> {state.flow}", 

14 f" note right of {state.id}: invokes {state.flow}", 

15 ] 

16 ) 

17 lines.append(f' state "{state.id}" as {state.id}') 

18 for state in flow.states: 

19 for trigger, transition in state.next.items(): 

20 label_parts: list[str] = [trigger] 

21 if transition.conditions is not None: 

22 cond_parts = [ 

23 f"{k}: {v}" for k, v in transition.conditions.conditions.items() 

24 ] 

25 label_parts.append(", ".join(cond_parts)) 

26 label = " | ".join(label_parts) 

27 lines.append(f" {state.id} --> {transition.target} : {label}") 

28 return "\n".join(lines)