61 lines
1.5 KiB
Python
61 lines
1.5 KiB
Python
"""Flat bond-graph network types used by analysis algorithms."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from dataclasses import dataclass, field
|
|
|
|
from bedit_core.models import (
|
|
BondConnection,
|
|
BondPort,
|
|
Component,
|
|
ComponentID,
|
|
ConnectionID,
|
|
PortID,
|
|
)
|
|
|
|
ComponentPath = tuple[ComponentID, ...]
|
|
|
|
|
|
@dataclass(eq=False)
|
|
class NetworkPort:
|
|
"""A bond port together with its owning component and hierarchical path."""
|
|
|
|
component_path: ComponentPath
|
|
component: Component
|
|
port_id: PortID
|
|
port: BondPort
|
|
|
|
|
|
@dataclass
|
|
class NetworkBond:
|
|
"""A bond connection whose endpoints are resolved to flattened ports."""
|
|
|
|
component_path: ComponentPath
|
|
connection_id: ConnectionID
|
|
connection: BondConnection
|
|
source: NetworkPort
|
|
target: NetworkPort
|
|
|
|
|
|
@dataclass
|
|
class BondGraphNetwork:
|
|
"""All components, bond ports, and bonds in a component hierarchy."""
|
|
|
|
root: Component
|
|
components: list[Component] = field(default_factory=list)
|
|
ports: list[NetworkPort] = field(default_factory=list)
|
|
bonds: list[NetworkBond] = field(default_factory=list)
|
|
|
|
def bonds_for(self, port: NetworkPort) -> list[NetworkBond]:
|
|
"""Return all bonds incident to ``port``."""
|
|
return [
|
|
bond
|
|
for bond in self.bonds
|
|
if bond.source is port or bond.target is port
|
|
]
|
|
|
|
@property
|
|
def connections(self) -> list[NetworkBond]:
|
|
"""Alias for ``bonds`` for callers using connection terminology."""
|
|
return self.bonds
|