Advertisement
Guest User

Untitled

a guest
Sep 19th, 2017
427
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 36.90 KB | None | 0 0
  1. (#( EtTools)
  2.  
  3. collect: [:each | Smalltalk at: each ifAbsent: [
  4.  
  5. Application errorPrerequisite: #TlsTrafficLightSystemApp missing: each]])!
  6.  
  7.  
  8.  
  9. TlsTrafficLightSystemApp becomeDefault!
  10.  
  11. EtWorkspace subclass: #TlsWorkspace
  12.  
  13. instanceVariableNames: 'defaultLabel '
  14.  
  15. classVariableNames: ''
  16.  
  17. poolDictionaries: ''!
  18.  
  19.  
  20.  
  21. TlsTrafficLightSystemApp becomeDefault!
  22.  
  23. Application subclass: #TlsTrafficLightSystemApp
  24.  
  25. instanceVariableNames: ''
  26.  
  27. classVariableNames: 'DefaultIsShowingMessages StartTimeInSeconds '
  28.  
  29. poolDictionaries: ''!
  30.  
  31.  
  32.  
  33. TlsTrafficLightSystemApp becomeDefault!
  34.  
  35. Object subclass: #TlsTrafficSystemComponent
  36.  
  37. instanceVariableNames: 'log isShowingMessages name '
  38.  
  39. classVariableNames: ''
  40.  
  41. poolDictionaries: ''!
  42.  
  43.  
  44.  
  45. TlsTrafficLightSystemApp becomeDefault!
  46.  
  47. TlsTrafficSystemComponent subclass: #TlsDetector
  48.  
  49. instanceVariableNames: 'isVehiclePresent trafficStream '
  50.  
  51. classVariableNames: ''
  52.  
  53. poolDictionaries: ''!
  54.  
  55.  
  56.  
  57. TlsTrafficLightSystemApp becomeDefault!
  58.  
  59. TlsTrafficSystemComponent subclass: #TlsIntersection
  60.  
  61. instanceVariableNames: 'phases lastPhaseServicedIndex phasesEntitledToBeOpen phasesOpen phasesTerminated phasesNeedingService somethingHasChangedSemaphore isStopped '
  62.  
  63. classVariableNames: ''
  64.  
  65. poolDictionaries: ''!
  66.  
  67.  
  68.  
  69. TlsTrafficLightSystemApp becomeDefault!
  70.  
  71. TlsTrafficSystemComponent subclass: #TlsPhase
  72.  
  73. instanceVariableNames: 'trafficStreams trafficStreamsToMakeRed isOpen trafficStreamsToMakeGreen minimumGreenTime minimumYellowTime intersection '
  74.  
  75. classVariableNames: ''
  76.  
  77. poolDictionaries: ''!
  78.  
  79.  
  80.  
  81. TlsTrafficLightSystemApp becomeDefault!
  82.  
  83. TlsTrafficSystemComponent subclass: #TlsSignalFace
  84.  
  85. instanceVariableNames: 'lightColor '
  86.  
  87. classVariableNames: ''
  88.  
  89. poolDictionaries: ''!
  90.  
  91.  
  92.  
  93. TlsTrafficLightSystemApp becomeDefault!
  94.  
  95. TlsTrafficSystemComponent subclass: #TlsTrafficStream
  96.  
  97. instanceVariableNames: 'isRed needsService signalFace intersection phase activePhase phasesToNotifyForService '
  98.  
  99. classVariableNames: ''
  100.  
  101. poolDictionaries: ''!
  102.  
  103.  
  104.  
  105.  
  106.  
  107. TlsTrafficLightSystemApp becomeDefault!
  108.  
  109.  
  110.  
  111. !TlsDetector publicMethods !
  112.  
  113.  
  114.  
  115. log: aWorkspace
  116.  
  117. super log: aWorkspace.
  118.  
  119. log isNil ifFalse: [
  120.  
  121. log defaultLabel: (self name), ': Autodetect (close to cancel)'.].
  122.  
  123.  
  124.  
  125. !
  126.  
  127.  
  128.  
  129. testDetectionAt: anArrayOfSecondCounts
  130.  
  131. "Detect a vehicle at the indicated seconds"
  132.  
  133.  
  134.  
  135. log isNil ifTrue: [ self error: 'No log'].
  136.  
  137. anArrayOfSecondCounts do: [ :secondCount |
  138.  
  139. "Fork the blocks so they execute in the background"
  140.  
  141. [
  142.  
  143. (Delay forSeconds: secondCount) wait.
  144.  
  145. self vehicleDetected.
  146.  
  147. log shell isDestroyed ifFalse: [
  148.  
  149. self show: 'Vehicle detected.'].
  150.  
  151. self isVehiclePresent: false.
  152.  
  153. ] forkAt: (Processor userBackgroundPriority).
  154.  
  155. ]. "End of do"!
  156.  
  157.  
  158.  
  159. testDetectionEvery: aMillisecondCount
  160.  
  161. "Detect a vehicle every few seconds until the log is closed..."
  162.  
  163. | detectorWorkspace |
  164.  
  165. "Fork the following block so it executes in the background"
  166.  
  167. [
  168.  
  169. log isNil ifTrue: [ self error: 'No log'].
  170.  
  171. [log shell isDestroyed] whileFalse: [
  172.  
  173. (Delay forMilliseconds: aMillisecondCount) wait.
  174.  
  175. self vehicleDetected.
  176.  
  177. log shell isDestroyed ifFalse: [
  178.  
  179. self show: 'Vehicle detected.'].
  180.  
  181. self isVehiclePresent: false.].
  182.  
  183. trafficStream isNil ifFalse: [ trafficStream stop].
  184.  
  185. ]
  186.  
  187. forkAt: (Processor userBackgroundPriority).!
  188.  
  189.  
  190.  
  191. trafficStream: aTrafficStream
  192.  
  193. trafficStream := aTrafficStream!
  194.  
  195.  
  196.  
  197. vehicleDetected
  198.  
  199. "Detect a vehicle"
  200.  
  201. self isVehiclePresent: true.
  202.  
  203. "Tell my stream"
  204.  
  205. trafficStream isNil ifFalse: [
  206.  
  207. trafficStream vehicleDetected.]! !
  208.  
  209.  
  210.  
  211. !TlsDetector privateMethods !
  212.  
  213.  
  214.  
  215. initialize
  216.  
  217. "initialize"
  218.  
  219. super initialize.
  220.  
  221. isVehiclePresent := false.
  222.  
  223. !
  224.  
  225.  
  226.  
  227. isVehiclePresent: aBoolean
  228.  
  229. isVehiclePresent := aBoolean.
  230.  
  231. ! !
  232.  
  233.  
  234.  
  235.  
  236.  
  237. !TlsIntersection publicMethods !
  238.  
  239.  
  240.  
  241. addPhaseNeedingService: aPhase
  242.  
  243. "Add a phase to the end of the line that need service"
  244.  
  245.  
  246.  
  247. (phasesNeedingService includes: aPhase) ifFalse: [
  248.  
  249. phasesNeedingService addLast: aPhase.
  250.  
  251. somethingHasChangedSemaphore signal.].
  252.  
  253. !
  254.  
  255.  
  256.  
  257. lastPhaseServicedIndex
  258.  
  259. lastPhaseServicedIndex isNil ifTrue: [
  260.  
  261. lastPhaseServicedIndex := phases size ].
  262.  
  263.  
  264.  
  265. ^lastPhaseServicedIndex!
  266.  
  267.  
  268.  
  269. lastPhaseServicedIndex: anInteger
  270.  
  271. lastPhaseServicedIndex := anInteger!
  272.  
  273.  
  274.  
  275. log: aWorkspace
  276.  
  277. super log: aWorkspace.
  278.  
  279. log isNil ifFalse: [
  280.  
  281. log defaultLabel: (self name). ].
  282.  
  283.  
  284.  
  285. !
  286.  
  287.  
  288.  
  289. minimumGreenTimeElapsedForPhase: aPhase
  290.  
  291. "Its time is up, so its not entitled to be green"
  292.  
  293.  
  294.  
  295. phasesEntitledToBeOpen remove: aPhase ifAbsent: [ nil ].
  296.  
  297.  
  298.  
  299. somethingHasChangedSemaphore signal.!
  300.  
  301.  
  302.  
  303. phases: aOrderedCollection
  304.  
  305. phases := aOrderedCollection!
  306.  
  307.  
  308.  
  309. phasesTerminated: anOrderedCollection
  310.  
  311. phasesTerminated := anOrderedCollection!
  312.  
  313.  
  314.  
  315. removeServicedPhases
  316.  
  317. "If there are no green phases, then service the next one on the queue if any "
  318.  
  319. | nextPhase nextPhaseIndex |
  320.  
  321.  
  322.  
  323. phasesNeedingService isEmpty ifTrue: [ ^nil ]. "Leave if nothing to do"
  324.  
  325.  
  326.  
  327. phasesNeedingService copy do: [ :phase |
  328.  
  329. phase stillNeedsService ifFalse: [
  330.  
  331. phasesNeedingService remove: phase ].].
  332.  
  333. !
  334.  
  335.  
  336.  
  337. startServicingPhases
  338.  
  339. "This is the main loop for the intersection
  340.  
  341. Whenever something has changed, service the next phase if needed."
  342.  
  343.  
  344.  
  345. "Fork this block in the background "
  346.  
  347.  
  348.  
  349. [
  350.  
  351. [ isStopped ] whileFalse: [
  352.  
  353.  
  354.  
  355. "Wait until something changes"
  356.  
  357. somethingHasChangedSemaphore wait.
  358.  
  359. "If there are no green phases, then service the next one on the queue if any "
  360.  
  361. self serviceNextPhase.].
  362.  
  363. self show: 'Stopped.'.
  364.  
  365. ] forkAt: Processor userBackgroundPriority.!
  366.  
  367.  
  368.  
  369. stop
  370.  
  371. "Stop servicing phases."
  372.  
  373. isStopped := true.
  374.  
  375. somethingHasChangedSemaphore signal.
  376.  
  377.  
  378.  
  379. ! !
  380.  
  381.  
  382.  
  383. !TlsIntersection privateMethods !
  384.  
  385.  
  386.  
  387. findNextPhaseToService
  388.  
  389. " Find the next phase needing service in priority order by the phases collection"
  390.  
  391. | phaseIndex nextPhase |
  392.  
  393. "Add one to the last serviced index, wrap if at the end"
  394.  
  395. phaseIndex := self lastPhaseServicedIndex.
  396.  
  397.  
  398.  
  399. 1 to: phases size do: [ :i |
  400.  
  401. "If not found yet"
  402.  
  403. nextPhase isNil ifTrue: [
  404.  
  405. "Add one to the search index, wrap if needed."
  406.  
  407. ((phaseIndex := (phaseIndex + 1)) > phases size) ifTrue: [
  408.  
  409. phaseIndex := 1 ].
  410.  
  411.  
  412.  
  413. "If we found it service it"
  414.  
  415. (phasesNeedingService includes: (phases at: phaseIndex)) ifTrue: [
  416.  
  417. nextPhase := phases at: phaseIndex.
  418.  
  419. lastPhaseServicedIndex := phaseIndex ]. ].].
  420.  
  421. ^nextPhase
  422.  
  423. !
  424.  
  425.  
  426.  
  427. initialize
  428.  
  429. super initialize.
  430.  
  431.  
  432.  
  433. phasesNeedingService := OrderedCollection new.
  434.  
  435. phasesTerminated := OrderedCollection new.
  436.  
  437. phasesEntitledToBeOpen := OrderedCollection new.
  438.  
  439. phasesOpen := OrderedCollection new.
  440.  
  441. isStopped := false.
  442.  
  443. "Caution, every intersection resets the count"
  444.  
  445. TlsTrafficLightSystemApp startTimeInSeconds: (Time now asSeconds).
  446.  
  447.  
  448.  
  449. somethingHasChangedSemaphore := Semaphore new.
  450.  
  451.  
  452.  
  453. !
  454.  
  455.  
  456.  
  457. serviceNextPhase
  458.  
  459. "If there are no green phases, then service the next one on the queue if any "
  460.  
  461. | nextPhase nextPhaseIndex |
  462.  
  463.  
  464.  
  465. phasesNeedingService isEmpty ifTrue: [ ^nil ]. "Leave if nothing to do"
  466.  
  467. phasesEntitledToBeOpen notEmpty ifTrue: [ ^nil ]. "Leave if still waiting"
  468.  
  469.  
  470.  
  471. nextPhase := self findNextPhaseToService.
  472.  
  473.  
  474.  
  475. nextPhase isNil ifTrue: [ ^nil ]. "Leave if nothing to do"
  476.  
  477.  
  478.  
  479. phasesNeedingService remove: nextPhase.
  480.  
  481.  
  482.  
  483. nextPhase isOpen ifFalse: [
  484.  
  485. phasesTerminated remove: nextPhase ifAbsent: [ nil ].
  486.  
  487.  
  488.  
  489. phasesOpen copy do: [ :phase |
  490.  
  491. phasesOpen remove: phase.
  492.  
  493. phase terminate.
  494.  
  495. phasesTerminated addLast: phase.].
  496.  
  497.  
  498.  
  499. "The phase will only open if it still needs service. Open answer true or false"
  500.  
  501. (nextPhase open) ifTrue: [
  502.  
  503. phasesEntitledToBeOpen addLast: nextPhase.
  504.  
  505. phasesOpen addLast: nextPhase.].
  506.  
  507.  
  508.  
  509. self removeServicedPhases. "Remove phases no longer waiting"
  510.  
  511. phasesNeedingService do: [ :phase |
  512.  
  513. self show: 'Waiting: ', phase name. ].
  514.  
  515. self show: '---Phase Change Complete-----------'. "Separator in log"
  516.  
  517. ].
  518.  
  519. ! !
  520.  
  521.  
  522.  
  523.  
  524.  
  525. !TlsPhase publicMethods !
  526.  
  527.  
  528.  
  529. addTrafficStreamNeedingService: aTrafficStream
  530.  
  531. "One of my trafficStreams needs service, so tell the intersection that I need service"
  532.  
  533.  
  534.  
  535. intersection addPhaseNeedingService: self.
  536.  
  537.  
  538.  
  539. !
  540.  
  541. -----------------------
  542.  
  543. END OF CODE
  544.  
  545. initialize
  546.  
  547. super initialize.
  548.  
  549. isOpen := false.
  550.  
  551. !
  552.  
  553.  
  554.  
  555. intersection: anIntersection
  556.  
  557. intersection := anIntersection!
  558.  
  559.  
  560.  
  561. isOpen
  562.  
  563. "Answer true if I am open"
  564.  
  565. ^isOpen!
  566.  
  567.  
  568.  
  569. minimumGreenTime: aTimeInSeconds
  570.  
  571. minimumGreenTime := aTimeInSeconds.
  572.  
  573. self show: 'Minimum green time is: ', aTimeInSeconds printString, ' seconds.'.!
  574.  
  575.  
  576.  
  577. minimumYellowTime
  578.  
  579. ^minimumYellowTime!
  580.  
  581.  
  582.  
  583. minimumYellowTime: aTimeInSeconds
  584.  
  585. minimumYellowTime := aTimeInSeconds.
  586.  
  587. self show: 'Minimum yellow time is: ', aTimeInSeconds printString, ' seconds.'.!
  588.  
  589.  
  590.  
  591. open
  592.  
  593. "Tell my red streams to go red and my green streams to go green"
  594.  
  595. "Answer true if I open, else false."
  596.  
  597. | areAnyInNeed completionSemaphore signalCount toMakeRed toMakeGreen |
  598.  
  599. "Check to make sure a traffic stream still needs service (could have been serviced already)"
  600.  
  601. areAnyInNeed := false.
  602.  
  603. completionSemaphore := Semaphore new.
  604.  
  605. trafficStreamsToMakeGreen do: [ :eachStream |
  606.  
  607. eachStream needsService ifTrue: [
  608.  
  609. areAnyInNeed := true ]].
  610.  
  611.  
  612.  
  613. "If no stream needs service, then do not go green and let the next phase go"
  614.  
  615. areAnyInNeed ifFalse: [
  616.  
  617. intersection minimumGreenTimeElapsedForPhase: self.
  618.  
  619. ^false ].
  620.  
  621.  
  622.  
  623. self show: 'Opening'.
  624.  
  625.  
  626.  
  627. isOpen := true.
  628.  
  629. trafficStreamsToMakeGreen do: [ :stream |
  630.  
  631. stream needsService: false. ].
  632.  
  633.  
  634.  
  635. "Fork each change to red to accommodate the delay to change from green to yellow to red"
  636.  
  637. (toMakeRed := trafficStreamsToMakeRed copy) do: [ :eachStream |
  638.  
  639. [eachStream goRed.
  640.  
  641. completionSemaphore signal ] forkAt: Processor userBackgroundPriority ].
  642.  
  643.  
  644.  
  645. "Wait for all to finish changing to red"
  646.  
  647. 1 to: toMakeRed size do: [ :i |
  648.  
  649. completionSemaphore wait ].
  650.  
  651.  
  652.  
  653. "Fork each change to green in case there is delay to change from red to green"
  654.  
  655. (toMakeGreen := trafficStreamsToMakeGreen copy) do: [ :eachStream |
  656.  
  657. [eachStream goGreenWithPhase: self.
  658.  
  659. completionSemaphore signal ] forkAt: Processor userBackgroundPriority ].
  660.  
  661.  
  662.  
  663. "Wait for all to finish changing to green"
  664.  
  665. 1 to: toMakeGreen size do: [ :i |
  666.  
  667. completionSemaphore wait ].
  668.  
  669.  
  670.  
  671. self show: 'Open'.
  672.  
  673.  
  674.  
  675. "When my minimum time is elapsed, tell the intersection."
  676.  
  677. [(Delay forSeconds: minimumGreenTime) wait.
  678.  
  679. intersection minimumGreenTimeElapsedForPhase: self
  680.  
  681. ] forkAt: Processor userBackgroundPriority.
  682.  
  683.  
  684.  
  685. ^true!
  686.  
  687.  
  688.  
  689. stillNeedsService
  690.  
  691. "Answer true if I still need service"
  692.  
  693.  
  694.  
  695. trafficStreamsToMakeGreen do: [ :stream |
  696.  
  697. stream needsService ifTrue: [ ^true ]].
  698.  
  699.  
  700.  
  701. ^false
  702.  
  703. !
  704.  
  705.  
  706.  
  707. stop
  708.  
  709. intersection stop!
  710.  
  711.  
  712.  
  713. terminate
  714.  
  715. "Terminate... going red is handled by the next phase"
  716.  
  717.  
  718.  
  719. self isOpen ifFalse: [ ^false ].
  720.  
  721.  
  722.  
  723. isOpen := false.
  724.  
  725.  
  726.  
  727. self show: 'Terminated'.
  728.  
  729. ^true !
  730.  
  731.  
  732.  
  733. trafficStreamsToMakeGreen
  734.  
  735. ^trafficStreamsToMakeGreen!
  736.  
  737.  
  738.  
  739. trafficStreamsToMakeGreen: anOrderedCollection
  740.  
  741. trafficStreamsToMakeGreen := anOrderedCollection!
  742.  
  743.  
  744.  
  745. trafficStreamsToMakeRed
  746.  
  747. ^trafficStreamsToMakeRed!
  748.  
  749.  
  750.  
  751. trafficStreamsToMakeRed: anOrderedCollection
  752.  
  753. trafficStreamsToMakeRed := anOrderedCollection! !
  754.  
  755.  
  756.  
  757.  
  758.  
  759. !TlsSignalFace publicMethods !
  760.  
  761.  
  762.  
  763. show: aMessage
  764.  
  765. "Skip messages for now"
  766.  
  767. ^self!
  768.  
  769.  
  770.  
  771. showGreen
  772.  
  773. self show: 'Light is Green'.
  774.  
  775. lightColor := ##green.!
  776.  
  777.  
  778.  
  779. showRed
  780.  
  781. self show: 'Light is Red'.
  782.  
  783. lightColor := ##red.!
  784.  
  785.  
  786.  
  787. showYellow
  788.  
  789. self show: 'Light is Yellow'.
  790.  
  791. lightColor := ##yellow.! !
  792.  
  793.  
  794.  
  795. !TlsSignalFace privateMethods !
  796.  
  797.  
  798.  
  799. lightColor
  800.  
  801. "Used for debugging only"
  802.  
  803. ^lightColor! !
  804.  
  805.  
  806.  
  807.  
  808.  
  809. !TlsTrafficLightSystemApp class publicMethods !
  810.  
  811.  
  812.  
  813. aaaNotes
  814.  
  815. "
  816.  
  817. This application is a prototype of the traffic light system described in 'The CRC Book'.
  818.  
  819. This application runs in IBM VisualAge for Smalltalk, Version 3 or higher.
  820.  
  821.  
  822.  
  823. To run the simulations, execute class methods test1 through test4a by swiping the comment
  824.  
  825. and executing. To stop the simulations, close the workspaces.
  826.  
  827.  
  828.  
  829. Some processes may take a long time to time out when running the simulations. To kill all the
  830.  
  831. processes, open a debugger with Transcript->Smalltalk Tools->Open Debugger. Then select
  832.  
  833. Processes->Debug Other. In the list, select all processes and press OK. Then close the
  834.  
  835. debugger and answer NO to the question about keeping the processes for future debugging.
  836.  
  837.  
  838.  
  839. Disclaimer: This application was written under time pressure ('OK, Dave, I'll get it
  840.  
  841. to you by Monday...'). It is a prototype to demonstrate the concepts of the chapter.
  842.  
  843. It has not been optimized or written with much emphasis on style and elegance.
  844.  
  845. I hope you find it useful for understanding Smalltalk and CRC concepts.
  846.  
  847.  
  848.  
  849. Enjoy!!
  850.  
  851.  
  852.  
  853. Lawrence C. Smith
  854.  
  855.  
  856.  
  857. smithlc@mindspring.com
  858.  
  859. 02/25/97
  860.  
  861.  
  862.  
  863. "!
  864.  
  865.  
  866.  
  867. copyright
  868.  
  869. ^'Copyright Lawrence Chad Smith, 1997 - All rights reserved'!
  870.  
  871.  
  872.  
  873. defaultIsShowingMessages
  874.  
  875. "Answer true if the default for the application is to show messages"
  876.  
  877. "Default to true"
  878.  
  879. DefaultIsShowingMessages isNil ifTrue: [
  880.  
  881. ^true ].
  882.  
  883.  
  884.  
  885. ^DefaultIsShowingMessages!
  886.  
  887.  
  888.  
  889. defaultIsShowingMessages: aBoolean
  890.  
  891. "Answer true if the default for the application is to show messages"
  892.  
  893. DefaultIsShowingMessages := aBoolean!
  894.  
  895.  
  896.  
  897. startTimeInSeconds
  898.  
  899. StartTimeInSeconds isNil ifTrue: [
  900.  
  901. StartTimeInSeconds := Time now asSeconds. ].
  902.  
  903. ^StartTimeInSeconds!
  904.  
  905.  
  906.  
  907. startTimeInSeconds: aSecondCount
  908.  
  909. StartTimeInSeconds := aSecondCount!
  910.  
  911.  
  912.  
  913. test1
  914.  
  915. "Test the system
  916.  
  917. This test just test the detector and the logs.
  918.  
  919.  
  920.  
  921. self test1.
  922.  
  923. "
  924.  
  925.  
  926.  
  927. (TlsDetector new)
  928.  
  929. name: 'Detector1';
  930.  
  931. log: (TlsWorkspace new open);
  932.  
  933. testDetectionEvery: 1000.
  934.  
  935. !
  936.  
  937.  
  938.  
  939. test2
  940.  
  941. "Test the system
  942.  
  943. self test2 ."
  944.  
  945. | log intersection detector1 phase1 trafficStream1 detector2 phase2 trafficStream2 |
  946.  
  947.  
  948.  
  949. System message: 'This test method is obsolete because the code was changed while developing test3... use test2a instead.'.
  950.  
  951. true ifTrue: [ ^nil ].
  952.  
  953.  
  954.  
  955. log := TlsWorkspace new open.
  956.  
  957. intersection := (TlsIntersection new)
  958.  
  959. name: 'Intersection';
  960.  
  961. log: log.
  962.  
  963.  
  964.  
  965. trafficStream1 := (TlsTrafficStream new)
  966.  
  967. name: 'TrafficStream1';
  968.  
  969. isRed: true;
  970.  
  971. log: log.
  972.  
  973. phase1 := (TlsPhase new)
  974.  
  975. name: 'Phase1';
  976.  
  977. intersection: intersection;
  978.  
  979. minimumGreenTime: 5;
  980.  
  981. minimumYellowTime: 1;
  982.  
  983. trafficStreams: (OrderedCollection with: trafficStream1);
  984.  
  985. log: log.
  986.  
  987.  
  988.  
  989. trafficStream1 phase: phase1.
  990.  
  991.  
  992.  
  993. detector1 := (TlsDetector new)
  994.  
  995. name: 'Detector1';
  996.  
  997. log: (TlsWorkspace new open);
  998.  
  999. trafficStream: trafficStream1;
  1000.  
  1001. testDetectionEvery: 1000;
  1002.  
  1003. yourself.
  1004.  
  1005.  
  1006.  
  1007. trafficStream2 := (TlsTrafficStream new)
  1008.  
  1009. name: 'TrafficStream2';
  1010.  
  1011. isRed: true;
  1012.  
  1013. log: log.
  1014.  
  1015. phase2 := (TlsPhase new)
  1016.  
  1017. name: 'Phase2';
  1018.  
  1019. intersection: intersection;
  1020.  
  1021. minimumGreenTime: 2;
  1022.  
  1023. minimumYellowTime: 1;
  1024.  
  1025. trafficStreams: (OrderedCollection with: trafficStream2);
  1026.  
  1027. log: log.
  1028.  
  1029.  
  1030.  
  1031. trafficStream2 phase: phase2.
  1032.  
  1033.  
  1034.  
  1035. detector2 := (TlsDetector new)
  1036.  
  1037. name: 'Detector2';
  1038.  
  1039. log: (TlsWorkspace new open);
  1040.  
  1041. trafficStream: trafficStream2;
  1042.  
  1043. testDetectionEvery: 4000;
  1044.  
  1045. yourself.
  1046.  
  1047.  
  1048.  
  1049.  
  1050.  
  1051. intersection phases: (OrderedCollection with: phase1 with: phase2);
  1052.  
  1053. phasesRed: (OrderedCollection with: phase1 with: phase2).
  1054.  
  1055.  
  1056.  
  1057. intersection startServicingPhases.
  1058.  
  1059.  
  1060.  
  1061. ^intersection!
  1062.  
  1063.  
  1064.  
  1065. test2a
  1066.  
  1067. "Test the system
  1068.  
  1069. This test has an intersection with two phases each with one trafficstream.
  1070.  
  1071. self test2a.
  1072.  
  1073.  
  1074.  
  1075. "
  1076.  
  1077. | log intersection detector1 phase1 trafficStream1 detector2 phase2 trafficStream2 |
  1078.  
  1079.  
  1080.  
  1081. log := TlsWorkspace new open.
  1082.  
  1083. intersection := (TlsIntersection new)
  1084.  
  1085. name: 'Intersection';
  1086.  
  1087. log: log.
  1088.  
  1089.  
  1090.  
  1091. trafficStream1 := (TlsTrafficStream new)
  1092.  
  1093. name: 'TrafficStream1';
  1094.  
  1095. isRed: true;
  1096.  
  1097. log: log.
  1098.  
  1099. trafficStream2 := (TlsTrafficStream new)
  1100.  
  1101. name: 'TrafficStream2';
  1102.  
  1103. isRed: true;
  1104.  
  1105. log: log.
  1106.  
  1107.  
  1108.  
  1109. phase1 := (TlsPhase new)
  1110.  
  1111. name: 'Phase1';
  1112.  
  1113. log: log;
  1114.  
  1115. intersection: intersection;
  1116.  
  1117. minimumGreenTime: 5;
  1118.  
  1119. minimumYellowTime: 1;
  1120.  
  1121. trafficStreamsToMakeGreen: (OrderedCollection with: trafficStream1);
  1122.  
  1123. trafficStreamsToMakeRed: (OrderedCollection with: trafficStream2).
  1124.  
  1125.  
  1126.  
  1127. phase2 := (TlsPhase new)
  1128.  
  1129. name: 'Phase2';
  1130.  
  1131. log: log;
  1132.  
  1133. intersection: intersection;
  1134.  
  1135. minimumGreenTime: 5;
  1136.  
  1137. minimumYellowTime: 2;
  1138.  
  1139. trafficStreamsToMakeGreen: (OrderedCollection with: trafficStream2);
  1140.  
  1141. trafficStreamsToMakeRed: (OrderedCollection with: trafficStream1).
  1142.  
  1143.  
  1144.  
  1145. trafficStream1 phasesToNotifyForService: (OrderedCollection with: phase1).
  1146.  
  1147. trafficStream2 phasesToNotifyForService: (OrderedCollection with: phase2).
  1148.  
  1149.  
  1150.  
  1151. detector1 := (TlsDetector new)
  1152.  
  1153. name: 'Detector1';
  1154.  
  1155. log: (TlsWorkspace new open);
  1156.  
  1157. trafficStream: trafficStream1;
  1158.  
  1159. testDetectionEvery: 1000;
  1160.  
  1161. yourself.
  1162.  
  1163.  
  1164.  
  1165. detector2 := (TlsDetector new)
  1166.  
  1167. name: 'Detector2';
  1168.  
  1169. log: (TlsWorkspace new open);
  1170.  
  1171. trafficStream: trafficStream2;
  1172.  
  1173. testDetectionEvery: 4000;
  1174.  
  1175. yourself.
  1176.  
  1177.  
  1178.  
  1179. intersection phases: (OrderedCollection with: phase1 with: phase2);
  1180.  
  1181. phasesTerminated: (OrderedCollection with: phase1 with: phase2).
  1182.  
  1183.  
  1184.  
  1185. intersection startServicingPhases.
  1186.  
  1187.  
  1188.  
  1189. ^intersection!
  1190.  
  1191.  
  1192.  
  1193. test3
  1194.  
  1195. "Test the system
  1196.  
  1197. self test3 .
  1198.  
  1199. This test has an intersection with four phases:
  1200.  
  1201.  
  1202.  
  1203. 1 North-South - 10 sec
  1204.  
  1205.  
  1206.  
  1207. 2 East-Left/West-Left - 10 sec
  1208.  
  1209. 3 West/West-Left - 5 sec
  1210.  
  1211. 4 East/West - 10 sec
  1212.  
  1213.  
  1214.  
  1215. There are six traffic streams
  1216.  
  1217. 1 North
  1218.  
  1219. 2 South
  1220.  
  1221. 3 East
  1222.  
  1223. 4 East-Left
  1224.  
  1225. 5 West
  1226.  
  1227. 6 West-Left
  1228.  
  1229. "
  1230.  
  1231.  
  1232.  
  1233. | log intersection detectors phases trafficStreams redStreams greenStreams phase |
  1234.  
  1235.  
  1236.  
  1237. log := TlsWorkspace new open.
  1238.  
  1239. intersection := (TlsIntersection new)
  1240.  
  1241. name: 'Intersection';
  1242.  
  1243. log: log.
  1244.  
  1245.  
  1246.  
  1247. trafficStreams := Array new: 6.
  1248.  
  1249. trafficStreams at: 1 put: ((TlsTrafficStream new)
  1250.  
  1251. name: 'North';
  1252.  
  1253. isRed: true;
  1254.  
  1255. log: log;
  1256.  
  1257. yourself).
  1258.  
  1259. trafficStreams at: 2 put: ((TlsTrafficStream new)
  1260.  
  1261. name: 'South';
  1262.  
  1263. isRed: true;
  1264.  
  1265. log: log;
  1266.  
  1267. yourself).
  1268.  
  1269.  
  1270.  
  1271. trafficStreams at: 3 put: ((TlsTrafficStream new)
  1272.  
  1273. name: 'East';
  1274.  
  1275. isRed: true;
  1276.  
  1277. log: log;
  1278.  
  1279. yourself).
  1280.  
  1281.  
  1282.  
  1283. trafficStreams at: 4 put: ((TlsTrafficStream new)
  1284.  
  1285. name: 'East-Left';
  1286.  
  1287. isRed: true;
  1288.  
  1289. log: log;
  1290.  
  1291. yourself).
  1292.  
  1293.  
  1294.  
  1295. trafficStreams at: 5 put: ((TlsTrafficStream new)
  1296.  
  1297. name: 'West';
  1298.  
  1299. isRed: true;
  1300.  
  1301. log: log;
  1302.  
  1303. yourself).
  1304.  
  1305.  
  1306.  
  1307. trafficStreams at: 6 put: ((TlsTrafficStream new)
  1308.  
  1309. name: 'West-Left';
  1310.  
  1311. isRed: true;
  1312.  
  1313. log: log;
  1314.  
  1315. yourself).
  1316.  
  1317.  
  1318.  
  1319. phases := Array new: 4.
  1320.  
  1321.  
  1322.  
  1323. "North/South Phase"
  1324.  
  1325.  
  1326.  
  1327. greenStreams := OrderedCollection with: (trafficStreams at: 1) with: (trafficStreams at: 2).
  1328.  
  1329. redStreams := (OrderedCollection new) addAll: trafficStreams;
  1330.  
  1331. removeAll: greenStreams;
  1332.  
  1333. yourself.
  1334.  
  1335. phase := (TlsPhase new)
  1336.  
  1337. name: 'North/South';
  1338.  
  1339. log: log;
  1340.  
  1341. intersection: intersection;
  1342.  
  1343. minimumGreenTime: 10;
  1344.  
  1345. minimumYellowTime: 3;
  1346.  
  1347. trafficStreamsToMakeGreen: greenStreams;
  1348.  
  1349. trafficStreamsToMakeRed: redStreams;
  1350.  
  1351. yourself.
  1352.  
  1353.  
  1354.  
  1355. phases at: 1 put: phase.
  1356.  
  1357.  
  1358.  
  1359. "East-Left/West-Left Phase"
  1360.  
  1361. greenStreams := OrderedCollection with: (trafficStreams at: 4) with: (trafficStreams at: 6).
  1362.  
  1363. redStreams := (OrderedCollection new) addAll: trafficStreams;
  1364.  
  1365. removeAll: greenStreams;
  1366.  
  1367. yourself.
  1368.  
  1369. phase := (TlsPhase new)
  1370.  
  1371. name: 'East-Left/West-Left';
  1372.  
  1373. log: log;
  1374.  
  1375. intersection: intersection;
  1376.  
  1377. minimumGreenTime: 10;
  1378.  
  1379. minimumYellowTime: 3;
  1380.  
  1381. trafficStreamsToMakeGreen: greenStreams;
  1382.  
  1383. trafficStreamsToMakeRed: redStreams;
  1384.  
  1385. yourself.
  1386.  
  1387.  
  1388.  
  1389. phases at: 2 put: phase.
  1390.  
  1391.  
  1392.  
  1393. "West/West-Left Phase"
  1394.  
  1395. greenStreams := OrderedCollection with: (trafficStreams at: 5) with: (trafficStreams at: 6).
  1396.  
  1397. redStreams := (OrderedCollection new) addAll: trafficStreams;
  1398.  
  1399. removeAll: greenStreams;
  1400.  
  1401. yourself.
  1402.  
  1403. phase := (TlsPhase new)
  1404.  
  1405. name: 'West/West-Left';
  1406.  
  1407. log: log;
  1408.  
  1409. intersection: intersection;
  1410.  
  1411. minimumGreenTime: 5;
  1412.  
  1413. minimumYellowTime: 3;
  1414.  
  1415. trafficStreamsToMakeGreen: greenStreams;
  1416.  
  1417. trafficStreamsToMakeRed: redStreams;
  1418.  
  1419. yourself.
  1420.  
  1421.  
  1422.  
  1423. phases at: 3 put: phase.
  1424.  
  1425.  
  1426.  
  1427. "East/West Phase"
  1428.  
  1429. greenStreams := OrderedCollection with: (trafficStreams at: 3) with: (trafficStreams at: 5).
  1430.  
  1431. redStreams := (OrderedCollection new) addAll: trafficStreams;
  1432.  
  1433. removeAll: greenStreams;
  1434.  
  1435. yourself.
  1436.  
  1437. phase := (TlsPhase new)
  1438.  
  1439. name: 'East/West';
  1440.  
  1441. log: log;
  1442.  
  1443. intersection: intersection;
  1444.  
  1445. minimumGreenTime: 10;
  1446.  
  1447. minimumYellowTime: 3;
  1448.  
  1449. trafficStreamsToMakeGreen: greenStreams;
  1450.  
  1451. trafficStreamsToMakeRed: redStreams;
  1452.  
  1453. yourself.
  1454.  
  1455.  
  1456.  
  1457. phases at: 4 put: phase.
  1458.  
  1459.  
  1460.  
  1461. (trafficStreams at: 1) phasesToNotifyForService: (OrderedCollection with: (phases at: 1)). "North"
  1462.  
  1463. (trafficStreams at: 2) phasesToNotifyForService: (OrderedCollection with: (phases at: 1)). "South"
  1464.  
  1465. (trafficStreams at: 3) phasesToNotifyForService: (OrderedCollection with: (phases at: 4)). "East"
  1466.  
  1467. (trafficStreams at: 4) phasesToNotifyForService: (OrderedCollection with: (phases at: 2)). "East-Left"
  1468.  
  1469. (trafficStreams at: 5) phasesToNotifyForService: (OrderedCollection with: (phases at: 3) with: (phases at: 4)). "W"
  1470.  
  1471. (trafficStreams at: 6) phasesToNotifyForService: (OrderedCollection with: (phases at: 2) with: (phases at: 3)). "W-L"
  1472.  
  1473.  
  1474.  
  1475. (TlsDetector new)
  1476.  
  1477. name: 'NorthDetector';
  1478.  
  1479. log: log;
  1480.  
  1481. trafficStream: (trafficStreams at: 1);
  1482.  
  1483. testDetectionEvery: 1000.
  1484.  
  1485.  
  1486.  
  1487. (TlsDetector new)
  1488.  
  1489. name: 'SouthDetector';
  1490.  
  1491. log: log;
  1492.  
  1493. trafficStream: (trafficStreams at: 2);
  1494.  
  1495. testDetectionEvery: 4000.
  1496.  
  1497.  
  1498.  
  1499.  
  1500.  
  1501. (TlsDetector new)
  1502.  
  1503. name: 'EastDetector';
  1504.  
  1505. log: log;
  1506.  
  1507. trafficStream: (trafficStreams at: 3);
  1508.  
  1509. testDetectionEvery: 4000.
  1510.  
  1511.  
  1512.  
  1513. (TlsDetector new)
  1514.  
  1515. name: 'EastLeftDetector';
  1516.  
  1517. log: log;
  1518.  
  1519. trafficStream: (trafficStreams at: 4);
  1520.  
  1521. testDetectionEvery: 4000.
  1522.  
  1523.  
  1524.  
  1525. (TlsDetector new)
  1526.  
  1527. name: 'WestDetector';
  1528.  
  1529. log: log;
  1530.  
  1531. trafficStream: (trafficStreams at: 5);
  1532.  
  1533. testDetectionEvery: 4000.
  1534.  
  1535.  
  1536.  
  1537. (TlsDetector new)
  1538.  
  1539. name: 'WestLeftDetector';
  1540.  
  1541. log: log;
  1542.  
  1543. trafficStream: (trafficStreams at: 6);
  1544.  
  1545. testDetectionEvery: 4000.
  1546.  
  1547.  
  1548.  
  1549.  
  1550.  
  1551. intersection phases: phases copy asOrderedCollection;
  1552.  
  1553. phasesTerminated: phases asOrderedCollection.
  1554.  
  1555.  
  1556.  
  1557. intersection startServicingPhases.
  1558.  
  1559.  
  1560.  
  1561. ^intersection
  1562.  
  1563.  
  1564.  
  1565.  
  1566.  
  1567. !
  1568.  
  1569.  
  1570.  
  1571. test4
  1572.  
  1573. "Test the system
  1574.  
  1575. self test4 .
  1576.  
  1577. This test has an intersection with four phases, and matches the input specification:
  1578.  
  1579.  
  1580.  
  1581. 1 MapleNorth+MapleSouth - 30 sec
  1582.  
  1583. 2 MainEast+MainWest - 40 sec
  1584.  
  1585. 3 MainEast+MainEastLeftTurn - 40 sec
  1586.  
  1587. 4 MainWest+MainWestLeftTurn - 40 sec
  1588.  
  1589.  
  1590.  
  1591. There are six traffic streams
  1592.  
  1593. 1 MapleSouth
  1594.  
  1595. 2 MapleNorth
  1596.  
  1597. 3 MainEast
  1598.  
  1599. 4 MainWest
  1600.  
  1601. 5 MainWestLeftTurn
  1602.  
  1603. 6 MainEastLeftTurn
  1604.  
  1605. "
  1606.  
  1607.  
  1608.  
  1609. | log detectorLog intersection detectors phases trafficStreams redStreams greenStreams phase |
  1610.  
  1611.  
  1612.  
  1613. log := TlsWorkspace new open.
  1614.  
  1615. detectorLog := TlsWorkspace new open.
  1616.  
  1617. intersection := (TlsIntersection new) name: 'Intersection';
  1618.  
  1619. log: log.
  1620.  
  1621.  
  1622.  
  1623. trafficStreams := Array new: 6.
  1624.  
  1625. trafficStreams at: 1 put: ((TlsTrafficStream new) name: 'MapleSouth(ts1)';
  1626.  
  1627. isRed: true;
  1628.  
  1629. log: log;
  1630.  
  1631. yourself).
  1632.  
  1633. trafficStreams at: 2 put: ((TlsTrafficStream new) name: 'MapleNorth(ts2)';
  1634.  
  1635. isRed: true;
  1636.  
  1637. log: log;
  1638.  
  1639. yourself).
  1640.  
  1641.  
  1642.  
  1643. trafficStreams at: 3 put: ((TlsTrafficStream new) name: 'MainEast(ts3)';
  1644.  
  1645. isRed: true;
  1646.  
  1647. log: log;
  1648.  
  1649. yourself).
  1650.  
  1651.  
  1652.  
  1653. trafficStreams at: 4 put: ((TlsTrafficStream new) name: 'MainWest(ts4)';
  1654.  
  1655. isRed: true;
  1656.  
  1657. log: log;
  1658.  
  1659. yourself).
  1660.  
  1661.  
  1662.  
  1663. trafficStreams at: 5 put: ((TlsTrafficStream new) name: 'MainEastLeftTurn(ts5)';
  1664.  
  1665. isRed: true;
  1666.  
  1667. log: log;
  1668.  
  1669. yourself).
  1670.  
  1671.  
  1672.  
  1673. trafficStreams at: 6 put: ((TlsTrafficStream new) name: 'MainWestLeftTurn(ts6)';
  1674.  
  1675. isRed: true;
  1676.  
  1677. log: log;
  1678.  
  1679. yourself).
  1680.  
  1681.  
  1682.  
  1683. phases := Array new: 4.
  1684.  
  1685.  
  1686.  
  1687. "MapleNorth+MapleSouth Phase"
  1688.  
  1689. greenStreams := OrderedCollection with: (trafficStreams at: 1) with: (trafficStreams at: 2).
  1690.  
  1691. redStreams := (OrderedCollection new) addAll: trafficStreams;
  1692.  
  1693. removeAll: greenStreams;
  1694.  
  1695. yourself.
  1696.  
  1697. phase := (TlsPhase new) name: 'MapleNorth+MapleSouth(p1)';
  1698.  
  1699. log: log;
  1700.  
  1701. intersection: intersection;
  1702.  
  1703. minimumGreenTime: 30;
  1704.  
  1705. minimumYellowTime: 10;
  1706.  
  1707. trafficStreamsToMakeGreen: greenStreams;
  1708.  
  1709. trafficStreamsToMakeRed: redStreams;
  1710.  
  1711. yourself.
  1712.  
  1713.  
  1714.  
  1715. phases at: 1 put: phase.
  1716.  
  1717.  
  1718.  
  1719. "MainEast+MainWest"
  1720.  
  1721. greenStreams := OrderedCollection with: (trafficStreams at: 3) with: (trafficStreams at: 4).
  1722.  
  1723. redStreams := (OrderedCollection new) addAll: trafficStreams;
  1724.  
  1725. removeAll: greenStreams;
  1726.  
  1727. yourself.
  1728.  
  1729. phase := (TlsPhase new)
  1730.  
  1731. name: 'MainEast+MainWest(p2)';
  1732.  
  1733. log: log;
  1734.  
  1735. intersection: intersection;
  1736.  
  1737. minimumGreenTime: 40;
  1738.  
  1739. minimumYellowTime: 10;
  1740.  
  1741. trafficStreamsToMakeGreen: greenStreams;
  1742.  
  1743. trafficStreamsToMakeRed: redStreams;
  1744.  
  1745. yourself.
  1746.  
  1747.  
  1748.  
  1749. phases at: 2 put: phase.
  1750.  
  1751.  
  1752.  
  1753. "MainEast+MainEastLeftTurn"
  1754.  
  1755. greenStreams := OrderedCollection with: (trafficStreams at: 3) with: (trafficStreams at: 5).
  1756.  
  1757. redStreams := (OrderedCollection new) addAll: trafficStreams;
  1758.  
  1759. removeAll: greenStreams;
  1760.  
  1761. yourself.
  1762.  
  1763. phase := (TlsPhase new)name: 'MainEast+MainEastLeftTurn(p3)';
  1764.  
  1765. log: log;
  1766.  
  1767. intersection: intersection;
  1768.  
  1769. minimumGreenTime: 40;
  1770.  
  1771. minimumYellowTime: 10;
  1772.  
  1773. trafficStreamsToMakeGreen: greenStreams;
  1774.  
  1775. trafficStreamsToMakeRed: redStreams;
  1776.  
  1777. yourself.
  1778.  
  1779.  
  1780.  
  1781. phases at: 3 put: phase.
  1782.  
  1783.  
  1784.  
  1785. "MainWest+MainWestLeftTurn"
  1786.  
  1787. greenStreams := OrderedCollection with: (trafficStreams at: 4) with: (trafficStreams at: 6).
  1788.  
  1789. redStreams := (OrderedCollection new) addAll: trafficStreams;
  1790.  
  1791. removeAll: greenStreams;
  1792.  
  1793. yourself.
  1794.  
  1795. phase := (TlsPhase new)
  1796.  
  1797. name: 'MainWest+MainWestLeftTurn(p4)';
  1798.  
  1799. log: log;
  1800.  
  1801. intersection: intersection;
  1802.  
  1803. minimumGreenTime: 40;
  1804.  
  1805. minimumYellowTime: 10;
  1806.  
  1807. trafficStreamsToMakeGreen: greenStreams;
  1808.  
  1809. trafficStreamsToMakeRed: redStreams;
  1810.  
  1811. yourself.
  1812.  
  1813.  
  1814.  
  1815. phases at: 4 put: phase.
  1816.  
  1817.  
  1818.  
  1819. (trafficStreams at: 1) phasesToNotifyForService: (OrderedCollection with: (phases at: 1)). "MapleN"
  1820.  
  1821. (trafficStreams at: 2) phasesToNotifyForService: (OrderedCollection with: (phases at: 1)). "MapleS"
  1822.  
  1823. (trafficStreams at: 3) phasesToNotifyForService: (OrderedCollection with: (phases at: 2) with: (phases at: 3)). "MainE"
  1824.  
  1825. (trafficStreams at: 4) phasesToNotifyForService: (OrderedCollection with: (phases at: 2) with: (phases at: 4)). "MainW"
  1826.  
  1827. (trafficStreams at: 5) phasesToNotifyForService: (OrderedCollection with: (phases at: 3)). "MainET"
  1828.  
  1829. (trafficStreams at: 6) phasesToNotifyForService: (OrderedCollection with: (phases at: 4)). "MainWT"
  1830.  
  1831.  
  1832.  
  1833. (TlsDetector new) name: 'MapleSouthDetector(dts1)';
  1834.  
  1835. log: detectorLog;
  1836.  
  1837. trafficStream: (trafficStreams at: 1);
  1838.  
  1839. testDetectionAt: #(10).
  1840.  
  1841.  
  1842.  
  1843. (TlsDetector new) name: 'MapleNorthDetector(dts2)';
  1844.  
  1845. log: detectorLog;
  1846.  
  1847. trafficStream: (trafficStreams at: 2);
  1848.  
  1849. testDetectionAt: #(243).
  1850.  
  1851.  
  1852.  
  1853. (TlsDetector new) name: 'MainEastDetector(dts3)';
  1854.  
  1855. log: detectorLog;
  1856.  
  1857. trafficStream: (trafficStreams at: 3);
  1858.  
  1859. testDetectionAt: #(75 147 232 302 345 495).
  1860.  
  1861.  
  1862.  
  1863. (TlsDetector new) name: 'MainWestDetector(dts4)';
  1864.  
  1865. log: detectorLog;
  1866.  
  1867. trafficStream: (trafficStreams at: 4);
  1868.  
  1869. testDetectionAt: #(222 303 340 504).
  1870.  
  1871.  
  1872.  
  1873. (TlsDetector new) name: 'MainEastLeftDetector(dts5)';
  1874.  
  1875. log: detectorLog;
  1876.  
  1877. trafficStream: (trafficStreams at: 5);
  1878.  
  1879. testDetectionAt: #(148 301).
  1880.  
  1881.  
  1882.  
  1883. (TlsDetector new) name: 'MainWestLeftDetector(dts6)';
  1884.  
  1885. log: detectorLog;
  1886.  
  1887. trafficStream: (trafficStreams at: 6);
  1888.  
  1889. testDetectionAt: #(45 304) .
  1890.  
  1891.  
  1892.  
  1893. detectorLog defaultLabel: 'All Detectors (close to cancel)'.
  1894.  
  1895.  
  1896.  
  1897. intersection phases: phases copy asOrderedCollection;
  1898.  
  1899. phasesTerminated: phases asOrderedCollection.
  1900.  
  1901.  
  1902.  
  1903. (trafficStreams at: 4) vehicleDetected.
  1904.  
  1905. intersection startServicingPhases.
  1906.  
  1907.  
  1908.  
  1909. ^intersection
  1910.  
  1911.  
  1912.  
  1913.  
  1914.  
  1915. !
  1916.  
  1917.  
  1918.  
  1919. test4a
  1920.  
  1921. "Test the system
  1922.  
  1923. self test4a .
  1924.  
  1925. This test has an intersection with four phases, and matches the input specification:
  1926.  
  1927.  
  1928.  
  1929. 1 MapleNorth+MapleSouth - 30 sec
  1930.  
  1931. 2 MainEast+MainWest - 40 sec
  1932.  
  1933. 3 MainEast+MainEastLeftTurn - 40 sec
  1934.  
  1935. 4 MainWest+MainWestLeftTurn - 40 sec
  1936.  
  1937.  
  1938.  
  1939. There are six traffic streams
  1940.  
  1941. 1 MapleSouth
  1942.  
  1943. 2 MapleNorth
  1944.  
  1945. 3 MainEast
  1946.  
  1947. 4 MainWest
  1948.  
  1949. 5 MainWestLeftTurn
  1950.  
  1951. 6 MainEastLeftTurn "
  1952.  
  1953.  
  1954.  
  1955. | log detectorLog intersection detectors phases trafficStreams redStreams greenStreams phase |
  1956.  
  1957.  
  1958.  
  1959. log := TlsWorkspace new open.
  1960.  
  1961. detectorLog := TlsWorkspace new open.
  1962.  
  1963. intersection := (TlsIntersection new) name: 'Intersection';
  1964.  
  1965. log: log.
  1966.  
  1967.  
  1968.  
  1969. trafficStreams := Array new: 6.
  1970.  
  1971. trafficStreams at: 1 put: ((TlsTrafficStream new) name: 'MapleSouth(ts1)';
  1972.  
  1973. isRed: true;
  1974.  
  1975. log: log;
  1976.  
  1977. yourself).
  1978.  
  1979. trafficStreams at: 2 put: ((TlsTrafficStream new) name: 'MapleNorth(ts2)';
  1980.  
  1981. isRed: true;
  1982.  
  1983. log: log;
  1984.  
  1985. yourself).
  1986.  
  1987.  
  1988.  
  1989. trafficStreams at: 3 put: ((TlsTrafficStream new) name: 'MainEast(ts3)';
  1990.  
  1991. isRed: true;
  1992.  
  1993. log: log;
  1994.  
  1995. yourself).
  1996.  
  1997.  
  1998.  
  1999. trafficStreams at: 4 put: ((TlsTrafficStream new) name: 'MainWest(ts4)';
  2000.  
  2001. isRed: true;
  2002.  
  2003. log: log;
  2004.  
  2005. yourself).
  2006.  
  2007.  
  2008.  
  2009. trafficStreams at: 5 put: ((TlsTrafficStream new) name: 'MainEastLeftTurn(ts5)';
  2010.  
  2011. isRed: true;
  2012.  
  2013. log: log;
  2014.  
  2015. yourself).
  2016.  
  2017.  
  2018.  
  2019. trafficStreams at: 6 put: ((TlsTrafficStream new) name: 'MainWestLeftTurn(ts6)';
  2020.  
  2021. isRed: true;
  2022.  
  2023. log: log;
  2024.  
  2025. yourself).
  2026.  
  2027.  
  2028.  
  2029. phases := Array new: 4.
  2030.  
  2031.  
  2032.  
  2033. "MapleNorth+MapleSouth Phase"
  2034.  
  2035. greenStreams := OrderedCollection with: (trafficStreams at: 1) with: (trafficStreams at: 2).
  2036.  
  2037. redStreams := (OrderedCollection new) addAll: trafficStreams;
  2038.  
  2039. removeAll: greenStreams;
  2040.  
  2041. yourself.
  2042.  
  2043. phase := (TlsPhase new) name: 'MapleNorth+MapleSouth(p1)';
  2044.  
  2045. log: log;
  2046.  
  2047. intersection: intersection;
  2048.  
  2049. minimumGreenTime: 30;
  2050.  
  2051. minimumYellowTime: 10;
  2052.  
  2053. trafficStreamsToMakeGreen: greenStreams;
  2054.  
  2055. trafficStreamsToMakeRed: redStreams;
  2056.  
  2057. yourself.
  2058.  
  2059.  
  2060.  
  2061. phases at: 1 put: phase.
  2062.  
  2063.  
  2064.  
  2065. "MainEast+MainWest"
  2066.  
  2067. greenStreams := OrderedCollection with: (trafficStreams at: 3) with: (trafficStreams at: 4).
  2068.  
  2069. redStreams := (OrderedCollection new) addAll: trafficStreams;
  2070.  
  2071. removeAll: greenStreams;
  2072.  
  2073. yourself.
  2074.  
  2075. phase := (TlsPhase new)
  2076.  
  2077. name: 'MainEast+MainWest(p2)';
  2078.  
  2079. log: log;
  2080.  
  2081. intersection: intersection;
  2082.  
  2083. minimumGreenTime: 40;
  2084.  
  2085. minimumYellowTime: 10;
  2086.  
  2087. trafficStreamsToMakeGreen: greenStreams;
  2088.  
  2089. trafficStreamsToMakeRed: redStreams;
  2090.  
  2091. yourself.
  2092.  
  2093.  
  2094.  
  2095. phases at: 3 put: phase.
  2096.  
  2097.  
  2098.  
  2099. "MainEast+MainEastLeftTurn"
  2100.  
  2101. greenStreams := OrderedCollection with: (trafficStreams at: 3) with: (trafficStreams at: 5).
  2102.  
  2103. redStreams := (OrderedCollection new) addAll: trafficStreams;
  2104.  
  2105. removeAll: greenStreams;
  2106.  
  2107. yourself.
  2108.  
  2109. phase := (TlsPhase new)name: 'MainEast+MainEastLeftTurn(p3)';
  2110.  
  2111. log: log;
  2112.  
  2113. intersection: intersection;
  2114.  
  2115. minimumGreenTime: 40;
  2116.  
  2117. minimumYellowTime: 10;
  2118.  
  2119. trafficStreamsToMakeGreen: greenStreams;
  2120.  
  2121. trafficStreamsToMakeRed: redStreams;
  2122.  
  2123. yourself.
  2124.  
  2125.  
  2126.  
  2127. phases at: 4 put: phase.
  2128.  
  2129.  
  2130.  
  2131. "MainWest+MainWestLeftTurn"
  2132.  
  2133. greenStreams := OrderedCollection with: (trafficStreams at: 4) with: (trafficStreams at: 6).
  2134.  
  2135. redStreams := (OrderedCollection new) addAll: trafficStreams;
  2136.  
  2137. removeAll: greenStreams;
  2138.  
  2139. yourself.
  2140.  
  2141. phase := (TlsPhase new)
  2142.  
  2143. name: 'MainWest+MainWestLeftTurn(p4)';
  2144.  
  2145. log: log;
  2146.  
  2147. intersection: intersection;
  2148.  
  2149. minimumGreenTime: 40;
  2150.  
  2151. minimumYellowTime: 10;
  2152.  
  2153. trafficStreamsToMakeGreen: greenStreams;
  2154.  
  2155. trafficStreamsToMakeRed: redStreams;
  2156.  
  2157. yourself.
  2158.  
  2159.  
  2160.  
  2161. phases at: 2 put: phase.
  2162.  
  2163.  
  2164.  
  2165. (trafficStreams at: 1) phasesToNotifyForService: (OrderedCollection with: (phases at: 1)). "MapleN"
  2166.  
  2167. (trafficStreams at: 2) phasesToNotifyForService: (OrderedCollection with: (phases at: 1)). "MapleS"
  2168.  
  2169. (trafficStreams at: 3) phasesToNotifyForService: (OrderedCollection with: (phases at: 3) with: (phases at: 4)). "MainE"
  2170.  
  2171. (trafficStreams at: 4) phasesToNotifyForService: (OrderedCollection with: (phases at: 3) with: (phases at: 2)). "MainW"
  2172.  
  2173. (trafficStreams at: 5) phasesToNotifyForService: (OrderedCollection with: (phases at: 4)). "MainET"
  2174.  
  2175. (trafficStreams at: 6) phasesToNotifyForService: (OrderedCollection with: (phases at: 2)). "MainWT"
  2176.  
  2177.  
  2178.  
  2179. (TlsDetector new) name: 'MapleSouthDetector(dts1)';
  2180.  
  2181. log: detectorLog;
  2182.  
  2183. trafficStream: (trafficStreams at: 1);
  2184.  
  2185. testDetectionAt: #(10).
  2186.  
  2187.  
  2188.  
  2189. (TlsDetector new) name: 'MapleNorthDetector(dts2)';
  2190.  
  2191. log: detectorLog;
  2192.  
  2193. trafficStream: (trafficStreams at: 2);
  2194.  
  2195. testDetectionAt: #(243).
  2196.  
  2197.  
  2198.  
  2199. (TlsDetector new) name: 'MainEastDetector(dts3)';
  2200.  
  2201. log: detectorLog;
  2202.  
  2203. trafficStream: (trafficStreams at: 3);
  2204.  
  2205. testDetectionAt: #(75 147 232 302 345 495).
  2206.  
  2207.  
  2208.  
  2209. (TlsDetector new) name: 'MainWestDetector(dts4)';
  2210.  
  2211. log: detectorLog;
  2212.  
  2213. trafficStream: (trafficStreams at: 4);
  2214.  
  2215. testDetectionAt: #(222 303 340 504).
  2216.  
  2217.  
  2218.  
  2219. (TlsDetector new) name: 'MainEastLeftDetector(dts5)';
  2220.  
  2221. log: detectorLog;
  2222.  
  2223. trafficStream: (trafficStreams at: 5);
  2224.  
  2225. testDetectionAt: #(148 301).
  2226.  
  2227.  
  2228.  
  2229. (TlsDetector new) name: 'MainWestLeftDetector(dts6)';
  2230.  
  2231. log: detectorLog;
  2232.  
  2233. trafficStream: (trafficStreams at: 6);
  2234.  
  2235. testDetectionAt: #(45 304) .
  2236.  
  2237.  
  2238.  
  2239.  
  2240.  
  2241. (trafficStreams at: 4) vehicleDetected.
  2242.  
  2243. intersection phases: phases copy asOrderedCollection;
  2244.  
  2245. phasesTerminated: phases asOrderedCollection.
  2246.  
  2247.  
  2248.  
  2249. intersection lastPhaseServicedIndex: 2. "Start with 4"
  2250.  
  2251. intersection startServicingPhases.
  2252.  
  2253.  
  2254.  
  2255. ^intersection
  2256.  
  2257.  
  2258.  
  2259.  
  2260.  
  2261. ! !
  2262.  
  2263.  
  2264.  
  2265.  
  2266.  
  2267. !TlsTrafficStream publicMethods !
  2268.  
  2269.  
  2270.  
  2271. activePhase: aPhase
  2272.  
  2273. activePhase := aPhase!
  2274.  
  2275.  
  2276.  
  2277. goGreenWithPhase: aPhase
  2278.  
  2279. "Change from red to green"
  2280.  
  2281.  
  2282.  
  2283. self isRed: false.
  2284.  
  2285. self needsService: false.
  2286.  
  2287. self signalFace showGreen.
  2288.  
  2289. self activePhase: aPhase.!
  2290.  
  2291.  
  2292.  
  2293. goRed
  2294.  
  2295. "Change from green to yellow to red in the background"
  2296.  
  2297.  
  2298.  
  2299. self isRed ifTrue: [ ^nil ]. "Already red"
  2300.  
  2301.  
  2302.  
  2303. self signalFace showYellow.
  2304.  
  2305.  
  2306.  
  2307. (Delay forSeconds: (activePhase minimumYellowTime)) wait.
  2308.  
  2309.  
  2310.  
  2311. self signalFace showRed.
  2312.  
  2313. self isRed: true.
  2314.  
  2315. !
  2316.  
  2317.  
  2318.  
  2319. phase: aPhase
  2320.  
  2321. phase := aPhase!
  2322.  
  2323.  
  2324.  
  2325. phasesToNotifyForService: anOrderedCollection
  2326.  
  2327. phasesToNotifyForService := anOrderedCollection!
  2328.  
  2329.  
  2330.  
  2331. stop
  2332.  
  2333. phasesToNotifyForService do: [ :aPhase |
  2334.  
  2335. aPhase stop. ].!
  2336.  
  2337.  
  2338.  
  2339. vehicleDetected
  2340.  
  2341. "Set whether I need service or not... if so, tell my intersection"
  2342.  
  2343. self needsService: true.
  2344.  
  2345. phasesToNotifyForService do: [ :eachPhase |
  2346.  
  2347. eachPhase addTrafficStreamNeedingService: self. ].
  2348.  
  2349. ! !
  2350.  
  2351.  
  2352.  
  2353. !TlsTrafficStream privateMethods !
  2354.  
  2355.  
  2356.  
  2357. intersection: anIntersection
  2358.  
  2359. intersection := anIntersection!
  2360.  
  2361.  
  2362.  
  2363. isRed
  2364.  
  2365. "Answer whether this stream is red... default to true"
  2366.  
  2367. "I am red if isRed is nil or true... or anything but false"
  2368.  
  2369. ^isRed ~~ false !
  2370.  
  2371.  
  2372.  
  2373. isRed: aBoolean
  2374.  
  2375. " I am red and therefore have no active phase"
  2376.  
  2377. isRed := aBoolean.
  2378.  
  2379. activePhase := nil.!
  2380.  
  2381.  
  2382.  
  2383. needsService
  2384.  
  2385. "Answer true if this stream needs service"
  2386.  
  2387.  
  2388.  
  2389. ^needsService == true!
  2390.  
  2391.  
  2392.  
  2393. needsService: aBoolean
  2394.  
  2395. "Set whether I need service or not... if so, tell my intersection"
  2396.  
  2397.  
  2398.  
  2399. needsService := aBoolean!
  2400.  
  2401.  
  2402.  
  2403. phase
  2404.  
  2405. ^phase!
  2406.  
  2407.  
  2408.  
  2409. signalFace
  2410.  
  2411. "Answer my signal face. If it's not initialize, create a new one"
  2412.  
  2413. signalFace isNil ifTrue: [
  2414.  
  2415. signalFace := TlsSignalFace new
  2416.  
  2417. log: log;
  2418.  
  2419. name: 'SignalFace for ', name;
  2420.  
  2421. yourself. ].
  2422.  
  2423.  
  2424.  
  2425. ^signalFace! !
  2426.  
  2427.  
  2428.  
  2429.  
  2430.  
  2431. !TlsTrafficSystemComponent class publicMethods !
  2432.  
  2433.  
  2434.  
  2435. new
  2436.  
  2437. "Answer a new instance which is initialized"
  2438.  
  2439. ^super new initialize! !
  2440.  
  2441.  
  2442.  
  2443.  
  2444.  
  2445. !TlsTrafficSystemComponent publicMethods !
  2446.  
  2447.  
  2448.  
  2449. initialize
  2450.  
  2451. "Initialize instance variables"
  2452.  
  2453. name := ''.!
  2454.  
  2455.  
  2456.  
  2457. isShowingMessages
  2458.  
  2459. "Answer true if this instance is showing messages... if nil then use the
  2460.  
  2461. application default "
  2462.  
  2463.  
  2464.  
  2465. isShowingMessages isNil ifTrue: [
  2466.  
  2467. ^TlsTrafficLightSystemApp defaultIsShowingMessages ].
  2468.  
  2469.  
  2470.  
  2471. ^isShowingMessages!
  2472.  
  2473.  
  2474.  
  2475. log
  2476.  
  2477. ^log!
  2478.  
  2479.  
  2480.  
  2481. log: anEtWorkspace
  2482.  
  2483. log := anEtWorkspace!
  2484.  
  2485.  
  2486.  
  2487. name
  2488.  
  2489. name isNil ifTrue: [ ^'' ].
  2490.  
  2491. ^name!
  2492.  
  2493.  
  2494.  
  2495. name: aString
  2496.  
  2497. name := aString!
  2498.  
  2499.  
  2500.  
  2501. printOn: aStream
  2502.  
  2503. "Answer a meaningful printString"
  2504.  
  2505. aStream nextPutAll: name , '(', self class name , ')'!
  2506.  
  2507.  
  2508.  
  2509. show: aMessage
  2510.  
  2511. "If we are showing messages, show the message block"
  2512.  
  2513. | time |
  2514.  
  2515.  
  2516.  
  2517. "If no log, do nothing"
  2518.  
  2519. log isNil ifTrue: [ ^nil ].
  2520.  
  2521. time := (Time now asSeconds - TlsTrafficLightSystemApp startTimeInSeconds).
  2522.  
  2523.  
  2524.  
  2525. "Otherwise show the string in the block (fork to avoid delays)"
  2526.  
  2527. self isShowingMessages ifTrue: [
  2528.  
  2529. [CwAppContext default syncExecInUI: [
  2530.  
  2531. self log shell isDestroyed ifFalse: [
  2532.  
  2533. self log cr; show: time printString, ' secs - ', self name, ': ', aMessage.
  2534.  
  2535. log confirmClose: false ] ]] forkAt: Processor userBackgroundPriority. ].
  2536.  
  2537. !
  2538.  
  2539.  
  2540.  
  2541. showBlock: aMessageBlock
  2542.  
  2543. "If we are showing messages, show the message block"
  2544.  
  2545. "A block is used to boost performance when is it expensive to build the message"
  2546.  
  2547.  
  2548.  
  2549. self show: aMessageBlock value.
  2550.  
  2551. ! !
  2552.  
  2553.  
  2554.  
  2555.  
  2556.  
  2557. !TlsWorkspace publicMethods !
  2558.  
  2559.  
  2560.  
  2561. defaultLabel
  2562.  
  2563.  
  2564.  
  2565. ^defaultLabel isNil
  2566.  
  2567. ifTrue:[ super defaultLabel ]
  2568.  
  2569. ifFalse:[ defaultLabel ]!
  2570.  
  2571.  
  2572.  
  2573. defaultLabel: aString
  2574.  
  2575. self shell title: aString.
  2576.  
  2577. defaultLabel := aString! !
  2578.  
  2579.  
  2580.  
  2581.  
  2582.  
  2583. TlsWorkspace initializeAfterLoad!
  2584.  
  2585. TlsTrafficLightSystemApp initializeAfterLoad!
  2586.  
  2587. TlsTrafficSystemComponent initializeAfterLoad!
  2588.  
  2589. TlsDetector initializeAfterLoad!
  2590.  
  2591. TlsIntersection initializeAfterLoad!
  2592.  
  2593. TlsPhase initializeAfterLoad!
  2594.  
  2595. TlsSignalFace initializeAfterLoad!
  2596.  
  2597. TlsTrafficStream initializeAfterLoad!
  2598.  
  2599.  
  2600.  
  2601. TlsTrafficLightSystemApp loaded!
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement