@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.
Files changed (60) hide show
  1. package/LICENSE +277 -0
  2. package/README.md +233 -2
  3. package/dist/comparison-report.d.ts +15 -0
  4. package/dist/comparison-report.js +258 -0
  5. package/dist/comparison-report.js.map +1 -0
  6. package/dist/csv.d.ts +13 -0
  7. package/dist/csv.js +188 -0
  8. package/dist/csv.js.map +1 -0
  9. package/dist/data-pool.d.ts +2 -0
  10. package/dist/data-pool.js +26 -0
  11. package/dist/data-pool.js.map +1 -0
  12. package/dist/engine-replay.d.ts +78 -0
  13. package/dist/engine-replay.js +115 -0
  14. package/dist/engine-replay.js.map +1 -0
  15. package/dist/engine-sequence.d.ts +26 -0
  16. package/dist/engine-sequence.js +132 -0
  17. package/dist/engine-sequence.js.map +1 -0
  18. package/dist/engine.d.ts +51 -0
  19. package/dist/engine.js +210 -0
  20. package/dist/engine.js.map +1 -0
  21. package/dist/helper.d.ts +10 -0
  22. package/dist/helper.js +26 -0
  23. package/dist/helper.js.map +1 -0
  24. package/dist/index.d.ts +18 -0
  25. package/dist/index.js +18 -0
  26. package/dist/index.js.map +1 -0
  27. package/dist/input-value.d.ts +2 -0
  28. package/dist/input-value.js +38 -0
  29. package/dist/input-value.js.map +1 -0
  30. package/dist/json.d.ts +8 -0
  31. package/dist/json.js +122 -0
  32. package/dist/json.js.map +1 -0
  33. package/dist/ktrace.d.ts +11 -0
  34. package/dist/ktrace.js +266 -0
  35. package/dist/ktrace.js.map +1 -0
  36. package/dist/protocol.d.ts +25 -0
  37. package/dist/protocol.js +18 -0
  38. package/dist/protocol.js.map +1 -0
  39. package/dist/trace-compare.d.ts +77 -0
  40. package/dist/trace-compare.js +156 -0
  41. package/dist/trace-compare.js.map +1 -0
  42. package/dist/trace-file.d.ts +6 -0
  43. package/dist/trace-file.js +65 -0
  44. package/dist/trace-file.js.map +1 -0
  45. package/dist/trace-record.d.ts +71 -0
  46. package/dist/trace-record.js +181 -0
  47. package/dist/trace-record.js.map +1 -0
  48. package/dist/trace-rename.d.ts +40 -0
  49. package/dist/trace-rename.js +112 -0
  50. package/dist/trace-rename.js.map +1 -0
  51. package/dist/trace-replay.d.ts +72 -0
  52. package/dist/trace-replay.js +169 -0
  53. package/dist/trace-replay.js.map +1 -0
  54. package/dist/trace-timing.d.ts +55 -0
  55. package/dist/trace-timing.js +122 -0
  56. package/dist/trace-timing.js.map +1 -0
  57. package/dist/xml.d.ts +8 -0
  58. package/dist/xml.js +179 -0
  59. package/dist/xml.js.map +1 -0
  60. package/package.json +70 -3
