Tutorial

12.8 Export the definition

The machine is data, so it can be exported: a JSON description of every state, event, guard and reduction. The browser's copy of the machine in Part 6 is generated from it, and a corpus of sample inputs, from Differential, says what the machine should do with each. Both are generated by the trax tool and committed, so a change to the machine shows up in review as a change to them.

Install the tool for the solution:

dotnet new tool-manifest
dotnet tool install Trax.Cli --version 1.61.0

Build the library, then export:

dotnet build Shopfront.Core
dotnet trax machine generate --assembly Shopfront.Core/bin/Debug/net10.0/Shopfront.Core.dll \
  --machine Shopfront.Machines.Checkout.CheckoutMachine \
  --ir-out machines/checkout --corpus-out machines/checkout
  ir        machines/checkout/checkout.ir.json
  corpus    machines/checkout/differential.json
Generated 2 artifact(s) for 'checkout'.

The corpus is worked out by running each sample through the reference engine, the TypeScript one the browser will use, so generate needs Node.js 22 or later on the path. Build the library again afterwards, so it embeds the corpus it was just given.

dotnet trax machine check, with the same arguments, fails when the committed files no longer match the machine. It belongs in CI.

  ok      ir        machines/checkout/checkout.ir.json
  ok      corpus    machines/checkout/differential.json
All artifacts for 'checkout' are up to date.

SDK Reference

Machine | Rules | AddStateMachines | Machines over GraphQL | UsePersistedOperations | trax CLI


Next: 12.9 Test it