jssm 5.104.2 → 5.112.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/MIGRATING-jssm-viz.md +67 -0
- package/README.md +179 -882
- package/dist/deno/README.md +347 -0
- package/dist/{es6 → deno}/jssm.d.ts +773 -39
- package/dist/deno/jssm.js +1 -0
- package/{jssm_compiler.d.ts → dist/deno/jssm_compiler.d.ts} +17 -2
- package/dist/deno/jssm_constants.d.ts +37 -0
- package/dist/deno/jssm_error.d.ts +27 -0
- package/dist/deno/jssm_theme.d.ts +15 -0
- package/dist/{es6 → deno}/jssm_types.d.ts +327 -8
- package/dist/deno/jssm_util.d.ts +258 -0
- package/dist/deno/jssm_viz.d.ts +175 -0
- package/dist/deno/jssm_viz_colors.d.ts +63 -0
- package/dist/jssm.es5.cjs +1 -1
- package/dist/jssm.es5.iife.js +1 -0
- package/dist/jssm.es6.mjs +1 -1
- package/dist/jssm_viz.cjs +1 -0
- package/dist/jssm_viz.iife.cjs +1 -0
- package/dist/jssm_viz.mjs +1 -0
- package/jssm.es5.d.cts +1191 -43
- package/jssm.es6.d.ts +1191 -43
- package/jssm_viz.es5.d.cts +2341 -0
- package/jssm_viz.es6.d.ts +2341 -0
- package/package.json +73 -24
- package/.clocignore +0 -1
- package/.codeclimate.yml +0 -22
- package/.editorconfig +0 -12
- package/.eslintrc +0 -20
- package/.gitattributes +0 -6
- package/.nycrc +0 -6
- package/.travis.yml +0 -9
- package/CHANGELOG.md +0 -178
- package/dist/es6/fsl_parser.js +0 -1
- package/dist/es6/jssm.js +0 -2488
- package/dist/es6/jssm_arrow.js +0 -187
- package/dist/es6/jssm_compiler.d.ts +0 -135
- package/dist/es6/jssm_compiler.js +0 -366
- package/dist/es6/jssm_constants.d.ts +0 -5
- package/dist/es6/jssm_constants.js +0 -94
- package/dist/es6/jssm_error.d.ts +0 -8
- package/dist/es6/jssm_error.js +0 -28
- package/dist/es6/jssm_theme.d.ts +0 -4
- package/dist/es6/jssm_theme.js +0 -13
- package/dist/es6/jssm_types.js +0 -3
- package/dist/es6/jssm_util.d.ts +0 -106
- package/dist/es6/jssm_util.js +0 -180
- package/dist/es6/themes/jssm_base_stylesheet.d.ts +0 -11
- package/dist/es6/themes/jssm_base_stylesheet.js +0 -58
- package/dist/es6/themes/jssm_theme_bold.d.ts +0 -11
- package/dist/es6/themes/jssm_theme_bold.js +0 -58
- package/dist/es6/themes/jssm_theme_default.d.ts +0 -11
- package/dist/es6/themes/jssm_theme_default.js +0 -58
- package/dist/es6/themes/jssm_theme_modern.d.ts +0 -11
- package/dist/es6/themes/jssm_theme_modern.js +0 -58
- package/dist/es6/themes/jssm_theme_ocean.d.ts +0 -11
- package/dist/es6/themes/jssm_theme_ocean.js +0 -56
- package/dist/es6/themes/jssm_theme_plain.d.ts +0 -11
- package/dist/es6/themes/jssm_theme_plain.js +0 -70
- package/dist/es6/version.js +0 -2
- package/dist/jssm.es5.iife.cjs +0 -1
- package/dist/jssm.es5.iife.nonmin.cjs +0 -23180
- package/dist/jssm.es5.nonmin.cjs +0 -23175
- package/dist/jssm.es6.nonmin.cjs +0 -23144
- package/fsl_parser.d.ts +0 -6
- package/jest-dragon.config.cjs +0 -33
- package/jest-spec.config.cjs +0 -33
- package/jest-stoch.config.cjs +0 -33
- package/jest-unicode.config.cjs +0 -33
- package/jssm.d.ts +0 -1141
- package/jssm_arrow.d.ts +0 -53
- package/jssm_constants.d.ts +0 -5
- package/jssm_error.d.ts +0 -8
- package/jssm_theme.d.ts +0 -4
- package/jssm_types.d.ts +0 -378
- package/jssm_util.d.ts +0 -106
- package/rollup.config.deno.js +0 -44
- package/rollup.config.es5.js +0 -52
- package/rollup.config.es6.js +0 -55
- package/tutorial_learn_testing.md +0 -168
- package/typedoc-options.cjs +0 -68
- package/version.d.ts +0 -2
- /package/dist/{es6 → deno}/fsl_parser.d.ts +0 -0
- /package/dist/{es6 → deno}/jssm_arrow.d.ts +0 -0
- /package/dist/{es6 → deno}/version.d.ts +0 -0
|
@@ -0,0 +1,2341 @@
|
|
|
1
|
+
import { circular_buffer } from 'circular_buffer_js';
|
|
2
|
+
|
|
3
|
+
declare type StateType$1 = string;
|
|
4
|
+
/**
|
|
5
|
+
* A color value accepted by jssm-viz for state and arrow styling. Currently
|
|
6
|
+
* any string, validated downstream by Graphviz / the named-colors list.
|
|
7
|
+
* Intended to be narrowed to `#RRGGBB` / `#RRGGBBAA` and CSS named colors
|
|
8
|
+
* in a future release.
|
|
9
|
+
*/
|
|
10
|
+
declare type JssmColor = string;
|
|
11
|
+
/**
|
|
12
|
+
* Three-state policy flag: `'required'`, `'disallowed'`, or `'optional'`.
|
|
13
|
+
* Used by machine configuration where a default-permissive middle ground
|
|
14
|
+
* is meaningful (for example, the `actions` config key).
|
|
15
|
+
*/
|
|
16
|
+
declare type JssmPermittedOpt = 'required' | 'disallowed' | 'optional';
|
|
17
|
+
/**
|
|
18
|
+
* The set of ASCII arrow tokens recognized by the FSL grammar. Each arrow
|
|
19
|
+
* encodes a direction (one-way left/right, or two-way) and a "kind" for
|
|
20
|
+
* each direction (`-` legal, `=` main path, `~` forced-only). See the
|
|
21
|
+
* Language Reference docs for the full semantic table.
|
|
22
|
+
*/
|
|
23
|
+
declare type JssmArrow = '->' | '<-' | '<->' | '<=->' | '<~->' | '=>' | '<=' | '<=>' | '<-=>' | '<~=>' | '~>' | '<~' | '<~>' | '<-~>' | '<=~>';
|
|
24
|
+
/**
|
|
25
|
+
* A type teaching Typescript the various supported shapes for nodes, mostly inherited from GraphViz
|
|
26
|
+
*/
|
|
27
|
+
declare type JssmShape = "box" | "polygon" | "ellipse" | "oval" | "circle" | "point" | "egg" | "triangle" | "plaintext" | "plain" | "diamond" | "trapezium" | "parallelogram" | "house" | "pentagon" | "hexagon" | "septagon" | "octagon" | "doublecircle" | "doubleoctagon" | "tripleoctagon" | "invtriangle" | "invtrapezium" | "invhouse" | "Mdiamond" | "Msquare" | "Mcircle" | "rect" | "rectangle" | "square" | "star" | "none" | "underline" | "cylinder" | "note" | "tab" | "folder" | "box3d" | "component" | "promoter" | "cds" | "terminator" | "utr" | "primersite" | "restrictionsite" | "fivepoverhang" | "threepoverhang" | "noverhang" | "assembly" | "signature" | "insulator" | "ribosite" | "rnastab" | "proteasesite" | "proteinstab" | "rpromoter" | "rarrow" | "larrow" | "lpromoter" | "record";
|
|
28
|
+
/**
|
|
29
|
+
* Semantic category of an arrow's transition. `'legal'` is a normal
|
|
30
|
+
* transition, `'main'` is part of the machine's primary path, `'forced'`
|
|
31
|
+
* may only be taken via {@link Machine.force_transition}, and `'none'`
|
|
32
|
+
* means no transition exists in that direction.
|
|
33
|
+
*/
|
|
34
|
+
declare type JssmArrowKind = 'none' | 'legal' | 'main' | 'forced';
|
|
35
|
+
/**
|
|
36
|
+
* Graphviz layout engine selector. Controls how jssm-viz lays out the
|
|
37
|
+
* rendered diagram; `'dot'` is the default and most useful for state
|
|
38
|
+
* machines. See the Graphviz documentation for the differences.
|
|
39
|
+
*/
|
|
40
|
+
declare type JssmLayout = 'dot' | 'circo' | 'twopi' | 'fdp' | 'neato';
|
|
41
|
+
declare type JssmCorner = 'regular' | 'rounded' | 'lined';
|
|
42
|
+
declare type JssmLineStyle = 'solid' | 'dashed' | 'dotted';
|
|
43
|
+
/**
|
|
44
|
+
* Tristate flag for whether a property may be overridden at runtime.
|
|
45
|
+
* `true` permits overrides, `false` forbids them, and `undefined` defers
|
|
46
|
+
* the decision to the surrounding configuration's default.
|
|
47
|
+
*/
|
|
48
|
+
declare type JssmAllowsOverride = true | false | undefined;
|
|
49
|
+
/**
|
|
50
|
+
* Runtime-iterable list of valid `flow` directions for FSL diagrams.
|
|
51
|
+
* Use this when you need to enumerate directions; for the type itself
|
|
52
|
+
* see {@link FslDirection}.
|
|
53
|
+
*/
|
|
54
|
+
declare const FslDirections: readonly ["up", "right", "down", "left"];
|
|
55
|
+
/**
|
|
56
|
+
* String literal type of the four supported FSL flow directions. This is
|
|
57
|
+
* the type of the `flow` config key on a machine.
|
|
58
|
+
*/
|
|
59
|
+
declare type FslDirection = typeof FslDirections[number];
|
|
60
|
+
/**
|
|
61
|
+
* Runtime-iterable list of the built-in theme names that ship with jssm-viz.
|
|
62
|
+
* Use this when you need to enumerate themes; for the type itself see
|
|
63
|
+
* {@link FslTheme}.
|
|
64
|
+
*/
|
|
65
|
+
declare const FslThemes: readonly ["default", "ocean", "modern", "plain", "bold"];
|
|
66
|
+
/**
|
|
67
|
+
* String literal type of the built-in theme names. This is the element
|
|
68
|
+
* type of the `theme` config key (which accepts an array so that themes
|
|
69
|
+
* can be layered).
|
|
70
|
+
*/
|
|
71
|
+
declare type FslTheme = typeof FslThemes[number];
|
|
72
|
+
/**
|
|
73
|
+
* Persistable snapshot of a Machine produced by {@link Machine.serialize}
|
|
74
|
+
* and consumed by {@link deserialize}. Carries the current state, the
|
|
75
|
+
* associated machine data, the recent history (subject to the configured
|
|
76
|
+
* capacity), and metadata to detect version-skew on rehydration.
|
|
77
|
+
*
|
|
78
|
+
* @typeParam DataType - The type of the user-supplied data payload (`mDT`).
|
|
79
|
+
*/
|
|
80
|
+
declare type JssmSerialization<DataType> = {
|
|
81
|
+
jssm_version: string;
|
|
82
|
+
timestamp: number;
|
|
83
|
+
comment?: string | undefined;
|
|
84
|
+
state: StateType$1;
|
|
85
|
+
history: [string, DataType][];
|
|
86
|
+
history_capacity: number;
|
|
87
|
+
data: DataType;
|
|
88
|
+
};
|
|
89
|
+
/**
|
|
90
|
+
* Declaration of a named property that a machine's states may carry.
|
|
91
|
+
* Set `required: true` to force every state to define the property, or
|
|
92
|
+
* provide `default_value` to fall back when the state does not specify it.
|
|
93
|
+
*/
|
|
94
|
+
declare type JssmPropertyDefinition = {
|
|
95
|
+
name: string;
|
|
96
|
+
default_value?: any;
|
|
97
|
+
required?: boolean;
|
|
98
|
+
};
|
|
99
|
+
declare type JssmTransitionPermitter<DataType> = (OldState: StateType$1, NewState: StateType$1, OldData: DataType, NewData: DataType) => boolean;
|
|
100
|
+
declare type JssmTransitionPermitterMaybeArray<DataType> = JssmTransitionPermitter<DataType> | Array<JssmTransitionPermitter<DataType>>;
|
|
101
|
+
/**
|
|
102
|
+
* A single directed transition (edge) within a state machine. Captures
|
|
103
|
+
* both the topology (`from` / `to`), the FSL semantics (`kind`,
|
|
104
|
+
* `forced_only`, `main_path`), and any optional metadata such as a
|
|
105
|
+
* per-edge `name`, an action label, a guard `check`, a transition
|
|
106
|
+
* `probability` for stochastic models, and an `after_time` for timed
|
|
107
|
+
* transitions.
|
|
108
|
+
*
|
|
109
|
+
* @typeParam StateType - The state-name type (usually `string`).
|
|
110
|
+
* @typeParam DataType - The machine's data payload type (`mDT`).
|
|
111
|
+
*/
|
|
112
|
+
declare type JssmTransition<StateType, DataType> = {
|
|
113
|
+
from: StateType;
|
|
114
|
+
to: StateType;
|
|
115
|
+
after_time?: number;
|
|
116
|
+
se?: JssmCompileSe<StateType, DataType>;
|
|
117
|
+
name?: StateType;
|
|
118
|
+
action?: StateType;
|
|
119
|
+
check?: JssmTransitionPermitterMaybeArray<DataType>;
|
|
120
|
+
probability?: number;
|
|
121
|
+
kind: JssmArrowKind;
|
|
122
|
+
forced_only: boolean;
|
|
123
|
+
main_path: boolean;
|
|
124
|
+
};
|
|
125
|
+
/** A list of {@link JssmTransition}s — the edge set of a machine. */
|
|
126
|
+
declare type JssmTransitions<StateType, DataType> = JssmTransition<StateType, DataType>[];
|
|
127
|
+
/**
|
|
128
|
+
* The set of states that can immediately precede or follow a given state.
|
|
129
|
+
* Returned by jssm helpers that report a state's connectivity in the graph.
|
|
130
|
+
*/
|
|
131
|
+
declare type JssmTransitionList = {
|
|
132
|
+
entrances: Array<StateType$1>;
|
|
133
|
+
exits: Array<StateType$1>;
|
|
134
|
+
};
|
|
135
|
+
/**
|
|
136
|
+
* Topology record for one node in a compiled machine: its name, the set of
|
|
137
|
+
* states it can be reached from, the set of states it can transition to,
|
|
138
|
+
* and whether reaching it constitutes "completing" the machine.
|
|
139
|
+
*/
|
|
140
|
+
declare type JssmGenericState = {
|
|
141
|
+
from: Array<StateType$1>;
|
|
142
|
+
name: StateType$1;
|
|
143
|
+
to: Array<StateType$1>;
|
|
144
|
+
complete: boolean;
|
|
145
|
+
};
|
|
146
|
+
/**
|
|
147
|
+
* The full internal bookkeeping snapshot of a {@link Machine}, exposed for
|
|
148
|
+
* advanced introspection. Contains the current state, the state map, the
|
|
149
|
+
* edge map and reverse-action map, and the original edge list. The
|
|
150
|
+
* `internal_state_impl_version` field exists so that consumers can detect
|
|
151
|
+
* shape changes if this representation evolves.
|
|
152
|
+
*/
|
|
153
|
+
declare type JssmMachineInternalState<DataType> = {
|
|
154
|
+
internal_state_impl_version: 1;
|
|
155
|
+
state: StateType$1;
|
|
156
|
+
states: Map<StateType$1, JssmGenericState>;
|
|
157
|
+
named_transitions: Map<StateType$1, number>;
|
|
158
|
+
edge_map: Map<StateType$1, Map<StateType$1, number>>;
|
|
159
|
+
actions: Map<StateType$1, Map<StateType$1, number>>;
|
|
160
|
+
reverse_actions: Map<StateType$1, Map<StateType$1, number>>;
|
|
161
|
+
edges: Array<JssmTransition<StateType$1, DataType>>;
|
|
162
|
+
};
|
|
163
|
+
declare type JssmStatePermitter<DataType> = (OldState: StateType$1, NewState: StateType$1, OldData: DataType, NewData: DataType) => boolean;
|
|
164
|
+
declare type JssmStatePermitterMaybeArray<DataType> = JssmStatePermitter<DataType> | Array<JssmStatePermitter<DataType>>;
|
|
165
|
+
/**
|
|
166
|
+
* A single key/value pair from an FSL `state X: { ... };` block, in the
|
|
167
|
+
* raw form produced by the parser before being condensed into a
|
|
168
|
+
* {@link JssmStateDeclaration}.
|
|
169
|
+
*/
|
|
170
|
+
declare type JssmStateDeclarationRule = {
|
|
171
|
+
key: string;
|
|
172
|
+
value: any;
|
|
173
|
+
name?: string;
|
|
174
|
+
};
|
|
175
|
+
/**
|
|
176
|
+
* The fully-condensed declaration for a single state, including its raw
|
|
177
|
+
* rule list (`declarations`) and the well-known styling fields jssm-viz
|
|
178
|
+
* understands. Returned by {@link Machine.state_declaration}.
|
|
179
|
+
*/
|
|
180
|
+
declare type JssmStateDeclaration = {
|
|
181
|
+
declarations: Array<JssmStateDeclarationRule>;
|
|
182
|
+
shape?: JssmShape;
|
|
183
|
+
color?: JssmColor;
|
|
184
|
+
corners?: JssmCorner;
|
|
185
|
+
lineStyle?: JssmLineStyle;
|
|
186
|
+
stateLabel?: string;
|
|
187
|
+
textColor?: JssmColor;
|
|
188
|
+
backgroundColor?: JssmColor;
|
|
189
|
+
borderColor?: JssmColor;
|
|
190
|
+
image?: string;
|
|
191
|
+
state: StateType$1;
|
|
192
|
+
property?: {
|
|
193
|
+
name: string;
|
|
194
|
+
value: unknown;
|
|
195
|
+
};
|
|
196
|
+
};
|
|
197
|
+
/**
|
|
198
|
+
* A loosened version of {@link JssmStateDeclaration} where every field is
|
|
199
|
+
* optional. Used as the value type for theme entries and for default
|
|
200
|
+
* state configuration where most fields will be inherited or merged.
|
|
201
|
+
*/
|
|
202
|
+
declare type JssmStateConfig = Partial<JssmStateDeclaration>;
|
|
203
|
+
declare type JssmStateStyleShape = {
|
|
204
|
+
key: 'shape';
|
|
205
|
+
value: JssmShape;
|
|
206
|
+
};
|
|
207
|
+
declare type JssmStateStyleColor = {
|
|
208
|
+
key: 'color';
|
|
209
|
+
value: JssmColor;
|
|
210
|
+
};
|
|
211
|
+
declare type JssmStateStyleTextColor = {
|
|
212
|
+
key: 'text-color';
|
|
213
|
+
value: JssmColor;
|
|
214
|
+
};
|
|
215
|
+
declare type JssmStateStyleCorners = {
|
|
216
|
+
key: 'corners';
|
|
217
|
+
value: JssmCorner;
|
|
218
|
+
};
|
|
219
|
+
declare type JssmStateStyleLineStyle = {
|
|
220
|
+
key: 'line-style';
|
|
221
|
+
value: JssmLineStyle;
|
|
222
|
+
};
|
|
223
|
+
declare type JssmStateStyleStateLabel = {
|
|
224
|
+
key: 'state-label';
|
|
225
|
+
value: string;
|
|
226
|
+
};
|
|
227
|
+
declare type JssmStateStyleBackgroundColor = {
|
|
228
|
+
key: 'background-color';
|
|
229
|
+
value: JssmColor;
|
|
230
|
+
};
|
|
231
|
+
declare type JssmStateStyleBorderColor = {
|
|
232
|
+
key: 'border-color';
|
|
233
|
+
value: JssmColor;
|
|
234
|
+
};
|
|
235
|
+
declare type JssmStateStyleImage = {
|
|
236
|
+
key: 'image';
|
|
237
|
+
value: string;
|
|
238
|
+
};
|
|
239
|
+
/**
|
|
240
|
+
* Tagged union of all individual style key/value pairs that may appear in
|
|
241
|
+
* a state's style configuration. The `key` discriminator selects which
|
|
242
|
+
* member, and the `value` is typed accordingly.
|
|
243
|
+
*/
|
|
244
|
+
declare type JssmStateStyleKey = JssmStateStyleShape | JssmStateStyleColor | JssmStateStyleTextColor | JssmStateStyleCorners | JssmStateStyleLineStyle | JssmStateStyleBackgroundColor | JssmStateStyleStateLabel | JssmStateStyleBorderColor | JssmStateStyleImage;
|
|
245
|
+
/**
|
|
246
|
+
* An ordered list of {@link JssmStateStyleKey} entries. Used by the
|
|
247
|
+
* `default_*_state_config` machine config options to provide a fallback
|
|
248
|
+
* style stack.
|
|
249
|
+
*/
|
|
250
|
+
declare type JssmStateStyleKeyList = JssmStateStyleKey[];
|
|
251
|
+
/**
|
|
252
|
+
* Full configuration object accepted by the {@link Machine} constructor and
|
|
253
|
+
* by {@link from}. Carries the transition list and the optional knobs
|
|
254
|
+
* governing layout, theming, history, start/end states, property
|
|
255
|
+
* definitions, machine metadata (author, license, version, ...) and the
|
|
256
|
+
* runtime hook surfaces (`time_source`, `timeout_source`, ...).
|
|
257
|
+
*
|
|
258
|
+
* Most users never construct one of these directly — the `sm` tagged
|
|
259
|
+
* template literal and {@link from} produce one from FSL source.
|
|
260
|
+
*
|
|
261
|
+
* @typeParam StateType - The state-name type (usually `string`).
|
|
262
|
+
* @typeParam DataType - The user-supplied data payload type (`mDT`).
|
|
263
|
+
*/
|
|
264
|
+
declare type JssmGenericConfig<StateType, DataType> = {
|
|
265
|
+
graph_layout?: JssmLayout;
|
|
266
|
+
complete?: Array<StateType>;
|
|
267
|
+
transitions: JssmTransitions<StateType, DataType>;
|
|
268
|
+
theme?: FslTheme[];
|
|
269
|
+
flow?: FslDirection;
|
|
270
|
+
name?: string;
|
|
271
|
+
data?: DataType;
|
|
272
|
+
nodes?: Array<StateType>;
|
|
273
|
+
check?: JssmStatePermitterMaybeArray<DataType>;
|
|
274
|
+
history?: number;
|
|
275
|
+
min_exits?: number;
|
|
276
|
+
max_exits?: number;
|
|
277
|
+
allow_islands?: false;
|
|
278
|
+
allow_force?: false;
|
|
279
|
+
actions?: JssmPermittedOpt;
|
|
280
|
+
simplify_bidi?: boolean;
|
|
281
|
+
allows_override?: JssmAllowsOverride;
|
|
282
|
+
config_allows_override?: JssmAllowsOverride;
|
|
283
|
+
dot_preamble?: string;
|
|
284
|
+
start_states: Array<StateType>;
|
|
285
|
+
end_states?: Array<StateType>;
|
|
286
|
+
initial_state?: StateType;
|
|
287
|
+
start_states_no_enforce?: boolean;
|
|
288
|
+
state_declaration?: Object[];
|
|
289
|
+
property_definition?: JssmPropertyDefinition[];
|
|
290
|
+
state_property?: JssmPropertyDefinition[];
|
|
291
|
+
arrange_declaration?: Array<Array<StateType>>;
|
|
292
|
+
arrange_start_declaration?: Array<Array<StateType>>;
|
|
293
|
+
arrange_end_declaration?: Array<Array<StateType>>;
|
|
294
|
+
machine_author?: string | Array<string>;
|
|
295
|
+
machine_comment?: string;
|
|
296
|
+
machine_contributor?: string | Array<string>;
|
|
297
|
+
machine_definition?: string;
|
|
298
|
+
machine_language?: string;
|
|
299
|
+
machine_license?: string;
|
|
300
|
+
machine_name?: string;
|
|
301
|
+
machine_version?: string;
|
|
302
|
+
fsl_version?: string;
|
|
303
|
+
auto_api?: boolean | string;
|
|
304
|
+
instance_name?: string | undefined;
|
|
305
|
+
default_state_config?: JssmStateStyleKeyList;
|
|
306
|
+
default_start_state_config?: JssmStateStyleKeyList;
|
|
307
|
+
default_end_state_config?: JssmStateStyleKeyList;
|
|
308
|
+
default_hooked_state_config?: JssmStateStyleKeyList;
|
|
309
|
+
default_terminal_state_config?: JssmStateStyleKeyList;
|
|
310
|
+
default_active_state_config?: JssmStateStyleKeyList;
|
|
311
|
+
rng_seed?: number | undefined;
|
|
312
|
+
time_source?: () => number;
|
|
313
|
+
timeout_source?: (Function: any, number: any) => number;
|
|
314
|
+
clear_timeout_source?: (number: any) => void;
|
|
315
|
+
};
|
|
316
|
+
/**
|
|
317
|
+
* Internal compiler intermediate: one link in a chained transition
|
|
318
|
+
* expression (an "s-expression" segment). Carries both directions of an
|
|
319
|
+
* arrow with optional per-direction action labels, probabilities, and
|
|
320
|
+
* after-times. The recursive `se` field allows the parser to chain
|
|
321
|
+
* arrows of the form `A -> B -> C`. Not intended for end-user code.
|
|
322
|
+
*
|
|
323
|
+
* @internal
|
|
324
|
+
*/
|
|
325
|
+
declare type JssmCompileSe<StateType, mDT> = {
|
|
326
|
+
to: StateType;
|
|
327
|
+
se?: JssmCompileSe<StateType, mDT>;
|
|
328
|
+
kind: JssmArrow;
|
|
329
|
+
l_action?: StateType;
|
|
330
|
+
r_action?: StateType;
|
|
331
|
+
l_probability: number;
|
|
332
|
+
r_probability: number;
|
|
333
|
+
l_after?: number;
|
|
334
|
+
r_after?: number;
|
|
335
|
+
};
|
|
336
|
+
declare type BasicHookDescription<mDT> = {
|
|
337
|
+
kind: 'hook';
|
|
338
|
+
from: string;
|
|
339
|
+
to: string;
|
|
340
|
+
handler: HookHandler<mDT>;
|
|
341
|
+
};
|
|
342
|
+
declare type HookDescriptionWithAction<mDT> = {
|
|
343
|
+
kind: 'named';
|
|
344
|
+
from: string;
|
|
345
|
+
to: string;
|
|
346
|
+
action: string;
|
|
347
|
+
handler: HookHandler<mDT>;
|
|
348
|
+
};
|
|
349
|
+
declare type StandardTransitionHook<mDT> = {
|
|
350
|
+
kind: 'standard transition';
|
|
351
|
+
handler: HookHandler<mDT>;
|
|
352
|
+
};
|
|
353
|
+
declare type MainTransitionHook<mDT> = {
|
|
354
|
+
kind: 'main transition';
|
|
355
|
+
handler: HookHandler<mDT>;
|
|
356
|
+
};
|
|
357
|
+
declare type ForcedTransitionHook<mDT> = {
|
|
358
|
+
kind: 'forced transition';
|
|
359
|
+
handler: HookHandler<mDT>;
|
|
360
|
+
};
|
|
361
|
+
declare type AnyTransitionHook<mDT> = {
|
|
362
|
+
kind: 'any transition';
|
|
363
|
+
handler: HookHandler<mDT>;
|
|
364
|
+
};
|
|
365
|
+
declare type GlobalActionHook<mDT> = {
|
|
366
|
+
kind: 'global action';
|
|
367
|
+
action: string;
|
|
368
|
+
handler: HookHandler<mDT>;
|
|
369
|
+
};
|
|
370
|
+
declare type AnyActionHook<mDT> = {
|
|
371
|
+
kind: 'any action';
|
|
372
|
+
handler: HookHandler<mDT>;
|
|
373
|
+
};
|
|
374
|
+
declare type EntryHook<mDT> = {
|
|
375
|
+
kind: 'entry';
|
|
376
|
+
to: string;
|
|
377
|
+
handler: HookHandler<mDT>;
|
|
378
|
+
};
|
|
379
|
+
declare type ExitHook<mDT> = {
|
|
380
|
+
kind: 'exit';
|
|
381
|
+
from: string;
|
|
382
|
+
handler: HookHandler<mDT>;
|
|
383
|
+
};
|
|
384
|
+
declare type AfterHook<mDT> = {
|
|
385
|
+
kind: 'after';
|
|
386
|
+
from: string;
|
|
387
|
+
handler: HookHandler<mDT>;
|
|
388
|
+
};
|
|
389
|
+
declare type PostBasicHookDescription<mDT> = {
|
|
390
|
+
kind: 'post hook';
|
|
391
|
+
from: string;
|
|
392
|
+
to: string;
|
|
393
|
+
handler: PostHookHandler<mDT>;
|
|
394
|
+
};
|
|
395
|
+
declare type PostHookDescriptionWithAction<mDT> = {
|
|
396
|
+
kind: 'post named';
|
|
397
|
+
from: string;
|
|
398
|
+
to: string;
|
|
399
|
+
action: string;
|
|
400
|
+
handler: PostHookHandler<mDT>;
|
|
401
|
+
};
|
|
402
|
+
declare type PostStandardTransitionHook<mDT> = {
|
|
403
|
+
kind: 'post standard transition';
|
|
404
|
+
handler: PostHookHandler<mDT>;
|
|
405
|
+
};
|
|
406
|
+
declare type PostMainTransitionHook<mDT> = {
|
|
407
|
+
kind: 'post main transition';
|
|
408
|
+
handler: PostHookHandler<mDT>;
|
|
409
|
+
};
|
|
410
|
+
declare type PostForcedTransitionHook<mDT> = {
|
|
411
|
+
kind: 'post forced transition';
|
|
412
|
+
handler: PostHookHandler<mDT>;
|
|
413
|
+
};
|
|
414
|
+
declare type PostAnyTransitionHook<mDT> = {
|
|
415
|
+
kind: 'post any transition';
|
|
416
|
+
handler: PostHookHandler<mDT>;
|
|
417
|
+
};
|
|
418
|
+
declare type PostGlobalActionHook<mDT> = {
|
|
419
|
+
kind: 'post global action';
|
|
420
|
+
action: string;
|
|
421
|
+
handler: PostHookHandler<mDT>;
|
|
422
|
+
};
|
|
423
|
+
declare type PostAnyActionHook<mDT> = {
|
|
424
|
+
kind: 'post any action';
|
|
425
|
+
handler: PostHookHandler<mDT>;
|
|
426
|
+
};
|
|
427
|
+
declare type PostEntryHook<mDT> = {
|
|
428
|
+
kind: 'post entry';
|
|
429
|
+
to: string;
|
|
430
|
+
handler: PostHookHandler<mDT>;
|
|
431
|
+
};
|
|
432
|
+
declare type PostExitHook<mDT> = {
|
|
433
|
+
kind: 'post exit';
|
|
434
|
+
from: string;
|
|
435
|
+
handler: PostHookHandler<mDT>;
|
|
436
|
+
};
|
|
437
|
+
declare type PreEverythingHook<mDT> = {
|
|
438
|
+
kind: 'pre everything';
|
|
439
|
+
handler: EverythingHookHandler<mDT>;
|
|
440
|
+
};
|
|
441
|
+
declare type EverythingHook<mDT> = {
|
|
442
|
+
kind: 'everything';
|
|
443
|
+
handler: EverythingHookHandler<mDT>;
|
|
444
|
+
};
|
|
445
|
+
declare type PrePostEverythingHook<mDT> = {
|
|
446
|
+
kind: 'pre post everything';
|
|
447
|
+
handler: PostEverythingHookHandler<mDT>;
|
|
448
|
+
};
|
|
449
|
+
declare type PostEverythingHook<mDT> = {
|
|
450
|
+
kind: 'post everything';
|
|
451
|
+
handler: PostEverythingHookHandler<mDT>;
|
|
452
|
+
};
|
|
453
|
+
/**
|
|
454
|
+
* Discriminated union of every kind of hook registration jssm understands,
|
|
455
|
+
* pre-transition and post-transition. The `kind` field selects the
|
|
456
|
+
* variant; remaining fields describe which transitions / states / actions
|
|
457
|
+
* the hook is bound to and supply the {@link HookHandler} or
|
|
458
|
+
* {@link PostHookHandler} to invoke.
|
|
459
|
+
*
|
|
460
|
+
* Pre-transition variants (`'hook'`, `'named'`, `'standard transition'`,
|
|
461
|
+
* `'main transition'`, `'forced transition'`, `'any transition'`,
|
|
462
|
+
* `'global action'`, `'any action'`, `'entry'`, `'exit'`, `'after'`)
|
|
463
|
+
* may return a falsy value to veto a transition. Post-transition
|
|
464
|
+
* variants (`'post *'`) cannot veto and are invoked only after a
|
|
465
|
+
* successful transition.
|
|
466
|
+
*/
|
|
467
|
+
declare type HookDescription<mDT> = BasicHookDescription<mDT> | HookDescriptionWithAction<mDT> | GlobalActionHook<mDT> | AnyActionHook<mDT> | StandardTransitionHook<mDT> | MainTransitionHook<mDT> | ForcedTransitionHook<mDT> | AnyTransitionHook<mDT> | EntryHook<mDT> | ExitHook<mDT> | AfterHook<mDT> | PostBasicHookDescription<mDT> | PostHookDescriptionWithAction<mDT> | PostGlobalActionHook<mDT> | PostAnyActionHook<mDT> | PostStandardTransitionHook<mDT> | PostMainTransitionHook<mDT> | PostForcedTransitionHook<mDT> | PostAnyTransitionHook<mDT> | PostEntryHook<mDT> | PostExitHook<mDT> | PreEverythingHook<mDT> | EverythingHook<mDT> | PrePostEverythingHook<mDT> | PostEverythingHook<mDT>;
|
|
468
|
+
/**
|
|
469
|
+
* Richer hook return value used when a hook needs to do more than just
|
|
470
|
+
* accept or veto a transition. `pass` is the required accept/veto flag
|
|
471
|
+
* (kept non-optional so that returning a stray object doesn't accidentally
|
|
472
|
+
* veto everything). The optional `state` overrides the destination state,
|
|
473
|
+
* `data` overrides the data observed by other hooks in the same chain,
|
|
474
|
+
* and `next_data` overrides the data committed after the transition.
|
|
475
|
+
*/
|
|
476
|
+
declare type HookComplexResult<mDT> = {
|
|
477
|
+
pass: boolean;
|
|
478
|
+
state?: StateType$1;
|
|
479
|
+
data?: mDT;
|
|
480
|
+
next_data?: mDT;
|
|
481
|
+
};
|
|
482
|
+
/**
|
|
483
|
+
* Return value from a {@link HookHandler}. May be a plain boolean to
|
|
484
|
+
* accept (`true`/`undefined`/`void`) or veto (`false`) the transition, or
|
|
485
|
+
* a {@link HookComplexResult} that additionally rewrites the next state
|
|
486
|
+
* and/or the next data payload.
|
|
487
|
+
*/
|
|
488
|
+
declare type HookResult<mDT> = true | false | undefined | void | HookComplexResult<mDT>;
|
|
489
|
+
/**
|
|
490
|
+
* Context object passed to every {@link HookHandler}. `data` is the
|
|
491
|
+
* data payload as it stands before the transition, and `next_data` is
|
|
492
|
+
* the payload that will be committed if the transition is accepted —
|
|
493
|
+
* handlers may inspect or mutate the latter via a
|
|
494
|
+
* {@link HookComplexResult} return value.
|
|
495
|
+
*/
|
|
496
|
+
declare type HookContext<mDT> = {
|
|
497
|
+
data: mDT;
|
|
498
|
+
next_data: mDT;
|
|
499
|
+
};
|
|
500
|
+
/**
|
|
501
|
+
* Context object passed to "everything" hooks ({@link EverythingHookHandler}
|
|
502
|
+
* and {@link PostEverythingHookHandler}). Extends the usual
|
|
503
|
+
* {@link HookContext} with `hook_name`, which identifies which specific
|
|
504
|
+
* hook fired so a single handler can route on it.
|
|
505
|
+
*/
|
|
506
|
+
declare type EverythingHookContext<mDT> = HookContext<mDT> & {
|
|
507
|
+
hook_name: string;
|
|
508
|
+
};
|
|
509
|
+
/**
|
|
510
|
+
* Signature of a pre-transition hook handler. Receives the current and
|
|
511
|
+
* proposed-next data payloads via a {@link HookContext} and returns a
|
|
512
|
+
* {@link HookResult}: a falsy result vetoes the transition, a truthy
|
|
513
|
+
* result allows it, and a {@link HookComplexResult} can additionally
|
|
514
|
+
* rewrite the next state or next data.
|
|
515
|
+
*/
|
|
516
|
+
declare type HookHandler<mDT> = (hook_context: HookContext<mDT>) => HookResult<mDT>;
|
|
517
|
+
/**
|
|
518
|
+
* Signature of a post-transition hook handler. Invoked after a successful
|
|
519
|
+
* transition has been committed; the return value is ignored (the
|
|
520
|
+
* transition cannot be undone).
|
|
521
|
+
*/
|
|
522
|
+
declare type PostHookHandler<mDT> = (hook_context: HookContext<mDT>) => void;
|
|
523
|
+
/**
|
|
524
|
+
* Signature of an "everything" pre-transition hook handler. Like
|
|
525
|
+
* {@link HookHandler} but receives an {@link EverythingHookContext} so the
|
|
526
|
+
* handler can dispatch on `hook_name`.
|
|
527
|
+
*/
|
|
528
|
+
declare type EverythingHookHandler<mDT> = (hook_context: EverythingHookContext<mDT>) => HookResult<mDT>;
|
|
529
|
+
/**
|
|
530
|
+
* Signature of an "everything" post-transition hook handler. Like
|
|
531
|
+
* {@link PostHookHandler} but receives an {@link EverythingHookContext}.
|
|
532
|
+
* The return value is ignored.
|
|
533
|
+
*/
|
|
534
|
+
declare type PostEverythingHookHandler<mDT> = (hook_context: EverythingHookContext<mDT>) => void;
|
|
535
|
+
/**
|
|
536
|
+
* Bounded history of recently-visited states paired with the data payload
|
|
537
|
+
* observed in each. Backed by `circular_buffer_js`, so the oldest entry
|
|
538
|
+
* is dropped silently once the configured capacity is exceeded.
|
|
539
|
+
*/
|
|
540
|
+
declare type JssmHistory<mDT> = circular_buffer<[StateType$1, mDT]>;
|
|
541
|
+
/**
|
|
542
|
+
* Pluggable random-number-generator function shape. Must return a value
|
|
543
|
+
* in `[0, 1)` exactly as `Math.random` does. Supplied via the
|
|
544
|
+
* `rng_seed`-aware machine configuration so that stochastic models can be
|
|
545
|
+
* made reproducible.
|
|
546
|
+
*/
|
|
547
|
+
declare type JssmRng = () => number;
|
|
548
|
+
|
|
549
|
+
declare const version: string;
|
|
550
|
+
declare const build_time: number;
|
|
551
|
+
|
|
552
|
+
declare type StateType = string;
|
|
553
|
+
|
|
554
|
+
/*******
|
|
555
|
+
*
|
|
556
|
+
* Core finite state machine class. Holds the full graph of states and
|
|
557
|
+
* transitions, the current state, hooks, data, properties, and all runtime
|
|
558
|
+
* behavior. Typically created via the {@link sm} tagged template literal
|
|
559
|
+
* rather than constructed directly.
|
|
560
|
+
*
|
|
561
|
+
* ```typescript
|
|
562
|
+
* import { sm } from 'jssm';
|
|
563
|
+
*
|
|
564
|
+
* const light = sm`Red 'next' => Green 'next' => Yellow 'next' => Red;`;
|
|
565
|
+
* light.state(); // 'Red'
|
|
566
|
+
* light.action('next'); // true
|
|
567
|
+
* light.state(); // 'Green'
|
|
568
|
+
* ```
|
|
569
|
+
*
|
|
570
|
+
* @typeparam mDT The machine data type — the type of the value stored in
|
|
571
|
+
* `.data()`. Defaults to `undefined` when no data is used.
|
|
572
|
+
*
|
|
573
|
+
*/
|
|
574
|
+
declare class Machine<mDT> {
|
|
575
|
+
_state: StateType;
|
|
576
|
+
_states: Map<StateType, JssmGenericState>;
|
|
577
|
+
_edges: Array<JssmTransition<StateType, mDT>>;
|
|
578
|
+
_edge_map: Map<StateType, Map<StateType, number>>;
|
|
579
|
+
_named_transitions: Map<StateType, number>;
|
|
580
|
+
_actions: Map<StateType, Map<StateType, number>>;
|
|
581
|
+
_reverse_actions: Map<StateType, Map<StateType, number>>;
|
|
582
|
+
_reverse_action_targets: Map<StateType, Map<StateType, number>>;
|
|
583
|
+
_start_states: Set<StateType>;
|
|
584
|
+
_end_states: Set<StateType>;
|
|
585
|
+
_machine_author?: Array<string>;
|
|
586
|
+
_machine_comment?: string;
|
|
587
|
+
_machine_contributor?: Array<string>;
|
|
588
|
+
_machine_definition?: string;
|
|
589
|
+
_machine_language?: string;
|
|
590
|
+
_machine_license?: string;
|
|
591
|
+
_machine_name?: string;
|
|
592
|
+
_machine_version?: string;
|
|
593
|
+
_fsl_version?: string;
|
|
594
|
+
_raw_state_declaration?: Array<Object>;
|
|
595
|
+
_state_declarations: Map<StateType, JssmStateDeclaration>;
|
|
596
|
+
_data?: mDT;
|
|
597
|
+
_instance_name: string;
|
|
598
|
+
_rng_seed: number;
|
|
599
|
+
_rng: JssmRng;
|
|
600
|
+
_graph_layout: JssmLayout;
|
|
601
|
+
_dot_preamble: string;
|
|
602
|
+
_arrange_declaration: Array<Array<StateType>>;
|
|
603
|
+
_arrange_start_declaration: Array<Array<StateType>>;
|
|
604
|
+
_arrange_end_declaration: Array<Array<StateType>>;
|
|
605
|
+
_themes: FslTheme[];
|
|
606
|
+
_flow: FslDirection;
|
|
607
|
+
_has_hooks: boolean;
|
|
608
|
+
_has_basic_hooks: boolean;
|
|
609
|
+
_has_named_hooks: boolean;
|
|
610
|
+
_has_entry_hooks: boolean;
|
|
611
|
+
_has_exit_hooks: boolean;
|
|
612
|
+
_has_after_hooks: boolean;
|
|
613
|
+
_has_global_action_hooks: boolean;
|
|
614
|
+
_has_transition_hooks: boolean;
|
|
615
|
+
_has_forced_transitions: boolean;
|
|
616
|
+
_hooks: Map<string, HookHandler<mDT>>;
|
|
617
|
+
_named_hooks: Map<string, HookHandler<mDT>>;
|
|
618
|
+
_entry_hooks: Map<string, HookHandler<mDT>>;
|
|
619
|
+
_exit_hooks: Map<string, HookHandler<mDT>>;
|
|
620
|
+
_after_hooks: Map<string, HookHandler<mDT>>;
|
|
621
|
+
_global_action_hooks: Map<string, HookHandler<mDT>>;
|
|
622
|
+
_any_action_hook: HookHandler<mDT> | undefined;
|
|
623
|
+
_standard_transition_hook: HookHandler<mDT> | undefined;
|
|
624
|
+
_main_transition_hook: HookHandler<mDT> | undefined;
|
|
625
|
+
_forced_transition_hook: HookHandler<mDT> | undefined;
|
|
626
|
+
_any_transition_hook: HookHandler<mDT> | undefined;
|
|
627
|
+
_has_post_hooks: boolean;
|
|
628
|
+
_has_post_basic_hooks: boolean;
|
|
629
|
+
_has_post_named_hooks: boolean;
|
|
630
|
+
_has_post_entry_hooks: boolean;
|
|
631
|
+
_has_post_exit_hooks: boolean;
|
|
632
|
+
_has_post_global_action_hooks: boolean;
|
|
633
|
+
_has_post_transition_hooks: boolean;
|
|
634
|
+
_code_allows_override: JssmAllowsOverride;
|
|
635
|
+
_config_allows_override: JssmAllowsOverride;
|
|
636
|
+
_post_hooks: Map<string, HookHandler<mDT>>;
|
|
637
|
+
_post_named_hooks: Map<string, HookHandler<mDT>>;
|
|
638
|
+
_post_entry_hooks: Map<string, HookHandler<mDT>>;
|
|
639
|
+
_post_exit_hooks: Map<string, HookHandler<mDT>>;
|
|
640
|
+
_post_global_action_hooks: Map<string, HookHandler<mDT>>;
|
|
641
|
+
_post_any_action_hook: HookHandler<mDT> | undefined;
|
|
642
|
+
_post_standard_transition_hook: HookHandler<mDT> | undefined;
|
|
643
|
+
_post_main_transition_hook: HookHandler<mDT> | undefined;
|
|
644
|
+
_post_forced_transition_hook: HookHandler<mDT> | undefined;
|
|
645
|
+
_post_any_transition_hook: HookHandler<mDT> | undefined;
|
|
646
|
+
_pre_everything_hook: EverythingHookHandler<mDT> | undefined;
|
|
647
|
+
_everything_hook: EverythingHookHandler<mDT> | undefined;
|
|
648
|
+
_pre_post_everything_hook: PostEverythingHookHandler<mDT> | undefined;
|
|
649
|
+
_post_everything_hook: PostEverythingHookHandler<mDT> | undefined;
|
|
650
|
+
_property_keys: Set<string>;
|
|
651
|
+
_default_properties: Map<string, any>;
|
|
652
|
+
_state_properties: Map<string, any>;
|
|
653
|
+
_required_properties: Set<string>;
|
|
654
|
+
_history: JssmHistory<mDT>;
|
|
655
|
+
_history_length: number;
|
|
656
|
+
_state_style: JssmStateConfig;
|
|
657
|
+
_active_state_style: JssmStateConfig;
|
|
658
|
+
_hooked_state_style: JssmStateConfig;
|
|
659
|
+
_terminal_state_style: JssmStateConfig;
|
|
660
|
+
_start_state_style: JssmStateConfig;
|
|
661
|
+
_end_state_style: JssmStateConfig;
|
|
662
|
+
_state_labels: Map<string, string>;
|
|
663
|
+
_time_source: () => number;
|
|
664
|
+
_create_started: number;
|
|
665
|
+
_created: number;
|
|
666
|
+
_after_mapping: Map<string, [string, number]>;
|
|
667
|
+
_timeout_source: (Function: any, number: any) => number;
|
|
668
|
+
_clear_timeout_source: (h: any) => void;
|
|
669
|
+
_timeout_handle: number | undefined;
|
|
670
|
+
_timeout_target: string | undefined;
|
|
671
|
+
_timeout_target_time: number | undefined;
|
|
672
|
+
constructor({ start_states, end_states, initial_state, start_states_no_enforce, complete, transitions, machine_author, machine_comment, machine_contributor, machine_definition, machine_language, machine_license, machine_name, machine_version, state_declaration, property_definition, state_property, fsl_version, dot_preamble, arrange_declaration, arrange_start_declaration, arrange_end_declaration, theme, flow, graph_layout, instance_name, history, data, default_state_config, default_active_state_config, default_hooked_state_config, default_terminal_state_config, default_start_state_config, default_end_state_config, allows_override, config_allows_override, rng_seed, time_source, timeout_source, clear_timeout_source }: JssmGenericConfig<StateType, mDT>);
|
|
673
|
+
/********
|
|
674
|
+
*
|
|
675
|
+
* Internal method for fabricating states. Not meant for external use.
|
|
676
|
+
*
|
|
677
|
+
* @internal
|
|
678
|
+
*
|
|
679
|
+
*/
|
|
680
|
+
_new_state(state_config: JssmGenericState): StateType;
|
|
681
|
+
/*********
|
|
682
|
+
*
|
|
683
|
+
* Get the current state of a machine.
|
|
684
|
+
*
|
|
685
|
+
* ```typescript
|
|
686
|
+
* import * as jssm from 'jssm';
|
|
687
|
+
*
|
|
688
|
+
* const lswitch = jssm.from('on <=> off;');
|
|
689
|
+
* console.log( lswitch.state() ); // 'on'
|
|
690
|
+
*
|
|
691
|
+
* lswitch.transition('off');
|
|
692
|
+
* console.log( lswitch.state() ); // 'off'
|
|
693
|
+
* ```
|
|
694
|
+
*
|
|
695
|
+
* @typeparam mDT The type of the machine data member; usually omitted
|
|
696
|
+
*
|
|
697
|
+
* @returns The current state name.
|
|
698
|
+
*
|
|
699
|
+
*/
|
|
700
|
+
state(): StateType;
|
|
701
|
+
/*********
|
|
702
|
+
*
|
|
703
|
+
* Get the label for a given state, if any; return `undefined` otherwise.
|
|
704
|
+
*
|
|
705
|
+
* ```typescript
|
|
706
|
+
* import * as jssm from 'jssm';
|
|
707
|
+
*
|
|
708
|
+
* const lswitch = jssm.from('a -> b; state a: { label: "Foo!"; };');
|
|
709
|
+
* console.log( lswitch.label_for('a') ); // 'Foo!'
|
|
710
|
+
* console.log( lswitch.label_for('b') ); // undefined
|
|
711
|
+
* ```
|
|
712
|
+
*
|
|
713
|
+
* See also {@link display_text}.
|
|
714
|
+
*
|
|
715
|
+
* @typeparam mDT The type of the machine data member; usually omitted
|
|
716
|
+
*
|
|
717
|
+
* @param state The state to get the label for.
|
|
718
|
+
*
|
|
719
|
+
* @returns The label string, or `undefined` if no label is set.
|
|
720
|
+
*
|
|
721
|
+
*/
|
|
722
|
+
label_for(state: StateType): string;
|
|
723
|
+
/*********
|
|
724
|
+
*
|
|
725
|
+
* Get whatever the node should show as text.
|
|
726
|
+
*
|
|
727
|
+
* Currently, this means to get the label for a given state, if any;
|
|
728
|
+
* otherwise to return the node's name. However, this definition is expected
|
|
729
|
+
* to grow with time, and it is currently considered ill-advised to manually
|
|
730
|
+
* parse this text.
|
|
731
|
+
*
|
|
732
|
+
* See also {@link label_for}.
|
|
733
|
+
*
|
|
734
|
+
* ```typescript
|
|
735
|
+
* import * as jssm from 'jssm';
|
|
736
|
+
*
|
|
737
|
+
* const lswitch = jssm.from('a -> b; state a: { label: "Foo!"; };');
|
|
738
|
+
* console.log( lswitch.display_text('a') ); // 'Foo!'
|
|
739
|
+
* console.log( lswitch.display_text('b') ); // 'b'
|
|
740
|
+
* ```
|
|
741
|
+
*
|
|
742
|
+
* @typeparam mDT The type of the machine data member; usually omitted
|
|
743
|
+
*
|
|
744
|
+
* @param state The state to get display text for.
|
|
745
|
+
*
|
|
746
|
+
* @returns The label if one exists, otherwise the state's name.
|
|
747
|
+
*
|
|
748
|
+
*/
|
|
749
|
+
display_text(state: StateType): string;
|
|
750
|
+
/*********
|
|
751
|
+
*
|
|
752
|
+
* Get the current data of a machine.
|
|
753
|
+
*
|
|
754
|
+
* ```typescript
|
|
755
|
+
* import * as jssm from 'jssm';
|
|
756
|
+
*
|
|
757
|
+
* const lswitch = jssm.from('on <=> off;', {data: 1});
|
|
758
|
+
* console.log( lswitch.data() ); // 1
|
|
759
|
+
* ```
|
|
760
|
+
*
|
|
761
|
+
* @typeparam mDT The type of the machine data member; usually omitted
|
|
762
|
+
*
|
|
763
|
+
* @returns A deep clone of the machine's current data value.
|
|
764
|
+
*
|
|
765
|
+
*/
|
|
766
|
+
data(): mDT;
|
|
767
|
+
/*********
|
|
768
|
+
*
|
|
769
|
+
* Get the current value of a given property name. Checks the current
|
|
770
|
+
* state's properties first, then falls back to the global default.
|
|
771
|
+
* Returns `undefined` if neither exists. For a throwing variant, see
|
|
772
|
+
* {@link strict_prop}.
|
|
773
|
+
*
|
|
774
|
+
* ```typescript
|
|
775
|
+
* const m = sm`property color default "grey"; a -> b;
|
|
776
|
+
* state b: { property color "blue"; };`;
|
|
777
|
+
*
|
|
778
|
+
* m.prop('color'); // 'grey' (default, because state is 'a')
|
|
779
|
+
* m.go('b');
|
|
780
|
+
* m.prop('color'); // 'blue' (state 'b' overrides the default)
|
|
781
|
+
* m.prop('size'); // undefined (no such property)
|
|
782
|
+
* ```
|
|
783
|
+
*
|
|
784
|
+
* @param name The relevant property name to look up.
|
|
785
|
+
*
|
|
786
|
+
* @returns The value behind the prop name, or `undefined` if not defined.
|
|
787
|
+
*
|
|
788
|
+
*/
|
|
789
|
+
prop(name: string): any;
|
|
790
|
+
/*********
|
|
791
|
+
*
|
|
792
|
+
* Get the current value of a given property name. If missing on the state
|
|
793
|
+
* and without a global default, throws a {@link JssmError}, unlike
|
|
794
|
+
* {@link prop}, which would return `undefined` instead.
|
|
795
|
+
*
|
|
796
|
+
* ```typescript
|
|
797
|
+
* const m = sm`property color default "grey"; a -> b;`;
|
|
798
|
+
*
|
|
799
|
+
* m.strict_prop('color'); // 'grey'
|
|
800
|
+
* m.strict_prop('size'); // throws JssmError
|
|
801
|
+
* ```
|
|
802
|
+
*
|
|
803
|
+
* @param name The relevant property name to look up.
|
|
804
|
+
*
|
|
805
|
+
* @returns The value behind the prop name.
|
|
806
|
+
*
|
|
807
|
+
* @throws {JssmError} If the property is not defined on the current state
|
|
808
|
+
* and has no default.
|
|
809
|
+
*
|
|
810
|
+
*/
|
|
811
|
+
strict_prop(name: string): any;
|
|
812
|
+
/*********
|
|
813
|
+
*
|
|
814
|
+
* Get the current value of every prop, as an object. If no current definition
|
|
815
|
+
* exists for a prop — that is, if the prop was defined without a default and
|
|
816
|
+
* the current state also doesn't define the prop — then that prop will be listed
|
|
817
|
+
* in the returned object with a value of `undefined`.
|
|
818
|
+
*
|
|
819
|
+
* ```typescript
|
|
820
|
+
* const traffic_light = sm`
|
|
821
|
+
*
|
|
822
|
+
* property can_go default true;
|
|
823
|
+
* property hesitate default true;
|
|
824
|
+
* property stop_first default false;
|
|
825
|
+
*
|
|
826
|
+
* Off -> Red => Green => Yellow => Red;
|
|
827
|
+
* [Red Yellow Green] ~> [Off FlashingRed];
|
|
828
|
+
* FlashingRed -> Red;
|
|
829
|
+
*
|
|
830
|
+
* state Red: { property stop_first true; property can_go false; };
|
|
831
|
+
* state Off: { property stop_first true; };
|
|
832
|
+
* state FlashingRed: { property stop_first true; };
|
|
833
|
+
* state Green: { property hesitate false; };
|
|
834
|
+
*
|
|
835
|
+
* `;
|
|
836
|
+
*
|
|
837
|
+
* traffic_light.state(); // Off
|
|
838
|
+
* traffic_light.props(); // { can_go: true, hesitate: true, stop_first: true; }
|
|
839
|
+
*
|
|
840
|
+
* traffic_light.go('Red');
|
|
841
|
+
* traffic_light.props(); // { can_go: false, hesitate: true, stop_first: true; }
|
|
842
|
+
*
|
|
843
|
+
* traffic_light.go('Green');
|
|
844
|
+
* traffic_light.props(); // { can_go: true, hesitate: false, stop_first: false; }
|
|
845
|
+
* ```
|
|
846
|
+
*
|
|
847
|
+
* @returns An object mapping every known property name to its current value
|
|
848
|
+
* (or `undefined` if the property has no default and the current state
|
|
849
|
+
* doesn't define it).
|
|
850
|
+
*
|
|
851
|
+
*/
|
|
852
|
+
props(): object;
|
|
853
|
+
/*********
|
|
854
|
+
*
|
|
855
|
+
* Check whether a given string is a known property's name.
|
|
856
|
+
*
|
|
857
|
+
* ```typescript
|
|
858
|
+
* const example = sm`property foo default 1; a->b;`;
|
|
859
|
+
*
|
|
860
|
+
* example.known_prop('foo'); // true
|
|
861
|
+
* example.known_prop('bar'); // false
|
|
862
|
+
* ```
|
|
863
|
+
*
|
|
864
|
+
* @param prop_name The relevant property name to look up
|
|
865
|
+
*
|
|
866
|
+
*/
|
|
867
|
+
known_prop(prop_name: string): boolean;
|
|
868
|
+
/*********
|
|
869
|
+
*
|
|
870
|
+
* List all known property names. If you'd also like values, use
|
|
871
|
+
* {@link props} instead. The order of the properties is not defined, and
|
|
872
|
+
* the properties generally will not be sorted.
|
|
873
|
+
*
|
|
874
|
+
* ```typescript
|
|
875
|
+
* const m = sm`property color default "grey"; property size default 1; a -> b;`;
|
|
876
|
+
*
|
|
877
|
+
* m.known_props(); // ['color', 'size']
|
|
878
|
+
* ```
|
|
879
|
+
*
|
|
880
|
+
* @returns An array of all property name strings defined on this machine.
|
|
881
|
+
*
|
|
882
|
+
*/
|
|
883
|
+
known_props(): string[];
|
|
884
|
+
/********
|
|
885
|
+
*
|
|
886
|
+
* Check whether a given state is a valid start state (either because it was
|
|
887
|
+
* explicitly named as such, or because it was the first mentioned state.)
|
|
888
|
+
*
|
|
889
|
+
* ```typescript
|
|
890
|
+
* import { sm, is_start_state } from 'jssm';
|
|
891
|
+
*
|
|
892
|
+
* const example = sm`a -> b;`;
|
|
893
|
+
*
|
|
894
|
+
* console.log( final_test.is_start_state('a') ); // true
|
|
895
|
+
* console.log( final_test.is_start_state('b') ); // false
|
|
896
|
+
*
|
|
897
|
+
* const example = sm`start_states: [a b]; a -> b;`;
|
|
898
|
+
*
|
|
899
|
+
* console.log( final_test.is_start_state('a') ); // true
|
|
900
|
+
* console.log( final_test.is_start_state('b') ); // true
|
|
901
|
+
* ```
|
|
902
|
+
*
|
|
903
|
+
* @typeparam mDT The type of the machine data member; usually omitted
|
|
904
|
+
*
|
|
905
|
+
* @param whichState The name of the state to check
|
|
906
|
+
*
|
|
907
|
+
*/
|
|
908
|
+
is_start_state(whichState: StateType): boolean;
|
|
909
|
+
/********
|
|
910
|
+
*
|
|
911
|
+
* Check whether a given state is a valid start state (either because it was
|
|
912
|
+
* explicitly named as such, or because it was the first mentioned state.)
|
|
913
|
+
*
|
|
914
|
+
* ```typescript
|
|
915
|
+
* import { sm, is_end_state } from 'jssm';
|
|
916
|
+
*
|
|
917
|
+
* const example = sm`a -> b;`;
|
|
918
|
+
*
|
|
919
|
+
* console.log( final_test.is_start_state('a') ); // false
|
|
920
|
+
* console.log( final_test.is_start_state('b') ); // true
|
|
921
|
+
*
|
|
922
|
+
* const example = sm`end_states: [a b]; a -> b;`;
|
|
923
|
+
*
|
|
924
|
+
* console.log( final_test.is_start_state('a') ); // true
|
|
925
|
+
* console.log( final_test.is_start_state('b') ); // true
|
|
926
|
+
* ```
|
|
927
|
+
*
|
|
928
|
+
* @typeparam mDT The type of the machine data member; usually omitted
|
|
929
|
+
*
|
|
930
|
+
* @param whichState The name of the state to check
|
|
931
|
+
*
|
|
932
|
+
*/
|
|
933
|
+
is_end_state(whichState: StateType): boolean;
|
|
934
|
+
/********
|
|
935
|
+
*
|
|
936
|
+
* Check whether a given state is final (either has no exits or is marked
|
|
937
|
+
* `complete`.)
|
|
938
|
+
*
|
|
939
|
+
* ```typescript
|
|
940
|
+
* import { sm, state_is_final } from 'jssm';
|
|
941
|
+
*
|
|
942
|
+
* const final_test = sm`first -> second;`;
|
|
943
|
+
*
|
|
944
|
+
* console.log( final_test.state_is_final('first') ); // false
|
|
945
|
+
* console.log( final_test.state_is_final('second') ); // true
|
|
946
|
+
* ```
|
|
947
|
+
*
|
|
948
|
+
* @typeparam mDT The type of the machine data member; usually omitted
|
|
949
|
+
*
|
|
950
|
+
* @param whichState The name of the state to check for finality
|
|
951
|
+
*
|
|
952
|
+
*/
|
|
953
|
+
state_is_final(whichState: StateType): boolean;
|
|
954
|
+
/********
|
|
955
|
+
*
|
|
956
|
+
* Check whether the current state is final (either has no exits or is marked
|
|
957
|
+
* `complete`.)
|
|
958
|
+
*
|
|
959
|
+
* ```typescript
|
|
960
|
+
* import { sm, is_final } from 'jssm';
|
|
961
|
+
*
|
|
962
|
+
* const final_test = sm`first -> second;`;
|
|
963
|
+
*
|
|
964
|
+
* console.log( final_test.is_final() ); // false
|
|
965
|
+
* state.transition('second');
|
|
966
|
+
* console.log( final_test.is_final() ); // true
|
|
967
|
+
* ```
|
|
968
|
+
*
|
|
969
|
+
*/
|
|
970
|
+
is_final(): boolean;
|
|
971
|
+
/********
|
|
972
|
+
*
|
|
973
|
+
* Serialize the current machine, including all defining state but not the
|
|
974
|
+
* machine string, to a structure. This means you will need the machine
|
|
975
|
+
* string to recreate (to not waste repeated space;) if you want the machine
|
|
976
|
+
* string embedded, call {@link serialize_with_string} instead.
|
|
977
|
+
*
|
|
978
|
+
* @typeparam mDT The type of the machine data member; usually omitted
|
|
979
|
+
*
|
|
980
|
+
* @param comment An optional comment string to embed in the serialized
|
|
981
|
+
* output for identification or debugging.
|
|
982
|
+
*
|
|
983
|
+
* @returns A {@link JssmSerialization} object containing the machine's
|
|
984
|
+
* current state, data, and timestamp.
|
|
985
|
+
*
|
|
986
|
+
*/
|
|
987
|
+
serialize(comment?: string | undefined): JssmSerialization<mDT>;
|
|
988
|
+
/** Get the graph layout direction (e.g. `'LR'`, `'TB'`). Set via the
|
|
989
|
+
* FSL `graph_layout` directive.
|
|
990
|
+
* @returns The layout string, or the default if not set.
|
|
991
|
+
*/
|
|
992
|
+
graph_layout(): string;
|
|
993
|
+
/** Get the Graphviz DOT preamble string, injected before the graph body
|
|
994
|
+
* during visualization. Set via the FSL `dot_preamble` directive.
|
|
995
|
+
* @returns The preamble string.
|
|
996
|
+
*/
|
|
997
|
+
dot_preamble(): string;
|
|
998
|
+
/** Get the machine's author list. Set via the FSL `machine_author` directive.
|
|
999
|
+
* @returns An array of author name strings.
|
|
1000
|
+
*/
|
|
1001
|
+
machine_author(): Array<string>;
|
|
1002
|
+
/** Get the machine's comment string. Set via the FSL `machine_comment` directive.
|
|
1003
|
+
* @returns The comment string.
|
|
1004
|
+
*/
|
|
1005
|
+
machine_comment(): string;
|
|
1006
|
+
/** Get the machine's contributor list. Set via the FSL `machine_contributor` directive.
|
|
1007
|
+
* @returns An array of contributor name strings.
|
|
1008
|
+
*/
|
|
1009
|
+
machine_contributor(): Array<string>;
|
|
1010
|
+
/** Get the machine's definition string. Set via the FSL `machine_definition` directive.
|
|
1011
|
+
* @returns The definition string.
|
|
1012
|
+
*/
|
|
1013
|
+
machine_definition(): string;
|
|
1014
|
+
/** Get the machine's language (ISO 639-1). Set via the FSL `machine_language` directive.
|
|
1015
|
+
* @returns The language code string.
|
|
1016
|
+
*/
|
|
1017
|
+
machine_language(): string;
|
|
1018
|
+
/** Get the machine's license string. Set via the FSL `machine_license` directive.
|
|
1019
|
+
* @returns The license string.
|
|
1020
|
+
*/
|
|
1021
|
+
machine_license(): string;
|
|
1022
|
+
/** Get the machine's name. Set via the FSL `machine_name` directive.
|
|
1023
|
+
* @returns The machine name string.
|
|
1024
|
+
*/
|
|
1025
|
+
machine_name(): string;
|
|
1026
|
+
/** Get the machine's version string. Set via the FSL `machine_version` directive.
|
|
1027
|
+
* @returns The version string.
|
|
1028
|
+
*/
|
|
1029
|
+
machine_version(): string;
|
|
1030
|
+
/** Get the raw state declaration objects as parsed from the FSL source.
|
|
1031
|
+
* @returns An array of raw state declaration objects.
|
|
1032
|
+
*/
|
|
1033
|
+
raw_state_declarations(): Array<Object>;
|
|
1034
|
+
/** Get the processed state declaration for a specific state.
|
|
1035
|
+
* @param which - The state to look up.
|
|
1036
|
+
* @returns The {@link JssmStateDeclaration} for the given state.
|
|
1037
|
+
*/
|
|
1038
|
+
state_declaration(which: StateType): JssmStateDeclaration;
|
|
1039
|
+
/** Get all processed state declarations as a Map.
|
|
1040
|
+
* @returns A `Map` from state name to {@link JssmStateDeclaration}.
|
|
1041
|
+
*/
|
|
1042
|
+
state_declarations(): Map<StateType, JssmStateDeclaration>;
|
|
1043
|
+
/** Get the FSL language version this machine was compiled under.
|
|
1044
|
+
* @returns The FSL version string.
|
|
1045
|
+
*/
|
|
1046
|
+
fsl_version(): string;
|
|
1047
|
+
/** Get the complete internal state of the machine as a serializable
|
|
1048
|
+
* structure. Includes actions, edges, edge map, named transitions,
|
|
1049
|
+
* reverse actions, current state, and states map.
|
|
1050
|
+
* @returns A {@link JssmMachineInternalState} snapshot.
|
|
1051
|
+
*/
|
|
1052
|
+
machine_state(): JssmMachineInternalState<mDT>;
|
|
1053
|
+
/*********
|
|
1054
|
+
*
|
|
1055
|
+
* List all the states known by the machine. Please note that the order of
|
|
1056
|
+
* these states is not guaranteed.
|
|
1057
|
+
*
|
|
1058
|
+
* ```typescript
|
|
1059
|
+
* import * as jssm from 'jssm';
|
|
1060
|
+
*
|
|
1061
|
+
* const lswitch = jssm.from('on <=> off;');
|
|
1062
|
+
* console.log( lswitch.states() ); // ['on', 'off']
|
|
1063
|
+
* ```
|
|
1064
|
+
*
|
|
1065
|
+
* @typeparam mDT The type of the machine data member; usually omitted
|
|
1066
|
+
*
|
|
1067
|
+
* @returns An array of all state names in the machine.
|
|
1068
|
+
*
|
|
1069
|
+
*/
|
|
1070
|
+
states(): Array<StateType>;
|
|
1071
|
+
/** Get the internal state descriptor for a given state name.
|
|
1072
|
+
* @param whichState - The state to look up.
|
|
1073
|
+
* @returns The {@link JssmGenericState} descriptor.
|
|
1074
|
+
* @throws {JssmError} If the state does not exist.
|
|
1075
|
+
*/
|
|
1076
|
+
state_for(whichState: StateType): JssmGenericState;
|
|
1077
|
+
/*********
|
|
1078
|
+
*
|
|
1079
|
+
* Check whether the machine knows a given state.
|
|
1080
|
+
*
|
|
1081
|
+
* ```typescript
|
|
1082
|
+
* import * as jssm from 'jssm';
|
|
1083
|
+
*
|
|
1084
|
+
* const lswitch = jssm.from('on <=> off;');
|
|
1085
|
+
*
|
|
1086
|
+
* console.log( lswitch.has_state('off') ); // true
|
|
1087
|
+
* console.log( lswitch.has_state('dance') ); // false
|
|
1088
|
+
* ```
|
|
1089
|
+
*
|
|
1090
|
+
* @typeparam mDT The type of the machine data member; usually omitted
|
|
1091
|
+
*
|
|
1092
|
+
* @param whichState The state to be checked for existence.
|
|
1093
|
+
*
|
|
1094
|
+
* @returns `true` if the state exists, `false` otherwise.
|
|
1095
|
+
*
|
|
1096
|
+
*/
|
|
1097
|
+
has_state(whichState: StateType): boolean;
|
|
1098
|
+
/*********
|
|
1099
|
+
*
|
|
1100
|
+
* Lists all edges of a machine.
|
|
1101
|
+
*
|
|
1102
|
+
* ```typescript
|
|
1103
|
+
* import { sm } from 'jssm';
|
|
1104
|
+
*
|
|
1105
|
+
* const lswitch = sm`on 'toggle' <=> 'toggle' off;`;
|
|
1106
|
+
*
|
|
1107
|
+
* lswitch.list_edges();
|
|
1108
|
+
* [
|
|
1109
|
+
* {
|
|
1110
|
+
* from: 'on',
|
|
1111
|
+
* to: 'off',
|
|
1112
|
+
* kind: 'main',
|
|
1113
|
+
* forced_only: false,
|
|
1114
|
+
* main_path: true,
|
|
1115
|
+
* action: 'toggle'
|
|
1116
|
+
* },
|
|
1117
|
+
* {
|
|
1118
|
+
* from: 'off',
|
|
1119
|
+
* to: 'on',
|
|
1120
|
+
* kind: 'main',
|
|
1121
|
+
* forced_only: false,
|
|
1122
|
+
* main_path: true,
|
|
1123
|
+
* action: 'toggle'
|
|
1124
|
+
* }
|
|
1125
|
+
* ]
|
|
1126
|
+
* ```
|
|
1127
|
+
*
|
|
1128
|
+
* @typeparam mDT The type of the machine data member; usually omitted
|
|
1129
|
+
*
|
|
1130
|
+
* @returns An array of all {@link JssmTransition} edge objects.
|
|
1131
|
+
*
|
|
1132
|
+
*/
|
|
1133
|
+
list_edges(): Array<JssmTransition<StateType, mDT>>;
|
|
1134
|
+
/** Get the map of named transitions (transitions with explicit names).
|
|
1135
|
+
* @returns A `Map` from transition name to edge index.
|
|
1136
|
+
*/
|
|
1137
|
+
list_named_transitions(): Map<StateType, number>;
|
|
1138
|
+
/** List all distinct action names defined anywhere in the machine.
|
|
1139
|
+
* @returns An array of action name strings.
|
|
1140
|
+
*/
|
|
1141
|
+
list_actions(): Array<StateType>;
|
|
1142
|
+
/** Whether any actions are defined on this machine.
|
|
1143
|
+
* @returns `true` if the machine has at least one action.
|
|
1144
|
+
*/
|
|
1145
|
+
get uses_actions(): boolean;
|
|
1146
|
+
/** Whether any forced (`~>`) transitions exist in this machine.
|
|
1147
|
+
* @returns `true` if at least one forced transition is defined.
|
|
1148
|
+
*/
|
|
1149
|
+
get uses_forced_transitions(): boolean;
|
|
1150
|
+
/*********
|
|
1151
|
+
*
|
|
1152
|
+
* Check if the code that built the machine allows overriding state and data.
|
|
1153
|
+
*
|
|
1154
|
+
* @returns The override permission from the FSL source code.
|
|
1155
|
+
*
|
|
1156
|
+
*/
|
|
1157
|
+
get code_allows_override(): JssmAllowsOverride;
|
|
1158
|
+
/*********
|
|
1159
|
+
*
|
|
1160
|
+
* Check if the machine config allows overriding state and data.
|
|
1161
|
+
*
|
|
1162
|
+
* @returns The override permission from the runtime config.
|
|
1163
|
+
*
|
|
1164
|
+
*/
|
|
1165
|
+
get config_allows_override(): JssmAllowsOverride;
|
|
1166
|
+
/*********
|
|
1167
|
+
*
|
|
1168
|
+
* Check if a machine allows overriding state and data. Resolves the
|
|
1169
|
+
* combined effect of code and config permissions — config may not be
|
|
1170
|
+
* less strict than code.
|
|
1171
|
+
*
|
|
1172
|
+
* @returns The effective override permission.
|
|
1173
|
+
*
|
|
1174
|
+
*/
|
|
1175
|
+
get allows_override(): JssmAllowsOverride;
|
|
1176
|
+
/** List all available theme names.
|
|
1177
|
+
* @returns An array of theme name strings.
|
|
1178
|
+
*/
|
|
1179
|
+
all_themes(): FslTheme[];
|
|
1180
|
+
/** Get the active theme(s) for this machine. Always stored as an array
|
|
1181
|
+
* internally; the union return type exists for setter compatibility.
|
|
1182
|
+
* @returns The current theme or array of themes.
|
|
1183
|
+
*/
|
|
1184
|
+
get themes(): FslTheme | FslTheme[];
|
|
1185
|
+
/** Set the active theme(s). Accepts a single theme name or an array.
|
|
1186
|
+
* @param to - A theme name or array of theme names to apply.
|
|
1187
|
+
*/
|
|
1188
|
+
set themes(to: FslTheme | FslTheme[]);
|
|
1189
|
+
/** Get the flow direction for graph layout (e.g. `'right'`, `'down'`).
|
|
1190
|
+
* Set via the FSL `flow` directive.
|
|
1191
|
+
* @returns The current flow direction.
|
|
1192
|
+
*/
|
|
1193
|
+
flow(): FslDirection;
|
|
1194
|
+
/** Look up a transition's edge index by source and target state names.
|
|
1195
|
+
* @param from - Source state name.
|
|
1196
|
+
* @param to - Target state name.
|
|
1197
|
+
* @returns The edge index in the edges array, or `undefined` if no
|
|
1198
|
+
* such transition exists.
|
|
1199
|
+
*/
|
|
1200
|
+
get_transition_by_state_names(from: StateType, to: StateType): number;
|
|
1201
|
+
/** Look up the full transition object for a given source→target pair.
|
|
1202
|
+
* @param from - Source state name.
|
|
1203
|
+
* @param to - Target state name.
|
|
1204
|
+
* @returns The {@link JssmTransition} object, or `undefined` if none exists.
|
|
1205
|
+
*/
|
|
1206
|
+
lookup_transition_for(from: StateType, to: StateType): JssmTransition<StateType, mDT>;
|
|
1207
|
+
/********
|
|
1208
|
+
*
|
|
1209
|
+
* List all transitions attached to the current state, sorted by entrance and
|
|
1210
|
+
* exit. The order of each sublist is not defined. A node could appear in
|
|
1211
|
+
* both lists.
|
|
1212
|
+
*
|
|
1213
|
+
* ```typescript
|
|
1214
|
+
* import { sm } from 'jssm';
|
|
1215
|
+
*
|
|
1216
|
+
* const light = sm`red 'next' -> green 'next' -> yellow 'next' -> red; [red yellow green] 'shutdown' ~> off 'start' -> red;`;
|
|
1217
|
+
*
|
|
1218
|
+
* light.state(); // 'red'
|
|
1219
|
+
* light.list_transitions(); // { entrances: [ 'yellow', 'off' ], exits: [ 'green', 'off' ] }
|
|
1220
|
+
* ```
|
|
1221
|
+
*
|
|
1222
|
+
* @typeparam mDT The type of the machine data member; usually omitted
|
|
1223
|
+
*
|
|
1224
|
+
* @param whichState The state whose transitions to have listed
|
|
1225
|
+
*
|
|
1226
|
+
*/
|
|
1227
|
+
list_transitions(whichState?: StateType): JssmTransitionList;
|
|
1228
|
+
/********
|
|
1229
|
+
*
|
|
1230
|
+
* List all entrances attached to the current state. Please note that the
|
|
1231
|
+
* order of the list is not defined. This list includes both unforced and
|
|
1232
|
+
* forced entrances; if this isn't desired, consider
|
|
1233
|
+
* {@link list_unforced_entrances} or {@link list_forced_entrances} as
|
|
1234
|
+
* appropriate.
|
|
1235
|
+
*
|
|
1236
|
+
* ```typescript
|
|
1237
|
+
* import { sm } from 'jssm';
|
|
1238
|
+
*
|
|
1239
|
+
* const light = sm`red 'next' -> green 'next' -> yellow 'next' -> red; [red yellow green] 'shutdown' ~> off 'start' -> red;`;
|
|
1240
|
+
*
|
|
1241
|
+
* light.state(); // 'red'
|
|
1242
|
+
* light.list_entrances(); // [ 'yellow', 'off' ]
|
|
1243
|
+
* ```
|
|
1244
|
+
*
|
|
1245
|
+
* @typeparam mDT The type of the machine data member; usually omitted
|
|
1246
|
+
*
|
|
1247
|
+
* @param whichState The state whose entrances to have listed
|
|
1248
|
+
*
|
|
1249
|
+
*/
|
|
1250
|
+
list_entrances(whichState?: StateType): Array<StateType>;
|
|
1251
|
+
/********
|
|
1252
|
+
*
|
|
1253
|
+
* List all exits attached to the current state. Please note that the order
|
|
1254
|
+
* of the list is not defined. This list includes both unforced and forced
|
|
1255
|
+
* exits; if this isn't desired, consider {@link list_unforced_exits} or
|
|
1256
|
+
* {@link list_forced_exits} as appropriate.
|
|
1257
|
+
*
|
|
1258
|
+
* ```typescript
|
|
1259
|
+
* import { sm } from 'jssm';
|
|
1260
|
+
*
|
|
1261
|
+
* const light = sm`red 'next' -> green 'next' -> yellow 'next' -> red; [red yellow green] 'shutdown' ~> off 'start' -> red;`;
|
|
1262
|
+
*
|
|
1263
|
+
* light.state(); // 'red'
|
|
1264
|
+
* light.list_exits(); // [ 'green', 'off' ]
|
|
1265
|
+
* ```
|
|
1266
|
+
*
|
|
1267
|
+
* @typeparam mDT The type of the machine data member; usually omitted
|
|
1268
|
+
*
|
|
1269
|
+
* @param whichState The state whose exits to have listed
|
|
1270
|
+
*
|
|
1271
|
+
*/
|
|
1272
|
+
list_exits(whichState?: StateType): Array<StateType>;
|
|
1273
|
+
/** Get the transitions available from a state, filtered to those with
|
|
1274
|
+
* probability data. Used by the probabilistic walk system.
|
|
1275
|
+
* @param whichState - The state to inspect.
|
|
1276
|
+
* @returns An array of {@link JssmTransition} edges exiting the state.
|
|
1277
|
+
* @throws {JssmError} If the state does not exist.
|
|
1278
|
+
*/
|
|
1279
|
+
probable_exits_for(whichState: StateType): Array<JssmTransition<StateType, mDT>>;
|
|
1280
|
+
/** Take a single random transition from the current state, weighted by
|
|
1281
|
+
* edge probabilities.
|
|
1282
|
+
* @returns `true` if a transition was taken, `false` otherwise.
|
|
1283
|
+
*/
|
|
1284
|
+
probabilistic_transition(): boolean;
|
|
1285
|
+
/** Take `n` consecutive probabilistic transitions and return the sequence
|
|
1286
|
+
* of states visited (before each transition).
|
|
1287
|
+
* @param n - Number of steps to walk.
|
|
1288
|
+
* @returns An array of state names visited during the walk.
|
|
1289
|
+
*/
|
|
1290
|
+
probabilistic_walk(n: number): Array<StateType>;
|
|
1291
|
+
/** Take `n` probabilistic steps and return a histograph of how many times
|
|
1292
|
+
* each state was visited.
|
|
1293
|
+
* @param n - Number of steps to walk.
|
|
1294
|
+
* @returns A `Map` from state name to visit count.
|
|
1295
|
+
*/
|
|
1296
|
+
probabilistic_histo_walk(n: number): Map<StateType, number>;
|
|
1297
|
+
/********
|
|
1298
|
+
*
|
|
1299
|
+
* List all actions available from this state. Please note that the order of
|
|
1300
|
+
* the actions is not guaranteed.
|
|
1301
|
+
*
|
|
1302
|
+
* ```typescript
|
|
1303
|
+
* import { sm } from 'jssm';
|
|
1304
|
+
*
|
|
1305
|
+
* const machine = sm`
|
|
1306
|
+
* red 'next' -> green 'next' -> yellow 'next' -> red;
|
|
1307
|
+
* [red yellow green] 'shutdown' ~> off 'start' -> red;
|
|
1308
|
+
* `;
|
|
1309
|
+
*
|
|
1310
|
+
* console.log( machine.state() ); // logs 'red'
|
|
1311
|
+
* console.log( machine.actions() ); // logs ['next', 'shutdown']
|
|
1312
|
+
*
|
|
1313
|
+
* machine.action('next'); // true
|
|
1314
|
+
* console.log( machine.state() ); // logs 'green'
|
|
1315
|
+
* console.log( machine.actions() ); // logs ['next', 'shutdown']
|
|
1316
|
+
*
|
|
1317
|
+
* machine.action('shutdown'); // true
|
|
1318
|
+
* console.log( machine.state() ); // logs 'off'
|
|
1319
|
+
* console.log( machine.actions() ); // logs ['start']
|
|
1320
|
+
*
|
|
1321
|
+
* machine.action('start'); // true
|
|
1322
|
+
* console.log( machine.state() ); // logs 'red'
|
|
1323
|
+
* console.log( machine.actions() ); // logs ['next', 'shutdown']
|
|
1324
|
+
* ```
|
|
1325
|
+
*
|
|
1326
|
+
* @typeparam mDT The type of the machine data member; usually omitted
|
|
1327
|
+
*
|
|
1328
|
+
* @param whichState The state whose actions to list. Defaults to the
|
|
1329
|
+
* current state.
|
|
1330
|
+
*
|
|
1331
|
+
* @returns An array of action names available from the given state.
|
|
1332
|
+
*
|
|
1333
|
+
*/
|
|
1334
|
+
actions(whichState?: StateType): Array<StateType>;
|
|
1335
|
+
/********
|
|
1336
|
+
*
|
|
1337
|
+
* List all states that have a specific action attached. Please note that
|
|
1338
|
+
* the order of the states is not guaranteed.
|
|
1339
|
+
*
|
|
1340
|
+
* ```typescript
|
|
1341
|
+
* import { sm } from 'jssm';
|
|
1342
|
+
*
|
|
1343
|
+
* const machine = sm`
|
|
1344
|
+
* red 'next' -> green 'next' -> yellow 'next' -> red;
|
|
1345
|
+
* [red yellow green] 'shutdown' ~> off 'start' -> red;
|
|
1346
|
+
* `;
|
|
1347
|
+
*
|
|
1348
|
+
* console.log( machine.list_states_having_action('next') ); // ['red', 'green', 'yellow']
|
|
1349
|
+
* console.log( machine.list_states_having_action('start') ); // ['off']
|
|
1350
|
+
* ```
|
|
1351
|
+
*
|
|
1352
|
+
* @typeparam mDT The type of the machine data member; usually omitted
|
|
1353
|
+
*
|
|
1354
|
+
* @param whichState The action to be checked for associated states
|
|
1355
|
+
*
|
|
1356
|
+
*/
|
|
1357
|
+
list_states_having_action(whichState: StateType): Array<StateType>;
|
|
1358
|
+
/** List all action names available as exits from a given state.
|
|
1359
|
+
* @param whichState - The state to inspect. Defaults to the current state.
|
|
1360
|
+
* @returns An array of action name strings.
|
|
1361
|
+
* @throws {JssmError} If the state does not exist.
|
|
1362
|
+
*/
|
|
1363
|
+
list_exit_actions(whichState?: StateType): Array<StateType>;
|
|
1364
|
+
/** List all action exits from a state with their probabilities.
|
|
1365
|
+
* @param whichState - The state to inspect. Defaults to the current state.
|
|
1366
|
+
* @returns An array of `{ action, probability }` objects.
|
|
1367
|
+
* @throws {JssmError} If the state does not exist.
|
|
1368
|
+
*/
|
|
1369
|
+
probable_action_exits(whichState?: StateType): Array<any>;
|
|
1370
|
+
/** Check whether a state has no incoming transitions (unreachable after start).
|
|
1371
|
+
* @param whichState - The state to check.
|
|
1372
|
+
* @returns `true` if the state has zero entrances.
|
|
1373
|
+
* @throws {JssmError} If the state does not exist.
|
|
1374
|
+
*/
|
|
1375
|
+
is_unenterable(whichState: StateType): boolean;
|
|
1376
|
+
/** Check whether any state in the machine is unenterable.
|
|
1377
|
+
* @returns `true` if at least one state has no incoming transitions.
|
|
1378
|
+
*/
|
|
1379
|
+
has_unenterables(): boolean;
|
|
1380
|
+
/** Check whether the current state is terminal (has no exits).
|
|
1381
|
+
* @returns `true` if the current state has zero exits.
|
|
1382
|
+
*/
|
|
1383
|
+
is_terminal(): boolean;
|
|
1384
|
+
/** Check whether a specific state is terminal (has no exits).
|
|
1385
|
+
* @param whichState - The state to check.
|
|
1386
|
+
* @returns `true` if the state has zero exits.
|
|
1387
|
+
* @throws {JssmError} If the state does not exist.
|
|
1388
|
+
*/
|
|
1389
|
+
state_is_terminal(whichState: StateType): boolean;
|
|
1390
|
+
/** Check whether any state in the machine is terminal.
|
|
1391
|
+
* @returns `true` if at least one state has no exits.
|
|
1392
|
+
*/
|
|
1393
|
+
has_terminals(): boolean;
|
|
1394
|
+
/** Check whether the current state is complete (every exit has an action).
|
|
1395
|
+
* @returns `true` if the current state is complete.
|
|
1396
|
+
*/
|
|
1397
|
+
is_complete(): boolean;
|
|
1398
|
+
/** Check whether a specific state is complete (every exit has an action).
|
|
1399
|
+
* @param whichState - The state to check.
|
|
1400
|
+
* @returns `true` if the state is complete.
|
|
1401
|
+
* @throws {JssmError} If the state does not exist.
|
|
1402
|
+
*/
|
|
1403
|
+
state_is_complete(whichState: StateType): boolean;
|
|
1404
|
+
/** Check whether any state in the machine is complete.
|
|
1405
|
+
* @returns `true` if at least one state is complete.
|
|
1406
|
+
*/
|
|
1407
|
+
has_completes(): boolean;
|
|
1408
|
+
/** Low-level hook registration. Installs a handler described by a
|
|
1409
|
+
* {@link HookDescription} into the appropriate internal map. Prefer the
|
|
1410
|
+
* convenience wrappers ({@link hook}, {@link hook_entry}, etc.) over
|
|
1411
|
+
* calling this directly.
|
|
1412
|
+
* @param HookDesc - A hook descriptor specifying kind, states, and handler.
|
|
1413
|
+
*/
|
|
1414
|
+
set_hook(HookDesc: HookDescription<mDT>): void;
|
|
1415
|
+
/** Register a pre-transition hook on a specific edge. Fires before
|
|
1416
|
+
* transitioning from `from` to `to`. If the handler returns `false`, the
|
|
1417
|
+
* transition is blocked.
|
|
1418
|
+
*
|
|
1419
|
+
* ```typescript
|
|
1420
|
+
* const m = sm`a -> b -> c;`;
|
|
1421
|
+
* m.hook('a', 'b', () => console.log('a->b'));
|
|
1422
|
+
* ```
|
|
1423
|
+
*
|
|
1424
|
+
* @param from - Source state name.
|
|
1425
|
+
* @param to - Target state name.
|
|
1426
|
+
* @param handler - Callback invoked before the transition.
|
|
1427
|
+
* @returns `this` for chaining.
|
|
1428
|
+
*/
|
|
1429
|
+
hook(from: string, to: string, handler: HookHandler<mDT>): Machine<mDT>;
|
|
1430
|
+
/** Register a pre-transition hook on a specific action-labeled edge.
|
|
1431
|
+
* @param from - Source state name.
|
|
1432
|
+
* @param to - Target state name.
|
|
1433
|
+
* @param action - The action label that triggers this hook.
|
|
1434
|
+
* @param handler - Callback invoked before the transition.
|
|
1435
|
+
* @returns `this` for chaining.
|
|
1436
|
+
*/
|
|
1437
|
+
hook_action(from: string, to: string, action: string, handler: HookHandler<mDT>): Machine<mDT>;
|
|
1438
|
+
/** Register a pre-transition hook on any edge triggered by a specific action.
|
|
1439
|
+
* @param action - The action name to hook.
|
|
1440
|
+
* @param handler - Callback invoked before any transition with this action.
|
|
1441
|
+
* @returns `this` for chaining.
|
|
1442
|
+
*/
|
|
1443
|
+
hook_global_action(action: string, handler: HookHandler<mDT>): Machine<mDT>;
|
|
1444
|
+
/** Register a pre-transition hook on any action-driven transition.
|
|
1445
|
+
* @param handler - Callback invoked before any action transition.
|
|
1446
|
+
* @returns `this` for chaining.
|
|
1447
|
+
*/
|
|
1448
|
+
hook_any_action(handler: HookHandler<mDT>): Machine<mDT>;
|
|
1449
|
+
/** Register a pre-transition hook on any standard (`->`) transition.
|
|
1450
|
+
* @param handler - Callback invoked before any legal transition.
|
|
1451
|
+
* @returns `this` for chaining.
|
|
1452
|
+
*/
|
|
1453
|
+
hook_standard_transition(handler: HookHandler<mDT>): Machine<mDT>;
|
|
1454
|
+
/** Register a pre-transition hook on any main-path (`=>`) transition.
|
|
1455
|
+
* @param handler - Callback invoked before any main transition.
|
|
1456
|
+
* @returns `this` for chaining.
|
|
1457
|
+
*/
|
|
1458
|
+
hook_main_transition(handler: HookHandler<mDT>): Machine<mDT>;
|
|
1459
|
+
/** Register a pre-transition hook on any forced (`~>`) transition.
|
|
1460
|
+
* @param handler - Callback invoked before any forced transition.
|
|
1461
|
+
* @returns `this` for chaining.
|
|
1462
|
+
*/
|
|
1463
|
+
hook_forced_transition(handler: HookHandler<mDT>): Machine<mDT>;
|
|
1464
|
+
/** Register a pre-transition hook on any transition regardless of kind.
|
|
1465
|
+
* @param handler - Callback invoked before every transition.
|
|
1466
|
+
* @returns `this` for chaining.
|
|
1467
|
+
*/
|
|
1468
|
+
hook_any_transition(handler: HookHandler<mDT>): Machine<mDT>;
|
|
1469
|
+
/** Register a hook that fires when entering a specific state.
|
|
1470
|
+
* @param to - The state being entered.
|
|
1471
|
+
* @param handler - Callback invoked on entry.
|
|
1472
|
+
* @returns `this` for chaining.
|
|
1473
|
+
*/
|
|
1474
|
+
hook_entry(to: string, handler: HookHandler<mDT>): Machine<mDT>;
|
|
1475
|
+
/** Register a hook that fires when leaving a specific state.
|
|
1476
|
+
* @param from - The state being exited.
|
|
1477
|
+
* @param handler - Callback invoked on exit.
|
|
1478
|
+
* @returns `this` for chaining.
|
|
1479
|
+
*/
|
|
1480
|
+
hook_exit(from: string, handler: HookHandler<mDT>): Machine<mDT>;
|
|
1481
|
+
/** Register a hook that fires after leaving a specific state (post-exit).
|
|
1482
|
+
* @param from - The state that was exited.
|
|
1483
|
+
* @param handler - Callback invoked after exit completes.
|
|
1484
|
+
* @returns `this` for chaining.
|
|
1485
|
+
*/
|
|
1486
|
+
hook_after(from: string, handler: HookHandler<mDT>): Machine<mDT>;
|
|
1487
|
+
/** Post-transition hook on a specific edge. Fires after the transition
|
|
1488
|
+
* from `from` to `to` has completed. Cannot block the transition.
|
|
1489
|
+
* @param from - Source state name.
|
|
1490
|
+
* @param to - Target state name.
|
|
1491
|
+
* @param handler - Callback invoked after the transition.
|
|
1492
|
+
* @returns `this` for chaining.
|
|
1493
|
+
*/
|
|
1494
|
+
post_hook(from: string, to: string, handler: HookHandler<mDT>): Machine<mDT>;
|
|
1495
|
+
/** Post-transition hook on a specific action-labeled edge.
|
|
1496
|
+
* @param from - Source state name.
|
|
1497
|
+
* @param to - Target state name.
|
|
1498
|
+
* @param action - The action label.
|
|
1499
|
+
* @param handler - Callback invoked after the transition.
|
|
1500
|
+
* @returns `this` for chaining.
|
|
1501
|
+
*/
|
|
1502
|
+
post_hook_action(from: string, to: string, action: string, handler: HookHandler<mDT>): Machine<mDT>;
|
|
1503
|
+
/** Post-transition hook on any edge triggered by a specific action.
|
|
1504
|
+
* @param action - The action name.
|
|
1505
|
+
* @param handler - Callback invoked after any transition with this action.
|
|
1506
|
+
* @returns `this` for chaining.
|
|
1507
|
+
*/
|
|
1508
|
+
post_hook_global_action(action: string, handler: HookHandler<mDT>): Machine<mDT>;
|
|
1509
|
+
/** Post-transition hook on any action-driven transition.
|
|
1510
|
+
* @param handler - Callback invoked after any action transition.
|
|
1511
|
+
* @returns `this` for chaining.
|
|
1512
|
+
*/
|
|
1513
|
+
post_hook_any_action(handler: HookHandler<mDT>): Machine<mDT>;
|
|
1514
|
+
/** Post-transition hook on any standard (`->`) transition.
|
|
1515
|
+
* @param handler - Callback invoked after any legal transition.
|
|
1516
|
+
* @returns `this` for chaining.
|
|
1517
|
+
*/
|
|
1518
|
+
post_hook_standard_transition(handler: HookHandler<mDT>): Machine<mDT>;
|
|
1519
|
+
/** Post-transition hook on any main-path (`=>`) transition.
|
|
1520
|
+
* @param handler - Callback invoked after any main transition.
|
|
1521
|
+
* @returns `this` for chaining.
|
|
1522
|
+
*/
|
|
1523
|
+
post_hook_main_transition(handler: HookHandler<mDT>): Machine<mDT>;
|
|
1524
|
+
/** Post-transition hook on any forced (`~>`) transition.
|
|
1525
|
+
* @param handler - Callback invoked after any forced transition.
|
|
1526
|
+
* @returns `this` for chaining.
|
|
1527
|
+
*/
|
|
1528
|
+
post_hook_forced_transition(handler: HookHandler<mDT>): Machine<mDT>;
|
|
1529
|
+
/** Post-transition hook on any transition regardless of kind.
|
|
1530
|
+
* @param handler - Callback invoked after every transition.
|
|
1531
|
+
* @returns `this` for chaining.
|
|
1532
|
+
*/
|
|
1533
|
+
post_hook_any_transition(handler: HookHandler<mDT>): Machine<mDT>;
|
|
1534
|
+
/** Post-transition hook that fires after entering a specific state.
|
|
1535
|
+
* @param to - The state that was entered.
|
|
1536
|
+
* @param handler - Callback invoked after entry.
|
|
1537
|
+
* @returns `this` for chaining.
|
|
1538
|
+
*/
|
|
1539
|
+
post_hook_entry(to: string, handler: HookHandler<mDT>): Machine<mDT>;
|
|
1540
|
+
/** Post-transition hook that fires after leaving a specific state.
|
|
1541
|
+
* @param from - The state that was exited.
|
|
1542
|
+
* @param handler - Callback invoked after exit.
|
|
1543
|
+
* @returns `this` for chaining.
|
|
1544
|
+
*/
|
|
1545
|
+
post_hook_exit(from: string, handler: HookHandler<mDT>): Machine<mDT>;
|
|
1546
|
+
/** Register a pre-transition hook that fires **before** all other pre-hooks
|
|
1547
|
+
* on every transition. If the handler returns `false`, the transition is
|
|
1548
|
+
* blocked. The handler receives an {@link EverythingHookContext} whose
|
|
1549
|
+
* `hook_name` is `'pre everything'`.
|
|
1550
|
+
*
|
|
1551
|
+
* ```typescript
|
|
1552
|
+
* const m = sm`a -> b -> c;`;
|
|
1553
|
+
* m.hook_pre_everything(({ hook_name }) => {
|
|
1554
|
+
* console.log(`${hook_name} fired`);
|
|
1555
|
+
* return true;
|
|
1556
|
+
* });
|
|
1557
|
+
* ```
|
|
1558
|
+
*
|
|
1559
|
+
* @param handler - Callback invoked before all other pre-hooks.
|
|
1560
|
+
* @returns `this` for chaining.
|
|
1561
|
+
*/
|
|
1562
|
+
hook_pre_everything(handler: EverythingHookHandler<mDT>): Machine<mDT>;
|
|
1563
|
+
/** Register a pre-transition hook that fires **after** all other pre-hooks
|
|
1564
|
+
* on every transition. If the handler returns `false`, the transition is
|
|
1565
|
+
* blocked. The handler receives an {@link EverythingHookContext} whose
|
|
1566
|
+
* `hook_name` is `'everything'`.
|
|
1567
|
+
*
|
|
1568
|
+
* ```typescript
|
|
1569
|
+
* const m = sm`a -> b -> c;`;
|
|
1570
|
+
* m.hook_everything(({ hook_name }) => {
|
|
1571
|
+
* console.log(`${hook_name} fired`);
|
|
1572
|
+
* return true;
|
|
1573
|
+
* });
|
|
1574
|
+
* ```
|
|
1575
|
+
*
|
|
1576
|
+
* @param handler - Callback invoked after all other pre-hooks.
|
|
1577
|
+
* @returns `this` for chaining.
|
|
1578
|
+
*/
|
|
1579
|
+
hook_everything(handler: EverythingHookHandler<mDT>): Machine<mDT>;
|
|
1580
|
+
/** Register a post-transition hook that fires **after** all other
|
|
1581
|
+
* post-hooks on every transition. Cannot block the transition. The
|
|
1582
|
+
* handler receives an {@link EverythingHookContext} whose `hook_name` is
|
|
1583
|
+
* `'post everything'`.
|
|
1584
|
+
*
|
|
1585
|
+
* ```typescript
|
|
1586
|
+
* const m = sm`a -> b -> c;`;
|
|
1587
|
+
* m.hook_post_everything(({ hook_name }) => {
|
|
1588
|
+
* console.log(`${hook_name} fired`);
|
|
1589
|
+
* });
|
|
1590
|
+
* ```
|
|
1591
|
+
*
|
|
1592
|
+
* @param handler - Callback invoked after all other post-hooks.
|
|
1593
|
+
* @returns `this` for chaining.
|
|
1594
|
+
*/
|
|
1595
|
+
hook_post_everything(handler: PostEverythingHookHandler<mDT>): Machine<mDT>;
|
|
1596
|
+
/** Register a post-transition hook that fires **before** all other
|
|
1597
|
+
* post-hooks on every transition. Cannot block the transition. The
|
|
1598
|
+
* handler receives an {@link EverythingHookContext} whose `hook_name` is
|
|
1599
|
+
* `'pre post everything'`.
|
|
1600
|
+
*
|
|
1601
|
+
* ```typescript
|
|
1602
|
+
* const m = sm`a -> b -> c;`;
|
|
1603
|
+
* m.hook_pre_post_everything(({ hook_name }) => {
|
|
1604
|
+
* console.log(`${hook_name} fired`);
|
|
1605
|
+
* });
|
|
1606
|
+
* ```
|
|
1607
|
+
*
|
|
1608
|
+
* @param handler - Callback invoked before all other post-hooks.
|
|
1609
|
+
* @returns `this` for chaining.
|
|
1610
|
+
*/
|
|
1611
|
+
hook_pre_post_everything(handler: PostEverythingHookHandler<mDT>): Machine<mDT>;
|
|
1612
|
+
/** Get the current RNG seed used for probabilistic transitions.
|
|
1613
|
+
* @returns The numeric seed value.
|
|
1614
|
+
*/
|
|
1615
|
+
get rng_seed(): number;
|
|
1616
|
+
/** Set the RNG seed. Pass `undefined` to reseed from the current time.
|
|
1617
|
+
* Resets the internal PRNG so subsequent probabilistic operations use the
|
|
1618
|
+
* new seed.
|
|
1619
|
+
* @param to - The seed value, or `undefined` for time-based seeding.
|
|
1620
|
+
*/
|
|
1621
|
+
set rng_seed(to: number | undefined);
|
|
1622
|
+
/** Get all edges between two states (there can be multiple with
|
|
1623
|
+
* different actions).
|
|
1624
|
+
* @param from - Source state name.
|
|
1625
|
+
* @param to - Target state name.
|
|
1626
|
+
* @returns An array of matching {@link JssmTransition} objects.
|
|
1627
|
+
*/
|
|
1628
|
+
edges_between(from: string, to: string): JssmTransition<StateType, mDT>[];
|
|
1629
|
+
/*********
|
|
1630
|
+
*
|
|
1631
|
+
* Replace the current state and data with no regard to the graph.
|
|
1632
|
+
*
|
|
1633
|
+
* ```typescript
|
|
1634
|
+
* import { sm } from 'jssm';
|
|
1635
|
+
*
|
|
1636
|
+
* const machine = sm`a -> b -> c;`;
|
|
1637
|
+
* console.log( machine.state() ); // 'a'
|
|
1638
|
+
*
|
|
1639
|
+
* machine.go('b');
|
|
1640
|
+
* machine.go('c');
|
|
1641
|
+
* console.log( machine.state() ); // 'c'
|
|
1642
|
+
*
|
|
1643
|
+
* machine.override('a');
|
|
1644
|
+
* console.log( machine.state() ); // 'a'
|
|
1645
|
+
* ```
|
|
1646
|
+
*
|
|
1647
|
+
*/
|
|
1648
|
+
override(newState: StateType, newData?: mDT | undefined): void;
|
|
1649
|
+
/*********
|
|
1650
|
+
*
|
|
1651
|
+
* Shared transition core used by {@link transition}, {@link force_transition},
|
|
1652
|
+
* and {@link action}. Runs validation, fires the full hook pipeline (pre-
|
|
1653
|
+
* everything, any-action, after, any-transition, exit, named, basic,
|
|
1654
|
+
* edge-type, entry, everything), commits the new state if nothing
|
|
1655
|
+
* rejected, and returns whether the transition succeeded.
|
|
1656
|
+
*
|
|
1657
|
+
* Not meant for external use. Call one of the public wrappers instead:
|
|
1658
|
+
* - `transition` for an ordinary legal transition
|
|
1659
|
+
* - `force_transition` to bypass the legality check
|
|
1660
|
+
* - `action` to dispatch by action name rather than target state
|
|
1661
|
+
*
|
|
1662
|
+
* @remarks
|
|
1663
|
+
* Known sharp edges, carried over from the original `// TODO` comments:
|
|
1664
|
+
* - The forced-ness behavior needs to be cleaned up a lot here.
|
|
1665
|
+
* - The callbacks are not fully correct across the forced / action / plain
|
|
1666
|
+
* cases and should be revisited.
|
|
1667
|
+
* - When multiple edges exist between two states with different `kind`
|
|
1668
|
+
* values, only the first edge's kind is used to pick the edge-type hook.
|
|
1669
|
+
*
|
|
1670
|
+
* @typeparam mDT The type of the machine data member; usually omitted.
|
|
1671
|
+
*
|
|
1672
|
+
* @param newStateOrAction The target state name (for a plain or forced
|
|
1673
|
+
* transition) or the action name (when `wasAction` is true).
|
|
1674
|
+
*
|
|
1675
|
+
* @param newData Optional replacement machine data to install alongside
|
|
1676
|
+
* the transition. Hooks may further override this via complex results.
|
|
1677
|
+
*
|
|
1678
|
+
* @param wasForced `true` if the caller invoked `force_transition`, in
|
|
1679
|
+
* which case legality is checked against `valid_force_transition` rather
|
|
1680
|
+
* than `valid_transition`.
|
|
1681
|
+
*
|
|
1682
|
+
* @param wasAction `true` if the caller invoked `action`, in which case
|
|
1683
|
+
* `newStateOrAction` is an action name and the target state is looked up
|
|
1684
|
+
* via the current action edge.
|
|
1685
|
+
*
|
|
1686
|
+
* @returns `true` if the transition was valid and every hook passed;
|
|
1687
|
+
* `false` if the transition was invalid or any hook rejected.
|
|
1688
|
+
*
|
|
1689
|
+
* @internal
|
|
1690
|
+
*
|
|
1691
|
+
*/
|
|
1692
|
+
transition_impl(newStateOrAction: StateType, newData: mDT | undefined, wasForced: boolean, wasAction: boolean): boolean;
|
|
1693
|
+
/** If the current state has an `after` timeout configured, schedule it.
|
|
1694
|
+
* Called internally after each transition.
|
|
1695
|
+
*/
|
|
1696
|
+
auto_set_state_timeout(): void;
|
|
1697
|
+
/*********
|
|
1698
|
+
*
|
|
1699
|
+
* Get a truncated history of the recent states and data of the machine.
|
|
1700
|
+
* Turned off by default; configure with `.from('...', {data: 5})` by length,
|
|
1701
|
+
* or set `.history_length` at runtime.
|
|
1702
|
+
*
|
|
1703
|
+
* History *does not contain the current state*. If you want that, call
|
|
1704
|
+
* `.history_inclusive` instead.
|
|
1705
|
+
*
|
|
1706
|
+
* ```typescript
|
|
1707
|
+
* const foo = jssm.from(
|
|
1708
|
+
* "a 'next' -> b 'next' -> c 'next' -> d 'next' -> e;",
|
|
1709
|
+
* { history: 3 }
|
|
1710
|
+
* );
|
|
1711
|
+
*
|
|
1712
|
+
* foo.action('next');
|
|
1713
|
+
* foo.action('next');
|
|
1714
|
+
* foo.action('next');
|
|
1715
|
+
* foo.action('next');
|
|
1716
|
+
*
|
|
1717
|
+
* foo.history; // [ ['b',undefined], ['c',undefined], ['d',undefined] ]
|
|
1718
|
+
* ```
|
|
1719
|
+
*
|
|
1720
|
+
* Notice that the machine's current state, `e`, is not in the returned list.
|
|
1721
|
+
*
|
|
1722
|
+
* @typeparam mDT The type of the machine data member; usually omitted
|
|
1723
|
+
*
|
|
1724
|
+
*/
|
|
1725
|
+
get history(): [string, mDT][];
|
|
1726
|
+
/*********
|
|
1727
|
+
*
|
|
1728
|
+
* Get a truncated history of the recent states and data of the machine,
|
|
1729
|
+
* including the current state. Turned off by default; configure with
|
|
1730
|
+
* `.from('...', {data: 5})` by length, or set `.history_length` at runtime.
|
|
1731
|
+
*
|
|
1732
|
+
* History inclusive contains the current state. If you only want past
|
|
1733
|
+
* states, call `.history` instead.
|
|
1734
|
+
*
|
|
1735
|
+
* The list returned will be one longer than the history buffer kept, as the
|
|
1736
|
+
* history buffer kept gets the current state added to it to produce this
|
|
1737
|
+
* list.
|
|
1738
|
+
*
|
|
1739
|
+
* ```typescript
|
|
1740
|
+
* const foo = jssm.from(
|
|
1741
|
+
* "a 'next' -> b 'next' -> c 'next' -> d 'next' -> e;",
|
|
1742
|
+
* { history: 3 }
|
|
1743
|
+
* );
|
|
1744
|
+
*
|
|
1745
|
+
* foo.action('next');
|
|
1746
|
+
* foo.action('next');
|
|
1747
|
+
* foo.action('next');
|
|
1748
|
+
* foo.action('next');
|
|
1749
|
+
*
|
|
1750
|
+
* foo.history_inclusive; // [ ['b',undefined], ['c',undefined], ['d',undefined], ['e',undefined] ]
|
|
1751
|
+
* ```
|
|
1752
|
+
*
|
|
1753
|
+
* Notice that the machine's current state, `e`, is in the returned list.
|
|
1754
|
+
*
|
|
1755
|
+
* @typeparam mDT The type of the machine data member; usually omitted
|
|
1756
|
+
*
|
|
1757
|
+
*/
|
|
1758
|
+
get history_inclusive(): [string, mDT][];
|
|
1759
|
+
/*********
|
|
1760
|
+
*
|
|
1761
|
+
* Find out how long a history this machine is keeping. Defaults to zero.
|
|
1762
|
+
* Settable directly.
|
|
1763
|
+
*
|
|
1764
|
+
* ```typescript
|
|
1765
|
+
* const foo = jssm.from("a -> b;");
|
|
1766
|
+
* foo.history_length; // 0
|
|
1767
|
+
*
|
|
1768
|
+
* const bar = jssm.from("a -> b;", { history: 3 });
|
|
1769
|
+
* foo.history_length; // 3
|
|
1770
|
+
* foo.history_length = 5;
|
|
1771
|
+
* foo.history_length; // 5
|
|
1772
|
+
* ```
|
|
1773
|
+
*
|
|
1774
|
+
* @typeparam mDT The type of the machine data member; usually omitted
|
|
1775
|
+
*
|
|
1776
|
+
*/
|
|
1777
|
+
get history_length(): number;
|
|
1778
|
+
set history_length(to: number);
|
|
1779
|
+
/********
|
|
1780
|
+
*
|
|
1781
|
+
* Instruct the machine to complete an action. Synonym for {@link do}.
|
|
1782
|
+
*
|
|
1783
|
+
* ```typescript
|
|
1784
|
+
* const light = sm`red 'next' -> green 'next' -> yellow 'next' -> red; [red yellow green] 'shutdown' ~> off 'start' -> red;`;
|
|
1785
|
+
*
|
|
1786
|
+
* light.state(); // 'red'
|
|
1787
|
+
* light.action('next'); // true
|
|
1788
|
+
* light.state(); // 'green'
|
|
1789
|
+
* ```
|
|
1790
|
+
*
|
|
1791
|
+
* @typeparam mDT The type of the machine data member; usually omitted
|
|
1792
|
+
*
|
|
1793
|
+
* @param actionName The action to engage
|
|
1794
|
+
*
|
|
1795
|
+
* @param newData The data change to insert during the action
|
|
1796
|
+
*
|
|
1797
|
+
* @returns `true` if the action was valid and the transition occurred,
|
|
1798
|
+
* `false` otherwise.
|
|
1799
|
+
*
|
|
1800
|
+
*/
|
|
1801
|
+
action(actionName: StateType, newData?: mDT): boolean;
|
|
1802
|
+
/********
|
|
1803
|
+
*
|
|
1804
|
+
* Get the standard style for a single state. ***Does not*** include
|
|
1805
|
+
* composition from an applied theme, or things from the underlying base
|
|
1806
|
+
* stylesheet; only the modifications applied by this machine.
|
|
1807
|
+
*
|
|
1808
|
+
* ```typescript
|
|
1809
|
+
* const light = sm`a -> b;`;
|
|
1810
|
+
* console.log(light.standard_state_style);
|
|
1811
|
+
* // {}
|
|
1812
|
+
*
|
|
1813
|
+
* const light = sm`a -> b; state: { shape: circle; };`;
|
|
1814
|
+
* console.log(light.standard_state_style);
|
|
1815
|
+
* // { shape: 'circle' }
|
|
1816
|
+
* ```
|
|
1817
|
+
*
|
|
1818
|
+
* @typeparam mDT The type of the machine data member; usually omitted
|
|
1819
|
+
*
|
|
1820
|
+
* @returns The {@link JssmStateConfig} for standard states.
|
|
1821
|
+
*
|
|
1822
|
+
*/
|
|
1823
|
+
get standard_state_style(): JssmStateConfig;
|
|
1824
|
+
/********
|
|
1825
|
+
*
|
|
1826
|
+
* Get the hooked state style. ***Does not*** include
|
|
1827
|
+
* composition from an applied theme, or things from the underlying base
|
|
1828
|
+
* stylesheet; only the modifications applied by this machine.
|
|
1829
|
+
*
|
|
1830
|
+
* The hooked style is only applied to nodes which have a named hook in the
|
|
1831
|
+
* graph. Open hooks set through the external API aren't graphed, because
|
|
1832
|
+
* that would be literally every node.
|
|
1833
|
+
*
|
|
1834
|
+
* ```typescript
|
|
1835
|
+
* const light = sm`a -> b;`;
|
|
1836
|
+
* console.log(light.hooked_state_style);
|
|
1837
|
+
* // {}
|
|
1838
|
+
*
|
|
1839
|
+
* const light = sm`a -> b; hooked_state: { shape: circle; };`;
|
|
1840
|
+
* console.log(light.hooked_state_style);
|
|
1841
|
+
* // { shape: 'circle' }
|
|
1842
|
+
* ```
|
|
1843
|
+
*
|
|
1844
|
+
* @typeparam mDT The type of the machine data member; usually omitted
|
|
1845
|
+
*
|
|
1846
|
+
* @returns The {@link JssmStateConfig} for hooked states.
|
|
1847
|
+
*
|
|
1848
|
+
*/
|
|
1849
|
+
get hooked_state_style(): JssmStateConfig;
|
|
1850
|
+
/********
|
|
1851
|
+
*
|
|
1852
|
+
* Get the start state style. ***Does not*** include composition from an
|
|
1853
|
+
* applied theme, or things from the underlying base stylesheet; only the
|
|
1854
|
+
* modifications applied by this machine.
|
|
1855
|
+
*
|
|
1856
|
+
* Start states are defined by the directive `start_states`, or in absentia,
|
|
1857
|
+
* are the first mentioned state.
|
|
1858
|
+
*
|
|
1859
|
+
* ```typescript
|
|
1860
|
+
* const light = sm`a -> b;`;
|
|
1861
|
+
* console.log(light.start_state_style);
|
|
1862
|
+
* // {}
|
|
1863
|
+
*
|
|
1864
|
+
* const light = sm`a -> b; start_state: { shape: circle; };`;
|
|
1865
|
+
* console.log(light.start_state_style);
|
|
1866
|
+
* // { shape: 'circle' }
|
|
1867
|
+
* ```
|
|
1868
|
+
*
|
|
1869
|
+
* @typeparam mDT The type of the machine data member; usually omitted
|
|
1870
|
+
*
|
|
1871
|
+
* @returns The {@link JssmStateConfig} for start states.
|
|
1872
|
+
*
|
|
1873
|
+
*/
|
|
1874
|
+
get start_state_style(): JssmStateConfig;
|
|
1875
|
+
/********
|
|
1876
|
+
*
|
|
1877
|
+
* Get the end state style. ***Does not*** include
|
|
1878
|
+
* composition from an applied theme, or things from the underlying base
|
|
1879
|
+
* stylesheet; only the modifications applied by this machine.
|
|
1880
|
+
*
|
|
1881
|
+
* End states are defined in the directive `end_states`, and are distinct
|
|
1882
|
+
* from terminal states. End states are voluntary successful endpoints for a
|
|
1883
|
+
* process. Terminal states are states that cannot be exited. By example,
|
|
1884
|
+
* most error states are terminal states, but not end states. Also, since
|
|
1885
|
+
* some end states can be exited and are determined by hooks, such as
|
|
1886
|
+
* recursive or iterative nodes, there is such a thing as an end state that
|
|
1887
|
+
* is not a terminal state.
|
|
1888
|
+
*
|
|
1889
|
+
* ```typescript
|
|
1890
|
+
* const light = sm`a -> b;`;
|
|
1891
|
+
* console.log(light.standard_state_style);
|
|
1892
|
+
* // {}
|
|
1893
|
+
*
|
|
1894
|
+
* const light = sm`a -> b; end_state: { shape: circle; };`;
|
|
1895
|
+
* console.log(light.standard_state_style);
|
|
1896
|
+
* // { shape: 'circle' }
|
|
1897
|
+
* ```
|
|
1898
|
+
*
|
|
1899
|
+
* @typeparam mDT The type of the machine data member; usually omitted
|
|
1900
|
+
*
|
|
1901
|
+
* @returns The {@link JssmStateConfig} for end states.
|
|
1902
|
+
*
|
|
1903
|
+
*/
|
|
1904
|
+
get end_state_style(): JssmStateConfig;
|
|
1905
|
+
/********
|
|
1906
|
+
*
|
|
1907
|
+
* Get the terminal state style. ***Does not*** include
|
|
1908
|
+
* composition from an applied theme, or things from the underlying base
|
|
1909
|
+
* stylesheet; only the modifications applied by this machine.
|
|
1910
|
+
*
|
|
1911
|
+
* Terminal state styles are automatically determined by the machine. Any
|
|
1912
|
+
* state without a valid exit transition is terminal.
|
|
1913
|
+
*
|
|
1914
|
+
* ```typescript
|
|
1915
|
+
* const light = sm`a -> b;`;
|
|
1916
|
+
* console.log(light.terminal_state_style);
|
|
1917
|
+
* // {}
|
|
1918
|
+
*
|
|
1919
|
+
* const light = sm`a -> b; terminal_state: { shape: circle; };`;
|
|
1920
|
+
* console.log(light.terminal_state_style);
|
|
1921
|
+
* // { shape: 'circle' }
|
|
1922
|
+
* ```
|
|
1923
|
+
*
|
|
1924
|
+
* @typeparam mDT The type of the machine data member; usually omitted
|
|
1925
|
+
*
|
|
1926
|
+
* @returns The {@link JssmStateConfig} for terminal states.
|
|
1927
|
+
*
|
|
1928
|
+
*/
|
|
1929
|
+
get terminal_state_style(): JssmStateConfig;
|
|
1930
|
+
/********
|
|
1931
|
+
*
|
|
1932
|
+
* Get the style for the active state. ***Does not*** include
|
|
1933
|
+
* composition from an applied theme, or things from the underlying base
|
|
1934
|
+
* stylesheet; only the modifications applied by this machine.
|
|
1935
|
+
*
|
|
1936
|
+
* ```typescript
|
|
1937
|
+
* const light = sm`a -> b;`;
|
|
1938
|
+
* console.log(light.active_state_style);
|
|
1939
|
+
* // {}
|
|
1940
|
+
*
|
|
1941
|
+
* const light = sm`a -> b; active_state: { shape: circle; };`;
|
|
1942
|
+
* console.log(light.active_state_style);
|
|
1943
|
+
* // { shape: 'circle' }
|
|
1944
|
+
* ```
|
|
1945
|
+
*
|
|
1946
|
+
* @typeparam mDT The type of the machine data member; usually omitted
|
|
1947
|
+
*
|
|
1948
|
+
* @returns The {@link JssmStateConfig} for the active state.
|
|
1949
|
+
*
|
|
1950
|
+
*/
|
|
1951
|
+
get active_state_style(): JssmStateConfig;
|
|
1952
|
+
/********
|
|
1953
|
+
*
|
|
1954
|
+
* Gets the composite style for a specific node by individually imposing the
|
|
1955
|
+
* style layers on a given object, after determining which layers are
|
|
1956
|
+
* appropriate.
|
|
1957
|
+
*
|
|
1958
|
+
* The order of composition is base, then theme, then user content. Each
|
|
1959
|
+
* item in the stack will be composited independently. First, the base state
|
|
1960
|
+
* style, then the theme state style, then the user state style.
|
|
1961
|
+
*
|
|
1962
|
+
* After the three state styles, we'll composite the hooked styles; then the
|
|
1963
|
+
* terminal styles; then the start styles; then the end styles; finally, the
|
|
1964
|
+
* active styles. Remember, last wins.
|
|
1965
|
+
*
|
|
1966
|
+
* The base state style must exist. All other styles are optional.
|
|
1967
|
+
*
|
|
1968
|
+
* @typeparam mDT The type of the machine data member; usually omitted
|
|
1969
|
+
*
|
|
1970
|
+
* @param state The state to compute the composite style for.
|
|
1971
|
+
*
|
|
1972
|
+
* @returns The fully composited {@link JssmStateConfig} for the given state.
|
|
1973
|
+
*
|
|
1974
|
+
*/
|
|
1975
|
+
style_for(state: StateType): JssmStateConfig;
|
|
1976
|
+
/********
|
|
1977
|
+
*
|
|
1978
|
+
* Instruct the machine to complete an action. Synonym for {@link action}.
|
|
1979
|
+
*
|
|
1980
|
+
* ```typescript
|
|
1981
|
+
* const light = sm`
|
|
1982
|
+
* off 'start' -> red;
|
|
1983
|
+
* red 'next' -> green 'next' -> yellow 'next' -> red;
|
|
1984
|
+
* [red yellow green] 'shutdown' ~> off;
|
|
1985
|
+
* `;
|
|
1986
|
+
*
|
|
1987
|
+
* light.state(); // 'off'
|
|
1988
|
+
* light.do('start'); // true
|
|
1989
|
+
* light.state(); // 'red'
|
|
1990
|
+
* light.do('next'); // true
|
|
1991
|
+
* light.state(); // 'green'
|
|
1992
|
+
* light.do('next'); // true
|
|
1993
|
+
* light.state(); // 'yellow'
|
|
1994
|
+
* light.do('dance'); // !! false - no such action
|
|
1995
|
+
* light.state(); // 'yellow'
|
|
1996
|
+
* light.do('start'); // !! false - yellow does not have the action start
|
|
1997
|
+
* light.state(); // 'yellow'
|
|
1998
|
+
* ```
|
|
1999
|
+
*
|
|
2000
|
+
* @typeparam mDT The type of the machine data member; usually omitted
|
|
2001
|
+
*
|
|
2002
|
+
* @param actionName The action to engage
|
|
2003
|
+
*
|
|
2004
|
+
* @param newData The data change to insert during the action
|
|
2005
|
+
*
|
|
2006
|
+
* @returns `true` if the action was valid and the transition occurred,
|
|
2007
|
+
* `false` otherwise.
|
|
2008
|
+
*
|
|
2009
|
+
*/
|
|
2010
|
+
do(actionName: StateType, newData?: mDT): boolean;
|
|
2011
|
+
/********
|
|
2012
|
+
*
|
|
2013
|
+
* Instruct the machine to complete a transition. Synonym for {@link go}.
|
|
2014
|
+
*
|
|
2015
|
+
* ```typescript
|
|
2016
|
+
* const light = sm`
|
|
2017
|
+
* off 'start' -> red;
|
|
2018
|
+
* red 'next' -> green 'next' -> yellow 'next' -> red;
|
|
2019
|
+
* [red yellow green] 'shutdown' ~> off;
|
|
2020
|
+
* `;
|
|
2021
|
+
*
|
|
2022
|
+
* light.state(); // 'off'
|
|
2023
|
+
* light.go('red'); // true
|
|
2024
|
+
* light.state(); // 'red'
|
|
2025
|
+
* light.go('green'); // true
|
|
2026
|
+
* light.state(); // 'green'
|
|
2027
|
+
* light.go('blue'); // !! false - no such state
|
|
2028
|
+
* light.state(); // 'green'
|
|
2029
|
+
* light.go('red'); // !! false - green may not go directly to red, only to yellow
|
|
2030
|
+
* light.state(); // 'green'
|
|
2031
|
+
* ```
|
|
2032
|
+
*
|
|
2033
|
+
* @typeparam mDT The type of the machine data member; usually omitted
|
|
2034
|
+
*
|
|
2035
|
+
* @param newState The state to switch to
|
|
2036
|
+
*
|
|
2037
|
+
* @param newData The data change to insert during the transition
|
|
2038
|
+
*
|
|
2039
|
+
* @returns `true` if the transition was legal and occurred, `false` otherwise.
|
|
2040
|
+
*
|
|
2041
|
+
*/
|
|
2042
|
+
transition(newState: StateType, newData?: mDT): boolean;
|
|
2043
|
+
/********
|
|
2044
|
+
*
|
|
2045
|
+
* Instruct the machine to complete a transition. Synonym for {@link transition}.
|
|
2046
|
+
*
|
|
2047
|
+
* ```typescript
|
|
2048
|
+
* const light = sm`red -> green -> yellow -> red; [red yellow green] 'shutdown' ~> off 'start' -> red;`;
|
|
2049
|
+
*
|
|
2050
|
+
* light.state(); // 'red'
|
|
2051
|
+
* light.go('green'); // true
|
|
2052
|
+
* light.state(); // 'green'
|
|
2053
|
+
* ```
|
|
2054
|
+
*
|
|
2055
|
+
* @typeparam mDT The type of the machine data member; usually omitted
|
|
2056
|
+
*
|
|
2057
|
+
* @param newState The state to switch to
|
|
2058
|
+
*
|
|
2059
|
+
* @param newData The data change to insert during the transition
|
|
2060
|
+
*
|
|
2061
|
+
* @returns `true` if the transition was legal and occurred, `false` otherwise.
|
|
2062
|
+
*
|
|
2063
|
+
*/
|
|
2064
|
+
go(newState: StateType, newData?: mDT): boolean;
|
|
2065
|
+
/********
|
|
2066
|
+
*
|
|
2067
|
+
* Instruct the machine to complete a forced transition (which will reject if
|
|
2068
|
+
* called with a normal {@link transition} call.)
|
|
2069
|
+
*
|
|
2070
|
+
* ```typescript
|
|
2071
|
+
* const light = sm`red -> green -> yellow -> red; [red yellow green] 'shutdown' ~> off 'start' -> red;`;
|
|
2072
|
+
*
|
|
2073
|
+
* light.state(); // 'red'
|
|
2074
|
+
* light.transition('off'); // false
|
|
2075
|
+
* light.state(); // 'red'
|
|
2076
|
+
* light.force_transition('off'); // true
|
|
2077
|
+
* light.state(); // 'off'
|
|
2078
|
+
* ```
|
|
2079
|
+
*
|
|
2080
|
+
* @typeparam mDT The type of the machine data member; usually omitted
|
|
2081
|
+
*
|
|
2082
|
+
* @param newState The state to switch to
|
|
2083
|
+
*
|
|
2084
|
+
* @param newData The data change to insert during the transition
|
|
2085
|
+
*
|
|
2086
|
+
* @returns `true` if a transition (forced or otherwise) existed and occurred,
|
|
2087
|
+
* `false` otherwise.
|
|
2088
|
+
*
|
|
2089
|
+
*/
|
|
2090
|
+
force_transition(newState: StateType, newData?: mDT): boolean;
|
|
2091
|
+
/** Get the edge index for an action from the current state.
|
|
2092
|
+
* @param action - The action name.
|
|
2093
|
+
* @returns The edge index, or `undefined` if the action is not available.
|
|
2094
|
+
*/
|
|
2095
|
+
current_action_for(action: StateType): number;
|
|
2096
|
+
/** Get the full transition object for an action from the current state.
|
|
2097
|
+
* @param action - The action name.
|
|
2098
|
+
* @returns The {@link JssmTransition} object.
|
|
2099
|
+
* @throws {JssmError} If the action is not available from the current state.
|
|
2100
|
+
*/
|
|
2101
|
+
current_action_edge_for(action: StateType): JssmTransition<StateType, mDT>;
|
|
2102
|
+
/** Check whether an action is available from the current state.
|
|
2103
|
+
* @param action - The action name to check.
|
|
2104
|
+
* @param _newData - Reserved for future data validation.
|
|
2105
|
+
* @returns `true` if the action can be taken.
|
|
2106
|
+
*/
|
|
2107
|
+
valid_action(action: StateType, _newData?: mDT): boolean;
|
|
2108
|
+
/** Check whether a transition to a given state is legal (non-forced) from
|
|
2109
|
+
* the current state.
|
|
2110
|
+
* @param newState - The target state.
|
|
2111
|
+
* @param _newData - Reserved for future data validation.
|
|
2112
|
+
* @returns `true` if the transition is legal.
|
|
2113
|
+
*/
|
|
2114
|
+
valid_transition(newState: StateType, _newData?: mDT): boolean;
|
|
2115
|
+
/** Check whether a forced transition to a given state exists from the
|
|
2116
|
+
* current state.
|
|
2117
|
+
* @param newState - The target state.
|
|
2118
|
+
* @param _newData - Reserved for future data validation.
|
|
2119
|
+
* @returns `true` if a forced (or any) transition exists.
|
|
2120
|
+
*/
|
|
2121
|
+
valid_force_transition(newState: StateType, _newData?: mDT): boolean;
|
|
2122
|
+
/** Get the instance name of this machine, if one was assigned at creation.
|
|
2123
|
+
* @returns The instance name string, or `undefined`.
|
|
2124
|
+
*/
|
|
2125
|
+
instance_name(): string | undefined;
|
|
2126
|
+
/** Get the creation date of this machine as a `Date` object.
|
|
2127
|
+
* @returns A `Date` representing when the machine was created.
|
|
2128
|
+
*/
|
|
2129
|
+
get creation_date(): Date;
|
|
2130
|
+
/** Get the creation timestamp (milliseconds since epoch).
|
|
2131
|
+
* @returns The timestamp as a number.
|
|
2132
|
+
*/
|
|
2133
|
+
get creation_timestamp(): number;
|
|
2134
|
+
/** Get the timestamp when construction began (before parsing).
|
|
2135
|
+
* @returns The start-of-construction timestamp as a number.
|
|
2136
|
+
*/
|
|
2137
|
+
get create_start_time(): number;
|
|
2138
|
+
/** Schedule an automatic transition to `next_state` after `after_time`
|
|
2139
|
+
* milliseconds. Only one timeout may be active at a time.
|
|
2140
|
+
* @param next_state - The state to transition to when the timer fires.
|
|
2141
|
+
* @param after_time - Delay in milliseconds.
|
|
2142
|
+
* @throws {JssmError} If a timeout is already pending.
|
|
2143
|
+
*/
|
|
2144
|
+
set_state_timeout(next_state: StateType, after_time: number): void;
|
|
2145
|
+
/** Cancel any pending state timeout. Safe to call when no timeout is active.
|
|
2146
|
+
*/
|
|
2147
|
+
clear_state_timeout(): void;
|
|
2148
|
+
/** Get the configured `after` timeout for a given state, if any.
|
|
2149
|
+
* @param which_state - The state to look up.
|
|
2150
|
+
* @returns A `[targetState, delayMs]` tuple, or `undefined` if no timeout
|
|
2151
|
+
* is configured for that state.
|
|
2152
|
+
*/
|
|
2153
|
+
state_timeout_for(which_state: StateType): [StateType, number] | undefined;
|
|
2154
|
+
/** Get the configured `after` timeout for the current state, if any.
|
|
2155
|
+
* @returns A `[targetState, delayMs]` tuple, or `undefined`.
|
|
2156
|
+
*/
|
|
2157
|
+
current_state_timeout(): [StateType, number] | undefined;
|
|
2158
|
+
/** Convenience method to create a new machine from a tagged template literal.
|
|
2159
|
+
* Equivalent to calling the top-level `sm` function.
|
|
2160
|
+
* @param template_strings - The template string array.
|
|
2161
|
+
* @param remainder - Interpolated values.
|
|
2162
|
+
* @returns A new {@link Machine} instance.
|
|
2163
|
+
*/
|
|
2164
|
+
sm(template_strings: TemplateStringsArray, ...remainder: any[]): Machine<mDT>;
|
|
2165
|
+
}
|
|
2166
|
+
|
|
2167
|
+
/**
|
|
2168
|
+
* Inject runtime configuration for jssm/viz. Currently only accepts a
|
|
2169
|
+
* custom `DOMParser` constructor for use by `*_svg_element` functions in
|
|
2170
|
+
* environments that do not provide one globally (e.g. Node + jsdom).
|
|
2171
|
+
*
|
|
2172
|
+
* Idempotent — last call wins. No-op if called with no recognized keys.
|
|
2173
|
+
*
|
|
2174
|
+
* ```typescript
|
|
2175
|
+
* // Node, with jsdom:
|
|
2176
|
+
* import { JSDOM } from 'jsdom';
|
|
2177
|
+
* import { configure, fsl_to_svg_element } from 'jssm/viz';
|
|
2178
|
+
*
|
|
2179
|
+
* configure({ DOMParser: new JSDOM().window.DOMParser });
|
|
2180
|
+
* const el = await fsl_to_svg_element('a -> b;');
|
|
2181
|
+
* ```
|
|
2182
|
+
*
|
|
2183
|
+
* @param opts Configuration overrides.
|
|
2184
|
+
* @param opts.DOMParser Constructor compatible with the WHATWG `DOMParser`
|
|
2185
|
+
* interface. Used as a fallback when `globalThis.DOMParser` is undefined.
|
|
2186
|
+
*
|
|
2187
|
+
* @throws {JssmError} if `DOMParser` is provided and is not a constructor.
|
|
2188
|
+
*/
|
|
2189
|
+
declare function configure(opts: {
|
|
2190
|
+
DOMParser?: typeof globalThis.DOMParser;
|
|
2191
|
+
}): void;
|
|
2192
|
+
/**
|
|
2193
|
+
* Look up a color from the default viz palette by key, returning empty
|
|
2194
|
+
* string if the key is unknown (so it disappears in feature concatenation).
|
|
2195
|
+
*
|
|
2196
|
+
* @internal
|
|
2197
|
+
*/
|
|
2198
|
+
declare function vc(col: string): string;
|
|
2199
|
+
/**
|
|
2200
|
+
* Build a graphviz-safe node identifier for a state, by index. Accepts
|
|
2201
|
+
* either a `string[]` (used historically; O(n) per call) or a
|
|
2202
|
+
* precomputed `Map<state, index>` (used by rendering hot paths; O(1)
|
|
2203
|
+
* per call). The map form is used during dot generation; the array
|
|
2204
|
+
* form is retained for direct test access via `_test`.
|
|
2205
|
+
*
|
|
2206
|
+
* @internal
|
|
2207
|
+
*/
|
|
2208
|
+
declare function node_of(state: string, state_index: string[] | Map<string, number>): string;
|
|
2209
|
+
/**
|
|
2210
|
+
* Convert an 8-channel hex color (`#RRGGBBAA`) to a 6-channel hex color
|
|
2211
|
+
* (`#RRGGBB`), discarding the alpha channel. Throws if the input is not
|
|
2212
|
+
* a 9-character `#`-prefixed string.
|
|
2213
|
+
*
|
|
2214
|
+
* Graphviz dot does not support alpha; this is a lossy projection.
|
|
2215
|
+
*
|
|
2216
|
+
* @internal
|
|
2217
|
+
*/
|
|
2218
|
+
declare function color8to6(color8: string): string;
|
|
2219
|
+
/**
|
|
2220
|
+
* Variant of {@link color8to6} that passes `undefined` through.
|
|
2221
|
+
*
|
|
2222
|
+
* @internal
|
|
2223
|
+
*/
|
|
2224
|
+
declare function u_color8to6(color8?: string): string | undefined;
|
|
2225
|
+
/**
|
|
2226
|
+
* Read the graphviz shape for a state through {@link jssm.Machine.style_for},
|
|
2227
|
+
* so theme-supplied shapes are honoured along with per-state declarations.
|
|
2228
|
+
* Returns `undefined` if neither a theme nor a state declaration supplies a
|
|
2229
|
+
* shape.
|
|
2230
|
+
*
|
|
2231
|
+
* @internal
|
|
2232
|
+
*/
|
|
2233
|
+
declare function shape_for_state<T>(u_jssm: Machine<T>, state: string): string | undefined;
|
|
2234
|
+
/**
|
|
2235
|
+
* Read the image filename for a state through {@link jssm.Machine.style_for},
|
|
2236
|
+
* so theme-supplied images are honoured along with per-state declarations.
|
|
2237
|
+
* Returns `undefined` if neither a theme nor a state declaration supplies an
|
|
2238
|
+
* image.
|
|
2239
|
+
*
|
|
2240
|
+
* @internal
|
|
2241
|
+
*/
|
|
2242
|
+
declare function image_for_state<T>(u_jssm: Machine<T>, state: string): string | undefined;
|
|
2243
|
+
/**
|
|
2244
|
+
* Compose a graphviz `style` string for a state by looking up its merged
|
|
2245
|
+
* style via {@link jssm.Machine.style_for}, then delegating to
|
|
2246
|
+
* {@link compose_style_string}. Theme-supplied `corners` and `lineStyle`
|
|
2247
|
+
* are honoured along with per-state declarations.
|
|
2248
|
+
*
|
|
2249
|
+
* @internal
|
|
2250
|
+
*/
|
|
2251
|
+
declare function style_for_state<T>(u_jssm: Machine<T>, state: string): string;
|
|
2252
|
+
/**
|
|
2253
|
+
* Render a {@link jssm.Machine} as a graphviz dot string.
|
|
2254
|
+
*
|
|
2255
|
+
* ```typescript
|
|
2256
|
+
* import { sm } from 'jssm';
|
|
2257
|
+
* import { machine_to_dot } from 'jssm/viz';
|
|
2258
|
+
*
|
|
2259
|
+
* const dot = machine_to_dot(sm`a -> b;`);
|
|
2260
|
+
* // 'digraph G { ... }'
|
|
2261
|
+
* ```
|
|
2262
|
+
*
|
|
2263
|
+
* @param u_jssm The machine to render.
|
|
2264
|
+
* @returns A complete graphviz dot source string.
|
|
2265
|
+
*/
|
|
2266
|
+
declare function machine_to_dot<T>(u_jssm: Machine<T>): string;
|
|
2267
|
+
/**
|
|
2268
|
+
* Render an FSL string directly to graphviz dot source.
|
|
2269
|
+
*
|
|
2270
|
+
* ```typescript
|
|
2271
|
+
* import { fsl_to_dot } from 'jssm/viz';
|
|
2272
|
+
* const dot = fsl_to_dot('a -> b;');
|
|
2273
|
+
* ```
|
|
2274
|
+
*
|
|
2275
|
+
* @param fsl The FSL source.
|
|
2276
|
+
* @returns A complete graphviz dot source string.
|
|
2277
|
+
*/
|
|
2278
|
+
declare function fsl_to_dot(fsl: string): string;
|
|
2279
|
+
/**
|
|
2280
|
+
* Render a graphviz dot source string to SVG using `@viz-js/viz`. The
|
|
2281
|
+
* underlying viz instance is lazy-initialized on first call and cached for
|
|
2282
|
+
* the lifetime of the module.
|
|
2283
|
+
*
|
|
2284
|
+
* ```typescript
|
|
2285
|
+
* const svg = await dot_to_svg('digraph G { a -> b }');
|
|
2286
|
+
* ```
|
|
2287
|
+
*
|
|
2288
|
+
* @param dot Graphviz dot source.
|
|
2289
|
+
* @returns A promise resolving to an SVG XML string.
|
|
2290
|
+
*/
|
|
2291
|
+
declare function dot_to_svg(dot: string): Promise<string>;
|
|
2292
|
+
/**
|
|
2293
|
+
* Render an FSL string directly to SVG.
|
|
2294
|
+
*
|
|
2295
|
+
* @param fsl The FSL source.
|
|
2296
|
+
* @returns A promise resolving to an SVG XML string.
|
|
2297
|
+
*/
|
|
2298
|
+
declare function fsl_to_svg_string(fsl: string): Promise<string>;
|
|
2299
|
+
/**
|
|
2300
|
+
* Render a {@link jssm.Machine} to SVG.
|
|
2301
|
+
*
|
|
2302
|
+
* @param u_jssm The machine to render.
|
|
2303
|
+
* @returns A promise resolving to an SVG XML string.
|
|
2304
|
+
*/
|
|
2305
|
+
declare function machine_to_svg_string<T>(u_jssm: Machine<T>): Promise<string>;
|
|
2306
|
+
/**
|
|
2307
|
+
* Render an FSL string directly to a parsed `SVGSVGElement`.
|
|
2308
|
+
*
|
|
2309
|
+
* @param fsl The FSL source.
|
|
2310
|
+
* @returns A promise resolving to a parsed `SVGSVGElement`.
|
|
2311
|
+
* @throws {JssmError} if no `DOMParser` is available (Node without `configure`).
|
|
2312
|
+
*/
|
|
2313
|
+
declare function fsl_to_svg_element(fsl: string): Promise<SVGSVGElement>;
|
|
2314
|
+
/**
|
|
2315
|
+
* Render a {@link jssm.Machine} to a parsed `SVGSVGElement`.
|
|
2316
|
+
*
|
|
2317
|
+
* @param u_jssm The machine to render.
|
|
2318
|
+
* @returns A promise resolving to a parsed `SVGSVGElement`.
|
|
2319
|
+
* @throws {JssmError} if no `DOMParser` is available (Node without `configure`).
|
|
2320
|
+
*/
|
|
2321
|
+
declare function machine_to_svg_element<T>(u_jssm: Machine<T>): Promise<SVGSVGElement>;
|
|
2322
|
+
/**
|
|
2323
|
+
* Compatibility wrapper for {@link machine_to_dot}, retained from
|
|
2324
|
+
* jssm-viz. Will be removed in the next major.
|
|
2325
|
+
*
|
|
2326
|
+
* @deprecated Use {@link machine_to_dot} instead.
|
|
2327
|
+
*/
|
|
2328
|
+
declare function dot<T>(machine: Machine<T>): string;
|
|
2329
|
+
|
|
2330
|
+
/** @internal — test-only access to private helpers. */
|
|
2331
|
+
declare const _test: {
|
|
2332
|
+
color8to6: typeof color8to6;
|
|
2333
|
+
u_color8to6: typeof u_color8to6;
|
|
2334
|
+
vc: typeof vc;
|
|
2335
|
+
node_of: typeof node_of;
|
|
2336
|
+
shape_for_state: typeof shape_for_state;
|
|
2337
|
+
image_for_state: typeof image_for_state;
|
|
2338
|
+
style_for_state: typeof style_for_state;
|
|
2339
|
+
};
|
|
2340
|
+
|
|
2341
|
+
export { _test, build_time, configure, dot, dot_to_svg, fsl_to_dot, fsl_to_svg_element, fsl_to_svg_string, machine_to_dot, machine_to_svg_element, machine_to_svg_string, version };
|