From aaec0fed6b7253e741f25d3c17f5e376b018fdae Mon Sep 17 00:00:00 2001 From: Xie Yuheng Date: Fri, 17 Nov 2023 16:48:14 +0800 Subject: [PATCH] [net] `transitionEntryFire` -- try to fire one transition entry --- TODO.md | 8 +++-- src/lang/net/fireTransitionEntry.ts | 8 ----- src/lang/net/transitionEntryFire.ts | 45 +++++++++++++++++++++++++++++ src/lang/task/Task.ts | 2 +- 4 files changed, 51 insertions(+), 12 deletions(-) delete mode 100644 src/lang/net/fireTransitionEntry.ts create mode 100644 src/lang/net/transitionEntryFire.ts diff --git a/TODO.md b/TODO.md index 36c1a75..54421a3 100644 --- a/TODO.md +++ b/TODO.md @@ -1,14 +1,16 @@ # net -[net] `fireTransitionEntry` -- try to fire one transition entry - -[net] `run` +[net] `react` - when we add a data into a place, all the neighboring transitions are checked for preparedness, prepared transitions are executed -- let's not handle non-deterministic for now. +[net] transitionEntryPutOutputs -- error handling + +- task should have more information + # type system Type -- simple structrual type system -- for JSON diff --git a/src/lang/net/fireTransitionEntry.ts b/src/lang/net/fireTransitionEntry.ts deleted file mode 100644 index 0f8cb77..0000000 --- a/src/lang/net/fireTransitionEntry.ts +++ /dev/null @@ -1,8 +0,0 @@ -import { Net, TransitionEntry } from "./Net" - -export async function fireTransitionEntry( - net: Net, - transitionEntry: TransitionEntry, -) { - // -} diff --git a/src/lang/net/transitionEntryFire.ts b/src/lang/net/transitionEntryFire.ts new file mode 100644 index 0000000..2e7fc22 --- /dev/null +++ b/src/lang/net/transitionEntryFire.ts @@ -0,0 +1,45 @@ +import { TransitionEntry } from "./Net" + +export type FiringResult = "Fired" | "NotFired" | "FiringError" + +export async function fireTransitionEntry( + transitionEntry: TransitionEntry, +): Promise { + if (!transitionEntry.task) return "NotFired" + + const inputs = transitionEntryGetInputs(transitionEntry) + if (inputs === undefined) return "NotFired" + + try { + const outputs = await transitionEntry.task.fn(...inputs) + transitionEntryPutOutputs(transitionEntry, outputs) + return "Fired" + } catch (error) { + return "FiringError" + } +} + +function transitionEntryGetInputs( + transitionEntry: TransitionEntry, +): Array | undefined { + if ( + transitionEntry.inputPlaceEntries.some( + (placeEntry) => placeEntry.queue.length === 0, + ) + ) { + return undefined + } + + return transitionEntry.inputPlaceEntries.map((placeEntry) => + placeEntry.queue.pop(), + ) +} + +function transitionEntryPutOutputs( + transitionEntry: TransitionEntry, + outputs: Array, +): void { + for (const placeEntry of [...transitionEntry.outputPlaceEntries].reverse()) { + placeEntry.queue.push(outputs.pop()) + } +} diff --git a/src/lang/task/Task.ts b/src/lang/task/Task.ts index 65d46da..da018f8 100644 --- a/src/lang/task/Task.ts +++ b/src/lang/task/Task.ts @@ -1,4 +1,4 @@ -export type TaskFunction = (...args: Array) => Promise> +export type TaskFunction = (...inputs: Array) => Promise> export type Task = { fn: TaskFunction