Skip to content

Commit 4ea55b0

Browse files
committed
Add utility function to convert a component graph to mermaid
This is useful when debugging tests, to get a nice representation of the component graph. Signed-off-by: Leandro Lucarella <[email protected]>
1 parent 57644f1 commit 4ea55b0

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

tests/utils/component_graph_utils.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
SolarInverter,
2020
)
2121

22+
from frequenz.sdk.microgrid.component_graph import ComponentGraph
23+
2224

2325
@dataclass
2426
class ComponentGraphConfig:
@@ -107,3 +109,18 @@ def create_component_graph_structure(
107109
components.add(DcEvCharger(id=ev_id, microgrid_id=microgrid_id))
108110
connections.add(ComponentConnection(source=junction_id, destination=ev_id))
109111
return components, connections
112+
113+
114+
def component_grap_to_mermaid(comp_graph: ComponentGraph) -> str:
115+
"""Return a string representation of the component graph in Mermaid format."""
116+
117+
def component_to_mermaid(component: Component) -> str:
118+
return f'"{component.id}"["{component}"]'
119+
120+
def connection_to_mermaid(connection: ComponentConnection) -> str:
121+
return f'"{connection.source}" --> "{connection.destination}"'
122+
123+
components = "\n".join(map(component_to_mermaid, comp_graph.components()))
124+
connections = "\n".join(map(connection_to_mermaid, comp_graph.connections()))
125+
126+
return f"graph TD\n{components}\n{connections}"

0 commit comments

Comments
 (0)