jssm 5.153.0 → 5.154.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/dist/cdn/viz.js CHANGED
@@ -23588,7 +23588,7 @@ var constants = /*#__PURE__*/Object.freeze({
23588
23588
  * Useful for runtime diagnostics and for embedding in serialized machine
23589
23589
  * snapshots so that deserializers can detect version-skew.
23590
23590
  */
23591
- const version = "5.153.0";
23591
+ const version = "5.154.0";
23592
23592
 
23593
23593
  // whargarbl lots of these return arrays could/should be sets
23594
23594
  const { state_name_chars, state_name_first_chars, action_label_chars } = constants;
@@ -23903,6 +23903,10 @@ function find_connected_components(states, edges) {
23903
23903
  }
23904
23904
  return result;
23905
23905
  }
23906
+ /** Default number of independent Monte-Carlo runs when none is declared. */
23907
+ const STOCHASTIC_DEFAULT_RUNS = 1000;
23908
+ /** Default per-run step cap (montecarlo) / walk length (steady_state). */
23909
+ const STOCHASTIC_DEFAULT_MAX_STEPS = 1000;
23906
23910
  class Machine {
23907
23911
  // whargarbl this badly needs to be broken up, monolith master
23908
23912
  constructor({ start_states, end_states = [], failed_outputs = [], initial_state, start_states_no_enforce, complete = [], transitions, machine_author, machine_comment, machine_contributor, machine_definition, machine_language, machine_license, machine_name, machine_version, npm_name, default_size, state_declaration, property_definition, state_property, fsl_version, dot_preamble = undefined, arrange_declaration = [], arrange_start_declaration = [], arrange_end_declaration = [], oarrange_declaration = [], farrange_declaration = [], theme = ['default'], flow = 'down', graph_layout = 'dot', instance_name, history, boundary_depth_limit, data, default_state_config, default_active_state_config, default_hooked_state_config, default_terminal_state_config, default_start_state_config, default_end_state_config, default_transition_config, default_graph_config, group_registry, group_metadata, group_hooks, state_hooks, allows_override, config_allows_override, allow_islands, editor_config, rng_seed, time_source, timeout_source, clear_timeout_source }) {
@@ -25419,6 +25423,147 @@ class Machine {
25419
25423
  probabilistic_histo_walk(n) {
25420
25424
  return histograph(this.probabilistic_walk(n));
25421
25425
  }
25426
+ /** One non-destructive weighted-random walk over the graph from `start`.
25427
+ *
25428
+ * Reads the graph and advances the PRNG only — it never calls
25429
+ * {@link Machine.transition}, so it fires no hooks, mutates no machine
25430
+ * state, and touches no `data`. A state with no probabilistic exits
25431
+ * (a terminal, or a forced-only `~>` state) ends the walk.
25432
+ *
25433
+ * @param start - State to begin the walk from.
25434
+ * @param max_steps - Maximum transitions before the walk is step-capped.
25435
+ * @returns The {@link JssmStochasticRun} for this walk.
25436
+ */
25437
+ _stochastic_one_walk(start, max_steps) {
25438
+ const states = [start];
25439
+ const edges = [];
25440
+ let cur = start;
25441
+ let terminated = false;
25442
+ for (let step = 0; step < max_steps; step++) {
25443
+ const exits = this.probable_exits_for(cur);
25444
+ if (exits.length === 0) {
25445
+ terminated = true;
25446
+ break;
25447
+ }
25448
+ const selected = weighted_rand_select(exits, undefined, this._rng);
25449
+ edges.push(`${cur}→${selected.to}`);
25450
+ cur = selected.to;
25451
+ states.push(cur);
25452
+ }
25453
+ return { states, edges, length: states.length - 1, terminated };
25454
+ }
25455
+ /** Lazily yield one {@link JssmStochasticRun} at a time.
25456
+ *
25457
+ * In `montecarlo` mode (default) yields `runs` independent walks from the
25458
+ * current state, each ending at a terminal or after `max_steps`. In
25459
+ * `steady_state` mode yields exactly one walk of `max_steps` steps. This
25460
+ * is the lazy engine behind {@link Machine.stochastic_summary}; the
25461
+ * fsl-stochastic panel drives it across animation frames.
25462
+ *
25463
+ * Passing `seed` reseeds the machine for reproducible runs. Unlike
25464
+ * {@link Machine.stochastic_summary}, the generator does NOT restore the
25465
+ * prior seed afterward — a direct caller's machine is left reseeded.
25466
+ *
25467
+ * @param opts - {@link JssmStochasticOptions}.
25468
+ * @returns A generator of per-run results.
25469
+ *
25470
+ * @example
25471
+ * const m = sm`a 'go' -> b 'go' -> c;`;
25472
+ * [...m.stochastic_runs({ runs: 2, seed: 1 })].length; // => 2
25473
+ */
25474
+ *stochastic_runs(opts = {}) {
25475
+ var _a, _b, _c, _d, _e;
25476
+ if (opts.seed !== undefined) {
25477
+ this.rng_seed = opts.seed;
25478
+ }
25479
+ const mode = (_a = opts.mode) !== null && _a !== void 0 ? _a : 'montecarlo';
25480
+ const max_steps = (_b = opts.max_steps) !== null && _b !== void 0 ? _b : STOCHASTIC_DEFAULT_MAX_STEPS;
25481
+ const runs = (mode === 'steady_state')
25482
+ ? 1
25483
+ : ((_e = (_c = opts.runs) !== null && _c !== void 0 ? _c : (_d = this.editor_config()) === null || _d === void 0 ? void 0 : _d.stochastic_run_count) !== null && _e !== void 0 ? _e : STOCHASTIC_DEFAULT_RUNS);
25484
+ const start = this.state();
25485
+ for (let i = 0; i < runs; i++) {
25486
+ yield this._stochastic_one_walk(start, max_steps);
25487
+ }
25488
+ }
25489
+ /** Run many weighted-random walks and return aggregate statistics.
25490
+ *
25491
+ * Honors `%` transition probabilities (via the existing probabilistic
25492
+ * machinery). Non-destructive: the machine's current state and
25493
+ * {@link Machine.rng_seed} are restored before returning, so calling this
25494
+ * never perturbs the live machine. `montecarlo` mode (default) reports
25495
+ * per-run `path_lengths`, `terminal_reached`, and `capped`; `steady_state`
25496
+ * mode runs one long walk and omits those fields.
25497
+ *
25498
+ * Timing (`after`) decorations and data-guard conditions are not modeled
25499
+ * by this sampler; it walks the probabilistic graph topology.
25500
+ *
25501
+ * @param opts - {@link JssmStochasticOptions}. `runs` defaults to the
25502
+ * machine's declared `editor: { stochastic_run_count }` (fsl#1334) when
25503
+ * present, otherwise {@link STOCHASTIC_DEFAULT_RUNS}.
25504
+ * @returns A {@link JssmStochasticSummary}.
25505
+ *
25506
+ * @see Machine.stochastic_runs
25507
+ * @see Machine.probabilistic_walk
25508
+ * @see Machine.editor_config
25509
+ *
25510
+ * @example
25511
+ * const m = sm`a 'go' -> b 'go' -> c;`;
25512
+ * const s = m.stochastic_summary({ runs: 100, seed: 1 });
25513
+ * s.terminal_reached; // => 100
25514
+ */
25515
+ stochastic_summary(opts = {}) {
25516
+ var _a, _b, _c;
25517
+ const mode = (_a = opts.mode) !== null && _a !== void 0 ? _a : 'montecarlo';
25518
+ const saved_seed = this._rng_seed;
25519
+ if (opts.seed !== undefined) {
25520
+ this.rng_seed = opts.seed;
25521
+ }
25522
+ const effective_seed = this._rng_seed;
25523
+ const state_visits = new Map();
25524
+ const edge_traversals = new Map();
25525
+ const path_lengths = [];
25526
+ let terminal_reached = 0, capped = 0, runs = 0;
25527
+ try {
25528
+ for (const run of this.stochastic_runs(Object.assign(Object.assign({}, opts), { mode }))) {
25529
+ runs += 1;
25530
+ for (const s of run.states) {
25531
+ state_visits.set(s, ((_b = state_visits.get(s)) !== null && _b !== void 0 ? _b : 0) + 1);
25532
+ }
25533
+ for (const e of run.edges) {
25534
+ edge_traversals.set(e, ((_c = edge_traversals.get(e)) !== null && _c !== void 0 ? _c : 0) + 1);
25535
+ }
25536
+ if (mode === 'montecarlo') {
25537
+ if (run.terminated) {
25538
+ terminal_reached += 1;
25539
+ path_lengths.push(run.length);
25540
+ }
25541
+ else {
25542
+ capped += 1;
25543
+ }
25544
+ }
25545
+ }
25546
+ }
25547
+ finally {
25548
+ // restore the PRNG so the call is non-destructive even when the loop throws
25549
+ this.rng_seed = saved_seed;
25550
+ }
25551
+ const total_visits = [...state_visits.values()].reduce((a, b) => a + b, 0);
25552
+ const state_visit_fraction = new Map();
25553
+ for (const [s, c] of state_visits) {
25554
+ state_visit_fraction.set(s, c / total_visits);
25555
+ }
25556
+ const summary = {
25557
+ mode, runs, seed: effective_seed,
25558
+ state_visits, state_visit_fraction, edge_traversals,
25559
+ };
25560
+ if (mode === 'montecarlo') {
25561
+ summary.path_lengths = path_lengths;
25562
+ summary.terminal_reached = terminal_reached;
25563
+ summary.capped = capped;
25564
+ }
25565
+ return summary;
25566
+ }
25422
25567
  /********
25423
25568
  *
25424
25569
  * List all actions available from this state. Please note that the order of
@@ -108,7 +108,7 @@ function parseFslArgs(argv, spec) {
108
108
  return { positional, flags, helpText };
109
109
  }
110
110
 
111
- const getVersion = () => "5.153.0";
111
+ const getVersion = () => "5.154.0";
112
112
  const SPEC = {
113
113
  flags: {
114
114
  help: { short: "h", boolean: true },