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.