Here is code that actually runs. It creates a single entity, then updates it in a loop until the ending time is exceeded. The next steps will be to see how it works with multiple entities of the same kind and then with entities of different kinds.
This example is quite trivial so far. It doesn’t do a thing that’s meaningful but we can see the underlying mechanisms building up. It should get interesting quickly enough.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
//entity item function entity(entityID,initialTime,incrementTime,endTime) { this.entityID = entityID; this.initialTime = initialTime; this.incrementTime = incrementTime; this.endTime = endTime; this.nextState = "increment"; feq.insertItem(initialTime,"absolute",this); displayProgressText("entity "+this.entityID+" created at time "+globalSimClock+"<br />"); this.activate = function(feqItem) { if (this.nextState == "increment") { console.log("entity "+this.entityID+" updated at time "+globalSimClock); displayProgressText("entity "+this.entityID+" updated at time "+globalSimClock+"<br />"); if (globalSimClock + this.incrementTime >= this.endTime) { this.nextState = "destroy"; } feqItem.update(this.incrementTime,"advance"); feq.insertExistingItem(feqItem); } else if (this.nextState == "destroy") { console.log("entity "+this.entityID+" terminated at time "+globalSimClock) displayProgressText("entity "+this.entityID+" terminated at time "+globalSimClock+"<br />"); } else { alert("entity "+this.entityID+" went into undefined state"); displayProgressText("entity "+this.entityID+" in undefined state at time "+globalSimClock+"<br />"); } }; //this.activate }; //entity var entityA = new entity(1,11.0,10.0,100.0); var keepRunning = true; while (keepRunning) { var itemList = feq.getFirstItem(); feq.feq.forEach(reportItemInfo); feq.reportSize(); //should be 0 if (itemList) { var item = itemList[0]; item.entity.activate(item); feq.feq.forEach(reportItemInfo); feq.reportSize(); //should be 1 } else { keepRunning = false; } } //while (keepRunning) |
I also had to modify the getFirstItem
method in the futureEventsQueue
object. It has to test whether the returned array is of zero length, which indicates that the future events queue is empty.
1 2 3 4 5 6 7 8 9 10 11 |
this.getFirstItem = function() { var feqItem = this.feq.splice(0,1); if (feqItem.length) { var t = feqItem[0].getActivationTime(); updateSimClock(t); this.feqSize--; return feqItem; } else { console.log("no items in FEQ to retrieve "+this.feqSize) } }; |
Here’s the browser output, now that I’ve started to build something the user can see via HTML. The first line is a clock that gets updated with each event. The remainder is a block that logs all the events, which in this case involve only a single entity.
101 minutes
entity 1 created at time 0
entity 1 updated at time 11
entity 1 updated at time 21
entity 1 updated at time 31
entity 1 updated at time 41
entity 1 updated at time 51
entity 1 updated at time 61
entity 1 updated at time 71
entity 1 updated at time 81
entity 1 updated at time 91
entity 1 terminated at time 101