libtcspc.Graph¶
- class libtcspc.Graph[source]¶
Bases:
objectProcessing graph.
A processing graph is a directed acyclic graph of
Nodeobjects. Each node is also assigned a name for later reference and retrieval.Edges of the graph connect a specific output port of one node to a specific input port of another (in that direction). Any given input or output port may have at most a single edge connected to it.
At any point in time, the graph may contain nodes with unconnected input or output ports. These ports are the input and output ports of the graph as a whole.
Note: It is not possible to add a connection between a graph input and a graph output that does not have an intermediate node. But this can be worked around by using a SelectAll (i.e., no-op) node.
- add_node(name, node, *, upstream=None, downstream=None)[source]¶
Add a node to the graph, optionally connecting it to an existing node.
- Parameters:
name (str or None) – Name under which the node will be retrievable. If
None, an auto-generated name of the form"{ClassName}-{N}"is assigned, whereNis the smallest non-negative integer making the name unique.node (Node) – The node to add.
upstream (tuple[str, str] or str or None) – If given, connect
upstreamto the new node’s"input"port. A(node_name, port_name)tuple selects an explicit output port; a bare node name defaults to its"output"port.downstream (tuple[str, str] or str or None) – If given, connect the new node’s
"output"port todownstream. A(node_name, port_name)tuple selects an explicit input port; a bare node name defaults to its"input"port.
- Returns:
The name under which the node was added (the auto-generated name if
namewasNone, otherwisename).- Return type:
- Raises:
ValueError – If
namealready exists in the graph, if a requested connection’s port types do not match, or if a requested connection would introduce a cycle.
- add_sequence(nodes, *, upstream=None, downstream=None)[source]¶
Add a sequence of single-input single-output nodes connected in series.
- Parameters:
nodes (Sequence[tuple[str, Node] or Node]) – The nodes to add, in order. Each element is either a
(name, node)tuple or a bareNode; in the latter case a name is auto-generated as byadd_node.upstream (tuple[str, str] or str or None) – If given, connect
upstreamto the first added node’s input port. The shorthand rules ofadd_nodeapply.downstream (tuple[str, str] or str or None) – If given, connect the last added node’s output port to
downstream. The shorthand rules ofadd_nodeapply.
- Raises:
ValueError – If any node does not have exactly one input and one output port, or if any requested connection fails.
- Return type:
None
Notes
If
nodesis empty and bothupstreamanddownstreamare given,upstreamis connected directly todownstream.
- connect(producer, consumer)[source]¶
Connect an output port to an input port.
- Parameters:
producer (tuple[str, str] or str) – A
(node_name, port_name)tuple selecting the producing node and its output port. A bare node name is shorthand for(name, "output").consumer (tuple[str, str] or str) – A
(node_name, port_name)tuple selecting the consuming node and its input port. A bare node name is shorthand for(name, "input").
- Raises:
ValueError – If either port is already connected, if the producer’s output event set is not compatible with the consumer’s input, or if the connection would introduce a cycle.
- Return type:
None
Notes
Validation is performed immediately. If the connection is rejected, the graph is restored to its previous state before the exception propagates.