From 352e493c9174f4508125b55f1cc3e3460307cd31 Mon Sep 17 00:00:00 2001 From: milanofthe Date: Wed, 18 Nov 2026 14:38:22 +0100 Subject: [PATCH] Carrier ports take what pathsim connects: same width, keys only where both channels have one --- docs/pvm-spec.md | 6 +- pathview/buses.py | 8 ++ pathview/converter.py | 7 +- src/lib/bus/expand.test.ts | 19 ++-- src/lib/bus/expand.ts | 154 +++++++++++++++++------------- src/lib/pyodide/codeBuilder.ts | 6 +- tests/fixtures/bus_expansion.json | 62 +++++++++++- tests/test_bus_expansion.py | 63 +++++++++++- 8 files changed, 241 insertions(+), 84 deletions(-) diff --git a/docs/pvm-spec.md b/docs/pvm-spec.md index b489692c..408bccfa 100644 --- a/docs/pvm-spec.md +++ b/docs/pvm-spec.md @@ -323,9 +323,11 @@ In PathSim Python code, subsystems map to `Subsystem(blocks=[...], connections=[ - A subsystem port carrying a bus becomes one port index per leaf signal, in bus order, on the Subsystem and on its Interface. Port indices after it shift accordingly. - Wiring that cannot be resolved is left out: a bus into a block that is not a bus block or subsystem, a picked signal missing from the bus, or a wire loop through bus blocks. -Editors should not create such wiring: a bus may only enter a Bus Creator, a Bus Selector or a subsystem port, and a Bus Selector only takes a bus. PathView rejects these wires while connecting and draws existing ones as errors. +A port with `bus` is a PathSim carrier: a bus into it is resolved to one connection per channel, a wire from a carrier port to a carrier port stays one connection. -The reference implementations are `src/lib/bus/expand.ts` and `pathview/buses.py`; `tests/fixtures/bus_expansion.json` lists the expected wiring for each case. +Editors should not create such wiring: a bus may only enter a Bus Creator, a Bus Selector, a subsystem port or a carrier port, and a Bus Selector only takes a bus. A carrier port takes what PathSim connects to it: one signal per channel, and where a signal comes from a channel with a key and meets a channel with a key, the same key. Signals from ports without `bus`, from channels named by their position and through subsystem ports have no key. PathView rejects these wires while connecting and draws existing ones as errors. + +The reference implementations are `src/lib/bus/expand.ts` and `pathview/buses.py`; `tests/fixtures/bus_expansion.json` lists the expected wiring for each case, and wires into carrier ports with the verdict of PathSim. --- diff --git a/pathview/buses.py b/pathview/buses.py index 158fb304..d5af7bb5 100644 --- a/pathview/buses.py +++ b/pathview/buses.py @@ -35,6 +35,14 @@ def port_bus(node: dict, direction: str, index: int): return ports[index].get("bus") if index < len(ports) else None +def channel_key(carrier, channel: int) -> str | None: + """Name of a channel of a carrier port in pathsim: its key, or None for a + channel without key, as those of a pathsim `Vector`, whose signals are + named by their position. Same rule as `channelKey` in the editor.""" + name = carrier[channel]["name"] if carrier and channel < len(carrier) else None + return None if name is None or name == str(channel) else name + + def is_bus_block(node: dict) -> bool: return node.get("type") in (BUS_CREATOR, BUS_SELECTOR) diff --git a/pathview/converter.py b/pathview/converter.py index 698fcffd..d3fadc62 100644 --- a/pathview/converter.py +++ b/pathview/converter.py @@ -16,7 +16,7 @@ from pathlib import Path from typing import Any -from pathview.buses import expand_buses, port_bus +from pathview.buses import channel_key, expand_buses, port_bus # ============================================================================= @@ -113,9 +113,8 @@ def port_reference(var_name: str, node: dict, direction: str, index: int, channe port = f"{var_name}[{json.dumps(port_key(node, direction, index))}]" if channel is None: return port - carrier = port_bus(node, direction, index) or [] - name = carrier[channel]["name"] if channel < len(carrier) else None - return f"{port}[{channel if name is None or name == str(channel) else json.dumps(name)}]" + key = channel_key(port_bus(node, direction, index), channel) + return f"{port}[{channel if key is None else json.dumps(key)}]" def group_connections_by_source(connections: list[dict], nodes: list[dict], node_vars: dict[str, str]) -> list[dict]: diff --git a/src/lib/bus/expand.test.ts b/src/lib/bus/expand.test.ts index 7e649915..260b03f4 100644 --- a/src/lib/bus/expand.test.ts +++ b/src/lib/bus/expand.test.ts @@ -108,7 +108,7 @@ describe('bus expansion', () => { expect(busWiringProblem(analysis, root, 'S', 0, 'Scope', 0)).toBeNull(); }); - it('takes a bus into a carrier port only with the signals of the carrier', () => { + it('keeps buses and plain signals at the ports that take them', () => { const creator = load(fixtures.scenarios.find((s) => s.name === 'bus creator into a carrier port')!); const analysis = analyzeBuses(creator.nodes, creator.connections); const root = analysis.root; @@ -116,11 +116,6 @@ describe('bus expansion', () => { expect(busWiringProblem(analysis, root, 'A', 0, 'Tk', 0)).toBe('signal-into-carrier'); expect(busWiringProblem(analysis, root, 'Cr', 0, 'Tk', 1)).toBe('bus-into-block'); - // a bus missing a signal of the carrier - const partial = creator.connections.filter((c) => c.id !== 'c3'); - const reduced = analyzeBuses(creator.nodes, partial); - expect(busWiringProblem(reduced, reduced.root, 'Cr', 0, 'Tk', 0)).toBe('bus-mismatch'); - // a carrier output is a bus const stream = load(fixtures.scenarios.find((s) => s.name === 'carrier into a bus selector')!); const streams = analyzeBuses(stream.nodes, stream.connections); @@ -128,6 +123,18 @@ describe('bus expansion', () => { expect(busWiringProblem(streams, streams.root, 'St', 0, 'Scope', 0)).toBe('bus-into-block'); }); + describe('wires into carrier ports, with the verdict of pathsim', () => { + const wiring = load(fixtures.wiring as unknown as Scenario); + const analysis = analyzeBuses(wiring.nodes, wiring.connections); + for (const { wire, accepted, name } of fixtures.wiring.cases) { + it(name, () => { + const c = wiring.connections.find((connection) => connection.id === wire)!; + const problem = busWiringProblem(analysis, analysis.root, c.sourceNodeId, c.sourcePortIndex, c.targetNodeId, c.targetPortIndex); + expect(problem === null).toBe(accepted); + }); + } + }); + it('applies the bus rules across subsystem ports', () => { const { nodes, connections } = load(fixtures.scenarios.find((s) => s.name === 'bus into a subsystem')!); const analysis = analyzeBuses(nodes, connections); diff --git a/src/lib/bus/expand.ts b/src/lib/bus/expand.ts index ca4e9927..e36a1f27 100644 --- a/src/lib/bus/expand.ts +++ b/src/lib/bus/expand.ts @@ -25,6 +25,16 @@ export function portBus(node: NodeInstance, direction: PortDirection, index: num return nodePort(node, direction, index)?.bus ?? null; } +/** + * Name of a channel of a carrier port in pathsim: its key, or null for a + * channel without key, as those of a pathsim `Vector`, whose signals are + * named by their position + */ +export function channelKey(carrier: BusStructure, channel: number): string | null { + const name = carrier?.[channel]?.name; + return name === undefined || name === String(channel) ? null : name; +} + /** A connection after bus expansion; a channel is the position of a single signal within a carrier port */ export interface ExpandedConnection extends Connection { sourceChannel?: number; @@ -210,71 +220,6 @@ export function analyzeBuses(nodes: NodeInstance[], connections: Connection[]) { } } - /** Level at a subsystem path from the root, by subsystem IDs */ - function levelAt(path: string[]): BusLevel | null { - let level: BusLevel | undefined = root; - for (const id of path) { - level = level?.children.get(id); - if (!level) return null; - } - return level; - } - - return { root, levelAt, structureIn, structureOut, elementNames }; -} - -export type BusAnalysis = ReturnType; - -/** Why a wire breaks the bus rules */ -export type BusWiringProblem = 'bus-into-block' | 'signal-into-selector' | 'signal-into-carrier' | 'bus-mismatch'; - -/** - * A bus may only enter a Bus Creator, a Bus Selector, a subsystem port or a - * carrier port, a Bus Selector only takes a bus, and a carrier port only a bus - * with its signals in its order. Returns the rule a wire from the source port to - * the target port breaks, or null if it keeps them. - */ -export function busWiringProblem( - analysis: BusAnalysis, - level: BusLevel, - sourceNodeId: string, - sourcePort: number, - targetNodeId: string, - targetPort: number -): BusWiringProblem | null { - const target = level.nodes.get(targetNodeId); - if (!target) return null; - const structure = analysis.structureOut(level, sourceNodeId, sourcePort); - switch (target.type) { - case NODE_TYPES.BUS_CREATOR: - case NODE_TYPES.SUBSYSTEM: - case NODE_TYPES.INTERFACE: - return null; - case NODE_TYPES.BUS_SELECTOR: - return structure ? null : 'signal-into-selector'; - default: { - const carrier = portBus(target, 'input', targetPort); - if (!carrier) return structure ? 'bus-into-block' : null; - if (!structure) return 'signal-into-carrier'; - return signalLeaves(structure).join() === signalLeaves(carrier).join() ? null : 'bus-mismatch'; - } - } -} - -/** - * The model without bus blocks, for code generation. Models without buses are - * returned unchanged. Connections that carry several signals are split, with - * IDs suffixed by the signal index. A wire between two plain blocks stays as it - * is, also from carrier to carrier. Wiring that cannot be resolved, such as a - * bus into a plain block or a signal missing from a bus, is left out. - */ -export function expandBuses( - nodes: NodeInstance[], - connections: Connection[] -): { nodes: NodeInstance[]; connections: ExpandedConnection[] } { - if (!containsBuses(nodes)) return { nodes, connections }; - - const { root, structureIn, structureOut } = analyzeBuses(nodes, connections); const count = (structure: BusStructure) => signalLeaves(structure).length; const range = (nodeId: string, offset: number, length: number): Endpoint[] => Array.from({ length }, (_, j) => ({ nodeId, port: offset + j })); @@ -363,6 +308,85 @@ export function expandBuses( } } + /** Level at a subsystem path from the root, by subsystem IDs */ + function levelAt(path: string[]): BusLevel | null { + let level: BusLevel | undefined = root; + for (const id of path) { + level = level?.children.get(id); + if (!level) return null; + } + return level; + } + + return { root, levelAt, structureIn, structureOut, elementNames, resolveOut, inputSlots }; +} + +export type BusAnalysis = ReturnType; + +/** Why a wire breaks the bus rules */ +export type BusWiringProblem = 'bus-into-block' | 'signal-into-selector' | 'signal-into-carrier' | 'bus-mismatch'; + +/** + * A bus may only enter a Bus Creator, a Bus Selector, a subsystem port or a + * carrier port, and a Bus Selector only takes a bus. A carrier port takes what + * pathsim connects to it (`Connection._validate_dimensions`): one signal per + * channel, and where a signal comes from a channel with a key and the channel + * it meets has one, the same key. Signals from plain ports, channels without + * key and signals through subsystem ports have none. Returns the rule a wire + * from the source port to the target port breaks, or null if it keeps them. + */ +export function busWiringProblem( + analysis: BusAnalysis, + level: BusLevel, + sourceNodeId: string, + sourcePort: number, + targetNodeId: string, + targetPort: number +): BusWiringProblem | null { + const target = level.nodes.get(targetNodeId); + if (!target) return null; + const structure = analysis.structureOut(level, sourceNodeId, sourcePort); + switch (target.type) { + case NODE_TYPES.BUS_CREATOR: + case NODE_TYPES.SUBSYSTEM: + case NODE_TYPES.INTERFACE: + return null; + case NODE_TYPES.BUS_SELECTOR: + return structure ? null : 'signal-into-selector'; + default: { + const carrier = portBus(target, 'input', targetPort); + if (!carrier) return structure ? 'bus-into-block' : null; + const sources = analysis.resolveOut(level, sourceNodeId, sourcePort); + if (sources.length !== carrier.length) return structure ? 'bus-mismatch' : 'signal-into-carrier'; + const sourceKey = (endpoint: (typeof sources)[number]) => { + const node = endpoint && endpoint.channel !== undefined ? level.nodes.get(endpoint.nodeId) : undefined; + return node ? channelKey(portBus(node, 'output', endpoint!.port), endpoint!.channel!) : null; + }; + const meets = sources.every((endpoint, i) => { + const a = sourceKey(endpoint); + const b = channelKey(carrier, i); + return a === null || b === null || a === b; + }); + return meets ? null : 'bus-mismatch'; + } + } +} + +/** + * The model without bus blocks, for code generation. Models without buses are + * returned unchanged. Connections that carry several signals are split, with + * IDs suffixed by the signal index. A wire between two plain blocks stays as it + * is, also from carrier to carrier. Wiring that cannot be resolved, such as a + * bus into a plain block or a signal missing from a bus, is left out. + */ +export function expandBuses( + nodes: NodeInstance[], + connections: Connection[] +): { nodes: NodeInstance[]; connections: ExpandedConnection[] } { + if (!containsBuses(nodes)) return { nodes, connections }; + + const { root, resolveOut, inputSlots } = analyzeBuses(nodes, connections); + function expandLevel(level: BusLevel): { nodes: NodeInstance[]; connections: ExpandedConnection[] } { const expandedNodes: NodeInstance[] = []; for (const node of level.nodeList) { diff --git a/src/lib/pyodide/codeBuilder.ts b/src/lib/pyodide/codeBuilder.ts index 528560e5..114d763b 100644 --- a/src/lib/pyodide/codeBuilder.ts +++ b/src/lib/pyodide/codeBuilder.ts @@ -8,7 +8,7 @@ import { nodePort } from '$lib/nodes/ports'; import { nodeRegistry } from '$lib/nodes/registry'; import { blockImportPaths } from '$lib/nodes/generated/blocks'; import { enginePath } from '$lib/constants/engine'; -import type { ExpandedConnection } from '$lib/bus/expand'; +import { channelKey, type ExpandedConnection } from '$lib/bus/expand'; /** Options for parameter string generation */ export interface ParamStringOptions { @@ -84,8 +84,8 @@ export function portKey(node: NodeInstance, direction: PortDirection, index: num function portReference(node: NodeInstance, varName: string, direction: PortDirection, index: number, channel?: number): string { const port = `${varName}[${JSON.stringify(portKey(node, direction, index))}]`; if (channel === undefined) return port; - const name = nodePort(node, direction, index)?.bus?.[channel]?.name; - return `${port}[${name === undefined || name === String(channel) ? channel : JSON.stringify(name)}]`; + const key = channelKey(nodePort(node, direction, index)?.bus ?? null, channel); + return `${port}[${key === null ? channel : JSON.stringify(key)}]`; } /** diff --git a/tests/fixtures/bus_expansion.json b/tests/fixtures/bus_expansion.json index 90d0ceff..91da069e 100644 --- a/tests/fixtures/bus_expansion.json +++ b/tests/fixtures/bus_expansion.json @@ -1,5 +1,5 @@ { - "description": "Bus expansion cases shared by src/lib/bus/expand.test.ts and tests/test_bus_expansion.py. Expected wiring per level as source:port>target:port, with [channel] for a single channel of a carrier port; level keys are subsystem ID paths joined by '/', the root is ''. The registry holds the block types with carrier ports, in the format of registry.json.", + "description": "Bus expansion cases shared by src/lib/bus/expand.test.ts and tests/test_bus_expansion.py. Expected wiring per level as source:port>target:port, with [channel] for a single channel of a carrier port; level keys are subsystem ID paths joined by '/', the root is ''. The registry holds the block types with carrier ports, in the format of registry.json. The wiring section holds wires into carrier ports with the verdict of pathsim.", "registry": { "blocks": { "Stream": { @@ -315,5 +315,63 @@ ], "expected": { "": ["V:0[1]>Scope:0"] } } - ] + ], + "wiring": { + "description": "Wires into carrier ports and whether pathsim accepts them (tests/test_bus_expansion.py builds the connections in pathsim, src/lib/bus/expand.test.ts checks busWiringProblem against the same verdict). Each Tank input 'in' takes one wire.", + "nodes": [ + { "id": "A", "type": "Constant", "inputs": [], "outputs": [{ "name": "out 0" }] }, + { "id": "B", "type": "Constant", "inputs": [], "outputs": [{ "name": "out 0" }] }, + { "id": "C", "type": "Constant", "inputs": [], "outputs": [{ "name": "out 0" }] }, + { "id": "St", "type": "Stream", "inputs": [], "outputs": [{ "name": "out" }] }, + { "id": "Vc", "type": "Vector", "inputs": [], "outputs": [{ "name": "x" }] }, + { "id": "S", "type": "BusSelector", "params": { "signals": ["F", "T", "P"] }, "inputs": [{ "name": "bus" }], "outputs": [{ "name": "F" }, { "name": "T" }, { "name": "P" }] }, + { "id": "Cu", "type": "BusCreator", "inputs": [{ "name": "in 0" }, { "name": "in 1" }, { "name": "in 2" }], "outputs": [{ "name": "out 0" }] }, + { "id": "Ck", "type": "BusCreator", "inputs": [{ "name": "in 0" }, { "name": "in 1" }, { "name": "in 2" }], "outputs": [{ "name": "out 0" }] }, + { "id": "Cr", "type": "BusCreator", "inputs": [{ "name": "in 0" }, { "name": "in 1" }, { "name": "in 2" }], "outputs": [{ "name": "out 0" }] }, + { "id": "C2", "type": "BusCreator", "inputs": [{ "name": "in 0" }, { "name": "in 1" }], "outputs": [{ "name": "out 0" }] }, + { "id": "Cp", "type": "BusCreator", "inputs": [{ "name": "in 0" }, { "name": "in 1" }, { "name": "in 2" }], "outputs": [{ "name": "out 0" }] }, + { "id": "T1", "type": "Tank", "inputs": [{ "name": "in" }, { "name": "Q" }], "outputs": [] }, + { "id": "T2", "type": "Tank", "inputs": [{ "name": "in" }, { "name": "Q" }], "outputs": [] }, + { "id": "T3", "type": "Tank", "inputs": [{ "name": "in" }, { "name": "Q" }], "outputs": [] }, + { "id": "T4", "type": "Tank", "inputs": [{ "name": "in" }, { "name": "Q" }], "outputs": [] }, + { "id": "T5", "type": "Tank", "inputs": [{ "name": "in" }, { "name": "Q" }], "outputs": [] }, + { "id": "T6", "type": "Tank", "inputs": [{ "name": "in" }, { "name": "Q" }], "outputs": [] }, + { "id": "T7", "type": "Tank", "inputs": [{ "name": "in" }, { "name": "Q" }], "outputs": [] }, + { "id": "T8", "type": "Tank", "inputs": [{ "name": "in" }, { "name": "Q" }], "outputs": [] } + ], + "connections": [ + { "id": "st-s", "sourceNodeId": "St", "sourcePortIndex": 0, "targetNodeId": "S", "targetPortIndex": 0 }, + { "id": "cu0", "sourceNodeId": "A", "sourcePortIndex": 0, "targetNodeId": "Cu", "targetPortIndex": 0, "label": "x" }, + { "id": "cu1", "sourceNodeId": "B", "sourcePortIndex": 0, "targetNodeId": "Cu", "targetPortIndex": 1, "label": "y" }, + { "id": "cu2", "sourceNodeId": "C", "sourcePortIndex": 0, "targetNodeId": "Cu", "targetPortIndex": 2, "label": "z" }, + { "id": "ck0", "sourceNodeId": "S", "sourcePortIndex": 0, "targetNodeId": "Ck", "targetPortIndex": 0 }, + { "id": "ck1", "sourceNodeId": "S", "sourcePortIndex": 1, "targetNodeId": "Ck", "targetPortIndex": 1 }, + { "id": "ck2", "sourceNodeId": "S", "sourcePortIndex": 2, "targetNodeId": "Ck", "targetPortIndex": 2 }, + { "id": "cr0", "sourceNodeId": "S", "sourcePortIndex": 1, "targetNodeId": "Cr", "targetPortIndex": 0 }, + { "id": "cr1", "sourceNodeId": "S", "sourcePortIndex": 0, "targetNodeId": "Cr", "targetPortIndex": 1 }, + { "id": "cr2", "sourceNodeId": "S", "sourcePortIndex": 2, "targetNodeId": "Cr", "targetPortIndex": 2 }, + { "id": "c20", "sourceNodeId": "A", "sourcePortIndex": 0, "targetNodeId": "C2", "targetPortIndex": 0 }, + { "id": "c21", "sourceNodeId": "B", "sourcePortIndex": 0, "targetNodeId": "C2", "targetPortIndex": 1 }, + { "id": "cp0", "sourceNodeId": "A", "sourcePortIndex": 0, "targetNodeId": "Cp", "targetPortIndex": 0 }, + { "id": "cp1", "sourceNodeId": "B", "sourcePortIndex": 0, "targetNodeId": "Cp", "targetPortIndex": 1 }, + { "id": "w1", "sourceNodeId": "Cu", "sourcePortIndex": 0, "targetNodeId": "T1", "targetPortIndex": 0 }, + { "id": "w2", "sourceNodeId": "Ck", "sourcePortIndex": 0, "targetNodeId": "T2", "targetPortIndex": 0 }, + { "id": "w3", "sourceNodeId": "Cr", "sourcePortIndex": 0, "targetNodeId": "T3", "targetPortIndex": 0 }, + { "id": "w4", "sourceNodeId": "C2", "sourcePortIndex": 0, "targetNodeId": "T4", "targetPortIndex": 0 }, + { "id": "w5", "sourceNodeId": "Vc", "sourcePortIndex": 0, "targetNodeId": "T5", "targetPortIndex": 0 }, + { "id": "w6", "sourceNodeId": "A", "sourcePortIndex": 0, "targetNodeId": "T6", "targetPortIndex": 0 }, + { "id": "w7", "sourceNodeId": "St", "sourcePortIndex": 0, "targetNodeId": "T7", "targetPortIndex": 0 }, + { "id": "w8", "sourceNodeId": "Cp", "sourcePortIndex": 0, "targetNodeId": "T8", "targetPortIndex": 0 } + ], + "cases": [ + { "wire": "w1", "accepted": true, "name": "signals of plain ports into a carrier, whatever their names" }, + { "wire": "w2", "accepted": true, "name": "channels of a carrier into channels of the same keys" }, + { "wire": "w3", "accepted": false, "name": "channels of a carrier into channels of other keys" }, + { "wire": "w4", "accepted": false, "name": "fewer signals than channels" }, + { "wire": "w5", "accepted": false, "name": "a vector of another width" }, + { "wire": "w6", "accepted": false, "name": "a single signal into a carrier of several channels" }, + { "wire": "w7", "accepted": true, "name": "carrier to carrier of the same keys" }, + { "wire": "w8", "accepted": true, "name": "a bus with an unconnected signal leaves its channel unconnected" } + ] + } } diff --git a/tests/test_bus_expansion.py b/tests/test_bus_expansion.py index 188bae01..4b0223e9 100644 --- a/tests/test_bus_expansion.py +++ b/tests/test_bus_expansion.py @@ -5,8 +5,13 @@ import pytest -from pathview.buses import expand_buses, is_bus_block -from pathview.converter import generate_python, load_registry, with_port_structure +from pathsim import Connection +from pathsim.blocks import Constant +from pathsim.blocks._block import Block +from pathsim.utils.carrier import Carrier, Vector + +from pathview.buses import SEPARATOR, channel_key, expand_buses, is_bus_block +from pathview.converter import generate_python, load_registry, port_reference, with_port_structure FIXTURES = json.loads((Path(__file__).parent / "fixtures" / "bus_expansion.json").read_text()) REGISTRY_PATH = Path(__file__).parent.parent / "pathview" / "data" / "registry.json" @@ -76,3 +81,57 @@ def test_converter_generates_no_bus_blocks(): code = generate_python(pvm, load_registry(REGISTRY_PATH)) assert "BusCreator" not in code assert "BusSelector" not in code + + +# wires into carrier ports, checked by pathsim ========================================== + +WIRING = FIXTURES["wiring"] + + +def _labels(names, buses): + """Port labels of a fixture block type: a carrier with keys, a vector, or a single channel.""" + labels, start = {}, 0 + for name in names: + bus = buses.get(name) + if bus is None: + labels[name], start = start, start + 1 + continue + keys = [channel_key(bus, i) for i in range(len(bus))] + labels[name] = Vector(len(bus), start) if all(k is None for k in keys) else Carrier(start, keys=keys) + start += len(bus) + return labels + + +def _pathsim_block(node): + block = REGISTRY["blocks"].get(node["type"]) + if block is None: + return Constant() + cls = type(node["type"], (Block,), { + "input_port_labels": _labels(block["inputs"], block.get("inputBuses", {})), + "output_port_labels": _labels(block["outputs"], block.get("outputBuses", {})), + }) + return cls() + + +def _pathsim_accepts(wire): + """Whether pathsim builds the connections the expansion makes of a wire.""" + nodes, connections = expand_buses(*_load(WIRING)) + by_id = {n["id"]: n for n in nodes} + blocks = {n["id"]: _pathsim_block(n) for n in nodes} + expanded = [c for c in connections if c["id"] == wire or c["id"].startswith(wire + SEPARATOR)] + #the expansion leaves out a wire whose signals do not meet the channels one to one + if not expanded: + return False + try: + for c in expanded: + source = port_reference(c["sourceNodeId"], by_id[c["sourceNodeId"]], "outputs", c["sourcePortIndex"], c.get("sourceChannel")) + target = port_reference(c["targetNodeId"], by_id[c["targetNodeId"]], "inputs", c["targetPortIndex"], c.get("targetChannel")) + Connection(eval(source, {}, blocks), eval(target, {}, blocks)) + except ValueError: + return False + return True + + +@pytest.mark.parametrize("case", WIRING["cases"], ids=lambda c: c["name"]) +def test_wiring_verdict_is_pathsims(case): + assert _pathsim_accepts(case["wire"]) == case["accepted"]