66} from "@hashintel/petrinaut" ;
77import {
88 useHazelIntegration ,
9+ type HazelSimulationState ,
910 type HazelValue ,
1011} from "./app/use-hazel-integration" ;
1112import { 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 } ) ;
0 commit comments