{"id":947,"date":"2016-09-01T13:29:14","date_gmt":"2016-09-01T18:29:14","guid":{"rendered":"http:\/\/rpchurchill.com\/?p=947"},"modified":"2016-09-01T13:29:14","modified_gmt":"2016-09-01T18:29:14","slug":"a-simple-discrete-event-simulation-part-4","status":"publish","type":"post","link":"https:\/\/rpchurchill.com\/wordpress\/posts\/2016\/09\/01\/a-simple-discrete-event-simulation-part-4\/","title":{"rendered":"A Simple Discrete-Event Simulation: Part 4"},"content":{"rendered":"<p>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.<\/p>\n<p>This example is quite trivial so far.  It doesn&#8217;t do a thing that&#8217;s meaningful but we can see the underlying mechanisms building up.  It should get interesting quickly enough.<\/p>\n<pre class=\"toolbar-overlay:false wrap:false height-set:true lang:default decode:true \">\r\n    \/\/entity item\r\n    function entity(entityID,initialTime,incrementTime,endTime) {\r\n      this.entityID = entityID;\r\n      this.initialTime = initialTime;\r\n      this.incrementTime = incrementTime;\r\n      this.endTime = endTime;\r\n      this.nextState = \"increment\";\r\n      feq.insertItem(initialTime,\"absolute\",this);\r\n      displayProgressText(\"entity \"+this.entityID+\" created at time \"+globalSimClock+\"<br \/>\");\r\n      this.activate = function(feqItem) {\r\n        if (this.nextState == \"increment\") {\r\n          console.log(\"entity \"+this.entityID+\" updated at time \"+globalSimClock);\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          feqItem.update(this.incrementTime,\"advance\");\r\n          feq.insertExistingItem(feqItem);\r\n        } else if (this.nextState == \"destroy\") {\r\n          console.log(\"entity \"+this.entityID+\" terminated at time \"+globalSimClock)\r\n          displayProgressText(\"entity \"+this.entityID+\" terminated at time \"+globalSimClock+\"<br \/>\");\r\n        } else {\r\n          alert(\"entity \"+this.entityID+\" went into undefined state\");\r\n          displayProgressText(\"entity \"+this.entityID+\" in undefined state at time \"+globalSimClock+\"<br \/>\");\r\n        }\r\n      };  \/\/this.activate      \r\n    };  \/\/entity\r\n\r\n    var entityA = new entity(1,11.0,10.0,100.0);\r\n    \r\n    var keepRunning = true;\r\n    while (keepRunning) {\r\n    \r\n      var itemList = feq.getFirstItem();\r\n      feq.feq.forEach(reportItemInfo); \r\n      feq.reportSize();  \/\/should be 0\r\n      \r\n      if (itemList) {\r\n        var item = itemList[0];\r\n        item.entity.activate(item);\r\n        feq.feq.forEach(reportItemInfo); \r\n        feq.reportSize();  \/\/should be 1\r\n      } else {\r\n        keepRunning = false;\r\n      }\r\n    }  \/\/while (keepRunning)\r\n\r\n<\/pre>\n<p>I also had to modify the <code>getFirstItem<\/code> method in the <code>futureEventsQueue<\/code> object.  It has to test whether the returned array is of zero length, which indicates that the future events queue is empty.<\/p>\n<pre class=\"toolbar-overlay:false wrap:false height-set:true lang:default decode:true \">\r\n      this.getFirstItem = function() {\r\n        var feqItem = this.feq.splice(0,1);\r\n        if (feqItem.length) {\r\n          var t = feqItem[0].getActivationTime();\r\n          updateSimClock(t);\r\n          this.feqSize--;\r\n          return feqItem;\r\n        } else {\r\n          console.log(\"no items in FEQ to retrieve \"+this.feqSize)\r\n        }\r\n      };\r\n<\/pre>\n<p>Here&#8217;s the browser output, now that I&#8217;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.<\/p>\n<p><code><br \/>\n101 minutes<\/p>\n<p>entity 1 created at time 0<br \/>\nentity 1 updated at time 11<br \/>\nentity 1 updated at time 21<br \/>\nentity 1 updated at time 31<br \/>\nentity 1 updated at time 41<br \/>\nentity 1 updated at time 51<br \/>\nentity 1 updated at time 61<br \/>\nentity 1 updated at time 71<br \/>\nentity 1 updated at time 81<br \/>\nentity 1 updated at time 91<br \/>\nentity 1 terminated at time 101<br \/>\n<\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 &hellip; <a href=\"https:\/\/rpchurchill.com\/wordpress\/posts\/2016\/09\/01\/a-simple-discrete-event-simulation-part-4\/\">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\/947"}],"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=947"}],"version-history":[{"count":1,"href":"https:\/\/rpchurchill.com\/wordpress\/wp-json\/wp\/v2\/posts\/947\/revisions"}],"predecessor-version":[{"id":948,"href":"https:\/\/rpchurchill.com\/wordpress\/wp-json\/wp\/v2\/posts\/947\/revisions\/948"}],"wp:attachment":[{"href":"https:\/\/rpchurchill.com\/wordpress\/wp-json\/wp\/v2\/media?parent=947"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/rpchurchill.com\/wordpress\/wp-json\/wp\/v2\/categories?post=947"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/rpchurchill.com\/wordpress\/wp-json\/wp\/v2\/tags?post=947"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}