Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add loop node #36

Merged
merged 1 commit into from
Sep 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,65 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`a node can run its children multiple times 1`] = `
{
"n1": {
"input": {
"data": "",
},
},
"n2": {
"input": {
"list": [
"a",
"b",
],
},
"status": "idle",
},
"n3": {
"input": {
"data": "b",
},
},
"n4": {
"input": {
"data": "b",
},
},
}
`;

exports[`a node can run its children multiple times 2 1`] = `
{
"n1": {
"input": {
"list": [
"a",
"b",
],
},
"status": "idle",
},
"n2": {
"input": {
"data": "",
"list": [
"a",
"b",
],
},
},
"n3": {
"input": {
"data": [
"a",
"b",
],
},
},
}
`;

exports[`avoid hanging on loops 1`] = `
{
"n1": {
Expand Down
21 changes: 11 additions & 10 deletions packages/@pufflig/ps-chains/src/engines/dataflow/constants.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import { Node, ParamValueMap } from "@pufflig/ps-types";
import { NextNode, Node, ParamValueMap } from "@pufflig/ps-types";

export const delimiterStart = "${{ps:ref:" as const;
export const delimiterEnd = "}}" as const;
export const executionPrefix = "exec:";

export const identity = (i: ParamValueMap, _: Partial<ParamValueMap>) => i;
export const getDefaultTargets = (node: Node) => (results: ParamValueMap) => {
if (!node.execution?.outputs?.[0]?.id) return [];
return [
{
execSource: node.execution.outputs[0].id,
inputs: results,
},
];
};
export const getDefaultTargets =
(node: Node) => (_input: ParamValueMap, _prev: ParamValueMap, results: ParamValueMap) => {
if (!node.execution?.outputs?.[0]?.id) return [];
return [
{
execSource: node.execution.outputs[0].id,
inputs: results,
} as NextNode,
];
};
50 changes: 50 additions & 0 deletions packages/@pufflig/ps-chains/src/engines/dataflow/runFlow.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import {
configOnlyFlow,
execWithLoop,
loopWithJoin,
mappedExample,
multiInput,
multiInputWithOutput,
Expand Down Expand Up @@ -419,3 +421,51 @@ test("when running a flow in dataflow mode, do no run child executable nodes", a
expect(onNodeRunError).toHaveBeenCalledTimes(0);
expect(res).toMatchSnapshot();
});

/**
* (1) => (🔄2) =2> (3) =2> (4)
*
*/
test("a node can run its children multiple times", async () => {
const onNodeInputUpdate = jest.fn();
const onNodeRunComplete = jest.fn();
const onNodeRunError = jest.fn();
const res = await runFlow(
execWithLoop,
"n1",
{},
{
onNodeRunComplete,
onNodeInputUpdate,
onNodeRunError,
}
);
expect(onNodeInputUpdate).toHaveBeenCalledTimes(6);
expect(onNodeRunComplete).toHaveBeenCalledTimes(4);
expect(onNodeRunError).toHaveBeenCalledTimes(0);
expect(res).toMatchSnapshot();
});

/**
* (🔄1) =2> (2) =2> (3)
*/
test("a node can run its children multiple times 2", async () => {
const onNodeInputUpdate = jest.fn();
const onNodeRunComplete = jest.fn();
const onNodeRunError = jest.fn();
const res = await runFlow(
loopWithJoin,
"n1",
{},
{
logLevel: "debug",
onNodeRunComplete,
onNodeInputUpdate,
onNodeRunError,
}
);
expect(onNodeInputUpdate).toHaveBeenCalledTimes(7);
expect(onNodeRunComplete).toHaveBeenCalledTimes(3);
expect(onNodeRunError).toHaveBeenCalledTimes(0);
expect(res).toMatchSnapshot();
});
23 changes: 19 additions & 4 deletions packages/@pufflig/ps-chains/src/engines/dataflow/runFlow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export async function runFlow(flow: Flow, nodeId: string, input: Record<string,
const isAlreadyRun = (runs[nodeId] || 0) > 0;

if (isAlreadyRun) {
logger.debug("Node already run");
logger.debug({ nodeId }, "Node already run");
return;
}

Expand Down Expand Up @@ -147,14 +147,29 @@ export async function runFlow(flow: Flow, nodeId: string, input: Record<string,
if (runOptions?.mode === "dataflow") return;

// define the execution order
const executionStack = await getTargets(result);
const executionOrder = await getTargets(resolvedInput, previousState.input, result);
const executionTargets = targets.filter((edge) => edge.sourceHandle.startsWith(executionPrefix));

for (const execution of executionStack) {
logger.debug({ executions: executionOrder }, "Defined executions");

for (const execution of executionOrder) {
visitedEdges.push(...executionTargets.map((e) => e.id));
const targetId = executionTargets.find((e) => e.sourceHandle === execution.execSource)?.target;

if (targetId) {
await run_flow_recursive(targetId, {});
// override the infinite loop guard
runs[targetId] = runs[targetId] - 1;

// data edges between this node and the target node
const dataEdges = Object.values(flow.definition.edges).filter(
(e) => e.source === nodeId && e.target === targetId && !e.sourceHandle.startsWith("exec:")
);

const edgeMap = getEdgeMap(dataEdges);
const mappedInput = edgeMap ? mapOutputToInput(execution.inputs, edgeMap) : {};

logger.debug({ nodeId, targetId, mappedInput }, "Running executable node");
await run_flow_recursive(targetId, mappedInput);
}
}
};
Expand Down
136 changes: 135 additions & 1 deletion packages/@pufflig/ps-chains/src/mocks/chains.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import { Flow } from "../types";
import { configOnlyNode, multiInputDataNode, simpleDataNode, simpleExecNode } from "./nodes";
import {
configOnlyNode,
joinNodeConfig,
loopNodeConfig,
multiInputDataNode,
simpleDataNode,
simpleExecNode,
} from "./nodes";

export const singleNodeFlow: Flow = {
nodeTypes: {
Expand Down Expand Up @@ -599,3 +606,130 @@ export const simpleExecWithData: Flow = {
},
state: {},
};

export const execWithLoop: Flow = {
nodeTypes: {
simple_node: simpleExecNode,
loop_node: loopNodeConfig,
},
definition: {
edges: {
e1: {
id: "e1",
source: "n1",
target: "n2",
sourceHandle: "exec:output",
targetHandle: "exec:input",
},
e2: {
id: "e2",
source: "n2",
target: "n3",
sourceHandle: "exec:output",
targetHandle: "exec:input",
},
e3: {
id: "e3",
source: "n2",
target: "n3",
sourceHandle: "data",
targetHandle: "data",
},
e4: {
id: "e4",
source: "n3",
target: "n4",
sourceHandle: "data",
targetHandle: "data",
},
},
nodes: {
n1: {
id: "n1",
type: "simple_node",
},
n2: {
id: "n2",
type: "loop_node",
},
n3: {
id: "n3",
type: "simple_node",
},
n4: {
id: "n4",
type: "simple_node",
},
},
},
state: {
n2: {
status: "idle",
input: {
list: ["a", "b"],
},
},
},
};

export const loopWithJoin: Flow = {
nodeTypes: {
simple_node: simpleExecNode,
loop_node: loopNodeConfig,
join_node: joinNodeConfig,
},
definition: {
edges: {
e1: {
id: "e1",
source: "n1",
target: "n2",
sourceHandle: "exec:output",
targetHandle: "exec:input",
},
e2: {
id: "e2",
source: "n2",
target: "n3",
sourceHandle: "exec:output",
targetHandle: "exec:input",
},
e3: {
id: "e3",
source: "n1",
target: "n2",
sourceHandle: "data",
targetHandle: "data",
},
e4: {
id: "e4",
source: "n2",
target: "n3",
sourceHandle: "list",
targetHandle: "data",
},
},
nodes: {
n1: {
id: "n1",
type: "loop_node",
},
n2: {
id: "n2",
type: "join_node",
},
n3: {
id: "n3",
type: "simple_node",
},
},
},
state: {
n1: {
status: "idle",
input: {
list: ["a", "b"],
},
},
},
};
Loading