Skip to content

Commit 8a4c87e

Browse files
authored
change how simulation state is sent to hazel (#89)
1 parent c18c7dc commit 8a4c87e

File tree

2 files changed

+38
-13
lines changed

2 files changed

+38
-13
lines changed

pocs/petrinaut-hazel/src/main/app.tsx

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
} from "@hashintel/petrinaut";
77
import {
88
useHazelIntegration,
9+
type HazelSimulationState,
910
type HazelValue,
1011
} from "./app/use-hazel-integration";
1112
import { produce } from "immer";
@@ -48,19 +49,35 @@ const isValidNetDefinition = (
4849
};
4950

5051
/**
51-
* Hazel errors if sent an empty array at the root of an object value returned, e.g. { simulationState: [] }.
52+
* Converts SimulationState to a 2D array format.
53+
* Each step becomes an array of objects with placeId, marking, and placeLabel.
5254
*/
53-
const stripEmptyTuple = (
55+
const simulationStateTo2DArray = (
56+
simulationState: SimulationState,
57+
): HazelSimulationState => {
58+
return simulationState.map((step) => {
59+
return Object.entries(step).map(([placeId, { marking, placeLabel }]) => ({
60+
placeId,
61+
marking,
62+
placeLabel,
63+
}));
64+
});
65+
};
66+
67+
const convertSimulationStateForHazel = (
5468
simulationState: SimulationState,
5569
): HazelValue["simulationState"] => {
5670
if (
5771
simulationState.length === 0 ||
5872
Object.keys(simulationState[0]).length === 0
5973
) {
74+
/**
75+
* Hazel errors if sent an empty array at the root of an object value returned, e.g. { simulationState: [] }.
76+
*/
6077
return undefined;
6178
}
6279

63-
return simulationState;
80+
return simulationStateTo2DArray(simulationState);
6481
};
6582

6683
/**
@@ -83,17 +100,19 @@ export const App = () => {
83100
try {
84101
const parsedValue = JSON.parse(value);
85102

86-
if (isValidNetDefinition(parsedValue.netDefinition)) {
87-
setNetDefinition(parsedValue.netDefinition);
88-
setSimulationState(parsedValue.simulationState ?? []);
103+
const { netDefinition, simulationState } = parsedValue;
104+
105+
if (isValidNetDefinition(netDefinition)) {
106+
setNetDefinition(netDefinition);
107+
setSimulationState(simulationState ?? []);
89108
} else {
90-
console.error("Invalid net definition", parsedValue.netDefinition);
109+
console.error("Invalid net definition", netDefinition);
91110
const defaultNetDefinition = createDefaultNetDefinition();
92111
setNetDefinition(defaultNetDefinition);
93112

94113
setSyntax({
95114
netDefinition: defaultNetDefinition,
96-
simulationState: stripEmptyTuple(simulationState),
115+
simulationState: convertSimulationStateForHazel(simulationState),
97116
});
98117
}
99118
} catch (error) {
@@ -109,7 +128,7 @@ export const App = () => {
109128

110129
setSyntax({
111130
netDefinition: netDefinition as PetriNetDefinitionObject,
112-
simulationState: stripEmptyTuple(simulationState),
131+
simulationState: convertSimulationStateForHazel(simulationState),
113132
});
114133
},
115134
[netDefinition, setSyntax],
@@ -121,7 +140,7 @@ export const App = () => {
121140
const newDefinition = produce(existingDefinition, definitionMutationFn);
122141
setSyntax({
123142
netDefinition: newDefinition as PetriNetDefinitionObject,
124-
simulationState: stripEmptyTuple(simulationState),
143+
simulationState: convertSimulationStateForHazel(simulationState),
125144
});
126145
return newDefinition;
127146
});

pocs/petrinaut-hazel/src/main/app/use-hazel-integration.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
import type { PetriNetDefinitionObject } from "@hashintel/petrinaut";
2-
import type { SimulationState } from "@hashintel/petrinaut/dist/petrinaut/types";
1+
import type {
2+
PetriNetDefinitionObject,
3+
TokenCounts,
4+
} from "@hashintel/petrinaut";
35
import { useCallback, useEffect, useState } from "react";
46

57
export type MessageToHazel =
@@ -37,9 +39,13 @@ type HazelIntegrationConfig = {
3739
onInit: (value: string) => void;
3840
};
3941

42+
export type HazelSimulationState = Array<
43+
Array<{ placeId: string; marking: TokenCounts; placeLabel: string }>
44+
>;
45+
4046
export type HazelValue = {
4147
netDefinition: PetriNetDefinitionObject;
42-
simulationState: SimulationState | undefined;
48+
simulationState: HazelSimulationState | undefined;
4349
};
4450

4551
const sendToHazel = (message: MessageToHazel, targetOrigin: string) => {

0 commit comments

Comments
 (0)