@sccharts-lab/trace 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +277 -0
- package/README.md +233 -2
- package/dist/comparison-report.d.ts +15 -0
- package/dist/comparison-report.js +258 -0
- package/dist/comparison-report.js.map +1 -0
- package/dist/csv.d.ts +13 -0
- package/dist/csv.js +188 -0
- package/dist/csv.js.map +1 -0
- package/dist/data-pool.d.ts +2 -0
- package/dist/data-pool.js +26 -0
- package/dist/data-pool.js.map +1 -0
- package/dist/engine-replay.d.ts +78 -0
- package/dist/engine-replay.js +115 -0
- package/dist/engine-replay.js.map +1 -0
- package/dist/engine-sequence.d.ts +26 -0
- package/dist/engine-sequence.js +132 -0
- package/dist/engine-sequence.js.map +1 -0
- package/dist/engine.d.ts +51 -0
- package/dist/engine.js +210 -0
- package/dist/engine.js.map +1 -0
- package/dist/helper.d.ts +10 -0
- package/dist/helper.js +26 -0
- package/dist/helper.js.map +1 -0
- package/dist/index.d.ts +18 -0
- package/dist/index.js +18 -0
- package/dist/index.js.map +1 -0
- package/dist/input-value.d.ts +2 -0
- package/dist/input-value.js +38 -0
- package/dist/input-value.js.map +1 -0
- package/dist/json.d.ts +8 -0
- package/dist/json.js +122 -0
- package/dist/json.js.map +1 -0
- package/dist/ktrace.d.ts +11 -0
- package/dist/ktrace.js +266 -0
- package/dist/ktrace.js.map +1 -0
- package/dist/protocol.d.ts +25 -0
- package/dist/protocol.js +18 -0
- package/dist/protocol.js.map +1 -0
- package/dist/trace-compare.d.ts +77 -0
- package/dist/trace-compare.js +156 -0
- package/dist/trace-compare.js.map +1 -0
- package/dist/trace-file.d.ts +6 -0
- package/dist/trace-file.js +65 -0
- package/dist/trace-file.js.map +1 -0
- package/dist/trace-record.d.ts +71 -0
- package/dist/trace-record.js +181 -0
- package/dist/trace-record.js.map +1 -0
- package/dist/trace-rename.d.ts +40 -0
- package/dist/trace-rename.js +112 -0
- package/dist/trace-rename.js.map +1 -0
- package/dist/trace-replay.d.ts +72 -0
- package/dist/trace-replay.js +169 -0
- package/dist/trace-replay.js.map +1 -0
- package/dist/trace-timing.d.ts +55 -0
- package/dist/trace-timing.js +122 -0
- package/dist/trace-timing.js.map +1 -0
- package/dist/xml.d.ts +8 -0
- package/dist/xml.js +179 -0
- package/dist/xml.js.map +1 -0
- package/package.json +70 -3
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { TraceFormat, TraceRecord } from "./trace-record.js";
|
|
2
|
+
export { formatTraceValue, inputsOnly, printedOf, sameTraceValue, timeDeltaOf, TRACE_FORMAT_ID, TRACE_FORMATS, TraceFileError, traceFormatOf, traceTimes, } from "./trace-record.js";
|
|
3
|
+
export type { TraceFormat, TraceRecord, TraceRole, TraceVariable } from "./trace-record.js";
|
|
4
|
+
export declare function writeTrace(trace: TraceRecord, format: TraceFormat): string;
|
|
5
|
+
/** Reads a trace in `format`, or in the format a file name's extension names. */
|
|
6
|
+
export declare function readTrace(text: string, formatOrFileName: TraceFormat | string): TraceRecord;
|
|
@@ -0,0 +1,65 @@
|
|
|
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 { readCsv, writeCsv } from "./csv.js";
|
|
18
|
+
import { readJson, writeJson } from "./json.js";
|
|
19
|
+
import { readKTrace, writeKTrace } from "./ktrace.js";
|
|
20
|
+
import { TraceFileError, traceFormatOf } from "./trace-record.js";
|
|
21
|
+
import { readXml, writeXml } from "./xml.js";
|
|
22
|
+
// Simulation traces as files: what Save writes, and what Replay and Compare read. KTrace is written
|
|
23
|
+
// the way KIELER writes it and loads in the official tools; CSV, JSON and XML hold every variable.
|
|
24
|
+
export { formatTraceValue, inputsOnly, printedOf, sameTraceValue, timeDeltaOf, TRACE_FORMAT_ID, TRACE_FORMATS, TraceFileError, traceFormatOf, traceTimes, } from "./trace-record.js";
|
|
25
|
+
export function writeTrace(trace, format) {
|
|
26
|
+
switch (format) {
|
|
27
|
+
case 'ktrace':
|
|
28
|
+
return writeKTrace(trace);
|
|
29
|
+
case 'csv':
|
|
30
|
+
return writeCsv(trace);
|
|
31
|
+
case 'json':
|
|
32
|
+
return writeJson(trace);
|
|
33
|
+
default:
|
|
34
|
+
return writeXml(trace);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
/** The format itself, or the one a file name's extension names (`run.ktrace`, `a/b/Run.CSV`). */
|
|
38
|
+
function formatFrom(formatOrFileName) {
|
|
39
|
+
const format = traceFormatOf(`.${formatOrFileName}`);
|
|
40
|
+
if (!format) {
|
|
41
|
+
throw new TraceFileError(`${formatOrFileName} is not a trace. Use a .ktrace, .csv, .json or .xml file.`);
|
|
42
|
+
}
|
|
43
|
+
return format;
|
|
44
|
+
}
|
|
45
|
+
/** Reads a trace in `format`, or in the format a file name's extension names. */
|
|
46
|
+
export function readTrace(text, formatOrFileName) {
|
|
47
|
+
let trace;
|
|
48
|
+
switch (formatFrom(formatOrFileName)) {
|
|
49
|
+
case 'ktrace':
|
|
50
|
+
trace = readKTrace(text);
|
|
51
|
+
break;
|
|
52
|
+
case 'csv':
|
|
53
|
+
trace = readCsv(text);
|
|
54
|
+
break;
|
|
55
|
+
case 'json':
|
|
56
|
+
trace = readJson(text);
|
|
57
|
+
break;
|
|
58
|
+
default:
|
|
59
|
+
trace = readXml(text);
|
|
60
|
+
}
|
|
61
|
+
if (trace.ticks.length === 0)
|
|
62
|
+
throw new TraceFileError('The trace has no ticks.');
|
|
63
|
+
return trace;
|
|
64
|
+
}
|
|
65
|
+
//# sourceMappingURL=trace-file.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"trace-file.js","sourceRoot":"","sources":["../src/trace-file.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AAEH,OAAO,EAAE,OAAO,EAAE,QAAQ,EAAE,iBAAa;AACzC,OAAO,EAAE,QAAQ,EAAE,SAAS,EAAE,kBAAc;AAC5C,OAAO,EAAE,UAAU,EAAE,WAAW,EAAE,oBAAgB;AAClD,OAAO,EAAE,cAAc,EAAE,aAAa,EAAE,0BAAsB;AAE9D,OAAO,EAAE,OAAO,EAAE,QAAQ,EAAE,iBAAa;AAEzC,oGAAoG;AACpG,mGAAmG;AAEnG,OAAO,EACH,gBAAgB,EAChB,UAAU,EACV,SAAS,EACT,cAAc,EACd,WAAW,EACX,eAAe,EACf,aAAa,EACb,cAAc,EACd,aAAa,EACb,UAAU,GACb,0BAAsB;AAGvB,MAAM,UAAU,UAAU,CAAC,KAAkB,EAAE,MAAmB;IAC9D,QAAQ,MAAM,EAAE,CAAC;QACb,KAAK,QAAQ;YACT,OAAO,WAAW,CAAC,KAAK,CAAC,CAAA;QAC7B,KAAK,KAAK;YACN,OAAO,QAAQ,CAAC,KAAK,CAAC,CAAA;QAC1B,KAAK,MAAM;YACP,OAAO,SAAS,CAAC,KAAK,CAAC,CAAA;QAC3B;YACI,OAAO,QAAQ,CAAC,KAAK,CAAC,CAAA;IAC9B,CAAC;AACL,CAAC;AAED,iGAAiG;AACjG,SAAS,UAAU,CAAC,gBAAwB;IACxC,MAAM,MAAM,GAAG,aAAa,CAAC,IAAI,gBAAgB,EAAE,CAAC,CAAA;IACpD,IAAI,CAAC,MAAM,EAAE,CAAC;QACV,MAAM,IAAI,cAAc,CAAC,GAAG,gBAAgB,2DAA2D,CAAC,CAAA;IAC5G,CAAC;IACD,OAAO,MAAM,CAAA;AACjB,CAAC;AAED,iFAAiF;AACjF,MAAM,UAAU,SAAS,CAAC,IAAY,EAAE,gBAAsC;IAC1E,IAAI,KAAkB,CAAA;IACtB,QAAQ,UAAU,CAAC,gBAAgB,CAAC,EAAE,CAAC;QACnC,KAAK,QAAQ;YACT,KAAK,GAAG,UAAU,CAAC,IAAI,CAAC,CAAA;YACxB,MAAK;QACT,KAAK,KAAK;YACN,KAAK,GAAG,OAAO,CAAC,IAAI,CAAC,CAAA;YACrB,MAAK;QACT,KAAK,MAAM;YACP,KAAK,GAAG,QAAQ,CAAC,IAAI,CAAC,CAAA;YACtB,MAAK;QACT;YACI,KAAK,GAAG,OAAO,CAAC,IAAI,CAAC,CAAA;IAC7B,CAAC;IACD,IAAI,KAAK,CAAC,KAAK,CAAC,MAAM,KAAK,CAAC;QAAE,MAAM,IAAI,cAAc,CAAC,yBAAyB,CAAC,CAAA;IACjF,OAAO,KAAK,CAAA;AAChB,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 { readCsv, writeCsv } from './csv'\nimport { readJson, writeJson } from './json'\nimport { readKTrace, writeKTrace } from './ktrace'\nimport { TraceFileError, traceFormatOf } from './trace-record'\nimport type { TraceFormat, TraceRecord } from './trace-record'\nimport { readXml, writeXml } from './xml'\n\n// Simulation traces as files: what Save writes, and what Replay and Compare read. KTrace is written\n// the way KIELER writes it and loads in the official tools; CSV, JSON and XML hold every variable.\n\nexport {\n formatTraceValue,\n inputsOnly,\n printedOf,\n sameTraceValue,\n timeDeltaOf,\n TRACE_FORMAT_ID,\n TRACE_FORMATS,\n TraceFileError,\n traceFormatOf,\n traceTimes,\n} from './trace-record'\nexport type { TraceFormat, TraceRecord, TraceRole, TraceVariable } from './trace-record'\n\nexport function writeTrace(trace: TraceRecord, format: TraceFormat): string {\n switch (format) {\n case 'ktrace':\n return writeKTrace(trace)\n case 'csv':\n return writeCsv(trace)\n case 'json':\n return writeJson(trace)\n default:\n return writeXml(trace)\n }\n}\n\n/** The format itself, or the one a file name's extension names (`run.ktrace`, `a/b/Run.CSV`). */\nfunction formatFrom(formatOrFileName: string): TraceFormat {\n const format = traceFormatOf(`.${formatOrFileName}`)\n if (!format) {\n throw new TraceFileError(`${formatOrFileName} is not a trace. Use a .ktrace, .csv, .json or .xml file.`)\n }\n return format\n}\n\n/** Reads a trace in `format`, or in the format a file name's extension names. */\nexport function readTrace(text: string, formatOrFileName: TraceFormat | string): TraceRecord {\n let trace: TraceRecord\n switch (formatFrom(formatOrFileName)) {\n case 'ktrace':\n trace = readKTrace(text)\n break\n case 'csv':\n trace = readCsv(text)\n break\n case 'json':\n trace = readJson(text)\n break\n default:\n trace = readXml(text)\n }\n if (trace.ticks.length === 0) throw new TraceFileError('The trace has no ticks.')\n return trace\n}\n"]}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
export type TraceRole = 'input' | 'output' | 'local';
|
|
2
|
+
export interface TraceVariable {
|
|
3
|
+
name: string;
|
|
4
|
+
/** Unknown when the file does not record it, as in CSV. */
|
|
5
|
+
role?: TraceRole;
|
|
6
|
+
/** The model's type name (bool, int, float, string), when known. */
|
|
7
|
+
type?: string;
|
|
8
|
+
}
|
|
9
|
+
/** A run of a model: every variable's value after each tick. */
|
|
10
|
+
export interface TraceRecord {
|
|
11
|
+
/** File name of the simulated model, when known. */
|
|
12
|
+
model?: string;
|
|
13
|
+
variables: TraceVariable[];
|
|
14
|
+
/** `ticks[i]` holds the values after tick i + 1. A value the file never gave is undefined. */
|
|
15
|
+
ticks: Record<string, unknown>[];
|
|
16
|
+
/** Ticks (1-based) after which a replay stops running, from KTrace `pause`. */
|
|
17
|
+
pauses?: number[];
|
|
18
|
+
/** Things the reader dropped or guessed, worth telling the user. */
|
|
19
|
+
notes?: string[];
|
|
20
|
+
/**
|
|
21
|
+
* Seconds at each tick, for a run on a timed sequence. In a sequence, the moments its rows
|
|
22
|
+
* happen: such a sequence describes input changes in time rather than per tick.
|
|
23
|
+
*/
|
|
24
|
+
times?: number[];
|
|
25
|
+
/** What the model printed during each tick, line by line: `printed[i]` for tick i + 1. */
|
|
26
|
+
printed?: string[][];
|
|
27
|
+
}
|
|
28
|
+
export type TraceFormat = 'ktrace' | 'csv' | 'json' | 'xml';
|
|
29
|
+
export declare const TRACE_FORMATS: {
|
|
30
|
+
format: TraceFormat;
|
|
31
|
+
label: string;
|
|
32
|
+
extension: string;
|
|
33
|
+
}[];
|
|
34
|
+
/** Identifies this lab's JSON and XML traces. */
|
|
35
|
+
export declare const TRACE_FORMAT_ID = "sccharts-lab-trace";
|
|
36
|
+
export declare class TraceFileError extends Error {
|
|
37
|
+
constructor(message: string, line?: number);
|
|
38
|
+
}
|
|
39
|
+
export declare function traceFormatOf(fileName: string): TraceFormat | undefined;
|
|
40
|
+
/** Values are the same when they are equal, or are primitives that print the same (CSV reads `"7"` as 7). */
|
|
41
|
+
export declare function sameTraceValue(a: unknown, b: unknown): boolean;
|
|
42
|
+
/** Text for a value in a report or a CSV cell: arrays and structs as JSON, strings as they are. */
|
|
43
|
+
export declare function formatTraceValue(value: unknown): string;
|
|
44
|
+
/** The variable that tells timed models how much time passed in a tick, if the trace has one. */
|
|
45
|
+
export declare function timeDeltaOf(trace: TraceRecord): TraceVariable | undefined;
|
|
46
|
+
/**
|
|
47
|
+
* Seconds after each tick: the times a timed run recorded, else the running sum of the time
|
|
48
|
+
* delta; undefined without either.
|
|
49
|
+
*/
|
|
50
|
+
export declare function traceTimes(trace: TraceRecord): number[] | undefined;
|
|
51
|
+
/** The printed lines per tick, or undefined when no tick printed anything. */
|
|
52
|
+
export declare function printedOf(lines: string[][] | undefined): string[][] | undefined;
|
|
53
|
+
/** The record with only the inputs, as replayed into another model. */
|
|
54
|
+
export declare function inputsOnly(trace: TraceRecord): TraceRecord;
|
|
55
|
+
/** Fills in the values a sparse file left out with the tick before's. */
|
|
56
|
+
export declare function carryForward(ticks: Record<string, unknown>[], variables: TraceVariable[]): Record<string, unknown>[];
|
|
57
|
+
/** Times given per tick count only without a time delta, which they would merely repeat. */
|
|
58
|
+
export declare function givenTimes(times: (number | undefined)[], variables: TraceVariable[], where: (index: number) => string): number[] | undefined;
|
|
59
|
+
export declare function typeOfValue(value: unknown): string | undefined;
|
|
60
|
+
/** Adds the variables a reader met, in order of first appearance. */
|
|
61
|
+
export declare class VariableList {
|
|
62
|
+
readonly variables: TraceVariable[];
|
|
63
|
+
private readonly byName;
|
|
64
|
+
add(name: string, role?: TraceRole, type?: string): TraceVariable;
|
|
65
|
+
get(name: string): TraceVariable | undefined;
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Text in a CSV cell or an XML element as a value. Arrays and structs are JSON; for strings the
|
|
69
|
+
* type decides, since a string input may well hold "12" or "true".
|
|
70
|
+
*/
|
|
71
|
+
export declare function decodeText(text: string, type?: string): unknown;
|
|
@@ -0,0 +1,181 @@
|
|
|
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 { isTimeDelta } from "./helper.js";
|
|
18
|
+
export const TRACE_FORMATS = [
|
|
19
|
+
{ format: 'ktrace', label: 'KTrace', extension: 'ktrace' },
|
|
20
|
+
{ format: 'csv', label: 'CSV', extension: 'csv' },
|
|
21
|
+
{ format: 'json', label: 'JSON', extension: 'json' },
|
|
22
|
+
{ format: 'xml', label: 'XML', extension: 'xml' },
|
|
23
|
+
];
|
|
24
|
+
/** Identifies this lab's JSON and XML traces. */
|
|
25
|
+
export const TRACE_FORMAT_ID = 'sccharts-lab-trace';
|
|
26
|
+
export class TraceFileError extends Error {
|
|
27
|
+
constructor(message, line) {
|
|
28
|
+
super(line === undefined ? message : `Line ${line}: ${message}`);
|
|
29
|
+
this.name = 'TraceFileError';
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
export function traceFormatOf(fileName) {
|
|
33
|
+
const extension = fileName.slice(fileName.lastIndexOf('.') + 1).toLowerCase();
|
|
34
|
+
return TRACE_FORMATS.find((entry) => entry.extension === extension)?.format;
|
|
35
|
+
}
|
|
36
|
+
/** Values are the same when they are equal, or are primitives that print the same (CSV reads `"7"` as 7). */
|
|
37
|
+
export function sameTraceValue(a, b) {
|
|
38
|
+
if (a === b)
|
|
39
|
+
return true;
|
|
40
|
+
if (Array.isArray(a) && Array.isArray(b)) {
|
|
41
|
+
return a.length === b.length && a.every((entry, index) => sameTraceValue(entry, b[index]));
|
|
42
|
+
}
|
|
43
|
+
if (a && b && typeof a === 'object' && typeof b === 'object') {
|
|
44
|
+
const keys = Object.keys(a);
|
|
45
|
+
return (keys.length === Object.keys(b).length &&
|
|
46
|
+
keys.every((key) => sameTraceValue(a[key], b[key])));
|
|
47
|
+
}
|
|
48
|
+
const primitive = (value) => ['boolean', 'number', 'string'].includes(typeof value);
|
|
49
|
+
return primitive(a) && primitive(b) && String(a) === String(b);
|
|
50
|
+
}
|
|
51
|
+
/** Text for a value in a report or a CSV cell: arrays and structs as JSON, strings as they are. */
|
|
52
|
+
export function formatTraceValue(value) {
|
|
53
|
+
if (value === undefined)
|
|
54
|
+
return '';
|
|
55
|
+
if (typeof value === 'string')
|
|
56
|
+
return value;
|
|
57
|
+
if (typeof value === 'number' || typeof value === 'boolean')
|
|
58
|
+
return String(value);
|
|
59
|
+
return JSON.stringify(value);
|
|
60
|
+
}
|
|
61
|
+
/** The variable that tells timed models how much time passed in a tick, if the trace has one. */
|
|
62
|
+
export function timeDeltaOf(trace) {
|
|
63
|
+
return trace.variables.find((variable) => variable.role !== 'output' &&
|
|
64
|
+
variable.role !== 'local' &&
|
|
65
|
+
isTimeDelta({ id: variable.name, categories: [] }));
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Seconds after each tick: the times a timed run recorded, else the running sum of the time
|
|
69
|
+
* delta; undefined without either.
|
|
70
|
+
*/
|
|
71
|
+
export function traceTimes(trace) {
|
|
72
|
+
if (trace.times)
|
|
73
|
+
return trace.times;
|
|
74
|
+
const delta = timeDeltaOf(trace);
|
|
75
|
+
if (!delta)
|
|
76
|
+
return undefined;
|
|
77
|
+
let time = 0;
|
|
78
|
+
return trace.ticks.map((tick) => {
|
|
79
|
+
const value = Number(tick[delta.name]);
|
|
80
|
+
if (Number.isFinite(value))
|
|
81
|
+
time += value;
|
|
82
|
+
// Float sums drift (0.1 + 0.2); a trace's times are only ever read to the microsecond.
|
|
83
|
+
return Math.round(time * 1e9) / 1e9;
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
/** The printed lines per tick, or undefined when no tick printed anything. */
|
|
87
|
+
export function printedOf(lines) {
|
|
88
|
+
return lines?.some((tick) => tick.length > 0) ? lines : undefined;
|
|
89
|
+
}
|
|
90
|
+
/** The record with only the inputs, as replayed into another model. */
|
|
91
|
+
export function inputsOnly(trace) {
|
|
92
|
+
const variables = trace.variables.filter((variable) => variable.role === 'input');
|
|
93
|
+
return {
|
|
94
|
+
...trace,
|
|
95
|
+
printed: undefined,
|
|
96
|
+
variables,
|
|
97
|
+
ticks: trace.ticks.map((tick) => Object.fromEntries(variables.map(({ name }) => [name, tick[name]]))),
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
/** Fills in the values a sparse file left out with the tick before's. */
|
|
101
|
+
export function carryForward(ticks, variables) {
|
|
102
|
+
const last = {};
|
|
103
|
+
return ticks.map((tick) => {
|
|
104
|
+
const full = {};
|
|
105
|
+
variables.forEach(({ name }) => {
|
|
106
|
+
if (tick[name] !== undefined)
|
|
107
|
+
last[name] = tick[name];
|
|
108
|
+
full[name] = last[name];
|
|
109
|
+
});
|
|
110
|
+
return full;
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
/** Times given per tick count only without a time delta, which they would merely repeat. */
|
|
114
|
+
export function givenTimes(times, variables, where) {
|
|
115
|
+
const hasDelta = variables.some((variable) => isTimeDelta({ id: variable.name, categories: [] }));
|
|
116
|
+
if (hasDelta || times.every((time) => time === undefined))
|
|
117
|
+
return undefined;
|
|
118
|
+
return times.map((time, index) => {
|
|
119
|
+
if (time === undefined || !Number.isFinite(time))
|
|
120
|
+
throw new TraceFileError(`${where(index)} has no time.`);
|
|
121
|
+
return time;
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
export function typeOfValue(value) {
|
|
125
|
+
if (Array.isArray(value))
|
|
126
|
+
return value.length > 0 ? typeOfValue(value[0]) : undefined;
|
|
127
|
+
if (typeof value === 'boolean')
|
|
128
|
+
return 'bool';
|
|
129
|
+
if (typeof value === 'number')
|
|
130
|
+
return Number.isInteger(value) ? 'int' : 'float';
|
|
131
|
+
if (typeof value === 'string')
|
|
132
|
+
return 'string';
|
|
133
|
+
return undefined;
|
|
134
|
+
}
|
|
135
|
+
/** Adds the variables a reader met, in order of first appearance. */
|
|
136
|
+
export class VariableList {
|
|
137
|
+
variables = [];
|
|
138
|
+
byName = new Map();
|
|
139
|
+
add(name, role, type) {
|
|
140
|
+
let variable = this.byName.get(name);
|
|
141
|
+
if (!variable) {
|
|
142
|
+
variable = { name, role, type };
|
|
143
|
+
this.byName.set(name, variable);
|
|
144
|
+
this.variables.push(variable);
|
|
145
|
+
}
|
|
146
|
+
else {
|
|
147
|
+
variable.role = variable.role ?? role;
|
|
148
|
+
// A float literal anywhere makes the variable a float, even when it began as 0.
|
|
149
|
+
variable.type = variable.type === 'int' && type === 'float' ? type : variable.type ?? type;
|
|
150
|
+
}
|
|
151
|
+
return variable;
|
|
152
|
+
}
|
|
153
|
+
get(name) {
|
|
154
|
+
return this.byName.get(name);
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* Text in a CSV cell or an XML element as a value. Arrays and structs are JSON; for strings the
|
|
159
|
+
* type decides, since a string input may well hold "12" or "true".
|
|
160
|
+
*/
|
|
161
|
+
export function decodeText(text, type) {
|
|
162
|
+
const trimmed = text.trim();
|
|
163
|
+
if (trimmed.startsWith('[') || trimmed.startsWith('{')) {
|
|
164
|
+
try {
|
|
165
|
+
return JSON.parse(trimmed);
|
|
166
|
+
}
|
|
167
|
+
catch {
|
|
168
|
+
// Not JSON after all: an ordinary string.
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
if (type === 'string')
|
|
172
|
+
return text;
|
|
173
|
+
if (trimmed === 'true' || trimmed === 'false')
|
|
174
|
+
return trimmed === 'true';
|
|
175
|
+
if (/^[+-]?(\d+\.?\d*|\.\d+)([eE][+-]?\d+)?$/.test(trimmed))
|
|
176
|
+
return Number(trimmed);
|
|
177
|
+
if (/^[+-]?0[xX][0-9a-fA-F]+$/.test(trimmed))
|
|
178
|
+
return Number.parseInt(trimmed, 16);
|
|
179
|
+
return text;
|
|
180
|
+
}
|
|
181
|
+
//# sourceMappingURL=trace-record.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"trace-record.js","sourceRoot":"","sources":["../src/trace-record.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AAEH,OAAO,EAAE,WAAW,EAAE,oBAAgB;AAqC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F,IAAI,0BAA0B,CAAC,IAAI,CAAC,OAAO,CAAC;QAAE,OAAO,MAAM,CAAC,QAAQ,CAAC,OAAO,EAAE,EAAE,CAAC,CAAA;IACjF,OAAO,IAAI,CAAA;AACf,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 { isTimeDelta } from './helper'\n\n// Simulation traces in memory: what Save writes, and what Replay and Compare read. The formats are in\n// ktrace.ts, csv.ts, json.ts and xml.ts; trace-file.ts reads and writes them.\n\nexport type TraceRole = 'input' | 'output' | 'local'\n\nexport interface TraceVariable {\n name: string\n /** Unknown when the file does not record it, as in CSV. */\n role?: TraceRole\n /** The model's type name (bool, int, float, string), when known. */\n type?: string\n}\n\n/** A run of a model: every variable's value after each tick. */\nexport interface TraceRecord {\n /** File name of the simulated model, when known. */\n model?: string\n variables: TraceVariable[]\n /** `ticks[i]` holds the values after tick i + 1. A value the file never gave is undefined. */\n ticks: Record<string, unknown>[]\n /** Ticks (1-based) after which a replay stops running, from KTrace `pause`. */\n pauses?: number[]\n /** Things the reader dropped or guessed, worth telling the user. */\n notes?: string[]\n /**\n * Seconds at each tick, for a run on a timed sequence. In a sequence, the moments its rows\n * happen: such a sequence describes input changes in time rather than per tick.\n */\n times?: number[]\n /** What the model printed during each tick, line by line: `printed[i]` for tick i + 1. */\n printed?: string[][]\n}\n\nexport type TraceFormat = 'ktrace' | 'csv' | 'json' | 'xml'\n\nexport const TRACE_FORMATS: { format: TraceFormat; label: string; extension: string }[] = [\n { format: 'ktrace', label: 'KTrace', extension: 'ktrace' },\n { format: 'csv', label: 'CSV', extension: 'csv' },\n { format: 'json', label: 'JSON', extension: 'json' },\n { format: 'xml', label: 'XML', extension: 'xml' },\n]\n\n/** Identifies this lab's JSON and XML traces. */\nexport const TRACE_FORMAT_ID = 'sccharts-lab-trace'\n\nexport class TraceFileError extends Error {\n constructor(message: string, line?: number) {\n super(line === undefined ? message : `Line ${line}: ${message}`)\n this.name = 'TraceFileError'\n }\n}\n\nexport function traceFormatOf(fileName: string): TraceFormat | undefined {\n const extension = fileName.slice(fileName.lastIndexOf('.') + 1).toLowerCase()\n return TRACE_FORMATS.find((entry) => entry.extension === extension)?.format\n}\n\n/** Values are the same when they are equal, or are primitives that print the same (CSV reads `\"7\"` as 7). */\nexport function sameTraceValue(a: unknown, b: unknown): boolean {\n if (a === b) return true\n if (Array.isArray(a) && Array.isArray(b)) {\n return a.length === b.length && a.every((entry, index) => sameTraceValue(entry, b[index]))\n }\n if (a && b && typeof a === 'object' && typeof b === 'object') {\n const keys = Object.keys(a)\n return (\n keys.length === Object.keys(b).length &&\n keys.every((key) =>\n sameTraceValue((a as Record<string, unknown>)[key], (b as Record<string, unknown>)[key])\n )\n )\n }\n const primitive = (value: unknown) => ['boolean', 'number', 'string'].includes(typeof value)\n return primitive(a) && primitive(b) && String(a) === String(b)\n}\n\n/** Text for a value in a report or a CSV cell: arrays and structs as JSON, strings as they are. */\nexport function formatTraceValue(value: unknown): string {\n if (value === undefined) return ''\n if (typeof value === 'string') return value\n if (typeof value === 'number' || typeof value === 'boolean') return String(value)\n return JSON.stringify(value)\n}\n\n/** The variable that tells timed models how much time passed in a tick, if the trace has one. */\nexport function timeDeltaOf(trace: TraceRecord): TraceVariable | undefined {\n return trace.variables.find(\n (variable) =>\n variable.role !== 'output' &&\n variable.role !== 'local' &&\n isTimeDelta({ id: variable.name, categories: [] })\n )\n}\n\n/**\n * Seconds after each tick: the times a timed run recorded, else the running sum of the time\n * delta; undefined without either.\n */\nexport function traceTimes(trace: TraceRecord): number[] | undefined {\n if (trace.times) return trace.times\n const delta = timeDeltaOf(trace)\n if (!delta) return undefined\n let time = 0\n return trace.ticks.map((tick) => {\n const value = Number(tick[delta.name])\n if (Number.isFinite(value)) time += value\n // Float sums drift (0.1 + 0.2); a trace's times are only ever read to the microsecond.\n return Math.round(time * 1e9) / 1e9\n })\n}\n\n/** The printed lines per tick, or undefined when no tick printed anything. */\nexport function printedOf(lines: string[][] | undefined): string[][] | undefined {\n return lines?.some((tick) => tick.length > 0) ? lines : undefined\n}\n\n/** The record with only the inputs, as replayed into another model. */\nexport function inputsOnly(trace: TraceRecord): TraceRecord {\n const variables = trace.variables.filter((variable) => variable.role === 'input')\n return {\n ...trace,\n printed: undefined,\n variables,\n ticks: trace.ticks.map((tick) => Object.fromEntries(variables.map(({ name }) => [name, tick[name]]))),\n }\n}\n\n/** Fills in the values a sparse file left out with the tick before's. */\nexport function carryForward(ticks: Record<string, unknown>[], variables: TraceVariable[]): Record<string, unknown>[] {\n const last: Record<string, unknown> = {}\n return ticks.map((tick) => {\n const full: Record<string, unknown> = {}\n variables.forEach(({ name }) => {\n if (tick[name] !== undefined) last[name] = tick[name]\n full[name] = last[name]\n })\n return full\n })\n}\n\n/** Times given per tick count only without a time delta, which they would merely repeat. */\nexport function givenTimes(\n times: (number | undefined)[],\n variables: TraceVariable[],\n where: (index: number) => string\n): number[] | undefined {\n const hasDelta = variables.some((variable) => isTimeDelta({ id: variable.name, categories: [] }))\n if (hasDelta || times.every((time) => time === undefined)) return undefined\n return times.map((time, index) => {\n if (time === undefined || !Number.isFinite(time)) throw new TraceFileError(`${where(index)} has no time.`)\n return time\n })\n}\n\nexport function typeOfValue(value: unknown): string | undefined {\n if (Array.isArray(value)) return value.length > 0 ? typeOfValue(value[0]) : undefined\n if (typeof value === 'boolean') return 'bool'\n if (typeof value === 'number') return Number.isInteger(value) ? 'int' : 'float'\n if (typeof value === 'string') return 'string'\n return undefined\n}\n\n/** Adds the variables a reader met, in order of first appearance. */\nexport class VariableList {\n readonly variables: TraceVariable[] = []\n\n private readonly byName = new Map<string, TraceVariable>()\n\n add(name: string, role?: TraceRole, type?: string): TraceVariable {\n let variable = this.byName.get(name)\n if (!variable) {\n variable = { name, role, type }\n this.byName.set(name, variable)\n this.variables.push(variable)\n } else {\n variable.role = variable.role ?? role\n // A float literal anywhere makes the variable a float, even when it began as 0.\n variable.type = variable.type === 'int' && type === 'float' ? type : variable.type ?? type\n }\n return variable\n }\n\n get(name: string): TraceVariable | undefined {\n return this.byName.get(name)\n }\n}\n\n/**\n * Text in a CSV cell or an XML element as a value. Arrays and structs are JSON; for strings the\n * type decides, since a string input may well hold \"12\" or \"true\".\n */\nexport function decodeText(text: string, type?: string): unknown {\n const trimmed = text.trim()\n if (trimmed.startsWith('[') || trimmed.startsWith('{')) {\n try {\n return JSON.parse(trimmed)\n } catch {\n // Not JSON after all: an ordinary string.\n }\n }\n if (type === 'string') return text\n if (trimmed === 'true' || trimmed === 'false') return trimmed === 'true'\n if (/^[+-]?(\\d+\\.?\\d*|\\.\\d+)([eE][+-]?\\d+)?$/.test(trimmed)) return Number(trimmed)\n if (/^[+-]?0[xX][0-9a-fA-F]+$/.test(trimmed)) return Number.parseInt(trimmed, 16)\n return text\n}\n"]}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import type { TraceRecord, TraceRole } from "./trace-record.js";
|
|
2
|
+
/** Names of one side to the names they stand for on the other. */
|
|
3
|
+
export type Renames = Record<string, string>;
|
|
4
|
+
/** Two names the user said are one variable, remembered for later matches. */
|
|
5
|
+
export type Alias = [string, string];
|
|
6
|
+
export interface NamedVariable {
|
|
7
|
+
name: string;
|
|
8
|
+
role?: TraceRole;
|
|
9
|
+
}
|
|
10
|
+
export interface RenameSuggestion {
|
|
11
|
+
from: string;
|
|
12
|
+
to: string;
|
|
13
|
+
/** The user matched these two before, or they differ only in case and separators. */
|
|
14
|
+
reason: 'remembered' | 'spelling';
|
|
15
|
+
}
|
|
16
|
+
/** Lower case without separators: NS_Green, nsGreen and ns-green are one name. */
|
|
17
|
+
export declare function nameKey(name: string): string;
|
|
18
|
+
/** Inputs pair with inputs and outputs with outputs; a side that does not know the role pairs with any. */
|
|
19
|
+
export declare function sameKind(a: NamedVariable, b: NamedVariable): boolean;
|
|
20
|
+
/** The names on each side that the other side does not have. */
|
|
21
|
+
export declare function unmatched(from: NamedVariable[], to: NamedVariable[]): {
|
|
22
|
+
from: NamedVariable[];
|
|
23
|
+
to: NamedVariable[];
|
|
24
|
+
};
|
|
25
|
+
/** Whether any name left over on one side could stand for one left over on the other. */
|
|
26
|
+
export declare function couldMatch(from: NamedVariable[], to: NamedVariable[]): boolean;
|
|
27
|
+
/**
|
|
28
|
+
* Pairs the names `from` has and `to` lacks with the names `to` has and `from` lacks: first the
|
|
29
|
+
* pairs in `aliases`, then names that are spelled the same apart from case and separators. A
|
|
30
|
+
* name pairs only when exactly one name on the other side fits it.
|
|
31
|
+
*/
|
|
32
|
+
export declare function suggestRenames(from: NamedVariable[], to: NamedVariable[], aliases?: Alias[]): RenameSuggestion[];
|
|
33
|
+
/** The trace with its variables renamed; names not in `renames` stay. */
|
|
34
|
+
export declare function renameTrace(trace: TraceRecord, renames: Renames): TraceRecord;
|
|
35
|
+
/** "`a` as `b`" for each rename, for a report or a note. */
|
|
36
|
+
export declare function describeRenames(renames: Renames): string;
|
|
37
|
+
/** The remembered aliases with these pairs added, newest first, at most `limit`. */
|
|
38
|
+
export declare function rememberAliases(aliases: Alias[], renames: Renames, limit?: number): Alias[];
|
|
39
|
+
/** The remembered aliases without these pairs, which the user declined. */
|
|
40
|
+
export declare function forgetAliases(aliases: Alias[], declined: Renames): Alias[];
|
|
@@ -0,0 +1,112 @@
|
|
|
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
|
+
/** Lower case without separators: NS_Green, nsGreen and ns-green are one name. */
|
|
18
|
+
export function nameKey(name) {
|
|
19
|
+
return name.toLowerCase().replace(/[^a-z0-9]/g, '');
|
|
20
|
+
}
|
|
21
|
+
/** Inputs pair with inputs and outputs with outputs; a side that does not know the role pairs with any. */
|
|
22
|
+
export function sameKind(a, b) {
|
|
23
|
+
const kind = (role) => (role === 'input' ? 'input' : role && 'other');
|
|
24
|
+
return !a.role || !b.role || kind(a.role) === kind(b.role);
|
|
25
|
+
}
|
|
26
|
+
/** The names on each side that the other side does not have. */
|
|
27
|
+
export function unmatched(from, to) {
|
|
28
|
+
const fromNames = new Set(from.map(({ name }) => name));
|
|
29
|
+
const toNames = new Set(to.map(({ name }) => name));
|
|
30
|
+
return {
|
|
31
|
+
from: from.filter(({ name }) => !toNames.has(name)),
|
|
32
|
+
to: to.filter(({ name }) => !fromNames.has(name)),
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
/** Whether any name left over on one side could stand for one left over on the other. */
|
|
36
|
+
export function couldMatch(from, to) {
|
|
37
|
+
const left = unmatched(from, to);
|
|
38
|
+
return left.from.some((a) => left.to.some((b) => sameKind(a, b)));
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Pairs the names `from` has and `to` lacks with the names `to` has and `from` lacks: first the
|
|
42
|
+
* pairs in `aliases`, then names that are spelled the same apart from case and separators. A
|
|
43
|
+
* name pairs only when exactly one name on the other side fits it.
|
|
44
|
+
*/
|
|
45
|
+
export function suggestRenames(from, to, aliases = []) {
|
|
46
|
+
const left = unmatched(from, to);
|
|
47
|
+
const suggestions = [];
|
|
48
|
+
const taken = new Set();
|
|
49
|
+
const pair = (reason, fits) => {
|
|
50
|
+
left.from.forEach((a) => {
|
|
51
|
+
if (suggestions.some((entry) => entry.from === a.name))
|
|
52
|
+
return;
|
|
53
|
+
const candidates = left.to.filter((b) => !taken.has(b.name) && sameKind(a, b) && fits(a, b));
|
|
54
|
+
if (candidates.length !== 1)
|
|
55
|
+
return;
|
|
56
|
+
// The other way round must be unique too: two names that both fit one are a guess.
|
|
57
|
+
const rivals = left.from.filter((other) => !suggestions.some((entry) => entry.from === other.name) &&
|
|
58
|
+
sameKind(other, candidates[0]) &&
|
|
59
|
+
fits(other, candidates[0]));
|
|
60
|
+
if (rivals.length !== 1)
|
|
61
|
+
return;
|
|
62
|
+
taken.add(candidates[0].name);
|
|
63
|
+
suggestions.push({ from: a.name, to: candidates[0].name, reason });
|
|
64
|
+
});
|
|
65
|
+
};
|
|
66
|
+
const aliased = (a, b) => aliases.some(([x, y]) => (x === a.name && y === b.name) || (x === b.name && y === a.name));
|
|
67
|
+
pair('remembered', aliased);
|
|
68
|
+
pair('spelling', (a, b) => nameKey(a.name) === nameKey(b.name));
|
|
69
|
+
return suggestions;
|
|
70
|
+
}
|
|
71
|
+
/** The trace with its variables renamed; names not in `renames` stay. */
|
|
72
|
+
export function renameTrace(trace, renames) {
|
|
73
|
+
const pairs = Object.entries(renames).filter(([from, to]) => from !== to);
|
|
74
|
+
if (pairs.length === 0)
|
|
75
|
+
return trace;
|
|
76
|
+
const names = new Set(trace.variables.map(({ name }) => name));
|
|
77
|
+
const targets = new Set();
|
|
78
|
+
pairs.forEach(([from, to]) => {
|
|
79
|
+
if (!names.has(from))
|
|
80
|
+
throw new Error(`The trace has no variable ${from}.`);
|
|
81
|
+
if (targets.has(to))
|
|
82
|
+
throw new Error(`Two variables cannot both become ${to}.`);
|
|
83
|
+
// Into a name that is itself renamed away (a swap) is fine; one that stays (no rename, or to itself) is taken.
|
|
84
|
+
const away = Object.hasOwn(renames, to) && renames[to] !== to;
|
|
85
|
+
if (names.has(to) && !away)
|
|
86
|
+
throw new Error(`The trace has a variable ${to} already.`);
|
|
87
|
+
targets.add(to);
|
|
88
|
+
});
|
|
89
|
+
const rename = (name) => (Object.hasOwn(renames, name) ? renames[name] : name);
|
|
90
|
+
return {
|
|
91
|
+
...trace,
|
|
92
|
+
variables: trace.variables.map((variable) => ({ ...variable, name: rename(variable.name) })),
|
|
93
|
+
ticks: trace.ticks.map((tick) => Object.fromEntries(Object.entries(tick).map(([name, value]) => [rename(name), value]))),
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
/** "`a` as `b`" for each rename, for a report or a note. */
|
|
97
|
+
export function describeRenames(renames) {
|
|
98
|
+
return Object.entries(renames)
|
|
99
|
+
.map(([from, to]) => `\`${from}\` as \`${to}\``)
|
|
100
|
+
.join(', ');
|
|
101
|
+
}
|
|
102
|
+
/** The remembered aliases with these pairs added, newest first, at most `limit`. */
|
|
103
|
+
export function rememberAliases(aliases, renames, limit = 200) {
|
|
104
|
+
const added = Object.entries(renames).filter(([from, to]) => from !== to);
|
|
105
|
+
const same = (a, b) => (a[0] === b[0] && a[1] === b[1]) || (a[0] === b[1] && a[1] === b[0]);
|
|
106
|
+
return [...added, ...aliases.filter((alias) => !added.some((entry) => same(entry, alias)))].slice(0, limit);
|
|
107
|
+
}
|
|
108
|
+
/** The remembered aliases without these pairs, which the user declined. */
|
|
109
|
+
export function forgetAliases(aliases, declined) {
|
|
110
|
+
return aliases.filter(([a, b]) => !Object.entries(declined).some(([from, to]) => (from === a && to === b) || (from === b && to === a)));
|
|
111
|
+
}
|
|
112
|
+
//# sourceMappingURL=trace-rename.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"trace-rename.js","sourceRoot":"","sources":["../src/trace-rename.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AAyBH,kFAAkF;AAClF,MAAM,UAAU,OAAO,CAAC,IAAY;IAChC,OAAO,IAAI,CAAC,WAAW,E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C,GAAG,KAAK,EAAE,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,IAAI,CAAC,KAAK,EAAE,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,EAAE,KAAK,CAAC,CAAA;AAC/G,CAAC;AAED,2EAA2E;AAC3E,MAAM,UAAU,aAAa,CAAC,OAAgB,EAAE,QAAiB;IAC7D,OAAO,OAAO,CAAC,MAAM,CACjB,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,MAAM,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC,IAAI,KAAK,CAAC,IAAI,EAAE,KAAK,CAAC,CAAC,IAAI,CAAC,IAAI,KAAK,CAAC,IAAI,EAAE,KAAK,CAAC,CAAC,CAAC,CACnH,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 type { TraceRecord, TraceRole, TraceVariable } from './trace-record'\n\n// Matching variables that two models, or a model and a file, name differently: NS_Green in one\n// is nsGreen in the other, or an input was renamed since the sequence was saved.\n\n/** Names of one side to the names they stand for on the other. */\nexport type Renames = Record<string, string>\n\n/** Two names the user said are one variable, remembered for later matches. */\nexport type Alias = [string, string]\n\nexport interface NamedVariable {\n name: string\n role?: TraceRole\n}\n\nexport interface RenameSuggestion {\n from: string\n to: string\n /** The user matched these two before, or they differ only in case and separators. */\n reason: 'remembered' | 'spelling'\n}\n\n/** Lower case without separators: NS_Green, nsGreen and ns-green are one name. */\nexport function nameKey(name: string): string {\n return name.toLowerCase().replace(/[^a-z0-9]/g, '')\n}\n\n/** Inputs pair with inputs and outputs with outputs; a side that does not know the role pairs with any. */\nexport function sameKind(a: NamedVariable, b: NamedVariable): boolean {\n const kind = (role?: TraceRole) => (role === 'input' ? 'input' : role && 'other')\n return !a.role || !b.role || kind(a.role) === kind(b.role)\n}\n\n/** The names on each side that the other side does not have. */\nexport function unmatched(from: NamedVariable[], to: NamedVariable[]): { from: NamedVariable[]; to: NamedVariable[] } {\n const fromNames = new Set(from.map(({ name }) => name))\n const toNames = new Set(to.map(({ name }) => name))\n return {\n from: from.filter(({ name }) => !toNames.has(name)),\n to: to.filter(({ name }) => !fromNames.has(name)),\n }\n}\n\n/** Whether any name left over on one side could stand for one left over on the other. */\nexport function couldMatch(from: NamedVariable[], to: NamedVariable[]): boolean {\n const left = unmatched(from, to)\n return left.from.some((a) => left.to.some((b) => sameKind(a, b)))\n}\n\n/**\n * Pairs the names `from` has and `to` lacks with the names `to` has and `from` lacks: first the\n * pairs in `aliases`, then names that are spelled the same apart from case and separators. A\n * name pairs only when exactly one name on the other side fits it.\n */\nexport function suggestRenames(from: NamedVariable[], to: NamedVariable[], aliases: Alias[] = []): RenameSuggestion[] {\n const left = unmatched(from, to)\n const suggestions: RenameSuggestion[] = []\n const taken = new Set<string>()\n const pair = (reason: RenameSuggestion['reason'], fits: (a: NamedVariable, b: NamedVariable) => boolean) => {\n left.from.forEach((a) => {\n if (suggestions.some((entry) => entry.from === a.name)) return\n const candidates = left.to.filter((b) => !taken.has(b.name) && sameKind(a, b) && fits(a, b))\n if (candidates.length !== 1) return\n // The other way round must be unique too: two names that both fit one are a guess.\n const rivals = left.from.filter(\n (other) =>\n !suggestions.some((entry) => entry.from === other.name) &&\n sameKind(other, candidates[0]) &&\n fits(other, candidates[0])\n )\n if (rivals.length !== 1) return\n taken.add(candidates[0].name)\n suggestions.push({ from: a.name, to: candidates[0].name, reason })\n })\n }\n const aliased = (a: NamedVariable, b: NamedVariable) =>\n aliases.some(([x, y]) => (x === a.name && y === b.name) || (x === b.name && y === a.name))\n pair('remembered', aliased)\n pair('spelling', (a, b) => nameKey(a.name) === nameKey(b.name))\n return suggestions\n}\n\n/** The trace with its variables renamed; names not in `renames` stay. */\nexport function renameTrace(trace: TraceRecord, renames: Renames): TraceRecord {\n const pairs = Object.entries(renames).filter(([from, to]) => from !== to)\n if (pairs.length === 0) return trace\n const names = new Set(trace.variables.map(({ name }) => name))\n const targets = new Set<string>()\n pairs.forEach(([from, to]) => {\n if (!names.has(from)) throw new Error(`The trace has no variable ${from}.`)\n if (targets.has(to)) throw new Error(`Two variables cannot both become ${to}.`)\n // Into a name that is itself renamed away (a swap) is fine; one that stays (no rename, or to itself) is taken.\n const away = Object.hasOwn(renames, to) && renames[to] !== to\n if (names.has(to) && !away) throw new Error(`The trace has a variable ${to} already.`)\n targets.add(to)\n })\n const rename = (name: string) => (Object.hasOwn(renames, name) ? renames[name] : name)\n return {\n ...trace,\n variables: trace.variables.map((variable): TraceVariable => ({ ...variable, name: rename(variable.name) })),\n ticks: trace.ticks.map((tick) =>\n Object.fromEntries(Object.entries(tick).map(([name, value]) => [rename(name), value]))\n ),\n }\n}\n\n/** \"`a` as `b`\" for each rename, for a report or a note. */\nexport function describeRenames(renames: Renames): string {\n return Object.entries(renames)\n .map(([from, to]) => `\\`${from}\\` as \\`${to}\\``)\n .join(', ')\n}\n\n/** The remembered aliases with these pairs added, newest first, at most `limit`. */\nexport function rememberAliases(aliases: Alias[], renames: Renames, limit = 200): Alias[] {\n const added = Object.entries(renames).filter(([from, to]) => from !== to) as Alias[]\n const same = (a: Alias, b: Alias) => (a[0] === b[0] && a[1] === b[1]) || (a[0] === b[1] && a[1] === b[0])\n return [...added, ...aliases.filter((alias) => !added.some((entry) => same(entry, alias)))].slice(0, limit)\n}\n\n/** The remembered aliases without these pairs, which the user declined. */\nexport function forgetAliases(aliases: Alias[], declined: Renames): Alias[] {\n return aliases.filter(\n ([a, b]) => !Object.entries(declined).some(([from, to]) => (from === a && to === b) || (from === b && to === a))\n )\n}\n"]}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import type { ReplayState } from "./protocol.js";
|
|
2
|
+
import type { TraceRecord } from "./trace-record.js";
|
|
3
|
+
/** A variable of the running model, as far as replaying is concerned. */
|
|
4
|
+
export interface ReplayTarget {
|
|
5
|
+
id: string;
|
|
6
|
+
input: boolean;
|
|
7
|
+
output: boolean;
|
|
8
|
+
type?: string;
|
|
9
|
+
}
|
|
10
|
+
export interface ReplayMismatch {
|
|
11
|
+
tick: number;
|
|
12
|
+
name: string;
|
|
13
|
+
expected: unknown;
|
|
14
|
+
actual: unknown;
|
|
15
|
+
}
|
|
16
|
+
/** A file value in the model's terms: strings for string inputs, 0 and 1 for booleans. */
|
|
17
|
+
export declare function coerceInput(value: unknown, type: string | undefined): unknown;
|
|
18
|
+
/**
|
|
19
|
+
* The file's inputs are matched to the model's by name. Inputs the model does not have are
|
|
20
|
+
* ignored, and the model's other inputs stay under the user's control. The file's first row is
|
|
21
|
+
* the tick after `offset`, and the tick number alone decides the values, so stepping back and
|
|
22
|
+
* running again gives the same run. Ticks below are the simulation's, rows the file's.
|
|
23
|
+
*/
|
|
24
|
+
export declare class TraceReplay {
|
|
25
|
+
readonly label: string;
|
|
26
|
+
readonly trace: TraceRecord;
|
|
27
|
+
/** False when another model's outputs are not expected to match, as in a batch comparison. */
|
|
28
|
+
readonly checkOutputs: boolean;
|
|
29
|
+
/** The model the replay belongs to; a restart of the same model keeps replaying. */
|
|
30
|
+
readonly modelUri?: string | undefined;
|
|
31
|
+
/** The simulation's tick before the file's first row: the tick at which replaying began. */
|
|
32
|
+
readonly offset: number;
|
|
33
|
+
/** Inputs the file sets and the model reads. */
|
|
34
|
+
readonly driven: string[];
|
|
35
|
+
/** Inputs in the file that the model does not have. */
|
|
36
|
+
readonly ignored: string[];
|
|
37
|
+
/** Inputs of the model that the file does not set. */
|
|
38
|
+
readonly unset: string[];
|
|
39
|
+
/** Outputs in both, checked after every tick the file covers. */
|
|
40
|
+
readonly checked: string[];
|
|
41
|
+
private readonly problems;
|
|
42
|
+
private readonly mismatches;
|
|
43
|
+
private readonly targets;
|
|
44
|
+
constructor(label: string, trace: TraceRecord, targets: ReplayTarget[],
|
|
45
|
+
/** False when another model's outputs are not expected to match, as in a batch comparison. */
|
|
46
|
+
checkOutputs?: boolean,
|
|
47
|
+
/** The model the replay belongs to; a restart of the same model keeps replaying. */
|
|
48
|
+
modelUri?: string | undefined,
|
|
49
|
+
/** The simulation's tick before the file's first row: the tick at which replaying began. */
|
|
50
|
+
offset?: number);
|
|
51
|
+
get length(): number;
|
|
52
|
+
/** The simulation's tick at which the file ends. */
|
|
53
|
+
get end(): number;
|
|
54
|
+
/** The file's row for a tick of the simulation, if the file covers that tick. */
|
|
55
|
+
private row;
|
|
56
|
+
/** The same file and settings against a fresh run of a model, e.g. after a restart, from tick 1. */
|
|
57
|
+
rebind(targets: ReplayTarget[], modelUri?: string): TraceReplay;
|
|
58
|
+
/**
|
|
59
|
+
* The values the model reads at `tick` (1-based), in its types. `current` gives an input's
|
|
60
|
+
* present value, which a file value must fit. Empty outside the file.
|
|
61
|
+
*/
|
|
62
|
+
inputsFor(tick: number, current: (id: string) => unknown): Map<string, unknown>;
|
|
63
|
+
/** Compares the model's outputs after `tick` with the file's. */
|
|
64
|
+
check(tick: number, actual: (id: string) => unknown): ReplayMismatch[];
|
|
65
|
+
/** Forgets what was checked after `tick`, when the simulation went back to it. */
|
|
66
|
+
rewindTo(tick: number): void;
|
|
67
|
+
/** The file asks to stop running after this tick (KTrace `pause`). */
|
|
68
|
+
pausesAfter(tick: number): boolean;
|
|
69
|
+
/** Where the file's value differs from the model's, for one variable. */
|
|
70
|
+
mismatchesOf(name: string): ReplayMismatch[];
|
|
71
|
+
state(tick: number): ReplayState;
|
|
72
|
+
}
|