Skip to content

Commit ec935b9

Browse files
authored
update how marking is serialised for Hazel (#90)
1 parent 8a4c87e commit ec935b9

File tree

2 files changed

+17
-11
lines changed

2 files changed

+17
-11
lines changed

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,19 @@ const isValidNetDefinition = (
5151
/**
5252
* Converts SimulationState to a 2D array format.
5353
* Each step becomes an array of objects with placeId, marking, and placeLabel.
54+
*
55+
* Marking is also converted to a representation of an association list.
5456
*/
55-
const simulationStateTo2DArray = (
57+
const simulationStateToHazelFormat = (
5658
simulationState: SimulationState,
5759
): HazelSimulationState => {
5860
return simulationState.map((step) => {
5961
return Object.entries(step).map(([placeId, { marking, placeLabel }]) => ({
6062
placeId,
61-
marking,
63+
marking: Object.entries(marking).map(([tokenId, count]) => ({
64+
0: tokenId,
65+
1: count,
66+
})),
6267
placeLabel,
6368
}));
6469
});
@@ -77,7 +82,7 @@ const convertSimulationStateForHazel = (
7782
return undefined;
7883
}
7984

80-
return simulationStateTo2DArray(simulationState);
85+
return simulationStateToHazelFormat(simulationState);
8186
};
8287

8388
/**
@@ -95,8 +100,6 @@ export const App = () => {
95100
id,
96101
codec: "json",
97102
onInit: (value) => {
98-
console.log("Received value", value);
99-
100103
try {
101104
const parsedValue = JSON.parse(value);
102105

@@ -123,7 +126,6 @@ export const App = () => {
123126

124127
const reportSimulationState = useCallback(
125128
(simulationState: SimulationState) => {
126-
console.log("Simulation state reported");
127129
setSimulationState(simulationState);
128130

129131
setSyntax({

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

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

74
export type MessageToHazel =
@@ -40,7 +37,14 @@ type HazelIntegrationConfig = {
4037
};
4138

4239
export type HazelSimulationState = Array<
43-
Array<{ placeId: string; marking: TokenCounts; placeLabel: string }>
40+
Array<{
41+
placeId: string;
42+
/**
43+
* Marking represented as a association list where the tokenId is the key and the count is the value, represented in the format Hazel expects.
44+
*/
45+
marking: Array<{ 0: string; 1: number }>;
46+
placeLabel: string;
47+
}>
4448
>;
4549

4650
export type HazelValue = {

0 commit comments

Comments
 (0)