@@ -0,0 +1,169 @@
1
+ /*
2
+ * KIELER - Kiel Integrated Environment for Layout Eclipse RichClient
3
+ *
4
+ * http://rtsys.informatik.uni-kiel.de/kieler
5
+ *
6
+ * Copyright 2026 by
7
+ * + Kiel University
8
+ * + Department of Computer Science
9
+ * + Real-Time and Embedded Systems Group
10
+ *
11
+ * This program and the accompanying materials are made available under the
12
+ * terms of the Eclipse Public License 2.0 which is available at
13
+ * http://www.eclipse.org/legal/epl-2.0.
14
+ *
15
+ * SPDX-License-Identifier: EPL-2.0
16
+ */
17
+ import { inputValue } from "./data-pool.js";
18
+ import { isCompatibleInput } from "./input-value.js";
19
+ import { formatTraceValue, sameTraceValue } from "./trace-record.js";
20
+ const MAX_PROBLEMS = 5;
21
+ /** A file value in the model's terms: strings for string inputs, 0 and 1 for booleans. */
22
+ export function coerceInput(value, type) {
23
+ if (Array.isArray(value))
24
+ return value.map((entry) => coerceInput(entry, type));
25
+ if (type === 'string' && (typeof value === 'number' || typeof value === 'boolean'))
26
+ return String(value);
27
+ if (type === 'bool' && (value === 0 || value === 1))
28
+ return value === 1;
29
+ return inputValue(value, type);
30
+ }
31
+ /**
32
+ * The file's inputs are matched to the model's by name. Inputs the model does not have are
33
+ * ignored, and the model's other inputs stay under the user's control. The file's first row is
34
+ * the tick after `offset`, and the tick number alone decides the values, so stepping back and
35
+ * running again gives the same run. Ticks below are the simulation's, rows the file's.
36
+ */
37
+ export class TraceReplay {
38
+ label;
39
+ trace;
40
+ checkOutputs;
41
+ modelUri;
42
+ offset;
43
+ /** Inputs the file sets and the model reads. */
44
+ driven;
45
+ /** Inputs in the file that the model does not have. */
46
+ ignored;
47
+ /** Inputs of the model that the file does not set. */
48
+ unset;
49
+ /** Outputs in both, checked after every tick the file covers. */
50
+ checked;
51
+ problems = [];
52
+ mismatches = new Map();
53
+ targets;
54
+ constructor(label, trace, targets,
55
+ /** False when another model's outputs are not expected to match, as in a batch comparison. */
56
+ checkOutputs = true,
57
+ /** The model the replay belongs to; a restart of the same model keeps replaying. */
58
+ modelUri,
59
+ /** The simulation's tick before the file's first row: the tick at which replaying began. */
60
+ offset = 0) {
61
+ this.label = label;
62
+ this.trace = trace;
63
+ this.checkOutputs = checkOutputs;
64
+ this.modelUri = modelUri;
65
+ this.offset = offset;
66
+ this.targets = new Map(targets.map((target) => [target.id, target]));
67
+ const fileInputs = trace.variables.filter((variable) => variable.role !== 'output' && variable.role !== 'local');
68
+ this.driven = fileInputs.filter(({ name }) => this.targets.get(name)?.input).map(({ name }) => name);
69
+ this.ignored = fileInputs
70
+ .filter(({ name, role }) => role === 'input' && !this.targets.get(name)?.input)
71
+ .map(({ name }) => name);
72
+ this.unset = targets.filter((target) => target.input && !this.driven.includes(target.id)).map(({ id }) => id);
73
+ this.checked = checkOutputs
74
+ ? trace.variables
75
+ .filter(({ name, role }) => role !== 'input' && role !== 'local' && this.targets.get(name)?.output)
76
+ .map(({ name }) => name)
77
+ : [];
78
+ }
79
+ get length() {
80
+ return this.trace.ticks.length;
81
+ }
82
+ /** The simulation's tick at which the file ends. */
83
+ get end() {
84
+ return this.offset + this.length;
85
+ }
86
+ /** The file's row for a tick of the simulation, if the file covers that tick. */
87
+ row(tick) {
88
+ return tick > this.offset ? this.trace.ticks[tick - this.offset - 1] : undefined;
89
+ }
90
+ /** The same file and settings against a fresh run of a model, e.g. after a restart, from tick 1. */
91
+ rebind(targets, modelUri) {
92
+ return new TraceReplay(this.label, this.trace, targets, this.checkOutputs, modelUri);
93
+ }
94
+ /**
95
+ * The values the model reads at `tick` (1-based), in its types. `current` gives an input's
96
+ * present value, which a file value must fit. Empty outside the file.
97
+ */
98
+ inputsFor(tick, current) {
99
+ const values = new Map();
100
+ const row = this.row(tick);
101
+ if (!row)
102
+ return values;
103
+ this.driven.forEach((name) => {
104
+ if (row[name] === undefined)
105
+ return;
106
+ const target = this.targets.get(name);
107
+ const value = coerceInput(row[name], target.type);
108
+ if (isCompatibleInput(value, current(name))) {
109
+ values.set(name, value);
110
+ }
111
+ else if (this.problems.length < MAX_PROBLEMS) {
112
+ const problem = `${name} cannot be ${formatTraceValue(row[name])} (row ${tick - this.offset})`;
113
+ if (!this.problems.includes(problem))
114
+ this.problems.push(problem);
115
+ }
116
+ });
117
+ return values;
118
+ }
119
+ /** Compares the model's outputs after `tick` with the file's. */
120
+ check(tick, actual) {
121
+ const row = this.row(tick);
122
+ if (!row)
123
+ return [];
124
+ const found = this.checked
125
+ .filter((name) => row[name] !== undefined && !sameTraceValue(actual(name), row[name]))
126
+ .map((name) => ({ tick, name, expected: row[name], actual: actual(name) }));
127
+ if (found.length > 0)
128
+ this.mismatches.set(tick, found);
129
+ else
130
+ this.mismatches.delete(tick);
131
+ return found;
132
+ }
133
+ /** Forgets what was checked after `tick`, when the simulation went back to it. */
134
+ rewindTo(tick) {
135
+ Array.from(this.mismatches.keys())
136
+ .filter((key) => key > tick)
137
+ .forEach((key) => this.mismatches.delete(key));
138
+ }
139
+ /** The file asks to stop running after this tick (KTrace `pause`). */
140
+ pausesAfter(tick) {
141
+ return this.trace.pauses?.includes(tick - this.offset) ?? false;
142
+ }
143
+ /** Where the file's value differs from the model's, for one variable. */
144
+ mismatchesOf(name) {
145
+ return Array.from(this.mismatches.values()).flatMap((entries) => entries.filter((entry) => entry.name === name));
146
+ }
147
+ state(tick) {
148
+ const ticks = Array.from(this.mismatches.keys()).sort((a, b) => a - b);
149
+ const first = ticks.length > 0 ? this.mismatches.get(ticks[0]) : undefined;
150
+ return {
151
+ label: this.label,
152
+ length: this.length,
153
+ tick: Math.max(0, Math.min(tick - this.offset, this.length)),
154
+ checked: this.checked,
155
+ ignored: this.ignored,
156
+ unset: this.unset,
157
+ mismatchTicks: ticks.length,
158
+ firstMismatch: first && {
159
+ tick: ticks[0],
160
+ text: first
161
+ .map((entry) => `${entry.name} is ${formatTraceValue(entry.actual)}, the trace has ${formatTraceValue(entry.expected)}`)
162
+ .join('; '),
163
+ },
164
+ problems: [...this.problems],
165
+ notes: this.trace.notes ?? [],
166
+ };
167
+ }
168
+ }
169
+ //# sourceMappingURL=trace-replay.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"trace-replay.js","sourceRoot":"","sources":["../src/trace-replay.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AAEH,OAAO,EAAE,UAAU,EAAE,uBAAmB;AACxC,OAAO,EAAE,iBAAiB,EAAE,yBAAqB;AAEjD,OAAO,EAAE,gBAAgB,EAAE,cAAc,EAAE,0BAAsB;AAqBjE,MAAM,YAAY,GAAG,CAAC,CAAA;AAEtB,0FAA0F;AAC1F,MAAM,UAAU,WAAW,CAAC,KAAc,EAAE,IAAwB;IAChE,IAAI,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC;QAAE,OAAO,KAAK,CAAC,GAAG,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,WAAW,CAAC,KAAK,EAAE,IAAI,CAAC,CAAC,CAAA;IAC/E,IAAI,IAAI,KAAK,QAAQ,IAAI,CAAC,OAAO,KAAK,KAAK,QAAQ,IAAI,OAAO,KAAK,KAAK,SAAS,CAAC;QAAE,OAAO,MAAM,CAAC,KAAK,CAAC,CAAA;IACxG,IAAI,IAAI,KAAK,MAAM,IAAI,CAAC,KAAK,KAAK,CAAC,IAAI,KAAK,KAAK,CAAC,CAAC;QAAE,OAAO,KAAK,KAAK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a,EAAE,KAAK,IAAI;gBACpB,IAAI,EAAE,KAAK,CAAC,CAAC,CAAC;gBACd,IAAI,EAAE,KAAK;qBACN,GAAG,CACA,CAAC,KAAK,EAAE,EAAE,CACN,GAAG,KAAK,CAAC,IAAI,OAAO,gBAAgB,CAAC,KAAK,CAAC,MAAM,CAAC,mBAAmB,gBAAgB,CACjF,KAAK,CAAC,QAAQ,CACjB,EAAE,CACV;qBACA,IAAI,CAAC,IAAI,CAAC;aAClB;YACD,QAAQ,EAAE,CAAC,GAAG,IAAI,CAAC,QAAQ,CAAC;YAC5B,KAAK,EAAE,IAAI,CAAC,KAAK,CAAC,KAAK,IAAI,EAAE;SAChC,CAAA;IACL,CAAC;CACJ","sourcesContent":["/*\n * KIELER - Kiel Integrated Environment for Layout Eclipse RichClient\n *\n * http://rtsys.informatik.uni-kiel.de/kieler\n *\n * Copyright 2026 by\n * + Kiel University\n * + Department of Computer Science\n * + Real-Time and Embedded Systems Group\n *\n * This program and the accompanying materials are made available under the\n * terms of the Eclipse Public License 2.0 which is available at\n * http://www.eclipse.org/legal/epl-2.0.\n *\n * SPDX-License-Identifier: EPL-2.0\n */\n\nimport { inputValue } from './data-pool'\nimport { isCompatibleInput } from './input-value'\nimport type { ReplayState } from './protocol'\nimport { formatTraceValue, sameTraceValue } from './trace-record'\nimport type { TraceRecord } from './trace-record'\n\n// Drives a running simulation's inputs from a trace file, tick by tick, and checks its outputs\n// against the file's.\n\n/** A variable of the running model, as far as replaying is concerned. */\nexport interface ReplayTarget {\n id: string\n input: boolean\n output: boolean\n type?: string\n}\n\nexport interface ReplayMismatch {\n tick: number\n name: string\n expected: unknown\n actual: unknown\n}\n\nconst MAX_PROBLEMS = 5\n\n/** A file value in the model's terms: strings for string inputs, 0 and 1 for booleans. */\nexport function coerceInput(value: unknown, type: string | undefined): unknown {\n if (Array.isArray(value)) return value.map((entry) => coerceInput(entry, type))\n if (type === 'string' && (typeof value === 'number' || typeof value === 'boolean')) return String(value)\n if (type === 'bool' && (value === 0 || value === 1)) return value === 1\n return inputValue(value, type)\n}\n\n/**\n * The file's inputs are matched to the model's by name. Inputs the model does not have are\n * ignored, and the model's other inputs stay under the user's control. The file's first row is\n * the tick after `offset`, and the tick number alone decides the values, so stepping back and\n * running again gives the same run. Ticks below are the simulation's, rows the file's.\n */\nexport class TraceReplay {\n /** Inputs the file sets and the model reads. */\n readonly driven: string[]\n\n /** Inputs in the file that the model does not have. */\n readonly ignored: string[]\n\n /** Inputs of the model that the file does not set. */\n readonly unset: string[]\n\n /** Outputs in both, checked after every tick the file covers. */\n readonly checked: string[]\n\n private readonly problems: string[] = []\n\n private readonly mismatches = new Map<number, ReplayMismatch[]>()\n\n private readonly targets: Map<string, ReplayTarget>\n\n constructor(\n readonly label: string,\n readonly trace: TraceRecord,\n targets: ReplayTarget[],\n /** False when another model's outputs are not expected to match, as in a batch comparison. */\n readonly checkOutputs = true,\n /** The model the replay belongs to; a restart of the same model keeps replaying. */\n readonly modelUri?: string,\n /** The simulation's tick before the file's first row: the tick at which replaying began. */\n readonly offset = 0\n ) {\n this.targets = new Map(targets.map((target) => [target.id, target]))\n const fileInputs = trace.variables.filter((variable) => variable.role !== 'output' && variable.role !== 'local')\n this.driven = fileInputs.filter(({ name }) => this.targets.get(name)?.input).map(({ name }) => name)\n this.ignored = fileInputs\n .filter(({ name, role }) => role === 'input' && !this.targets.get(name)?.input)\n .map(({ name }) => name)\n this.unset = targets.filter((target) => target.input && !this.driven.includes(target.id)).map(({ id }) => id)\n this.checked = checkOutputs\n ? trace.variables\n .filter(({ name, role }) => role !== 'input' && role !== 'local' && this.targets.get(name)?.output)\n .map(({ name }) => name)\n : []\n }\n\n get length(): number {\n return this.trace.ticks.length\n }\n\n /** The simulation's tick at which the file ends. */\n get end(): number {\n return this.offset + this.length\n }\n\n /** The file's row for a tick of the simulation, if the file covers that tick. */\n private row(tick: number): Record<string, unknown> | undefined {\n return tick > this.offset ? this.trace.ticks[tick - this.offset - 1] : undefined\n }\n\n /** The same file and settings against a fresh run of a model, e.g. after a restart, from tick 1. */\n rebind(targets: ReplayTarget[], modelUri?: string): TraceReplay {\n return new TraceReplay(this.label, this.trace, targets, this.checkOutputs, modelUri)\n }\n\n /**\n * The values the model reads at `tick` (1-based), in its types. `current` gives an input's\n * present value, which a file value must fit. Empty outside the file.\n */\n inputsFor(tick: number, current: (id: string) => unknown): Map<string, unknown> {\n const values = new Map<string, unknown>()\n const row = this.row(tick)\n if (!row) return values\n this.driven.forEach((name) => {\n if (row[name] === undefined) return\n const target = this.targets.get(name)!\n const value = coerceInput(row[name], target.type)\n if (isCompatibleInput(value, current(name))) {\n values.set(name, value)\n } else if (this.problems.length < MAX_PROBLEMS) {\n const problem = `${name} cannot be ${formatTraceValue(row[name])} (row ${tick - this.offset})`\n if (!this.problems.includes(problem)) this.problems.push(problem)\n }\n })\n return values\n }\n\n /** Compares the model's outputs after `tick` with the file's. */\n check(tick: number, actual: (id: string) => unknown): ReplayMismatch[] {\n const row = this.row(tick)\n if (!row) return []\n const found = this.checked\n .filter((name) => row[name] !== undefined && !sameTraceValue(actual(name), row[name]))\n .map((name) => ({ tick, name, expected: row[name], actual: actual(name) }))\n if (found.length > 0) this.mismatches.set(tick, found)\n else this.mismatches.delete(tick)\n return found\n }\n\n /** Forgets what was checked after `tick`, when the simulation went back to it. */\n rewindTo(tick: number): void {\n Array.from(this.mismatches.keys())\n .filter((key) => key > tick)\n .forEach((key) => this.mismatches.delete(key))\n }\n\n /** The file asks to stop running after this tick (KTrace `pause`). */\n pausesAfter(tick: number): boolean {\n return this.trace.pauses?.includes(tick - this.offset) ?? false\n }\n\n /** Where the file's value differs from the model's, for one variable. */\n mismatchesOf(name: string): ReplayMismatch[] {\n return Array.from(this.mismatches.values()).flatMap((entries) => entries.filter((entry) => entry.name === name))\n }\n\n state(tick: number): ReplayState {\n const ticks = Array.from(this.mismatches.keys()).sort((a, b) => a - b)\n const first = ticks.length > 0 ? this.mismatches.get(ticks[0]) : undefined\n return {\n label: this.label,\n length: this.length,\n tick: Math.max(0, Math.min(tick - this.offset, this.length)),\n checked: this.checked,\n ignored: this.ignored,\n unset: this.unset,\n mismatchTicks: ticks.length,\n firstMismatch: first && {\n tick: ticks[0],\n text: first\n .map(\n (entry) =>\n `${entry.name} is ${formatTraceValue(entry.actual)}, the trace has ${formatTraceValue(\n entry.expected\n )}`\n )\n .join('; '),\n },\n problems: [...this.problems],\n notes: this.trace.notes ?? [],\n }\n }\n}\n"]}
@@ -0,0 +1,55 @@
1
+ import type { TraceRecord } from "./trace-record.js";
2
+ /** A run stops after this many ticks, in case a model keeps asking to wake up at once. */
3
+ export declare const MAX_TIMED_TICKS = 1000000;
4
+ /**
5
+ * How a model is ticked. `clock`: at the sequence's moments and whenever the model asks to wake
6
+ * up (its sleep output), with the time since the last tick as the time delta, as KIELER's
7
+ * dynamic ticks do. `timer`: at the sequence's moments and every `period` seconds, with
8
+ * `input` true on the timer's ticks and false on the others. `events`: at the sequence's moments only.
9
+ */
10
+ export type TickMode = {
11
+ kind: 'clock';
12
+ delta: string;
13
+ sleep?: string;
14
+ } | {
15
+ kind: 'timer';
16
+ input: string;
17
+ period: number;
18
+ } | {
19
+ kind: 'events';
20
+ };
21
+ export interface TimedTick {
22
+ /** Seconds since the start of the run. */
23
+ time: number;
24
+ /** What the model reads: the sequence's values at this moment, plus the time delta or the timer. */
25
+ inputs: Record<string, unknown>;
26
+ /** For a timer: the timer fired at this tick. */
27
+ timer: boolean;
28
+ }
29
+ /** The moments of a timed sequence, with what changes at each. */
30
+ export declare function timedEvents(sequence: TraceRecord): {
31
+ time: number;
32
+ values: Record<string, unknown>;
33
+ }[];
34
+ /**
35
+ * Hands out the ticks of one run in order. The first tick is at time 0. Call `next` with the
36
+ * model's sleep output after each tick; it returns undefined once the run reaches `until`.
37
+ */
38
+ export declare class TimedSchedule {
39
+ private readonly mode;
40
+ /** Seconds; the last tick is at or before this moment. */
41
+ private readonly until;
42
+ private readonly events;
43
+ private index;
44
+ private now;
45
+ private started;
46
+ private count;
47
+ constructor(sequence: TraceRecord, mode: TickMode,
48
+ /** Seconds; the last tick is at or before this moment. */
49
+ until: number);
50
+ /** Ticks handed out so far. */
51
+ get ticks(): number;
52
+ next(sleep?: unknown): TimedTick | undefined;
53
+ /** The first timer edge after the last tick. */
54
+ private nextTimer;
55
+ }
@@ -0,0 +1,122 @@
1
+ /*
2
+ * KIELER - Kiel Integrated Environment for Layout Eclipse RichClient
3
+ *
4
+ * http://rtsys.informatik.uni-kiel.de/kieler
5
+ *
6
+ * Copyright 2026 by
7
+ * + Kiel University
8
+ * + Department of Computer Science
9
+ * + Real-Time and Embedded Systems Group
10
+ *
11
+ * This program and the accompanying materials are made available under the
12
+ * terms of the Eclipse Public License 2.0 which is available at
13
+ * http://www.eclipse.org/legal/epl-2.0.
14
+ *
15
+ * SPDX-License-Identifier: EPL-2.0
16
+ */
17
+ // When the ticks of a run on a timed sequence happen. A timed sequence lists input changes at
18
+ // moments in seconds rather than per tick, so models that keep time differently (clocks, or a
19
+ // counted timer input) can be run under the same conditions.
20
+ /** Times closer than this are the same moment: float sums of periods never land exactly. */
21
+ const EPSILON = 1e-9;
22
+ /** A run stops after this many ticks, in case a model keeps asking to wake up at once. */
23
+ export const MAX_TIMED_TICKS = 1000000;
24
+ /** The moments of a timed sequence, with what changes at each. */
25
+ export function timedEvents(sequence) {
26
+ const { times } = sequence;
27
+ if (!times)
28
+ throw new Error('The sequence has no time column.');
29
+ let last = -Infinity;
30
+ return sequence.ticks.map((values, index) => {
31
+ const time = times[index];
32
+ if (!Number.isFinite(time) || time < 0) {
33
+ throw new Error(`Row ${index + 1} of the sequence has no valid time.`);
34
+ }
35
+ if (time < last - EPSILON) {
36
+ throw new Error(`The sequence's times must not go backwards: row ${index + 1} is at ${time} s, after ${last} s.`);
37
+ }
38
+ last = time;
39
+ const changed = {};
40
+ Object.entries(values).forEach(([name, value]) => {
41
+ if (value !== undefined)
42
+ changed[name] = value;
43
+ });
44
+ return { time, values: changed };
45
+ });
46
+ }
47
+ /**
48
+ * Hands out the ticks of one run in order. The first tick is at time 0. Call `next` with the
49
+ * model's sleep output after each tick; it returns undefined once the run reaches `until`.
50
+ */
51
+ export class TimedSchedule {
52
+ mode;
53
+ until;
54
+ events;
55
+ index = 0;
56
+ now = 0;
57
+ started = false;
58
+ count = 0;
59
+ constructor(sequence, mode,
60
+ /** Seconds; the last tick is at or before this moment. */
61
+ until) {
62
+ this.mode = mode;
63
+ this.until = until;
64
+ this.events = timedEvents(sequence);
65
+ const inputs = sequence.variables.filter((variable) => variable.role !== 'output' && variable.role !== 'local');
66
+ if (mode.kind === 'clock' && inputs.some((variable) => variable.name === mode.delta)) {
67
+ throw new Error(`A timed sequence must not set ${mode.delta}: the run computes it from the times.`);
68
+ }
69
+ if (mode.kind === 'timer' && !(mode.period > 0))
70
+ throw new Error('The timer period must be positive.');
71
+ }
72
+ /** Ticks handed out so far. */
73
+ get ticks() {
74
+ return this.count;
75
+ }
76
+ next(sleep) {
77
+ if (this.count >= MAX_TIMED_TICKS)
78
+ return undefined;
79
+ let time;
80
+ if (!this.started) {
81
+ time = 0;
82
+ }
83
+ else {
84
+ const candidates = [this.events[this.index]?.time ?? Infinity];
85
+ if (this.mode.kind === 'clock' && typeof sleep === 'number' && sleep > 0) {
86
+ // A float residue can leave a sleep too short to move the clock; still honour it.
87
+ candidates.push(this.now + Math.max(sleep, 1e-6));
88
+ }
89
+ if (this.mode.kind === 'timer')
90
+ candidates.push(this.nextTimer());
91
+ time = Math.min(...candidates);
92
+ if (!Number.isFinite(time) || time > this.until + EPSILON)
93
+ return undefined;
94
+ }
95
+ const inputs = {};
96
+ while (this.index < this.events.length && this.events[this.index].time <= time + EPSILON) {
97
+ Object.assign(inputs, this.events[this.index].values);
98
+ this.index++;
99
+ }
100
+ let timer = false;
101
+ if (this.mode.kind === 'clock') {
102
+ inputs[this.mode.delta] = this.started ? round(time - this.now) : 0;
103
+ }
104
+ else if (this.mode.kind === 'timer') {
105
+ timer = this.started && Math.abs(time - this.nextTimer()) <= EPSILON;
106
+ inputs[this.mode.input] = timer;
107
+ }
108
+ this.started = true;
109
+ this.now = time;
110
+ this.count++;
111
+ return { time: round(time), inputs, timer };
112
+ }
113
+ /** The first timer edge after the last tick. */
114
+ nextTimer() {
115
+ const { period } = this.mode;
116
+ return (Math.floor(this.now / period + EPSILON) + 1) * period;
117
+ }
118
+ }
119
+ function round(value) {
120
+ return Math.round(value * 1e9) / 1e9;
121
+ }
122
+ //# sourceMappingURL=trace-timing.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"trace-timing.js","sourceRoot":"","sources":["../src/trace-timing.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AAIH,8FAA8F;AAC9F,8FAA8F;AAC9F,6DAA6D;AAE7D,4FAA4F;AAC5F,MAAM,OAAO,GAAG,IAAI,CAAA;AAEpB,0FAA0F;AAC1F,MAAM,CAAC,MAAM,eAAe,GAAG,OAAO,CAAA;AAsBtC,kEAAkE;AAClE,MAAM,UAAU,WAAW,CAAC,QAAqB;IAC7C,MAAM,EAAE,KAAK,EAAE,GAAG,QAAQ,CAAA;IAC1B,IAAI,CAAC,KAAK;QAAE,MAAM,IAAI,KAAK,CAAC,kCAAkC,CAAC,CAAA;IAC/D,IAAI,IAAI,GAAG,CAAC,QAAQ,CAAA;IACpB,OAAO,QAAQ,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,MAAM,EAAE,KAAK,EAAE,EAAE;QACxC,MAAM,IAAI,GAAG,KAAK,CAAC,KAAK,CAAC,CAAA;QACzB,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,IAAI,CAAC,IAAI,IAAI,GAAG,CAAC,EAAE,CAAC;YACrC,MAAM,IAAI,KAAK,CAAC,OAAO,KAAK,GAAG,CAAC,qCAAqC,CAAC,CAAA;QAC1E,CAAC;QACD,IAAI,IAAI,GAAG,IAAI,GAAG,OAAO,EAAE,CAAC;YACxB,MAAM,IAAI,KAAK,CACX,mDAAmD,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AI,CAAC,GAAG,GAAG,MAAM,GAAG,OAAO,CAAC,GAAG,CAAC,CAAC,GAAG,MAAM,CAAA;IACjE,CAAC;CACJ;AAED,SAAS,KAAK,CAAC,KAAa;IACxB,OAAO,IAAI,CAAC,KAAK,CAAC,KAAK,GAAG,GAAG,CAAC,GAAG,GAAG,CAAA;AACxC,CAAC","sourcesContent":["/*\n * KIELER - Kiel Integrated Environment for Layout Eclipse RichClient\n *\n * http://rtsys.informatik.uni-kiel.de/kieler\n *\n * Copyright 2026 by\n * + Kiel University\n * + Department of Computer Science\n * + Real-Time and Embedded Systems Group\n *\n * This program and the accompanying materials are made available under the\n * terms of the Eclipse Public License 2.0 which is available at\n * http://www.eclipse.org/legal/epl-2.0.\n *\n * SPDX-License-Identifier: EPL-2.0\n */\n\nimport type { TraceRecord } from './trace-record'\n\n// When the ticks of a run on a timed sequence happen. A timed sequence lists input changes at\n// moments in seconds rather than per tick, so models that keep time differently (clocks, or a\n// counted timer input) can be run under the same conditions.\n\n/** Times closer than this are the same moment: float sums of periods never land exactly. */\nconst EPSILON = 1e-9\n\n/** A run stops after this many ticks, in case a model keeps asking to wake up at once. */\nexport const MAX_TIMED_TICKS = 1000000\n\n/**\n * How a model is ticked. `clock`: at the sequence's moments and whenever the model asks to wake\n * up (its sleep output), with the time since the last tick as the time delta, as KIELER's\n * dynamic ticks do. `timer`: at the sequence's moments and every `period` seconds, with\n * `input` true on the timer's ticks and false on the others. `events`: at the sequence's moments only.\n */\nexport type TickMode =\n | { kind: 'clock'; delta: string; sleep?: string }\n | { kind: 'timer'; input: string; period: number }\n | { kind: 'events' }\n\nexport interface TimedTick {\n /** Seconds since the start of the run. */\n time: number\n /** What the model reads: the sequence's values at this moment, plus the time delta or the timer. */\n inputs: Record<string, unknown>\n /** For a timer: the timer fired at this tick. */\n timer: boolean\n}\n\n/** The moments of a timed sequence, with what changes at each. */\nexport function timedEvents(sequence: TraceRecord): { time: number; values: Record<string, unknown> }[] {\n const { times } = sequence\n if (!times) throw new Error('The sequence has no time column.')\n let last = -Infinity\n return sequence.ticks.map((values, index) => {\n const time = times[index]\n if (!Number.isFinite(time) || time < 0) {\n throw new Error(`Row ${index + 1} of the sequence has no valid time.`)\n }\n if (time < last - EPSILON) {\n throw new Error(\n `The sequence's times must not go backwards: row ${index + 1} is at ${time} s, after ${last} s.`\n )\n }\n last = time\n const changed: Record<string, unknown> = {}\n Object.entries(values).forEach(([name, value]) => {\n if (value !== undefined) changed[name] = value\n })\n return { time, values: changed }\n })\n}\n\n/**\n * Hands out the ticks of one run in order. The first tick is at time 0. Call `next` with the\n * model's sleep output after each tick; it returns undefined once the run reaches `until`.\n */\nexport class TimedSchedule {\n private readonly events: { time: number; values: Record<string, unknown> }[]\n\n private index = 0\n\n private now = 0\n\n private started = false\n\n private count = 0\n\n constructor(\n sequence: TraceRecord,\n private readonly mode: TickMode,\n /** Seconds; the last tick is at or before this moment. */\n private readonly until: number\n ) {\n this.events = timedEvents(sequence)\n const inputs = sequence.variables.filter((variable) => variable.role !== 'output' && variable.role !== 'local')\n if (mode.kind === 'clock' && inputs.some((variable) => variable.name === mode.delta)) {\n throw new Error(`A timed sequence must not set ${mode.delta}: the run computes it from the times.`)\n }\n if (mode.kind === 'timer' && !(mode.period > 0)) throw new Error('The timer period must be positive.')\n }\n\n /** Ticks handed out so far. */\n get ticks(): number {\n return this.count\n }\n\n next(sleep?: unknown): TimedTick | undefined {\n if (this.count >= MAX_TIMED_TICKS) return undefined\n let time: number\n if (!this.started) {\n time = 0\n } else {\n const candidates = [this.events[this.index]?.time ?? Infinity]\n if (this.mode.kind === 'clock' && typeof sleep === 'number' && sleep > 0) {\n // A float residue can leave a sleep too short to move the clock; still honour it.\n candidates.push(this.now + Math.max(sleep, 1e-6))\n }\n if (this.mode.kind === 'timer') candidates.push(this.nextTimer())\n time = Math.min(...candidates)\n if (!Number.isFinite(time) || time > this.until + EPSILON) return undefined\n }\n const inputs: Record<string, unknown> = {}\n while (this.index < this.events.length && this.events[this.index].time <= time + EPSILON) {\n Object.assign(inputs, this.events[this.index].values)\n this.index++\n }\n let timer = false\n if (this.mode.kind === 'clock') {\n inputs[this.mode.delta] = this.started ? round(time - this.now) : 0\n } else if (this.mode.kind === 'timer') {\n timer = this.started && Math.abs(time - this.nextTimer()) <= EPSILON\n inputs[this.mode.input] = timer\n }\n this.started = true\n this.now = time\n this.count++\n return { time: round(time), inputs, timer }\n }\n\n /** The first timer edge after the last tick. */\n private nextTimer(): number {\n const { period } = this.mode as { period: number }\n return (Math.floor(this.now / period + EPSILON) + 1) * period\n }\n}\n\nfunction round(value: number): number {\n return Math.round(value * 1e9) / 1e9\n}\n"]}
package/dist/xml.d.ts ADDED
@@ -0,0 +1,8 @@
1
+ import type { TraceRecord } from "./trace-record.js";
2
+ /**
3
+ * `<trace>` with the `<variable>` declarations, then one `<tick n="1" time="…">` per tick holding
4
+ * `<input>`, `<output>`, `<local>` (or `<value>` for an unknown role) elements, and a `<printed>`
5
+ * per line the model printed.
6
+ */
7
+ export declare function writeXml(trace: TraceRecord): string;
8
+ export declare function readXml(text: string): TraceRecord;
package/dist/xml.js ADDED
@@ -0,0 +1,179 @@
1
+ /*
2
+ * KIELER - Kiel Integrated Environment for Layout Eclipse RichClient
3
+ *
4
+ * http://rtsys.informatik.uni-kiel.de/kieler
5
+ *
6
+ * Copyright 2026 by
7
+ * + Kiel University
8
+ * + Department of Computer Science
9
+ * + Real-Time and Embedded Systems Group
10
+ *
11
+ * This program and the accompanying materials are made available under the
12
+ * terms of the Eclipse Public License 2.0 which is available at
13
+ * http://www.eclipse.org/legal/epl-2.0.
14
+ *
15
+ * SPDX-License-Identifier: EPL-2.0
16
+ */
17
+ import { carryForward, decodeText, formatTraceValue, givenTimes, printedOf, TRACE_FORMAT_ID, TraceFileError, traceTimes, typeOfValue, VariableList, } from "./trace-record.js";
18
+ // This lab's XML trace.
19
+ function xmlEscape(text) {
20
+ return text
21
+ .replace(/&/g, '&amp;')
22
+ .replace(/</g, '&lt;')
23
+ .replace(/>/g, '&gt;')
24
+ .replace(/"/g, '&quot;')
25
+ .replace(/[\r\n\t]/g, (ch) => `&#${ch.charCodeAt(0)};`);
26
+ }
27
+ function xmlUnescape(text) {
28
+ return text.replace(/&(#x[0-9a-fA-F]+|#\d+|amp|lt|gt|quot|apos);/g, (_, entity) => {
29
+ if (entity.startsWith('#x'))
30
+ return String.fromCodePoint(Number.parseInt(entity.slice(2), 16));
31
+ if (entity.startsWith('#'))
32
+ return String.fromCodePoint(Number(entity.slice(1)));
33
+ return { amp: '&', lt: '<', gt: '>', quot: '"', apos: "'" }[entity] ?? '';
34
+ });
35
+ }
36
+ const XML_ELEMENT = { input: 'input', output: 'output', local: 'local' };
37
+ /**
38
+ * `<trace>` with the `<variable>` declarations, then one `<tick n="1" time="…">` per tick holding
39
+ * `<input>`, `<output>`, `<local>` (or `<value>` for an unknown role) elements, and a `<printed>`
40
+ * per line the model printed.
41
+ */
42
+ export function writeXml(trace) {
43
+ const times = traceTimes(trace);
44
+ const attribute = (name, value) => (value ? ` ${name}="${xmlEscape(value)}"` : '');
45
+ const lines = [
46
+ '<?xml version="1.0" encoding="UTF-8"?>',
47
+ `<trace format="${TRACE_FORMAT_ID}" version="1"${attribute('model', trace.model)}>`,
48
+ ...trace.variables.map(({ name, role, type }) => ` <variable${attribute('name', name)}${attribute('role', role)}${attribute('type', type)}/>`),
49
+ ];
50
+ trace.ticks.forEach((tick, index) => {
51
+ lines.push(` <tick n="${index + 1}"${times ? ` time="${times[index]}"` : ''}>`);
52
+ trace.variables.forEach(({ name, role }) => {
53
+ if (tick[name] === undefined)
54
+ return;
55
+ const element = role ? XML_ELEMENT[role] : 'value';
56
+ lines.push(` <${element} name="${xmlEscape(name)}">${xmlEscape(formatTraceValue(tick[name]))}</${element}>`);
57
+ });
58
+ trace.printed?.[index]?.forEach((text) => lines.push(` <printed>${xmlEscape(text)}</printed>`));
59
+ lines.push(' </tick>');
60
+ });
61
+ lines.push('</trace>');
62
+ return `${lines.join('\n')}\n`;
63
+ }
64
+ /** Enough XML for the files this lab writes and hand edits of them: elements, attributes, text, entities. */
65
+ function parseXml(text) {
66
+ const root = { name: '#document', attributes: {}, children: [], text: '', line: 1 };
67
+ const stack = [root];
68
+ let i = 0;
69
+ const lineAt = (index) => (text.slice(0, index).match(/\n/g) ?? []).length + 1;
70
+ while (i < text.length) {
71
+ const open = text.indexOf('<', i);
72
+ const top = stack[stack.length - 1];
73
+ if (open < 0) {
74
+ top.text += xmlUnescape(text.slice(i));
75
+ break;
76
+ }
77
+ top.text += xmlUnescape(text.slice(i, open));
78
+ if (text.startsWith('<!--', open)) {
79
+ const end = text.indexOf('-->', open);
80
+ if (end < 0)
81
+ throw new TraceFileError('A comment is not closed with -->.', lineAt(open));
82
+ i = end + 3;
83
+ }
84
+ else if (text.startsWith('<![CDATA[', open)) {
85
+ const end = text.indexOf(']]>', open);
86
+ if (end < 0)
87
+ throw new TraceFileError('A CDATA section is not closed.', lineAt(open));
88
+ top.text += text.slice(open + 9, end);
89
+ i = end + 3;
90
+ }
91
+ else if (text.startsWith('<?', open) || text.startsWith('<!', open)) {
92
+ const end = text.indexOf('>', open);
93
+ if (end < 0)
94
+ throw new TraceFileError('A declaration is not closed.', lineAt(open));
95
+ i = end + 1;
96
+ }
97
+ else {
98
+ const end = text.indexOf('>', open);
99
+ if (end < 0)
100
+ throw new TraceFileError('A tag is not closed with >.', lineAt(open));
101
+ const tag = text.slice(open + 1, end);
102
+ if (tag.startsWith('/')) {
103
+ const name = tag.slice(1).trim();
104
+ if (top.name !== name) {
105
+ throw new TraceFileError(`</${name}> closes <${top.name}>.`, lineAt(open));
106
+ }
107
+ stack.pop();
108
+ }
109
+ else {
110
+ const selfClosing = tag.endsWith('/');
111
+ const body = selfClosing ? tag.slice(0, -1) : tag;
112
+ const name = /^[^\s/]+/.exec(body)?.[0];
113
+ if (!name)
114
+ throw new TraceFileError('A tag has no name.', lineAt(open));
115
+ const attributes = {};
116
+ const pattern = /([^\s=]+)\s*=\s*("([^"]*)"|'([^']*)')/g;
117
+ let match = pattern.exec(body.slice(name.length));
118
+ while (match) {
119
+ attributes[match[1]] = xmlUnescape(match[3] ?? match[4] ?? '');
120
+ match = pattern.exec(body.slice(name.length));
121
+ }
122
+ const element = { name, attributes, children: [], text: '', line: lineAt(open) };
123
+ top.children.push(element);
124
+ if (!selfClosing)
125
+ stack.push(element);
126
+ }
127
+ i = end + 1;
128
+ }
129
+ }
130
+ if (stack.length > 1)
131
+ throw new TraceFileError(`<${stack[stack.length - 1].name}> is not closed.`);
132
+ return root;
133
+ }
134
+ export function readXml(text) {
135
+ const trace = parseXml(text).children.find((element) => element.name === 'trace');
136
+ if (!trace)
137
+ throw new TraceFileError('Expected a <trace> element.');
138
+ const list = new VariableList();
139
+ trace.children
140
+ .filter((element) => element.name === 'variable')
141
+ .forEach((element) => {
142
+ if (!element.attributes.name)
143
+ throw new TraceFileError('A <variable> has no name.', element.line);
144
+ const { role } = element.attributes;
145
+ list.add(element.attributes.name, ['input', 'output', 'local'].includes(role) ? role : undefined, element.attributes.type);
146
+ });
147
+ const roles = {
148
+ input: 'input',
149
+ output: 'output',
150
+ local: 'local',
151
+ value: undefined,
152
+ };
153
+ const elements = trace.children.filter((element) => element.name === 'tick');
154
+ const printed = elements.map((element) => element.children.filter((child) => child.name === 'printed').map((child) => child.text));
155
+ const ticks = elements.map((element) => {
156
+ const tick = {};
157
+ element.children.forEach((child) => {
158
+ if (!(child.name in roles))
159
+ return;
160
+ const { name } = child.attributes;
161
+ if (!name)
162
+ throw new TraceFileError(`An <${child.name}> has no name.`, child.line);
163
+ const declared = list.get(name);
164
+ const value = decodeText(child.text, declared?.type);
165
+ list.add(name, roles[child.name], typeOfValue(value));
166
+ tick[name] = value;
167
+ });
168
+ return tick;
169
+ });
170
+ const times = elements.map(({ attributes }) => (attributes.time ? Number(attributes.time) : undefined));
171
+ return {
172
+ model: trace.attributes.model,
173
+ variables: list.variables,
174
+ ticks: carryForward(ticks, list.variables),
175
+ times: givenTimes(times, list.variables, (index) => `The <tick> on line ${elements[index].line}`),
176
+ printed: printedOf(printed),
177
+ };
178
+ }
179
+ //# sourceMappingURL=xml.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"xml.js","sourceRoot":"","sources":["../src/xml.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AAEH,OAAO,EACH,YAAY,EACZ,UAAU,EACV,gBAAgB,EAChB,UAAU,EACV,SAAS,EACT,eAAe,EACf,cAAc,EACd,UAAU,EACV,WAAW,EACX,YAAY,GACf,0BAAsB;AAGvB,wBAAwB;AAExB,SAAS,SAAS,CAAC,IAAY;IAC3B,OAAO,IAAI;SACN,OAAO,CAAC,IAAI,EAAE,OAAO,CAAC;SACtB,OAAO,CAAC,IAAI,EAAE,MAAM,CAAC;SACrB,OAAO,CAAC,IAAI,EAAE,MAAM,CAAC;SACrB,OAAO,CAAC,IAAI,EAAE,QAAQ,CAAC;SACvB,OAAO,CAAC,WAAW,EAAE,CAAC,EAAE,EAAE,EAAE,CAAC,KAAK,EAAE,CAAC,UAAU,CAAC,CAAC,CAAC,GAAG,CAAC,CAAA;AAC/D,CAAC;AAED,SAAS,WAAW,CAAC,IAAY;IAC7B,OAAO,IAAI,CAAC,OAAO,CAAC,8CAA8C,EAAE,CAAC,CAAC,EAAE,MAAc,EAAE,EAAE;QACtF,IAAI,MAAM,CAAC,UAAU,CAAC,IAAI,CAAC;YAAE,OAAO,MAAM,CAAC,aAAa,CAAC,MAAM,CAAC,QAAQ,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAA;QAC9F,IAAI,MAAM,CAAC,UAAU,CAAC,GAAG,CAAC;YAAE,OAAO,MAAM,CAAC,aAAa,CAAC,MAAM,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,CAAA;QAChF,OAAO,EAAE,GAAG,EAAE,GAAG,EAAE,EAAE,EAAE,GAAG,EAAE,EA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CAAC,CAAC,CAAA;IACvG,OAAO;QACH,KAAK,EAAE,KAAK,CAAC,UAAU,CAAC,KAAK;QAC7B,SAAS,EAAE,IAAI,CAAC,SAAS;QACzB,KAAK,EAAE,YAAY,CAAC,KAAK,EAAE,IAAI,CAAC,SAAS,CAAC;QAC1C,KAAK,EAAE,UAAU,CAAC,KAAK,EAAE,IAAI,CAAC,SAAS,EAAE,CAAC,KAAK,EAAE,EAAE,CAAC,sBAAsB,QAAQ,CAAC,KAAK,CAAC,CAAC,IAAI,EAAE,CAAC;QACjG,OAAO,EAAE,SAAS,CAAC,OAAO,CAAC;KAC9B,CAAA;AACL,CAAC","sourcesContent":["/*\n * KIELER - Kiel Integrated Environment for Layout Eclipse RichClient\n *\n * http://rtsys.informatik.uni-kiel.de/kieler\n *\n * Copyright 2026 by\n * + Kiel University\n * + Department of Computer Science\n * + Real-Time and Embedded Systems Group\n *\n * This program and the accompanying materials are made available under the\n * terms of the Eclipse Public License 2.0 which is available at\n * http://www.eclipse.org/legal/epl-2.0.\n *\n * SPDX-License-Identifier: EPL-2.0\n */\n\nimport {\n carryForward,\n decodeText,\n formatTraceValue,\n givenTimes,\n printedOf,\n TRACE_FORMAT_ID,\n TraceFileError,\n traceTimes,\n typeOfValue,\n VariableList,\n} from './trace-record'\nimport type { TraceRecord, TraceRole } from './trace-record'\n\n// This lab's XML trace.\n\nfunction xmlEscape(text: string): string {\n return text\n .replace(/&/g, '&amp;')\n .replace(/</g, '&lt;')\n .replace(/>/g, '&gt;')\n .replace(/\"/g, '&quot;')\n .replace(/[\\r\\n\\t]/g, (ch) => `&#${ch.charCodeAt(0)};`)\n}\n\nfunction xmlUnescape(text: string): string {\n return text.replace(/&(#x[0-9a-fA-F]+|#\\d+|amp|lt|gt|quot|apos);/g, (_, entity: string) => {\n if (entity.startsWith('#x')) return String.fromCodePoint(Number.parseInt(entity.slice(2), 16))\n if (entity.startsWith('#')) return String.fromCodePoint(Number(entity.slice(1)))\n return { amp: '&', lt: '<', gt: '>', quot: '\"', apos: \"'\" }[entity] ?? ''\n })\n}\n\nconst XML_ELEMENT: Record<TraceRole, string> = { input: 'input', output: 'output', local: 'local' }\n\n/**\n * `<trace>` with the `<variable>` declarations, then one `<tick n=\"1\" time=\"…\">` per tick holding\n * `<input>`, `<output>`, `<local>` (or `<value>` for an unknown role) elements, and a `<printed>`\n * per line the model printed.\n */\nexport function writeXml(trace: TraceRecord): string {\n const times = traceTimes(trace)\n const attribute = (name: string, value: string | undefined) => (value ? ` ${name}=\"${xmlEscape(value)}\"` : '')\n const lines = [\n '<?xml version=\"1.0\" encoding=\"UTF-8\"?>',\n `<trace format=\"${TRACE_FORMAT_ID}\" version=\"1\"${attribute('model', trace.model)}>`,\n ...trace.variables.map(\n ({ name, role, type }) =>\n ` <variable${attribute('name', name)}${attribute('role', role)}${attribute('type', type)}/>`\n ),\n ]\n trace.ticks.forEach((tick, index) => {\n lines.push(` <tick n=\"${index + 1}\"${times ? ` time=\"${times[index]}\"` : ''}>`)\n trace.variables.forEach(({ name, role }) => {\n if (tick[name] === undefined) return\n const element = role ? XML_ELEMENT[role] : 'value'\n lines.push(\n ` <${element} name=\"${xmlEscape(name)}\">${xmlEscape(formatTraceValue(tick[name]))}</${element}>`\n )\n })\n trace.printed?.[index]?.forEach((text) => lines.push(` <printed>${xmlEscape(text)}</printed>`))\n lines.push(' </tick>')\n })\n lines.push('</trace>')\n return `${lines.join('\\n')}\\n`\n}\n\ninterface XmlElement {\n name: string\n attributes: Record<string, string>\n children: XmlElement[]\n text: string\n line: number\n}\n\n/** Enough XML for the files this lab writes and hand edits of them: elements, attributes, text, entities. */\nfunction parseXml(text: string): XmlElement {\n const root: XmlElement = { name: '#document', attributes: {}, children: [], text: '', line: 1 }\n const stack = [root]\n let i = 0\n const lineAt = (index: number) => (text.slice(0, index).match(/\\n/g) ?? []).length + 1\n while (i < text.length) {\n const open = text.indexOf('<', i)\n const top = stack[stack.length - 1]\n if (open < 0) {\n top.text += xmlUnescape(text.slice(i))\n break\n }\n top.text += xmlUnescape(text.slice(i, open))\n if (text.startsWith('<!--', open)) {\n const end = text.indexOf('-->', open)\n if (end < 0) throw new TraceFileError('A comment is not closed with -->.', lineAt(open))\n i = end + 3\n } else if (text.startsWith('<![CDATA[', open)) {\n const end = text.indexOf(']]>', open)\n if (end < 0) throw new TraceFileError('A CDATA section is not closed.', lineAt(open))\n top.text += text.slice(open + 9, end)\n i = end + 3\n } else if (text.startsWith('<?', open) || text.startsWith('<!', open)) {\n const end = text.indexOf('>', open)\n if (end < 0) throw new TraceFileError('A declaration is not closed.', lineAt(open))\n i = end + 1\n } else {\n const end = text.indexOf('>', open)\n if (end < 0) throw new TraceFileError('A tag is not closed with >.', lineAt(open))\n const tag = text.slice(open + 1, end)\n if (tag.startsWith('/')) {\n const name = tag.slice(1).trim()\n if (top.name !== name) {\n throw new TraceFileError(`</${name}> closes <${top.name}>.`, lineAt(open))\n }\n stack.pop()\n } else {\n const selfClosing = tag.endsWith('/')\n const body = selfClosing ? tag.slice(0, -1) : tag\n const name = /^[^\\s/]+/.exec(body)?.[0]\n if (!name) throw new TraceFileError('A tag has no name.', lineAt(open))\n const attributes: Record<string, string> = {}\n const pattern = /([^\\s=]+)\\s*=\\s*(\"([^\"]*)\"|'([^']*)')/g\n let match = pattern.exec(body.slice(name.length))\n while (match) {\n attributes[match[1]] = xmlUnescape(match[3] ?? match[4] ?? '')\n match = pattern.exec(body.slice(name.length))\n }\n const element: XmlElement = { name, attributes, children: [], text: '', line: lineAt(open) }\n top.children.push(element)\n if (!selfClosing) stack.push(element)\n }\n i = end + 1\n }\n }\n if (stack.length > 1) throw new TraceFileError(`<${stack[stack.length - 1].name}> is not closed.`)\n return root\n}\n\nexport function readXml(text: string): TraceRecord {\n const trace = parseXml(text).children.find((element) => element.name === 'trace')\n if (!trace) throw new TraceFileError('Expected a <trace> element.')\n const list = new VariableList()\n trace.children\n .filter((element) => element.name === 'variable')\n .forEach((element) => {\n if (!element.attributes.name) throw new TraceFileError('A <variable> has no name.', element.line)\n const { role } = element.attributes\n list.add(\n element.attributes.name,\n ['input', 'output', 'local'].includes(role) ? (role as TraceRole) : undefined,\n element.attributes.type\n )\n })\n const roles: Record<string, TraceRole | undefined> = {\n input: 'input',\n output: 'output',\n local: 'local',\n value: undefined,\n }\n const elements = trace.children.filter((element) => element.name === 'tick')\n const printed = elements.map((element) =>\n element.children.filter((child) => child.name === 'printed').map((child) => child.text)\n )\n const ticks = elements.map((element) => {\n const tick: Record<string, unknown> = {}\n element.children.forEach((child) => {\n if (!(child.name in roles)) return\n const { name } = child.attributes\n if (!name) throw new TraceFileError(`An <${child.name}> has no name.`, child.line)\n const declared = list.get(name)\n const value = decodeText(child.text, declared?.type)\n list.add(name, roles[child.name], typeOfValue(value))\n tick[name] = value\n })\n return tick\n })\n const times = elements.map(({ attributes }) => (attributes.time ? Number(attributes.time) : undefined))\n return {\n model: trace.attributes.model,\n variables: list.variables,\n ticks: carryForward(ticks, list.variables),\n times: givenTimes(times, list.variables, (index) => `The <tick> on line ${elements[index].line}`),\n printed: printedOf(printed),\n }\n}\n"]}