-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
505 lines (417 loc) · 11.7 KB
/
index.js
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
//@ts-check
/**
* Utility function
*
* @param {Array} arrA
* @param {Array} arrB
* @returns {Object}
*/
function zipObject(arrA, arrB) {
const zip = (arrX, arrY) => arrX.map((x, i) => [x, arrY[i]]);
return Object.fromEntries(zip(arrA, arrB));
}
/**
* A basic Promise-based and queue-based Semaphore
*/
const Semaphore = class SillySemaphore {
constructor(permits) {
this._permits = permits;
this._queue = [];
}
getPosition(acquirer) {
const idx = this._queue.findIndex(entry => entry.acquirer === acquirer);
return idx + 1; // to get rid of '-1'
}
async acquire(acquirer) {
return new Promise( (resolve, reject) => {
this._queue.push({
acquirer,
resolve,
reject
});
this._maybeNotify();
});
}
async release(_acquirer) {
this._permits++;
this._maybeNotify();
}
/**
* Reject all pending promises and nullify the 'queue'
* so that future calls fail...
*/
die() {
let entry;
while (entry = this._queue.pop()) {
entry.reject();
}
this._queue = null;
}
_maybeNotify() {
if (this._permits > 0) {
const entry = this._queue.shift();
if (entry) {
this._permits--;
entry.resolve();
}
}
}
}
class Sim {
static userVars = {};
static userAlgos = {};
static ctrl = null;
static lane1 = [];
static lane2 = [];
static crossing = 0;
static ui = {};
static creatorInterval = null;
static start() {
// if (Sim.ui.$sim === undefined) Sim.resetUI();
Sim.clearErrors();
Sim.loadUserInputs();
Sim.ui.$fieldset.disabled = true;
Sim.ctrl = new Controller();
Sim.ctrl.run();
Sim.initCreator();
Sim.redraw();
}
static stop() {
function freezeSimUI() {
// To replace all elements so no one can alter the current state
Sim.ui.$sim.outerHTML = Sim.ui.$sim.outerHTML;
// To signal that they no longer reference actual DOM elements and free them maybe(?)
Sim.ui = {};
}
function freeVariables() {
// Kill all user defined semaphores
Object.entries(Sim.userVars)
.map(entry => entry[1])
.filter(val => val instanceof Semaphore)
.forEach(sema => sema.die());
Sim.userVars = {};
}
function freeThreads() {
// TODO:
// - kill "threads" (maybe simply call .destory() of each existing Traverser)
// - cancel Controller 'ctrl' (AFAIK you can not cancel Promises...)
}
Sim.ui.$fieldset.disabled = false;
clearInterval(Sim.creatorInterval);
freezeSimUI();
//freeThreads();
//freeVariables();
}
static setup() {
const $ = str => document.querySelector(str);
Object.assign(Sim.ui, {
$form: $('form'),
$fieldset: $('form fieldset'),
$semaNames: $('#sema-names'),
$semaVals: $('#sema-vals'),
$intNames: $('#int-names'),
$intVals: $('#int-vals'),
$controller: $('#controller'),
$traverser1: $('#traverser1'),
$traverser2: $('#traverser2'),
$sim: $('#sim'),
$intersection: $('#intersection'),
})
Sim.ui.$form.onsubmit = (ev) => {
ev.preventDefault();
Sim.start();
}
$('#btn-load-attempt').onclick = (ev) => {
ev.preventDefault();
Sim.loadPreset(Sim.presets.myAttempt);
}
$('#btn-load-correct').onclick = (ev) => {
ev.preventDefault();
Sim.loadPreset(Sim.presets.correctAnswer);
}
}
static loadUserInputs() {
const {ui} = Sim;
// userAlgos...
Object.assign(Sim.userAlgos,
{
controller: ui.$controller.value,
traverser1: ui.$traverser1.value,
traverser2: ui.$traverser2.value,
}
)
// userVars...
const SEP = /,\s*/; // separator
const userSemas = zipObject(
ui.$semaNames.value.split(SEP),
ui.$semaVals.value.split(SEP).map(val => new Semaphore(Number(val)))
);
const userInts = zipObject(
ui.$intNames.value.split(SEP),
ui.$intVals.value.split(SEP).map(val => Number(val))
);
Object.assign(Sim.userVars, userSemas, userInts);
}
static initCreator() {
// maybe create a new instance of Traverser every second
Sim.creatorInterval = setInterval(Sim.maybeCreateTraverser, 1 * 1000);
}
/**
* Maybe create a new instance of Traverser and run it
* @returns {Traverser} The newly created traverser or null.
*/
static maybeCreateTraverser() {
// maybe not
if (Math.random() < 0.5) {
return null;
}
if (Math.random() < 0.5) {
if (Sim.lane1.length < Traverser1.MAX) {
const t1 = new Traverser1();
Sim.lane1.push(t1);
t1.run();
return t1;
}
} else {
if (Sim.lane2.length < Traverser2.MAX) {
const t2 = new Traverser2();
Sim.lane2.push(t2);
t2.run();
return t2;
}
}
// no room for a new 'Traverser' in the randomly chosen 'lane'
return null;
}
static redraw() {
// Just update 'data-*' and '--pos' values, and let CSS take care of the rest.
// Traffic light
const {ui, ctrl, userVars} = Sim;
ui.$sim.dataset.light = userVars.light;
ui.$sim.dataset.ctrlQueued = ctrl.orderVec.some(sema => userVars[sema].getPosition(ctrl) > 0);
// Lanes
const {lane1, lane2, redrawTraverser} = Sim;
lane1.sort(Traverser.compareTraversers);
lane2.sort(Traverser.compareTraversers);
lane1.forEach(redrawTraverser);
lane2.forEach(redrawTraverser);
// FIXME: Maybe it's better to use Proxy(userVars) and redraw after its attributes are accessed..
// Redraw before each repaint
requestAnimationFrame(Sim.redraw);
}
/**
* Update Traverser
*
* @param {Traverser} t
* @param {number} i - index
*/
static redrawTraverser(t, i) {
t.$elem.style.setProperty('--pos', i);
t.$elem.title =
`${t.name}\n\n` +
`orderVec: {${t.orderVec.join(', ')}}\n` +
`waitVec: {${t.getWaitVec().join(', ')}}`;
}
static showError(algoSource, message) {
Sim.ui[ '$' + algoSource ].setAttribute('title', message);
Sim.stop();
}
static clearErrors() {
Sim.ui.$form.querySelectorAll('[title]').forEach($x => $x.removeAttribute('title'));
}
/**
* Populate UI inputs with preset data
*
* @param {object} preset
*/
static loadPreset(preset) {
const keys = 'semaNames semaVals intNames intVals controller traverser1 traverser2'.split(' ');
for (const key of keys) {
Sim.ui['$' + key].value = preset[key];
}
}
}
class Algorithm {
/**
* @param {String} algoSource - "controller", "traverser1", or "traverser2"
*/
constructor(algoSource) {
this.algoSource = algoSource;
this.userAlgo = Sim.userAlgos[algoSource];
this.orderVec = Algorithm.parseOrderVector(this.userAlgo);
}
async run() {
const AsyncFunction = Object.getPrototypeOf(async function(){}).constructor;
try {
const asyncFunc = new AsyncFunction(`
with (Sim.userVars) {
${ Algorithm.awaitifyThis(this.userAlgo) }
}
`);
await asyncFunc.call(this);
} catch (userError) {
console.error('userError', userError); // for actual debugging
Sim.showError(this.algoSource, userError.message);
}
}
/**
* Replace function calls with `await`ed method invocations associated with `this` object.
*
* @param {string} code
* @returns {string} Updated code
*/
static awaitifyThis(code) {
// Prefix "p", "v", "sleep", and "traverse" calls with `await` and attach them to `this`
return code.replace(/\b(p|v|sleep|traverse)\(/g, 'await this.$1(');
}
async p(x) {
await this.sleep(0);
await x.acquire(this);
}
async v(x) {
await this.sleep(0);
await x.release(this);
}
async sleep(secs) {
return new Promise((resolve, _reject) => {
setTimeout(resolve, secs * 1000);
});
}
/**
* A vehicle's place in line is determined by a vector of priorities
* which are based on calls to p(...)
*
* @todo Should dedup before returning?
*
* @param {string} code
* @return {Array<string>}
*/
static parseOrderVector(code) {
const pCalls = code.match(/\bp\(\s*(\w+)\s*\)/g) || [];
const acquiredSemaphores = pCalls.map(x => x.slice(2, -1).trim());
return acquiredSemaphores;
}
}
class Controller extends Algorithm {
constructor() {
super('controller');
}
}
class Traverser extends Algorithm {
static counter = 0;
static freeColors = 'blue coral darkkhaki firebrick yellowgreen gray skyblue teal orange pink purple yellow'.split(' ');
constructor(algoSource) {
super(algoSource);
this.id = this.getUniqueId();
this.color = this.getUniqueColor();
this.type = Math.random() < 0.25 ? 'truck' : 'car'; // 25% chance of being a truck
this.name = `${this.color} ${this.type} #${this.id}`;
this.$elem = null;
// FIXME: These attributes are set by children
this.lane = null;
this.dir = this.algoSource === 'traverser1' ? 'south' : 'west';
this.initialPos = 'traverser1' ? '4' : '7'; // the very end of the line
}
/**
* Create a new visual element and display it on page
*/
initElem() {
this.$elem = document.createElement('span');
this.$elem.classList.add('vehicle', this.type, this.dir);
this.$elem.title = this.name; // (will be updated)
this.$elem.style.setProperty('--pos', this.initialPos); // (will be updated)
this.$elem.style.setProperty('--color', this.color);
Sim.ui.$intersection.append(this.$elem);
return this.$elem; // always return something
}
destroyElem() {
Traverser.freeColors.push(this.color);
this.$elem.remove();
}
destroy() {
this.destroyElem();
this.lane.splice(this.lane.findIndex(t => t === this), 1);
}
async traverse() {
// "Restarting the engine takes some time" za3ma
// Adds an element of "randomness"
await this.sleep(Math.random());
// Cross the intersection then "keep moving" and fade away...
await this.enterIntersection();
await this.leaveIntersection();
}
async enterIntersection() {
Sim.crossing++;
this.$elem.dataset.state = 'leaving';
this.assertNoCollision();
await this.sleep(0.5);
}
async leaveIntersection() {
await this.sleep(1);
this.assertNoCollision();
this.destroy();
Sim.crossing--;
}
assertNoCollision() {
const happened = Sim.crossing > 1; // more than one vehicle crossing the intersection
if (happened) {
Sim.ui.$sim.dataset.state = 'error';
throw new Error('Collision!');
}
}
/**
* @returns {Array<number>}
*/
getWaitVec() {
const vec = this.orderVec.map(semaName => Sim.userVars[semaName].getPosition(this));
return vec;
}
getUniqueId() {
// FIXME: Should throw error when about to overflow? Althrough this sim won't run for a long time for this to happen
return (++Traverser.counter).toString(36).toUpperCase();
}
getUniqueColor() {
return Traverser.freeColors.shift();
}
/**
*
* @param {Traverser} a
* @param {Traverser} b
* @returns {number}
*/
static compareTraversers(a, b) {
return Traverser.compareVecs( a.getWaitVec(), b.getWaitVec() );
}
/**
* Sort wait vecs in an ascending order
*
* @param {Array<number>} vecA
* @param {Array<number>} vecB
* @returns {number}
*/
static compareVecs(vecA, vecB) {
for (let i = 0; i < vecA.length; i++) {
if (vecA[i] - vecB[i] !== 0) {
return vecA[i] - vecB[i];
}
}
return 0;
}
}
class Traverser1 extends Traverser {
static MAX = 4;
constructor() {
super('traverser1');
this.initElem();
this.lane = Sim.lane1;
}
}
class Traverser2 extends Traverser {
static MAX = 7;
constructor() {
super('traverser2');
this.initElem();
this.lane = Sim.lane2;
}
}
Sim.setup();