Skip to content
Draft
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
2 changes: 2 additions & 0 deletions libs/@hashintel/petrinaut/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,13 @@
"d3-scale": "4.0.2",
"elkjs": "0.11.0",
"monaco-editor": "0.55.1",
"monaco-pyright-lsp": "0.1.7",
"react-icons": "5.5.0",
"react-resizable-panels": "4.6.5",
"reactflow": "11.11.4",
"typescript": "5.9.3",
"uuid": "13.0.0",
"vscode-languageserver": "^9.0.1",
"vscode-languageserver-types": "3.17.5",
"web-worker": "1.4.1"
},
Expand Down
2 changes: 2 additions & 0 deletions libs/@hashintel/petrinaut/src/constants/ui.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import type { SubView } from "../components/sub-view/types";
import { diagnosticsSubView } from "../views/Editor/panels/BottomPanel/subviews/diagnostics";
import { netSettingsSubView } from "../views/Editor/panels/BottomPanel/subviews/net-settings";
import { simulationSettingsSubView } from "../views/Editor/panels/BottomPanel/subviews/simulation-settings";
import { simulationTimelineSubView } from "../views/Editor/panels/BottomPanel/subviews/simulation-timeline";
import { differentialEquationsListSubView } from "../views/Editor/panels/LeftSideBar/subviews/differential-equations-list";
Expand Down Expand Up @@ -46,6 +47,7 @@ export const LEFT_SIDEBAR_SUBVIEWS: SubView[] = [

// Base subviews always visible in the bottom panel
export const BOTTOM_PANEL_SUBVIEWS: SubView[] = [
netSettingsSubView,
diagnosticsSubView,
simulationSettingsSubView,
];
Expand Down
96 changes: 96 additions & 0 deletions libs/@hashintel/petrinaut/src/core/default-codes-python.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import type { Color } from "./types/sdcpn";

export function generateDefaultDifferentialEquationCodePython(
type: Color,
): string {
const fields = type.elements.map((el) => el.name).join(", ");
const derivatives = type.elements
.map((el) => ` "${el.name}": 1`)
.join(",\n");
return `# This function defines the differential equation for the place of type "${type.name}".
# It receives the current tokens and parameters.
# It should return the derivative of the token value in this place.
def dynamics(tokens, parameters):
return [
{
${derivatives}
} # Example: all derivatives = 1
for token in tokens
for ${fields} in [${type.elements.map((el) => `token["${el.name}"]`).join(", ")}]
]`;
}

export const DEFAULT_DIFFERENTIAL_EQUATION_CODE_PYTHON = `# This function defines the differential equation for the place.
# It receives the current tokens and parameters.
# It should return the derivative of the token value in this place.
def dynamics(tokens, parameters):
return [
{"x": 1, "y": 1} # dx/dt = 1, dy/dt = 1
for token in tokens
]`;

export const generateDefaultLambdaCodePython = (
lambdaType: "predicate" | "stochastic",
): string => `# This function controls when the transition will fire,
# once enabled by sufficient tokens in its input places.
# It receives tokens from input places keyed by place name,
# and any global parameters defined.
def lambda_fn(tokens_by_place, parameters):
# tokens_by_place is a dict like:
# {"PlaceA": [{"x": 0, "y": 0}], "PlaceB": [...]}
# where "x" and "y" are examples of dimensions (properties)
# of the token's type.

# When defining a predicate check,
# return a boolean (True = enabled, False = disabled).
#
# When defining a stochastic firing rate, return a number:
# 1. 0 means disabled
# 2. float("inf") means always enabled
# 3. Any other number is the average rate per second

${lambdaType === "predicate" ? "return True # Always enabled" : "return 1.0 # Average firing rate of once per second"}`;

export function generateDefaultTransitionKernelCodePython(
_inputs: { placeName: string; type: Color; weight: number }[],
outputs: { placeName: string; type: Color; weight: number }[],
): string {
const outputEntries = outputs
.map((arc) => {
const tokens = Array.from({ length: arc.weight })
.map(
() =>
`{${arc.type.elements.map((el) => `"${el.name}": 0`).join(", ")}}`,
)
.join(", ");
return ` "${arc.placeName}": [${tokens}],`;
})
.join("\n");

return `# This function defines the kernel for the transition.
# It receives tokens from input places,
# and any global parameters defined,
# and should return tokens for output places keyed by place name.
def transition_kernel(tokens_by_place, parameters):
# tokens_by_place is a dict like:
# {"PlaceA": [{"x": 0, "y": 0}], "PlaceB": [...]}

# Return a dict with output place names as keys
return {
${outputEntries}
}`;
}

export const DEFAULT_TRANSITION_KERNEL_CODE_PYTHON = `# This function defines the kernel for the transition.
# It receives tokens from input places,
# and any global parameters defined,
# and should return tokens for output places keyed by place name.
def transition_kernel(tokens_by_place, parameters):
# Return a dict with output place names as keys
return {
# Example: tokens for output place named "OutputPlace"
"OutputPlace": [
{"x": 0, "y": 0} # Each token is a dict with named dimensions
],
# If there are more output places, add them here
}`;
9 changes: 9 additions & 0 deletions libs/@hashintel/petrinaut/src/core/types/sdcpn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,11 @@ export type DifferentialEquation = {
code: string;
};

export type SDCPNLanguage = "typescript" | "python";

export type SDCPN = {
/** Expression language for all code in this file. Defaults to "typescript" if absent. */
language?: SDCPNLanguage;
places: Place[];
transitions: Transition[];
types: Color[];
Expand All @@ -66,3 +70,8 @@ export type MinimalNetMetadata = {
};

export type MutateSDCPN = (mutateFn: (sdcpn: SDCPN) => void) => void;

/** Resolve the effective language of an SDCPN (defaults to "typescript"). */
export function getSDCPNLanguage(sdcpn: SDCPN): SDCPNLanguage {
return sdcpn.language ?? "typescript";
}
Loading
Loading