statifier 0.0.0

An SCXML statechart interpreter core, a conformant sibling of the Elixir statifier package.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
//! What interpreting answers besides the next state: the effects, plain data
//! a host reads and acts on.
//!
//! The interpreter never logs, never writes anywhere and never delivers an
//! event itself. Each of those is an [`Effect`], an owned value the host
//! receives in the order the chart made them, ported field for field from
//! the reference's effect vocabulary (`Statifier.Effect` in statifier-ex).
//! Every effect carries the three counters as they stood when it was made:
//! the macrostep, the microstep within it and the round.
//!
//! With the `serde` feature an effect serializes as a map whose `kind` is
//! the variant in snake case, beside its fields in camel case; an optional
//! value that is `None` is left out, so a present `null` stays a value.

use crate::Value;
use predicator_rs::Map;

/// One effect: something the chart did that a host may observe.
///
/// The enum and every variant are `#[non_exhaustive]`: later versions add
/// variants and fields, so a host matches with a wildcard arm and binds
/// fields with `..`.
///
/// ```
/// use statifier::Effect;
///
/// fn describe(effect: &Effect) -> String {
///     match effect {
///         Effect::Log { label, value, .. } => format!("log {label:?}: {value:?}"),
///         Effect::DatamodelChange { location_source, new_value, .. } => {
///             format!("{location_source} = {new_value:?}")
///         }
///         _ => String::from("another effect"),
///     }
/// }
/// # let _ = describe;
/// ```
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq)]
pub enum Effect {
    /// A `<log>` ran: its label and the value of its expression.
    #[non_exhaustive]
    Log {
        /// The `label` attribute as written, `None` when it was not.
        label: Option<String>,
        /// The value `expr` evaluated to, `None` for a `<log>` that wrote
        /// no `expr`. The value is not coerced: it is displayed, not sent.
        value: Option<Value>,
        /// The index of the `<log>` among the chart's executable-content
        /// nodes, in document order.
        content_index: usize,
        /// The block the `<log>` ran in.
        owner: BlockOwner,
        /// The macrostep counter when it ran.
        macrostep: u64,
        /// The microstep counter when it ran.
        microstep: u64,
        /// The round counter when it ran.
        round: u64,
    },
    /// A write to the datamodel landed: enough to apply the same write to a
    /// copy of the datamodel without reading the chart's.
    #[non_exhaustive]
    DatamodelChange {
        /// The location written, resolved: the root first, then one segment
        /// per property or index.
        location_path: Vec<PathSegment>,
        /// The location as the document wrote it.
        location_source: String,
        /// The value written.
        new_value: Value,
        /// The value that stood at the full path before the write,
        /// [`Value::Undefined`] when nothing did.
        prior_value: Value,
        /// The index of the `<data>` the write bound, among the chart's
        /// `<data>` elements in document order; `None` for any other write.
        data_index: Option<usize>,
        /// The index of the executable-content node that made the write;
        /// `None` for a write made outside executable content.
        content_index: Option<usize>,
        /// The block the write belongs to; `None` only for a `<data>`
        /// binding. The value an empty `<finalize>` assigns carries
        /// [`BlockOwner::Finalize`], and an `<invoke>`'s `idlocation` write
        /// carries [`BlockOwner::Invoke`].
        owner: Option<BlockOwner>,
        /// The macrostep counter when it ran.
        macrostep: u64,
        /// The microstep counter when it ran.
        microstep: u64,
        /// The round counter when it ran.
        round: u64,
    },
    /// A `<send>` with no delay ran: a message for the host to deliver now.
    ///
    /// Every target is an effect, the session's own internal queue
    /// (`#_internal`) included: the host routes the message, so an internal
    /// send comes back to the chart as the host delivers it. A send the
    /// chart refused for its type or its target answers no effect: it
    /// raises `error.execution` or `error.communication` instead.
    #[non_exhaustive]
    Send {
        /// The event's name, as `event` or `eventexpr` gave it; `None` when
        /// neither was written or it was null.
        event: Option<Value>,
        /// The target, as `target` or `targetexpr` gave it; `None` when
        /// neither was written or it was null, which is the session itself. For a type the
        /// host registered, it is the host processor's own route and is
        /// never read here.
        target: Option<Value>,
        /// The type, as `type` or `typeexpr` gave it; `None` when neither
        /// was written, which is the SCXML Event I/O Processor.
        send_type: Option<Value>,
        /// The event's data: the `<content>`'s value when the `<send>` has
        /// one, else the `namelist` and `<param>` values as a map of name
        /// to value (the last of a repeated name kept), else
        /// [`Value::Undefined`].
        data: Value,
        /// The send id: the `id` attribute as written, else one generated,
        /// `send_` and the session's send counter.
        send_id: String,
        /// Whether the document named the send (an `id` or an
        /// `idlocation`), rather than the id being generated: an event the
        /// send delivers carries its send id only when it did.
        id_from_author: bool,
        /// The session's timer counter after this send advanced it, which
        /// it does only for a type the host registered; `None` for the
        /// SCXML Event I/O Processor's types. It tells apart two sends that
        /// share every other field, as two iterations of a `<foreach>` do.
        ordinal: Option<u64>,
        /// The index of the `<send>` among the chart's executable-content
        /// nodes, in document order.
        content_index: usize,
        /// The block the `<send>` ran in.
        owner: BlockOwner,
        /// The macrostep counter when it ran.
        macrostep: u64,
        /// The microstep counter when it ran.
        microstep: u64,
        /// The round counter when it ran.
        round: u64,
    },
    /// A `<send>` with a delay ran: a message for the host to deliver once
    /// the delay has passed, unless a `<cancel>` names its send id first.
    /// It carries what [`Effect::Send`] does, and the delay.
    #[non_exhaustive]
    SendDelayed {
        /// The event's name, as `event` or `eventexpr` gave it; `None` when
        /// neither was written or it was null.
        event: Option<Value>,
        /// The target, as `target` or `targetexpr` gave it; `None` when
        /// neither was written or it was null. It is judged when the delay has passed,
        /// not here, apart from a target that names nothing the SCXML Event
        /// I/O Processor carries, which is refused now.
        target: Option<Value>,
        /// The type, as `type` or `typeexpr` gave it; `None` when neither
        /// was written.
        send_type: Option<Value>,
        /// The event's data, as [`Effect::Send`]'s `data`.
        data: Value,
        /// The send id a `<cancel>` names it by: the `id` attribute as
        /// written, else one generated.
        send_id: String,
        /// Whether the document named the send (an `id` or an
        /// `idlocation`).
        id_from_author: bool,
        /// The delay in whole milliseconds, as `delay` or `delayexpr` gave
        /// it.
        delay_ms: i64,
        /// The session's timer counter after this send advanced it: every
        /// delayed send advances it.
        ordinal: u64,
        /// The index of the `<send>` among the chart's executable-content
        /// nodes, in document order.
        content_index: usize,
        /// The block the `<send>` ran in.
        owner: BlockOwner,
        /// The macrostep counter when it ran, not when the delay passes.
        macrostep: u64,
        /// The microstep counter when it ran.
        microstep: u64,
        /// The round counter when it ran.
        round: u64,
    },
    /// The datamodel as it stood when the chart started, every `<data>` id
    /// declared and none of them bound yet: the base a host applies each
    /// [`Effect::DatamodelChange`] to. It is the first effect a chart
    /// answers when it starts.
    #[non_exhaustive]
    DatamodelInit {
        /// The datamodel: the host's values, the system variables over them,
        /// and every `<data>` id the host did not supply, as
        /// [`Value::Undefined`].
        datamodel: Map,
        /// The macrostep counter when it was made.
        macrostep: u64,
        /// The microstep counter when it was made.
        microstep: u64,
        /// The round counter when it was made.
        round: u64,
    },
    /// A macrostep spent its round budget without reaching a stable
    /// configuration. The chart is where the last round left it and still
    /// running: nothing exited and nothing stopped. It comes last among the
    /// effects of the call that spent the budget.
    #[non_exhaustive]
    BudgetExhausted {
        /// The indexes of the active states as the last round left them,
        /// ascending.
        configuration: Vec<usize>,
        /// The budget spent: the most rounds one macrostep may take.
        budget: u64,
        /// The events still on the internal queue, oldest first, each as
        /// `_event` would read it: a map of its name, type, send id, origin,
        /// origin type, invoke id and data.
        pending_internal_events: Vec<Value>,
        /// The macrostep counter when it was spent.
        macrostep: u64,
        /// The microstep counter when it was spent.
        microstep: u64,
        /// The round counter when it was spent: the rounds the macrostep took.
        round: u64,
    },
    /// The chart stopped: every active state has exited, and nothing more
    /// happens to it. It comes last among the effects of the call that
    /// stopped it.
    #[non_exhaustive]
    Done {
        /// The top-level final state's `<donedata>`: [`Value::Undefined`]
        /// when it has none, or when evaluating it failed; `None` when no
        /// top-level final state exited, as when the chart was cancelled.
        donedata: Option<Value>,
        /// The data of the first `error.execution` evaluating the donedata
        /// raised, `None` when it raised none. Nothing can take that event
        /// from the internal queue once the chart has stopped, so it is
        /// carried here.
        donedata_error: Option<Value>,
        /// The indexes of the states that were active when the chart
        /// stopped, ascending.
        configuration: Vec<usize>,
        /// The macrostep counter when it stopped.
        macrostep: u64,
        /// The microstep counter when it stopped.
        microstep: u64,
        /// The round counter when it stopped.
        round: u64,
    },
    /// A `<cancel>` ran: the host drops the delayed send it names, if one
    /// is still pending. A send id that names nothing pending is not an
    /// error.
    #[non_exhaustive]
    Cancel {
        /// The send id, as `sendid` or `sendidexpr` gave it.
        send_id: Value,
        /// The session's timer counter after this cancel advanced it: every
        /// cancel advances it.
        ordinal: u64,
        /// The index of the `<cancel>` among the chart's
        /// executable-content nodes, in document order.
        content_index: usize,
        /// The block the `<cancel>` ran in.
        owner: BlockOwner,
        /// The macrostep counter when it ran.
        macrostep: u64,
        /// The microstep counter when it ran.
        microstep: u64,
        /// The round counter when it ran.
        round: u64,
    },
    /// An `<invoke>` ran: an invocation started in its state's invoke pass.
    /// The SCXML type (none, `scxml` or the type's URI) is recorded live and
    /// the driver starts its child session; any other type is answered for
    /// the host and never recorded live.
    #[non_exhaustive]
    Invoke {
        /// The invoke id: the `id` attribute as written, else one generated,
        /// `inv_` and the session's invoke counter, after the state's id and
        /// a dot when the state has one.
        invoke_id: String,
        /// The type, as `type` or `typeexpr` gave it; `None` when neither
        /// was written or it was null.
        invoke_type: Option<Value>,
        /// The source, as `src` or `srcexpr` gave it; `None` when neither
        /// was written or it was null. Nothing here fetches it.
        src: Option<Value>,
        /// The `namelist` and `<param>` values as a map of name to value
        /// (the last of a repeated name kept), [`Value::Undefined`] when
        /// there are none.
        params: Value,
        /// The `<content>`'s value, `None` when the `<invoke>` has none:
        /// inline markup is its source text.
        content: Option<Value>,
        /// The `autoforward` attribute.
        autoforward: bool,
        /// The index of the state holding the `<invoke>`.
        state_index: usize,
        /// Which of the state's `<invoke>` elements, in document order from
        /// 0.
        invoke_index: usize,
        /// The macrostep counter when it ran.
        macrostep: u64,
        /// The microstep counter when it ran.
        microstep: u64,
        /// The round counter when it ran.
        round: u64,
    },
    /// The state holding a live invocation exited: the invocation is
    /// cancelled and its child stopped.
    #[non_exhaustive]
    CancelInvoke {
        /// The invoke id.
        invoke_id: String,
        /// The index of the state that exited.
        state_index: usize,
        /// The macrostep counter when it exited.
        macrostep: u64,
        /// The microstep counter when it exited.
        microstep: u64,
        /// The round counter when it exited.
        round: u64,
    },
    /// An external event reached a live invocation whose `<invoke>` sets
    /// `autoforward`: the event is forwarded to the child unchanged.
    #[non_exhaustive]
    Autoforward {
        /// The invoke id.
        invoke_id: String,
        /// The index of the state holding the `<invoke>`.
        state_index: usize,
        /// The event, as `_event` reads it: a map of its name, type, send
        /// id, origin, origin type, invoke id and data.
        event: Value,
        /// The macrostep counter when it was forwarded.
        macrostep: u64,
        /// The microstep counter when it was forwarded.
        microstep: u64,
        /// The round counter when it was forwarded.
        round: u64,
    },
    /// An effect an invoked child session answered, among the effects of
    /// the call that ran it, in the order it was made. A grandchild's comes
    /// wrapped once, naming its own session.
    #[non_exhaustive]
    Child {
        /// The child's session id: its parent's, a dot and the invoke id.
        session_id: String,
        /// The effect.
        effect: Box<Effect>,
    },
}

