{"id":1094,"date":"2016-10-25T23:43:08","date_gmt":"2016-10-26T04:43:08","guid":{"rendered":"http:\/\/rpchurchill.com\/?p=1094"},"modified":"2016-10-26T01:07:58","modified_gmt":"2016-10-26T06:07:58","slug":"a-simple-discrete-event-simulation-part-31","status":"publish","type":"post","link":"https:\/\/rpchurchill.com\/wordpress\/posts\/2016\/10\/25\/a-simple-discrete-event-simulation-part-31\/","title":{"rendered":"A Simple Discrete-Event Simulation: Part 31"},"content":{"rendered":"<p>Today I made a few basic changes.<\/p>\n<p>Instead of placing all elements (components and entities) that contain discrete-event simulation features into the <code>setOfEntities<\/code> array I created a <code>setOfComponents<\/code> 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.<\/p>\n<pre class=\"toolbar-overlay:false wrap:false height-set:true lang:default decode:true \">\r\n    \/\/collection of all entities in the system\r\n    var setOfEntities = [];  \/\/new Array();\r\n    \/\/collection of all components in the system\r\n    var setOfComponents = [];\r\n...\r\n    function generateNewEntity() {\r\n      \/\/generator is an arrivals component\r\n      \/\/start is an initial activation time\r\n      var newEntity = new EntityPassive();\r\n      setOfEntities.push(newEntity);\r\n      return newEntity;\r\n    }  \/\/generateNewEntity\r\n...\r\n    \/\/Arrivals component: generates entities that will be processed by the model\r\n    function ArrivalsComponent(scheduleBlockMinutes,scheduleArray) {\r\n      setOfComponents.push(this);\r\n      ...\r\n...\r\n      \/\/draw everything\r\n      clearCanvas(\"#000000\");\r\n      \/\/new way\r\n      var numComponents = setOfComponents.length;\r\n      for (i=0; i<numComponents; i++) {\r\n        setOfComponents[i].drawData();\r\n      }\r\n      \/\/old way\r\n\/\/      arrival1.drawData();\r\n\/\/      entry1.drawData();\r\n\/\/      queue1.drawData();\r\n\/\/      process1.drawData(); \r\n\/\/      exit1.drawData();\r\n<\/pre>\n<p>I added a function to remove entities from the setOfEntities array when they finish getting processed at an ExitComponent.<\/p>\n<pre class=\"toolbar-overlay:false wrap:false height-set:true lang:default decode:true \">\r\n    function removeEntityFromList(entityID) {\r\n      var i = 0;\r\n      while (setOfEntities[i].entityID != entityID) {\r\n        i++;\r\n      }\r\n      if (i < setOfEntities.length) {\r\n        setOfEntities.splice(i,1);\r\n      }\r\n    }\r\n\r\n...\r\n      \/\/from the ExitComponent\r\n      this.receiveEntity = function(entity) {\r\n        \/\/receive the entity\r\n        this.exitResidenceTime = globalSimClock - entity.getEntryTime();\r\n        \/\/display what was done\r\n        this.exitTime = globalSimClock;\r\n        this.currentExitEntityID = entity.entityID;\r\n        this.activity = \"entity exits\";\r\n        \/\/set timer to clear the display after a bit\r\n        this.endExitDisplayTime = globalSimClock+this.displayDelay;\r\n        feq.newItem(this.endExitDisplayTime,this,\"clearExitDisplay\");\r\n        displayProgressText(\"Exit comp. \"+this.componentID+\" receives entity: \"+this.currentExitEntityID+\" at time \"+globalSimClock.toFixed(6));\r\n        \/\/here's the call \r\n        removeEntityFromList(entity.entityID);\r\n      };\r\n\r\n<\/pre>\n<p>Finally, I moved the definition of the detail parameters of the DataDisplay objects so they can be defined outside of each component.<\/p>\n<pre class=\"toolbar-overlay:false wrap:false height-set:true lang:default decode:true \">\r\n    \/\/define the display component\r\n    function DisplayGroup() {\r\n\/\/      this.label = label;\r\n\/\/      this.xLocation = x;\r\n\/\/      this.yLocation = y;\r\n\/\/      this.valueWidth = vw;\r\n\/\/      this.labelColor = lc;\r\n\/\/      this.valueColor = vc;\r\n\/\/      this.borderColor = bc;\r\n      this.maxLabelWidth = 0;\r\n      globalCTX.font = \"12px Arial\";\r\n      this.valueCount = 0;\r\n      this.valueList = []; \/\/new Array();\r\n      this.define = function(label,x,y,vw,lc,vc,bc) {\r\n        \/\/used to define outside of an object\r\n        this.label = label;\r\n        this.xLocation = x;\r\n        this.yLocation = y;\r\n        this.valueWidth = vw;\r\n        this.labelColor = lc;\r\n        this.valueColor = vc;\r\n        this.borderColor = bc;\r\n      };\r\n      this.addValue = function(value,label,format,places) {\r\n        ...\r\n      };\r\n      this.drawBasic = function() {\r\n        ...\r\n      };\r\n    }  \/\/DisplayGroup\r\n...\r\n    \/\/support the definitions in each component object\r\n      \/\/old way\r\n      \/\/this.dataGroup = new DisplayGroup(this.componentName,10,10,80,\"#00FFFF\",\"#FF0000\",\"#FFFF00\");\r\n      \/\/new way\r\n      \/\/this must be defined here so the addValue methods will work\r\n      this.dataGroup = new DisplayGroup();\r\n      \/\/this should be defined externally\r\n      this.defineDataGroup = function(x,y,vw,bc,vc,lc) {\r\n        this.dataGroup.define(this.componentName,x,y,vw,bc,vc,lc);\r\n      };\r\n      this.dataGroup.addValue(this.componentID,\"comp. ID\",\"integer\");\r\n      this.dataGroup.addValue(this.startBlockTime,\"Start Time\",\"numdec\",5);\r\n      this.dataGroup.addValue(this.currentBlockTime,\"CurrentTime\",\"numdec\",5);\r\n...\r\n    \/\/set the values\r\n    var arrival1 = new ArrivalsComponent(30.0,arrivalSchedule);\r\n    arrival1.defineDataGroup(10,10,80,\"#00FFFF\",\"#FF0000\",\"#FFFF00\");\r\n\r\n    var entry1 = new EntryComponent(2.0);\r\n    entry1.defineDataGroup(175,10,80,\"#00FFFF\",\"#FF0000\",\"#FFFF00\");\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>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 &hellip; <a href=\"https:\/\/rpchurchill.com\/wordpress\/posts\/2016\/10\/25\/a-simple-discrete-event-simulation-part-31\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[60],"tags":[121,49],"_links":{"self":[{"href":"https:\/\/rpchurchill.com\/wordpress\/wp-json\/wp\/v2\/posts\/1094"}],"collection":[{"href":"https:\/\/rpchurchill.com\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/rpchurchill.com\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/rpchurchill.com\/wordpress\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/rpchurchill.com\/wordpress\/wp-json\/wp\/v2\/comments?post=1094"}],"version-history":[{"count":3,"href":"https:\/\/rpchurchill.com\/wordpress\/wp-json\/wp\/v2\/posts\/1094\/revisions"}],"predecessor-version":[{"id":1097,"href":"https:\/\/rpchurchill.com\/wordpress\/wp-json\/wp\/v2\/posts\/1094\/revisions\/1097"}],"wp:attachment":[{"href":"https:\/\/rpchurchill.com\/wordpress\/wp-json\/wp\/v2\/media?parent=1094"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/rpchurchill.com\/wordpress\/wp-json\/wp\/v2\/categories?post=1094"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/rpchurchill.com\/wordpress\/wp-json\/wp\/v2\/tags?post=1094"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}