{"id":945,"date":"2016-08-31T22:02:33","date_gmt":"2016-09-01T03:02:33","guid":{"rendered":"http:\/\/rpchurchill.com\/?p=945"},"modified":"2017-02-17T10:09:03","modified_gmt":"2017-02-17T15:09:03","slug":"a-simple-discrete-event-simulation-part-3","status":"publish","type":"post","link":"https:\/\/rpchurchill.com\/wordpress\/posts\/2016\/08\/31\/a-simple-discrete-event-simulation-part-3\/","title":{"rendered":"A Simple Discrete-Event Simulation: Part 3"},"content":{"rendered":"<p>Here is an entity object that knows how to be created and activated from external calls.  It updates itself and puts itself back into the future events queue is appropriate.  This is in keeping with my <a href=\"https:\/\/rpchurchill.com\/wordpress\/posts\/2016\/01\/14\/discrete-event-simulation-looking-under-the-hood\/\">original concept<\/a> of how an entity would work.  The key, of course, is to keep rewriting and simplifying this thing until more and more of the mechanics are hidden from the coder.  Languages like <a href=\"http:\/\/www.wolverinesoftware.com\/SLXOverview.htm\">SLX<\/a> do this implicitly, but if we&#8217;re building a framework from scratch, given the limitations imposed by JavaScript, there&#8217;s only so far we&#8217;re going to be able to go with that.  Still, it&#8217;ll be fun to see how far we get.<\/p>\n<p>In the meantime the forms here mean I&#8217;ll have to streamline some of the existing code.  Since the state information is all kept within the entity itself it no longer has to be kept with the future events queue elements (since those elements contain a reference to the entity), so those data structures and calls will be pruned.<\/p>\n<p>I&#8217;ll make this all run tomorrow, but here&#8217;s the rough code for now:<\/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      \/\/initialize on creation\r\n      var this.entityID = entityID;\r\n      var this.initialTime = initialTime;\r\n      var this.incrementTime = incrementTime;\r\n      var this.endTime = endTime;\r\n      var this.nextState = \"increment\";\r\n      \/\/insert an event element into the FEQ\r\n      \/\/FEQ element has reference to this entity forever\r\n      feq.insertItem(initalTime,\"absolute\",this);\r\n      \/\/an external call will pull the element from the FEQ\r\n      \/\/\"item\" will be a reference to the FEQ element so it can be put back in if needed\r\n      this.activate = function(item) {\r\n        if (this.nextState == \"increment\") {\r\n          \/\/do stuff based on state\r\n          console.log(\"entity \"+this.entityID+\" updated at time \"+globalSimClock);\r\n          if (globalSimClock + this.incrementTime >= this.endTime) {\r\n            \/\/set item to termminate when appropriate\r\n            this.nextState = \"destroy\";\r\n          }\r\n          \/\/if item survives it goes back into the FEQ\r\n          item.update(this.incrementTime,\"advance\");\r\n          feq.insertExistingItem(item);\r\n        } else if (this.nextState == \"destroy\") {\r\n          \/\/if it doesn't put a reference back into the FEQ...\r\n          \/\/...it will eventually be garbage collected\r\n          console.log(\"entity \"+this.entityID+\" terminated at time \"+globalSimClock)\r\n        } else {\r\n          \/\/this, of course, shouldn't happen\r\n          alert(\"entity \"+this.entityID+\" went into undefined state\");\r\n        }\r\n      };  \/\/this.activate      \r\n    };  \/\/entity\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Here is an entity object that knows how to be created and activated from external calls. It updates itself and puts itself back into the future events queue is appropriate. This is in keeping with my original concept of how &hellip; <a href=\"https:\/\/rpchurchill.com\/wordpress\/posts\/2016\/08\/31\/a-simple-discrete-event-simulation-part-3\/\">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\/945"}],"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=945"}],"version-history":[{"count":3,"href":"https:\/\/rpchurchill.com\/wordpress\/wp-json\/wp\/v2\/posts\/945\/revisions"}],"predecessor-version":[{"id":1636,"href":"https:\/\/rpchurchill.com\/wordpress\/wp-json\/wp\/v2\/posts\/945\/revisions\/1636"}],"wp:attachment":[{"href":"https:\/\/rpchurchill.com\/wordpress\/wp-json\/wp\/v2\/media?parent=945"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/rpchurchill.com\/wordpress\/wp-json\/wp\/v2\/categories?post=945"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/rpchurchill.com\/wordpress\/wp-json\/wp\/v2\/tags?post=945"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}