44 *
55 * @author Hokeun Kim ([email protected] ) 66 */
7- import type { WritablePort } from "../core/internal" ;
7+ import type { IOPort } from "../core/internal" ;
88import {
99 Log ,
1010 TimeValue ,
@@ -428,6 +428,7 @@ export class Quadrant extends Reactor {
428428 childrenBoundaries = new State < Box [ ] > ( new Array < Box > ( ) ) ;
429429
430430 totalCost = new State < number > ( 0 ) ;
431+ trollPort = new OutPort < ConfirmExitMsg > ( this ) ;
431432
432433 constructor (
433434 parent : Reactor ,
@@ -525,14 +526,19 @@ export class Quadrant extends Reactor {
525526 this . writable ( this . toSecondChild ) ,
526527 this . writable ( this . toThirdChild ) ,
527528 this . writable ( this . toFourthChild ) ,
528- this . writable ( this . toAccumulator ) ,
529+ this . toFirstChild . asConnectable ( ) ,
530+ this . toSecondChild . asConnectable ( ) ,
531+ this . toThirdChild . asConnectable ( ) ,
532+ this . toFourthChild . asConnectable ( ) ,
533+ this . toAccumulator . asConnectable ( ) ,
529534 this . localFacilities ,
530535 this . knownFacilities ,
531536 this . maxDepthOfKnownOpenFacility ,
532537 this . supportCustomers ,
533538 this . hasChildren ,
534539 this . childrenBoundaries ,
535- this . totalCost
540+ this . totalCost ,
541+ this . writable ( this . trollPort )
536542 ] ,
537543 function (
538544 this ,
@@ -544,20 +550,26 @@ export class Quadrant extends Reactor {
544550 depth ,
545551 fromProducer ,
546552 toProducer ,
547- toFirstChild ,
548- toSecondChild ,
549- toThirdChild ,
550- toFourthChild ,
551- toAccumulator ,
553+ toFirstChildW ,
554+ toSecondChildW ,
555+ toThirdChildW ,
556+ toFourthChildW ,
557+ toFirstChildC ,
558+ toSecondChildC ,
559+ toThirdChildC ,
560+ toFourthChildC ,
561+ toAccumulatorC ,
552562 localFacilities ,
553563 knownFacilities ,
554564 maxDepthOfKnownOpenFacility ,
555565 supportCustomers ,
556566 hasChildren ,
557567 childrenBoundaries ,
558- totalCost
568+ totalCost ,
569+ trollPort
559570 ) {
560571 const thisReactor = this . getReactor ( ) ;
572+ const toAccumulatorUpstreams : Array < IOPort < ConfirmExitMsg > > = [ ] ;
561573
562574 // Helper functions for mutation reaction.
563575 const notifyParentOfFacility = function ( p : Point ) : void {
@@ -622,11 +634,12 @@ export class Quadrant extends Reactor {
622634
623635 // console.log(`Children boundaries: ${childrenBoundaries.get()[0]}, ${childrenBoundaries.get()[1]}, ${childrenBoundaries.get()[2]}, ${childrenBoundaries.get()[3]}`)
624636 const accumulator = new Accumulator ( thisReactor ) ;
625- const toAccumulatorOfQuadrant = (
626- toAccumulator as unknown as WritablePort < Msg >
627- ) . getPort ( ) ;
628637 // Connect Accumulator's output to Quadrant's output.
629- this . connect ( accumulator . toNextAccumulator , toAccumulatorOfQuadrant ) ;
638+ toAccumulatorUpstreams . push ( accumulator . toNextAccumulator ) ;
639+ this . connect (
640+ accumulator . toNextAccumulator . asConnectable ( ) ,
641+ toAccumulatorC
642+ ) ;
630643
631644 const firstChild = new Quadrant (
632645 thisReactor ,
@@ -640,11 +653,11 @@ export class Quadrant extends Reactor {
640653 maxDepthOfKnownOpenFacility . get ( ) ,
641654 Point . arrayClone ( supportCustomers . get ( ) )
642655 ) ;
643- const toFirstChildPort = (
644- toFirstChild as unknown as WritablePort < Msg >
645- ) . getPort ( ) ;
646- this . connect ( toFirstChildPort , firstChild . fromProducer ) ;
647- this . connect ( firstChild . toAccumulator , accumulator . fromFirstQuadrant ) ;
656+ this . connect ( toFirstChildC , firstChild . fromProducer . asConnectable ( ) ) ;
657+ this . connect (
658+ firstChild . toAccumulator . asConnectable ( ) ,
659+ accumulator . fromFirstQuadrant . asConnectable ( )
660+ ) ;
648661
649662 const secondChild = new Quadrant (
650663 thisReactor ,
@@ -658,13 +671,13 @@ export class Quadrant extends Reactor {
658671 maxDepthOfKnownOpenFacility . get ( ) ,
659672 Point . arrayClone ( supportCustomers . get ( ) )
660673 ) ;
661- const toSecondChildPort = (
662- toSecondChild as unknown as WritablePort < Msg >
663- ) . getPort ( ) ;
664- this . connect ( toSecondChildPort , secondChild . fromProducer ) ;
665674 this . connect (
666- secondChild . toAccumulator ,
667- accumulator . fromSecondQuadrant
675+ toSecondChildC ,
676+ secondChild . fromProducer . asConnectable ( )
677+ ) ;
678+ this . connect (
679+ secondChild . toAccumulator . asConnectable ( ) ,
680+ accumulator . fromSecondQuadrant . asConnectable ( )
668681 ) ;
669682
670683 const thirdChild = new Quadrant (
@@ -679,11 +692,11 @@ export class Quadrant extends Reactor {
679692 maxDepthOfKnownOpenFacility . get ( ) ,
680693 Point . arrayClone ( supportCustomers . get ( ) )
681694 ) ;
682- const toThirdChildPort = (
683- toThirdChild as unknown as WritablePort < Msg >
684- ) . getPort ( ) ;
685- this . connect ( toThirdChildPort , thirdChild . fromProducer ) ;
686- this . connect ( thirdChild . toAccumulator , accumulator . fromThirdQuadrant ) ;
695+ this . connect ( toThirdChildC , thirdChild . fromProducer . asConnectable ( ) ) ;
696+ this . connect (
697+ thirdChild . toAccumulator . asConnectable ( ) ,
698+ accumulator . fromThirdQuadrant . asConnectable ( )
699+ ) ;
687700
688701 const fourthChild = new Quadrant (
689702 thisReactor ,
@@ -697,13 +710,13 @@ export class Quadrant extends Reactor {
697710 maxDepthOfKnownOpenFacility . get ( ) ,
698711 Point . arrayClone ( supportCustomers . get ( ) )
699712 ) ;
700- const toFourthChildPort = (
701- toFourthChild as unknown as WritablePort < Msg >
702- ) . getPort ( ) ;
703- this . connect ( toFourthChildPort , fourthChild . fromProducer ) ;
704713 this . connect (
705- fourthChild . toAccumulator ,
706- accumulator . fromFourthQuadrant
714+ toFourthChildC ,
715+ fourthChild . fromProducer . asConnectable ( )
716+ ) ;
717+ this . connect (
718+ fourthChild . toAccumulator . asConnectable ( ) ,
719+ accumulator . fromFourthQuadrant . asConnectable ( )
707720 ) ;
708721
709722 supportCustomers . set ( new Array < Point > ( ) ) ;
@@ -733,16 +746,16 @@ export class Quadrant extends Reactor {
733746 if ( childrenBoundaries . get ( ) [ i ] . contains ( point ) ) {
734747 switch ( i ) {
735748 case 0 :
736- toFirstChild . set ( msg ) ;
749+ toFirstChildW . set ( msg ) ;
737750 break ;
738751 case 1 :
739- toSecondChild . set ( msg ) ;
752+ toSecondChildW . set ( msg ) ;
740753 break ;
741754 case 2 :
742- toThirdChild . set ( msg ) ;
755+ toThirdChildW . set ( msg ) ;
743756 break ;
744757 case 3 :
745- toFourthChild . set ( msg ) ;
758+ toFourthChildW . set ( msg ) ;
746759 break ;
747760 }
748761 break ;
@@ -754,18 +767,23 @@ export class Quadrant extends Reactor {
754767 case RequestExitMsg :
755768 if ( ! hasChildren . get ( ) ) {
756769 // No children, number of facilities will be counted on parent's side.
757- toAccumulator . set (
770+ toAccumulatorUpstreams . forEach ( ( val ) => {
771+ this . disconnect ( val , toAccumulatorC . getPort ( ) ) ;
772+ } ) ;
773+ this . connect ( trollPort . getPort ( ) . asConnectable ( ) , toAccumulatorC ) ;
774+
775+ trollPort . set (
758776 new ConfirmExitMsg (
759777 0 , // facilities
760778 supportCustomers . get ( ) . length , // supportCustomers
761779 1 // quadrantReactors
762780 )
763781 ) ;
764782 } else {
765- toFirstChild . set ( msg ) ;
766- toSecondChild . set ( msg ) ;
767- toThirdChild . set ( msg ) ;
768- toFourthChild . set ( msg ) ;
783+ toFirstChildW . set ( msg ) ;
784+ toSecondChildW . set ( msg ) ;
785+ toThirdChildW . set ( msg ) ;
786+ toFourthChildW . set ( msg ) ;
769787 }
770788 break ;
771789 default :
0 commit comments