WorkflowGraph#

class scikitplot.agent.WorkflowGraph(start, nodes, edges=(), *, channels=None, initial=None)[source]#

A checked, immutable workflow: nodes, edges and channels.

Parameters:
startstr

The first node.

nodessequence of Task, FanOut or Finish

Every node, with unique names.

edgessequence of Edge

Transitions; a source’s edges are tried in order. At most one edge of a source is unconditional, and it is the last.

channelsmapping of str to str or callable, default=None

Channel name to reducer: a name in REDUCERS or a callable (old, new) -> value.

initialmapping, default=None

Initial channel values (JSON); a channel not given starts at its reducer’s empty value (None for a custom reducer).

Raises:
WorkflowError

If the graph is malformed, has a node unreachable from start or one from which no Finish is reachable, has a cycle through no node with max_visits, or an on_exhausted target that leads back to its node.

Parameters:
  • start (str)

  • nodes (Sequence[Any])

  • edges (Sequence[Edge])

  • channels (Mapping[str, Any] | None)

  • initial (Mapping[str, Any] | None)

See also

WorkflowGraph.sequence

A graph that runs its nodes in order.

Examples

>>> from scikitplot.agent import Edge, Finish, Task, Tool, WorkflowGraph
>>> count = Tool("count", lambda: 1)
>>> graph = WorkflowGraph(
...     "tick",
...     [
...         Task("tick", count, into="n", max_visits=3, on_exhausted="done"),
...         Finish("done", answer=lambda view: view.channels["n"]),
...     ],
...     [Edge("tick", "tick")],
...     channels={"n": "add"},
... )
>>> [tool.name for tool in graph.tools()]
['count']
property fingerprint: str#

Digest of the graph’s structure.

Notes

Developer. Names, kinds, tools, modes, bounds and edge order are covered; callbacks are not (a function has no stable digest), so two graphs that differ only in a callback share a fingerprint.

property nodes: Mapping[str, Any]#

Every node, by name.

planner(*, artifacts=None)[source]#

Return the planner that walks this graph.

Parameters:
artifactsArtifactStore or None, default=None

Where offloaded results are read from; needed when the agent offloads results (offload_results_over).

Returns:
callable

planner(state, tools) -> Decision, as Agent expects.

Parameters:

artifacts (Any)

Return type:

Callable[[…], Decision]

replay(state, *, artifacts=None)[source]#

Rebuild where state stands in this graph.

Parameters:
stateRunState

A run’s state.

artifactsArtifactStore or None, default=None

As for planner.

Returns:
WorkflowView
Raises:
WorkflowError

If a recorded step is not the one this graph would have taken.

Parameters:
Return type:

WorkflowView

classmethod sequence(*nodes, channels=None, initial=None)[source]#

Return a graph that runs nodes in order and ends at the last one.

Parameters:
*nodesTask, FanOut or Finish

At least one; the last must be a Finish.

channels, initial

As for WorkflowGraph.

Returns:
WorkflowGraph
Raises:
WorkflowError

If no node is given or the last is not a Finish.

Parameters:
Return type:

WorkflowGraph

property start: str#

The first node’s name.

tools()[source]#

Return every tool the graph calls, for the agent’s toolset.

Returns:
list of Tool

Task and ordered-branch tools, then one tool per concurrent fan-out, each name once.

Return type:

list[Tool]