Skip to content

Auto-detect and import Petrinaut JSON#1187

Open
tslil-topos wants to merge 2 commits intomainfrom
tslil/push-okmynlqztnoz
Open

Auto-detect and import Petrinaut JSON#1187
tslil-topos wants to merge 2 commits intomainfrom
tslil/push-okmynlqztnoz

Conversation

@tslil-topos
Copy link
Copy Markdown
Collaborator

The exported json has many features which we ignore in our import procedure because they don't (obviously) map to features of the basic Petri net models.

 Per-place: colorId, dynamicsEnabled, differentialEquationId, visualizerCode, x, y
 Per-arc: weight
 Per-transition: lambdaType, lambdaCode, transitionKernelCode, x, y
 Top-level: types (token coloring), differentialEquations, parameters, version

The exported json has many features which we ignore in our import procedure because they don't (obviously) map to features of the basic Petri net models.

```
 Per-place: colorId, dynamicsEnabled, differentialEquationId, visualizerCode, x, y
 Per-arc: weight
 Per-transition: lambdaType, lambdaCode, transitionKernelCode, x, y
 Top-level: types (token coloring), differentialEquations, parameters, version
```
@tslil-topos tslil-topos self-assigned this Apr 2, 2026
@tslil-topos tslil-topos requested review from epatters and kasbah April 2, 2026 10:55
type: "model",
theory: "petri-net",
name: title,
version: currentVersion(),
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A more future proof way would be to set this to "1" explicitly and call migrateDocument on the object before returning. That way we don't accidentally break this when bumping the schema.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i don't have strong feelings about this, only to say that i hope that by the time the schema changes the way that we interop with petrinaut is not via parsing some fraction of their json schema and converting it to our notebook schema :)

Copy link
Copy Markdown
Member

@epatters epatters left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, this is great. Just a few small comments below.

} from "catlog-wasm";

/** Detects a Petrinaut-exported JSON file. */
export function isFromPetrinaut(data: unknown): boolean {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a typical use case for type predicates: https://www.typescriptlang.org/docs/handbook/2/narrowing.html#using-type-predicates

So the signature of this function would become:

export function isFromPetrinaut(data: unknown): data is PetrinautFile {

}

/** Converts a Petrinaut-exported JSON file to a CatCoLab petri-net model document. */
export function convertFromPetrinaut(data: unknown): Document {
Copy link
Copy Markdown
Member

@epatters epatters Apr 2, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With the above change, you can give this function the desired signature and avoid an explicit type in the function body:

export function convertFromPetrinaut(data: PetrinautFile): Document {

}

const cellOrder: string[] = [];
const cellContents: Record<string, NotebookCell<ModelJudgment>> = {};
Copy link
Copy Markdown
Member

@epatters epatters Apr 2, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not too important, but I'd suggest using the helpers in NotebookUtils like newNotebook and appendCells instead of directly manipulating cellOrder/cellContents.

Copy link
Copy Markdown
Member

@epatters epatters Apr 2, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know this code isn't intended to stick around for a long time (famous last words), but this folder still feels like the wrong place for it. Perhaps src/model/ or src/stdlib/ instead?

@epatters epatters added enhancement New feature or request frontend TypeScript frontend and Rust-wasm integrations external Work on interfacing with other tools labels Apr 2, 2026
@epatters epatters changed the title feature: auto-detect and import from Petrinaut json Auto-detect and import Petrinaut JSON Apr 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request external Work on interfacing with other tools frontend TypeScript frontend and Rust-wasm integrations

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants