-
Recent Posts
Recent Comments
- R.P. Churchill on TWSL Series 07: Discovery and Data Collection
- R.P. Churchill on A Simulationist’s Framework for Business Analysis: Round Two
- LN on A Simulationist’s Framework for Business Analysis: Round Two
- R.P. Churchill on Starting to Learn About the Java Memory Model
- R.P. Churchill on Multidimensional Arrays in Javascript
Categories
Meta
October 2025 M T W T F S S 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
A Simple Discrete-Event Simulation: Part 80
I was working on other things today so the only thing I added to this project was the grid. It’s at a y-elevation of zero. More interestingly, it’s generation is automated, with the relevant code being shown below. Because floating-point numbers can be notoriously touchy after numerous operations I added in the smidge factor to ensure that end lines are always drawn and that the center lines can be found reliably. The center lines can be drawn in a different color if the number of divisions across the total width or length of the grid is even, meaning there will be an odd number of lines.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
function define3DLine(x1,y1,x2,y2,lineColor) { var gridMaterial = new THREE.LineBasicMaterial({ color: lineColor }); var geometry = new THREE.Geometry(); geometry.vertices.push( new THREE.Vector3(x1, 0, y1), new THREE.Vector3(x2, 0, y2) ); var line = new THREE.Line(geometry, gridMaterial); scene.add(line); } function defineReferenceGrid() { var smidge = 0.00000001; var xMin = global3DMinX; var xMax = global3DMaxX; var xInc = (xMax - xMin) / 26; xMin -= xInc; xMax += xInc; var xMiddle = (xMin + xMax) / 2.0; var zMin = global3DMinY; var zMax = global3DMaxY; var zInc = (zMax - zMin) / 3i6; zMin -= zInc; zMax += zInc; var zMiddle = (zMin + zMax) / 2.0; xMax += smidge; zMax += smidge; var gridLineColor; for (var z=zMin; z<=zMax; z+=zInc) { if ((z < zMiddle-smidge) || (z > zMiddle+smidge)) { gridLineColor = "#000088"; } else { //center line gridLineColor = "#880000"; } define3DLine(xMin,z,xMax,z,gridLineColor); } for (var x=xMin; x<=xMax; x+=xInc) { if ((x < xMiddle-smidge) || (x > xMiddle+smidge)) { gridLineColor = "#000088"; } else { gridLineColor = "#880000"; } define3DLine(x,zMin,x,zMax,gridLineColor); } } //defineReferenceGrid |
The grid fits to the layout so well because I make use of the fact that I had the boundary locations available since they’d been calculated to identify the center about which the camera flies and at which it looks. I extended the grid exactly one block in each direction. I wanted the blocks to be (roughly) square so I created more of them in the z-direction than in the x-direction. Last but not least the function can and should be parameterized instead of hard-coded like I’ve done here, but that’s a trivial matter.
I put the following scene together as part of preparations for a presentation. The code for generating the grid is a bit more primitive but this exercise drove me to learn how to modify the endpoints of existing lines as well as their color. In define3DLine
we specify the endpoints as a pair of vector objects pushed into an array called vertices
. If we want to modify those endpoints we simply dereference them in the other direction. The trick in this case is knowing you need to set the flag which lets the framework know the change needs to be processed internally. Some operation are straightforward — you just change and value and it takes effect (the line’s color works this way as we’ve already seen). In other cases you have to set the flag because the framework has to do something extra in response to the changes that wouldn’t happen naturally.
Always be on the lookout for such gotchas if the first thing you try doesn’t work the way you think it should. As it happens I found the verticesNeedUpdate
property when I was using the Firefox debugger to inspect the line I was trying to change. A quick search in the documentation told me that my hunch was correct. You know what they say: RTFM.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
//define a line in 3D function define3DLine(x1,y1,z1,x2,y2,z2,lineColor) { var gridMaterial = new THREE.LineBasicMaterial({ color: lineColor }); var geometry = new THREE.Geometry(); geometry.vertices.push( new THREE.Vector3(x1, y1, z1), new THREE.Vector3(x2, y2, z2) ); var line = new THREE.Line(geometry, gridMaterial); scene.add(line); return line; } function colorToHex(color) { //strip off hash character var c = color.slice(1,color.length); //if 3 convert to 6 if (c.length == 3) { c = c[0] + c[0] + c[1] + c[1] + c[2] + c[2]; } return "0x" + c; } //modify an existing line function update3DLine(segment, x1, y1, z1, x2, y2, z2, lineColor) { segment.geometry.vertices[0].x = x1; segment.geometry.vertices[0].y = y1; segment.geometry.vertices[0].z = z1; segment.geometry.vertices[1].x = x2; segment.geometry.vertices[1].y = y2; segment.geometry.vertices[1].z = z2; //set flag so new coordinates are used segment.geometry.verticesNeedUpdate = true; segment.material.color.setHex(colorToHex(lineColor)); } |
You can see the results here, in the lines showing where the camera is located as it orbits the scene. The camera does the same thing in the simulation code demos up to this point as well.
A Simple Discrete-Event Simulation: Part 79
The direct link to the latest version is here. Open this in a new window and try resizing it.
While preparing to give a presentation at the CharmCityJS Meetup this Wednesday I spent quite a bit of time investigating the setup of the 3D display, particularly in full screen mode. The first thing I had to do was make the full screen 3D display work again, since I inadvertently broke it when I updated the base HTML last week. Once that was done I set about making it work the way I want even as the user resizes the browser window.
Here’s the base HTML. In full screen 3D mode the normal components are all set to not display and a renderer canvas element is assigned to fill the entire window. Therefore a single wrapper is used to enclose all of the other elements.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<body id="body"> <div id="wrapper"> <canvas id="bcanimation" height="465px" width="420px">The HTML5 Canvas object is not supported on your browser.</canvas> <div class="button_row"> <button id="run_button" onmouseup="runClick()" style="height: 50px; width: 50px" disabled>Run</button> <button id="pause_button" onmouseup="pauseClick()" style="height: 50px; width: 50px" disabled>Pause</button> <button id="step_button" onmouseup="stepClick()" style="height: 50px; width: 154px" disabled>Step</button> <button id="reset_button" onmouseup="resetClick()" style="height: 50px; width: 50px" disabled>Reset</button> <button id="timeOps_button" onmouseup="toggleTimeOpsClick()" style="height: 50px; width: 50px" disabled>Time</button> <button id="multiplier_button" onmouseup="toggleMultiplier()" style="height: 50px; width: 50px; margin-right: 0;" disabled>x1</button> </div> <div id="window3D"></div> <div id="simClock"></div> <textarea id="simProgress"></textarea> </div> ... |
Here’s the code that sets up the 3D display. I originally controlled this with a single variable, virtual3DViewOnly
, but realized that making the display 3D and making it stereo/VR could easily be controlled with a separate setting. The stereoVR
setting is only used in three places, as it turns out.
If fullScreen3D
is set to true the script hides the standard components and attaches a dedicated, full-screen canvas to the window as described. If it is set to false is creates a renderer canvas of a fixed size and attaches it to the target div element. If the stereoVR flag is set then the renderer’s output is piped through an additional transform to produce the stereo display. There are a few other mechanics in place to support changing the size of the window interactively. These are mostly not necessary when not in full screen 3D mode.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
//*************************************************************************************** //*** basic 3D setup //*************************************************************************************** //var virtual3DViewOnly = true; //false; var fullScreen3D = true; var stereoVR = true; var renderer; var effect; var scene; var camera; var light; var fovBaseX; var fovBase = 75; function calcFovBaseX(angle,y) { var ang = angle / 180.0 * Math.PI; ang *= 0.5; var x = (y * 0.5) / Math.tan(ang); return x; } function calcFovAngle(height) { var hgt = height * 0.5; hgt /= fovBaseX; var angle = 2.0 * Math.atan(hgt); var ang = angle / Math.PI * 180.0; return ang; } //fovBaseX = calcFovBaseX(75,500); //var k = calcFovAngle(500); function initGraphics3D() { renderer = new THREE.WebGLRenderer({antialias: true, alpha: true}); //if (virtual3DViewOnly) { if (fullScreen3D) { var handleBody = document.getElementById("body"); handleBody.style.margin = "0px"; handleBody.style.overflow = "hidden"; //set wrapper div to not display var handleWrapper = document.getElementById("wrapper"); handleWrapper.style.display = "none"; //set the renderer to cover the entire screen renderer.setSize( window.innerWidth, window.innerHeight ); fovBaseX = calcFovBaseX(fovBase, window.innerHeight); document.body.appendChild( renderer.domElement ); if (stereoVR) { effect = new THREE.StereoEffect(renderer); } } else { renderer.setSize( 420,260 ); fovBaseX = calcFovBaseX(fovBase, 260); var window3D = document.getElementById("window3D"); window3D.appendChild( renderer.domElement ); } renderer.setClearColor("#000000",1); scene = new THREE.Scene(); if (fullScreen3D) { camera = new THREE.PerspectiveCamera( fovBase, window.innerWidth / window.innerHeight, 0.1, 1000 ); } else { camera = new THREE.PerspectiveCamera( fovBase, 420 / 260, 0.1, 1000 ); } light = new THREE.HemisphereLight( 0xeeeeee, 0x888888, 1 ); light.position.set( 0, 300, 0 ); scene.add( light ); } //initGraphics3D |
The other part of the setup involves adding a camera, which defines the viewing frustum. This is the transform that maps the internal 3D representation of objects to the 2D screen. It requires a field of view angle, an aspect ratio, and settings to clip near and far distances from the viewing point. There’s the relevant Three.js documentation. When the size of the window changes the aspect ratio and field of view angle have to change accordingly.
Here’s how changes to the window size are detected and how the camera parameters are updated. I included links to the pages where I found these methods.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
//*************************************************************************************** //*** handle resize events when doing fullScreen3D //*************************************************************************************** //from: http://stackoverflow.com/questions/641857/javascript-window-resize-event /* function: addEvent @param: obj (Object)(Required) - The object which you wish to attach your event to. @param: type (String)(Required) - The type of event you wish to establish. @param: callback (Function)(Required) - The method you wish to be called by your event listener. @param: eventReturn (Boolean)(Optional) - Whether you want the event object returned to your callback method. */ var addEvent = function(obj, type, callback, eventReturn) { if(obj == null || typeof obj === 'undefined') return; if(obj.addEventListener) obj.addEventListener(type, callback, eventReturn ? true : false); else if(obj.attachEvent) obj.attachEvent("on" + type, callback); else obj["on" + type] = callback; }; //An example call to the new addEvent function: var watch = function(evt) { //Older browser versions may return evt.srcElement //Newer browser versions should return evt.currentTarget var dimensions = { height: (evt.srcElement || evt.currentTarget).innerHeight, width: (evt.srcElement || evt.currentTarget).innerWidth }; //https://threejs.org/docs/api/cameras/PerspectiveCamera.html renderer.setSize(dimensions.width, dimensions.height); camera.aspect = dimensions.width / dimensions.height; var newFov = calcFovAngle(dimensions.height); camera.fov = newFov; camera.updateProjectionMatrix(); //you can change the aspect and fov, but must call this to make them effective }; if (fullScreen3D) { addEvent(window, 'resize', watch, true); } |
Setting the aspect ratio as the window size changes is straightforward but recalculating the field of view angle was trickier, and that’s where the calcFovBaseX
and calcFovAngle
functions come in. The field of view angle is given an initial value (of 75 degrees) in the fovBase
variable. This angle is applicable to the initial internal height of the window, in pixels. If the height of the window changes we have to back calculate the new field of view angle that keeps the display the same relative size. (Note that you might not want this behavior, but if you do you have to take steps like those I describe here.)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
function calcFovBaseX(angle,y) { var ang = angle / 180.0 * Math.PI; ang *= 0.5; var x = (y * 0.5) / Math.tan(ang); return x; } function calcFovAngle(height) { var hgt = height * 0.5; hgt /= fovBaseX; var angle = 2.0 * Math.atan(hgt); var ang = angle / Math.PI * 180.0; return ang; } //test to ensure operation is reversible fovBaseX = calcFovBaseX(75,500); //returns 325.8063432103014 var k = calcFovAngle(500); //returns 75! |
In order to keep the displayed scene a constant size we have to keep the viewing location the same distance from the scene. Consider the following figure:
We want to keep the distance (x) from the viewer to the scene constant as we change the height of the window (y), so we have to find the new angle (a2) to go along with a new window height (y2). In order to do that we need to find the constant distance (x) to use. Remembering that the tangent of an angle is the ratio of the height of a right triangle divided by the width of that triangle we know that:
tan( a1 / 2) = (0.5y1) / x
which means that
x = 0.5y1 / tan( a1 / 2).
We know the original values for y, and a1 so we can calculate x using the calcFovBaseX
function. Given a new y2 when the screen size changes, and using our constant x, we can find the new fov angle like this:
a2 = 2 atan( 0.5y2 / x)
This calculation is done in the calcFovAngle
function. Remember that we have to convert from degrees to radians and back again to do these calculations properly. A good check to make sure we get both functions right is to generate a base x value for a given field of view angle and window height, then feed that window height back into the second function to be sure it returns that same angle.
Once I got all this straightened out it worked like a charm, and it worked if we’re viewing one 3D image or doing so in stereo. Interestingly, the apparent size of the scene is determined based on the initial viewing frustum and the initial window size. The scene will appear to be different sizes based on the initial size of the window, and will not change even if the size of the window is changed. However, a scene initialized in a larger starting window will appear larger than one initialized in a smaller starting window. The apparent size of the scene is also a function of where the camera is located in the global 3D space, and we’ll experiment with that some tomorrow.
Until then, don’t let anyone tell you that trigonometry never comes up in your everyday life. đŸ™‚
Last but not least you may notice that the shape for the Combined component, (C21) is itself rotating about a y-axis drawn through its center. The locations of the components are all defined relative to their center points (on the x-z plane). If doing 3D transforms natively the procedure is to translate the object so the point you want to rotate around is at the origin of the global 3D space, perform the rotation (or scale or skew) operation, and then translate the object back to where you want it. The makers of Three.js know that’s kind of painful, so they hide all those mechanics from the user and allow the code to just say:
1 |
this.component3D.rotation.y += 0.01; |
In this example this.component
is a reference to one of the shape objects we created and rotation
is the total angle of rotation from the original orientation. The y
gives access to rotation about the y-axis individually.
A Simple Discrete-Event Simulation: Part 78
Today I did a bunch of work behind the scenes, starting with automatically determining the exclusivity of components based on their defined capacities, where appropriate. Queue, Process, Bag, and Combined components are exclusive if their governing capacities are finite and not exclusive if they are infinite. All other component types are always non-exclusive. It could be argued that Process components should never be defined to have infinite capacities when modeling any real system, since the whole purpose is to examine its finite constraints. The same might also be true of bag components. I should probably have the system throw a warning if a user specifies an infinite capacity for a Process or Bag component. Components that can operationally handle an infinite capacity are as a practical matter limited in two ways. One is by the amount of memory that can be allocated to store entities in internal queues and the other is by the number of entities that can be individually displayed.
Next I streamlined a bunch of the initializers for the graphic display components by defining a lot of default parameter values and embedding an assignment into a constructor. This was especially germane to the graphic characteristics of Path components, whose endpoints are usually determined automatically by the non-Path components they are connected to. I also removed references to the internal DisplayGroup mechanisms in the initialization code, but the mechanics remain in place within the component objects.
Finally, in preparation for doing something a little cleaner with the specification of internal entity locations, I redefined the internal arrays that store those locations to be multidimensional. The code is otherwise unchanged, and all locations are saved in master array zero. The goal is to be able to specify locations for different internal entity queues (e.g., parking and exit queues in Bag components, waiting and process queues in Combined components) in their own arrays rather than having to figure out which of the latter locations apply to entities in the second queue. This should also allow for a process to represent multiple, parallel operations, possibly each with their own sub-queues, but that is an idea for later.
The only visible change in the model is that I lowered the queue capacity in the Combined component down to two so I could test to ensure it handles exclusivity properly, which it does. You may have to reset and rerun a couple of times to see enough entities back up in that component, but it happens.
A Simple Discrete-Event Simulation: Part 77
Today I created a combined Queue-Process component I called a Combined component. You can see it in today’s version of the model as component 50, labeled C21, having replaced queue 21 and process 21. I suspect that this will become the default Process component once I have it cleaned up and a bit more bulletproof. I started with a Queue component and then added features until I had the behaviors I wanted. The main change is that the hand-off between the queue and process parts is handled internally and instantaneously. The mechanism for pulling from the previous component had to be adjust as well, as did the code to draw the 2D and 3D graphic displays.
Here’s the code for the new component:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 |
function CombinedComponent(traversalTime, processTime, processTimeSwitch, queueCapacity, processCapacity, routingTable) { if (typeof queueCapacity === "undefined") {queueCapacity = Infinity;} if (typeof processCapacity === "undefined") {processCapacity = Infinity;} if (typeof routingTable === "undefined") {routingTable = [1.0];} setOfComponents.push(this); this.componentID = getNewComponentID(); this.componentType = "Combined"; this.componentName = "Combined"; this.componentGroup = "Combined"; this.exclusive = false; this.routingMethod = 3; //1: one connection, 2: distribution, 3 routing this.previousComponentList = []; this.previousComponentCount = 0; this.nextComponentList = []; this.nextComponentCount = 0; this.nextComponentIDList = []; this.traversalTime = traversalTime; this.processTime = processTime; this.processTimeSwitch = processTimeSwitch; this.queueCapacity = queueCapacity; //negative or very large means infinite capacity and non-exclusive by default) this.processCapacity = processCapacity; this.savedDestination = -1; this.previousComponentIndex = 0; this.nextComponentIndex = 0; this.entityQueue = []; this.processQueue = []; this.routingTable = routingTable; this.openStatus = true; this.entryTime = ""; this.entryEntityID = ""; this.exitTime = ""; this.exitEntityID = ""; this.exitResidenceTime = ""; this.countInQueue = 0; this.countInTraversal = 0; this.countInProcessQueue = 0; this.countInProcess = 0; this.activity = ""; this.endEntryDisplayTime = 0; this.endExitDisplayTime = 0; this.endAllDisplayTime = 0; this.displayDelay = 0; this.graphic = null; this.reset = function() { this.savedDestination = -1; this.previousComponentIndex = this.previousComponentCount - 1; this.nextComponentIndex = this.nextComponentCount - 1; this.entityQueue = []; this.processQueue = []; this.openStatus = true; this.entryTime = ""; this.entryEntityID = ""; this.exitTime = ""; this.exitEntityID = ""; this.exitResidenceTime = ""; this.countInQueue = 0; this.countInTraversal = 0; this.activity = ""; this.endEntryDisplayTime = 0; this.endExitDisplayTime = 0; this.endAllDisplayTime = 0; }; this.assignPreviousComponent = function(prev) { this.previousComponentList.push(prev); this.previousComponentCount++; this.previousComponentIndex = this.previousComponentCount - 1; }; this.assignNextComponent = function(next) { //CombinedComponent this.nextComponentList.push(next); this.nextComponentCount++; this.nextComponentIndex = this.nextComponentCount - 1; next.assignPreviousComponent(this); }; this.getNextComponentIDs = function() { for (var i = 0; i < this.nextComponentCount; i++) { if (this.nextComponentList[i].getComponentType() != "Path") { this.nextComponentIDList[i] = this.nextComponentList[i].getComponentID(); } else { this.nextComponentIDList[i] = this.nextComponentList[i].passComponentID(); } } }; this.verifyLinks = function() { var i; var error = ""; if (this.nextComponentCount > 0) { for (i = 0; i < this.nextComponentCount; i++) { //> if (this.nextComponentList[i]) { //link exists if (typeof this.nextComponentList[i] === "object") { //link points to an object if ("componentType" in this.nextComponentList[i]) { //object contains member componentType if ((this.nextComponentList[i].componentType == "Arrivals") || (this.nextComponentList[i].componentType == "Entry")) { error += this.componentType + " comp. " + this.componentID + " next comp. list element " + i + " is not an allowed comp.\n"; } } else { //linked object does not contain member componentType error += this.componentType + " comp. " + this.componentID + " next comp. list item " + i + " does not have componentType\n"; } } else { //link points to something that is not an object error += this.componentType + " comp. " + this.componentID + " next comp. list item " + i + " is not an object\n"; } } else { //link that should exist does not error += this.componentType + " comp. " + this.componentID + " next comp. list item " + i + " does not exist\n"; } } } else { error += this.componentType + " comp. " + this.componentID + " has index of zero next components\n"; } if (this.previousComponentCount > 0) { for (i = 0; i < this.previousComponentCount; i++) { //> if (this.previousComponentList[i]) { //link exists if (typeof this.previousComponentList[i] === "object") { //link points to an object if ("componentType" in this.previousComponentList[i]) { //object contains member componentType if ((this.previousComponentList[i].componentType == "Arrivals") || (this.previousComponentList[i].componentType == "Exit")) { error += this.componentType + " comp. " + this.componentID + " previous comp. list element " + i + " is not an allowed comp.\n"; } } else { //linked object does not contain member componentType error += this.componentType + " comp. " + this.componentID + " previous comp. list item " + i + " does not have componentType\n"; } } else { //link points to something that is not an object error += this.componentType + " comp. " + this.componentID + " previous comp. list item " + i + " is not an object\n"; } } else { //link that should exist does not error += this.componentType + " comp. " + this.componentID + " previous comp. list item " + i + " does not exist\n"; } } } else { error += this.componentType + " comp. " + this.componentID + " has index of zero previous components\n"; } return error; }; this.getComponentID = function() { return this.componentID; }; this.getComponentType = function() { //CombinedComponent return this.componentType; }; this.getComponentName = function() { return this.componentName; }; this.setComponentName = function(componentName) { this.componentName = componentName; }; this.getComponentGroup = function() { return this.componentGroup; }; this.setComponentGroup = function(componentGroup) { this.componentGroup = componentGroup; addToGroupStatsNameListWrapper(componentGroup); }; this.getExclusive = function() { return this.exclusive; }; this.setExclusive = function(exclusive) { this.exclusive = exclusive; }; this.getTraversalTime = function() { return this.traversalTime; }; this.getProcessTime = function() { return this.processTime; }; this.getProcessTimeSwitch = function() { return this.processTimeSwitch; }; this.setProcessTimeSwitch = function(processTimeSwitch) { this.processTimeSwitch = processTimeSwitch; }; this.getQueueCapacity = function() { return this.queueCapacity; }; this.setQueueCapacity = function(queueCapacity) { this.queueCapacity = queueCapacity; }; this.getProcessCapacity = function() { return this.processCapacity; }; this.setProcessCapacity = function(processCapacity) { this.processCapacity = processCapacity; }; this.getOpenStatus = function() { return this.openStatus; }; this.setOpenStatus = function(openStatus) { this.openStatus = openStatus; }; this.getForwardAttemptTime = function() { if (this.countInProcessQueue > this.countInProcess) { return this.processQueue[this.countInProcessQueue - 1].getForwardAttemptTime(); } else { return Infinity; } }; this.getRoutingMethod = function() { return this.routingMethod; }; this.setRoutingMethod = function(routingMethod) { this.routingMethod = routingMethod; }; this.getEntryTime = function() { return this.entryTime; }; this.getEntryEntityID = function() { return this.entryEntityID; }; this.getExitTime = function() { return this.exitTime; }; this.getExitEntityID = function() { return this.exitEntityID; }; this.getExitResidenceTime = function() { return this.exitResidenceTime; }; this.getCountInQueue = function() { return this.countInQueue; }; this.getCountInTraversal = function() { return this.countInTraversal; }; this.getCountInProcessQueue = function() { return this.countInProcessQueue; }; this.getCountInProcess = function() { return this.countInProcess; }; this.getActivity = function() { return this.activity; }; this.getEndEntryDisplayTime = function() { return this.endEntryDisplayTime; }; this.getEndExitDisplayTime = function() { return this.endExitDisplayTime; }; this.getEndAllDisplayTime = function() { return this.endAllDisplayTime; }; this.dataGroup = new DisplayGroup1(); this.defineDataGroup = function(displayDelay, x, y, vw, bc, vc, lc) { this.displayDelay = displayDelay; this.dataGroup.define(this.componentID, this.componentType, x, y, vw, bc, vc, lc); }; this.dataGroup.addValue(this.entryEntityID, "Entry ID", "integer"); this.dataGroup.addValue(this.countInQueue, "# In Queue", "integer"); this.dataGroup.addValue(this.countInTraversal, "# Traversing", "integer"); this.dataGroup.addValue(this.maxCapacity, "Capacity", "integer"); this.dataGroup.addValue(this.exitEntityID, "Exit ID", "integer"); //this.dataGroup.addValue(this.exitResidenceTime,"Resdnce Tm","numdec",5); this.dataGroup.addValue(this.nextComponentIndex, "Next comp.", "integer"); this.dataGroup.addValue(this.activity, "Activity", "text"); this.dataGroup.addValue(this.getForwardAttemptTime(), "Fwd Attmpt", "numdec", 5); this.assignDisplayValues = function() { this.dataGroup.valueList[0].value = this.entryEntityID; this.dataGroup.valueList[1].value = this.countInQueue; this.dataGroup.valueList[2].value = this.countInTraversal; this.dataGroup.valueList[3].value = this.maxCapacity; this.dataGroup.valueList[4].value = this.exitEntityID; //this.dataGroup.valueList[5].value = this.exitResidenceTime; this.dataGroup.valueList[5].value = this.nextComponentIndex; this.dataGroup.valueList[6].value = this.activity; this.dataGroup.valueList[7].value = this.getForwardAttemptTime(); if (this.exclusive) { if (this.openStatus) { this.dataGroup.setBorderColor("#00FF00"); } else { this.dataGroup.setBorderColor("#FF0000"); } } }; this.drawData = function() { //Combined component this.assignDisplayValues(); this.dataGroup.drawBasic(); }; this.defineGraphic = function(graphic) { this.graphic = graphic; }; this.updateGraphic = function() { this.graphic.setTraverseValue(this.countInTraversal); this.graphic.setCountValue(this.countInQueue); }; this.isOpen = function() { //CombinedComponent if (this.exclusive) { if (this.currentCount() < (this.queueCapacity + this.processCapacity)) { this.openStatus = true; } else { this.openStatus = false; } for (var i = 0; i < this.previousComponentCount; i++) { if (this.previousComponentList[i].getComponentType() == "Path") { this.previousComponentList[i].setPreviousStatus(this.openStatus); //this may only be needed to determine open/closed status for display, count <=> capacity used when something is trying to enter } } } return this.openStatus; //if not exclusive should be set to true by default }; this.clearEntryDisplay = function() { //only clear display if a new one hasn't started a new timer if (globalSimClock >= this.endEntryDisplayTime) { this.entryTime = ""; this.entryEntityID = ""; } if (globalSimClock >= this.endAllDisplayTime) { this.activity = ""; } //displayProgressText("Queue entry "+this.componentID+" clears at time "+globalSimClock.toFixed(6)); }; this.clearExitDisplay = function() { //only clear display if a new one hasn't started a new timer if (globalSimClock >= this.endExitDisplayTime) { this.exitTime = ""; this.exitEntityID = ""; this.exitResidenceTime = ""; } if (globalSimClock >= this.endAllDisplayTime) { this.activity = ""; } //displayProgressText("Queue exit "+this.componentID+" clears at time "+globalSimClock.toFixed(6)); }; this.currentCount = function() { var count = 0; if (this.exclusive) { //start with entities already in component count = this.countInQueue + this.countInProcessQueue; //add entities in feeding paths for (var i = 0; i < this.previousComponentCount; i++) { if (this.previousComponentList[i].componentType == "Path") { //TODO: consider adding test for whether path is boundary component for associated exclusive group of components -- done this by inserting no-time/no-space control components count += this.previousComponentList[i].currentCount(); } } } return count; }; this.pullFromPrevious = function() { //CombinedComponent var oldest = this.previousComponentList[0].getForwardAttemptTime(); var oldestIndex = 0; for (var i = 1; i < this.previousComponentCount; i++) { var age = this.previousComponentList[i].getForwardAttemptTime(); if (age < oldest) { oldestIndex = i; } } if (this.previousComponentList[oldestIndex].getComponentType() != "Path") { if (this.previousComponentList[oldestIndex].getComponentType() != "Entry") { //TODO: this should call forward entity in a way that ensures that previous component only sends entity to where it is requested and if one is available and if this is a legitimate destination this.previousComponentList[oldestIndex].forwardEntity(this.componentID); } } else { displayProgressText("Combined comp. " + this.componentID + " pulls from previous at time " + globalSimClock.toFixed(6)); this.previousComponentList[oldestIndex].pullFromPrevious(this.componentID); } }; this.nextOpen = function() { var startIndex = this.nextComponentIndex; var tempIndex = startIndex; do { tempIndex++; if (tempIndex >= this.nextComponentCount) { tempIndex = 0; } if (this.nextComponentList[tempIndex].isOpen()) { //open link found, update and return nextComponentIndex return tempIndex; } } while (tempIndex != startIndex); return -1; //no open links found, leave nextComponentIndex unchanged }; this.queueToProcess = function() { if (this.countInQueue > this.countInTraversal) { //an entity is available to move var entity = this.entityQueue.pop(); this.countInQueue--; this.processQueue.unshift(entity); this.countInProcessQueue++; this.countInProcess++; var pTime = this.processTime[entityProcessTimeIndex(entity,this.processTimeSwitch)]; advance(pTime, this, "processComplete", entity); //should be open now if (this.exclusive) { if (!this.openStatus) { displayProgressText("Combined comp. " + this.componentID + " calls pull from previous at time " + globalSimClock.toFixed(6)); this.pullFromPrevious(); //TODO: call this with a modest (~1 time unit) delay to account for reaction time? //may or may not successfully get an entity but should always be called } } } }; this.traverseComplete = function() { this.countInTraversal--; //TODO: ensure handled properly if traversal time is zero //figure out which entity just finished traversing var tempID = this.entityQueue[this.countInTraversal].entityID; //TODO: this works for FIFO, but not necessarily for other logics //this.entityQueue[this.countInTraversal].setForwardAttemptTime(globalSimClock); displayProgressText("Queue comp. " + this.componentID + " entity: " + tempID + " trav. at time " + globalSimClock.toFixed(6)); if (this.countInQueue - this.countInTraversal == 1) { //don't bother trying to forward unless this is the lead item in the queue if (this.countInProcessQueue < this.processCapacity) { //is there space in the process queue this.queueToProcess(); } } }; this.processComplete = function() { this.countInProcess--; //figure out which entity just finished processing var tempID = this.processQueue[this.countInProcess].entityID; //TODO: this works for FIFO, but not necessarily for other logics this.processQueue[this.countInProcess].setForwardAttemptTime(globalSimClock); displayProgressText("Combined comp. " + this.componentID + " entity: " + tempID + " processed at " + globalSimClock.toFixed(6)); if (this.countInProcessQueue - this.countInProcess == 1) { //don't bother trying to forward unless this is the lead item in the queue this.forwardEntity(); } }; this.forwardEntity = function(destIndex) { //CombinedComponent if (typeof destIndex === "undefined") {destIndex = -1;} var dest = -1; if (destIndex >= 0) { //pull request from a specific downstream component, must send entity there if (this.routingMethod == 1) { //single connection, nothing to do dest = 0; } else if (this.routingMethod == 2) { //distribution, send to any request dest = 0; while ((this.nextComponentIDList[dest] != destIndex) && (dest < this.nextComponentCount)) { //second test should not be needed, loop can't fail to return valid result dest++; } } else if (this.routingMethod == 3) { //model routing logic, TODO: don't forward if not desired destination dest = 0; while ((this.nextComponentIDList[dest] != destIndex) && (dest < this.nextComponentCount)) { //second test should not be needed, loop can't fail to return valid result dest++; } } dummy2 = 0; } else { if (this.routingMethod == 1) { //single connection if (this.nextComponentList[0].isOpen()) { dest = 0; } } else if (this.routingMethod == 2) { //distribution var nextIndex = this.nextOpen(); if (nextIndex >= 0) { dest = nextIndex; //this.nextComponentIndex = dest; } } else if (this.routingMethod == 3) { //model routing logic if (this.savedDestination >= 0) { dest = this.savedDestination; } else { dest = 0; var test = Math.random(); //need access to entity type but can't pop it off queue here var index = this.countInQueue - 1; if (index >= 0) { index = entityDiversionPercentIndex(this.entityQueue[index]); //get head item in queue and find out what type it is } else { index = 0; //nothing in queue, following code will work but nothing will be popped and processed below } while (test > this.routingTable[index][dest]) { dest++; } if (dest <= this.nextComponentCount) { if (!this.nextComponentList[dest].isOpen()) { dest = -1; //this works only because destinations are unique (i.e., can't send to multiple parallel types/processes, must send to a single queue feeding them) } } else { alert("Queue comp. tried to assign destination with too high of an index") } if (dest >= 0) { this.savedDestination = dest; } } } else { //0 uninitialized or anything else alert("comp. " + this.componentID + " incorrect routing method: " + this.routingMethod); } } if (dest >= 0) { if (this.countInProcessQueue > this.countInProcess) { var entity = this.processQueue.pop(); //TODO-: are we testing to ensure the next entity is really available //calculate how long item was in queue this.exitResidenceTime = globalSimClock - entity.getLocalEntryTime(); //now use this to calculate stats for the interval this.exitTime = globalSimClock; this.exitEntityID = entity.entityID; this.activity = "forward entity"; this.endExitDisplayTime = globalSimClock + this.displayDelay; this.endAllDisplayTime = this.endExitDisplayTime; advance(this.displayDelay, this, "clearExitDisplay"); displayProgressText("Combined comp. " + this.componentID + " forwards entity: " + this.exitEntityID + " at time " + globalSimClock.toFixed(6)); this.countInProcessQueue--; this.queueToProcess(); this.isOpen(); this.nextComponentIndex = dest; this.savedDestination = -1; this.nextComponentList[dest].receiveEntity(entity); //record stats if (this.nextComponentList[dest].getComponentGroup() != entity.getComponentGroup()) { //truly leaving a component group along the current connection recordGroupStatsWrapper(this.componentGroup, entity.getComponentGroupEntryTime(), entity); } } } }; this.receiveEntity = function(entity) { //CombinedComponent //receive the entity entity.setLocalEntryTime(); //record time entity entered queue if (entity.getComponentGroup() != this.componentGroup) { entity.setComponentGroup(this.componentGroup); entity.setComponentGroupEntryTime(globalSimClock); recordGroupStatsSystemEntryWrapper(this.componentGroup,entity); } entity.setForwardAttemptTime(Infinity); entity.setPermission(false); //entity has reached end of related components group, permission no longer matters this.entityQueue.unshift(entity); this.countInQueue++; this.countInTraversal++; //TODO: ensure handled properly if traversal time is zero this.isOpen(); //display what was done this.entryTime = globalSimClock; this.entryEntityID = entity.entityID; this.activity = "receive entity"; //set timer to clear the display after a bit this.endEntryDisplayTime = globalSimClock + this.displayDelay; this.endAllDisplayTime = this.endEntryDisplayTime; advance(this.displayDelay, this, "clearEntryDisplay"); displayProgressText("Combined comp. " + this.componentID + " receives entity: " + this.entryEntityID + " at time " + globalSimClock.toFixed(6)); if (this.traversalTime > 0) { //set timer for the new entity to track its traversal time advance(this.traversalTime, this, "traverseComplete"); //TODO: don't bother if traversal time is zero, also insure countInTraversal is decremented (or not incremented) as needed } else if (this.traversalTime == 0.0) { this.traverseComplete(); } }; this.activate = function(nextState) { if (nextState == "clearEntryDisplay") { this.clearEntryDisplay(); } else if (nextState == "clearExitDisplay") { this.clearExitDisplay(); } else if (nextState == "traverseComplete") { this.traverseComplete(); } else if (nextState == "processComplete") { this.processComplete(); } else { errorUndefinedAdvanceState(this.entityID, this.nextState); } }; //this.activate } //CombinedComponent |
I see numerous opportunities for compressing and streamlining things as I continue to work with this code. One of the insights is that any Queue or Process with a finite capacity is effectively exclusive, so I can probably automate the setting of that value for each component. That will be one of the things I work on tomorrow. I’m also thinking of modifying the way the entities are drawn in the various components. The current code works but is a bit twisty and opaque, so it could stand some clarification and regularizing.
I also fixed the 3D display mechanisms for the entities in the Bag and Combined components.
A Simple Discrete-Event Simulation: Part 76
Today I added a series of Queue and Process components into the pathway traversed by the sub-entities leaving and returning to the Bag component. I found one change I had to make, which was adding the ability of the ReturnFromSubJourney component to return its parent component’s componentGroup
value. This was necessary so entities could test to see whether they were leaving the component groups represented by the two new Queue-Process pairs.
I changed the routing to divert more entities over to the Bag component to exercise the added components. I then found that the number of entities in the system exceeded the maximum number of 3D entity objects I’d allocated (which halted the simulation), so I changed the maximum from 35 to 50.
Everything else worked perfectly, including the reporting. It added report sections for “Ped Primary” and “Ped Secondary” (short for Pedestrian Primary and Secondary, or Queue and Process 20 and Queue and Process 21, respectively). I was originally wondering if it would create new sections of reports for the spawned entities but the way I had it set up they retained the Process Speed and Residency properties of their parent entities and the Queue and Process components were set to use those properties.
It suddenly occurred to me how to make a combined Queue-Process component, and also a process component with multiple, parallel processing positions. Both of these ideas had been on my To Do list for a while, so that’s what I’ll work on tomorrow. Such a component will greatly compress and simplify the specification and layout of a simulation system.
Last but not least I seriously reworked the basic HTML and CSS configuration.
A Simple Discrete-Event Simulation: Part 75
Today I expanded the capability of the Bag component to allow entities to reside there, spawn new sub-entities that undergo an external process of their own, and return to the “parked” entity. Delay times can be specified that control how long it takes the spawned sub-entity to start moving once the main entity parks, and how long it takes the before main entity transfers to the exit queue once the sub-entity returns. Think of the delays like this. You drive into a parking lot and park. Then you gather up your belongings, paperwork, and do whatever else you might have to before walking in to your next activity. The activity has its own duration, at which point you walk back to your parked car. Then you put all your stuff back in the car, get settled as you need to, and you finally drive out of the parking lot. The spawned entity is, in our example, colored orange. When it is waiting at the main entity within the Bag component is is displayed on top of the main entity that spawned it, a handful of pixels up and to the right.
The value for the Bag component’s process time has no meaning in this case, though perhaps it should double as one or both of the delay times.
Two new components were created to serve as the internal “entry” into the sub-process and “exit” back out of the sub-process. They have as little of the machinery common to the other component types as possible. When entities are forwarded to them they are immediately forwarded to their next component. The source component can only connect to a single, downstream component, but multiple upstream components can be connected to the destination component.
The external system can be made up of any types of components (except Entry or Exit components) but I’ve included only two paths so I didn’t have to think about reporting on top of everything else I did today. That will be tomorrow’s project. The process of traversing an external system is referred to as a “sub-journey” and the new components and processes are named accordingly.
Here’s the updated code for the Bag component:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 |
function BagComponent(processTime, maxCapacity, routingTable, spawnFlag, spawnArriveDelayTime, spawnDepartDelayTime) { if (typeof maxCapacity === "undefined") {maxCapacity = 1;} if (typeof routingTable === "undefined") {routingTable = [1.0];} if (typeof spawnFlag === "undefined") {spawnFlag = false;} if (typeof spawnArriveDelayTime === "undefined") {spawnArriveDelayTime = 0.0;} if (typeof spawnDepartDelayTime === "undefined") {spawnDepartDelayTime = 0.0;} //generally exclusive, should always be fed by a queue or at least "protected" by a status-based diversion component setOfComponents.push(this); this.componentID = getNewComponentID(); this.componentType = "Bag"; this.componentName = "Bag"; this.componentGroup = "Bag"; this.exclusive = true; this.routingMethod = 1; //1: one connection, 2: distribution, 3 routing this.previousComponentList = []; this.previousComponentCount = 0; this.nextComponentList = []; this.nextComponentCount = 0; this.nextComponentIDList = []; this.processTime = processTime; //this.processTimeSwitch = processTimeSwitch; this.maxCapacity = maxCapacity; this.savedDestination = -1; this.previousComponentIndex = 0; this.nextComponentIndex = 0; this.entityQueue = []; this.subEntityQueue = []; for (var i=0; i<maxCapacity; i++) { this.entityQueue[i] = null; this.subEntityQueue[i] = null; } this.exitQueue = []; this.routingTable = routingTable; this.openStatus = true; this.entryTime = ""; this.entryEntityID = ""; this.exitTime = ""; this.exitEntityID = ""; this.exitResidenceTime = ""; this.countInBag = 0; this.countInProcess = 0; this.activity = ""; this.endEntryDisplayTime = 0; this.endExitDisplayTime = 0; this.endAllDisplayTime = 0; this.displayDelay = 0; this.graphic = null; this.spawnFlag = spawnFlag; this.spawnArriveDelayTime = spawnArriveDelayTime; this.spawnDepartDelayTime = spawnDepartDelayTime; this.startSubComponent = null; //TODO: verify that this is assigned if spawnFlag set to true this.returnSubComponent = null; //TODO: verify that this is assigned if spawnFlag set to true this.reset = function() { this.previousComponentIndex = this.previousComponentCount - 1; this.nextComponentIndex = this.nextComponentCount - 1; this.entityQueue = []; for (var i=0; i<maxCapacity; i++) { this.entityQueue[i] = null; } this.exitQueue = []; this.openStatus = true; this.savedDestination = -1; this.entryTime = ""; this.entryEntityID = ""; this.exitTime = ""; this.exitEntityID = ""; this.exitResidenceTime = ""; this.countInBag = 0; this.countInProcess = 0; this.activity = ""; this.endEntryDisplayTime = 0; this.endExitDisplayTime = 0; this.endAllDisplayTime = 0; }; this.assignPreviousComponent = function(prev) { //TODO-: implement code that makes this actually work this.previousComponentList.push(prev); this.previousComponentCount++; this.previousComponentIndex = this.previousComponentCount - 1; //TODO-: assign this automatically when upstream link and exclusive paths are required? }; this.assignNextComponent = function(next) { //BagComponent this.nextComponentList.push(next); this.nextComponentCount++; this.nextComponentIndex = this.nextComponentCount - 1; next.assignPreviousComponent(this); //TODO-: automatically assign upstream link if downstream component is exclusive? }; this.assignStartSubJourneyComponent = function(startComp) { //next component must be non-exclusive, should be a StartSubJourneyComponent this.startSubComponent = startComp; //TODO: verify that this is assigned if spawnFlag set to true }; this.assignReturnFromSubJourneyComponent = function(returnComp) { this.returnSubComponent = returnComp; //TODO: verify that this is assigned if spawnFlag set to true }; this.verifyLinks = function() { var i; var error = ""; if (this.nextComponentCount > 0) { for (i = 0; i < this.nextComponentCount; i++) { //> if (this.nextComponentList[i]) { //link exists if (typeof this.nextComponentList[i] === "object") { //link points to an object if ("componentType" in this.nextComponentList[i]) { //object contains member componentType if ((this.nextComponentList[i].componentType == "Arrivals") || (this.nextComponentList[i].componentType == "Entry")) { error += this.componentType + " comp. " + this.componentID + " next comp. list element " + i + " is not an allowed comp.\n"; } } else { //linked object does not contain member componentType error += this.componentType + " comp. " + this.componentID + " next comp. list item " + i + " does not have componentType\n"; } } else { //link points to something that is not an object error += this.componentType + " comp. " + this.componentID + " next comp. list item " + i + " is not an object\n"; } } else { //link that should exist does not error += this.componentType + " comp. " + this.componentID + " next comp. list item " + i + " does not exist\n"; } } } else { error += this.componentType + " comp. " + this.componentID + " has index of zero next components\n"; } if (this.previousComponentCount > 0) { for (i = 0; i < this.previousComponentCount; i++) { //> if (this.previousComponentList[i]) { //link exists if (typeof this.previousComponentList[i] === "object") { //link points to an object if ("componentType" in this.previousComponentList[i]) { //object contains member componentType if ((this.previousComponentList[i].componentType == "Arrivals") || (this.previousComponentList[i].componentType == "Exit")) { error += this.componentType + " comp. " + this.componentID + " previous comp. list element " + i + " is not an allowed comp.\n"; } } else { //linked object does not contain member componentType error += this.componentType + " comp. " + this.componentID + " previous comp. list item " + i + " does not have componentType\n"; } } else { //link points to something that is not an object error += this.componentType + " comp. " + this.componentID + " previous comp. list item " + i + " is not an object\n"; } } else { //link that should exist does not error += this.componentType + " comp. " + this.componentID + " previous comp. list item " + i + " does not exist\n"; } } } else { error += this.componentType + " comp. " + this.componentID + " has index of zero previous components\n"; } return error; }; this.getNextComponentIDs = function() { for (var i = 0; i < this.nextComponentCount; i++) { if (this.nextComponentList[i].getComponentType() != "Path") { this.nextComponentIDList[i] = this.nextComponentList[i].getComponentID(); } else { this.nextComponentIDList[i] = this.nextComponentList[i].passComponentID(); } } }; this.getComponentID = function() { return this.componentID; }; this.getComponentType = function() { //BagComponent return this.componentType; }; this.getComponentName = function() { return this.componentName; }; this.setComponentName = function(componentName) { this.componentName = componentName; }; this.getComponentGroup = function() { return this.componentGroup; }; this.setComponentGroup = function(componentGroup) { this.componentGroup = componentGroup; addToGroupStatsNameListWrapper(componentGroup); }; this.getExclusive = function() { return this.exclusive; }; this.setExclusive = function(exclusive) { this.exclusive = exclusive; }; this.getProcessTime = function() { return this.processTime; }; //this.getProcessTimeSwitch = function() { // return this.processTimeSwitch; //}; //this.setProcessTimeSwitch = function(processTimeSwitch) { // this.processTimeSwitch = processTimeSwitch; //}; this.getMaxCapacity = function() { return this.maxCapacity; }; this.setMaxCapacity = function(maxCapacity) { this.maxCapacity = maxCapacity; }; this.getOpenStatus = function() { return this.openStatus; }; this.setOpenStatus = function(openStatus) { this.openStatus = openStatus; }; this.getForwardAttemptTime = function() { if (this.exitQueue.length > 0) { return this.exitQueue[this.exitQueue.length - 1].getForwardAttemptTime(); } else { return Infinity; } }; this.getRoutingMethod = function() { return this.routingMethod; }; this.setRoutingMethod = function(routingMethod) { this.routingMethod = routingMethod; }; this.getEntryTime = function() { return this.entryTime; }; this.getEntryEntityID = function() { return this.entryEntityID; }; this.getExitTime = function() { return this.exitTime; }; this.getExitEntityID = function() { return this.exitEntityID; }; this.getExitResidenceTime = function() { return this.exitResidenceTime; }; this.getCountInBag = function() { return this.countInBag; }; this.getCountInProcess = function() { return this.countInProcess; }; this.getActivity = function() { return this.activity; }; this.getEndEntryDisplayTime = function() { return this.endEntryDisplayTime; }; this.getEndExitDisplayTime = function() { return this.endExitDisplayTime; }; this.getEndAllDisplayTime = function() { return this.endAllDisplayTime; }; this.dataGroup = new DisplayGroup1(); this.defineDataGroup = function(displayDelay, x, y, vw, bc, vc, lc) { this.displayDelay = displayDelay; this.dataGroup.define(this.componentID, this.componentType, x, y, vw, bc, vc, lc); }; this.dataGroup.addValue(this.entryEntityID, "Entry ID", "integer"); this.dataGroup.addValue(this.countInBag, "# In Bag", "numdec", "integer"); this.dataGroup.addValue(this.exitEntityID, "Exit ID", "integer"); this.dataGroup.addValue(this.exitResidenceTime, "Resdnce Tm", "numdec", 5); this.dataGroup.addValue(this.activity, "Activity", "text"); this.assignDisplayValues = function() { this.dataGroup.valueList[0].value = this.entryEntityID; this.dataGroup.valueList[1].value = this.countInBag; this.dataGroup.valueList[2].value = this.exitEntityID; this.dataGroup.valueList[3].value = this.exitResidenceTime; this.dataGroup.valueList[4].value = this.activity; if (this.exclusive) { if (this.openStatus) { this.dataGroup.setBorderColor("#00FF00"); } else { this.dataGroup.setBorderColor("#FF0000"); } } }; this.drawData = function() { //BagComponent this.assignDisplayValues(); this.dataGroup.drawBasic(); }; this.defineGraphic = function(graphic) { this.graphic = graphic; }; this.updateGraphic = function() { this.graphic.setTraverseValue(this.countInProcess); this.graphic.setCountValue(this.countInBag); //if (this.exclusive) { // if (this.openStatus) { // this.graphic.setBorderColor("#00FF00"); // } else { // this.graphic.setBorderColor("#FF0000"); // } //} }; this.isOpen = function() { //BagComponent if (this.exclusive) { if (this.currentCount() < this.maxCapacity) { this.openStatus = true; } else { this.openStatus = false; } for (var i = 0; i < this.previousComponentCount; i++) { if (this.previousComponentList[i].getComponentType() == "Path") { this.previousComponentList[i].setPreviousStatus(this.openStatus); //this may only be needed to determine open/closed status for display, count <=> capacity used when something is trying to enter } } } return this.openStatus; //if not exclusive should be set to true by default }; this.clearEntryDisplay = function() { //only clear display if a new one hasn't started a new timer if (globalSimClock >= this.endEntryDisplayTime) { this.entryTime = ""; this.entryEntityID = ""; } if (globalSimClock >= this.endAllDisplayTime) { this.activity = ""; } //displayProgressText("Bag entry "+this.componentID+" clears at time "+globalSimClock.toFixed(6)); }; this.clearExitDisplay = function() { //only clear display if a new one hasn't started a new timer if (globalSimClock >= this.endExitDisplayTime) { this.exitTime = ""; this.exitEntityID = ""; this.exitResidenceTime = ""; } if (globalSimClock >= this.endAllDisplayTime) { this.activity = ""; } //displayProgressText("Bag exit "+this.componentID+" clears at time "+globalSimClock.toFixed(6)); }; this.currentCount = function() { var count = 0; if (this.exclusive) { //start with entities already in component count = this.countInBag; //add entities in feeding paths for (var i = 0; i < this.previousComponentCount; i++) { if (this.previousComponentList[i].componentType == "Path") { //TODO- consider adding test for whether path is boundary component for associated exclusive group of components //do this using no-time/no-space control component to define boundary count += this.previousComponentList[i].currentCount(); } } } return count; }; this.pullFromPrevious = function() { //BagComponent var oldest = this.previousComponentList[0].getForwardAttemptTime(); var oldestIndex = 0; for (var i = 1; i < this.previousComponentCount; i++) { var age = this.previousComponentList[i].getForwardAttemptTime(); if (age < oldest) { oldestIndex = i; } } if (this.previousComponentList[oldestIndex].getComponentType() != "Path") { if (this.previousComponentList[oldestIndex].getComponentType() != "Entry") { //TODO: this should call forward entity in a way that ensures that previous component only sends entity to where it is requested and if one is available and if this is a legitimate destination this.previousComponentList[oldestIndex].forwardEntity(this.componentID); } } else { displayProgressText("Bag comp. " + this.componentID + " pulls from previous (" + oldestIndex + ") at time " + globalSimClock.toFixed(6)); this.previousComponentList[oldestIndex].pullFromPrevious(this.componentID); } }; this.nextOpen = function() { var startIndex = this.nextComponentIndex; var tempIndex = startIndex; do { tempIndex++; if (tempIndex >= this.nextComponentCount) { tempIndex = 0; } if (this.nextComponentList[tempIndex].isOpen()) { //open link found, update and return nextComponentIndex return tempIndex; } } while (tempIndex != startIndex); return -1; //no open links found, leave nextComponentIndex unchanged }; this.processComplete = function(entity) { //BagComponent this.countInProcess--; //TODO: ensure handled properly if process time is zero //prob. not applicable //figure out which entity just finished processing var tempID = entity.entityID; this.exitQueue.unshift(entity); this.entityQueue[entity.getLocalIndex()] = null; entity.setForwardAttemptTime(globalSimClock); displayProgressText("Bag comp. " + this.componentID + " entity: " + tempID + " processed at " + globalSimClock.toFixed(6)); this.forwardEntity(); //try to forward it }; this.startSubJourney = function(subEntity) { this.startSubComponent.receiveEntity(subEntity); this.subEntityQueue[subEntity.parentEntity.getLocalIndex()] = null; }; this.continueFromSubJourney = function(subEntity) { this.subEntityQueue[subEntity.parentEntity.getLocalIndex()] = null; this.processComplete(subEntity.parentEntity); removeEntityFromList(subEntity.getEntityID()); //be sure to destroy the subEntity }; this.returnFromSubJourney = function(subEntity) { this.subEntityQueue[subEntity.parentEntity.getLocalIndex()] = subEntity; if (this.spawnDepartDelayTime > 0.0) { advance(this.spawnDepartDelayTime, this, "endSubDepartDelay", subEntity); } else { this.continueFromSubJourney(subEntity); } }; //##default parameters## //this.forwardEntity = function(destIndex = -1) { //BagComponent //this.forwardEntity = function() { //BagComponent ///var routingTable = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : -1; this.forwardEntity = function(destIndex) { //BagComponent if (typeof destIndex === "undefined") {destIndex = -1;} var dest = -1; if (destIndex >= 0) { //pull request from a specific downstream component, must send entity there if (this.routingMethod == 1) { //single connection, nothing to do dest = 0; } else if (this.routingMethod == 2) { //distribution, send to any request dest = 0; while ((this.nextComponentIDList[dest] != destIndex) && (dest < this.nextComponentCount)) { //second test should not be needed, loop can't fail to return valid result dest++; } } else if (this.routingMethod == 3) { //model routing logic, TODO: don't forward if not desired destination dest = 0; while ((this.nextComponentIDList[dest] != destIndex) && (dest < this.nextComponentCount)) { //second test should not be needed, loop can't fail to return valid result dest++; } } dummy2 = 0; } else { if (this.routingMethod == 1) { //single connection if (this.nextComponentList[0].isOpen()) { dest = 0; } } else if (this.routingMethod == 2) { //distribution var nextIndex = this.nextOpen(); if (nextIndex >= 0) { dest = nextIndex; //this.nextComponentIndex = dest; } } else if (this.routingMethod == 3) { //model routing logic if (this.savedDestination >= 0) { dest = this.savedDestination; } else { dest = 0; var test = Math.random(); //need access to entity type but can't pop it off queue here var index = this.countInQueue - 1; if (index >= 0) { index = entityDiversionPercentIndex(this.entityQueue[index]); //get head item in queue and find out what type it is } else { index = 0; //nothing in queue, following code will work but nothing will be popped and processed below } while (test > this.routingTable[index][dest]) { dest++; } if (dest <= this.nextComponentCount) { if (!this.nextComponentList[dest].isOpen()) { dest = -1; } } else { alert("Bag comp. tried to assign destination with too high of an index") } if (dest >= 0) { this.savedDestination = dest; //ensure that once destination is determined for this entity that we don't keep changing it } } } else { //0 uninitialized or anything else alert("comp. " + this.componentID + " incorrect routing method: " + this.routingMethod); } } if (dest >= 0) { if (this.countInBag > this.countInProcess) { var entity = this.exitQueue.pop(); //TODO-: are we testing to ensure the next entity is really available if (entity) { //TODO-: since we've tested above this should not be necessary //calculate how long item was in process this.exitResidenceTime = globalSimClock - entity.getLocalEntryTime(); this.exitTime = globalSimClock; this.exitEntityID = entity.entityID; this.activity = "forward entity"; this.endExitDisplayTime = globalSimClock + this.displayDelay; this.endAllDisplayTime = this.endExitDisplayTime; advance(this.displayDelay, this, "clearExitDisplay"); displayProgressText("Bag comp. " + this.componentID + " forwards entity: " + this.exitEntityID + " at time " + globalSimClock.toFixed(6)); this.countInBag--; //should be open now if (this.exclusive) { displayProgressText("Bag comp. " + this.componentID + " calls pull from previous at time " + globalSimClock.toFixed(6)); if (!this.openStatus) { this.pullFromPrevious(); //TODO: call this with a modest (~1 sec) delay to account for reaction time? //may or may not successfully get an entity but should always be called } } this.isOpen(); this.nextComponentIndex = dest; this.savedDestination = -1; //clear old choice when entity successfully forwarded this.nextComponentList[dest].receiveEntity(entity); //record stats if (this.nextComponentList[dest].getComponentGroup() != entity.getComponentGroup()) { //truly leaving a component group along the current connection recordGroupStatsWrapper(this.componentGroup, entity.getComponentGroupEntryTime(), entity); } } } } }; this.receiveEntity = function(entity) { //BagComponent //receive the entity entity.setLocalEntryTime(); //record time entity entered bag if (entity.getComponentGroup() != this.componentGroup) { entity.setComponentGroup(this.componentGroup); entity.setComponentGroupEntryTime(globalSimClock); recordGroupStatsSystemEntryWrapper(this.componentGroup,entity); } //figure out which parking space to use var i = 0; while (this.entityQueue[i] != null) { i++; } if (i < this.maxCapacity) { this.entityQueue[i] = entity; entity.setLocalIndex(i); } else { //this shouldn't happen alert("Bag comp. "+this.componentID+" over capacity at time "+this.globalSimClock.toFixed(6)); } entity.setForwardAttemptTime(Infinity); //TODO: figure out how to handle this entity.setPermission(false); //entity has reached end of related components group, permission no longer matters this.countInProcess++; this.countInBag++; //TODO: handle if process time is zero? this.isOpen(); //display what was done this.entryTime = globalSimClock; this.entryEntityID = entity.entityID; this.activity = "receive entity"; //set timer to clear the display after a bit this.endEntryDisplayTime = globalSimClock + this.displayDelay; this.endAllDisplayTime = this.endEntryDisplayTime; advance(this.displayDelay, this, "clearEntryDisplay"); //set timer for the process duration // var pTime = this.processTime[entityProcessTimeIndex(entity,this.processTimeSwitch)]; if (!this.spawnFlag) { var pTime = processTime(entity); advance(pTime, this, "processComplete",entity); } else { //spawn subordinate entity var subEntity = generateNewEntity(entity); //reference to parent entity assignEntityTypesSub(subEntity); this.subEntityQueue[i] = subEntity; recordGroupStatsSystemEntryWrapper("SubSystem",subEntity); if (this.spawnArriveDelayTime > 0.0) { advance(this.spawnArriveDelayTime, this, "endSubArriveDelay", subEntity); } else { this.startSubJourney(subEntity); } } displayProgressText("Bag comp. " + this.componentID + " receives entity: " + this.entryEntityID + " at time " + globalSimClock.toFixed(6)); }; this.activate = function(nextState, entity2) { if (nextState == "clearEntryDisplay") { this.clearEntryDisplay(); } else if (nextState == "clearExitDisplay") { this.clearExitDisplay(); } else if (nextState == "processComplete") { this.processComplete(entity2); } else if (nextState == "endSubArriveDelay") { this.startSubJourney(entity2); } else if (nextState == "endSubDepartDelay") { this.continueFromSubJourney(entity2); } else { errorUndefinedAdvanceState(this.entityID, this.nextState); } }; //this.activate } //BagComponent |
Here are the new source and destination components:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
function StartSubJourneyComponent(parent) { setOfComponents.push(this); this.componentID = getNewComponentID(); this.componentType = "startSubJourney"; this.componentName = "startSubJourney"; this.nextComponent = null; this.parent = parent; this.exclusive = false; this.graphic = null; this.x1 = 0; this.y1 = 0; this.reset = function() { //do nothing }; this.assignNextComponent = function(next) { //StartSubJourneyComponent this.nextComponent = next; next.assignPreviousComponent(this); }; this.verifyLinks = function() { var error = ""; if (this.nextComponent) { //link exists if (typeof this.nextComponent === "object") { //link points to an object if ("componentType" in this.nextComponent) { //object contains member componentType if ((this.nextComponent.componentType == "Arrivals") || (this.nextComponent.componentType == "Entry")) { error += this.componentType + " comp. " + this.componentID + " next comp. is not an allowed comp.\n"; } } else { //linked object does not contain member componentType error += this.componentType + " comp. " + this.componentID + " next comp. does not have componentType\n"; } } else { //link points to something that is not an object error += this.componentType + " comp. " + this.componentID + " next comp. is not an object\n"; } } else { //link that should exist does not error += this.componentType + " comp. " + this.componentID + " next comp. does not exist\n"; } return error; }; this.getComponentID = function() { return this.componentID; }; this.getComponentType = function() { return this.componentType; }; this.getComponentName = function() { return this.componentName; }; this.setComponentName = function(componentName) { this.componentName = componentName; }; this.getExclusive = function() { return this.exclusive; }; this.defineGraphic = function(graphic) { this.graphic = graphic; //TODO: ensure location automatically set by return node in parent component }; this.updateGraphic = function() { }; this.assignLocation = function(x,y) { this.x1 = x; this.y1 = y; }; this.getLocation = function() { return {x: this.x1, y: this.y1}; } this.receiveEntity = function(entity) { this.nextComponent.receiveEntity(entity); }; } //StartSubJourneyComponent function ReturnFromSubJourneyComponent(parent) { setOfComponents.push(this); this.componentID = getNewComponentID(); this.componentType = "returnFromSubJourney"; this.componentName = "returnFromSubJourney"; this.parent = parent; this.exclusive = false; this.openStatus = true; this.previousComponentList = []; this.previousComponentCount = 0; this.previousComponentIndex = 0; this.graphic = null; this.x1 = 0; this.y1 = 0; this.reset = function() { //do nothing }; this.assignPreviousComponent = function(prev) { this.previousComponentList.push(prev); this.previousComponentCount++; this.previousComponentIndex = this.previousComponentCount - 1; }; this.verifyLinks = function() { var error = ""; var i; if (this.previousComponentCount > 0) { for (i = 0; i < this.previousComponentCount; i++) { //> if (this.previousComponentList[i]) { //link exists if (typeof this.previousComponentList[i] === "object") { //link points to an object if ("componentType" in this.previousComponentList[i]) { //object contains member componentType if ((this.previousComponentList[i].componentType == "Arrivals") || (this.previousComponentList[i].componentType == "Exit")) { error += this.componentType + " comp. " + this.componentID + " previous comp. list element " + i + " is not an allowed comp.\n"; } } else { //linked object does not contain member componentType error += this.componentType + " comp. " + this.componentID + " previous comp. list item " + i + " does not have componentType\n"; } } else { //link points to something that is not an object error += this.componentType + " comp. " + this.componentID + " previous comp. list item " + i + " is not an object\n"; } } else { //link that should exist does not error += this.componentType + " comp. " + this.componentID + " previous comp. list item " + i + " does not exist\n"; } } } else { error += this.componentType + " comp. " + this.componentID + " has index of zero previous components\n"; } return error; }; |
Here’s the code that defines the entire system:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 |
//schedule for seven hours of arrivals in half-hour blocks var arrivalSchedule = [0, 1, 2, 6, 7, 7, 8, 9, 7, 6, 4, 2, 1, 0]; var entryDistribution = [[1.0], [1.0], [1.0], [1.0], [1.0], [1.0], [1.0], [1.0], [1.0], [1.0], [1.0], [1.0], [1.0], [1.0]]; var arrival1 = new ArrivalsComponent(30.0, arrivalSchedule, entryDistribution); arrival1.defineDataGroup(2.0, 61, 2, 70, globalNeutralColor, globalValueColor, globalLabelColor); //leave in place var tempGraphic; var routingTable = [[1.0],[1.0],[1.0]]; var entry1 = new EntryComponent(routingTable); entry1.defineDataGroup(2.0, 90, 105, 80, globalNeutralColor, globalValueColor, globalLabelColor); //x,y,valuewidth,border,value,label entry1.setRoutingMethod(3); //1 single connection, 2 distribution logic, 3 model logic entry1.setComponentName("Entry1"); tempGraphic = new DisplayElement(entry1, 210, 5, 68, 20, 0.0, false, false, []); entry1.defineGraphic(tempGraphic); arrival1.assignNextComponent(entry1); var pathE1Q0A = new PathComponent(); pathE1Q0A.setSpeedTime(10.0, 1.0); tempGraphic = new DisplayElement(pathE1Q0A, 0, 0, 260, 37, 0.0, false, false, []); pathE1Q0A.defineGraphic(tempGraphic); entry1.assignNextComponent(pathE1Q0A); var pathE1Q0B = new PathComponent(); pathE1Q0B.setSpeedTime(10.0, 1.0); tempGraphic = new DisplayElement(pathE1Q0B, 260, 37, 0, 0, 0.0, false, false, []); pathE1Q0B.defineGraphic(tempGraphic); var routingTableQ0 = [[1.0],[1.0],[1.0]]; //not actually needed since routingMethod is 2 here var queue0 = new QueueComponent(0.0, Infinity, routingTableQ0); queue0.defineDataGroup(2.0, 90, 205, 80, globalNeutralColor, globalValueColor, globalLabelColor); queue0.setRoutingMethod(2); //1 single connection, 2 distribution logic, 3 model logic queue0.setComponentName("Q0"); queue0.setComponentGroup("Primary"); tempGraphic = new DisplayElement(queue0, 200, 50, 88, 56, 0.0, false, false, []); var tgBaseY = 25; //49; tempGraphic.addXYLoc(44, tgBaseY + 24); tempGraphic.addXYLoc(32, tgBaseY + 24); tempGraphic.addXYLoc(20, tgBaseY + 24); tempGraphic.addXYLoc(8, tgBaseY + 24); tempGraphic.addXYLoc(8, tgBaseY + 12); tempGraphic.addXYLoc(20, tgBaseY + 12); tempGraphic.addXYLoc(32, tgBaseY + 12); tempGraphic.addXYLoc(44, tgBaseY + 12); tempGraphic.addXYLoc(56, tgBaseY + 12); tempGraphic.addXYLoc(68, tgBaseY + 12); tempGraphic.addXYLoc(80, tgBaseY + 12); tempGraphic.addXYLoc(80, tgBaseY); tempGraphic.addXYLoc(68, tgBaseY); tempGraphic.addXYLoc(56, tgBaseY); tempGraphic.addXYLoc(44, tgBaseY); tempGraphic.addXYLoc(32, tgBaseY); tempGraphic.addXYLoc(20, tgBaseY); tempGraphic.addXYLoc(8, tgBaseY); queue0.defineGraphic(tempGraphic); pathE1Q0B.assignNextComponent(queue0); pathE1Q0A.assignNextComponent(pathE1Q0B); var pathQ0Q1 = new PathComponent(); pathQ0Q1.setSpeedTime(20, 1.0); tempGraphic = new DisplayElement(pathQ0Q1, 0, 0, 0, 0, 0.0, false, false, []); pathQ0Q1.defineGraphic(tempGraphic); queue0.assignNextComponent(pathQ0Q1); var pathQ0Q2 = new PathComponent(); pathQ0Q2.setSpeedTime(20, 1.0); tempGraphic = new DisplayElement(pathQ0Q2, 0, 0, 0, 0, 0.0, false, false, []); pathQ0Q2.defineGraphic(tempGraphic); queue0.assignNextComponent(pathQ0Q2); var pathQ0Q3 = new PathComponent(); pathQ0Q3.setSpeedTime(20, 1.0); tempGraphic = new DisplayElement(pathQ0Q3, 0, 0, 0, 0, 0.0, false, false, []); pathQ0Q3.defineGraphic(tempGraphic); queue0.assignNextComponent(pathQ0Q3); var routingTableQ123 = [[1.0],[1.0],[1.0]]; //not actually needed since routingMethod is 1 here var queue1 = new QueueComponent(3.0, 3, routingTableQ123); queue1.defineDataGroup(2.0, 5, 341, 80, globalNeutralColor, globalValueColor, globalLabelColor); queue1.setRoutingMethod(1); //1 single connection, 2 distribution logic, 3 model logic queue1.setExclusive(true); queue1.setComponentName("Q1"); queue1.setComponentGroup("Primary"); tempGraphic = new DisplayElement(queue1, 141, 131, 64, 36, 0.0, false, false, []); tgBaseY = 29; //53 tempGraphic.addXYLoc(32, tgBaseY); tempGraphic.addXYLoc(20, tgBaseY); tempGraphic.addXYLoc(8, tgBaseY); queue1.defineGraphic(tempGraphic); pathQ0Q1.assignNextComponent(queue1); var queue2 = new QueueComponent(3.0, 3, routingTableQ123); queue2.defineDataGroup(2.0, 175, 341, 80, globalNeutralColor, globalValueColor, globalLabelColor); queue2.setRoutingMethod(1); //1 single connection, 2 distribution logic, 3 model logic queue2.setExclusive(true); queue2.setComponentName("Q2"); queue2.setComponentGroup("Primary"); tempGraphic = new DisplayElement(queue2, 212, 131, 64, 36, 0.0, false, false, []); tgBaseY = 29; //53; tempGraphic.addXYLoc(32, tgBaseY); tempGraphic.addXYLoc(20, tgBaseY); tempGraphic.addXYLoc(8, tgBaseY); queue2.defineGraphic(tempGraphic); pathQ0Q2.assignNextComponent(queue2); var queue3 = new QueueComponent(3.0, 3, routingTableQ123); queue3.defineDataGroup(2.0, 175, 341, 80, globalNeutralColor, globalValueColor, globalLabelColor); queue3.setRoutingMethod(1); //1 single connection, 2 distribution logic, 3 model logic queue3.setExclusive(true); queue3.setComponentName("Q3"); queue3.setComponentGroup("Primary"); tempGraphic = new DisplayElement(queue3, 283, 131, 64, 36, 0.0, false, false, []); tgBaseY = 29; //53; tempGraphic.addXYLoc(32, tgBaseY); tempGraphic.addXYLoc(20, tgBaseY); tempGraphic.addXYLoc(8, tgBaseY); queue3.defineGraphic(tempGraphic); pathQ0Q3.assignNextComponent(queue3); var pathQ1P1 = new PathComponent(); pathQ1P1.setSpeedTime(20, 1.0); tempGraphic = new DisplayElement(pathQ1P1, 0, 0, 0, 0, 0.0, false, false, []); pathQ1P1.defineGraphic(tempGraphic); queue1.assignNextComponent(pathQ1P1); var pathQ2P2 = new PathComponent(); pathQ2P2.setSpeedTime(20, 1.0); tempGraphic = new DisplayElement(pathQ2P2, 0, 0, 0, 0, 0.0, false, false, []); pathQ2P2.defineGraphic(tempGraphic); queue2.assignNextComponent(pathQ2P2); var pathQ3P3 = new PathComponent(); pathQ3P3.setSpeedTime(20, 1.0); tempGraphic = new DisplayElement(pathQ3P3, 0, 0, 0, 0, 0.0, false, false, []); pathQ3P3.defineGraphic(tempGraphic); queue3.assignNextComponent(pathQ3P3); var routingTableP123 = [[0.82, 0.9, 1.0],[0.75,0.9,1.0],[0.448,0.9,1.0]]; //citizen, LPR, visitor / parking_lot, exit, secondary var processTimeP123 = [10.0,20.8,13.0]; //fast, slow visitor, slow citizen or LPR var process1 = new ProcessComponent(processTimeP123, 0, 1, routingTableP123); process1.defineDataGroup(2.0, 5, 477, 80, globalNeutralColor, globalValueColor, globalLabelColor); process1.setExclusive(true); process1.setRoutingMethod(3); //1 single connection, 2 distribution logic, 3 model logic process1.setComponentName("P1"); process1.setComponentGroup("Primary"); tempGraphic = new DisplayElement(process1, 141, 192, 64, 36, 0.0, false, false, []); tgBaseY = 29; //53; tempGraphic.addXYLoc(32, tgBaseY); process1.defineGraphic(tempGraphic); pathQ1P1.assignNextComponent(process1); var process2 = new ProcessComponent(processTimeP123, 0, 1, routingTableP123); process2.defineDataGroup(2.0, 175, 477, 80, globalNeutralColor, globalValueColor, globalLabelColor); process2.setExclusive(true); process2.setRoutingMethod(3); //1 single connection, 2 distribution logic, 3 model logic process2.setComponentName("P2"); process2.setComponentGroup("Primary"); tempGraphic = new DisplayElement(process2, 212, 192, 64, 36, 0.0, false, false, []); tgBaseY = 29; //53; tempGraphic.addXYLoc(32, tgBaseY); process2.defineGraphic(tempGraphic); pathQ2P2.assignNextComponent(process2); var process3 = new ProcessComponent(processTimeP123, 0, 1, routingTableP123); process3.defineDataGroup(2.0, 175, 477, 80, globalNeutralColor, globalValueColor, globalLabelColor); process3.setExclusive(true); process3.setRoutingMethod(3); //1 single connection, 2 distribution logic, 3 model logic process3.setComponentName("P3"); process3.setComponentGroup("Primary"); tempGraphic = new DisplayElement(process3, 283, 192, 64, 36, 0.0, false, false, []); tgBaseY = 29; //53; tempGraphic.addXYLoc(32, tgBaseY); process3.defineGraphic(tempGraphic); pathQ3P3.assignNextComponent(process3); var pathP1X1 = new PathComponent(); pathP1X1.setSpeedTime(30, 1.0); tempGraphic = new DisplayElement(pathP1X1, 0, 0, 0, 0, 0.0, false, false, []); pathP1X1.defineGraphic(tempGraphic); process1.assignNextComponent(pathP1X1); var pathP2X1 = new PathComponent(); pathP2X1.setSpeedTime(30, 1.0); tempGraphic = new DisplayElement(pathP2X1, 0, 0, 0, 0, 0.0, false, false, []); pathP2X1.defineGraphic(tempGraphic); process2.assignNextComponent(pathP2X1); var pathP3X1 = new PathComponent(); pathP3X1.setSpeedTime(30, 1.0); tempGraphic = new DisplayElement(pathP3X1, 0, 0, 0, 0, 0.0, false, false, []); pathP3X1.defineGraphic(tempGraphic); process3.assignNextComponent(pathP3X1); var exit1 = new ExitComponent(); exit1.defineDataGroup(2.0, 90, 576, 80, globalNeutralColor, globalValueColor, globalLabelColor); exit1.setComponentName("Exit1"); tempGraphic = new DisplayElement(exit1, 210, 429, 68, 20, 0.0, false, false, []); exit1.defineGraphic(tempGraphic); pathP1X1.assignNextComponent(exit1); pathP2X1.assignNextComponent(exit1); pathP3X1.assignNextComponent(exit1); var pathP1Q10 = new PathComponent(); pathP1Q10.setSpeedTime(20, 1.0); tempGraphic = new DisplayElement(pathP1Q10, 0, 0, 0, 0, 0.0, false, false, []); pathP1Q10.defineGraphic(tempGraphic); process1.assignNextComponent(pathP1Q10); var pathP2Q10 = new PathComponent(); pathP2Q10.setSpeedTime(20, 1.0); tempGraphic = new DisplayElement(pathP2Q10, 0, 0, 0, 0, 0.0, false, false, []); pathP2Q10.defineGraphic(tempGraphic); process2.assignNextComponent(pathP2Q10); var pathP3Q10 = new PathComponent(); pathP3Q10.setSpeedTime(20, 1.0); tempGraphic = new DisplayElement(pathP3Q10, 0, 0, 0, 0, 0.0, false, false, []); pathP3Q10.defineGraphic(tempGraphic); process3.assignNextComponent(pathP3Q10); var routingTableQ10 = [[1.0],[1.0],[1.0]]; //not actually needed since routingMethod is 2 here var queue10 = new QueueComponent(3.0, Infinity, routingTableQ10); queue10.defineDataGroup(2.0, 230, 353, 80, globalNeutralColor, globalValueColor, globalLabelColor); queue10.setRoutingMethod(2); //1 single connection, 2 distribution logic, 3 model logic queue10.setExclusive(false); queue10.setComponentName("Q10"); queue10.setComponentGroup("Secondary"); tempGraphic = new DisplayElement(queue10, 314, 253, 64, 48, 0.0, false, false, []); tgBaseY = 29; //53 tempGraphic.addXYLoc(32, tgBaseY + 12); tempGraphic.addXYLoc(20, tgBaseY + 12); tempGraphic.addXYLoc(8, tgBaseY + 12); tempGraphic.addXYLoc(8, tgBaseY); tempGraphic.addXYLoc(20, tgBaseY); tempGraphic.addXYLoc(32, tgBaseY); tempGraphic.addXYLoc(44, tgBaseY); tempGraphic.addXYLoc(56, tgBaseY); queue10.defineGraphic(tempGraphic); pathP1Q10.assignNextComponent(queue10); pathP2Q10.assignNextComponent(queue10); pathP3Q10.assignNextComponent(queue10); var pathQ10P10 = new PathComponent(); pathQ10P10.setSpeedTime(20, 1.0); tempGraphic = new DisplayElement(pathQ10P10, 0, 0, 0, 0, 0.0, false, false, []); pathQ10P10.defineGraphic(tempGraphic); queue10.assignNextComponent(pathQ10P10); var pathQ10P11 = new PathComponent(); pathQ10P11.setSpeedTime(20, 1.0); tempGraphic = new DisplayElement(pathQ10P11, 0, 0, 0, 0, 0.0, false, false, []); pathQ10P11.defineGraphic(tempGraphic); queue10.assignNextComponent(pathQ10P11); var routingTableP1011 = [[0.8,1.0],[0.8,1.0],[0.8, 1.0]]; var processTimeP1011 = [40.0,40.0,40.0]; //fast, slow visitor, slow citizen or LPR var process10 = new ProcessComponent(processTimeP1011, 0, 1, routingTableP1011); process10.defineDataGroup(2.0, 5, 477, 80, globalNeutralColor, globalValueColor, globalLabelColor); process10.setExclusive(true); process10.setRoutingMethod(3); //1 single connection, 2 distribution logic, 3 model logic process10.setComponentName("P10"); process10.setComponentGroup("Secondary"); tempGraphic = new DisplayElement(process10, 279, 326, 64, 36, 0.0, false, false, []); tgBaseY = 29; //53; tempGraphic.addXYLoc(32, tgBaseY); process10.defineGraphic(tempGraphic); pathQ10P10.assignNextComponent(process10); var pathP10X1 = new PathComponent(); pathP10X1.setSpeedTime(20, 1.0); tempGraphic = new DisplayElement(pathP10X1, 0, 0, 0, 0, 0.0, false, false, []); pathP10X1.defineGraphic(tempGraphic); process10.assignNextComponent(pathP10X1); pathP10X1.assignNextComponent(exit1); var process11 = new ProcessComponent(processTimeP1011, 0, 1, routingTableP1011); process11.defineDataGroup(2.0, 5, 477, 80, globalNeutralColor, globalValueColor, globalLabelColor); process11.setExclusive(true); process11.setRoutingMethod(3); //1 single connection, 2 distribution logic, 3 model logic process11.setComponentName("P11"); process11.setComponentGroup("Secondary"); tempGraphic = new DisplayElement(process11, 350, 326, 64, 36, 0.0, false, false, []); tgBaseY = 29; //53; tempGraphic.addXYLoc(32, tgBaseY); process11.defineGraphic(tempGraphic); pathQ10P11.assignNextComponent(process11); var pathP11X1 = new PathComponent(); pathP11X1.setSpeedTime(20, 1.0); tempGraphic = new DisplayElement(pathP11X1, 0, 0, 0, 0, 0.0, false, false, []); pathP11X1.defineGraphic(tempGraphic); process11.assignNextComponent(pathP11X1); pathP11X1.assignNextComponent(exit1); var pathP10X2 = new PathComponent(); pathP10X2.setSpeedTime(20, 1.0); tempGraphic = new DisplayElement(pathP10X2, 0, 0, 0, 0, 0.0, false, false, []); pathP10X2.defineGraphic(tempGraphic); process10.assignNextComponent(pathP10X2); var pathP11X2 = new PathComponent(); pathP11X2.setSpeedTime(20, 1.0); tempGraphic = new DisplayElement(pathP11X2, 0, 0, 0, 0, 0.0, false, false, []); pathP11X2.defineGraphic(tempGraphic); process11.assignNextComponent(pathP11X2); var exit2 = new ExitComponent(); exit2.defineDataGroup(2.0, 230, 576, 80, globalNeutralColor, globalValueColor, globalLabelColor); exit2.setComponentName("Exit2"); tempGraphic = new DisplayElement(exit2, 314, 400, 68, 20, 0.0, false, false, []); exit2.defineGraphic(tempGraphic); pathP10X2.assignNextComponent(exit2); pathP11X2.assignNextComponent(exit2); var globalProcessTimeSettings = [[5,7,9],[30,50,80]]; function processTimeBag1(entity) { var processTime; var globalIndex = 1; if (entity.getPropertyValue("Residency") == "Citizen") { processTime = globalProcessTimeSettings[globalIndex][0]; } else if (entity.getPropertyValue("Residency") == "LPR") { processTime = globalProcessTimeSettings[globalIndex][1]; } else if (entity.getPropertyValue("Residency") == "Visitor") { processTime = globalProcessTimeSettings[globalIndex][2]; } return processTime; } var routingTableB1 = [[1.0],[1.0],[1.0]]; //var processTimeB1 = [30.0,50.0,80.0]; //citizen, LPR, or visitor //var bag1 = new BagComponent(processTimeB1, 1, 12, routingTableB1); var bag1 = new BagComponent(processTimeBag1, 12, routingTableB1, true, 10.0, 10.0); bag1.defineDataGroup(2.0,5,300,100, globalNeutralColor, globalValueColor, globalLabelColor); bag1.setExclusive(true); bag1.setRoutingMethod(1); //1 single connection, 2 distribution logic, 3 model logic bag1.setComponentName("B1"); bag1.setComponentGroup("Parking Lot"); tempGraphic = new DisplayElement(bag1, 130, 253, 52, 90, 0.0, false, false, []); tgBaseY = 29; tempGraphic.addXYLoc(8, tgBaseY); tempGraphic.addXYLoc(20, tgBaseY); tempGraphic.addXYLoc(32, tgBaseY); tempGraphic.addXYLoc(44, tgBaseY); tempGraphic.addXYLoc(8, tgBaseY + 12); tempGraphic.addXYLoc(20, tgBaseY + 12); tempGraphic.addXYLoc(32, tgBaseY + 12); tempGraphic.addXYLoc(44, tgBaseY + 12); tempGraphic.addXYLoc(8, tgBaseY + 24); tempGraphic.addXYLoc(20, tgBaseY + 24); tempGraphic.addXYLoc(32, tgBaseY + 24); tempGraphic.addXYLoc(44, tgBaseY + 24); //end fixed spaces = maxCapacity tempGraphic.addXYLoc(26, tgBaseY + 52); //start spaces for exitQueue tempGraphic.addXYLoc(14, tgBaseY + 52); tempGraphic.addXYLoc(14, tgBaseY + 40); tempGraphic.addXYLoc(26, tgBaseY + 40); tempGraphic.addXYLoc(38, tgBaseY + 40); bag1.defineGraphic(tempGraphic); var pathP1B1 = new PathComponent(); pathP1B1.setSpeedTime(20,1.0); tempGraphic = new DisplayElement(pathP1B1, 0, 0, 0, 0, 0.0, false, false, []); pathP1B1.defineGraphic(tempGraphic); process1.assignNextComponent(pathP1B1); pathP1B1.assignNextComponent(bag1); var pathP2B1 = new PathComponent(); pathP2B1.setSpeedTime(20,1.0); tempGraphic = new DisplayElement(pathP2B1, 0, 0, 0, 0, 0.0, false, false, []); pathP2B1.defineGraphic(tempGraphic); process2.assignNextComponent(pathP2B1); pathP2B1.assignNextComponent(bag1); var pathP3B1 = new PathComponent(); pathP3B1.setSpeedTime(20,1.0); tempGraphic = new DisplayElement(pathP3B1, 0, 0, 0, 0, 0.0, false, false, []); pathP3B1.defineGraphic(tempGraphic); process3.assignNextComponent(pathP3B1); pathP3B1.assignNextComponent(bag1); var routingTableP20 = [[1.0],[1.0],[1.0]]; var processTimeP20 = [60.0,60.0,60.0]; //fast, slow visitor, slow citizen or LPR var process20 = new ProcessComponent(processTimeP20, 1, 1, routingTableP20); process20.defineDataGroup(2.0, 5, 477, 80, globalNeutralColor, globalValueColor, globalLabelColor); process20.setExclusive(true); process20.setRoutingMethod(1); //1 single connection, 2 distribution logic, 3 model logic process20.setComponentName("P20"); process20.setComponentGroup("Parking Lot"); tempGraphic = new DisplayElement(process20, 157, 368, 64, 36, 0.0, false, false, []); tgBaseY = 29; tempGraphic.addXYLoc(32, tgBaseY); process20.defineGraphic(tempGraphic); var pathB1P20 = new PathComponent(); pathB1P20.setSpeedTime(20,1.0); tempGraphic = new DisplayElement(pathB1P20, 0, 0, 0, 0, 0.0, false, false, []); pathB1P20.defineGraphic(tempGraphic); bag1.assignNextComponent(pathB1P20); pathB1P20.assignNextComponent(process20); var pathP20X1 = new PathComponent(); pathP20X1.setSpeedTime(20,1.0); tempGraphic = new DisplayElement(pathP20X1, 0, 0, 0, 0, 0.0, false, false, []); pathP20X1.defineGraphic(tempGraphic); process20.assignNextComponent(pathP20X1); pathP20X1.assignNextComponent(exit1); var startSubJourneyBag1 = new StartSubJourneyComponent(bag1); bag1.assignStartSubJourneyComponent(startSubJourneyBag1); tempGraphic = new DisplayElement(startSubJourneyBag1, 0, 0, 128, 263, 0.0, false, true, []); startSubJourneyBag1.defineGraphic(tempGraphic); //TODO: bury this assignment in the DisplayElement constructor var returnFromSubJourneyBag1 = new ReturnFromSubJourneyComponent(bag1); bag1.assignReturnFromSubJourneyComponent(returnFromSubJourneyBag1); tempGraphic = new DisplayElement(returnFromSubJourneyBag1, 128, 333, 0, 0, 0.0, true, false, []); returnFromSubJourneyBag1.defineGraphic(tempGraphic); var pathB1Z = new PathComponent(); pathB1Z.setSpeedTime(20,1.0); tempGraphic = new DisplayElement(pathB1Z, 128, 263, 50, 298, 0.0, false, false, []); pathB1Z.defineGraphic(tempGraphic); var pathZB1 = new PathComponent(); pathZB1.setSpeedTime(20,1.0); tempGraphic = new DisplayElement(pathZB1, 50, 298, 128, 333, 0.0, false, false, []); pathZB1.defineGraphic(tempGraphic); startSubJourneyBag1.assignNextComponent(pathB1Z); pathB1Z.assignNextComponent(pathZB1); pathZB1.assignNextComponent(returnFromSubJourneyBag1); |
Some adjustments had to be made to the DisplayElement object as well, to handle drawing the 2D and 3D versions of the new components.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 |
function DisplayElement(parent,x1,y1,w,h,angle,count,traverse,xylocs) { //TODO: include mechanisms for drawing paths here this.parent = parent; this.x1 = x1; this.y1 = y1; if ((this.parent.getComponentType() == "startSubJourney") || (this.parent.getComponentType() == "returnFromSubJourney")) { this.x2 = w; this.y2 = h; this.isJourney = true; this.isPath = false; } else if (this.parent.getComponentType() != "Path") { this.x2 = x1 + w; this.y2 = y1 + h; this.isJourney = false; this.isPath = false; } else { this.x2 = w; this.y2 = h; this.isJourney = false; this.isPath = true; } this.angle = angle; //not used for now, default to 0.0; this.index = this.parent.getComponentID(); this.nodeColor = globalNodeColor; var tempType = this.parent.getComponentType(); switch (tempType) { case "Queue": this.type = "Q"; break; case "Process": this.type = "P"; break; case "Entry": this.type = "E"; break; case "Exit": this.type = "X"; break; case "Bag": this.type = "B"; break; case "Stack": this.type = "S"; break; case "Control": this.type = "C"; break; default: this.type = "?"; //other types are never displayed this way } this.label = this.index + "-" + this.type + ": " +this.parent.getComponentName(); this.labelWidth = globalCTX.measureText(this.label).width; this.innode = true; this.outnode = true; if (this.parent.getComponentType() == "Entry") { this.innode = false; } else if (this.parent.getComponentType() == "Exit") { this.outnode = false; } else if (this.parent.getComponentType() == "Arrivals") { this.innode = false; } else if (this.parent.getComponentType() == "startSubJourney") { this.innode = false; } else if (this.parent.getComponentType() == "returnFromSubJourney") { this.outnode = false; } this.count = count; this.traverse = traverse; this.countValue = 0; this.traverseValue = 0; this.nodexi = w * 0.5; this.nodexo = this.nodexi; this.nodeyi = 0.0; this.nodeyo = h; this.locsCount = xylocs.length; this.entityLocs = []; this.component3D = null; //use this information to define a 3D display component for (var i=0; i<this.locsCount; i++) { this.entityLocs.push(xylocs[i]); } this.addXYLoc = function(xl,yl) { var loc = {x: xl, y: yl}; this.entityLocs.push(loc); this.locsCount++; }; this.setNeutralColor = function(nc) { this.neutralColor = nc; }; this.setTraverseValue = function(tv) { this.traverseValue = tv; }; this.setCountValue = function(cv) { this.countValue = cv; }; this.setStartPoint = function(x1,y1) { this.x1 = x1; this.y1 = y1; }; this.setEndPoint = function(x2,y2) { this.x2 = x2; this.y2 = y2; }; this.getInNode = function() { if (this.innode) { if (this.isJourney) { return {x: this.x1, y: this.y1}; } else if (!this.isPath) { return {x: this.x1+this.nodexi, y: this.y1+this.nodeyi}; } else { return {x: this.x1, y: this.y1}; } } else { alert("display element queried for non-existent incoming node"); return null; //in theory this should never happen } }; this.getOutNode = function() { if (this.outnode) { if (this.isJourney) { return {x: this.x2, y: this.y2}; } else if (!this.isPath) { return {x: this.x1+this.nodexo, y: this.y1+this.nodeyo}; } else { return {x: this.x2, y: this.y2}; } } else { alert("display element queried for non-existent outgoing node"); return null; //in theory this should never happen } }; this.defineColors = function(lc,vc,nc,nvc,rc,rvc,wc,wvc) { this.labelColor = lc; //text labels this.valueColor = vc; //text values this.neutralColor = nc; //non-exclusive path and component color this.neutralVertexColor = nvc; //non-exclusive wireframe color this.readyColor = rc; //exclusive open component or ready-to-go entity color this.readyVertexColor = rvc; //exclusive open component or ready-to-go entity vertex color this.waitingColor = wc; //exclusive closed component or in-process entity color this.waitingVertexColor = wvc; //exclusive closed component or in-process entity vertex color }; this.defineGlobalDefaultColors = function() { this.defineColors(globalLabelColor,globalValueColor,globalNeutralColor,globalNeutralVertexColor, globalReadyColor,globalReadyVertexColor,globalWaitingColor,globalWaitingVertexColor); }; this.defineGlobalDefaultColors(); this.drawBasic = function() { var borderColor; if (this.parent.getExclusive()) { if (this.parent.getOpenStatus()) { borderColor = this.readyColor; } else { borderColor = this.waitingColor; } } else { borderColor = this.neutralColor; } if (this.isJourney) { globalCTX.strokeStyle = borderColor; globalCTX.beginPath(); if (this.innode) { globalCTX.moveTo(this.x1-5+0.5,this.y1-5+0.5); globalCTX.lineTo(this.x1+4+0.5,this.y1-5+0.5); globalCTX.lineTo(this.x1+4+0.5,this.y1+4+0.5); globalCTX.lineTo(this.x1-5+0.5,this.y1+4+0.5); globalCTX.lineTo(this.x1-5+0.5,this.y1-5+0.5); } else { globalCTX.moveTo(this.x2-5+0.5,this.y2-5+0.5); globalCTX.lineTo(this.x2+4+0.5,this.y2-5+0.5); globalCTX.lineTo(this.x2+4+0.5,this.y2+4+0.5); globalCTX.lineTo(this.x2-5+0.5,this.y2+4+0.5); globalCTX.lineTo(this.x2-5+0.5,this.y2-5+0.5); } globalCTX.stroke(); //do nothing } else if (!this.isPath) { var runningHeight = 15; globalCTX.strokeStyle = borderColor; globalCTX.beginPath(); globalCTX.moveTo(this.x1+0.5,this.y1+0.5); globalCTX.lineTo(this.x2+0.5,this.y1+0.5); globalCTX.lineTo(this.x2+0.5,this.y2+0.5); globalCTX.lineTo(this.x1+0.5,this.y2+0.5); globalCTX.lineTo(this.x1+0.5,this.y1+0.5); globalCTX.stroke(); globalCTX.font = "12px Arial"; globalCTX.fillStyle = this.labelColor; globalCTX.textAlign = "center"; globalCTX.fillText(this.label,this.x1+this.nodexi,this.y1+runningHeight); runningHeight += 12; if (this.traverse) { globalCTX.textAlign = "right"; var waitChar = "P:"; if (this.parent.getComponentType() == "Queue") { waitChar = "T:"; } else if (this.parent.getComponentType() == "Process") { waitChar = "P:"; } globalCTX.fillText(waitChar,x1+15,this.y1+runningHeight); globalCTX.fillStyle = this.valueColor; globalCTX.textAlign = "left"; globalCTX.fillText(this.traverseValue,x1+17,this.y1+runningHeight); runningHeight += 12; } if (this.count) { globalCTX.fillStyle = this.labelColor; globalCTX.textAlign = "right"; globalCTX.fillText("Q:",x1+15,this.y1+runningHeight); globalCTX.fillStyle = this.valueColor; globalCTX.textAlign = "left"; globalCTX.fillText(this.countValue,x1+17,this.y1+runningHeight); runningHeight += 12; } } else { globalCTX.strokeStyle = borderColor; globalCTX.beginPath(); globalCTX.moveTo(this.x1+0.5,this.y1+0.5); globalCTX.lineTo(this.x2+0.5,this.y2+0.5); globalCTX.stroke(); } }; this.drawNodes = function() { if (this.isJourney) { if (this.innode) { drawNode(this.x1,this.y1,3,this.nodeColor); } if (this.outnode) { drawNode(this.x2,this.y2,3,this.nodeColor); } } else if (!this.isPath) { if (this.innode) { drawNode(this.x1+this.nodexi,this.y1+this.nodeyi,3,this.nodeColor); } if (this.outnode) { drawNode(this.x1+this.nodexo,this.y1+this.nodeyo,3,this.nodeColor); } } else { if (this.parent.previousComponent.getComponentType() == "Path") { drawNode(this.x1,this.y1,3,this.nodeColor); } if (this.parent.nextComponent.getComponentType() == "Path") { drawNode(this.x2,this.y2,3,this.nodeColor); } } }; this.drawEntities = function() { var i; var drawCount; var drawColor; if (this.isPath) { drawCount = this.parent.entityQueue.length; for (i=0; i<drawCount; i++) { var location = this.parent.entityQueue[i].getLocation(); drawNode(location.x,location.y,5,this.readyColor); drawNode(location.x,location.y,3,this.parent.entityQueue[i].getEntityColor()) } } else if (this.parent.getComponentType() == "Bag") { drawCount = this.parent.maxCapacity; //should be < this.entityLocs.length drawColor = this.waitingColor; for (i=0; i<drawCount; i++) { if (this.parent.entityQueue[i] != null) { var xx = this.entityLocs[i].x; var yy = this.entityLocs[i].y; drawNode(this.x1+xx,this.y1+yy,5,drawColor); drawNode(this.x1+xx,this.y1+yy,3,this.parent.entityQueue[i].getEntityColor()); if (this.parent.subEntityQueue[i] != null) { drawNode(this.x1+xx+3,this.y1+yy-3,5,drawColor) drawNode(this.x1+xx+3,this.y1+yy-3,3,this.parent.subEntityQueue[i].getEntityColor()); } } } drawCount = this.entityLocs.length - this.parent.maxCapacity; if (this.parent.exitQueue.length < drawCount) { drawCount = this.parent.exitQueue.length; } drawColor = this.readyColor; for (i=0; i<drawCount; i++) { //locs for exit queue in order from maxCapacity var xx = this.entityLocs[this.parent.maxCapacity + i].x; var yy = this.entityLocs[this.parent.maxCapacity + i].y; drawNode(this.x1+xx,this.y1+yy,5,drawColor); //exit queue items in reverse order drawNode(this.x1+xx,this.y1+yy,3,this.parent.exitQueue[this.parent.exitQueue.length-1-i].getEntityColor()); } } else if (this.parent.getComponentType() != "Arrivals") { var qCount = this.countValue - this.traverseValue; if (this.countValue > this.locsCount) { drawCount = this.locsCount; } else { drawCount = this.countValue; } for (i=0; i<drawCount; i++) { if (i < qCount) { drawColor = this.readyColor; } else { drawColor = this.waitingColor; } var xx = this.entityLocs[i].x; var yy = this.entityLocs[i].y; drawNode(this.x1+xx,this.y1+yy,5,drawColor); drawNode(this.x1+xx,this.y1+yy,3,this.parent.entityQueue[i].getEntityColor()); } } }; this.define3DComponent = function() { //when everything else is defined and verified, call this to build 3D component info list var faceColor; var vertexColor; if (this.parent.getExclusive()) { if (this.parent.getOpenStatus()) { faceColor = this.readyColor; vertexColor = this.readyVertexColor; } else { faceColor = this.waitingColor; vertexColor = this.waitingVertexColor; } } else { faceColor = this.neutralColor; vertexColor = this.neutralVertexColor; } if (this.isPath) { this.component3D = define3DPathComponent(this.x1,this.y1,this.x2,this.y2,faceColor); if (this.parent.nextComponent.getComponentType() == "Path") { //define a 3D node here, only at the end of the first path define3DNode(this.x2,this.y2,globalNodeColor,globalNodeVertexColor); } } else if (this.parent.getComponentType() != "Arrivals") { if (!this.isJourney) { this.component3D = define3DBoxComponent(this.x1,this.y1,w,h,faceColor,vertexColor,this.innode,this.outnode); } else { if (this.outnode) { define3DNode(this.x2,this.y2,globalNodeColor,globalNodeVertexColor); } if (this.innode) { define3DNode(this.x1,this.y1,globalNodeColor,globalNodeVertexColor); } } } else { //do nothing } }; this.update3DComponent = function() { var faceColor; var vertexColor; if (this.parent.getExclusive()) { if (this.parent.getOpenStatus()) { faceColor = this.readyColor; vertexColor = this.readyVertexColor; } else { faceColor = this.waitingColor; vertexColor = this.waitingVertexColor; } } else { faceColor = this.neutralColor; vertexColor = this.neutralVertexColor; } if (this.isPath) { update3DPathColor(this.component3D,faceColor); } else if (this.parent.getComponentType() != "Arrivals") { if (!this.isJourney) { update3DComponentColor(this.component3D,faceColor,vertexColor); } } }; this.update3DEntities = function() { var i; if (this.isPath) { var drawCount = this.parent.entityQueue.length; for (i=0; i<drawCount; i++) { var location = this.parent.entityQueue[i].getLocation(); update3DEntity(global3DRefreshEntityCount++,location.x,location.y,this.readyColor,this.readyVertexColor); } } else if (this.parent.getComponentType() != "Arrivals") { var qCount = this.countValue - this.traverseValue; if (this.countValue > this.locsCount) { drawCount = this.locsCount; } else { drawCount = this.countValue; } var faceColor; var vertexColor; for (i=0; i<drawCount; i++) { if (i < qCount) { faceColor = this.readyColor; vertexColor = this.readyVertexColor; } else { faceColor = this.waitingColor; vertexColor = this.waitingVertexColor; } var xx = this.x1 + this.entityLocs[i].x; //var yy = global3DTopHeight + (global3DEntityHeight / 2) + global3DNodeUpOffset; //declared inside update3DEntity var zz = this.y1 + this.entityLocs[i].y; update3DEntity(global3DRefreshEntityCount++,xx,zz,faceColor,vertexColor); } } }; } //DisplayElement |
I also made the background transparent so it’ll look a bit nicer in an iframe and need to do something more explicit with the 3D versions of the sub-entities.
A Simple Discrete-Event Simulation: Part 74
Following yesterday’s work I had three tasks to complete. The first task was to fix the reporting for cases where a Process or Bag component is not the last item in a component group. The fix was to only run the reporting process for exiting a group when the entity leaving a Process or Bag component is going to a different non-Path component that’s in a different group. This involved embedding the report function at the end of the forwardEntity
function in the if
statement shown below.
1 2 3 4 |
//record stats if (this.nextComponentList[dest].getComponentGroup() != entity.getComponentGroup()) { //truly leaving a component group along the current connection (specified by dest) recordGroupStatsWrapper(this.componentGroup, entity.getComponentGroupEntryTime(), entity); } |
The getComponentGroup
function in Path components had to be modified so it actually returns the component group of its next component, so it always returns the component group of the next non-Path component.
The second task was to experiment with different ways of specifying the process time for certain components in a modular way. I originally passed in an array of values and called a fixed, external function to determine the array index to use base on the relevant entity’s user-defined properties. Later I added an additional index to the process so different Process or Bag components could generate lookup indices based on different properties or combinations of properties. I then realized that I could simply pass a custom function in for each case. The code for one way of doing this is here:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
function processTimeBag1(entity) { var processTime; if (entity.getPropertyValue("Residency") == "Citizen") { processTime = 30; } else if (entity.getPropertyValue("Residency") == "LPR") { processTime = 50; } else if (entity.getPropertyValue("Residency") == "Visitor") { processTime = 80; } return processTime; } var routingTableB1 = [[1.0],[1.0],[1.0]]; var bag1 = new BagComponent(processTimeBag1, 12, routingTableB1); ... var pTime = processTime(entity); //processTime is the internal name for the processTimeBag1 parameter advance(pTime, this, "processComplete",entity); |
If we want to retain some additional form of external modularity of parameters we could do something like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
var globalProcessTimeSettings = [[5,7,9],[30,50,80]]; function processTimeBag1(entity) { var processTime; var globalIndex = 1; if (entity.getPropertyValue("Residency") == "Citizen") { processTime = globalProcessTimeSettings[globalIndex][0]; } else if (entity.getPropertyValue("Residency") == "LPR") { processTime = globalProcessTimeSettings[globalIndex][1]; } else if (entity.getPropertyValue("Residency") == "Visitor") { processTime = globalProcessTimeSettings[globalIndex][2]; } return processTime; } var routingTableB1 = [[1.0],[1.0],[1.0]]; var bag1 = new BagComponent(processTimeBag1, 12, routingTableB1); ... var pTime = processTime(entity); //processTime is the internal name for the processTimeBag1 parameter advance(pTime, this, "processComplete",entity); |
The possibilities are many.
The third and final task was to allow the simulation to run until it is empty if a specific flag is set. I defined a global variable called runToEmptyFlag
and modified two tests that control when to stop the simulation and when to stop recording data. The logic says to run to at least the minimum time and then stop if runToEmptyFlag
is false, or keep going until there are no entities left in the system if runToEmptyFlag
is true. Today’s version of the model has the flag set to true, so it will run beyond 630 time units as long as there are entities still in the system. If the first run doesn’t run past that time because it clears out then reset and rerun it until you see the desired behavior. The runtimes can vary quite a bit depending on how the type and routing dice fall.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
this.increment = function() { //function in the DisplayControlComponent object //update the displays displayControlUpdate = true; //this.displayEnd is set by the global value for when to stop running, globalEndTime if ((globalSimClock >= this.displayEnd) && (!runToEmptyFlag || (setOfEntities.length == 0))) { keepRunning = false; //tells the main event loop to stop pulling events, if any are avialable } else { //calculate next update time var nextTime = this.displayIncrement * this.displayMultiplier; while ((this.displayClock + nextTime) < globalSimClock) { nextTime += this.displayIncrement * this.displayMultiplier; } this.displayClock += nextTime; advance(nextTime,this,"increment"); //currently set to run in 30-unit time intervals } }; |
1 2 3 4 5 6 7 8 9 10 |
this.increment = function() { //function in the StatsTimerComponent object incrementStatsTimeIndexWrapper(); //this.timeEnd is set by the global value for when to stop running, globalEndTime if ((globalSimClock < this.timeEnd) || (!runToEmptyFlag) || ((globalSimClock >= this.timeEnd) && runToEmptyFlag && (setOfEntities.length > 0)) ) { advance(statsInterval, this, "increment"); //currently set to run in 30-unit time intervals } }; |
I noticed that the final time increment would not be reported properly if the systems runs to empty beyond its allotted time. The fix was to add in an extra reporting increment if the global clock is greater than the number of reporting cycles run times the number of time units per cycle.
1 2 3 4 5 6 7 8 9 10 |
function displayEndMessage() { globalCTX.font = "12px Arial"; globalCTX.fillStyle = "#FF00FF"; globalCTX.textAlign = "left"; globalCTX.fillText("Simulation has ended", 10.0, canvas.height - 2); if (globalSimClock > (statsTimeIndex * statsUpdateInterval)) { //over allotted time test and increment added here incrementStatsTimeIndexWrapper(); } reportStatsWrapper(); } //displayEndMessage |
A Simple Discrete-Event Simulation: Part 73
Today I added the Bag component (number 36 in the frame above), which you can think of as a parking lot. It works largely the same way as a ProcessComponent but it doesn’t store its entities in a FIFO queue internally. Rather, it defines a list of entities with one space for each possible entity up to the defined maxCapacity
of the component. An entity enters the component, gets assigned to the lowest empty index in the list, and begins working off its defined process time. When the process time ends the entity is moved over to an exit queue, which is another list of entities within the Bag component. The exit queue shows which entities are waiting to leave the Bag component, which is only relevant if the next downstream component is an exclusive one that is not currently open. I added a downstream Process component to the model in order to visually demonstrate this behavior.
The procedure for drawing the entities in the linked displayElement
is a bit different than for the queue or process components. The designer should specify more entityLocs
spaces than the capacity of the component. The first maxCapacity
spaces are used to display the entities while they are “parked”. Any additional spaces are used to display the entities waiting in the exit queue. The Bag component in today’s version of the example model has a capacity of twelve and an additional five spaces reserved to show the exit queue. If there are more entities in the internal exit queue than there are spaces left to display them then the extras do not get displayed. One trick to make this more visually interesting, if it’s appropriate to the real process being modeled, is to assign the entity locations up to the Bag’s capacity in a semi-random order. That way the entities look like they’re parking in a more naturalistic manner rather than obediently filling spaces in regimented order. The border modeling programs I worked with at Regal Decision Systems (BorderWizard, CanSim, and SimFronteras) all allowed for this, and the effect was particularly striking in situations where there were large numbers of vehicles in a parking area. The commercial parking area at Ambassador Bridge in Detroit used to fill up in a big way when many trucks needed to park so the drivers could attend to customs paperwork. (That process has since been largely obviated by automating the paperwork so it’s completed offsite before the vehicle makes the crossing.)
Having a Bag component feed directly into another component that’s exclusive should probably be rare, but the full behavior is included. I’m thinking of my time analyzing and collecting data at dozens of border crossings where vehicles and pedestrians were almost always able to leave a Process or Bag (parking lot, literally) and go someplace else. There was effectively a non-exclusive queue in front of most processes and parking lots. That said, one can easily imagine a tightly coupled manufacturing process when internal queues have been squeezed out by design or after rearrangement following a Lean analysis.
Here’s the code for the entire Bag component.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 |
function BagComponent(processTime, processTimeSwitch, maxCapacity, routingTable) { if (typeof maxCapacity === "undefined") {maxCapacity = 1;} if (typeof routingTable === "undefined") {routingTable = [1.0];} //generally exclusive, should always be fed by a queue or at least "protected" by a status-based diversion component setOfComponents.push(this); this.componentID = getNewComponentID(); this.componentType = "Bag"; this.componentName = "Bag"; this.componentGroup = "Bag"; this.exclusive = true; this.routingMethod = 1; //1: one connection, 2: distribution, 3 routing this.previousComponentList = []; this.previousComponentCount = 0; this.nextComponentList = []; this.nextComponentCount = 0; this.nextComponentIDList = []; this.processTime = processTime; this.processTimeSwitch = processTimeSwitch; this.maxCapacity = maxCapacity; this.savedDestination = -1; this.previousComponentIndex = 0; this.nextComponentIndex = 0; this.entityQueue = []; for (var i=0; i<maxCapacity; i++) { this.entityQueue[i] = null; } this.exitQueue = []; this.routingTable = routingTable; this.openStatus = true; this.entryTime = ""; this.entryEntityID = ""; this.exitTime = ""; this.exitEntityID = ""; this.exitResidenceTime = ""; this.countInBag = 0; this.countInProcess = 0; this.activity = ""; this.endEntryDisplayTime = 0; this.endExitDisplayTime = 0; this.endAllDisplayTime = 0; this.displayDelay = 0; this.graphic = null; this.reset = function() { this.previousComponentIndex = this.previousComponentCount - 1; this.nextComponentIndex = this.nextComponentCount - 1; this.entityQueue = []; for (var i=0; i<maxCapacity; i++) { this.entityQueue[i] = null; } this.exitQueue = []; this.openStatus = true; this.savedDestination = -1; this.entryTime = ""; this.entryEntityID = ""; this.exitTime = ""; this.exitEntityID = ""; this.exitResidenceTime = ""; this.countInBag = 0; this.countInProcess = 0; this.activity = ""; this.endEntryDisplayTime = 0; this.endExitDisplayTime = 0; this.endAllDisplayTime = 0; }; this.assignPreviousComponent = function(prev) { //TODO-: implement code that makes this actually work this.previousComponentList.push(prev); this.previousComponentCount++; this.previousComponentIndex = this.previousComponentCount - 1; //TODO-: assign this automatically when upstream link and exclusive paths are required? }; this.assignNextComponent = function(next) { //BagComponent this.nextComponentList.push(next); this.nextComponentCount++; this.nextComponentIndex = this.nextComponentCount - 1; next.assignPreviousComponent(this); //TODO-: automatically assign upstream link if downstream component is exclusive? }; this.verifyLinks = function() { var i; var error = ""; if (this.nextComponentCount > 0) { for (i = 0; i < this.nextComponentCount; i++) { //> if (this.nextComponentList[i]) { //link exists if (typeof this.nextComponentList[i] === "object") { //link points to an object if ("componentType" in this.nextComponentList[i]) { //object contains member componentType if ((this.nextComponentList[i].componentType == "Arrivals") || (this.nextComponentList[i].componentType == "Entry")) { error += this.componentType + " comp. " + this.componentID + " next comp. list element " + i + " is not an allowed comp.\n"; } } else { //linked object does not contain member componentType error += this.componentType + " comp. " + this.componentID + " next comp. list item " + i + " does not have componentType\n"; } } else { //link points to something that is not an object error += this.componentType + " comp. " + this.componentID + " next comp. list item " + i + " is not an object\n"; } } else { //link that should exist does not error += this.componentType + " comp. " + this.componentID + " next comp. list item " + i + " does not exist\n"; } } } else { error += this.componentType + " comp. " + this.componentID + " has index of zero next components\n"; } if (this.previousComponentCount > 0) { for (i = 0; i < this.previousComponentCount; i++) { //> if (this.previousComponentList[i]) { //link exists if (typeof this.previousComponentList[i] === "object") { //link points to an object if ("componentType" in this.previousComponentList[i]) { //object contains member componentType if ((this.previousComponentList[i].componentType == "Arrivals") || (this.previousComponentList[i].componentType == "Exit")) { error += this.componentType + " comp. " + this.componentID + " previous comp. list element " + i + " is not an allowed comp.\n"; } } else { //linked object does not contain member componentType error += this.componentType + " comp. " + this.componentID + " previous comp. list item " + i + " does not have componentType\n"; } } else { //link points to something that is not an object error += this.componentType + " comp. " + this.componentID + " previous comp. list item " + i + " is not an object\n"; } } else { //link that should exist does not error += this.componentType + " comp. " + this.componentID + " previous comp. list item " + i + " does not exist\n"; } } } else { error += this.componentType + " comp. " + this.componentID + " has index of zero previous components\n"; } return error; }; this.getNextComponentIDs = function() { for (var i = 0; i < this.nextComponentCount; i++) { if (this.nextComponentList[i].getComponentType() != "Path") { this.nextComponentIDList[i] = this.nextComponentList[i].getComponentID(); } else { this.nextComponentIDList[i] = this.nextComponentList[i].passComponentID(); } } }; this.getComponentID = function() { return this.componentID; }; this.getComponentType = function() { //BagComponent return this.componentType; }; this.getComponentName = function() { return this.componentName; }; this.setComponentName = function(componentName) { this.componentName = componentName; }; this.getComponentGroup = function() { return this.componentGroup; }; this.setComponentGroup = function(componentGroup) { this.componentGroup = componentGroup; addToGroupStatsNameListWrapper(componentGroup); }; this.getExclusive = function() { return this.exclusive; }; this.setExclusive = function(exclusive) { this.exclusive = exclusive; }; this.getProcessTime = function() { return this.processTime; }; this.getProcessTimeSwitch = function() { return this.processTimeSwitch; }; this.setProcessTimeSwitch = function(processTimeSwitch) { this.processTimeSwitch = processTimeSwitch; }; this.getMaxCapacity = function() { return this.maxCapacity; }; this.setMaxCapacity = function(maxCapacity) { this.maxCapacity = maxCapacity; }; this.getOpenStatus = function() { return this.openStatus; }; this.setOpenStatus = function(openStatus) { this.openStatus = openStatus; }; this.getForwardAttemptTime = function() { if (this.exitQueue.length > 0) { return this.exitQueue[this.exitQueue.length - 1].getForwardAttemptTime(); } else { return Infinity; } }; this.getRoutingMethod = function() { return this.routingMethod; }; this.setRoutingMethod = function(routingMethod) { this.routingMethod = routingMethod; }; this.getEntryTime = function() { return this.entryTime; }; this.getEntryEntityID = function() { return this.entryEntityID; }; this.getExitTime = function() { return this.exitTime; }; this.getExitEntityID = function() { return this.exitEntityID; }; this.getExitResidenceTime = function() { return this.exitResidenceTime; }; this.getCountInBag = function() { return this.countInBag; }; this.getCountInProcess = function() { return this.countInProcess; }; this.getActivity = function() { return this.activity; }; this.getEndEntryDisplayTime = function() { return this.endEntryDisplayTime; }; this.getEndExitDisplayTime = function() { return this.endExitDisplayTime; }; this.getEndAllDisplayTime = function() { return this.endAllDisplayTime; }; this.dataGroup = new DisplayGroup1(); this.defineDataGroup = function(displayDelay, x, y, vw, bc, vc, lc) { this.displayDelay = displayDelay; this.dataGroup.define(this.componentID, this.componentType, x, y, vw, bc, vc, lc); }; this.dataGroup.addValue(this.entryEntityID, "Entry ID", "integer"); this.dataGroup.addValue(this.countInBag, "# In Bag", "numdec", "integer"); this.dataGroup.addValue(this.exitEntityID, "Exit ID", "integer"); this.dataGroup.addValue(this.exitResidenceTime, "Resdnce Tm", "numdec", 5); this.dataGroup.addValue(this.activity, "Activity", "text"); this.assignDisplayValues = function() { this.dataGroup.valueList[0].value = this.entryEntityID; this.dataGroup.valueList[1].value = this.countInBag; this.dataGroup.valueList[2].value = this.exitEntityID; this.dataGroup.valueList[3].value = this.exitResidenceTime; this.dataGroup.valueList[4].value = this.activity; if (this.exclusive) { if (this.openStatus) { this.dataGroup.setBorderColor("#00FF00"); } else { this.dataGroup.setBorderColor("#FF0000"); } } }; this.drawData = function() { //BagComponent this.assignDisplayValues(); this.dataGroup.drawBasic(); }; this.defineGraphic = function(graphic) { this.graphic = graphic; }; this.updateGraphic = function() { this.graphic.setTraverseValue(this.countInProcess); this.graphic.setCountValue(this.countInBag); //if (this.exclusive) { // if (this.openStatus) { // this.graphic.setBorderColor("#00FF00"); // } else { // this.graphic.setBorderColor("#FF0000"); // } //} }; this.isOpen = function() { //BagComponent if (this.exclusive) { if (this.currentCount() < this.maxCapacity) { this.openStatus = true; } else { this.openStatus = false; } for (var i = 0; i < this.previousComponentCount; i++) { if (this.previousComponentList[i].getComponentType() == "Path") { this.previousComponentList[i].setPreviousStatus(this.openStatus); //this may only be needed to determine open/closed status for display, count <=> capacity used when something is trying to enter } } } return this.openStatus; //if not exclusive should be set to true by default }; this.clearEntryDisplay = function() { //only clear display if a new one hasn't started a new timer if (globalSimClock >= this.endEntryDisplayTime) { this.entryTime = ""; this.entryEntityID = ""; } if (globalSimClock >= this.endAllDisplayTime) { this.activity = ""; } //displayProgressText("Bag entry "+this.componentID+" clears at time "+globalSimClock.toFixed(6)); }; this.clearExitDisplay = function() { //only clear display if a new one hasn't started a new timer if (globalSimClock >= this.endExitDisplayTime) { this.exitTime = ""; this.exitEntityID = ""; this.exitResidenceTime = ""; } if (globalSimClock >= this.endAllDisplayTime) { this.activity = ""; } //displayProgressText("Bag exit "+this.componentID+" clears at time "+globalSimClock.toFixed(6)); }; this.currentCount = function() { var count = 0; if (this.exclusive) { //start with entities already in component count = this.countInBag; //add entities in feeding paths for (var i = 0; i < this.previousComponentCount; i++) { if (this.previousComponentList[i].componentType == "Path") { //TODO- consider adding test for whether path is boundary component for associated exclusive group of components //do this using no-time/no-space control component to define boundary count += this.previousComponentList[i].currentCount(); } } } return count; }; this.pullFromPrevious = function() { //BagComponent var oldest = this.previousComponentList[0].getForwardAttemptTime(); var oldestIndex = 0; for (var i = 1; i < this.previousComponentCount; i++) { var age = this.previousComponentList[i].getForwardAttemptTime(); if (age < oldest) { oldestIndex = i; } } if (this.previousComponentList[oldestIndex].getComponentType() != "Path") { if (this.previousComponentList[oldestIndex].getComponentType() != "Entry") { //TODO: this should call forward entity in a way that ensures that previous component only sends entity to where it is requested and if one is available and if this is a legitimate destination this.previousComponentList[oldestIndex].forwardEntity(this.componentID); } } else { displayProgressText("Bag comp. " + this.componentID + " pulls from previous (" + oldestIndex + ") at time " + globalSimClock.toFixed(6)); this.previousComponentList[oldestIndex].pullFromPrevious(this.componentID); } }; this.nextOpen = function() { var startIndex = this.nextComponentIndex; var tempIndex = startIndex; do { tempIndex++; if (tempIndex >= this.nextComponentCount) { tempIndex = 0; } if (this.nextComponentList[tempIndex].isOpen()) { //open link found, update and return nextComponentIndex return tempIndex; } } while (tempIndex != startIndex); return -1; //no open links found, leave nextComponentIndex unchanged }; this.processComplete = function(entity) { //BagComponent this.countInProcess--; //TODO: ensure handled properly if process time is zero //prob. not applicable //figure out which entity just finished processing var tempID = entity.entityID; this.exitQueue.unshift(entity); this.entityQueue[entity.getLocalIndex()] = null; entity.setForwardAttemptTime(globalSimClock); displayProgressText("Bag comp. " + this.componentID + " entity: " + tempID + " processed at " + globalSimClock.toFixed(6)); this.forwardEntity(); //try to forward it }; //##default parameters## //this.forwardEntity = function(destIndex = -1) { //BagComponent //this.forwardEntity = function() { //BagComponent ///var routingTable = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : -1; this.forwardEntity = function(destIndex) { //BagComponent if (typeof destIndex === "undefined") {destIndex = -1;} var dest = -1; if (destIndex >= 0) { //pull request from a specific downstream component, must send entity there if (this.routingMethod == 1) { //single connection, nothing to do dest = 0; } else if (this.routingMethod == 2) { //distribution, send to any request dest = 0; while ((this.nextComponentIDList[dest] != destIndex) && (dest < this.nextComponentCount)) { //second test should not be needed, loop can't fail to return valid result dest++; } } else if (this.routingMethod == 3) { //model routing logic, TODO: don't forward if not desired destination dest = 0; while ((this.nextComponentIDList[dest] != destIndex) && (dest < this.nextComponentCount)) { //second test should not be needed, loop can't fail to return valid result dest++; } } dummy2 = 0; } else { if (this.routingMethod == 1) { //single connection if (this.nextComponentList[0].isOpen()) { dest = 0; } } else if (this.routingMethod == 2) { //distribution var nextIndex = this.nextOpen(); if (nextIndex >= 0) { dest = nextIndex; //this.nextComponentIndex = dest; } } else if (this.routingMethod == 3) { //model routing logic if (this.savedDestination >= 0) { dest = this.savedDestination; } else { dest = 0; var test = Math.random(); //need access to entity type but can't pop it off queue here var index = this.countInQueue - 1; if (index >= 0) { index = entityDiversionPercentIndex(this.entityQueue[index]); //get head item in queue and find out what type it is } else { index = 0; //nothing in queue, following code will work but nothing will be popped and processed below } while (test > this.routingTable[index][dest]) { dest++; } if (dest <= this.nextComponentCount) { if (!this.nextComponentList[dest].isOpen()) { dest = -1; } } else { alert("Bag comp. tried to assign destination with too high of an index") } if (dest >= 0) { this.savedDestination = dest; //ensure that once destination is determined for this entity that we don't keep changing it } } } else { //0 uninitialized or anything else alert("comp. " + this.componentID + " incorrect routing method: " + this.routingMethod); } } if (dest >= 0) { if (this.countInBag > this.countInProcess) { var entity = this.exitQueue.pop(); //TODO-: are we testing to ensure the next entity is really available if (entity) { //TODO-: since we've tested above this should not be necessary //calculate how long item was in process this.exitResidenceTime = globalSimClock - entity.getLocalEntryTime(); this.exitTime = globalSimClock; this.exitEntityID = entity.entityID; this.activity = "forward entity"; this.endExitDisplayTime = globalSimClock + this.displayDelay; this.endAllDisplayTime = this.endExitDisplayTime; advance(this.displayDelay, this, "clearExitDisplay"); displayProgressText("Bag comp. " + this.componentID + " forwards entity: " + this.exitEntityID + " at time " + globalSimClock.toFixed(6)); this.countInBag--; //should be open now if (this.exclusive) { displayProgressText("Bag comp. " + this.componentID + " calls pull from previous at time " + globalSimClock.toFixed(6)); if (!this.openStatus) { this.pullFromPrevious(); //TODO: call this with a modest (~1 sec) delay to account for reaction time? //may or may not successfully get an entity but should always be called } } this.isOpen(); //if (this.exclusive) { // for (var i=0; i<this.previousComponentCount; i++) { // if (this.previousComponentList[i].getComponentType() == "Path") { // this.previousComponentList[i].setPreviousStatus(this.openStatus); //this may only be needed to determine open/closed status for display, count <=> capacity used when something is trying to enter // } // } //} //this.nextComponentList[this.nextComponentIndex].receiveEntity(entity); this.nextComponentIndex = dest; this.savedDestination = -1; //clear old choice when entity successfully forwarded this.nextComponentList[dest].receiveEntity(entity); //record stats //TODO: test to ensure not going to another component in the same componentGroup //TODO: do the same thing for the ProcessComponent recordGroupStatsWrapper(this.componentGroup, entity.getComponentGroupEntryTime(), entity); } } } }; this.receiveEntity = function(entity) { //BagComponent //receive the entity entity.setLocalEntryTime(); //record time entity entered bag if (entity.getComponentGroup() != this.componentGroup) { entity.setComponentGroup(this.componentGroup); entity.setComponentGroupEntryTime(globalSimClock); recordGroupStatsSystemEntryWrapper(this.componentGroup,entity); } //figure out which parking space to use var i = 0; while (this.entityQueue[i] != null) { i++; } if (i < this.maxCapacity) { this.entityQueue[i] = entity; entity.setLocalIndex(i); } else { //this shouldn't happen alert("Bag comp. "+this.componentID+" over capacity at time "+this.globalSimClock.toFixed(6)); } entity.setForwardAttemptTime(Infinity); //TODO: figure out how to handle this entity.setPermission(false); //entity has reached end of related components group, permission no longer matters this.countInProcess++; this.countInBag++; //TODO: handle if process time is zero? this.isOpen(); //display what was done this.entryTime = globalSimClock; this.entryEntityID = entity.entityID; this.activity = "receive entity"; //set timer to clear the display after a bit this.endEntryDisplayTime = globalSimClock + this.displayDelay; this.endAllDisplayTime = this.endEntryDisplayTime; advance(this.displayDelay, this, "clearEntryDisplay"); //set timer for the process duration var pTime = this.processTime[entityProcessTimeIndex(entity,this.processTimeSwitch)]; advance(pTime, this, "processComplete",entity); displayProgressText("Bag comp. " + this.componentID + " receives entity: " + this.entryEntityID + " at time " + globalSimClock.toFixed(6)); }; this.activate = function(nextState, entity2) { if (nextState == "clearEntryDisplay") { this.clearEntryDisplay(); } else if (nextState == "clearExitDisplay") { this.clearExitDisplay(); } else if (nextState == "processComplete") { this.processComplete(entity2); } else { errorUndefinedAdvanceState(this.entityID, this.nextState); } }; //this.activate } //BagComponent |
I had to add the parameter processTimeSwitch
to allow different type indices to be used for determining the process time for each entity type. The process times are provided to the component as an array of values meant to apply to different types using the function entityProcessTimeIndex(entity,this.processTimeSwitch)
. In the primary Process components I wanted the types to be based on a combination of the entity’s residency and process speed. In the secondary group I specified the same time for all types so it didn’t matter what indices were used. I noticed the problem when I was looking at the report output for the “Parking Lot” group when there was no Process component in place after the Bag component. I had defined process times of 30, 50, and 80 units envisioning that they would apply to citizens, LPRs, and visitors, respectively, but the process times reported for those types varied in ways I didn’t expect. Then I remembered how the entityProcessTimeIndex
function worked and added a switch to it to allow different indices to be generated based on an entity’s properties. That index had to be added to the component definition for both the Bag and Process components.
Since JavaScript is so modular it should be possible to supply a custom function to the processTime
parameter that embodies any combination of indices and process time values. I’ll experiment with that tomorrow.
Here’s the updated code for drawing the display element.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
this.drawEntities = function() { var i; var drawCount; var drawColor; //if (this.parent.getComponentType() == "Path") { if (this.isPath) { drawCount = this.parent.entityQueue.length; for (i=0; i<drawCount; i++) { var location = this.parent.entityQueue[i].getLocation(); drawNode(location.x,location.y,5,this.readyColor); drawNode(location.x,location.y,3,this.parent.entityQueue[i].getEntityColor()) } } else if (this.parent.getComponentType() == "Bag") { //new section drawCount = this.parent.maxCapacity; //should be < this.entityLocs.length drawColor = this.waitingColor; for (i=0; i<drawCount; i++) { if (this.parent.entityQueue[i] != null) { var xx = this.entityLocs[i].x; var yy = this.entityLocs[i].y; drawNode(this.x1+xx,this.y1+yy,5,drawColor); drawNode(this.x1+xx,this.y1+yy,3,this.parent.entityQueue[i].getEntityColor()); } } drawCount = this.entityLocs.length - this.parent.maxCapacity; if (this.parent.exitQueue.length < drawCount) { drawCount = this.parent.exitQueue.length; } drawColor = this.readyColor; for (i=0; i<drawCount; i++) { //locs for exit queue in order from maxCapacity var xx = this.entityLocs[this.parent.maxCapacity + i].x; var yy = this.entityLocs[this.parent.maxCapacity + i].y; drawNode(this.x1+xx,this.y1+yy,5,drawColor); //exit queue items in reverse order drawNode(this.x1+xx,this.y1+yy,3,this.parent.exitQueue[this.parent.exitQueue.length-1-i].getEntityColor()); } } else if (this.parent.getComponentType() != "Arrivals") { var qCount = this.countValue - this.traverseValue; if (this.countValue > this.locsCount) { drawCount = this.locsCount; } else { drawCount = this.countValue; } for (i=0; i<drawCount; i++) { if (i < qCount) { drawColor = this.readyColor; } else { drawColor = this.waitingColor; } var xx = this.entityLocs[i].x; var yy = this.entityLocs[i].y; drawNode(this.x1+xx,this.y1+yy,5,drawColor); drawNode(this.x1+xx,this.y1+yy,3,this.parent.entityQueue[i].getEntityColor()); } } }; |
I tested the Bag component with and without the subsequent Process component and everything worked as it should procedurally. I also assigned the Bag and Process components to a componentGroup
called “Parking Lot” and the reporting mechanism picked up the new group and reported it like a champ. However, I noticed that there were negative entity counts during some time intervals in the “Parking Lot” section of the report for each entity type. I traced this down to the erroneous assumption that exiting a Process or Bag component should always trigger recording an exit from a component group. This worked fine when the groups start with a Queue component and end with a Process component, but not so well when a group contained multiple Process and Bag components in series. I therefore have to add a test to see whether an entity is truly exiting a component group before triggering the exit reporting mechanism. That will be another part of tomorrow’s work.
Last but not least I’ll update the framework so it can be allowed to run until all entities have cleared the system, after a specified minimum running time. This is not always the desired behavior for analysis or reporting, so a switch setting will have to be included as well.
A Simple Discrete-Event Simulation: Part 72
Following last week’s investigations I tried moving all of the functions inside the EntityPassive object (closure) to the outside, to be attached as external prototype functions. As I discovered, in keeping with what I’ve read, attaching the functions outside of the object results in a much smaller memory footprint. With internal functions the object took up 144 bytes and with external functions it consumed 480. There are other differences as well, as indicated by the before-and-after memory comparisons generated using the Memory Inspector in Firefox.
480 bytes, by the way, is a 224-byte base allocation plus a 64-byte incremental allocation plus a 128-byte allocation, as I described in my last post. You might want to keep this mechanism in the back of your mind if you’re working on a memory-intensive application, particularly when the objects are larger or there are large numbers of objects to be allocated.
Here’s the usage for internally declared functions:
Here’s the usage for externally declared functions:
The code runs without a hitch either way.
One caution about using the Memory Inspector in Firefox: you should probably take at least two snapshots of every state. I’ve found that the first pass doesn’t generate consistent information but subsequent passes seem to get a more accurate read on what’s there.
Posted in Simulation
Tagged discrete-event sim project, JavaScript, memory management
Leave a comment