@sccharts-lab/trace 0.0.0-stage → 0.1.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/LICENSE +277 -0
- package/README.md +233 -2
- package/dist/comparison-report.d.ts +15 -0
- package/dist/comparison-report.js +258 -0
- package/dist/comparison-report.js.map +1 -0
- package/dist/csv.d.ts +13 -0
- package/dist/csv.js +188 -0
- package/dist/csv.js.map +1 -0
- package/dist/data-pool.d.ts +2 -0
- package/dist/data-pool.js +26 -0
- package/dist/data-pool.js.map +1 -0
- package/dist/engine-replay.d.ts +78 -0
- package/dist/engine-replay.js +115 -0
- package/dist/engine-replay.js.map +1 -0
- package/dist/engine-sequence.d.ts +26 -0
- package/dist/engine-sequence.js +132 -0
- package/dist/engine-sequence.js.map +1 -0
- package/dist/engine.d.ts +51 -0
- package/dist/engine.js +210 -0
- package/dist/engine.js.map +1 -0
- package/dist/helper.d.ts +10 -0
- package/dist/helper.js +26 -0
- package/dist/helper.js.map +1 -0
- package/dist/index.d.ts +18 -0
- package/dist/index.js +18 -0
- package/dist/index.js.map +1 -0
- package/dist/input-value.d.ts +2 -0
- package/dist/input-value.js +38 -0
- package/dist/input-value.js.map +1 -0
- package/dist/json.d.ts +8 -0
- package/dist/json.js +122 -0
- package/dist/json.js.map +1 -0
- package/dist/ktrace.d.ts +11 -0
- package/dist/ktrace.js +266 -0
- package/dist/ktrace.js.map +1 -0
- package/dist/protocol.d.ts +25 -0
- package/dist/protocol.js +18 -0
- package/dist/protocol.js.map +1 -0
- package/dist/trace-compare.d.ts +77 -0
- package/dist/trace-compare.js +156 -0
- package/dist/trace-compare.js.map +1 -0
- package/dist/trace-file.d.ts +6 -0
- package/dist/trace-file.js +65 -0
- package/dist/trace-file.js.map +1 -0
- package/dist/trace-record.d.ts +71 -0
- package/dist/trace-record.js +181 -0
- package/dist/trace-record.js.map +1 -0
- package/dist/trace-rename.d.ts +40 -0
- package/dist/trace-rename.js +112 -0
- package/dist/trace-rename.js.map +1 -0
- package/dist/trace-replay.d.ts +72 -0
- package/dist/trace-replay.js +169 -0
- package/dist/trace-replay.js.map +1 -0
- package/dist/trace-timing.d.ts +55 -0
- package/dist/trace-timing.js +122 -0
- package/dist/trace-timing.js.map +1 -0
- package/dist/xml.d.ts +8 -0
- package/dist/xml.js +179 -0
- package/dist/xml.js.map +1 -0
- package/package.json +70 -3
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import type { CompileResult, Simulation, SimTickResult } from "@sccharts-lab/engine";
|
|
2
|
+
import type { Diagnostic } from "@sccharts-lab/engine/types";
|
|
3
|
+
import type { ReplayState } from "./protocol.js";
|
|
4
|
+
import type { TraceRecord } from "./trace-record.js";
|
|
5
|
+
import type { Renames } from "./trace-rename.js";
|
|
6
|
+
import { TraceReplay } from "./trace-replay.js";
|
|
7
|
+
import type { ReplayMismatch, ReplayTarget } from "./trace-replay.js";
|
|
8
|
+
type Compiled = Pick<CompileResult, 'sim' | 'chart' | 'interface' | 'index'>;
|
|
9
|
+
export interface EngineReplayOptions {
|
|
10
|
+
/** How the replay names the file, e.g. its file name (ReplayState.label). Default `trace`. */
|
|
11
|
+
label?: string;
|
|
12
|
+
/** False when another model's outputs are not expected to match, as in a batch comparison. Default true. */
|
|
13
|
+
checkOutputs?: boolean;
|
|
14
|
+
/** The file's names to the model's (`suggestRenames` proposes them); applied before matching. */
|
|
15
|
+
renames?: Renames;
|
|
16
|
+
}
|
|
17
|
+
/** One tick driven by the file. */
|
|
18
|
+
export interface ReplayStep {
|
|
19
|
+
tick: SimTickResult;
|
|
20
|
+
/** The inputs the file gave this tick, in the model's types. */
|
|
21
|
+
inputs: Record<string, unknown>;
|
|
22
|
+
/** Checked outputs that differ from the file after this tick. */
|
|
23
|
+
mismatches: ReplayMismatch[];
|
|
24
|
+
/** The file asks to stop running after this tick (KTrace `pause`). */
|
|
25
|
+
pause: boolean;
|
|
26
|
+
}
|
|
27
|
+
/** What the model's variables are for replaying: every pool variable, inputs and outputs as KIELER marks them. */
|
|
28
|
+
export declare function replayTargets(result: Compiled): ReplayTarget[];
|
|
29
|
+
/**
|
|
30
|
+
* Drives `simulation` from a trace, starting with its next tick (the file's first row is the tick after the ones
|
|
31
|
+
* already run, as when the extension starts replaying during a run). `step()` runs one tick of the file;
|
|
32
|
+
* `run()` runs to the end of the file, stopping early where `stop` says so.
|
|
33
|
+
*/
|
|
34
|
+
export declare class SimulationReplay {
|
|
35
|
+
readonly result: Compiled;
|
|
36
|
+
readonly simulation: Simulation;
|
|
37
|
+
readonly replay: TraceReplay;
|
|
38
|
+
private readonly variables;
|
|
39
|
+
constructor(result: Compiled, simulation: Simulation, trace: TraceRecord, options?: EngineReplayOptions);
|
|
40
|
+
/** The file has driven its last row. */
|
|
41
|
+
get done(): boolean;
|
|
42
|
+
/** A variable's value after the last tick (before the first: an input's starting value), as the model holds it. */
|
|
43
|
+
private current;
|
|
44
|
+
/** The value checked against the file: the pool after the tick, a name it leaves out keeping its value. */
|
|
45
|
+
private actual;
|
|
46
|
+
/** Runs the next tick with the file's inputs; undefined once the file has ended or the model has stopped. */
|
|
47
|
+
step(): ReplayStep | undefined;
|
|
48
|
+
/** Runs to the end of the file, or until `stop` returns true for a tick, or a runtime error stops the model. */
|
|
49
|
+
run(stop?: (step: ReplayStep) => boolean): ReplayStep[];
|
|
50
|
+
/** The runtime error that stopped the model, if any. */
|
|
51
|
+
get error(): Diagnostic | undefined;
|
|
52
|
+
/** Goes back to the state after `tick` (simulation and checks), e.g. for stepping back during a replay. */
|
|
53
|
+
rewindTo(tick: number): void;
|
|
54
|
+
/** What a view shows: progress, the file's inputs the model lacks and the other way round, the first mismatch. */
|
|
55
|
+
state(): ReplayState;
|
|
56
|
+
}
|
|
57
|
+
export interface ReplayOutcome {
|
|
58
|
+
replay: SimulationReplay;
|
|
59
|
+
/** Every tick that differs, with each output that differs. */
|
|
60
|
+
mismatches: ReplayMismatch[];
|
|
61
|
+
/** The outputs that differ at the first tick that differs. */
|
|
62
|
+
firstMismatch?: ReplayMismatch[];
|
|
63
|
+
/** The replayed run as a trace (what Save would write). */
|
|
64
|
+
run: TraceRecord;
|
|
65
|
+
state: ReplayState;
|
|
66
|
+
error?: Diagnostic;
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Replays the whole trace into a simulation of `result` (a fresh one unless `simulation` is given, which then goes
|
|
70
|
+
* on from its current tick) and reports every output that differs from the file. `stopAtMismatch` stops at the
|
|
71
|
+
* first tick that differs, as the extension's Run does. `model` names the model in the replayed run's trace.
|
|
72
|
+
*/
|
|
73
|
+
export declare function replayTrace(result: CompileResult, trace: TraceRecord, options?: EngineReplayOptions & {
|
|
74
|
+
simulation?: Simulation;
|
|
75
|
+
stopAtMismatch?: boolean;
|
|
76
|
+
model?: string;
|
|
77
|
+
}): ReplayOutcome;
|
|
78
|
+
export {};
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
// SPDX-License-Identifier: EPL-2.0
|
|
2
|
+
//
|
|
3
|
+
// Replaying a trace into a simulation of the in-browser engine, as the extension's Replay does on KIELER's server:
|
|
4
|
+
// the file's inputs drive the model tick by tick (matched by name, after renames, in the model's types), and after
|
|
5
|
+
// each tick the model's outputs are checked against the file's. TraceReplay does the matching and the checking;
|
|
6
|
+
// this drives a Simulation with it.
|
|
7
|
+
import { createSimulation } from "@sccharts-lab/engine";
|
|
8
|
+
import { inputValue } from "./data-pool.js";
|
|
9
|
+
import { initialValue, poolVariables, traceFromRun } from "./engine.js";
|
|
10
|
+
import { renameTrace } from "./trace-rename.js";
|
|
11
|
+
import { TraceReplay } from "./trace-replay.js";
|
|
12
|
+
/** What the model's variables are for replaying: every pool variable, inputs and outputs as KIELER marks them. */
|
|
13
|
+
export function replayTargets(result) {
|
|
14
|
+
return poolVariables(result).map(({ name, input, output, type }) => ({ id: name, input, output, type }));
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Drives `simulation` from a trace, starting with its next tick (the file's first row is the tick after the ones
|
|
18
|
+
* already run, as when the extension starts replaying during a run). `step()` runs one tick of the file;
|
|
19
|
+
* `run()` runs to the end of the file, stopping early where `stop` says so.
|
|
20
|
+
*/
|
|
21
|
+
export class SimulationReplay {
|
|
22
|
+
result;
|
|
23
|
+
simulation;
|
|
24
|
+
replay;
|
|
25
|
+
variables;
|
|
26
|
+
constructor(result, simulation, trace, options = {}) {
|
|
27
|
+
this.result = result;
|
|
28
|
+
this.simulation = simulation;
|
|
29
|
+
const variables = poolVariables(result);
|
|
30
|
+
this.variables = new Map(variables.map((v) => [v.name, v]));
|
|
31
|
+
const renamed = options.renames ? renameTrace(trace, options.renames) : trace;
|
|
32
|
+
this.replay = new TraceReplay(options.label ?? 'trace', renamed, replayTargets(result), options.checkOutputs ?? true, undefined, simulation.tick);
|
|
33
|
+
}
|
|
34
|
+
/** The file has driven its last row. */
|
|
35
|
+
get done() {
|
|
36
|
+
return this.simulation.tick >= this.replay.end;
|
|
37
|
+
}
|
|
38
|
+
/** A variable's value after the last tick (before the first: an input's starting value), as the model holds it. */
|
|
39
|
+
current(name) {
|
|
40
|
+
const variable = this.variables.get(name);
|
|
41
|
+
const history = this.simulation.history;
|
|
42
|
+
for (let index = history.length - 1; index >= 0; index--) {
|
|
43
|
+
const value = history[index].pool[name];
|
|
44
|
+
if (value !== undefined)
|
|
45
|
+
return inputValue(value, variable?.type);
|
|
46
|
+
}
|
|
47
|
+
if (!variable?.input)
|
|
48
|
+
return undefined;
|
|
49
|
+
return initialValue(variable);
|
|
50
|
+
}
|
|
51
|
+
/** The value checked against the file: the pool after the tick, a name it leaves out keeping its value. */
|
|
52
|
+
actual(name) {
|
|
53
|
+
const history = this.simulation.history;
|
|
54
|
+
for (let index = history.length - 1; index >= 0; index--) {
|
|
55
|
+
const value = history[index].pool[name];
|
|
56
|
+
if (value !== undefined)
|
|
57
|
+
return value;
|
|
58
|
+
}
|
|
59
|
+
return undefined;
|
|
60
|
+
}
|
|
61
|
+
/** Runs the next tick with the file's inputs; undefined once the file has ended or the model has stopped. */
|
|
62
|
+
step() {
|
|
63
|
+
if (this.done || this.error)
|
|
64
|
+
return undefined;
|
|
65
|
+
const next = this.simulation.tick + 1;
|
|
66
|
+
const inputs = Object.fromEntries(this.replay.inputsFor(next, (id) => this.current(id)));
|
|
67
|
+
const tick = this.simulation.step(inputs);
|
|
68
|
+
const mismatches = tick.error ? [] : this.replay.check(tick.tick, (id) => this.actual(id));
|
|
69
|
+
return { tick, inputs, mismatches, pause: this.replay.pausesAfter(tick.tick) };
|
|
70
|
+
}
|
|
71
|
+
/** Runs to the end of the file, or until `stop` returns true for a tick, or a runtime error stops the model. */
|
|
72
|
+
run(stop = () => false) {
|
|
73
|
+
const steps = [];
|
|
74
|
+
for (let step = this.step(); step; step = this.step()) {
|
|
75
|
+
steps.push(step);
|
|
76
|
+
if (step.tick.error || stop(step))
|
|
77
|
+
break;
|
|
78
|
+
}
|
|
79
|
+
return steps;
|
|
80
|
+
}
|
|
81
|
+
/** The runtime error that stopped the model, if any. */
|
|
82
|
+
get error() {
|
|
83
|
+
return this.simulation.history.find((tick) => tick.error)?.error;
|
|
84
|
+
}
|
|
85
|
+
/** Goes back to the state after `tick` (simulation and checks), e.g. for stepping back during a replay. */
|
|
86
|
+
rewindTo(tick) {
|
|
87
|
+
this.simulation.rewind(tick);
|
|
88
|
+
this.replay.rewindTo(tick);
|
|
89
|
+
}
|
|
90
|
+
/** What a view shows: progress, the file's inputs the model lacks and the other way round, the first mismatch. */
|
|
91
|
+
state() {
|
|
92
|
+
return this.replay.state(this.simulation.tick);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Replays the whole trace into a simulation of `result` (a fresh one unless `simulation` is given, which then goes
|
|
97
|
+
* on from its current tick) and reports every output that differs from the file. `stopAtMismatch` stops at the
|
|
98
|
+
* first tick that differs, as the extension's Run does. `model` names the model in the replayed run's trace.
|
|
99
|
+
*/
|
|
100
|
+
export function replayTrace(result, trace, options = {}) {
|
|
101
|
+
const simulation = options.simulation ?? createSimulation(result);
|
|
102
|
+
const replay = new SimulationReplay(result, simulation, trace, options);
|
|
103
|
+
const steps = replay.run((step) => !!options.stopAtMismatch && step.mismatches.length > 0);
|
|
104
|
+
const mismatches = steps.flatMap((step) => step.mismatches);
|
|
105
|
+
const first = steps.find((step) => step.mismatches.length > 0);
|
|
106
|
+
return {
|
|
107
|
+
replay,
|
|
108
|
+
mismatches,
|
|
109
|
+
...(first ? { firstMismatch: first.mismatches } : {}),
|
|
110
|
+
run: traceFromRun(result, simulation, { model: options.model }),
|
|
111
|
+
state: replay.state(),
|
|
112
|
+
...(replay.error ? { error: replay.error } : {}),
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
//# sourceMappingURL=engine-replay.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"engine-replay.js","sourceRoot":"","sources":["../src/engine-replay.ts"],"names":[],"mappings":"AAAA,mCAAmC;AACnC,EAAE;AACF,mHAAmH;AACnH,mHAAmH;AACnH,gHAAgH;AAChH,oCAAoC;AAEpC,OAAO,EAAE,gBAAgB,EAAE,6BAA4B;AAGvD,OAAO,EAAE,UAAU,EAAE,uBAAmB;AACxC,OAAO,EAAE,YAAY,EAAE,aAAa,EAAE,YAAY,EAAE,oBAAgB;AAIpE,OAAO,EAAE,WAAW,EAAE,0BAAsB;AAE5C,OAAO,EAAE,WAAW,EAAE,0BAAsB;AAyB5C,kHAAkH;AAClH,MAAM,UAAU,aAAa,CAAC,MAAgB;IAC1C,OAAO,aAAa,CAAC,MAAM,CAAC,CAAC,GAAG,CAAC,CAAC,EAAE,IAAI,EAAE,KAAK,EAAE,MAAM,EAAE,IAAI,EAAE,EAAE,EAAE,CAAC,CAAC,EAAE,EAAE,EAAE,IAAI,EAAE,KAAK,EAAE,MAAM,EAAE,IAAI,EAAE,CAAC,CAAC,CAAA;AAC5G,CAAC;AAED;;;;GAIG;AACH,MAAM,OAAO,gBAAgB;IAMZ;IACA;IANJ,M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CAAC,MAAM,CAAC,CAAA;IACjE,MAAM,MAAM,GAAG,IAAI,gBAAgB,CAAC,MAAM,EAAE,UAAU,EAAE,KAAK,EAAE,OAAO,CAAC,CAAA;IACvE,MAAM,KAAK,GAAG,MAAM,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC,CAAC,OAAO,CAAC,cAAc,IAAI,IAAI,CAAC,UAAU,CAAC,MAAM,GAAG,CAAC,CAAC,CAAA;IAC1F,MAAM,UAAU,GAAG,KAAK,CAAC,OAAO,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,UAAU,CAAC,CAAA;IAC3D,MAAM,KAAK,GAAG,KAAK,CAAC,IAAI,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,UAAU,CAAC,MAAM,GAAG,CAAC,CAAC,CAAA;IAC9D,OAAO;QACH,MAAM;QACN,UAAU;QACV,GAAG,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE,aAAa,EAAE,KAAK,CAAC,UAAU,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;QACrD,GAAG,EAAE,YAAY,CAAC,MAAM,EAAE,UAAU,EAAE,EAAE,KAAK,EAAE,OAAO,CAAC,KAAK,EAAE,CAAC;QAC/D,KAAK,EAAE,MAAM,CAAC,KAAK,EAAE;QACrB,GAAG,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE,KAAK,EAAE,MAAM,CAAC,KAAK,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;KACnD,CAAA;AACL,CAAC","sourcesContent":["// SPDX-License-Identifier: EPL-2.0\n//\n// Replaying a trace into a simulation of the in-browser engine, as the extension's Replay does on KIELER's server:\n// the file's inputs drive the model tick by tick (matched by name, after renames, in the model's types), and after\n// each tick the model's outputs are checked against the file's. TraceReplay does the matching and the checking;\n// this drives a Simulation with it.\n\nimport { createSimulation } from '@sccharts-lab/engine'\nimport type { CompileResult, Simulation, SimTickResult } from '@sccharts-lab/engine'\nimport type { Diagnostic, Value } from '@sccharts-lab/engine/types'\nimport { inputValue } from './data-pool'\nimport { initialValue, poolVariables, traceFromRun } from './engine'\nimport type { PoolVariable } from './engine'\nimport type { ReplayState } from './protocol'\nimport type { TraceRecord } from './trace-record'\nimport { renameTrace } from './trace-rename'\nimport type { Renames } from './trace-rename'\nimport { TraceReplay } from './trace-replay'\nimport type { ReplayMismatch, ReplayTarget } from './trace-replay'\n\ntype Compiled = Pick<CompileResult, 'sim' | 'chart' | 'interface' | 'index'>\n\nexport interface EngineReplayOptions {\n /** How the replay names the file, e.g. its file name (ReplayState.label). Default `trace`. */\n label?: string\n /** False when another model's outputs are not expected to match, as in a batch comparison. Default true. */\n checkOutputs?: boolean\n /** The file's names to the model's (`suggestRenames` proposes them); applied before matching. */\n renames?: Renames\n}\n\n/** One tick driven by the file. */\nexport interface ReplayStep {\n tick: SimTickResult\n /** The inputs the file gave this tick, in the model's types. */\n inputs: Record<string, unknown>\n /** Checked outputs that differ from the file after this tick. */\n mismatches: ReplayMismatch[]\n /** The file asks to stop running after this tick (KTrace `pause`). */\n pause: boolean\n}\n\n/** What the model's variables are for replaying: every pool variable, inputs and outputs as KIELER marks them. */\nexport function replayTargets(result: Compiled): ReplayTarget[] {\n return poolVariables(result).map(({ name, input, output, type }) => ({ id: name, input, output, type }))\n}\n\n/**\n * Drives `simulation` from a trace, starting with its next tick (the file's first row is the tick after the ones\n * already run, as when the extension starts replaying during a run). `step()` runs one tick of the file;\n * `run()` runs to the end of the file, stopping early where `stop` says so.\n */\nexport class SimulationReplay {\n readonly replay: TraceReplay\n\n private readonly variables: Map<string, PoolVariable>\n\n constructor(\n readonly result: Compiled,\n readonly simulation: Simulation,\n trace: TraceRecord,\n options: EngineReplayOptions = {}\n ) {\n const variables = poolVariables(result)\n this.variables = new Map(variables.map((v) => [v.name, v]))\n const renamed = options.renames ? renameTrace(trace, options.renames) : trace\n this.replay = new TraceReplay(\n options.label ?? 'trace',\n renamed,\n replayTargets(result),\n options.checkOutputs ?? true,\n undefined,\n simulation.tick\n )\n }\n\n /** The file has driven its last row. */\n get done(): boolean {\n return this.simulation.tick >= this.replay.end\n }\n\n /** A variable's value after the last tick (before the first: an input's starting value), as the model holds it. */\n private current(name: string): unknown {\n const variable = this.variables.get(name)\n const history = this.simulation.history\n for (let index = history.length - 1; index >= 0; index--) {\n const value = history[index].pool[name]\n if (value !== undefined) return inputValue(value, variable?.type)\n }\n if (!variable?.input) return undefined\n return initialValue(variable)\n }\n\n /** The value checked against the file: the pool after the tick, a name it leaves out keeping its value. */\n private actual(name: string): unknown {\n const history = this.simulation.history\n for (let index = history.length - 1; index >= 0; index--) {\n const value = history[index].pool[name]\n if (value !== undefined) return value\n }\n return undefined\n }\n\n /** Runs the next tick with the file's inputs; undefined once the file has ended or the model has stopped. */\n step(): ReplayStep | undefined {\n if (this.done || this.error) return undefined\n const next = this.simulation.tick + 1\n const inputs = Object.fromEntries(this.replay.inputsFor(next, (id) => this.current(id)))\n const tick = this.simulation.step(inputs as Record<string, Value>)\n const mismatches = tick.error ? [] : this.replay.check(tick.tick, (id) => this.actual(id))\n return { tick, inputs, mismatches, pause: this.replay.pausesAfter(tick.tick) }\n }\n\n /** Runs to the end of the file, or until `stop` returns true for a tick, or a runtime error stops the model. */\n run(stop: (step: ReplayStep) => boolean = () => false): ReplayStep[] {\n const steps: ReplayStep[] = []\n for (let step = this.step(); step; step = this.step()) {\n steps.push(step)\n if (step.tick.error || stop(step)) break\n }\n return steps\n }\n\n /** The runtime error that stopped the model, if any. */\n get error(): Diagnostic | undefined {\n return this.simulation.history.find((tick) => tick.error)?.error\n }\n\n /** Goes back to the state after `tick` (simulation and checks), e.g. for stepping back during a replay. */\n rewindTo(tick: number): void {\n this.simulation.rewind(tick)\n this.replay.rewindTo(tick)\n }\n\n /** What a view shows: progress, the file's inputs the model lacks and the other way round, the first mismatch. */\n state(): ReplayState {\n return this.replay.state(this.simulation.tick)\n }\n}\n\nexport interface ReplayOutcome {\n replay: SimulationReplay\n /** Every tick that differs, with each output that differs. */\n mismatches: ReplayMismatch[]\n /** The outputs that differ at the first tick that differs. */\n firstMismatch?: ReplayMismatch[]\n /** The replayed run as a trace (what Save would write). */\n run: TraceRecord\n state: ReplayState\n error?: Diagnostic\n}\n\n/**\n * Replays the whole trace into a simulation of `result` (a fresh one unless `simulation` is given, which then goes\n * on from its current tick) and reports every output that differs from the file. `stopAtMismatch` stops at the\n * first tick that differs, as the extension's Run does. `model` names the model in the replayed run's trace.\n */\nexport function replayTrace(\n result: CompileResult,\n trace: TraceRecord,\n options: EngineReplayOptions & { simulation?: Simulation; stopAtMismatch?: boolean; model?: string } = {}\n): ReplayOutcome {\n const simulation = options.simulation ?? createSimulation(result)\n const replay = new SimulationReplay(result, simulation, trace, options)\n const steps = replay.run((step) => !!options.stopAtMismatch && step.mismatches.length > 0)\n const mismatches = steps.flatMap((step) => step.mismatches)\n const first = steps.find((step) => step.mismatches.length > 0)\n return {\n replay,\n mismatches,\n ...(first ? { firstMismatch: first.mismatches } : {}),\n run: traceFromRun(result, simulation, { model: options.model }),\n state: replay.state(),\n ...(replay.error ? { error: replay.error } : {}),\n }\n}\n"]}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { CompileResult } from "@sccharts-lab/engine";
|
|
2
|
+
import type { TraceRecord } from "./trace-record.js";
|
|
3
|
+
import type { TickMode } from "./trace-timing.js";
|
|
4
|
+
export interface SequenceRun {
|
|
5
|
+
trace: TraceRecord;
|
|
6
|
+
/** What the extension's report says about the run: inputs the sequence does not set or the model lacks, ... */
|
|
7
|
+
notes: string[];
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Runs `ticks` ticks of a fresh simulation with its inputs from `sequence` (in the model's names: `renameTrace`
|
|
11
|
+
* first where they differ). `label` names the model in the notes.
|
|
12
|
+
*/
|
|
13
|
+
export declare function runOnSequence(result: CompileResult, sequence: TraceRecord, ticks: number, options?: {
|
|
14
|
+
label?: string;
|
|
15
|
+
model?: string;
|
|
16
|
+
}): SequenceRun;
|
|
17
|
+
/**
|
|
18
|
+
* Runs a fresh simulation on a timed sequence (`sequence.times`) up to `until` seconds. A model with `deltaT` ticks at
|
|
19
|
+
* the sequence's moments and when its `sleepT` wakes it, with the time since as `deltaT`; another model as `mode`
|
|
20
|
+
* says (default: only at the sequence's moments). The trace has the time of each tick.
|
|
21
|
+
*/
|
|
22
|
+
export declare function runOnTimedSequence(result: CompileResult, sequence: TraceRecord, until: number, options?: {
|
|
23
|
+
label?: string;
|
|
24
|
+
model?: string;
|
|
25
|
+
mode?: TickMode;
|
|
26
|
+
}): SequenceRun;
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
// SPDX-License-Identifier: EPL-2.0
|
|
2
|
+
//
|
|
3
|
+
// Running a model of the in-browser engine on an input sequence, as the extension's "Run models on a sequence"
|
|
4
|
+
// does on KIELER's server (trace-tools.ts runTicked, runTimed): a number of ticks, one per row (the last values
|
|
5
|
+
// staying after the sequence ends); or, for a sequence with times, for a number of seconds, ticked at the
|
|
6
|
+
// sequence's moments and as the model keeps time (TimedSchedule). The run's trace and the notes the extension puts
|
|
7
|
+
// in its report come back; compareTraces and comparisonMarkdown({ preamble }) compare several models' runs.
|
|
8
|
+
import { createSimulation } from "@sccharts-lab/engine";
|
|
9
|
+
import { inputValue } from "./data-pool.js";
|
|
10
|
+
import { initialValue, poolVariables, traceFromRun } from "./engine.js";
|
|
11
|
+
import { SimulationReplay } from "./engine-replay.js";
|
|
12
|
+
import { isTimeDelta } from "./helper.js";
|
|
13
|
+
import { isCompatibleInput } from "./input-value.js";
|
|
14
|
+
import { formatTraceValue } from "./trace-record.js";
|
|
15
|
+
import { coerceInput } from "./trace-replay.js";
|
|
16
|
+
import { MAX_TIMED_TICKS, TimedSchedule } from "./trace-timing.js";
|
|
17
|
+
const code = (name) => `\`${name}\``;
|
|
18
|
+
/**
|
|
19
|
+
* Runs `ticks` ticks of a fresh simulation with its inputs from `sequence` (in the model's names: `renameTrace`
|
|
20
|
+
* first where they differ). `label` names the model in the notes.
|
|
21
|
+
*/
|
|
22
|
+
export function runOnSequence(result, sequence, ticks, options = {}) {
|
|
23
|
+
const label = options.label ?? options.model ?? 'The model';
|
|
24
|
+
const simulation = createSimulation(result);
|
|
25
|
+
const replay = new SimulationReplay(result, simulation, sequence, { label, checkOutputs: false });
|
|
26
|
+
if (ticks > 0)
|
|
27
|
+
replay.run((step) => step.tick.tick >= ticks);
|
|
28
|
+
// After the sequence's end its last values stay.
|
|
29
|
+
while (simulation.tick < ticks && !replay.error)
|
|
30
|
+
simulation.step({});
|
|
31
|
+
const trace = traceFromRun(result, simulation, { model: options.model });
|
|
32
|
+
const notes = [];
|
|
33
|
+
const { unset, ignored } = replay.replay;
|
|
34
|
+
if (unset.length) {
|
|
35
|
+
const kept = unset.map((name) => `${code(name)} = ${formatTraceValue(trace.ticks[0]?.[name])}`).join(', ');
|
|
36
|
+
notes.push(`${label}: the sequence does not set ${kept} (the value at tick 1).`);
|
|
37
|
+
}
|
|
38
|
+
if (ignored.length)
|
|
39
|
+
notes.push(`${label} has no input ${ignored.map(code).join(', ')}.`);
|
|
40
|
+
if (replay.error)
|
|
41
|
+
notes.push(`${label} stopped after ${trace.ticks.length} ticks: ${replay.error.message}.`);
|
|
42
|
+
return { trace, notes };
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Runs a fresh simulation on a timed sequence (`sequence.times`) up to `until` seconds. A model with `deltaT` ticks at
|
|
46
|
+
* the sequence's moments and when its `sleepT` wakes it, with the time since as `deltaT`; another model as `mode`
|
|
47
|
+
* says (default: only at the sequence's moments). The trace has the time of each tick.
|
|
48
|
+
*/
|
|
49
|
+
export function runOnTimedSequence(result, sequence, until, options = {}) {
|
|
50
|
+
const label = options.label ?? options.model ?? 'The model';
|
|
51
|
+
const variables = poolVariables(result);
|
|
52
|
+
const inputs = variables.filter((v) => v.input);
|
|
53
|
+
const delta = inputs.find((v) => isTimeDelta({ id: v.name, categories: [] }));
|
|
54
|
+
const sleep = variables.find((v) => /^[#_]*sleepT$/i.test(v.name));
|
|
55
|
+
const notes = [];
|
|
56
|
+
let mode = options.mode ?? { kind: 'events' };
|
|
57
|
+
if (delta) {
|
|
58
|
+
mode = { kind: 'clock', delta: delta.name, sleep: sleep?.name };
|
|
59
|
+
}
|
|
60
|
+
else if (mode.kind === 'timer' && !inputs.some((v) => v.name === mode.input)) {
|
|
61
|
+
notes.push(`${label} has no input ${code(mode.input)}, so it ticked only at the sequence's moments.`);
|
|
62
|
+
mode = { kind: 'events' };
|
|
63
|
+
}
|
|
64
|
+
const simulation = createSimulation(result);
|
|
65
|
+
let schedule;
|
|
66
|
+
try {
|
|
67
|
+
schedule = new TimedSchedule(sequence, mode, until);
|
|
68
|
+
}
|
|
69
|
+
catch (error) {
|
|
70
|
+
notes.push(`${label}: ${error instanceof Error ? error.message : error}`);
|
|
71
|
+
return { trace: { variables: [], ticks: [] }, notes };
|
|
72
|
+
}
|
|
73
|
+
const byName = new Map(inputs.map((v) => [v.name, v]));
|
|
74
|
+
const current = new Map(inputs.map((v) => [v.name, initialValue(v)]));
|
|
75
|
+
const times = [];
|
|
76
|
+
const unfit = new Set();
|
|
77
|
+
let wake;
|
|
78
|
+
let failed;
|
|
79
|
+
for (let tick = schedule.next(); tick; tick = schedule.next(wake)) {
|
|
80
|
+
// As the extension queues them: in the inputs' types, refusing values that do not fit.
|
|
81
|
+
const given = {};
|
|
82
|
+
for (const [name, raw] of Object.entries(tick.inputs)) {
|
|
83
|
+
const input = byName.get(name);
|
|
84
|
+
if (!input || raw === undefined)
|
|
85
|
+
continue;
|
|
86
|
+
const value = coerceInput(raw, input.type);
|
|
87
|
+
if (isCompatibleInput(value, current.get(name))) {
|
|
88
|
+
given[name] = value;
|
|
89
|
+
current.set(name, value);
|
|
90
|
+
}
|
|
91
|
+
else {
|
|
92
|
+
unfit.add(name);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
const ran = simulation.step(given);
|
|
96
|
+
if (ran.error) {
|
|
97
|
+
failed = ran.error.message;
|
|
98
|
+
break;
|
|
99
|
+
}
|
|
100
|
+
for (const v of inputs)
|
|
101
|
+
if (ran.pool[v.name] !== undefined)
|
|
102
|
+
current.set(v.name, inputValue(ran.pool[v.name], v.type));
|
|
103
|
+
times.push(tick.time);
|
|
104
|
+
wake = mode.kind === 'clock' && mode.sleep ? ran.pool[mode.sleep] : undefined;
|
|
105
|
+
}
|
|
106
|
+
const run = traceFromRun(result, simulation, { model: options.model });
|
|
107
|
+
const trace = { ...run, times: times.slice(0, run.ticks.length) };
|
|
108
|
+
const set = new Set(sequence.variables.filter((v) => v.role !== 'output' && v.role !== 'local').map(({ name }) => name));
|
|
109
|
+
const how = mode.kind === 'clock'
|
|
110
|
+
? `at the sequence's moments and when its clock woke it${mode.sleep ? ` (${code(mode.sleep)})` : ''}`
|
|
111
|
+
: mode.kind === 'timer'
|
|
112
|
+
? `at the sequence's moments and every ${Number((mode.period * 1000).toFixed(6))} ms, with ${code(mode.input)} true on the timer's ticks`
|
|
113
|
+
: "only at the sequence's moments";
|
|
114
|
+
notes.push(`${label} ticked ${times.length} times, ${how}.`);
|
|
115
|
+
const ignored = Array.from(set).filter((name) => !byName.has(name));
|
|
116
|
+
if (ignored.length > 0)
|
|
117
|
+
notes.push(`${label} has no input ${ignored.map(code).join(', ')}.`);
|
|
118
|
+
const own = new Set([delta?.name, mode.kind === 'timer' ? mode.input : undefined]);
|
|
119
|
+
const unset = inputs.filter((v) => !set.has(v.name) && !own.has(v.name));
|
|
120
|
+
if (unset.length > 0) {
|
|
121
|
+
const kept = unset.map((v) => `${code(v.name)} = ${formatTraceValue(trace.ticks[0]?.[v.name])}`);
|
|
122
|
+
notes.push(`${label}: the sequence does not set ${kept.join(', ')} (the value at the first tick).`);
|
|
123
|
+
}
|
|
124
|
+
if (unfit.size > 0)
|
|
125
|
+
notes.push(`${label}: the sequence's values for ${Array.from(unfit).join(', ')} do not fit its inputs.`);
|
|
126
|
+
if (failed)
|
|
127
|
+
notes.push(`${label} stopped after ${times.length} ticks: ${failed}.`);
|
|
128
|
+
if (schedule.ticks >= MAX_TIMED_TICKS)
|
|
129
|
+
notes.push(`${label} reached ${MAX_TIMED_TICKS} ticks and was stopped.`);
|
|
130
|
+
return { trace, notes };
|
|
131
|
+
}
|
|
132
|
+
//# sourceMappingURL=engine-sequence.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"engine-sequence.js","sourceRoot":"","sources":["../src/engine-sequence.ts"],"names":[],"mappings":"AAAA,mCAAmC;AACnC,EAAE;AACF,+GAA+G;AAC/G,gHAAgH;AAChH,0GAA0G;AAC1G,mHAAmH;AACnH,4GAA4G;AAE5G,OAAO,EAAE,gBAAgB,EAAE,6BAA4B;AAGvD,OAAO,EAAE,UAAU,EAAE,uBAAmB;AACxC,OAAO,EAAE,YAAY,EAAE,aAAa,EAAE,YAAY,EAAE,oBAAgB;AACpE,OAAO,EAAE,gBAAgB,EAAE,2BAAuB;AAClD,OAAO,EAAE,WAAW,EAAE,oBAAgB;AACtC,OAAO,EAAE,iBAAiB,EAAE,yBAAqB;AACjD,OAAO,EAAE,gBAAgB,EAAE,0BAAsB;AAEjD,OAAO,EAAE,WAAW,EAAE,0BAAsB;AAC5C,OAAO,EAAE,eAAe,EAAE,aAAa,EAAE,0BAAsB;AAS/D,MAAM,IAAI,GAAG,CAAC,IAAY,EAAE,EAAE,CAAC,KAAK,IAAI,IAAI,CAAA;AAE5C;;;GAGG;AACH,MAAM,UAAU,aAAa,CAAC,MAAqB,EAAE,QAAqB,EAAE,KAAa,EAAE,UAA8C,EAAE;IACvI,MAAM,KAAK,GAAG,OAAO,CAAC,KAAK,IAAI,OAAO,CAAC,KAAK,IAAI,WAAW,CAAA;IAC3D,MAAM,UAAU,GAAG,gBAAgB,CAAC,MAAM,CAAC,CAAA;IAC3C,MAAM,MAAM,GAAG,IAAI,gBAAgB,CAAC,MAAM,EAAE,UAAU,EAAE,QA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AAC,IAAI,CAAC,KAAK,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,yBAAyB,CAAC,CAAA;IAC5H,IAAI,MAAM;QAAE,KAAK,CAAC,IAAI,CAAC,GAAG,KAAK,kBAAkB,KAAK,CAAC,MAAM,WAAW,MAAM,GAAG,CAAC,CAAA;IAClF,IAAI,QAAQ,CAAC,KAAK,IAAI,eAAe;QAAE,KAAK,CAAC,IAAI,CAAC,GAAG,KAAK,YAAY,eAAe,yBAAyB,CAAC,CAAA;IAC/G,OAAO,EAAE,KAAK,EAAE,KAAK,EAAE,CAAA;AAC3B,CAAC","sourcesContent":["// SPDX-License-Identifier: EPL-2.0\n//\n// Running a model of the in-browser engine on an input sequence, as the extension's \"Run models on a sequence\"\n// does on KIELER's server (trace-tools.ts runTicked, runTimed): a number of ticks, one per row (the last values\n// staying after the sequence ends); or, for a sequence with times, for a number of seconds, ticked at the\n// sequence's moments and as the model keeps time (TimedSchedule). The run's trace and the notes the extension puts\n// in its report come back; compareTraces and comparisonMarkdown({ preamble }) compare several models' runs.\n\nimport { createSimulation } from '@sccharts-lab/engine'\nimport type { CompileResult } from '@sccharts-lab/engine'\nimport type { Value } from '@sccharts-lab/engine/types'\nimport { inputValue } from './data-pool'\nimport { initialValue, poolVariables, traceFromRun } from './engine'\nimport { SimulationReplay } from './engine-replay'\nimport { isTimeDelta } from './helper'\nimport { isCompatibleInput } from './input-value'\nimport { formatTraceValue } from './trace-record'\nimport type { TraceRecord } from './trace-record'\nimport { coerceInput } from './trace-replay'\nimport { MAX_TIMED_TICKS, TimedSchedule } from './trace-timing'\nimport type { TickMode } from './trace-timing'\n\nexport interface SequenceRun {\n trace: TraceRecord\n /** What the extension's report says about the run: inputs the sequence does not set or the model lacks, ... */\n notes: string[]\n}\n\nconst code = (name: string) => `\\`${name}\\``\n\n/**\n * Runs `ticks` ticks of a fresh simulation with its inputs from `sequence` (in the model's names: `renameTrace`\n * first where they differ). `label` names the model in the notes.\n */\nexport function runOnSequence(result: CompileResult, sequence: TraceRecord, ticks: number, options: { label?: string; model?: string } = {}): SequenceRun {\n const label = options.label ?? options.model ?? 'The model'\n const simulation = createSimulation(result)\n const replay = new SimulationReplay(result, simulation, sequence, { label, checkOutputs: false })\n if (ticks > 0) replay.run((step) => step.tick.tick >= ticks)\n // After the sequence's end its last values stay.\n while (simulation.tick < ticks && !replay.error) simulation.step({})\n const trace = traceFromRun(result, simulation, { model: options.model })\n const notes: string[] = []\n const { unset, ignored } = replay.replay\n if (unset.length) {\n const kept = unset.map((name) => `${code(name)} = ${formatTraceValue(trace.ticks[0]?.[name])}`).join(', ')\n notes.push(`${label}: the sequence does not set ${kept} (the value at tick 1).`)\n }\n if (ignored.length) notes.push(`${label} has no input ${ignored.map(code).join(', ')}.`)\n if (replay.error) notes.push(`${label} stopped after ${trace.ticks.length} ticks: ${replay.error.message}.`)\n return { trace, notes }\n}\n\n/**\n * Runs a fresh simulation on a timed sequence (`sequence.times`) up to `until` seconds. A model with `deltaT` ticks at\n * the sequence's moments and when its `sleepT` wakes it, with the time since as `deltaT`; another model as `mode`\n * says (default: only at the sequence's moments). The trace has the time of each tick.\n */\nexport function runOnTimedSequence(\n result: CompileResult,\n sequence: TraceRecord,\n until: number,\n options: { label?: string; model?: string; mode?: TickMode } = {}\n): SequenceRun {\n const label = options.label ?? options.model ?? 'The model'\n const variables = poolVariables(result)\n const inputs = variables.filter((v) => v.input)\n const delta = inputs.find((v) => isTimeDelta({ id: v.name, categories: [] }))\n const sleep = variables.find((v) => /^[#_]*sleepT$/i.test(v.name))\n const notes: string[] = []\n let mode: TickMode = options.mode ?? { kind: 'events' }\n if (delta) {\n mode = { kind: 'clock', delta: delta.name, sleep: sleep?.name }\n } else if (mode.kind === 'timer' && !inputs.some((v) => v.name === (mode as { input: string }).input)) {\n notes.push(`${label} has no input ${code(mode.input)}, so it ticked only at the sequence's moments.`)\n mode = { kind: 'events' }\n }\n const simulation = createSimulation(result)\n let schedule: TimedSchedule\n try {\n schedule = new TimedSchedule(sequence, mode, until)\n } catch (error) {\n notes.push(`${label}: ${error instanceof Error ? error.message : error}`)\n return { trace: { variables: [], ticks: [] }, notes }\n }\n const byName = new Map(inputs.map((v) => [v.name, v]))\n const current = new Map<string, unknown>(inputs.map((v) => [v.name, initialValue(v)]))\n const times: number[] = []\n const unfit = new Set<string>()\n let wake: unknown\n let failed: string | undefined\n for (let tick = schedule.next(); tick; tick = schedule.next(wake)) {\n // As the extension queues them: in the inputs' types, refusing values that do not fit.\n const given: Record<string, Value> = {}\n for (const [name, raw] of Object.entries(tick.inputs)) {\n const input = byName.get(name)\n if (!input || raw === undefined) continue\n const value = coerceInput(raw, input.type)\n if (isCompatibleInput(value, current.get(name))) {\n given[name] = value as Value\n current.set(name, value)\n } else {\n unfit.add(name)\n }\n }\n const ran = simulation.step(given)\n if (ran.error) {\n failed = ran.error.message\n break\n }\n for (const v of inputs) if (ran.pool[v.name] !== undefined) current.set(v.name, inputValue(ran.pool[v.name], v.type))\n times.push(tick.time)\n wake = mode.kind === 'clock' && mode.sleep ? ran.pool[mode.sleep] : undefined\n }\n const run = traceFromRun(result, simulation, { model: options.model })\n const trace = { ...run, times: times.slice(0, run.ticks.length) }\n const set = new Set(sequence.variables.filter((v) => v.role !== 'output' && v.role !== 'local').map(({ name }) => name))\n const how =\n mode.kind === 'clock'\n ? `at the sequence's moments and when its clock woke it${mode.sleep ? ` (${code(mode.sleep)})` : ''}`\n : mode.kind === 'timer'\n ? `at the sequence's moments and every ${Number((mode.period * 1000).toFixed(6))} ms, with ${code(mode.input)} true on the timer's ticks`\n : \"only at the sequence's moments\"\n notes.push(`${label} ticked ${times.length} times, ${how}.`)\n const ignored = Array.from(set).filter((name) => !byName.has(name))\n if (ignored.length > 0) notes.push(`${label} has no input ${ignored.map(code).join(', ')}.`)\n const own = new Set([delta?.name, mode.kind === 'timer' ? mode.input : undefined])\n const unset = inputs.filter((v) => !set.has(v.name) && !own.has(v.name))\n if (unset.length > 0) {\n const kept = unset.map((v) => `${code(v.name)} = ${formatTraceValue(trace.ticks[0]?.[v.name])}`)\n notes.push(`${label}: the sequence does not set ${kept.join(', ')} (the value at the first tick).`)\n }\n if (unfit.size > 0) notes.push(`${label}: the sequence's values for ${Array.from(unfit).join(', ')} do not fit its inputs.`)\n if (failed) notes.push(`${label} stopped after ${times.length} ticks: ${failed}.`)\n if (schedule.ticks >= MAX_TIMED_TICKS) notes.push(`${label} reached ${MAX_TIMED_TICKS} ticks and was stopped.`)\n return { trace, notes }\n}\n"]}
|
package/dist/engine.d.ts
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import type { CompileResult, Simulation, SimTickResult } from "@sccharts-lab/engine";
|
|
2
|
+
import type { Value } from "@sccharts-lab/engine/types";
|
|
3
|
+
import type { TraceComparison } from "./trace-compare.js";
|
|
4
|
+
import type { TraceRecord, TraceVariable } from "./trace-record.js";
|
|
5
|
+
/** A variable of KIELER's data pool for a compiled chart. */
|
|
6
|
+
export interface PoolVariable {
|
|
7
|
+
name: string;
|
|
8
|
+
input: boolean;
|
|
9
|
+
output: boolean;
|
|
10
|
+
/** KIELER's type name: bool, int, float, string, class, struct. */
|
|
11
|
+
type: string;
|
|
12
|
+
/** Array dimensions (empty for scalars), for an input's value before the first tick. */
|
|
13
|
+
dims: number[];
|
|
14
|
+
/** Not in the pool before the first tick: a string without a value, a member declaration of a class or struct. */
|
|
15
|
+
late: boolean;
|
|
16
|
+
}
|
|
17
|
+
/** A completed run: a Simulation, or the ticks it returned. */
|
|
18
|
+
export type EngineRun = Simulation | readonly SimTickResult[];
|
|
19
|
+
type Compiled = Pick<CompileResult, 'sim' | 'chart' | 'interface' | 'index'>;
|
|
20
|
+
/**
|
|
21
|
+
* KIELER's data pool for the chart, in KIELER's order (its `#interface`), without the compiler's helpers. The names
|
|
22
|
+
* are the engine's pool's (exec.ts builds it from the same declarations). Empty when the chart did not compile.
|
|
23
|
+
*/
|
|
24
|
+
export declare function poolVariables(result: Compiled): PoolVariable[];
|
|
25
|
+
/** The columns of a trace of the chart's runs, as the extension saves them: name, role and KIELER's type. */
|
|
26
|
+
export declare function traceColumns(result: Compiled): TraceVariable[];
|
|
27
|
+
export interface TraceOptions {
|
|
28
|
+
/** The file name the trace names (the extension: the simulated model's file name). */
|
|
29
|
+
model?: string;
|
|
30
|
+
/**
|
|
31
|
+
* `pool` (the default, as the extension): an input's value after the tick, as KIELER's data pool has it. `read`:
|
|
32
|
+
* the value the tick read (`ranWith`), which differs where the chart writes its input (basics.md B-10); a trace
|
|
33
|
+
* replays such a run only with these.
|
|
34
|
+
*/
|
|
35
|
+
inputs?: 'pool' | 'read';
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* The run as the trace the extension saves for the same run on KIELER's server: every variable of KIELER's data pool
|
|
39
|
+
* after each tick, inputs, outputs and the chart's own locals, and what the chart printed. A tick with a runtime
|
|
40
|
+
* error ends the trace.
|
|
41
|
+
*/
|
|
42
|
+
export declare function traceFromRun(result: Compiled, run: EngineRun, options?: TraceOptions): TraceRecord;
|
|
43
|
+
/** Compares runs of the engine, the first being the reference (convenience over compareTraces). */
|
|
44
|
+
export declare function compareRuns(runs: ({
|
|
45
|
+
label: string;
|
|
46
|
+
result: Compiled;
|
|
47
|
+
run: EngineRun;
|
|
48
|
+
} & TraceOptions)[]): TraceComparison;
|
|
49
|
+
/** An input's value before the first tick, in the form the extension edits it (an unset string is ''). */
|
|
50
|
+
export declare function initialValue(variable: PoolVariable): Value;
|
|
51
|
+
export {};
|