/// The block of executable content an effect came from. State and
/// transition indexes count in document order, the `<scxml>` root being
/// state 0, and `ordinal` counts a state's blocks of one kind from 0.
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum BlockOwner {
    /// One of a state's `<onentry>` blocks.
    #[non_exhaustive]
    OnEntry {
        /// The state's index.
        state: usize,
        /// Which of its `<onentry>` blocks.
        ordinal: usize,
    },
    /// One of a state's `<onexit>` blocks.
    #[non_exhaustive]
    OnExit {
        /// The state's index.
        state: usize,
        /// Which of its `<onexit>` blocks.
        ordinal: usize,
    },
    /// A transition's own content.
    #[non_exhaustive]
    Transition {
        /// The transition's index.
        transition: usize,
    },
    /// An `<invoke>`, for its `idlocation` write.
    #[non_exhaustive]
    Invoke {
        /// The index of the state holding the `<invoke>`.
        state: usize,
        /// Which of the state's `<invoke>` elements.
        invoke: usize,
    },
    /// An `<invoke>`'s `<finalize>` block.
    #[non_exhaustive]
    Finalize {
        /// The index of the state holding the `<invoke>`.
        state: usize,
        /// Which of the state's `<invoke>` elements.
        invoke: usize,
    },
}

/// One segment of a resolved datamodel location.
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum PathSegment {
    /// A property, or the root's name: `b` in `a.b`, `'b'` in `a['b']`.
    Key(String),
    /// An integer index or key: `0` in `a[0]`.
    Index(i64),
}