A Simple Discrete-Event Simulation: Part 34

The exercise of writing out requirements and mulling them over yielded the desired insights about how to proceed. Today I’ll describe the external display mechanism, which blindly redraws all of the desired values with every global redraw event and in a manner entirely external to the simulation element.

Here’s the code for the individual display items. The addition here is the updateValue method, which takes the new input value as a parameter. Remembering that JavaScript lets you do some interesting things–while also being highly annoying and cumbersome–we see that the method is capable of handling inputs that are functions. If the input parameter is not a function then the parameter is assumed to be a value which can be assigned directly. If the input parameter is a function (that itself has no parameters, we can expand that capability later), then the method obtains the value returned by the function. Naturally this only works if the function passed as a parameter returns a usable value. This method works for simple functions but does not appear to work if you pass in a method call to an object. That is apparently too many layers of indirection. I have it as a note to explore this further.

The other thing the update does is check to see if the new value is different than the last value. If it is the method returns a value of true (as opposed to a default of false). This capability isn’t important for today’s work but will be for tomorrow’s.

Next up is a reworking of the DisplayGroup object. Since the mechanism must be completely external to any simulation element I went ahead and moved the definition of many of the parameters back to the function/constructor call itself (the define method is left in place for now). I also made the addValue method capable of accepting functions as arguments, with the same limitations described above. I also modified the object so that an empty string can be passed in for the label value, in which case the values will be drawn starting on the first line instead of the second.

Here is the streamlined code for the Queue component, from yesterday.

Here is how a queue component and all of the display mechanisms are instantiated.

Finally, the requisite calls to update and draw the values are made for every screen refresh. In theory these calls can be added to a global array like the components are, which would allow them to also be invoked in a loop.

As described previously, much of this overhead is due to the fact that JavaScript enforces passing simple data types by value, which means we have to go out of our way to provide updates. Beyond that we have to define everything about the external display mechanisms before using them.

Tomorrow I’ll expand these definitions to support display groups that are visible only for a limited time when triggered by a change in the displayed values.

This entry was posted in Simulation and tagged . Bookmark the permalink.

Leave a Reply