statecharts_core.js 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353
  1. /**
  2. * Statechart core runtime.
  3. */
  4. nextObjectId = 1;
  5. function objectId(obj) {
  6. if (obj == null) {
  7. return null;
  8. }
  9. if (obj.assignedObjectId == null) {
  10. obj.assignedObjectId = nextObjectId++;
  11. }
  12. return obj.assignedObjectId;
  13. }
  14. function time() {
  15. return (new Date).getTime();
  16. }
  17. function RuntimeException(msg) {
  18. this.msg = msg;
  19. }
  20. function InputException(msg) {
  21. RuntimeException.call(this, msg);
  22. }
  23. InputException.prototype = new RuntimeException();
  24. function AssociationException(msg) {
  25. RuntimeException.call(this, msg);
  26. }
  27. AssociationException.prototype = new RuntimeException();
  28. function AssociationReferenceException(msg) {
  29. RuntimeException.call(this, msg);
  30. }
  31. AssociationReferenceException.prototype = new RuntimeException();
  32. function ParameterException(msg) {
  33. RuntimeException.call(this, msg);
  34. }
  35. ParameterException.prototype = new RuntimeException();
  36. function EventQueueEntry(scheduledTime, theEvent, instances) {
  37. if (!(instances instanceof Array)) {
  38. throw new Error("Must be array")
  39. }
  40. this.scheduledTime = scheduledTime;
  41. this.theEvent = theEvent;
  42. this.instances = instances; // list of instances to receive the event
  43. }
  44. function EventQueue() {
  45. this.eventList = [];
  46. this.sortFunction = function (a, b) {
  47. return a.scheduledTime - b.scheduledTime
  48. }
  49. }
  50. EventQueue.prototype.isEmpty = function () {
  51. return this.eventList.length === 0;
  52. };
  53. EventQueue.prototype.getEarliestTime = function () {
  54. if (this.isEmpty()) {
  55. return Infinity;
  56. } else {
  57. return this.eventList[0].scheduledTime;
  58. }
  59. };
  60. EventQueue.prototype.add = function (theEvent) {
  61. this.eventList.push(theEvent);
  62. this.eventList.sort(this.sortFunction);
  63. return objectId(theEvent);
  64. };
  65. EventQueue.prototype.remove = function (eventId) {
  66. this.eventList = this.eventList.filter(function (el) {
  67. return objectId(el) !== eventId;
  68. }).sort()
  69. }
  70. EventQueue.prototype.pop = function () {
  71. return this.eventList.shift()
  72. };
  73. function Association(toClass, minCard, maxCard) {
  74. this.toClass = toClass;
  75. this.minCard = minCard;
  76. this.maxCard = maxCard;
  77. this.instances = {};
  78. this.size = 0;
  79. this.nextId = 0;
  80. }
  81. Association.prototype.allowedToAdd = function () {
  82. return (this.maxCard === -1 || this.size < this.maxCard);
  83. };
  84. Association.prototype.allowedToRemove = function () {
  85. return (this.minCard === -1 || this.size > this.minCard);
  86. };
  87. Association.prototype.addInstance = function (instance) {
  88. if (this.allowedToAdd()) {
  89. const id = this.nextId++;
  90. this.instances[id] = instance;
  91. return id;
  92. } else {
  93. throw new AssociationException('Not allowed to add the instance to the association.');
  94. }
  95. };
  96. Association.prototype.removeInstance = function (instance) {
  97. if (this.allowedToRemove()) {
  98. delete this.instances[this.instancesToIds[instance]]
  99. } else {
  100. throw new AssociationException('Not allowed to remove the instance to the association.');
  101. }
  102. };
  103. Association.prototype.getInstance = function (index) {
  104. const instance = this.instances[index];
  105. if (instance === undefined) {
  106. throw new AssociationException('Invalid index for fetching instance(s) from association.');
  107. }
  108. return instance;
  109. };
  110. function ObjectManagerBase(controller) {
  111. this.controller = controller;
  112. // this.events = new EventQueue();
  113. this.instances = [];
  114. // this.regex = /^([a-zA-Z_]\w*)(?:\[(\d+)])?$/;
  115. this.regex = /^([a-zA-Z_]\w*)(?:\[(\d+)\])?$/;
  116. }
  117. // ObjectManagerBase.prototype.addEvent = function (theEvent, timeOffset) {
  118. // if (timeOffset === undefined) timeOffset = 0;
  119. // this.events.add(new EventQueueEntry(this.controller.simulatedTime + timeOffset, theEvent));
  120. // };
  121. // ObjectManagerBase.prototype.broadcast = function (source, newEvent, timeOffset) {
  122. // if (timeOffset === undefined) timeOffset = 0;
  123. // for (let i in this.instances) {
  124. // if (!this.instances.hasOwnProperty(i) || i === source) continue;
  125. // this.instances[i].addEvent(newEvent, timeOffset);
  126. // }
  127. // };
  128. // ObjectManagerBase.prototype.getEarliestEventTime = function () {
  129. // let earliestTime = this.events.getEarliestTime();
  130. // for (let i in this.instances) {
  131. // if (!this.instances.hasOwnProperty(i)) continue;
  132. // earliestTime = Math.min(earliestTime, this.instances[i].earliestEventTime);
  133. // }
  134. // return earliestTime;
  135. // };
  136. // ObjectManagerBase.prototype.stepAll = function () {
  137. // this.step();
  138. // for (let i in this.instances) {
  139. // if (!this.instances.hasOwnProperty(i)) continue;
  140. // let instance = this.instances[i];
  141. // if (instance.active && (instance.getEarliestEventTime() <= this.controller.simulatedTime || instance.eventlessTransitions())) {
  142. // instance.step();
  143. // }
  144. // }
  145. // };
  146. // ObjectManagerBase.prototype.step = function () {
  147. // while (this.events.getEarliestTime() <= this.controller.simulatedTime) {
  148. // this.handleEvent(this.events.pop());
  149. // }
  150. // };
  151. ObjectManagerBase.prototype.initializeAll = function () {
  152. for (const i of this.instances) {
  153. i.initialize();
  154. }
  155. };
  156. ObjectManagerBase.prototype.performBigStep = function (eventList) {
  157. for (const e of eventList) {
  158. if (e.name === 'narrowCast') {
  159. this.handleNarrowCastEvent(e.parameters);
  160. } else if (e.name === 'broadCast') {
  161. this.handleBroadcastEvent(e.parameters);
  162. } else if (e.name === 'createInstance') {
  163. this.handleCreateEvent(e.parameters);
  164. } else if (e.name === 'associateInstance') {
  165. this.handleAssociateEvent(e.parameters);
  166. } else if (e.name === 'disassociateInstance') {
  167. this.handleDisassociateEvent(e.parameters);
  168. } else if (e.name === 'startInstance') {
  169. this.handleStartInstanceEvent(e.parameters);
  170. } else if (e.name === 'createAndStartInstance') {
  171. this.handleCreateAndStartEvent(e.parameters);
  172. } else if (e.name === 'deleteInstance') {
  173. this.handleDeleteInstanceEvent(e.parameters);
  174. }
  175. }
  176. };
  177. // just to comply with the RuntimeClassBase interface, ducktype-wise:
  178. ObjectManagerBase.prototype.processBigStepOutput = function () {
  179. }
  180. ObjectManagerBase.prototype.processAssociationReference = function (inputString) {
  181. if (inputString.length === 0) {
  182. throw new AssociationReferenceException('Empty association reference.');
  183. }
  184. const pathString = inputString.split('/');
  185. let result = [];
  186. if (inputString !== '') {
  187. for (let p in pathString) {
  188. if (!pathString.hasOwnProperty(p)) continue;
  189. var m = this.regex.exec(pathString[p]);
  190. if (m) {
  191. var name = m[1];
  192. var index = m[2];
  193. if (!index) {
  194. index = -1;
  195. }
  196. result.push({name: name, index: index});
  197. } else {
  198. throw new AssociationReferenceException('Invalid entry in association reference.');
  199. }
  200. }
  201. }
  202. return result;
  203. };
  204. ObjectManagerBase.prototype.handleStartInstanceEvent = function (parameters) {
  205. if (parameters.length !== 2) {
  206. throw new ParameterException('The start instance event needs 2 parameters.');
  207. }
  208. const source = parameters[0];
  209. const traversalList = this.processAssociationReference(parameters[1]);
  210. let instances = this.getInstances(source, traversalList);
  211. for (let i in instances) {
  212. if (!instances.hasOwnProperty(i)) continue;
  213. instances[i].instance.initialize();
  214. }
  215. source.addEvent(new Event("instanceStarted", parameters=[parameters[1]]));
  216. // ^ deprecated, will be replaced by:
  217. // this.controller.addInputLowLevel(
  218. // new Event("instanceStarted", null, [parameters[1]]),
  219. // [source],
  220. // 0);
  221. };
  222. ObjectManagerBase.prototype.handleBroadcastEvent = function (parameters) {
  223. if (parameters.length !== 2) {
  224. throw new ParameterException('The broadcast event needs 2 parameters (source of event and event name).');
  225. }
  226. this.controller.addInputLowLevel(
  227. paramaters[0],
  228. this.instances,
  229. this.controller.simulatedTime) // time offset
  230. // this.broadcast(parameters[0], parameters[1]);
  231. };
  232. ObjectManagerBase.prototype.handleCreateEvent = function (parameters) {
  233. if (parameters.length < 2) {
  234. throw new ParameterException('The create event needs at least 2 parameters.');
  235. }
  236. const source = parameters[0];
  237. const associationName = parameters[1];
  238. let association = source.associations[associationName];
  239. if (!association) {
  240. throw new ParameterException('No such association: ' + associationName);
  241. }
  242. if (association.allowedToAdd()) {
  243. // allow subclasses to be instantiated
  244. let className = undefined;
  245. let creationParameters = undefined;
  246. if (parameters.length === 2) {
  247. className = association.toClass;
  248. creationParameters = [];
  249. } else {
  250. // 3rd parameter is class name
  251. className = parameters[2];
  252. // parameters after 3rd parameter are creation parameters
  253. creationParameters = parameters.slice(3);
  254. }
  255. let newInstance = this.createInstance(className, creationParameters);
  256. if (newInstance === undefined) {
  257. throw new ParameterException('Creating instance: no such class: ' + className);
  258. }
  259. let index = association.addInstance(newInstance);
  260. // add parent association to created instance
  261. // if a parent association is defined in the class diagram
  262. let parentAssociation = newInstance.associations['parent'];
  263. if (parentAssociation !== undefined) {
  264. parentAssociation.addInstance(source);
  265. }
  266. // TODO: maybe change order of Event constructor parameters such that we don't have to
  267. // explicitly set the port to 'undefined'?
  268. source.addEvent(new Event('instanceCreated', undefined, [associationName + '[' + index + ']']));
  269. return [source, associationName + "[" + index.toString() + "]"];
  270. } else {
  271. source.addEvent(new Event('instanceCreationError', undefined, [associationName]));
  272. return [];
  273. }
  274. };
  275. ObjectManagerBase.prototype.handleCreateAndStartEvent = function (parameters) {
  276. params = this.handleCreateEvent(parameters);
  277. if (params) {
  278. this.handleStartInstanceEvent(params);
  279. }
  280. }
  281. ObjectManagerBase.prototype.handleDeleteInstanceEvent = function (parameters) {
  282. if (parameters.length !== 2) {
  283. throw new ParameterException('The delete instance event needs 2 parameters.');
  284. }
  285. const source = parameters[0];
  286. const traversalList = this.processAssociationReference(parameters[1]);
  287. let instances = this.getInstances(source, traversalList);
  288. for (let i in instances) {
  289. if (!instances.hasOwnProperty(i)) continue;
  290. instances[i].instance.stop();
  291. instances[i].instance.userDefinedDestructor();
  292. // delete association from source instance
  293. let associationToRemove = instances[i].ref.associations[instances[i].assocName];
  294. if (instances[i].assocIndex === -1) {
  295. // empty instances object
  296. associationToRemove.instances = {};
  297. } else {
  298. // remove property from instances object
  299. delete associationToRemove.instances[instances[i].assocIndex];
  300. }
  301. // also remove instance from Om's list of instances
  302. let index = this.instances.indexOf(instances[i].instance);
  303. this.instances.splice(index, 1);
  304. }
  305. source.addEvent(new Event('instanceDeleted', undefined, [parameters[1]]));
  306. };
  307. ObjectManagerBase.prototype.handleAssociateEvent = function (parameters) {
  308. if (parameters.length !== 3) {
  309. throw new ParameterException('The associateInstance event needs 3 parameters.');
  310. }
  311. const source = parameters[0];
  312. const sourceList = parameters[1];
  313. let traversalList = this.processAssociationReference(sourceList);
  314. let toCopyList = this.getInstances(source, traversalList);
  315. if (toCopyList.length !== 1) {
  316. throw new AssociationReferenceException('Invalid source association reference.');
  317. }
  318. let wrappedToCopyInstance = toCopyList[0].instance;
  319. let destList = this.processAssociationReference(parameters[2]);
  320. if (destList.length === 0) {
  321. throw new AssociationReferenceException('Invalid destination association reference.');
  322. }
  323. let last = destList.pop();
  324. if (last.index !== -1) {
  325. throw new AssociationReferenceException('Last association name in association reference could not be accompanied by an index.');
  326. }
  327. let instances = this.getInstances(source, destList);
  328. for (let i in instances) {
  329. if (!instances.hasOwnProperty(i)) continue;
  330. instances[i].instance.associations[last.name].addInstance(wrappedToCopyInstance);
  331. }
  332. };
  333. ObjectManagerBase.prototype.handleDisassociateEvent = function(parameters) {
  334. if (parameters.length !== 2) {
  335. throw new ParameterException('The disassociate event needs 2 parameters.');
  336. }
  337. var source = parameters[0];
  338. var traversal_list = this.processAssociationReference(parameters[1]);
  339. var instances = this.getInstances(source, traversal_list);
  340. for (var i in instances) {
  341. if (!instances.hasOwnProperty(i)) continue;
  342. // delete association from source instance
  343. var association_to_remove = instances[i].ref.associations[instances[i].assocName];
  344. if (instances[i].assocIndex === -1) {
  345. // empty instances object
  346. association_to_remove.instances = {};
  347. } else {
  348. // remove property from instances object
  349. delete association_to_remove.instances[instances[i].assocIndex];
  350. }
  351. }
  352. source.addEvent(new Event("instance_disassociated", undefined, [parameters[1]]));
  353. };
  354. ObjectManagerBase.prototype.handleNarrowCastEvent = function (parameters) {
  355. if (parameters.length !== 3) {
  356. throw new ParameterException('The narrowCast event needs 3 parameters.');
  357. }
  358. let source = parameters[0];
  359. let traversalList = this.processAssociationReference(parameters[1]);
  360. let castEvent = parameters[2];
  361. let instances = this.getInstances(source, traversalList);
  362. for (let i in instances) {
  363. if (!instances.hasOwnProperty(i)) continue;
  364. instances[i].instance.addEvent(castEvent);
  365. }
  366. };
  367. ObjectManagerBase.prototype.getInstances = function (source, traversalList) {
  368. let currents = [{
  369. instance: source,
  370. ref: null,
  371. assocName: null,
  372. assocIndex: null
  373. }];
  374. for (let t in traversalList) {
  375. if (!traversalList.hasOwnProperty(t)) continue;
  376. let name = traversalList[t].name;
  377. let index = traversalList[t].index;
  378. let nexts = [];
  379. for (var c in currents) {
  380. if (!currents.hasOwnProperty(c)) continue;
  381. let association = currents[c].instance.associations[name];
  382. if (index >= 0) {
  383. nexts.push({
  384. instance: association.getInstance(index),
  385. ref: currents[c].instance,
  386. assocName: name,
  387. assocIndex: index
  388. });
  389. } else if (index === -1) {
  390. for (let i in association.instances) {
  391. if (!association.instances.hasOwnProperty(i)) continue;
  392. nexts.push({
  393. instance: association.instances[i],
  394. ref: currents[c].instance,
  395. assocName: name,
  396. assocIndex: index
  397. });
  398. }
  399. } else {
  400. throw new AssociationReferenceException('Incorrect index in association reference.');
  401. }
  402. }
  403. currents = nexts;
  404. }
  405. return currents;
  406. };
  407. ObjectManagerBase.prototype.instantiate = function (toClass, constructParams) {
  408. // pure virtual
  409. };
  410. ObjectManagerBase.prototype.createInstance = function (toClass, constructParams) {
  411. let instance = this.instantiate(toClass, constructParams);
  412. this.instances.push(instance);
  413. return instance;
  414. };
  415. // Event
  416. function Event(name, port, parameters) {
  417. if (port === undefined) port = '';
  418. if (parameters === undefined) parameters = [];
  419. this.name = name;
  420. this.port = port;
  421. this.parameters = parameters;
  422. }
  423. function OutputListener(portNames) {
  424. this.portNames = portNames;
  425. this.queue = [];
  426. }
  427. OutputListener.prototype.add = function (theEvent) {
  428. if (this.portNames.length === 0 || this.portNames.indexOf(theEvent.port) !== -1) {
  429. this.queue.push(theEvent);
  430. }
  431. };
  432. OutputListener.prototype.fetch = function (timeout) {
  433. if (this.queue.length > 0) {
  434. return this.queue.shift();
  435. }
  436. };
  437. function ControllerBase(objectManager) {
  438. this.objectManager = objectManager;
  439. this.privatePortCounter = 0;
  440. // keep track of input ports
  441. this.inputPorts = {};
  442. this.inputQueue = new EventQueue();
  443. // keep track of output ports
  444. this.outputPorts = [];
  445. this.outputListeners = [];
  446. this.simulatedTime = 0;
  447. this.startTime = 0;
  448. }
  449. ControllerBase.prototype.getSimulatedTime = function () {
  450. return this.simulatedTime
  451. }
  452. ControllerBase.prototype.addInputPort = function (virtualName, instance) {
  453. let portName;
  454. if (instance === undefined) {
  455. portName = virtualName;
  456. } else {
  457. portName = 'private' + (this.privatePortCounter++) + virtualName;
  458. }
  459. this.inputPorts[portName] = {
  460. virtualName: virtualName,
  461. instance: instance
  462. };
  463. return portName;
  464. };
  465. ControllerBase.prototype.addOutputPort = function (portName) {
  466. this.outputPorts.push(portName);
  467. };
  468. // ControllerBase.prototype.broadcast = function (newEvent, timeOffset) {
  469. // if (timeOffset === undefined) timeOffset = 0
  470. // this.objectManager.broadcast(undefined, newEvent, timeOffset);
  471. // };
  472. ControllerBase.prototype.start = function () {
  473. this.startTime = time();
  474. this.objectManager.initializeAll();
  475. };
  476. ControllerBase.prototype.stop = function () {
  477. };
  478. ControllerBase.prototype.wakeup = function () {
  479. // will be overridden
  480. }
  481. ControllerBase.prototype.portToInstanceList = function(port) {
  482. if (port.instance !== undefined)
  483. return [port.instance];
  484. else
  485. return this.objectManager.instances;
  486. }
  487. ControllerBase.prototype.wallclockToSimtime = function() {
  488. return time() - this.startTime;
  489. }
  490. ControllerBase.prototype.addInput = function (eventName, portName, parameters, timestamp = 0) {
  491. // console.log("addInput", eventName, portName, parameters);
  492. if (eventName === '') {
  493. throw new InputException("Input event can't have an empty name.");
  494. }
  495. const port = this.inputPorts[portName];
  496. if (port === undefined) {
  497. throw new InputException('Input port mismatch, no such port: ' + portName + '.');
  498. }
  499. const virtualName = port.virtualName;
  500. const e = new Event(eventName, port.virtualName, parameters);
  501. const targetInstances = this.portToInstanceList(port);
  502. this.inputQueue.add(new EventQueueEntry(
  503. timestamp,
  504. e,
  505. targetInstances));
  506. this.wakeup();
  507. };
  508. // Lower-level: port attribute of event ignored, instead targetInstance is given
  509. ControllerBase.prototype.addInputLowLevel = function (event, targetInstances, timestamp) {
  510. const result = this.inputQueue.add(new EventQueueEntry(
  511. timestamp,
  512. event,
  513. targetInstances));
  514. this.wakeup();
  515. return result;
  516. }
  517. // Blocking call. Handle input events <= until (in simtime) as fast as possible.
  518. ControllerBase.prototype.runUntil = function (until) {
  519. while (this.inputQueue.getEarliestTime() <= until) {
  520. const entry = this.inputQueue.pop();
  521. this.simulatedTime = entry.scheduledTime;
  522. // console.log("simTime =", this.simulatedTime);
  523. for (const i of entry.instances) {
  524. i.performBigStep([entry.theEvent]);
  525. i.processBigStepOutput();
  526. }
  527. }
  528. };
  529. ControllerBase.prototype.outputEvent = function (theEvent) {
  530. for (const l of this.outputListeners) {
  531. l.add(theEvent);
  532. }
  533. };
  534. ControllerBase.prototype.addOutputListener = function (ports) {
  535. let listener = new OutputListener(ports);
  536. this.outputListeners.push(listener);
  537. return listener;
  538. };
  539. ControllerBase.prototype.addMyOwnOutputListener = function (listener) {
  540. this.outputListeners.push(listener);
  541. };
  542. // function GameLoopControllerBase(objectManager) {
  543. // ControllerBase.call(this, objectManager);
  544. // }
  545. // GameLoopControllerBase.prototype = new ControllerBase();
  546. // GameLoopControllerBase.prototype.update = function () {
  547. // const until = time();
  548. // while (this.inputQueue.getEarliestTime() <= until) {
  549. // this.handleDue();
  550. // }
  551. // };
  552. // EventLoop
  553. // parameters:
  554. // schedule - a callback scheduling another callback in the event loop
  555. // this callback should take 2 parameters: (callback, timeout) and return an ID
  556. // clear - a callback that clears a scheduled callback
  557. // this callback should take an ID that was returned by 'schedule'
  558. function EventLoop(schedule, clear) {
  559. this.scheduleCallback = schedule;
  560. this.clearCallback = clear;
  561. this.scheduledId = null;
  562. this.lastPrint = 0.0;
  563. }
  564. EventLoop.prototype.schedule = function (f, waitTime) {
  565. if (this.scheduledId) {
  566. // if the following error occurs, it is probably due to a flaw in the logic of EventLoopControllerBase
  567. throw new RuntimeException('EventLoop class intended to maintain at most 1 scheduled callback.');
  568. }
  569. if (waitTime !== Infinity) {
  570. this.scheduledId = this.scheduleCallback(f, waitTime)
  571. }
  572. };
  573. EventLoop.prototype.clear = function () {
  574. if (this.scheduledId) {
  575. this.clearCallback(this.scheduledId);
  576. this.scheduledId = null;
  577. }
  578. };
  579. function EventLoopControllerBase(objectManager, eventLoop, finishedCallback) {
  580. ControllerBase.call(this, objectManager);
  581. this.eventLoop = eventLoop;
  582. this.finishedCallback = finishedCallback;
  583. // this.behindScheduleCallback = behindScheduleCallback;
  584. // this.lastPrintTime = 0;
  585. }
  586. EventLoopControllerBase.prototype = new ControllerBase();
  587. // EventLoopControllerBase.prototype.addInput = function (eventName, portName, parameters, timeOffset = 0) {
  588. // ControllerBase.prototype.addInput.call(this, eventName, portName, parameters, timeOffset);
  589. // };
  590. EventLoopControllerBase.prototype.start = function () {
  591. ControllerBase.prototype.start.call(this);
  592. this.wakeup();
  593. };
  594. // EventLoopControllerBase.prototype.stop = function () {
  595. // this.eventLoop.clear();
  596. // ControllerBase.prototype.stop.call(this);
  597. // };
  598. EventLoopControllerBase.prototype.wakeup = function () {
  599. // (re-)schedule next wakeup
  600. this.eventLoop.clear();
  601. const simTimeNow = time() - this.startTime;
  602. const realTimeEarliest = this.inputQueue.getEarliestTime() + this.startTime;
  603. const realWaitTime = realTimeEarliest - time();
  604. // We do not directly call 'runUntil'. It only schedules the next call (which may be with timeout zero, i.e. ASAP). Therefore, it is 100% safe to call 'wakeup' from anywhere, even from action code (most likely indirectly by a call to 'addInput').
  605. this.eventLoop.schedule(() => {
  606. this.runUntil(simTimeNow + this.purposefullyBehind);
  607. this.wakeup();
  608. }, realWaitTime);
  609. if (realWaitTime < 0) {
  610. this.purposefullyBehind = realWaitTime;
  611. } else {
  612. this.purposefullyBehind = 0;
  613. }
  614. };
  615. // JsEventLoop
  616. function JsEventLoop() {
  617. EventLoop.call(this, window.setTimeout.bind(window), window.clearTimeout.bind(window));
  618. }
  619. JsEventLoop.prototype = new EventLoop();
  620. // Enum-like construct holding statechart semantic options.
  621. StatechartSemantics = {
  622. // Big Step Maximality
  623. TakeOne: 0,
  624. TakeMany: 1,
  625. // Concurrency
  626. Single: 0,
  627. Many: 1, // not yet implemented
  628. // Preemption (unsupported)
  629. NonPreemptive: 0,
  630. Preemptive: 1,
  631. // Internal Event Lifeline
  632. Queue: 0,
  633. NextSmallStep: 1,
  634. NextComboStep: 2,
  635. // Input Event Lifeline
  636. Whole: 0,
  637. FirstSmallStep: 1,
  638. FirstComboStep: 2,
  639. // Priority
  640. SourceParent: 0,
  641. SourceChild: 1
  642. };
  643. let DefaultStatechartSemantics = function () {
  644. this.bigStepMaximality = this.TakeMany;
  645. this.concurrency = this.Single;
  646. this.internalEventLifeline = this.Queue;
  647. this.inputEventLifeline = this.FirstComboStep;
  648. this.priority = this.SourceParent;
  649. };
  650. function State(stateId, name, obj) {
  651. this.stateId = stateId;
  652. this.name = name;
  653. this.obj = obj;
  654. this.ancestors = [];
  655. this.descendants = [];
  656. this.children = [];
  657. this.myParent = null;
  658. this.enter = null;
  659. this.exit = null;
  660. this.defaultState = null;
  661. this.transitions = [];
  662. this.myHistory = [];
  663. this.hasEventlessTransitions = false;
  664. }
  665. State.prototype.getEffectiveTargetStates = function () {
  666. let targets = [this];
  667. if (this.defaultState != null) {
  668. Array.prototype.push.apply(targets, this.defaultState.getEffectiveTargetStates());
  669. }
  670. return targets;
  671. }
  672. State.prototype.fixTree = function () {
  673. for (let c of this.children) {
  674. if (c instanceof HistoryState) {
  675. this.myHistory.push(c);
  676. }
  677. c.myParent = this;
  678. c.ancestors.push(this);
  679. Array.prototype.push.apply(c.ancestors, this.ancestors);
  680. c.fixTree();
  681. }
  682. Array.prototype.push.apply(this.descendants, this.children)
  683. for (let c of this.children) {
  684. Array.prototype.push.apply(this.descendants, c.descendants)
  685. }
  686. }
  687. State.prototype.addChild = function (child) {
  688. this.children.push(child);
  689. }
  690. State.prototype.addTransition = function (transition) {
  691. this.transitions.push(transition);
  692. }
  693. State.prototype.setEnter = function (enter) {
  694. this.enter = enter.bind(this.obj);
  695. }
  696. State.prototype.setExit = function (exit) {
  697. this.exit = exit.bind(this.obj);
  698. }
  699. function HistoryState(stateId, name, obj) {
  700. State.call(this, stateId, name, obj);
  701. }
  702. HistoryState.prototype = new State();
  703. function ShallowHistoryState(stateId, name, obj) {
  704. HistoryState.call(this, stateId, name, obj);
  705. }
  706. ShallowHistoryState.prototype = new HistoryState();
  707. ShallowHistoryState.prototype.getEffectiveTargetStates = function () {
  708. if (this.stateId in this.obj.historyValues) {
  709. let targets = [];
  710. for (let hv of this.obj.historyValues[this.stateId]) {
  711. Array.prototype.push.apply(targets, hv.getEffectiveTargetStates());
  712. }
  713. return targets;
  714. } else {
  715. // TODO: is it correct that in this case, the parent itself is also entered?
  716. return this.myParent.getEffectiveTargetStates();
  717. }
  718. }
  719. function DeepHistoryState(stateId, name, obj) {
  720. HistoryState.call(this, stateId, name, obj);
  721. }
  722. DeepHistoryState.prototype = new HistoryState();
  723. DeepHistoryState.prototype.getEffectiveTargetStates = function () {
  724. if (this.stateId in this.obj.historyValues) {
  725. return this.obj.historyValues[this.stateId];
  726. } else {
  727. // TODO: is it correct that in this case, the parent itself is also entered?
  728. return this.myParent.getEffectiveTargetStates();
  729. }
  730. }
  731. function ParallelState(stateId, name, obj) {
  732. State.call(this, stateId, name, obj);
  733. }
  734. ParallelState.prototype = new State();
  735. ParallelState.prototype.getEffectiveTargetStates = function () {
  736. let targets = [this];
  737. for (let c of this.children) {
  738. if (!(c instanceof HistoryState)) {
  739. Array.prototype.push.apply(targets, c.getEffectiveTargetStates());
  740. }
  741. }
  742. return targets;
  743. }
  744. function Transition(obj, source, targets) {
  745. this.guard = null;
  746. this.action = null;
  747. this.trigger = null;
  748. this.source = source;
  749. this.targets = targets;
  750. this.obj = obj;
  751. this.enabledEvent = null; // the event that enabled this transition
  752. this.optimize();
  753. }
  754. Transition.prototype.isEnabled = function (events) {
  755. if (this.trigger === null) {
  756. this.enabledEvent = null;
  757. return (this.guard === null || this.guard([]));
  758. } else {
  759. for (let i in events) {
  760. theEvent = events[i];
  761. if ((this.trigger.name === theEvent.name && (!this.trigger.port || this.trigger.port === theEvent.port)) && (this.guard === null || this.guard(theEvent.parameters))) {
  762. this.enabledEvent = theEvent;
  763. return true;
  764. }
  765. }
  766. }
  767. }
  768. Transition.prototype.fire = function () {
  769. // console.log("TRANSITION ", this.source.name, "->", this.targets[0].name);
  770. let targets = this.privateGetEffectiveTargetStates();
  771. let exitSet = this.privateExitSet(targets);
  772. for (let s of exitSet) {
  773. for (let h of s.myHistory) {
  774. let f = function (s0) {
  775. return s0.ancestors.length > 0 && s0.myParent === s;
  776. }
  777. if (h instanceof DeepHistoryState) {
  778. f = function (s0) {
  779. return s0.descendants.length === 0 && s.descendants.indexOf(s0) >= 0;
  780. }
  781. }
  782. this.obj.historyValues[h.stateId] = this.obj.configuration.filter(f);
  783. }
  784. }
  785. for (let s of exitSet) {
  786. // console.log("EXIT", s.name);
  787. if (s.exit != null) {
  788. s.exit();
  789. }
  790. }
  791. this.obj.configuration = this.obj.configuration.filter(function (el) {
  792. return exitSet.indexOf(el) < 0;
  793. })
  794. // combo state changed area
  795. this.obj.comboStep.changed.push(this.lca);
  796. for (let d of this.lca.descendants) {
  797. this.obj.comboStep.changed.push(d);
  798. }
  799. // execute transition action(s)
  800. if (this.action != null) {
  801. let params = [];
  802. if (this.enabledEvent != null) params = this.enabledEvent.parameters;
  803. this.action(params);
  804. }
  805. // enter states...
  806. for (let s of this.privateEnterSet(targets)) {
  807. this.obj.configuration.push(s);
  808. // console.log("ENTER", s.name);
  809. if (s.enter != null) {
  810. s.enter();
  811. }
  812. }
  813. this.obj.configuration = this.obj.configuration.sort(function (a, b) {
  814. return a.stateId - b.stateId
  815. })
  816. this.enabledEvent = null;
  817. }
  818. Transition.prototype.privateGetEffectiveTargetStates = function () {
  819. let targets = []
  820. for (let target of this.targets) {
  821. for (let effectiveTarget of target.getEffectiveTargetStates()) {
  822. if (targets.indexOf(effectiveTarget) < 0) {
  823. targets.push(effectiveTarget);
  824. }
  825. }
  826. }
  827. return targets;
  828. }
  829. Transition.prototype.privateExitSet = function (targets) {
  830. let exitSet = this.lca.descendants.slice(0).filter(function (obj) {
  831. return function (s) {
  832. return obj.obj.configuration.indexOf(s) >= 0;
  833. }
  834. }(this));
  835. exitSet.reverse();
  836. return exitSet;
  837. }
  838. Transition.prototype.privateEnterSet = function* (targets) {
  839. let target = targets[0];
  840. let reversedAncestors = target.ancestors.slice(0).reverse();
  841. for (let a of reversedAncestors) {
  842. if (this.source.ancestors.indexOf(a) >= 0) {
  843. } else {
  844. yield a;
  845. }
  846. }
  847. for (let target of targets) {
  848. yield target;
  849. }
  850. }
  851. Transition.prototype.setGuard = function (guard) {
  852. this.guard = guard.bind(this.obj);
  853. }
  854. Transition.prototype.setAction = function (action) {
  855. this.action = action.bind(this.obj);
  856. }
  857. Transition.prototype.setTrigger = function (trigger) {
  858. this.trigger = trigger;
  859. if (this.trigger === null) {
  860. this.source.hasEventlessTransitions = true;
  861. }
  862. }
  863. Transition.prototype.optimize = function () {
  864. let target = this.targets[0];
  865. this.lca = this.source.myParent;
  866. if (this.source.myParent !== target.myParent) { // external
  867. for (let a of this.source.ancestors) {
  868. if (target.ancestors.indexOf(a) >= 0) {
  869. this.lca = a;
  870. break;
  871. }
  872. }
  873. }
  874. }
  875. // RuntimeClassBase
  876. function RuntimeClassBase(controller) {
  877. // this.active = false;
  878. // this.privateSetStable(true);
  879. // this.events = new EventQueue();
  880. this.controller = controller;
  881. this.inports = {};
  882. this.timers = {};
  883. this.states = {};
  884. this.semantics = new DefaultStatechartSemantics();
  885. this.configuration = [];
  886. this.currentState = {};
  887. this.historyValues = {};
  888. this.timers = {};
  889. // this.timersToAdd = {};
  890. this.bigStep = new BigStepState();
  891. this.comboStep = new ComboStepState();
  892. this.smallStep = new SmallStepState();
  893. }
  894. RuntimeClassBase.prototype.eventlessTransitions = function () {
  895. for (let s of this.configuration) {
  896. if (s.hasEventlessTransitions) return true;
  897. }
  898. return false;
  899. }
  900. RuntimeClassBase.prototype.initialize = function () {
  901. // this.active = true;
  902. // this.privateSetStable(false);
  903. this.initializeStatechart();
  904. this.processBigStepOutput();
  905. };
  906. RuntimeClassBase.prototype.sccdYield = function () {
  907. return Math.max(0, (time() - this.controller.simulatedTime) / 1000.0);
  908. }
  909. RuntimeClassBase.prototype.getSimulatedTime = function () {
  910. return this.controller.simulatedTime;
  911. }
  912. RuntimeClassBase.prototype.updateConfiguration = function (states) {
  913. this.configuration = states.slice(0);
  914. };
  915. // RuntimeClassBase.prototype.stop = function () {
  916. // this.active = false;
  917. // this.privateSetStable(false);
  918. // };
  919. RuntimeClassBase.prototype.addTimer = function (index, timeout) {
  920. this.timers[index] = this.controller.addInputLowLevel(
  921. new Event('event'+index+'After'),
  922. [this], // targets
  923. this.controller.simulatedTime + timeout*1000); // timestamp
  924. // this.timersToAdd[index] = new EventQueueEntry(this.controller.simulatedTime + Math.trunc(timeout * 1000), new Event('event' + index + 'After'));
  925. };
  926. RuntimeClassBase.prototype.removeTimer = function (index) {
  927. this.controller.inputQueue.remove(this.timers[index]);
  928. delete this.timers[index];
  929. // if (index in this.timersToAdd) delete this.timersToAdd[index];
  930. // this.controller.inputQueue.remove(this.timers[index]);
  931. // // this.events.remove(this.timers[index]);
  932. // delete this.timers[index];
  933. };
  934. RuntimeClassBase.prototype.addEvent = function (event, timeOffset = 0) {
  935. // console.log("DEPRECATED: RuntimeClassBase.addEvent");
  936. const _add = e => {
  937. this.controller.addInputLowLevel(e, [this], this.controller.simulatedTime + timeOffset);
  938. }
  939. if (event instanceof Array) {
  940. for (const e of event)
  941. _add(e);
  942. } else {
  943. _add(event);
  944. }
  945. };
  946. RuntimeClassBase.prototype.processBigStepOutput = function () {
  947. for (const e of this.bigStep.outputEventsPort) {
  948. this.controller.outputEvent(e);
  949. }
  950. let om = this.bigStep.outputEventsPm;
  951. for (const e of this.bigStep.outputEventsPm) {
  952. this.controller.addInputLowLevel(e, [this.controller.objectManager], this.controller.simulatedTime);
  953. }
  954. };
  955. // RuntimeClassBase.prototype.step = function () {
  956. // let stable = false;
  957. // while (!stable) {
  958. // let due = [];
  959. // if (this.events.getEarliestTime() <= this.controller.simulatedTime) {
  960. // due = [this.events.pop()];
  961. // }
  962. // stable = !this.performBigStep(due);
  963. // this.processBigStepOutput();
  964. // }
  965. // for (let key in this.timersToAdd) {
  966. // if (!this.timersToAdd.hasOwnProperty(key)) continue;
  967. // this.timers[key] = this.events.add(this.timersToAdd[key]);
  968. // }
  969. // this.timersToAdd = {};
  970. // this.privateSetStable(true);
  971. // };
  972. RuntimeClassBase.prototype.inState = function (stateStrings) {
  973. let stateIds = [];
  974. for (let stateString of stateStrings) {
  975. stateIds.push(this.states[stateString].stateId);
  976. }
  977. for (let stateId of stateIds) {
  978. let found = false;
  979. for (let s of this.configuration) {
  980. if (s.stateId === stateId) {
  981. found = true;
  982. break;
  983. }
  984. }
  985. if (!found) return false;
  986. }
  987. return true;
  988. };
  989. RuntimeClassBase.prototype.performBigStep = function (inputEvents) {
  990. this.bigStep.next(inputEvents);
  991. this.smallStep.reset();
  992. this.comboStep.reset();
  993. while (this.performComboStep()) {
  994. this.bigStep.hasStepped = true;
  995. if (this.semantics.bigStepMaximality === StatechartSemantics.TakeOne)
  996. break;
  997. }
  998. return this.bigStep.hasStepped;
  999. };
  1000. RuntimeClassBase.prototype.performComboStep = function () {
  1001. this.comboStep.next();
  1002. while (this.performSmallStep()) {
  1003. this.comboStep.hasStepped = true;
  1004. }
  1005. return this.comboStep.hasStepped;
  1006. };
  1007. // generate transition candidates for current small step
  1008. RuntimeClassBase.prototype.generateCandidates = function () {
  1009. let enabledEvents = this.getEnabledEvents();
  1010. let enabledTransitions = [];
  1011. for (let s of this.configuration) {
  1012. if (this.comboStep.changed.indexOf(s) < 0) {
  1013. for (let t of s.transitions) {
  1014. if (t.isEnabled(enabledEvents)) {
  1015. enabledTransitions.push(t);
  1016. }
  1017. }
  1018. }
  1019. }
  1020. return enabledTransitions;
  1021. };
  1022. RuntimeClassBase.prototype.performSmallStep = function () {
  1023. let privateYoungerThan =
  1024. function (x, y) {
  1025. if (y.source.ancestors.indexOf(x.source) >= 0) {
  1026. return 1;
  1027. } else if (x.source.ancestors.indexOf(y.source) >= 0) {
  1028. return -1;
  1029. } else {
  1030. throw new Exception('These items have no relation with each other.');
  1031. }
  1032. };
  1033. if (this.smallStep.hasStepped) {
  1034. this.smallStep.next();
  1035. }
  1036. let candidates = this.generateCandidates();
  1037. if (candidates.length > 0) {
  1038. toSkip = new Set();
  1039. conflicting = [];
  1040. for (var i in candidates) {
  1041. c1 = candidates[i];
  1042. if (!toSkip.has(c1)) {
  1043. conflict = [c1];
  1044. for (let j in candidates.slice(candidates.indexOf(c1))) {
  1045. c2 = candidates[j];
  1046. if (c1.source.ancestors.indexOf(c2.source) >= 0 || c2.source.ancestors.indexOf(c1.source) >= 0) {
  1047. conflict.push(c2);
  1048. toSkip.add(c2);
  1049. }
  1050. }
  1051. conflicting.push(conflict.sort(privateYoungerThan));
  1052. }
  1053. }
  1054. if (this.semantics.concurrency === StatechartSemantics.Single) {
  1055. let candidate = conflicting[0];
  1056. if (this.semantics.priority === StatechartSemantics.SourceParent) {
  1057. candidate[candidate.length - 1].fire();
  1058. } else {
  1059. candidate[0].fire();
  1060. }
  1061. } else if (this.semantics.concurrency === StatechartSemantics.Many) {
  1062. // TODO: Implement
  1063. }
  1064. this.smallStep.hasStepped = true;
  1065. }
  1066. return this.smallStep.hasStepped;
  1067. };
  1068. RuntimeClassBase.prototype.getEnabledEvents = function () {
  1069. let result = this.smallStep.currentEvents.concat(this.comboStep.currentEvents);
  1070. if (this.semantics.inputEventLifeline === StatechartSemantics.Whole ||
  1071. (!this.bigStep.hasStepped &&
  1072. (this.semantics.inputEventLifeline === StatechartSemantics.FirstComboStep ||
  1073. (!this.comboStep.hasStepped &&
  1074. this.semantics.inputEventLifeline === StatechartSemantics.FirstSmallStep)))) {
  1075. result = result.concat(this.bigStep.inputEvents);
  1076. }
  1077. return result;
  1078. };
  1079. RuntimeClassBase.prototype.raiseInternalEvent = function (theEvent) {
  1080. if (this.semantics.internalEventLifeline === StatechartSemantics.NextSmallStep) {
  1081. this.smallStep.addNextEvent(theEvent);
  1082. } else if (this.semantics.internalEventLifeline === StatechartSemantics.NextComboStep) {
  1083. this.comboStep.addNextEvent(theEvent);
  1084. } else if (this.semantics.internalEventLifeline === StatechartSemantics.Queue) {
  1085. this.addEvent(theEvent, 0);
  1086. }
  1087. };
  1088. RuntimeClassBase.prototype.initializeStatechart = function () {
  1089. this.updateConfiguration(this.defaultTargets);
  1090. for (let state of this.defaultTargets) {
  1091. if (state.enter != null) {
  1092. state.enter();
  1093. }
  1094. }
  1095. };
  1096. RuntimeClassBase.prototype.getInPortName = function (name) {
  1097. // const portName = this.inports[name];
  1098. // if ( portName != null ) {
  1099. // return portName;
  1100. // }
  1101. return name;
  1102. };
  1103. RuntimeClassBase.prototype.getOutPortName = function (name) {
  1104. // const portName = this.outports[name];
  1105. // if ( portName != null ) {
  1106. // return portName;
  1107. // }
  1108. return name;
  1109. };
  1110. let BigStepState = function () {
  1111. this.inputEvents = [];
  1112. this.outputEventsPort = [];
  1113. this.outputEventsPm = [];
  1114. this.hasStepped = true;
  1115. this.transitions = 0;
  1116. };
  1117. BigStepState.prototype.next = function (inputEvents) {
  1118. this.inputEvents = inputEvents;
  1119. this.outputEventsPort = [];
  1120. this.outputEventsPm = [];
  1121. this.hasStepped = false;
  1122. this.transitions = 0;
  1123. };
  1124. BigStepState.prototype.outputEvent = function (event) {
  1125. this.outputEventsPort.push(event);
  1126. };
  1127. BigStepState.prototype.outputEventOm = function (event) {
  1128. this.outputEventsPm.push(event);
  1129. };
  1130. let ComboStepState = function () {
  1131. this.currentEvents = [];
  1132. this.nextEvents = [];
  1133. this.changed = [];
  1134. this.hasStepped = true;
  1135. };
  1136. ComboStepState.prototype.reset = function () {
  1137. this.currentEvents = [];
  1138. this.nextEvents = [];
  1139. };
  1140. ComboStepState.prototype.next = function () {
  1141. this.currentEvents = this.nextEvents;
  1142. this.nextEvents = [];
  1143. this.changed = [];
  1144. this.hasStepped = false;
  1145. };
  1146. ComboStepState.prototype.addNextEvent = function (event) {
  1147. this.nextEvents.push(event);
  1148. };
  1149. ComboStepState.prototype.setArenaChanged = function (arena) {
  1150. this.changed.push(arena);
  1151. };
  1152. ComboStepState.prototype.isArenaChanged = function (arena) {
  1153. return (this.changed.indexOf(arena) !== -1);
  1154. };
  1155. ComboStepState.prototype.isStable = function () {
  1156. return (this.changed.length === 0);
  1157. };
  1158. let SmallStepState = function () {
  1159. this.currentEvents = [];
  1160. this.nextEvents = [];
  1161. this.candidates = [];
  1162. this.hasStepped = true;
  1163. };
  1164. SmallStepState.prototype.reset = function () {
  1165. this.currentEvents = [];
  1166. this.nextEvents = [];
  1167. };
  1168. SmallStepState.prototype.next = function () {
  1169. this.currentEvents = this.nextEvents;
  1170. this.nextEvents = [];
  1171. this.candidates = [];
  1172. this.hasStepped = false;
  1173. };
  1174. SmallStepState.prototype.addNextEvent = function (event) {
  1175. this.nextEvents.push(event);
  1176. };
  1177. SmallStepState.prototype.addCandidate = function (t, p) {
  1178. this.candidates.push({transition: t, parameters: p});
  1179. };
  1180. SmallStepState.prototype.hasCandidates = function () {
  1181. return (this.candidates.length > 0);
  1182. };