diff --git a/src/convert_to_python.py b/src/convert_to_python.py index e1e1aa2b..a8685f48 100644 --- a/src/convert_to_python.py +++ b/src/convert_to_python.py @@ -168,10 +168,24 @@ def make_edge_data(data: dict) -> list[dict]: f"Invalid source handle '{edge['sourceHandle']}' for {edge}." ) else: + # make sure that the source block has only one output port (ie. that sourceHandle is None) + assert edge["sourceHandle"] is None, ( + f"Source block {block.id} has multiple output ports, " + "but connection method hasn't been implemented." + ) output_index = 0 if isinstance(target_block, Scope): input_index = target_block._connections_order.index(edge["id"]) + elif isinstance(target_block, Bubbler): + if edge["targetHandle"] == "sample_in_soluble": + input_index = 0 + elif edge["targetHandle"] == "sample_in_insoluble": + input_index = 1 + else: + raise ValueError( + f"Invalid target handle '{edge['targetHandle']}' for {edge}." + ) elif isinstance(target_block, FestimWall): if edge["targetHandle"] == "c_0": input_index = 0 @@ -182,6 +196,11 @@ def make_edge_data(data: dict) -> list[dict]: f"Invalid target handle '{edge['targetHandle']}' for {edge}." ) else: + # make sure that the target block has only one input port (ie. that targetHandle is None) + assert edge["targetHandle"] is None, ( + f"Target block {target_block.id} has multiple input ports, " + "but connection method hasn't been implemented." + ) input_index = block_to_input_index[target_block] edge["source_var_name"] = node["var_name"] diff --git a/src/pathsim_utils.py b/src/pathsim_utils.py index 0274f377..c1554525 100644 --- a/src/pathsim_utils.py +++ b/src/pathsim_utils.py @@ -437,11 +437,24 @@ def make_connections(nodes, edges, blocks) -> list[Connection]: f"Invalid source handle '{edge['sourceHandle']}' for {edge}." ) else: + # make sure that the source block has only one output port (ie. that sourceHandle is None) + assert edge["sourceHandle"] is None, ( + f"Source block {block.id} has multiple output ports, " + "but connection method hasn't been implemented." + ) output_index = 0 if isinstance(target_block, Scope): input_index = target_block._connections_order.index(edge["id"]) - # TODO we should do the same for all blocks with several input/target ports + elif isinstance(target_block, Bubbler): + if edge["targetHandle"] == "sample_in_soluble": + input_index = 0 + elif edge["targetHandle"] == "sample_in_insoluble": + input_index = 1 + else: + raise ValueError( + f"Invalid target handle '{edge['targetHandle']}' for {edge}." + ) elif isinstance(target_block, FestimWall): if edge["targetHandle"] == "c_0": input_index = 0 @@ -452,6 +465,11 @@ def make_connections(nodes, edges, blocks) -> list[Connection]: f"Invalid target handle '{edge['targetHandle']}' for {edge}." ) else: + # make sure that the target block has only one input port (ie. that targetHandle is None) + assert edge["targetHandle"] is None, ( + f"Target block {target_block.id} has multiple input ports, " + "but connection method hasn't been implemented." + ) input_index = block_to_input_index[target_block] connection = Connection( diff --git a/test/test_convert_python.py b/test/test_convert_python.py index d70660e7..38dfc6ea 100644 --- a/test/test_convert_python.py +++ b/test/test_convert_python.py @@ -41,11 +41,41 @@ }, ], "edges": [ - {"source": "1", "target": "2", "id": "e1-2"}, - {"source": "2", "target": "3", "id": "e2-3"}, - {"source": "3", "target": "4", "id": "e3-4"}, - {"source": "3", "target": "5", "id": "e3-5"}, - {"source": "4", "target": "5", "id": "e4-5"}, + { + "source": "1", + "target": "2", + "id": "e1-2", + "sourceHandle": None, + "targetHandle": None, + }, + { + "source": "2", + "target": "3", + "id": "e2-3", + "sourceHandle": None, + "targetHandle": None, + }, + { + "source": "3", + "target": "4", + "id": "e3-4", + "sourceHandle": None, + "targetHandle": None, + }, + { + "source": "3", + "target": "5", + "id": "e3-5", + "sourceHandle": None, + "targetHandle": None, + }, + { + "source": "4", + "target": "5", + "id": "e4-5", + "sourceHandle": None, + "targetHandle": None, + }, ], "solverParams": { "Solver": "SSPRK22",