-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathcheck.ts
561 lines (499 loc) · 15 KB
/
check.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
import { Range, Position } from 'vscode';
import * as path from 'path';
import { DCollection } from '../diagnostic';
import { isNumber } from 'util';
import { Moment } from 'moment';
export enum CheckState {
Running = 'R',
Success = 'S',
Error = 'E',
Stopped = 'X'
}
export enum CheckStatus {
NotStarted,
Starting,
SanyParsing,
SanyFinished,
InitialStatesComputing,
SuccessorStatesComputing,
Checkpointing,
CheckingLiveness,
CheckingLivenessFinal,
ServerRunning,
WorkersRegistered,
Finished
}
const STATUS_NAMES = new Map<CheckStatus, string>();
STATUS_NAMES.set(CheckStatus.NotStarted, 'Not started');
STATUS_NAMES.set(CheckStatus.Starting, 'Starting');
STATUS_NAMES.set(CheckStatus.SanyParsing, 'SANY parsing');
STATUS_NAMES.set(CheckStatus.SanyFinished, 'SANY finished');
STATUS_NAMES.set(CheckStatus.InitialStatesComputing, 'Computing initial states');
STATUS_NAMES.set(CheckStatus.SuccessorStatesComputing, 'Computing reachable states');
STATUS_NAMES.set(CheckStatus.Checkpointing, 'Checkpointing');
STATUS_NAMES.set(CheckStatus.CheckingLiveness, 'Checking liveness');
STATUS_NAMES.set(CheckStatus.CheckingLivenessFinal, 'Checking final liveness');
STATUS_NAMES.set(CheckStatus.ServerRunning, 'Master waiting for workers');
STATUS_NAMES.set(CheckStatus.WorkersRegistered, 'Workers connected');
STATUS_NAMES.set(CheckStatus.Finished, 'Finished');
const STATE_NAMES = new Map<CheckState, string>();
STATE_NAMES.set(CheckState.Running, 'Running');
STATE_NAMES.set(CheckState.Success, 'Success');
STATE_NAMES.set(CheckState.Error, 'Errors');
STATE_NAMES.set(CheckState.Stopped, 'Stopped');
const VALUE_FORMAT_LENGTH_THRESHOLD = 30;
/**
* Statistics on initial state generation.
*/
export class InitialStateStatItem {
constructor(
readonly timeStamp: string,
readonly diameter: number,
readonly total: number,
readonly distinct: number,
readonly queueSize: number
) {}
}
/**
* Statistics on coverage.
*/
export class CoverageItem {
constructor(
readonly module: string,
readonly action: string,
readonly filePath: string | undefined,
readonly range: Range,
readonly total: number,
readonly distinct: number
) {}
}
enum MessageSpanType {
Text = 'T',
SourceLink = 'SL'
}
export class MessageSpan {
private constructor(
readonly type: MessageSpanType,
readonly text: string,
readonly filePath?: string | undefined,
readonly location?: Position | undefined
) {}
static newTextSpan(text: string): MessageSpan {
return new MessageSpan(MessageSpanType.Text, text);
}
static newSourceLinkSpan(text: string, filePath: string, location: Position): MessageSpan {
return new MessageSpan(MessageSpanType.SourceLink, text, filePath, location);
}
}
/**
* Represents an error or warning line of a message.
*/
export class MessageLine {
constructor(
readonly spans: ReadonlyArray<MessageSpan>
) {}
static fromText(text: string): MessageLine {
return new MessageLine([ MessageSpan.newTextSpan(text) ]);
}
toString(): string {
return this.spans.map((s) => s.text).join('');
}
}
export type ValueKey = string | number;
/**
* Type of value change between two consecutive states.
*/
export enum Change {
NOT_CHANGED = 'N',
ADDED = 'A',
MODIFIED = 'M',
DELETED = 'D'
}
/**
* Base class for values.
*/
export class Value {
static idCounter = 0;
static idStep = 1;
changeType = Change.NOT_CHANGED;
readonly id: number;
constructor(
readonly key: ValueKey,
readonly str: string
) {
Value.idCounter += Value.idStep;
this.id = Value.idCounter;
}
/**
* Switches off ID incrementation. For tests only.
*/
static switchIdsOff(): void {
Value.idStep = 0;
}
/**
* Switches on ID incrementation. For tests only.
*/
static switchIdsOn(): void {
Value.idStep = 1;
}
setModified(): Value {
this.changeType = Change.MODIFIED;
return this;
}
setAdded(): Value {
this.changeType = Change.ADDED;
return this;
}
setDeleted(): Value {
this.changeType = Change.MODIFIED;
return this;
}
/**
* Adds formatted representation of the value to the given array of strings.
*/
format(indent: string): string {
return `${this.str}`;
}
}
/**
* A value that is represented by some variable name.
*/
export class NameValue extends Value {
constructor(key: ValueKey, name: string) {
super(key, name);
}
}
/**
* Value that is a collection of other values.
*/
export abstract class CollectionValue extends Value {
readonly expandSingle = true;
deletedItems: Value[] | undefined;
constructor(
key: ValueKey,
readonly items: Value[],
readonly prefix: string,
readonly postfix: string,
readonly delim = ', ',
toStr?: (v: Value) => string
) {
super(key, makeCollectionValueString(items, prefix, postfix, delim, toStr || (v => v.str)));
}
addDeletedItems(items: Value[]): void {
if (!items || items.length === 0) {
return;
}
if (!this.deletedItems) {
this.deletedItems = [];
}
const delItems = this.deletedItems;
items.forEach(delItem => {
const newValue = new Value(delItem.key, delItem.str); // No need in deep copy here
newValue.changeType = Change.DELETED;
delItems.push(newValue);
});
}
findItem(id: number): Value | undefined {
for (const item of this.items) {
if (item.changeType === Change.DELETED) {
continue;
}
if (item.id === id) {
return item;
}
if (item instanceof CollectionValue) {
const subItem = item.findItem(id);
if (subItem) {
return subItem;
}
}
}
return undefined;
}
format(indent: string): string {
if (this.items.length === 0) {
return `${this.prefix}${this.postfix}`;
}
if (this.str.length <= VALUE_FORMAT_LENGTH_THRESHOLD) {
return this.str;
}
const subIndent = indent + ' ';
const fmtFunc = (v: Value) => this.formatKey(subIndent, v) + '' + v.format(subIndent);
const body = makeCollectionValueString(this.items, '', '', this.delim + '\n', fmtFunc);
return `${this.prefix}\n${body}\n${indent}${this.postfix}`;
}
formatKey(indent: string, value: Value): string {
return `${indent}${value.key}: `;
}
}
/**
* Represents a set: {1, "b", <<TRUE, 5>>}, {}, etc.
*/
export class SetValue extends CollectionValue {
constructor(key: ValueKey, items: Value[]) {
super(key, items, '{', '}');
}
setModified(): SetValue {
super.setModified();
return this;
}
formatKey(indent: string, _: Value): string {
return indent;
}
}
/**
* Represents a sequence/tuple: <<1, "b", TRUE>>, <<>>, etc.
*/
export class SequenceValue extends CollectionValue {
constructor(key: ValueKey, items: Value[]) {
super(key, items, '<<', '>>');
}
formatKey(indent: string, _: Value): string {
return indent;
}
}
/**
* Represents a structure: [a |-> 'A', b |-> 34, c |-> <<TRUE, 2>>], [], etc.
*/
export class StructureValue extends CollectionValue {
constructor(
key: ValueKey,
items: Value[],
prefix = '[',
postfix = ']',
delim = ', ',
readonly itemSep = ' |-> ',
toStr = StructureValue.itemToString,
preserveOrder = false) {
super(key, items, prefix, postfix, delim, toStr);
if (!preserveOrder) {
items.sort(StructureValue.compareItems);
}
}
static itemToString(item: Value): string {
return `${item.key} |-> ${item.str}`;
}
static funcItemToString(item: Value): string {
return `${item.key} :> ${item.str}`;
}
static compareItems(a: Value, b: Value): number {
if (a.key < b.key) {
return -1;
} else if (a.key > b.key) {
return 1;
}
return 0;
}
setModified(): StructureValue {
super.setModified();
return this;
}
formatKey(indent: string, value: Value): string {
return `${indent}${value.key}` + this.itemSep;
}
}
/**
* A state of a process in a particular moment of time.
*/
export class ErrorTraceItem {
constructor(
readonly num: number,
readonly title: string,
readonly module: string,
readonly action: string,
readonly filePath: string | undefined,
readonly range: Range,
readonly variables: StructureValue // Variables are presented as items of a structure
) {}
}
/**
* An output line produced by Print/PrintT along with the number of consecutive occurrences.
*/
export class OutputLine {
count = 1;
constructor(readonly text: string) {
}
increment(): void {
this.count += 1;
}
}
/**
* A warning, issued by TLC.
*/
export class WarningInfo {
constructor(
readonly lines: MessageLine[]
) {}
}
/**
* An error, issued by TLC.
*/
export class ErrorInfo {
constructor(
public lines: MessageLine[],
readonly errorTrace: ErrorTraceItem[]
) {}
}
export enum ModelCheckResultSource {
Process, // The result comes from an ongoing TLC process
OutFile // The result comes from a .out file
}
export class SpecFiles {
readonly tlaFileName: string;
readonly cfgFileName: string;
constructor(
readonly tlaFilePath: string,
readonly cfgFilePath: string
) {
this.tlaFileName = path.basename(tlaFilePath);
this.cfgFileName = path.basename(cfgFilePath);
}
}
/**
* Represents the state of a TLA model checking process.
*/
export class ModelCheckResult {
readonly stateName: string;
readonly startDateTimeStr: string | undefined;
readonly endDateTimeStr: string | undefined;
readonly durationStr: string | undefined;
readonly statusDetails: string | undefined;
constructor(
readonly source: ModelCheckResultSource,
readonly specFiles: SpecFiles | unknown,
readonly showFullOutput: boolean,
readonly state: CheckState,
readonly status: CheckStatus,
readonly processInfo: string | undefined,
readonly initialStatesStat: InitialStateStatItem[],
readonly coverageStat: CoverageItem[],
readonly warnings: WarningInfo[],
readonly errors: ErrorInfo[],
readonly sanyMessages: DCollection | undefined,
readonly startDateTime: Moment | undefined,
readonly endDateTime: Moment | undefined,
readonly duration: number | undefined,
readonly workersCount: number,
readonly collisionProbability: string | undefined,
readonly outputLines: OutputLine[],
) {
this.stateName = getStateName(this.state);
this.startDateTimeStr = dateTimeToStr(startDateTime);
this.endDateTimeStr = dateTimeToStr(endDateTime);
this.durationStr = durationToStr(duration);
let statusDetails;
switch (state) {
case CheckState.Running:
statusDetails = getStatusName(status);
break;
case CheckState.Success:
statusDetails = collisionProbability
? `Fingerprint collision probability: ${collisionProbability}`
: '';
break;
case CheckState.Error:
statusDetails = `${errors.length} error(s)`;
break;
}
this.statusDetails = statusDetails;
}
static createEmpty(source: ModelCheckResultSource): ModelCheckResult {
return new ModelCheckResult(
source, undefined, false, CheckState.Running, CheckStatus.Starting, undefined, [], [], [], [],
undefined, undefined, undefined, undefined, 0, undefined, []);
}
formatValue(valueId: number): string | undefined {
for (const err of this.errors) {
for (const items of err.errorTrace) {
const value = items.variables.findItem(valueId);
if (value) {
return value.format('');
}
}
}
return undefined;
}
}
function getStateName(state: CheckState): string {
const name = STATE_NAMES.get(state);
if (typeof name !== 'undefined') {
return name;
}
throw new Error(`Name not defined for check state ${state}`);
}
export function getStatusName(status: CheckStatus): string {
const name = STATUS_NAMES.get(status);
if (name) {
return name;
}
throw new Error(`Name not defined for check status ${status}`);
}
/**
* Recursively finds and marks all the changes between two collections.
*/
export function findChanges(prev: CollectionValue, state: CollectionValue): boolean {
let pi = 0;
let si = 0;
let modified = false;
const deletedItems = [];
while (pi < prev.items.length && si < state.items.length) {
const prevValue = prev.items[pi];
const stateValue = state.items[si];
if (prevValue.key > stateValue.key) {
stateValue.changeType = Change.ADDED;
modified = true;
si += 1;
} else if (prevValue.key < stateValue.key) {
deletedItems.push(prevValue);
pi += 1;
} else {
if (prevValue instanceof CollectionValue && stateValue instanceof CollectionValue) {
modified = findChanges(prevValue, stateValue) || modified;
} else if (prevValue.str !== stateValue.str) {
stateValue.changeType = Change.MODIFIED;
modified = true;
}
si += 1;
pi += 1;
}
}
for (; si < state.items.length; si++) {
state.items[si].changeType = Change.ADDED;
modified = true;
}
for (; pi < prev.items.length; pi++) {
deletedItems.push(prev.items[pi]);
}
state.addDeletedItems(deletedItems);
modified = modified || deletedItems.length > 0;
if (modified) {
state.changeType = Change.MODIFIED;
}
return modified;
}
function dateTimeToStr(dateTime: Moment | undefined): string {
if (!dateTime) {
return 'not yet';
}
return dateTime.format('HH:mm:ss (MMM D)');
}
function durationToStr(dur: number | undefined): string {
if (!isNumber(dur)) {
return '';
}
return `${dur} msec`;
}
function makeCollectionValueString(
items: Value[],
prefix: string,
postfix: string,
delimiter: string,
toStr: (v: Value) => string
) {
// TODO: trim to fit into 100 symbols
const valuesStr = items
.filter(i => i.changeType !== Change.DELETED)
.map(i => toStr(i))
.join(delimiter);
return prefix + valuesStr + postfix;
}