Today I made a few basic changes.
Instead of placing all elements (components and entities) that contain discrete-event simulation features into the setOfEntities
array I created a setOfComponents
array and placed the components in there, instead. This allows me to loop through lists of like elements to perform certain operations on them, like asking them to draw themselves or (possibly) asking them to de-link themselves at the end of a simulation so they can be garbage collected.
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 |
//collection of all entities in the system var setOfEntities = []; //new Array(); //collection of all components in the system var setOfComponents = []; ... function generateNewEntity() { //generator is an arrivals component //start is an initial activation time var newEntity = new EntityPassive(); setOfEntities.push(newEntity); return newEntity; } //generateNewEntity ... //Arrivals component: generates entities that will be processed by the model function ArrivalsComponent(scheduleBlockMinutes,scheduleArray) { setOfComponents.push(this); ... ... //draw everything clearCanvas("#000000"); //new way var numComponents = setOfComponents.length; for (i=0; i<numComponents; i++) { setOfComponents[i].drawData(); } //old way // arrival1.drawData(); // entry1.drawData(); // queue1.drawData(); // process1.drawData(); // exit1.drawData(); |
I added a function to remove entities from the setOfEntities array when they finish getting processed at an ExitComponent.
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 |
function removeEntityFromList(entityID) { var i = 0; while (setOfEntities[i].entityID != entityID) { i++; } if (i < setOfEntities.length) { setOfEntities.splice(i,1); } } ... //from the ExitComponent this.receiveEntity = function(entity) { //receive the entity this.exitResidenceTime = globalSimClock - entity.getEntryTime(); //display what was done this.exitTime = globalSimClock; this.currentExitEntityID = entity.entityID; this.activity = "entity exits"; //set timer to clear the display after a bit this.endExitDisplayTime = globalSimClock+this.displayDelay; feq.newItem(this.endExitDisplayTime,this,"clearExitDisplay"); displayProgressText("Exit comp. "+this.componentID+" receives entity: "+this.currentExitEntityID+" at time "+globalSimClock.toFixed(6)); //here's the call removeEntityFromList(entity.entityID); }; |
Finally, I moved the definition of the detail parameters of the DataDisplay objects so they can be defined outside of each component.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
//define the display component function DisplayGroup() { // this.label = label; // this.xLocation = x; // this.yLocation = y; // this.valueWidth = vw; // this.labelColor = lc; // this.valueColor = vc; // this.borderColor = bc; this.maxLabelWidth = 0; globalCTX.font = "12px Arial"; this.valueCount = 0; this.valueList = []; //new Array(); this.define = function(label,x,y,vw,lc,vc,bc) { //used to define outside of an object this.label = label; this.xLocation = x; this.yLocation = y; this.valueWidth = vw; this.labelColor = lc; this.valueColor = vc; this.borderColor = bc; }; this.addValue = function(value,label,format,places) { ... }; this.drawBasic = function() { ... }; } //DisplayGroup ... //support the definitions in each component object //old way //this.dataGroup = new DisplayGroup(this.componentName,10,10,80,"#00FFFF","#FF0000","#FFFF00"); //new way //this must be defined here so the addValue methods will work this.dataGroup = new DisplayGroup(); //this should be defined externally this.defineDataGroup = function(x,y,vw,bc,vc,lc) { this.dataGroup.define(this.componentName,x,y,vw,bc,vc,lc); }; this.dataGroup.addValue(this.componentID,"comp. ID","integer"); this.dataGroup.addValue(this.startBlockTime,"Start Time","numdec",5); this.dataGroup.addValue(this.currentBlockTime,"CurrentTime","numdec",5); ... //set the values var arrival1 = new ArrivalsComponent(30.0,arrivalSchedule); arrival1.defineDataGroup(10,10,80,"#00FFFF","#FF0000","#FFFF00"); var entry1 = new EntryComponent(2.0); entry1.defineDataGroup(175,10,80,"#00FFFF","#FF0000","#FFFF00"); |