Skip to content
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
39 changes: 29 additions & 10 deletions pocs/petrinaut-hazel/src/main/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
} from "@hashintel/petrinaut";
import {
useHazelIntegration,
type HazelSimulationState,
type HazelValue,
} from "./app/use-hazel-integration";
import { produce } from "immer";
Expand Down Expand Up @@ -48,19 +49,35 @@ const isValidNetDefinition = (
};

/**
* Hazel errors if sent an empty array at the root of an object value returned, e.g. { simulationState: [] }.
* Converts SimulationState to a 2D array format.
* Each step becomes an array of objects with placeId, marking, and placeLabel.
*/
const stripEmptyTuple = (
const simulationStateTo2DArray = (
simulationState: SimulationState,
): HazelSimulationState => {
return simulationState.map((step) => {
return Object.entries(step).map(([placeId, { marking, placeLabel }]) => ({
placeId,
marking,
placeLabel,
}));
});
};

const convertSimulationStateForHazel = (
simulationState: SimulationState,
): HazelValue["simulationState"] => {
if (
simulationState.length === 0 ||
Object.keys(simulationState[0]).length === 0
) {
/**
* Hazel errors if sent an empty array at the root of an object value returned, e.g. { simulationState: [] }.
*/
return undefined;
}

return simulationState;
return simulationStateTo2DArray(simulationState);
};

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

if (isValidNetDefinition(parsedValue.netDefinition)) {
setNetDefinition(parsedValue.netDefinition);
setSimulationState(parsedValue.simulationState ?? []);
const { netDefinition, simulationState } = parsedValue;

if (isValidNetDefinition(netDefinition)) {
setNetDefinition(netDefinition);
setSimulationState(simulationState ?? []);
} else {
console.error("Invalid net definition", parsedValue.netDefinition);
console.error("Invalid net definition", netDefinition);
const defaultNetDefinition = createDefaultNetDefinition();
setNetDefinition(defaultNetDefinition);

setSyntax({
netDefinition: defaultNetDefinition,
simulationState: stripEmptyTuple(simulationState),
simulationState: convertSimulationStateForHazel(simulationState),
});
}
} catch (error) {
Expand All @@ -109,7 +128,7 @@ export const App = () => {

setSyntax({
netDefinition: netDefinition as PetriNetDefinitionObject,
simulationState: stripEmptyTuple(simulationState),
simulationState: convertSimulationStateForHazel(simulationState),
});
},
[netDefinition, setSyntax],
Expand All @@ -121,7 +140,7 @@ export const App = () => {
const newDefinition = produce(existingDefinition, definitionMutationFn);
setSyntax({
netDefinition: newDefinition as PetriNetDefinitionObject,
simulationState: stripEmptyTuple(simulationState),
simulationState: convertSimulationStateForHazel(simulationState),
});
return newDefinition;
});
Expand Down
12 changes: 9 additions & 3 deletions pocs/petrinaut-hazel/src/main/app/use-hazel-integration.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import type { PetriNetDefinitionObject } from "@hashintel/petrinaut";
import type { SimulationState } from "@hashintel/petrinaut/dist/petrinaut/types";
import type {
PetriNetDefinitionObject,
TokenCounts,
} from "@hashintel/petrinaut";
import { useCallback, useEffect, useState } from "react";

export type MessageToHazel =
Expand Down Expand Up @@ -37,9 +39,13 @@ type HazelIntegrationConfig = {
onInit: (value: string) => void;
};

export type HazelSimulationState = Array<
Array<{ placeId: string; marking: TokenCounts; placeLabel: string }>
>;

export type HazelValue = {
netDefinition: PetriNetDefinitionObject;
simulationState: SimulationState | undefined;
simulationState: HazelSimulationState | undefined;
};

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