@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
package/dist/ktrace.js
ADDED
|
@@ -0,0 +1,266 @@
|
|
|
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, sameTraceValue, TraceFileError, VariableList } from "./trace-record.js";
|
|
18
|
+
// KTrace, KIELER's trace format.
|
|
19
|
+
const KTRACE_KEYWORDS = ['reset', 'pause', 'goto'];
|
|
20
|
+
function ktraceName(name) {
|
|
21
|
+
return KTRACE_KEYWORDS.includes(name) ? `^${name}` : name;
|
|
22
|
+
}
|
|
23
|
+
function ktraceValue(value, type, name) {
|
|
24
|
+
if (typeof value === 'boolean')
|
|
25
|
+
return String(value);
|
|
26
|
+
if (typeof value === 'number') {
|
|
27
|
+
if (!Number.isFinite(value))
|
|
28
|
+
throw new TraceFileError(`${name} is ${value}, which KTrace cannot hold.`);
|
|
29
|
+
const float = type === 'float' || type === 'double' || !Number.isInteger(value);
|
|
30
|
+
// A float written without a point reads back as an int, and KIELER's trace check then fails it.
|
|
31
|
+
return float && Number.isInteger(value) ? value.toFixed(1) : String(value);
|
|
32
|
+
}
|
|
33
|
+
if (typeof value === 'string')
|
|
34
|
+
return JSON.stringify(value);
|
|
35
|
+
if (Array.isArray(value))
|
|
36
|
+
return `{${value.map((entry) => ktraceValue(entry, type, name)).join(', ')}}`;
|
|
37
|
+
throw new TraceFileError(`${name} holds a struct, which KTrace cannot hold. Save as JSON or XML instead.`);
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* KTrace as KIELER writes it: one tick per line, inputs then `=>` and outputs, each value only
|
|
41
|
+
* when it changed. Variables that are neither inputs nor outputs have no place in KTrace.
|
|
42
|
+
*/
|
|
43
|
+
export function writeKTrace(trace) {
|
|
44
|
+
const inputs = trace.variables.filter((variable) => variable.role === 'input');
|
|
45
|
+
const outputs = trace.variables.filter((variable) => variable.role === 'output');
|
|
46
|
+
const lines = [`// ${trace.model ?? 'Simulation'}: ${trace.ticks.length} ticks, saved by SCCharts Lab`];
|
|
47
|
+
const last = new Map();
|
|
48
|
+
const changes = (variables, tick) => variables.flatMap(({ name, type }) => {
|
|
49
|
+
const value = tick[name];
|
|
50
|
+
if (value === undefined || value === null)
|
|
51
|
+
return [];
|
|
52
|
+
if (last.has(name) && sameTraceValue(last.get(name), value))
|
|
53
|
+
return [];
|
|
54
|
+
last.set(name, value);
|
|
55
|
+
return [`${ktraceName(name)} = ${ktraceValue(value, type, name)}`];
|
|
56
|
+
});
|
|
57
|
+
trace.ticks.forEach((tick, index) => {
|
|
58
|
+
const ins = changes(inputs, tick);
|
|
59
|
+
const outs = changes(outputs, tick);
|
|
60
|
+
let line = ins.join(' ');
|
|
61
|
+
if (outs.length > 0)
|
|
62
|
+
line += `${line ? ' ' : ''}=> ${outs.join(' ')}`;
|
|
63
|
+
if (trace.pauses?.includes(index + 1))
|
|
64
|
+
line += `${line ? ' ' : ''}pause`;
|
|
65
|
+
// KTrace has no place for printed text; a comment keeps it readable and KIELER skips it.
|
|
66
|
+
const printed = trace.printed?.[index] ?? [];
|
|
67
|
+
const comment = printed.length > 0 ? ` // printed ${printed.map((text) => JSON.stringify(text)).join(' ')}` : '';
|
|
68
|
+
lines.push(`${line};${comment}`);
|
|
69
|
+
});
|
|
70
|
+
return `${lines.join('\n')}\n`;
|
|
71
|
+
}
|
|
72
|
+
function tokenizeKTrace(text) {
|
|
73
|
+
const tokens = [];
|
|
74
|
+
let line = 1;
|
|
75
|
+
let i = 0;
|
|
76
|
+
while (i < text.length) {
|
|
77
|
+
const ch = text[i];
|
|
78
|
+
if (ch === '\n') {
|
|
79
|
+
line++;
|
|
80
|
+
i++;
|
|
81
|
+
}
|
|
82
|
+
else if (/\s/.test(ch)) {
|
|
83
|
+
i++;
|
|
84
|
+
}
|
|
85
|
+
else if (text.startsWith('//', i)) {
|
|
86
|
+
while (i < text.length && text[i] !== '\n')
|
|
87
|
+
i++;
|
|
88
|
+
}
|
|
89
|
+
else if (text.startsWith('/*', i)) {
|
|
90
|
+
const end = text.indexOf('*/', i + 2);
|
|
91
|
+
if (end < 0)
|
|
92
|
+
throw new TraceFileError('A comment is not closed with */.', line);
|
|
93
|
+
line += (text.slice(i, end).match(/\n/g) ?? []).length;
|
|
94
|
+
i = end + 2;
|
|
95
|
+
}
|
|
96
|
+
else if (ch === '#') {
|
|
97
|
+
// Pragmas such as #include apply to KIELER's test runner, not to replaying.
|
|
98
|
+
while (i < text.length && text[i] !== '\n')
|
|
99
|
+
i++;
|
|
100
|
+
}
|
|
101
|
+
else if (ch === '"') {
|
|
102
|
+
let j = i + 1;
|
|
103
|
+
while (j < text.length && text[j] !== '"') {
|
|
104
|
+
if (text[j] === '\n')
|
|
105
|
+
throw new TraceFileError('A string is not closed on its line.', line);
|
|
106
|
+
j += text[j] === '\\' ? 2 : 1;
|
|
107
|
+
}
|
|
108
|
+
if (j >= text.length)
|
|
109
|
+
throw new TraceFileError('A string is not closed.', line);
|
|
110
|
+
const literal = text.slice(i, j + 1);
|
|
111
|
+
let value;
|
|
112
|
+
try {
|
|
113
|
+
value = JSON.parse(literal);
|
|
114
|
+
}
|
|
115
|
+
catch {
|
|
116
|
+
// KIELER also accepts escapes JSON does not, such as \'.
|
|
117
|
+
value = literal.slice(1, -1).replace(/\\(.)/g, '$1');
|
|
118
|
+
}
|
|
119
|
+
tokens.push({ kind: 'string', text: literal, value, line });
|
|
120
|
+
i = j + 1;
|
|
121
|
+
}
|
|
122
|
+
else if (text.startsWith('=>', i)) {
|
|
123
|
+
tokens.push({ kind: 'symbol', text: '=>', line });
|
|
124
|
+
i += 2;
|
|
125
|
+
}
|
|
126
|
+
else if (/[0-9]/.test(ch) || (ch === '.' && /[0-9]/.test(text[i + 1] ?? ''))) {
|
|
127
|
+
const match = /^(0[xX][0-9a-fA-F]+|(\d+\.?\d*|\.\d+)([eE][+-]?\d+)?[fF]?)/.exec(text.slice(i));
|
|
128
|
+
const literal = match?.[0] ?? ch;
|
|
129
|
+
const hex = /^0[xX]/.test(literal);
|
|
130
|
+
const float = !hex && /[.eEfF]/.test(literal);
|
|
131
|
+
const value = hex ? Number.parseInt(literal, 16) : Number(literal.replace(/[fF]$/, ''));
|
|
132
|
+
tokens.push({ kind: 'number', text: float ? 'float' : 'int', value, line });
|
|
133
|
+
i += literal.length;
|
|
134
|
+
}
|
|
135
|
+
else if (/[\^A-Za-z_]/.test(ch)) {
|
|
136
|
+
const match = /^\^?[A-Za-z_][A-Za-z0-9_]*/.exec(text.slice(i));
|
|
137
|
+
if (!match)
|
|
138
|
+
throw new TraceFileError(`Unexpected "${ch}".`, line);
|
|
139
|
+
tokens.push({ kind: 'name', text: match[0], line });
|
|
140
|
+
i += match[0].length;
|
|
141
|
+
}
|
|
142
|
+
else if ('=;{},:-[]'.includes(ch)) {
|
|
143
|
+
tokens.push({ kind: 'symbol', text: ch, line });
|
|
144
|
+
i++;
|
|
145
|
+
}
|
|
146
|
+
else {
|
|
147
|
+
throw new TraceFileError(`Unexpected "${ch}".`, line);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
return tokens;
|
|
151
|
+
}
|
|
152
|
+
/**
|
|
153
|
+
* Reads the literal part of KTrace: ticks of `name = value` assignments, `=>` before the outputs,
|
|
154
|
+
* `pause`, and `reset;` between traces (only the first is kept). Values must be literals.
|
|
155
|
+
*/
|
|
156
|
+
export function readKTrace(text) {
|
|
157
|
+
const tokens = tokenizeKTrace(text);
|
|
158
|
+
const list = new VariableList();
|
|
159
|
+
const ticks = [];
|
|
160
|
+
const pauses = [];
|
|
161
|
+
const notes = [];
|
|
162
|
+
let pos = 0;
|
|
163
|
+
const peek = (offset = 0) => tokens[pos + offset];
|
|
164
|
+
const isSymbol = (token, symbol) => token?.kind === 'symbol' && token.text === symbol;
|
|
165
|
+
const isWord = (token, word) => token?.kind === 'name' && token.text === word;
|
|
166
|
+
const lastLine = () => tokens[tokens.length - 1]?.line;
|
|
167
|
+
const expect = (symbol) => {
|
|
168
|
+
const token = tokens[pos];
|
|
169
|
+
if (!isSymbol(token, symbol)) {
|
|
170
|
+
throw new TraceFileError(`Expected "${symbol}"${token ? ` before "${token.text}"` : ' at the end'}.`, token?.line ?? lastLine());
|
|
171
|
+
}
|
|
172
|
+
pos++;
|
|
173
|
+
};
|
|
174
|
+
const value = () => {
|
|
175
|
+
const token = tokens[pos];
|
|
176
|
+
if (!token)
|
|
177
|
+
throw new TraceFileError('A value is missing at the end of the file.', lastLine());
|
|
178
|
+
if (isSymbol(token, '-')) {
|
|
179
|
+
pos++;
|
|
180
|
+
const number = tokens[pos];
|
|
181
|
+
if (number?.kind !== 'number')
|
|
182
|
+
throw new TraceFileError('Expected a number after "-".', token.line);
|
|
183
|
+
pos++;
|
|
184
|
+
return { value: -number.value, type: number.text };
|
|
185
|
+
}
|
|
186
|
+
if (isSymbol(token, '{')) {
|
|
187
|
+
pos++;
|
|
188
|
+
const entries = [];
|
|
189
|
+
let type;
|
|
190
|
+
while (!isSymbol(peek(), '}')) {
|
|
191
|
+
const entry = value();
|
|
192
|
+
entries.push(entry.value);
|
|
193
|
+
type = type ?? entry.type;
|
|
194
|
+
if (!isSymbol(peek(), '}'))
|
|
195
|
+
expect(',');
|
|
196
|
+
}
|
|
197
|
+
pos++;
|
|
198
|
+
return { value: entries, type };
|
|
199
|
+
}
|
|
200
|
+
pos++;
|
|
201
|
+
if (token.kind === 'number')
|
|
202
|
+
return { value: token.value, type: token.text };
|
|
203
|
+
if (token.kind === 'string')
|
|
204
|
+
return { value: token.value, type: 'string' };
|
|
205
|
+
if (isWord(token, 'true') || isWord(token, 'false'))
|
|
206
|
+
return { value: token.text === 'true', type: 'bool' };
|
|
207
|
+
throw new TraceFileError(`Only literal values can be replayed, such as true, 3, 1.5, "text" or {1, 2}; found "${token.text}".`, token.line);
|
|
208
|
+
};
|
|
209
|
+
while (pos < tokens.length) {
|
|
210
|
+
// `reset;` separates traces; one may also open or close the file.
|
|
211
|
+
if (isWord(peek(), 'reset') && isSymbol(peek(1), ';')) {
|
|
212
|
+
pos += 2;
|
|
213
|
+
if (ticks.length > 0) {
|
|
214
|
+
const more = tokens.slice(pos).filter((token) => isSymbol(token, ';')).length;
|
|
215
|
+
if (more > 0)
|
|
216
|
+
notes.push('The file holds more than one trace; only the first is used.');
|
|
217
|
+
break;
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
else {
|
|
221
|
+
if (peek()?.kind === 'name' && isSymbol(peek(1), ':'))
|
|
222
|
+
pos += 2; // a named tick
|
|
223
|
+
const tick = {};
|
|
224
|
+
let role = 'input';
|
|
225
|
+
while (pos < tokens.length && !isSymbol(peek(), ';')) {
|
|
226
|
+
const token = peek();
|
|
227
|
+
if (isSymbol(token, '=>')) {
|
|
228
|
+
if (role === 'output')
|
|
229
|
+
throw new TraceFileError('A tick has "=>" twice.', token.line);
|
|
230
|
+
role = 'output';
|
|
231
|
+
pos++;
|
|
232
|
+
}
|
|
233
|
+
else if (isWord(token, 'pause')) {
|
|
234
|
+
pauses.push(ticks.length + 1);
|
|
235
|
+
pos++;
|
|
236
|
+
}
|
|
237
|
+
else if (isWord(token, 'goto')) {
|
|
238
|
+
throw new TraceFileError('Traces that loop with goto cannot be replayed. Write the ticks out instead.', token.line);
|
|
239
|
+
}
|
|
240
|
+
else if (token.kind === 'name') {
|
|
241
|
+
pos++;
|
|
242
|
+
if (isSymbol(peek(), '[')) {
|
|
243
|
+
throw new TraceFileError(`Assign the whole array, as in ${token.text} = {1, 2}; single elements cannot be replayed.`, token.line);
|
|
244
|
+
}
|
|
245
|
+
expect('=');
|
|
246
|
+
const name = token.text.replace(/^\^/, '');
|
|
247
|
+
const parsed = value();
|
|
248
|
+
list.add(name, role, parsed.type);
|
|
249
|
+
tick[name] = parsed.value;
|
|
250
|
+
}
|
|
251
|
+
else {
|
|
252
|
+
throw new TraceFileError(`Expected a variable name, found "${token.text}".`, token.line);
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
expect(';');
|
|
256
|
+
ticks.push(tick);
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
return {
|
|
260
|
+
variables: list.variables,
|
|
261
|
+
ticks: carryForward(ticks, list.variables),
|
|
262
|
+
pauses: pauses.length > 0 ? pauses : undefined,
|
|
263
|
+
notes: notes.length > 0 ? notes : undefined,
|
|
264
|
+
};
|
|
265
|
+
}
|
|
266
|
+
//# sourceMappingURL=ktrace.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ktrace.js","sourceRoot":"","sources":["../src/ktrace.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AAEH,OAAO,EAAE,YAAY,EAAE,cAAc,EAAE,cAAc,EAAE,YAAY,EAAE,0BAAsB;AAG3F,iCAAiC;AAEjC,MAAM,eAAe,GAAG,CAAC,OAAO,EAAE,OAAO,EAAE,MAAM,CAAC,CAAA;AAElD,SAAS,UAAU,CAAC,IAAY;IAC5B,OAAO,eAAe,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,IAAI,I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CAAC,IAAI,CACb,CAAA;oBACL,CAAC;oBACD,MAAM,CAAC,GAAG,CAAC,CAAA;oBACX,MAAM,IAAI,GAAG,KAAK,CAAC,IAAI,CAAC,OAAO,CAAC,KAAK,EAAE,EAAE,CAAC,CAAA;oBAC1C,MAAM,MAAM,GAAG,KAAK,EAAE,CAAA;oBACtB,IAAI,CAAC,GAAG,CAAC,IAAI,EAAE,IAAI,EAAE,MAAM,CAAC,IAAI,CAAC,CAAA;oBACjC,IAAI,CAAC,IAAI,CAAC,GAAG,MAAM,CAAC,KAAK,CAAA;gBAC7B,CAAC;qBAAM,CAAC;oBACJ,MAAM,IAAI,cAAc,CAAC,oCAAoC,KAAK,CAAC,IAAI,IAAI,EAAE,KAAK,CAAC,IAAI,CAAC,CAAA;gBAC5F,CAAC;YACL,CAAC;YACD,MAAM,CAAC,GAAG,CAAC,CAAA;YACX,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,CAAA;QACpB,CAAC;IACL,CAAC;IACD,OAAO;QACH,SAAS,EAAE,IAAI,CAAC,SAAS;QACzB,KAAK,EAAE,YAAY,CAAC,KAAK,EAAE,IAAI,CAAC,SAAS,CAAC;QAC1C,MAAM,EAAE,MAAM,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,SAAS;QAC9C,KAAK,EAAE,KAAK,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,SAAS;KAC9C,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 { carryForward, sameTraceValue, TraceFileError, VariableList } from './trace-record'\nimport type { TraceRecord, TraceRole, TraceVariable } from './trace-record'\n\n// KTrace, KIELER's trace format.\n\nconst KTRACE_KEYWORDS = ['reset', 'pause', 'goto']\n\nfunction ktraceName(name: string): string {\n return KTRACE_KEYWORDS.includes(name) ? `^${name}` : name\n}\n\nfunction ktraceValue(value: unknown, type: string | undefined, name: string): string {\n if (typeof value === 'boolean') return String(value)\n if (typeof value === 'number') {\n if (!Number.isFinite(value)) throw new TraceFileError(`${name} is ${value}, which KTrace cannot hold.`)\n const float = type === 'float' || type === 'double' || !Number.isInteger(value)\n // A float written without a point reads back as an int, and KIELER's trace check then fails it.\n return float && Number.isInteger(value) ? value.toFixed(1) : String(value)\n }\n if (typeof value === 'string') return JSON.stringify(value)\n if (Array.isArray(value)) return `{${value.map((entry) => ktraceValue(entry, type, name)).join(', ')}}`\n throw new TraceFileError(`${name} holds a struct, which KTrace cannot hold. Save as JSON or XML instead.`)\n}\n\n/**\n * KTrace as KIELER writes it: one tick per line, inputs then `=>` and outputs, each value only\n * when it changed. Variables that are neither inputs nor outputs have no place in KTrace.\n */\nexport function writeKTrace(trace: TraceRecord): string {\n const inputs = trace.variables.filter((variable) => variable.role === 'input')\n const outputs = trace.variables.filter((variable) => variable.role === 'output')\n const lines = [`// ${trace.model ?? 'Simulation'}: ${trace.ticks.length} ticks, saved by SCCharts Lab`]\n const last = new Map<string, unknown>()\n const changes = (variables: TraceVariable[], tick: Record<string, unknown>) =>\n variables.flatMap(({ name, type }) => {\n const value = tick[name]\n if (value === undefined || value === null) return []\n if (last.has(name) && sameTraceValue(last.get(name), value)) return []\n last.set(name, value)\n return [`${ktraceName(name)} = ${ktraceValue(value, type, name)}`]\n })\n trace.ticks.forEach((tick, index) => {\n const ins = changes(inputs, tick)\n const outs = changes(outputs, tick)\n let line = ins.join(' ')\n if (outs.length > 0) line += `${line ? ' ' : ''}=> ${outs.join(' ')}`\n if (trace.pauses?.includes(index + 1)) line += `${line ? ' ' : ''}pause`\n // KTrace has no place for printed text; a comment keeps it readable and KIELER skips it.\n const printed = trace.printed?.[index] ?? []\n const comment = printed.length > 0 ? ` // printed ${printed.map((text) => JSON.stringify(text)).join(' ')}` : ''\n lines.push(`${line};${comment}`)\n })\n return `${lines.join('\\n')}\\n`\n}\n\ntype Token = { kind: 'name' | 'number' | 'string' | 'symbol'; text: string; value?: unknown; line: number }\n\nfunction tokenizeKTrace(text: string): Token[] {\n const tokens: Token[] = []\n let line = 1\n let i = 0\n while (i < text.length) {\n const ch = text[i]\n if (ch === '\\n') {\n line++\n i++\n } else if (/\\s/.test(ch)) {\n i++\n } else if (text.startsWith('//', i)) {\n while (i < text.length && text[i] !== '\\n') i++\n } else if (text.startsWith('/*', i)) {\n const end = text.indexOf('*/', i + 2)\n if (end < 0) throw new TraceFileError('A comment is not closed with */.', line)\n line += (text.slice(i, end).match(/\\n/g) ?? []).length\n i = end + 2\n } else if (ch === '#') {\n // Pragmas such as #include apply to KIELER's test runner, not to replaying.\n while (i < text.length && text[i] !== '\\n') i++\n } else if (ch === '\"') {\n let j = i + 1\n while (j < text.length && text[j] !== '\"') {\n if (text[j] === '\\n') throw new TraceFileError('A string is not closed on its line.', line)\n j += text[j] === '\\\\' ? 2 : 1\n }\n if (j >= text.length) throw new TraceFileError('A string is not closed.', line)\n const literal = text.slice(i, j + 1)\n let value: string\n try {\n value = JSON.parse(literal)\n } catch {\n // KIELER also accepts escapes JSON does not, such as \\'.\n value = literal.slice(1, -1).replace(/\\\\(.)/g, '$1')\n }\n tokens.push({ kind: 'string', text: literal, value, line })\n i = j + 1\n } else if (text.startsWith('=>', i)) {\n tokens.push({ kind: 'symbol', text: '=>', line })\n i += 2\n } else if (/[0-9]/.test(ch) || (ch === '.' && /[0-9]/.test(text[i + 1] ?? ''))) {\n const match = /^(0[xX][0-9a-fA-F]+|(\\d+\\.?\\d*|\\.\\d+)([eE][+-]?\\d+)?[fF]?)/.exec(text.slice(i))\n const literal = match?.[0] ?? ch\n const hex = /^0[xX]/.test(literal)\n const float = !hex && /[.eEfF]/.test(literal)\n const value = hex ? Number.parseInt(literal, 16) : Number(literal.replace(/[fF]$/, ''))\n tokens.push({ kind: 'number', text: float ? 'float' : 'int', value, line })\n i += literal.length\n } else if (/[\\^A-Za-z_]/.test(ch)) {\n const match = /^\\^?[A-Za-z_][A-Za-z0-9_]*/.exec(text.slice(i))\n if (!match) throw new TraceFileError(`Unexpected \"${ch}\".`, line)\n tokens.push({ kind: 'name', text: match[0], line })\n i += match[0].length\n } else if ('=;{},:-[]'.includes(ch)) {\n tokens.push({ kind: 'symbol', text: ch, line })\n i++\n } else {\n throw new TraceFileError(`Unexpected \"${ch}\".`, line)\n }\n }\n return tokens\n}\n\n/**\n * Reads the literal part of KTrace: ticks of `name = value` assignments, `=>` before the outputs,\n * `pause`, and `reset;` between traces (only the first is kept). Values must be literals.\n */\nexport function readKTrace(text: string): TraceRecord {\n const tokens = tokenizeKTrace(text)\n const list = new VariableList()\n const ticks: Record<string, unknown>[] = []\n const pauses: number[] = []\n const notes: string[] = []\n let pos = 0\n const peek = (offset = 0) => tokens[pos + offset]\n const isSymbol = (token: Token | undefined, symbol: string) => token?.kind === 'symbol' && token.text === symbol\n const isWord = (token: Token | undefined, word: string) => token?.kind === 'name' && token.text === word\n const lastLine = () => tokens[tokens.length - 1]?.line\n const expect = (symbol: string) => {\n const token = tokens[pos]\n if (!isSymbol(token, symbol)) {\n throw new TraceFileError(\n `Expected \"${symbol}\"${token ? ` before \"${token.text}\"` : ' at the end'}.`,\n token?.line ?? lastLine()\n )\n }\n pos++\n }\n\n const value = (): { value: unknown; type?: string } => {\n const token = tokens[pos]\n if (!token) throw new TraceFileError('A value is missing at the end of the file.', lastLine())\n if (isSymbol(token, '-')) {\n pos++\n const number = tokens[pos]\n if (number?.kind !== 'number') throw new TraceFileError('Expected a number after \"-\".', token.line)\n pos++\n return { value: -(number.value as number), type: number.text }\n }\n if (isSymbol(token, '{')) {\n pos++\n const entries: unknown[] = []\n let type: string | undefined\n while (!isSymbol(peek(), '}')) {\n const entry = value()\n entries.push(entry.value)\n type = type ?? entry.type\n if (!isSymbol(peek(), '}')) expect(',')\n }\n pos++\n return { value: entries, type }\n }\n pos++\n if (token.kind === 'number') return { value: token.value, type: token.text }\n if (token.kind === 'string') return { value: token.value, type: 'string' }\n if (isWord(token, 'true') || isWord(token, 'false')) return { value: token.text === 'true', type: 'bool' }\n throw new TraceFileError(\n `Only literal values can be replayed, such as true, 3, 1.5, \"text\" or {1, 2}; found \"${token.text}\".`,\n token.line\n )\n }\n\n while (pos < tokens.length) {\n // `reset;` separates traces; one may also open or close the file.\n if (isWord(peek(), 'reset') && isSymbol(peek(1), ';')) {\n pos += 2\n if (ticks.length > 0) {\n const more = tokens.slice(pos).filter((token) => isSymbol(token, ';')).length\n if (more > 0) notes.push('The file holds more than one trace; only the first is used.')\n break\n }\n } else {\n if (peek()?.kind === 'name' && isSymbol(peek(1), ':')) pos += 2 // a named tick\n const tick: Record<string, unknown> = {}\n let role: TraceRole = 'input'\n while (pos < tokens.length && !isSymbol(peek(), ';')) {\n const token = peek()\n if (isSymbol(token, '=>')) {\n if (role === 'output') throw new TraceFileError('A tick has \"=>\" twice.', token.line)\n role = 'output'\n pos++\n } else if (isWord(token, 'pause')) {\n pauses.push(ticks.length + 1)\n pos++\n } else if (isWord(token, 'goto')) {\n throw new TraceFileError(\n 'Traces that loop with goto cannot be replayed. Write the ticks out instead.',\n token.line\n )\n } else if (token.kind === 'name') {\n pos++\n if (isSymbol(peek(), '[')) {\n throw new TraceFileError(\n `Assign the whole array, as in ${token.text} = {1, 2}; single elements cannot be replayed.`,\n token.line\n )\n }\n expect('=')\n const name = token.text.replace(/^\\^/, '')\n const parsed = value()\n list.add(name, role, parsed.type)\n tick[name] = parsed.value\n } else {\n throw new TraceFileError(`Expected a variable name, found \"${token.text}\".`, token.line)\n }\n }\n expect(';')\n ticks.push(tick)\n }\n }\n return {\n variables: list.variables,\n ticks: carryForward(ticks, list.variables),\n pauses: pauses.length > 0 ? pauses : undefined,\n notes: notes.length > 0 ? notes : undefined,\n }\n}\n"]}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/** A trace file driving the inputs of the running simulation. */
|
|
2
|
+
export interface ReplayState {
|
|
3
|
+
/** File name of the trace. */
|
|
4
|
+
label: string;
|
|
5
|
+
/** Ticks in the file. */
|
|
6
|
+
length: number;
|
|
7
|
+
/** Rows of the file driven so far, at most `length`. */
|
|
8
|
+
tick: number;
|
|
9
|
+
/** Outputs in both the file and the model, checked after each tick. */
|
|
10
|
+
checked: string[];
|
|
11
|
+
/** Inputs in the file that the model does not have. */
|
|
12
|
+
ignored: string[];
|
|
13
|
+
/** Inputs of the model the file does not set; they stay under the user's control. */
|
|
14
|
+
unset: string[];
|
|
15
|
+
/** Ticks so far at which a checked output differs from the file. */
|
|
16
|
+
mismatchTicks: number;
|
|
17
|
+
firstMismatch?: {
|
|
18
|
+
tick: number;
|
|
19
|
+
text: string;
|
|
20
|
+
};
|
|
21
|
+
/** File values that do not fit the model's inputs. */
|
|
22
|
+
problems: string[];
|
|
23
|
+
/** What the reader left out, e.g. further traces in the file. */
|
|
24
|
+
notes: string[];
|
|
25
|
+
}
|
package/dist/protocol.js
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
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
|
+
export {};
|
|
18
|
+
//# sourceMappingURL=protocol.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"protocol.js","sourceRoot":"","sources":["../src/protocol.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG","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\n// The part of keith-vscode's simulation/protocol.ts that replaying uses: what a view shows of a replay.\n\n/** A trace file driving the inputs of the running simulation. */\nexport interface ReplayState {\n /** File name of the trace. */\n label: string\n /** Ticks in the file. */\n length: number\n /** Rows of the file driven so far, at most `length`. */\n tick: number\n /** Outputs in both the file and the model, checked after each tick. */\n checked: string[]\n /** Inputs in the file that the model does not have. */\n ignored: string[]\n /** Inputs of the model the file does not set; they stay under the user's control. */\n unset: string[]\n /** Ticks so far at which a checked output differs from the file. */\n mismatchTicks: number\n firstMismatch?: { tick: number; text: string }\n /** File values that do not fit the model's inputs. */\n problems: string[]\n /** What the reader left out, e.g. further traces in the file. */\n notes: string[]\n}\n"]}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import type { TraceRecord, TraceRole } from "./trace-record.js";
|
|
2
|
+
export interface NamedTrace {
|
|
3
|
+
/** How the report names the run, e.g. the trace's file name. */
|
|
4
|
+
label: string;
|
|
5
|
+
trace: TraceRecord;
|
|
6
|
+
}
|
|
7
|
+
/** A variable taking a new value: at the first tick, and wherever it differs from the tick before. */
|
|
8
|
+
export interface ValueChange {
|
|
9
|
+
tick: number;
|
|
10
|
+
time?: number;
|
|
11
|
+
value: unknown;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* `tick`: tick n of one trace against tick n of the others. `time`: every trace has times (runs
|
|
15
|
+
* on a timed sequence, or ticked with deltaT), so the values are compared at every moment any
|
|
16
|
+
* trace ticks. That still works when the models tick at different moments and different rates.
|
|
17
|
+
*/
|
|
18
|
+
export type Alignment = 'tick' | 'time';
|
|
19
|
+
export interface VariableComparison {
|
|
20
|
+
name: string;
|
|
21
|
+
role?: TraceRole;
|
|
22
|
+
/** Ticks (or, aligned by time, moments) compared one to one. */
|
|
23
|
+
compared: number;
|
|
24
|
+
/** Ticks, or times in seconds, where a trace differs from the first. */
|
|
25
|
+
differing: number[];
|
|
26
|
+
/** Aligned by time: for how many seconds some trace differs from the first. */
|
|
27
|
+
differingFor?: number;
|
|
28
|
+
/** Per trace, the changes of value. */
|
|
29
|
+
changes: ValueChange[][];
|
|
30
|
+
/** Every trace goes through the same values in the same order, whenever it does so. */
|
|
31
|
+
sameSequence: boolean;
|
|
32
|
+
/** Largest distance, in ticks, between the n-th change of a trace and of the first. */
|
|
33
|
+
worstTickShift: number;
|
|
34
|
+
/** The same in seconds, when every trace has times. */
|
|
35
|
+
worstTimeShift?: number;
|
|
36
|
+
}
|
|
37
|
+
/** The lines the models printed, each with its tick, lined up the n-th with the n-th like value changes. */
|
|
38
|
+
export interface PrintedComparison {
|
|
39
|
+
/** Per trace, every printed line in order. */
|
|
40
|
+
lines: {
|
|
41
|
+
tick: number;
|
|
42
|
+
time?: number;
|
|
43
|
+
text: string;
|
|
44
|
+
}[][];
|
|
45
|
+
/** Every trace printed the same lines in the same order. */
|
|
46
|
+
sameSequence: boolean;
|
|
47
|
+
worstTickShift: number;
|
|
48
|
+
worstTimeShift?: number;
|
|
49
|
+
}
|
|
50
|
+
export interface TraceComparison {
|
|
51
|
+
alignment: Alignment;
|
|
52
|
+
traces: {
|
|
53
|
+
label: string;
|
|
54
|
+
model?: string;
|
|
55
|
+
ticks: number;
|
|
56
|
+
duration?: number;
|
|
57
|
+
}[];
|
|
58
|
+
/** Aligned by time: the moments compared run up to here, the end of the shortest trace. */
|
|
59
|
+
until?: number;
|
|
60
|
+
/** Variables in every trace, inputs first, then outputs, then the rest. */
|
|
61
|
+
variables: VariableComparison[];
|
|
62
|
+
/** Variables only some traces have, with the traces that have them. */
|
|
63
|
+
partial: {
|
|
64
|
+
name: string;
|
|
65
|
+
labels: string[];
|
|
66
|
+
}[];
|
|
67
|
+
/** When any trace printed something. */
|
|
68
|
+
printed?: PrintedComparison;
|
|
69
|
+
}
|
|
70
|
+
/** The value a variable holds after `tick`, or at `time`, from its changes. */
|
|
71
|
+
export declare function valueOf(changes: ValueChange[], alignment: Alignment, at: number): unknown;
|
|
72
|
+
/**
|
|
73
|
+
* Compares the traces against the first: tick by tick (or moment by moment, see
|
|
74
|
+
* {@link Alignment}), and change by change. Changes still line up when one model reacts a few
|
|
75
|
+
* ticks, or a fraction of a second, later than the other.
|
|
76
|
+
*/
|
|
77
|
+
export declare function compareTraces(traces: NamedTrace[]): TraceComparison;
|
|
@@ -0,0 +1,156 @@
|
|
|
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 { sameTraceValue, traceTimes } from "./trace-record.js";
|
|
18
|
+
const ROLE_ORDER = ['input', 'output', 'local', undefined];
|
|
19
|
+
/** Times closer than this are one moment. */
|
|
20
|
+
const EPSILON = 1e-9;
|
|
21
|
+
function round(value) {
|
|
22
|
+
return Math.round(value * 1e9) / 1e9;
|
|
23
|
+
}
|
|
24
|
+
function changesOf(trace, name, times) {
|
|
25
|
+
const changes = [];
|
|
26
|
+
trace.ticks.forEach((tick, index) => {
|
|
27
|
+
const value = tick[name];
|
|
28
|
+
const last = changes[changes.length - 1];
|
|
29
|
+
if (value === undefined || (last && sameTraceValue(last.value, value)))
|
|
30
|
+
return;
|
|
31
|
+
changes.push({ tick: index + 1, time: times?.[index], value });
|
|
32
|
+
});
|
|
33
|
+
return changes;
|
|
34
|
+
}
|
|
35
|
+
/** The value a variable holds after `tick`, or at `time`, from its changes. */
|
|
36
|
+
export function valueOf(changes, alignment, at) {
|
|
37
|
+
let value;
|
|
38
|
+
for (const change of changes) {
|
|
39
|
+
const moment = alignment === 'time' ? change.time ?? 0 : change.tick;
|
|
40
|
+
if (moment > at + (alignment === 'time' ? EPSILON : 0))
|
|
41
|
+
break;
|
|
42
|
+
value = change.value;
|
|
43
|
+
}
|
|
44
|
+
return value;
|
|
45
|
+
}
|
|
46
|
+
/** Tick by tick for the ticks every trace has. */
|
|
47
|
+
function differingTicks(traces, name, shared) {
|
|
48
|
+
const differing = [];
|
|
49
|
+
for (let tick = 1; tick <= shared; tick++) {
|
|
50
|
+
const reference = traces[0].trace.ticks[tick - 1][name];
|
|
51
|
+
if (traces.some(({ trace }) => !sameTraceValue(trace.ticks[tick - 1][name], reference)))
|
|
52
|
+
differing.push(tick);
|
|
53
|
+
}
|
|
54
|
+
return differing;
|
|
55
|
+
}
|
|
56
|
+
/** At every moment any trace ticks, up to `until`; also how long the values differ in total. */
|
|
57
|
+
function differingTimes(changes, moments, until) {
|
|
58
|
+
const differing = [];
|
|
59
|
+
let differingFor = 0;
|
|
60
|
+
moments.forEach((moment, index) => {
|
|
61
|
+
const reference = valueOf(changes[0], 'time', moment);
|
|
62
|
+
if (changes.some((other) => !sameTraceValue(valueOf(other, 'time', moment), reference))) {
|
|
63
|
+
differing.push(moment);
|
|
64
|
+
differingFor += (moments[index + 1] ?? until) - moment;
|
|
65
|
+
}
|
|
66
|
+
});
|
|
67
|
+
return { differing, differingFor: round(differingFor) };
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Compares the traces against the first: tick by tick (or moment by moment, see
|
|
71
|
+
* {@link Alignment}), and change by change. Changes still line up when one model reacts a few
|
|
72
|
+
* ticks, or a fraction of a second, later than the other.
|
|
73
|
+
*/
|
|
74
|
+
export function compareTraces(traces) {
|
|
75
|
+
if (traces.length < 2)
|
|
76
|
+
throw new Error('Pick at least two traces to compare.');
|
|
77
|
+
const times = traces.map(({ trace }) => traceTimes(trace));
|
|
78
|
+
const timed = times.every((entry) => entry !== undefined && entry.length > 0);
|
|
79
|
+
// Runs that read the same deltaT every tick tick at the same moments: compare tick by tick.
|
|
80
|
+
const sameGrid = timed &&
|
|
81
|
+
times.every((entry) => entry.length === times[0].length &&
|
|
82
|
+
entry.every((time, index) => Math.abs(time - times[0][index]) <= EPSILON));
|
|
83
|
+
const alignment = timed && !sameGrid ? 'time' : 'tick';
|
|
84
|
+
const names = new Map();
|
|
85
|
+
traces.forEach(({ label, trace }) => trace.variables.forEach(({ name, role }) => {
|
|
86
|
+
const entry = names.get(name) ?? { role, labels: [] };
|
|
87
|
+
entry.role = entry.role ?? role;
|
|
88
|
+
entry.labels.push(label);
|
|
89
|
+
names.set(name, entry);
|
|
90
|
+
}));
|
|
91
|
+
const common = Array.from(names.entries())
|
|
92
|
+
.filter(([, entry]) => entry.labels.length === traces.length)
|
|
93
|
+
.sort(([, a], [, b]) => ROLE_ORDER.indexOf(a.role) - ROLE_ORDER.indexOf(b.role));
|
|
94
|
+
const shared = Math.min(...traces.map(({ trace }) => trace.ticks.length));
|
|
95
|
+
const until = alignment === 'time' ? Math.min(...times.map((entry) => entry[entry.length - 1])) : undefined;
|
|
96
|
+
const moments = alignment === 'time'
|
|
97
|
+
? Array.from(new Set(times.flatMap((entry) => entry.filter((time) => time <= until + EPSILON))))
|
|
98
|
+
.sort((a, b) => a - b)
|
|
99
|
+
.filter((time, index, all) => index === 0 || time - all[index - 1] > EPSILON)
|
|
100
|
+
: [];
|
|
101
|
+
const variables = common.map(([name, { role }]) => {
|
|
102
|
+
const changes = traces.map(({ trace }, index) => changesOf(trace, name, times[index]));
|
|
103
|
+
const [first, ...rest] = changes;
|
|
104
|
+
const sameSequence = rest.every((other) => other.length === first.length &&
|
|
105
|
+
other.every((change, index) => sameTraceValue(change.value, first[index].value)));
|
|
106
|
+
let worstTickShift = 0;
|
|
107
|
+
let worstTimeShift = timed ? 0 : undefined;
|
|
108
|
+
rest.forEach((other) => other.slice(0, first.length).forEach((change, index) => {
|
|
109
|
+
worstTickShift = Math.max(worstTickShift, Math.abs(change.tick - first[index].tick));
|
|
110
|
+
if (worstTimeShift !== undefined) {
|
|
111
|
+
const shift = Math.abs((change.time ?? 0) - (first[index].time ?? 0));
|
|
112
|
+
worstTimeShift = Math.max(worstTimeShift, round(shift));
|
|
113
|
+
}
|
|
114
|
+
}));
|
|
115
|
+
const base = { name, role, changes, sameSequence, worstTickShift, worstTimeShift };
|
|
116
|
+
if (alignment === 'time') {
|
|
117
|
+
return { ...base, compared: moments.length, ...differingTimes(changes, moments, until) };
|
|
118
|
+
}
|
|
119
|
+
return { ...base, compared: shared, differing: differingTicks(traces, name, shared) };
|
|
120
|
+
});
|
|
121
|
+
let printed;
|
|
122
|
+
if (traces.some(({ trace }) => trace.printed?.some((lines) => lines.length > 0))) {
|
|
123
|
+
const lines = traces.map(({ trace }, index) => (trace.printed ?? []).flatMap((texts, tick) => texts.map((text) => ({ tick: tick + 1, time: times[index]?.[tick], text }))));
|
|
124
|
+
const [first, ...rest] = lines;
|
|
125
|
+
let worstTickShift = 0;
|
|
126
|
+
let worstTimeShift = timed ? 0 : undefined;
|
|
127
|
+
rest.forEach((other) => other.slice(0, first.length).forEach((line, index) => {
|
|
128
|
+
worstTickShift = Math.max(worstTickShift, Math.abs(line.tick - first[index].tick));
|
|
129
|
+
if (worstTimeShift !== undefined) {
|
|
130
|
+
worstTimeShift = Math.max(worstTimeShift, round(Math.abs((line.time ?? 0) - (first[index].time ?? 0))));
|
|
131
|
+
}
|
|
132
|
+
}));
|
|
133
|
+
printed = {
|
|
134
|
+
lines,
|
|
135
|
+
sameSequence: rest.every((other) => other.length === first.length && other.every((line, index) => line.text === first[index].text)),
|
|
136
|
+
worstTickShift,
|
|
137
|
+
worstTimeShift,
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
return {
|
|
141
|
+
alignment,
|
|
142
|
+
printed,
|
|
143
|
+
traces: traces.map(({ label, trace }, index) => ({
|
|
144
|
+
label,
|
|
145
|
+
model: trace.model,
|
|
146
|
+
ticks: trace.ticks.length,
|
|
147
|
+
duration: times[index]?.[times[index].length - 1],
|
|
148
|
+
})),
|
|
149
|
+
until,
|
|
150
|
+
variables,
|
|
151
|
+
partial: Array.from(names.entries())
|
|
152
|
+
.filter(([, entry]) => entry.labels.length < traces.length)
|
|
153
|
+
.map(([name, entry]) => ({ name, labels: entry.labels })),
|
|
154
|
+
};
|
|
155
|
+
}
|
|
156
|
+
//# sourceMappingURL=trace-compare.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"trace-compare.js","sourceRoot":"","sources":["../src/trace-compare.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AAEH,OAAO,EAAE,cAAc,EAAE,UAAU,EAAE,0BAAsB;AAoE3D,MAAM,UAAU,GAA8B,CAAC,OAAO,EAAE,QAAQ,EAAE,OAAO,EAAE,SAAS,CAAC,CAAA;AAErF,6CAA6C;AAC7C,MAAM,OAAO,GAAG,IAAI,CAAA;AAEpB,SAAS,KAAK,CAAC,KAAa;IACxB,OAAO,IAAI,CAAC,KAAK,CAAC,KAAK,GAAG,GAAG,CAAC,GAAG,GAAG,CAAA;AACxC,CAAC;AAED,SAAS,SAAS,CAAC,KAAkB,EAAE,IAAY,EAAE,KAA2B;IAC5E,MA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AAK,CACpB,CAAC,KAAK,EAAE,EAAE,CACN,KAAK,CAAC,MAAM,KAAK,KAAK,CAAC,MAAM,IAAI,KAAK,CAAC,KAAK,CAAC,CAAC,IAAI,EAAE,KAAK,EAAE,EAAE,CAAC,IAAI,CAAC,IAAI,KAAK,KAAK,CAAC,KAAK,CAAC,CAAC,IAAI,CAAC,CACrG;YACD,cAAc;YACd,cAAc;SACjB,CAAA;IACL,CAAC;IAED,OAAO;QACH,SAAS;QACT,OAAO;QACP,MAAM,EAAE,MAAM,CAAC,GAAG,CAAC,CAAC,EAAE,KAAK,EAAE,KAAK,EAAE,EAAE,KAAK,EAAE,EAAE,CAAC,CAAC;YAC7C,KAAK;YACL,KAAK,EAAE,KAAK,CAAC,KAAK;YAClB,KAAK,EAAE,KAAK,CAAC,KAAK,CAAC,MAAM;YACzB,QAAQ,EAAE,KAAK,CAAC,KAAK,CAAC,EAAE,CAAC,KAAK,CAAC,KAAK,CAAE,CAAC,MAAM,GAAG,CAAC,CAAC;SACrD,CAAC,CAAC;QACH,KAAK;QACL,SAAS;QACT,OAAO,EAAE,KAAK,CAAC,IAAI,CAAC,KAAK,CAAC,OAAO,EAAE,CAAC;aAC/B,MAAM,CAAC,CAAC,CAAC,EAAE,KAAK,CAAC,EAAE,EAAE,CAAC,KAAK,CAAC,MAAM,CAAC,MAAM,GAAG,MAAM,CAAC,MAAM,CAAC;aAC1D,GAAG,CAAC,CAAC,CAAC,IAAI,EAAE,KAAK,CAAC,EAAE,EAAE,CAAC,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,KAAK,CAAC,MAAM,EAAE,CAAC,CAAC;KAChE,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 { sameTraceValue, traceTimes } from './trace-record'\nimport type { TraceRecord, TraceRole } from './trace-record'\n\n// Compares runs of models, usually two implementations of one spec fed the same inputs. The reports\n// are in comparison-report.ts.\n\nexport interface NamedTrace {\n /** How the report names the run, e.g. the trace's file name. */\n label: string\n trace: TraceRecord\n}\n\n/** A variable taking a new value: at the first tick, and wherever it differs from the tick before. */\nexport interface ValueChange {\n tick: number\n time?: number\n value: unknown\n}\n\n/**\n * `tick`: tick n of one trace against tick n of the others. `time`: every trace has times (runs\n * on a timed sequence, or ticked with deltaT), so the values are compared at every moment any\n * trace ticks. That still works when the models tick at different moments and different rates.\n */\nexport type Alignment = 'tick' | 'time'\n\nexport interface VariableComparison {\n name: string\n role?: TraceRole\n /** Ticks (or, aligned by time, moments) compared one to one. */\n compared: number\n /** Ticks, or times in seconds, where a trace differs from the first. */\n differing: number[]\n /** Aligned by time: for how many seconds some trace differs from the first. */\n differingFor?: number\n /** Per trace, the changes of value. */\n changes: ValueChange[][]\n /** Every trace goes through the same values in the same order, whenever it does so. */\n sameSequence: boolean\n /** Largest distance, in ticks, between the n-th change of a trace and of the first. */\n worstTickShift: number\n /** The same in seconds, when every trace has times. */\n worstTimeShift?: number\n}\n\n/** The lines the models printed, each with its tick, lined up the n-th with the n-th like value changes. */\nexport interface PrintedComparison {\n /** Per trace, every printed line in order. */\n lines: { tick: number; time?: number; text: string }[][]\n /** Every trace printed the same lines in the same order. */\n sameSequence: boolean\n worstTickShift: number\n worstTimeShift?: number\n}\n\nexport interface TraceComparison {\n alignment: Alignment\n traces: { label: string; model?: string; ticks: number; duration?: number }[]\n /** Aligned by time: the moments compared run up to here, the end of the shortest trace. */\n until?: number\n /** Variables in every trace, inputs first, then outputs, then the rest. */\n variables: VariableComparison[]\n /** Variables only some traces have, with the traces that have them. */\n partial: { name: string; labels: string[] }[]\n /** When any trace printed something. */\n printed?: PrintedComparison\n}\n\nconst ROLE_ORDER: (TraceRole | undefined)[] = ['input', 'output', 'local', undefined]\n\n/** Times closer than this are one moment. */\nconst EPSILON = 1e-9\n\nfunction round(value: number): number {\n return Math.round(value * 1e9) / 1e9\n}\n\nfunction changesOf(trace: TraceRecord, name: string, times: number[] | undefined): ValueChange[] {\n const changes: ValueChange[] = []\n trace.ticks.forEach((tick, index) => {\n const value = tick[name]\n const last = changes[changes.length - 1]\n if (value === undefined || (last && sameTraceValue(last.value, value))) return\n changes.push({ tick: index + 1, time: times?.[index], value })\n })\n return changes\n}\n\n/** The value a variable holds after `tick`, or at `time`, from its changes. */\nexport function valueOf(changes: ValueChange[], alignment: Alignment, at: number): unknown {\n let value: unknown\n for (const change of changes) {\n const moment = alignment === 'time' ? change.time ?? 0 : change.tick\n if (moment > at + (alignment === 'time' ? EPSILON : 0)) break\n value = change.value\n }\n return value\n}\n\n/** Tick by tick for the ticks every trace has. */\nfunction differingTicks(traces: NamedTrace[], name: string, shared: number): number[] {\n const differing: number[] = []\n for (let tick = 1; tick <= shared; tick++) {\n const reference = traces[0].trace.ticks[tick - 1][name]\n if (traces.some(({ trace }) => !sameTraceValue(trace.ticks[tick - 1][name], reference))) differing.push(tick)\n }\n return differing\n}\n\n/** At every moment any trace ticks, up to `until`; also how long the values differ in total. */\nfunction differingTimes(\n changes: ValueChange[][],\n moments: number[],\n until: number\n): { differing: number[]; differingFor: number } {\n const differing: number[] = []\n let differingFor = 0\n moments.forEach((moment, index) => {\n const reference = valueOf(changes[0], 'time', moment)\n if (changes.some((other) => !sameTraceValue(valueOf(other, 'time', moment), reference))) {\n differing.push(moment)\n differingFor += (moments[index + 1] ?? until) - moment\n }\n })\n return { differing, differingFor: round(differingFor) }\n}\n\n/**\n * Compares the traces against the first: tick by tick (or moment by moment, see\n * {@link Alignment}), and change by change. Changes still line up when one model reacts a few\n * ticks, or a fraction of a second, later than the other.\n */\nexport function compareTraces(traces: NamedTrace[]): TraceComparison {\n if (traces.length < 2) throw new Error('Pick at least two traces to compare.')\n const times = traces.map(({ trace }) => traceTimes(trace))\n const timed = times.every((entry) => entry !== undefined && entry.length > 0)\n // Runs that read the same deltaT every tick tick at the same moments: compare tick by tick.\n const sameGrid =\n timed &&\n times.every(\n (entry) =>\n entry!.length === times[0]!.length &&\n entry!.every((time, index) => Math.abs(time - times[0]![index]) <= EPSILON)\n )\n const alignment: Alignment = timed && !sameGrid ? 'time' : 'tick'\n const names = new Map<string, { role?: TraceRole; labels: string[] }>()\n traces.forEach(({ label, trace }) =>\n trace.variables.forEach(({ name, role }) => {\n const entry = names.get(name) ?? { role, labels: [] }\n entry.role = entry.role ?? role\n entry.labels.push(label)\n names.set(name, entry)\n })\n )\n const common = Array.from(names.entries())\n .filter(([, entry]) => entry.labels.length === traces.length)\n .sort(([, a], [, b]) => ROLE_ORDER.indexOf(a.role) - ROLE_ORDER.indexOf(b.role))\n const shared = Math.min(...traces.map(({ trace }) => trace.ticks.length))\n const until = alignment === 'time' ? Math.min(...times.map((entry) => entry![entry!.length - 1])) : undefined\n const moments =\n alignment === 'time'\n ? Array.from(new Set(times.flatMap((entry) => entry!.filter((time) => time <= until! + EPSILON))))\n .sort((a, b) => a - b)\n .filter((time, index, all) => index === 0 || time - all[index - 1] > EPSILON)\n : []\n\n const variables = common.map(([name, { role }]): VariableComparison => {\n const changes = traces.map(({ trace }, index) => changesOf(trace, name, times[index]))\n const [first, ...rest] = changes\n const sameSequence = rest.every(\n (other) =>\n other.length === first.length &&\n other.every((change, index) => sameTraceValue(change.value, first[index].value))\n )\n let worstTickShift = 0\n let worstTimeShift = timed ? 0 : undefined\n rest.forEach((other) =>\n other.slice(0, first.length).forEach((change, index) => {\n worstTickShift = Math.max(worstTickShift, Math.abs(change.tick - first[index].tick))\n if (worstTimeShift !== undefined) {\n const shift = Math.abs((change.time ?? 0) - (first[index].time ?? 0))\n worstTimeShift = Math.max(worstTimeShift, round(shift))\n }\n })\n )\n const base = { name, role, changes, sameSequence, worstTickShift, worstTimeShift }\n if (alignment === 'time') {\n return { ...base, compared: moments.length, ...differingTimes(changes, moments, until!) }\n }\n return { ...base, compared: shared, differing: differingTicks(traces, name, shared) }\n })\n\n let printed: PrintedComparison | undefined\n if (traces.some(({ trace }) => trace.printed?.some((lines) => lines.length > 0))) {\n const lines = traces.map(({ trace }, index) =>\n (trace.printed ?? []).flatMap((texts, tick) =>\n texts.map((text) => ({ tick: tick + 1, time: times[index]?.[tick], text }))\n )\n )\n const [first, ...rest] = lines\n let worstTickShift = 0\n let worstTimeShift = timed ? 0 : undefined\n rest.forEach((other) =>\n other.slice(0, first.length).forEach((line, index) => {\n worstTickShift = Math.max(worstTickShift, Math.abs(line.tick - first[index].tick))\n if (worstTimeShift !== undefined) {\n worstTimeShift = Math.max(\n worstTimeShift,\n round(Math.abs((line.time ?? 0) - (first[index].time ?? 0)))\n )\n }\n })\n )\n printed = {\n lines,\n sameSequence: rest.every(\n (other) =>\n other.length === first.length && other.every((line, index) => line.text === first[index].text)\n ),\n worstTickShift,\n worstTimeShift,\n }\n }\n\n return {\n alignment,\n printed,\n traces: traces.map(({ label, trace }, index) => ({\n label,\n model: trace.model,\n ticks: trace.ticks.length,\n duration: times[index]?.[times[index]!.length - 1],\n })),\n until,\n variables,\n partial: Array.from(names.entries())\n .filter(([, entry]) => entry.labels.length < traces.length)\n .map(([name, entry]) => ({ name, labels: entry.labels })),\n }\n}\n"]}
|