@@ -263,19 +263,27 @@ metadata and cannot contain other RIDDL definitions.
263263
264264## Entities and States
265265
266- Entities are stateful objects with explicit states:
266+ Entities are stateful objects with explicit states. Each state
267+ references a type that defines its data structure, and can
268+ optionally contain handlers that define behavior while the
269+ entity is in that state:
267270
268271```
269272entity Product is {
270- state ProductData of ProductRecord with {
271- briefly as "Product state containing all product information"
272- described by {
273- | Contains the complete product information including identification,
274- | pricing, and inventory information.
273+ state ProductData of ProductRecord is {
274+ handler ProductHandler is {
275+ on command UpdatePrice {
276+ set field ProductRecord.price to "field price of
277+ command UpdatePrice"
278+ send event PriceUpdated to
279+ outlet ProductEvents.Products
280+ }
275281 }
282+ } with {
283+ briefly as "Product state with update handling"
276284 }
277-
278- // Commands, events, handlers
285+
286+ // Commands, events, types
279287} with {
280288 briefly as "Represents a purchasable item"
281289 described by {
@@ -285,6 +293,16 @@ entity Product is {
285293}
286294```
287295
296+ States can also be defined without a body when handlers are
297+ defined at the entity level instead:
298+
299+ ```
300+ entity SimpleProduct is {
301+ state ProductData of ProductRecord
302+ handler ProductHandler is { ??? }
303+ }
304+ ```
305+
288306States reference record types that define the data structure:
289307
290308```
@@ -296,8 +314,47 @@ type ProductRecord is {
296314} with {
297315 briefly as "Record type containing product data"
298316 described by {
299- | Defines the structure of product data including identification and pricing.
317+ | Defines the structure of product data including
318+ | identification and pricing.
319+ }
320+ }
321+ ```
322+
323+ When an entity has multiple states with their own handlers,
324+ it models a finite state machine—each state responds to
325+ messages differently, and the ` morph ` statement transitions
326+ between states:
327+
328+ ```
329+ entity Order is {
330+ state PendingOrder of PendingOrderData is {
331+ handler PendingHandler is {
332+ on command ConfirmOrder {
333+ morph entity Order to state Order.ActiveOrder
334+ with command ConfirmOrder
335+ }
336+ }
300337 }
338+ state ActiveOrder of ActiveOrderData is {
339+ handler ActiveHandler is {
340+ on command ShipOrder {
341+ morph entity Order to state Order.ShippedOrder
342+ with command ShipOrder
343+ }
344+ on command CancelOrder {
345+ morph entity Order to state Order.CancelledOrder
346+ with command CancelOrder
347+ }
348+ }
349+ }
350+ state ShippedOrder of ShippedOrderData is {
351+ handler ShippedHandler is { ??? }
352+ }
353+ state CancelledOrder of CancelledOrderData is {
354+ handler CancelledHandler is { ??? }
355+ }
356+ } with {
357+ option is finite state machine
301358}
302359```
303360
0 commit comments