statecharts_core.js 40 KB

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