jssm 5.77.0 → 5.79.0
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/CHANGELOG.md +32 -44
- package/README.md +2 -2
- package/dist/es6/jssm-dot.js +1 -1
- package/dist/es6/jssm.d.ts +165 -12
- package/dist/es6/jssm.js +266 -18
- package/dist/es6/jssm_types.d.ts +27 -3
- package/dist/es6/jssm_util.d.ts +44 -1
- package/dist/es6/jssm_util.js +68 -1
- package/dist/es6/version.js +1 -1
- package/dist/jssm.es5.cjs.js +1 -1
- package/dist/jssm.es5.iife.js +1 -1
- package/jssm.d.ts +165 -12
- package/jssm_types.d.ts +27 -3
- package/jssm_util.d.ts +44 -1
- package/package.json +1 -1
package/dist/es6/jssm.d.ts
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
declare type StateType = string;
|
|
2
|
-
import { circular_buffer } from 'circular_buffer_js';
|
|
3
2
|
import { JssmGenericState, JssmGenericConfig, JssmTransition, JssmTransitionList, // JssmTransitionRule,
|
|
4
|
-
JssmMachineInternalState, JssmParseTree, JssmStateDeclaration, JssmArrow, JssmArrowDirection, JssmArrowKind, JssmLayout, FslDirection, FslTheme, HookDescription, HookHandler, HookContext, HookResult, HookComplexResult } from './jssm_types';
|
|
5
|
-
import { seq, weighted_rand_select, weighted_sample_select, histograph, weighted_histo_key } from './jssm_util';
|
|
3
|
+
JssmMachineInternalState, JssmParseTree, JssmStateDeclaration, JssmArrow, JssmArrowDirection, JssmArrowKind, JssmLayout, JssmHistory, JssmSerialization, FslDirection, FslTheme, HookDescription, HookHandler, HookContext, HookResult, HookComplexResult } from './jssm_types';
|
|
4
|
+
import { seq, unique, find_repeated, weighted_rand_select, weighted_sample_select, histograph, weighted_histo_key } from './jssm_util';
|
|
6
5
|
import { shapes, gviz_shapes, named_colors } from './jssm_constants';
|
|
7
6
|
import { version } from './version';
|
|
8
7
|
/*********
|
|
@@ -251,9 +250,12 @@ declare class Machine<mDT> {
|
|
|
251
250
|
_post_main_transition_hook: HookHandler<mDT> | undefined;
|
|
252
251
|
_post_forced_transition_hook: HookHandler<mDT> | undefined;
|
|
253
252
|
_post_any_transition_hook: HookHandler<mDT> | undefined;
|
|
254
|
-
|
|
253
|
+
_property_keys: Set<string>;
|
|
254
|
+
_default_properties: Map<string, any>;
|
|
255
|
+
_state_properties: Map<string, any>;
|
|
256
|
+
_history: JssmHistory<mDT>;
|
|
255
257
|
_history_length: number;
|
|
256
|
-
constructor({ start_states, complete, transitions, machine_author, machine_comment, machine_contributor, machine_definition, machine_language, machine_license, machine_name, machine_version, state_declaration, fsl_version, dot_preamble, arrange_declaration, arrange_start_declaration, arrange_end_declaration, theme, flow, graph_layout, instance_name, history, data }: JssmGenericConfig<mDT>);
|
|
258
|
+
constructor({ start_states, 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 }: JssmGenericConfig<mDT>);
|
|
257
259
|
/********
|
|
258
260
|
*
|
|
259
261
|
* Internal method for fabricating states. Not meant for external use.
|
|
@@ -295,6 +297,105 @@ declare class Machine<mDT> {
|
|
|
295
297
|
*
|
|
296
298
|
*/
|
|
297
299
|
data(): mDT;
|
|
300
|
+
/*********
|
|
301
|
+
*
|
|
302
|
+
* Get the current value of a given property name.
|
|
303
|
+
*
|
|
304
|
+
* ```typescript
|
|
305
|
+
*
|
|
306
|
+
* ```
|
|
307
|
+
*
|
|
308
|
+
* @param name The relevant property name to look up
|
|
309
|
+
*
|
|
310
|
+
* @returns The value behind the prop name. Because functional props are
|
|
311
|
+
* evaluated as getters, this can be anything.
|
|
312
|
+
*
|
|
313
|
+
*/
|
|
314
|
+
prop(name: string): any;
|
|
315
|
+
/*********
|
|
316
|
+
*
|
|
317
|
+
* Get the current value of every prop, as an object. If no current definition
|
|
318
|
+
* exists for a prop - that is, if the prop was defined without a default and
|
|
319
|
+
* the current state also doesn't define the prop - then that prop will be listed
|
|
320
|
+
* in the returned object with a value of `undefined`.
|
|
321
|
+
*
|
|
322
|
+
* ```typescript
|
|
323
|
+
* const traffic_light = sm`
|
|
324
|
+
*
|
|
325
|
+
* property can_go default true;
|
|
326
|
+
* property hesitate default true;
|
|
327
|
+
* property stop_first default false;
|
|
328
|
+
*
|
|
329
|
+
* Off -> Red => Green => Yellow => Red;
|
|
330
|
+
* [Red Yellow Green] ~> [Off FlashingRed];
|
|
331
|
+
* FlashingRed -> Red;
|
|
332
|
+
*
|
|
333
|
+
* state Red: { property stop_first true; property can_go false; };
|
|
334
|
+
* state Off: { property stop_first true; };
|
|
335
|
+
* state FlashingRed: { property stop_first true; };
|
|
336
|
+
* state Green: { property hesitate false; };
|
|
337
|
+
*
|
|
338
|
+
* `;
|
|
339
|
+
*
|
|
340
|
+
* traffic_light.state(); // Off
|
|
341
|
+
* traffic_light.props(); // { can_go: true, hesitate: true, stop_first: true; }
|
|
342
|
+
*
|
|
343
|
+
* traffic_light.go('Red');
|
|
344
|
+
* traffic_light.props(); // { can_go: false, hesitate: true, stop_first: true; }
|
|
345
|
+
*
|
|
346
|
+
* traffic_light.go('Green');
|
|
347
|
+
* traffic_light.props(); // { can_go: true, hesitate: false, stop_first: false; }
|
|
348
|
+
* ```
|
|
349
|
+
*
|
|
350
|
+
*/
|
|
351
|
+
props(): object;
|
|
352
|
+
/*********
|
|
353
|
+
*
|
|
354
|
+
* Get the current value of every prop, as an object. Compare
|
|
355
|
+
* {@link prop_map}, which returns a `Map`.
|
|
356
|
+
*
|
|
357
|
+
* ```typescript
|
|
358
|
+
*
|
|
359
|
+
* ```
|
|
360
|
+
*
|
|
361
|
+
*/
|
|
362
|
+
/*********
|
|
363
|
+
*
|
|
364
|
+
* Get the current value of every prop, as an object. Compare
|
|
365
|
+
* {@link prop_map}, which returns a `Map`. Akin to {@link strict_prop},
|
|
366
|
+
* this throws if a required prop is missing.
|
|
367
|
+
*
|
|
368
|
+
* ```typescript
|
|
369
|
+
*
|
|
370
|
+
* ```
|
|
371
|
+
*
|
|
372
|
+
*/
|
|
373
|
+
/*********
|
|
374
|
+
*
|
|
375
|
+
* Check whether a given string is a known property's name.
|
|
376
|
+
*
|
|
377
|
+
* ```typescript
|
|
378
|
+
* const example = sm`property foo default 1; a->b;`;
|
|
379
|
+
*
|
|
380
|
+
* example.known_prop('foo'); // true
|
|
381
|
+
* example.known_prop('bar'); // false
|
|
382
|
+
* ```
|
|
383
|
+
*
|
|
384
|
+
* @param prop_name The relevant property name to look up
|
|
385
|
+
*
|
|
386
|
+
*/
|
|
387
|
+
known_prop(prop_name: string): boolean;
|
|
388
|
+
/*********
|
|
389
|
+
*
|
|
390
|
+
* List all known property names. If you'd also like values, use
|
|
391
|
+
* {@link props} instead. The order of the properties is not defined, and
|
|
392
|
+
* the properties generally will not be sorted.
|
|
393
|
+
*
|
|
394
|
+
* ```typescript
|
|
395
|
+
* ```
|
|
396
|
+
*
|
|
397
|
+
*/
|
|
398
|
+
known_props(): string[];
|
|
298
399
|
/********
|
|
299
400
|
*
|
|
300
401
|
* Check whether a given state is final (either has no exits or is marked
|
|
@@ -330,10 +431,19 @@ declare class Machine<mDT> {
|
|
|
330
431
|
* console.log( final_test.is_final() ); // true
|
|
331
432
|
* ```
|
|
332
433
|
*
|
|
434
|
+
*/
|
|
435
|
+
is_final(): boolean;
|
|
436
|
+
/********
|
|
437
|
+
*
|
|
438
|
+
* Serialize the current machine, including all defining state but not the
|
|
439
|
+
* machine string, to a structure. This means you will need the machine
|
|
440
|
+
* string to recreate (to not waste repeated space;) if you want the machine
|
|
441
|
+
* string embedded, call {@link serialize_with_string} instead.
|
|
442
|
+
*
|
|
333
443
|
* @typeparam mDT The type of the machine data member; usually omitted
|
|
334
444
|
*
|
|
335
445
|
*/
|
|
336
|
-
|
|
446
|
+
serialize(comment?: string | undefined): JssmSerialization<mDT>;
|
|
337
447
|
graph_layout(): string;
|
|
338
448
|
dot_preamble(): string;
|
|
339
449
|
machine_author(): Array<string>;
|
|
@@ -665,7 +775,7 @@ declare class Machine<mDT> {
|
|
|
665
775
|
set history_length(to: number);
|
|
666
776
|
/********
|
|
667
777
|
*
|
|
668
|
-
* Instruct the machine to complete an action.
|
|
778
|
+
* Instruct the machine to complete an action. Synonym for {@link do}.
|
|
669
779
|
*
|
|
670
780
|
* ```typescript
|
|
671
781
|
* const light = sm`red 'next' -> green 'next' -> yellow 'next' -> red; [red yellow green] 'shutdown' ~> off 'start' -> red;`;
|
|
@@ -683,16 +793,58 @@ declare class Machine<mDT> {
|
|
|
683
793
|
*
|
|
684
794
|
*/
|
|
685
795
|
action(actionName: StateType, newData?: mDT): boolean;
|
|
796
|
+
/********
|
|
797
|
+
*
|
|
798
|
+
* Instruct the machine to complete an action. Synonym for {@link action}.
|
|
799
|
+
*
|
|
800
|
+
* ```typescript
|
|
801
|
+
* const light = sm`
|
|
802
|
+
* off 'start' -> red;
|
|
803
|
+
* red 'next' -> green 'next' -> yellow 'next' -> red;
|
|
804
|
+
* [red yellow green] 'shutdown' ~> off;
|
|
805
|
+
* `;
|
|
806
|
+
*
|
|
807
|
+
* light.state(); // 'off'
|
|
808
|
+
* light.do('start'); // true
|
|
809
|
+
* light.state(); // 'red'
|
|
810
|
+
* light.do('next'); // true
|
|
811
|
+
* light.state(); // 'green'
|
|
812
|
+
* light.do('next'); // true
|
|
813
|
+
* light.state(); // 'yellow'
|
|
814
|
+
* light.do('dance'); // !! false - no such action
|
|
815
|
+
* light.state(); // 'yellow'
|
|
816
|
+
* light.do('start'); // !! false - yellow does not have the action start
|
|
817
|
+
* light.state(); // 'yellow'
|
|
818
|
+
* ```
|
|
819
|
+
*
|
|
820
|
+
* @typeparam mDT The type of the machine data member; usually omitted
|
|
821
|
+
b *
|
|
822
|
+
* @param actionName The action to engage
|
|
823
|
+
*
|
|
824
|
+
* @param newData The data change to insert during the action
|
|
825
|
+
*
|
|
826
|
+
*/
|
|
827
|
+
do(actionName: StateType, newData?: mDT): boolean;
|
|
686
828
|
/********
|
|
687
829
|
*
|
|
688
830
|
* Instruct the machine to complete a transition. Synonym for {@link go}.
|
|
689
831
|
*
|
|
690
832
|
* ```typescript
|
|
691
|
-
* const light = sm`
|
|
833
|
+
* const light = sm`
|
|
834
|
+
* off 'start' -> red;
|
|
835
|
+
* red 'next' -> green 'next' -> yellow 'next' -> red;
|
|
836
|
+
* [red yellow green] 'shutdown' ~> off;
|
|
837
|
+
* `;
|
|
692
838
|
*
|
|
693
|
-
* light.state();
|
|
694
|
-
* light.
|
|
695
|
-
* light.state();
|
|
839
|
+
* light.state(); // 'off'
|
|
840
|
+
* light.go('red'); // true
|
|
841
|
+
* light.state(); // 'red'
|
|
842
|
+
* light.go('green'); // true
|
|
843
|
+
* light.state(); // 'green'
|
|
844
|
+
* light.go('blue'); // !! false - no such state
|
|
845
|
+
* light.state(); // 'green'
|
|
846
|
+
* light.go('red'); // !! false - green may not go directly to red, only to yellow
|
|
847
|
+
* light.state(); // 'green'
|
|
696
848
|
* ```
|
|
697
849
|
*
|
|
698
850
|
* @typeparam mDT The type of the machine data member; usually omitted
|
|
@@ -804,4 +956,5 @@ declare function from<mDT>(MachineAsString: string, ExtraConstructorFields?: Par
|
|
|
804
956
|
declare function is_hook_complex_result<mDT>(hr: unknown): hr is HookComplexResult<mDT>;
|
|
805
957
|
declare function is_hook_rejection<mDT>(hr: HookResult<mDT>): boolean;
|
|
806
958
|
declare function abstract_hook_step<mDT>(maybe_hook: HookHandler<mDT> | undefined, hook_args: HookContext<mDT>): HookComplexResult<mDT>;
|
|
807
|
-
|
|
959
|
+
declare function deserialize<mDT>(machine_string: string, ser: JssmSerialization<mDT>): Machine<mDT>;
|
|
960
|
+
export { version, transfer_state_properties, Machine, deserialize, make, wrap_parse as parse, compile, sm, from, arrow_direction, arrow_left_kind, arrow_right_kind, seq, unique, find_repeated, weighted_rand_select, histograph, weighted_sample_select, weighted_histo_key, shapes, gviz_shapes, named_colors, is_hook_rejection, is_hook_complex_result, abstract_hook_step };
|
package/dist/es6/jssm.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
// whargarbl lots of these return arrays could/should be sets
|
|
2
2
|
import { reduce as reduce_to_639 } from 'reduce-to-639-1';
|
|
3
3
|
import { circular_buffer } from 'circular_buffer_js';
|
|
4
|
-
import { seq, weighted_rand_select, weighted_sample_select, histograph, weighted_histo_key, array_box_if_string, hook_name, named_hook_name } from './jssm_util';
|
|
4
|
+
import { seq, unique, find_repeated, weighted_rand_select, weighted_sample_select, histograph, weighted_histo_key, array_box_if_string, name_bind_prop_and_state, hook_name, named_hook_name } from './jssm_util';
|
|
5
5
|
import { shapes, gviz_shapes, named_colors } from './jssm_constants';
|
|
6
6
|
import { parse } from './jssm-dot';
|
|
7
7
|
import { version } from './version'; // replaced from package.js in build
|
|
@@ -351,6 +351,16 @@ function compile_rule_handler(rule) {
|
|
|
351
351
|
if (rule.key === 'machine_language') {
|
|
352
352
|
return { agg_as: 'machine_language', val: reduce_to_639(rule.value) };
|
|
353
353
|
}
|
|
354
|
+
// manually rehandled to make `undefined` as a property safe
|
|
355
|
+
if (rule.key === 'property_definition') {
|
|
356
|
+
if (rule.hasOwnProperty('default_value')) {
|
|
357
|
+
return { agg_as: 'property_definition', val: { name: rule.name, default_value: rule.default_value } };
|
|
358
|
+
}
|
|
359
|
+
else {
|
|
360
|
+
return { agg_as: 'property_definition', val: { name: rule.name } };
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
// state properties are in here
|
|
354
364
|
if (rule.key === 'state_declaration') {
|
|
355
365
|
if (!rule.name) {
|
|
356
366
|
throw new JssmError(undefined, 'State declarations must have a name');
|
|
@@ -361,6 +371,7 @@ function compile_rule_handler(rule) {
|
|
|
361
371
|
'arrange_end_declaration'].includes(rule.key)) {
|
|
362
372
|
return { agg_as: rule.key, val: [rule.value] };
|
|
363
373
|
}
|
|
374
|
+
// things that can only exist once and are just a value under their own name
|
|
364
375
|
const tautologies = [
|
|
365
376
|
'graph_layout', 'start_states', 'end_states', 'machine_name', 'machine_version',
|
|
366
377
|
'machine_comment', 'machine_author', 'machine_contributor', 'machine_definition',
|
|
@@ -440,6 +451,8 @@ function compile(tree) {
|
|
|
440
451
|
machine_license: [],
|
|
441
452
|
machine_name: [],
|
|
442
453
|
machine_reference: [],
|
|
454
|
+
property_definition: [],
|
|
455
|
+
state_property: {},
|
|
443
456
|
theme: [],
|
|
444
457
|
flow: [],
|
|
445
458
|
dot_preamble: [],
|
|
@@ -452,10 +465,15 @@ function compile(tree) {
|
|
|
452
465
|
const rule = compile_rule_handler(tr), agg_as = rule.agg_as, val = rule.val; // TODO FIXME no any
|
|
453
466
|
results[agg_as] = results[agg_as].concat(val);
|
|
454
467
|
});
|
|
468
|
+
const property_keys = results['property_definition'].map(pd => pd.name), repeat_props = find_repeated(property_keys);
|
|
469
|
+
if (repeat_props.length) {
|
|
470
|
+
throw new JssmError(undefined, `Cannot repeat property definitions. Saw ${JSON.stringify(repeat_props)}`);
|
|
471
|
+
}
|
|
455
472
|
const assembled_transitions = [].concat(...results['transition']);
|
|
456
473
|
const result_cfg = {
|
|
457
474
|
start_states: results.start_states.length ? results.start_states : [assembled_transitions[0].from],
|
|
458
|
-
transitions: assembled_transitions
|
|
475
|
+
transitions: assembled_transitions,
|
|
476
|
+
state_property: []
|
|
459
477
|
};
|
|
460
478
|
const oneOnlyKeys = [
|
|
461
479
|
'graph_layout', 'machine_name', 'machine_version', 'machine_comment',
|
|
@@ -473,11 +491,28 @@ function compile(tree) {
|
|
|
473
491
|
}
|
|
474
492
|
});
|
|
475
493
|
['arrange_declaration', 'arrange_start_declaration', 'arrange_end_declaration',
|
|
476
|
-
'machine_author', 'machine_contributor', 'machine_reference',
|
|
494
|
+
'machine_author', 'machine_contributor', 'machine_reference',
|
|
495
|
+
'state_declaration', 'property_definition'].map((multiKey) => {
|
|
477
496
|
if (results[multiKey].length) {
|
|
478
497
|
result_cfg[multiKey] = results[multiKey];
|
|
479
498
|
}
|
|
480
499
|
});
|
|
500
|
+
// re-walk state declarations, already wrapped up, to get state properties,
|
|
501
|
+
// which go out in a different datastructure
|
|
502
|
+
results.state_declaration.forEach(sd => {
|
|
503
|
+
sd.declarations.forEach(decl => {
|
|
504
|
+
if (decl.key === 'state_property') {
|
|
505
|
+
const label = name_bind_prop_and_state(decl.name, sd.state);
|
|
506
|
+
console.log(`Bind ${sd.state}:${decl.name} as ${label}`);
|
|
507
|
+
if (result_cfg.state_property.findIndex(c => c.name === label) !== -1) {
|
|
508
|
+
throw new JssmError(undefined, `A state may only bind a property once (${sd.state} re-binds ${decl.name})`);
|
|
509
|
+
}
|
|
510
|
+
else {
|
|
511
|
+
result_cfg.state_property.push({ name: label, default_value: decl.value });
|
|
512
|
+
}
|
|
513
|
+
}
|
|
514
|
+
});
|
|
515
|
+
});
|
|
481
516
|
return result_cfg;
|
|
482
517
|
}
|
|
483
518
|
/*********
|
|
@@ -527,6 +562,9 @@ function transfer_state_properties(state_decl) {
|
|
|
527
562
|
case 'border-color':
|
|
528
563
|
state_decl.borderColor = d.value;
|
|
529
564
|
break;
|
|
565
|
+
case 'state_property':
|
|
566
|
+
state_decl.property = { name: d.name, value: d.value };
|
|
567
|
+
break;
|
|
530
568
|
default: throw new JssmError(undefined, `Unknown state property: '${JSON.stringify(d)}'`);
|
|
531
569
|
}
|
|
532
570
|
});
|
|
@@ -535,7 +573,7 @@ function transfer_state_properties(state_decl) {
|
|
|
535
573
|
// TODO add a lotta docblock here
|
|
536
574
|
class Machine {
|
|
537
575
|
// whargarbl this badly needs to be broken up, monolith master
|
|
538
|
-
constructor({ start_states, complete = [], transitions, machine_author, machine_comment, machine_contributor, machine_definition, machine_language, machine_license, machine_name, machine_version, state_declaration, fsl_version, dot_preamble = undefined, arrange_declaration = [], arrange_start_declaration = [], arrange_end_declaration = [], theme = 'default', flow = 'down', graph_layout = 'dot', instance_name, history, data }) {
|
|
576
|
+
constructor({ start_states, 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 = undefined, arrange_declaration = [], arrange_start_declaration = [], arrange_end_declaration = [], theme = 'default', flow = 'down', graph_layout = 'dot', instance_name, history, data }) {
|
|
539
577
|
this._instance_name = instance_name;
|
|
540
578
|
this._state = start_states[0];
|
|
541
579
|
this._states = new Map();
|
|
@@ -600,6 +638,9 @@ class Machine {
|
|
|
600
638
|
this._post_forced_transition_hook = undefined;
|
|
601
639
|
this._post_any_transition_hook = undefined;
|
|
602
640
|
this._data = data;
|
|
641
|
+
this._property_keys = new Set();
|
|
642
|
+
this._default_properties = new Map();
|
|
643
|
+
this._state_properties = new Map();
|
|
603
644
|
this._history_length = history || 0;
|
|
604
645
|
this._history = new circular_buffer(this._history_length);
|
|
605
646
|
if (state_declaration) {
|
|
@@ -697,6 +738,19 @@ class Machine {
|
|
|
697
738
|
*/
|
|
698
739
|
}
|
|
699
740
|
});
|
|
741
|
+
if (Array.isArray(property_definition)) {
|
|
742
|
+
property_definition.forEach(pr => {
|
|
743
|
+
this._property_keys.add(pr.name);
|
|
744
|
+
if (pr.hasOwnProperty('default_value')) {
|
|
745
|
+
this._default_properties.set(pr.name, pr.default_value);
|
|
746
|
+
}
|
|
747
|
+
});
|
|
748
|
+
}
|
|
749
|
+
if (Array.isArray(state_property)) {
|
|
750
|
+
state_property.forEach(sp => {
|
|
751
|
+
this._state_properties.set(sp.name, sp.default_value);
|
|
752
|
+
});
|
|
753
|
+
}
|
|
700
754
|
}
|
|
701
755
|
/********
|
|
702
756
|
*
|
|
@@ -763,6 +817,136 @@ class Machine {
|
|
|
763
817
|
return true; // todo whargarbl
|
|
764
818
|
}
|
|
765
819
|
*/
|
|
820
|
+
// NEEDS_DOCS
|
|
821
|
+
/*********
|
|
822
|
+
*
|
|
823
|
+
* Get the current value of a given property name.
|
|
824
|
+
*
|
|
825
|
+
* ```typescript
|
|
826
|
+
*
|
|
827
|
+
* ```
|
|
828
|
+
*
|
|
829
|
+
* @param name The relevant property name to look up
|
|
830
|
+
*
|
|
831
|
+
* @returns The value behind the prop name. Because functional props are
|
|
832
|
+
* evaluated as getters, this can be anything.
|
|
833
|
+
*
|
|
834
|
+
*/
|
|
835
|
+
prop(name) {
|
|
836
|
+
const bound_name = name_bind_prop_and_state(name, this.state());
|
|
837
|
+
if (this._state_properties.has(bound_name)) {
|
|
838
|
+
return this._state_properties.get(bound_name);
|
|
839
|
+
}
|
|
840
|
+
else if (this._default_properties.has(name)) {
|
|
841
|
+
return this._default_properties.get(name);
|
|
842
|
+
}
|
|
843
|
+
else {
|
|
844
|
+
return undefined;
|
|
845
|
+
}
|
|
846
|
+
}
|
|
847
|
+
// NEEDS_DOCS
|
|
848
|
+
// COMEBACK add prop_map, sparse_props and strict_props to doc text when implemented
|
|
849
|
+
/*********
|
|
850
|
+
*
|
|
851
|
+
* Get the current value of every prop, as an object. If no current definition
|
|
852
|
+
* exists for a prop - that is, if the prop was defined without a default and
|
|
853
|
+
* the current state also doesn't define the prop - then that prop will be listed
|
|
854
|
+
* in the returned object with a value of `undefined`.
|
|
855
|
+
*
|
|
856
|
+
* ```typescript
|
|
857
|
+
* const traffic_light = sm`
|
|
858
|
+
*
|
|
859
|
+
* property can_go default true;
|
|
860
|
+
* property hesitate default true;
|
|
861
|
+
* property stop_first default false;
|
|
862
|
+
*
|
|
863
|
+
* Off -> Red => Green => Yellow => Red;
|
|
864
|
+
* [Red Yellow Green] ~> [Off FlashingRed];
|
|
865
|
+
* FlashingRed -> Red;
|
|
866
|
+
*
|
|
867
|
+
* state Red: { property stop_first true; property can_go false; };
|
|
868
|
+
* state Off: { property stop_first true; };
|
|
869
|
+
* state FlashingRed: { property stop_first true; };
|
|
870
|
+
* state Green: { property hesitate false; };
|
|
871
|
+
*
|
|
872
|
+
* `;
|
|
873
|
+
*
|
|
874
|
+
* traffic_light.state(); // Off
|
|
875
|
+
* traffic_light.props(); // { can_go: true, hesitate: true, stop_first: true; }
|
|
876
|
+
*
|
|
877
|
+
* traffic_light.go('Red');
|
|
878
|
+
* traffic_light.props(); // { can_go: false, hesitate: true, stop_first: true; }
|
|
879
|
+
*
|
|
880
|
+
* traffic_light.go('Green');
|
|
881
|
+
* traffic_light.props(); // { can_go: true, hesitate: false, stop_first: false; }
|
|
882
|
+
* ```
|
|
883
|
+
*
|
|
884
|
+
*/
|
|
885
|
+
props() {
|
|
886
|
+
const ret = {};
|
|
887
|
+
this.known_props().forEach(p => ret[p] = this.prop(p));
|
|
888
|
+
return ret;
|
|
889
|
+
}
|
|
890
|
+
// NEEDS_DOCS
|
|
891
|
+
// TODO COMEBACK
|
|
892
|
+
/*********
|
|
893
|
+
*
|
|
894
|
+
* Get the current value of every prop, as an object. Compare
|
|
895
|
+
* {@link prop_map}, which returns a `Map`.
|
|
896
|
+
*
|
|
897
|
+
* ```typescript
|
|
898
|
+
*
|
|
899
|
+
* ```
|
|
900
|
+
*
|
|
901
|
+
*/
|
|
902
|
+
// sparse_props(name: string): object {
|
|
903
|
+
// }
|
|
904
|
+
// NEEDS_DOCS
|
|
905
|
+
// TODO COMEBACK
|
|
906
|
+
/*********
|
|
907
|
+
*
|
|
908
|
+
* Get the current value of every prop, as an object. Compare
|
|
909
|
+
* {@link prop_map}, which returns a `Map`. Akin to {@link strict_prop},
|
|
910
|
+
* this throws if a required prop is missing.
|
|
911
|
+
*
|
|
912
|
+
* ```typescript
|
|
913
|
+
*
|
|
914
|
+
* ```
|
|
915
|
+
*
|
|
916
|
+
*/
|
|
917
|
+
// strict_props(name: string): object {
|
|
918
|
+
// }
|
|
919
|
+
/*********
|
|
920
|
+
*
|
|
921
|
+
* Check whether a given string is a known property's name.
|
|
922
|
+
*
|
|
923
|
+
* ```typescript
|
|
924
|
+
* const example = sm`property foo default 1; a->b;`;
|
|
925
|
+
*
|
|
926
|
+
* example.known_prop('foo'); // true
|
|
927
|
+
* example.known_prop('bar'); // false
|
|
928
|
+
* ```
|
|
929
|
+
*
|
|
930
|
+
* @param prop_name The relevant property name to look up
|
|
931
|
+
*
|
|
932
|
+
*/
|
|
933
|
+
known_prop(prop_name) {
|
|
934
|
+
return this._property_keys.has(prop_name);
|
|
935
|
+
}
|
|
936
|
+
// NEEDS_DOCS
|
|
937
|
+
/*********
|
|
938
|
+
*
|
|
939
|
+
* List all known property names. If you'd also like values, use
|
|
940
|
+
* {@link props} instead. The order of the properties is not defined, and
|
|
941
|
+
* the properties generally will not be sorted.
|
|
942
|
+
*
|
|
943
|
+
* ```typescript
|
|
944
|
+
* ```
|
|
945
|
+
*
|
|
946
|
+
*/
|
|
947
|
+
known_props() {
|
|
948
|
+
return [...this._property_keys];
|
|
949
|
+
}
|
|
766
950
|
/********
|
|
767
951
|
*
|
|
768
952
|
* Check whether a given state is final (either has no exits or is marked
|
|
@@ -800,13 +984,32 @@ class Machine {
|
|
|
800
984
|
* console.log( final_test.is_final() ); // true
|
|
801
985
|
* ```
|
|
802
986
|
*
|
|
803
|
-
* @typeparam mDT The type of the machine data member; usually omitted
|
|
804
|
-
*
|
|
805
987
|
*/
|
|
806
988
|
is_final() {
|
|
807
989
|
// return ((!this.is_changing()) && this.state_is_final(this.state()));
|
|
808
990
|
return this.state_is_final(this.state());
|
|
809
991
|
}
|
|
992
|
+
/********
|
|
993
|
+
*
|
|
994
|
+
* Serialize the current machine, including all defining state but not the
|
|
995
|
+
* machine string, to a structure. This means you will need the machine
|
|
996
|
+
* string to recreate (to not waste repeated space;) if you want the machine
|
|
997
|
+
* string embedded, call {@link serialize_with_string} instead.
|
|
998
|
+
*
|
|
999
|
+
* @typeparam mDT The type of the machine data member; usually omitted
|
|
1000
|
+
*
|
|
1001
|
+
*/
|
|
1002
|
+
serialize(comment) {
|
|
1003
|
+
return {
|
|
1004
|
+
comment,
|
|
1005
|
+
state: this._state,
|
|
1006
|
+
data: this._data,
|
|
1007
|
+
jssm_version: version,
|
|
1008
|
+
history: this._history.toArray(),
|
|
1009
|
+
history_capacity: this._history.capacity,
|
|
1010
|
+
timestamp: new Date().getTime(),
|
|
1011
|
+
};
|
|
1012
|
+
}
|
|
810
1013
|
graph_layout() {
|
|
811
1014
|
return this._graph_layout;
|
|
812
1015
|
}
|
|
@@ -862,11 +1065,6 @@ class Machine {
|
|
|
862
1065
|
states: this._states
|
|
863
1066
|
};
|
|
864
1067
|
}
|
|
865
|
-
/*
|
|
866
|
-
load_machine_state(): boolean {
|
|
867
|
-
return false; // todo whargarbl
|
|
868
|
-
}
|
|
869
|
-
*/
|
|
870
1068
|
/*********
|
|
871
1069
|
*
|
|
872
1070
|
* List all the states known by the machine. Please note that the order of
|
|
@@ -1724,7 +1922,7 @@ class Machine {
|
|
|
1724
1922
|
}
|
|
1725
1923
|
/********
|
|
1726
1924
|
*
|
|
1727
|
-
* Instruct the machine to complete an action.
|
|
1925
|
+
* Instruct the machine to complete an action. Synonym for {@link do}.
|
|
1728
1926
|
*
|
|
1729
1927
|
* ```typescript
|
|
1730
1928
|
* const light = sm`red 'next' -> green 'next' -> yellow 'next' -> red; [red yellow green] 'shutdown' ~> off 'start' -> red;`;
|
|
@@ -1744,16 +1942,60 @@ class Machine {
|
|
|
1744
1942
|
action(actionName, newData) {
|
|
1745
1943
|
return this.transition_impl(actionName, newData, false, true);
|
|
1746
1944
|
}
|
|
1945
|
+
/********
|
|
1946
|
+
*
|
|
1947
|
+
* Instruct the machine to complete an action. Synonym for {@link action}.
|
|
1948
|
+
*
|
|
1949
|
+
* ```typescript
|
|
1950
|
+
* const light = sm`
|
|
1951
|
+
* off 'start' -> red;
|
|
1952
|
+
* red 'next' -> green 'next' -> yellow 'next' -> red;
|
|
1953
|
+
* [red yellow green] 'shutdown' ~> off;
|
|
1954
|
+
* `;
|
|
1955
|
+
*
|
|
1956
|
+
* light.state(); // 'off'
|
|
1957
|
+
* light.do('start'); // true
|
|
1958
|
+
* light.state(); // 'red'
|
|
1959
|
+
* light.do('next'); // true
|
|
1960
|
+
* light.state(); // 'green'
|
|
1961
|
+
* light.do('next'); // true
|
|
1962
|
+
* light.state(); // 'yellow'
|
|
1963
|
+
* light.do('dance'); // !! false - no such action
|
|
1964
|
+
* light.state(); // 'yellow'
|
|
1965
|
+
* light.do('start'); // !! false - yellow does not have the action start
|
|
1966
|
+
* light.state(); // 'yellow'
|
|
1967
|
+
* ```
|
|
1968
|
+
*
|
|
1969
|
+
* @typeparam mDT The type of the machine data member; usually omitted
|
|
1970
|
+
b *
|
|
1971
|
+
* @param actionName The action to engage
|
|
1972
|
+
*
|
|
1973
|
+
* @param newData The data change to insert during the action
|
|
1974
|
+
*
|
|
1975
|
+
*/
|
|
1976
|
+
do(actionName, newData) {
|
|
1977
|
+
return this.transition_impl(actionName, newData, false, true);
|
|
1978
|
+
}
|
|
1747
1979
|
/********
|
|
1748
1980
|
*
|
|
1749
1981
|
* Instruct the machine to complete a transition. Synonym for {@link go}.
|
|
1750
1982
|
*
|
|
1751
1983
|
* ```typescript
|
|
1752
|
-
* const light = sm`
|
|
1984
|
+
* const light = sm`
|
|
1985
|
+
* off 'start' -> red;
|
|
1986
|
+
* red 'next' -> green 'next' -> yellow 'next' -> red;
|
|
1987
|
+
* [red yellow green] 'shutdown' ~> off;
|
|
1988
|
+
* `;
|
|
1753
1989
|
*
|
|
1754
|
-
* light.state();
|
|
1755
|
-
* light.
|
|
1756
|
-
* light.state();
|
|
1990
|
+
* light.state(); // 'off'
|
|
1991
|
+
* light.go('red'); // true
|
|
1992
|
+
* light.state(); // 'red'
|
|
1993
|
+
* light.go('green'); // true
|
|
1994
|
+
* light.state(); // 'green'
|
|
1995
|
+
* light.go('blue'); // !! false - no such state
|
|
1996
|
+
* light.state(); // 'green'
|
|
1997
|
+
* light.go('red'); // !! false - green may not go directly to red, only to yellow
|
|
1998
|
+
* light.state(); // 'green'
|
|
1757
1999
|
* ```
|
|
1758
2000
|
*
|
|
1759
2001
|
* @typeparam mDT The type of the machine data member; usually omitted
|
|
@@ -1966,6 +2208,12 @@ function abstract_hook_step(maybe_hook, hook_args) {
|
|
|
1966
2208
|
return { pass: true };
|
|
1967
2209
|
}
|
|
1968
2210
|
}
|
|
1969
|
-
|
|
2211
|
+
function deserialize(machine_string, ser) {
|
|
2212
|
+
const machine = from(machine_string, { data: ser.data, history: ser.history_capacity });
|
|
2213
|
+
machine._state = ser.state;
|
|
2214
|
+
ser.history.forEach(history_item => machine._history.push(history_item));
|
|
2215
|
+
return machine;
|
|
2216
|
+
}
|
|
2217
|
+
export { version, transfer_state_properties, Machine, deserialize, make, wrap_parse as parse, compile, sm, from, arrow_direction, arrow_left_kind, arrow_right_kind,
|
|
1970
2218
|
// WHARGARBL TODO these should be exported to a utility library
|
|
1971
|
-
seq, weighted_rand_select, histograph, weighted_sample_select, weighted_histo_key, shapes, gviz_shapes, named_colors, is_hook_rejection, is_hook_complex_result, abstract_hook_step };
|
|
2219
|
+
seq, unique, find_repeated, weighted_rand_select, histograph, weighted_sample_select, weighted_histo_key, shapes, gviz_shapes, named_colors, is_hook_rejection, is_hook_complex_result, abstract_hook_step };
|