{"id":959,"date":"2016-09-07T20:14:43","date_gmt":"2016-09-08T01:14:43","guid":{"rendered":"http:\/\/rpchurchill.com\/?p=959"},"modified":"2016-09-07T20:14:43","modified_gmt":"2016-09-08T01:14:43","slug":"a-simple-discrete-event-simulation-part-6","status":"publish","type":"post","link":"https:\/\/rpchurchill.com\/wordpress\/posts\/2016\/09\/07\/a-simple-discrete-event-simulation-part-6\/","title":{"rendered":"A Simple Discrete-Event Simulation: Part 6"},"content":{"rendered":"<p>Now that I&#8217;m sure the basics are working the way I&#8217;d like I want to see if I can streamline things some more.<\/p>\n<p>The first thing to do is recognize that the next item being pulled off the future events queue can always be referred to the same way.  If we make the item a global variable then we can always have code refer to it by a known name without having to pass the reference around all over the place.  This cleans things up quite a bit.<\/p>\n<p>We&#8217;ll start by creating a variable called <code>feqCurrent<\/code>, which is intended to hold a <code>futureEventItem<\/code> object.<\/p>\n<pre class=\"toolbar-overlay:false wrap:false height-set:true lang:default decode:true \">\r\n    var feqCurrent;  \/\/global feqItem\r\n<\/pre>\n<p>Next, we&#8217;ll copy the <code>insertExistingItem<\/code> method in the <code>futureEventsQueue<\/code> object so it eliminates the need to pass in a reference to an <code>feqItem<\/code>, because it refers to the global variable instead.<\/p>\n<pre class=\"toolbar-overlay:false wrap:false height-set:true lang:default decode:true \">\r\n      this.insertCurrent = function() {\r\n        this.insertTime = feqCurrent.getActivationTime();\r\n        globalInsertTime = this.insertTime;\r\n        if (this.feqSize == 0) {  \r\n          this.feq[0] = feqCurrent;\r\n          this.feqSize++;\r\n        } else {\r\n          \/\/find index of feq item to insert before\r\n          var insertIndex = this.feq.findIndex(this.findLaterTime);\r\n          \/\/insert the element\r\n          if (insertIndex < 0) {\r\n            insertIndex = this.feq.length;\r\n          }\r\n          this.feq.splice(insertIndex,0,feqCurrent);\r\n          this.feqSize++;\r\n        }\r\n      };\r\n<\/pre>\n<p>We'll follow that up by streamlining the <code>advance<\/code> function in the same way.<\/p>\n<pre class=\"toolbar-overlay:false wrap:false height-set:true lang:default decode:true \">\r\n    function advance(byTime) {\r\n      feqCurrent.update(byTime,\"advance\");\r\n      feq.insertCurrent();\r\n    };  \r\n<\/pre>\n<p>Then we'll do the same to the <code>activate<\/code> function in the <code>entity<\/code> object. <\/p>\n<pre class=\"toolbar-overlay:false wrap:false height-set:true lang:default decode:true \">\r\n      this.activate = function() {\r\n        if (this.nextState == \"increment\") {\r\n          displayProgressText(\"entity \"+this.entityID+\" updated at time \"+globalSimClock+\"<br \/>\");\r\n          if (globalSimClock + this.incrementTime >= this.endTime) {\r\n            this.nextState = \"destroy\";\r\n          }\r\n          advance(this.incrementTime);\r\n        } else if (this.nextState == \"destroy\") {\r\n          displayProgressText(\"entity \"+this.entityID+\" terminated at time \"+globalSimClock+\"<br \/>\");\r\n        } else {\r\n          displayProgressText(\"entity \"+this.entityID+\" in undefined state at time \"+globalSimClock+\"<br \/>\");\r\n        }\r\n      };  \/\/this.activate      \r\n<\/pre>\n<p>Finally, we'll do the same thing to the main event processing loop.<\/p>\n<pre class=\"toolbar-overlay:false wrap:false height-set:true lang:default decode:true \">\r\n    var keepRunning = true;\r\n    while (keepRunning) {\r\n    \r\n      var itemList = feq.getFirstItem();\r\n      \r\n      if (itemList) {\r\n        feqCurrent = itemList[0];\r\n        feqCurrent.entity.activate();\r\n      } else {\r\n        keepRunning = false;\r\n      }\r\n    }  \/\/while (keepRunning)\r\n<\/pre>\n<p>The output remains the same as before, so we assume we haven't broken anything.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Now that I&#8217;m sure the basics are working the way I&#8217;d like I want to see if I can streamline things some more. The first thing to do is recognize that the next item being pulled off the future events &hellip; <a href=\"https:\/\/rpchurchill.com\/wordpress\/posts\/2016\/09\/07\/a-simple-discrete-event-simulation-part-6\/\">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\/959"}],"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=959"}],"version-history":[{"count":1,"href":"https:\/\/rpchurchill.com\/wordpress\/wp-json\/wp\/v2\/posts\/959\/revisions"}],"predecessor-version":[{"id":960,"href":"https:\/\/rpchurchill.com\/wordpress\/wp-json\/wp\/v2\/posts\/959\/revisions\/960"}],"wp:attachment":[{"href":"https:\/\/rpchurchill.com\/wordpress\/wp-json\/wp\/v2\/media?parent=959"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/rpchurchill.com\/wordpress\/wp-json\/wp\/v2\/categories?post=959"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/rpchurchill.com\/wordpress\/wp-json\/wp\/v2\/tags?post=959"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}