Skip to content

Commit

Permalink
[net] transitionEntryFire -- try to fire one transition entry
Browse files Browse the repository at this point in the history
  • Loading branch information
xieyuheng committed Nov 17, 2023
1 parent fa0271b commit aaec0fe
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 12 deletions.
8 changes: 5 additions & 3 deletions TODO.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
8 changes: 0 additions & 8 deletions src/lang/net/fireTransitionEntry.ts

This file was deleted.

45 changes: 45 additions & 0 deletions src/lang/net/transitionEntryFire.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { TransitionEntry } from "./Net"

export type FiringResult = "Fired" | "NotFired" | "FiringError"

export async function fireTransitionEntry(
transitionEntry: TransitionEntry,
): Promise<FiringResult> {
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<any> | 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<any>,
): void {
for (const placeEntry of [...transitionEntry.outputPlaceEntries].reverse()) {
placeEntry.queue.push(outputs.pop())
}
}
2 changes: 1 addition & 1 deletion src/lang/task/Task.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export type TaskFunction = (...args: Array<any>) => Promise<Array<any>>
export type TaskFunction = (...inputs: Array<any>) => Promise<Array<any>>

export type Task = {
fn: TaskFunction
Expand Down

0 comments on commit aaec0fe

Please sign in to comment.