예제 #1
0
        public static void Main(String[] argv)
        {
            int nUnits = 8;

            Console.WriteLine("Enter number of units: ");
            nUnits = (int)Console.Read(); //Ask.Int("\nnUnits = ", 3, 10);

            // make the buttons
            One2OneChannel[] _event    = Channel.one2oneArray(nUnits);
            One2OneChannel[] configure = Channel.one2oneArray(nUnits);

            // construct an array of front-ends to a single alting barrier
            AltingBarrier[] group = AltingBarrier.create(nUnits);

            // make the gadgets
            AltingBarrierExampleProcess[] gadgets = new AltingBarrierExampleProcess[nUnits];
            for (int i = 0; i < gadgets.Length; i++)
            {
                gadgets[i] = new AltingBarrierExampleProcess(_event[i].In(), group[i], configure[i].Out());
            }

            //new CSPParallel(
            //    new IamCSProcess[]
            //    {
            //        buttons, new CSPParallel(gadgets)
            //    }
            //).run();
        }
예제 #2
0
 /*
  * This creates, and returns, another front-end to be held by a newly enrolling
  * process.  Initially, none exist - so this
  * (or {@link #expand(int)}) must be called at least once.
  * <p>
  * <i>Note: except for the first time, this method should only be called by
  * an AltingBarrier synchronised on this AltingBarrierBase.</I>
  * <p>
  *
  * @return the new front-ends.
  *
  */
 internal AltingBarrier expand()
 {
     enrolled++;
     countdown++;
     frontEnds = new AltingBarrier(this, frontEnds);
     return(frontEnds);
 }
 public AltingBarrierExampleProcess(
     AltingChannelInput click, AltingBarrier group, ChannelOutput configure
     )
 {
     this.click     = click;
     this.group     = group;
     this.configure = configure;
 }
예제 #4
0
 public AltingBarrierGadget0(
     AltingChannelInput click, AltingBarrier group, ChannelOutput configure
     )
 {
     this.click     = click;
     this.group     = group;
     this.configure = configure;
 }
 public AltingBarrierExampleProcess(
     AltingChannelInput input, AltingBarrier barrier, ChannelOutput output, int num
     )
 {
     this.input   = input;
     this.barrier = barrier;
     this.output  = output;
     this.num     = num;
 }
 public Reindeer(int number, AltingBarrier stable, ChannelOutput harness, ChannelInput harnessed,
                 ChannelInput returned, ChannelInput unharness, int holidayTime)
 {
     this.number      = number;
     this.stable      = stable;
     this.harness     = harness;
     this.harnessed   = harnessed;
     this.returned    = returned;
     this.unharness   = unharness;
     this.holidayTime = holidayTime;
 }
예제 #7
0
 /*
  * This creates, and returns, more front-ends to be held by newly enrolling
  * processes.  Initially, none exist - so this
  * (or {@link #expand()}) must be called at least once.
  * <p>
  * <i>Note: except for the first time, this method should only be called by
  * an AltingBarrier synchronised on this AltingBarrierBase.</I>
  * <p>
  *
  * @param n the number of front-ends to be created.
  * <p>
  *
  * @return the new front-ends.
  *
  */
 public AltingBarrier[] expand(int n)
 {
     AltingBarrier[] ab = new AltingBarrier[n];
     for (int i = 0; i < n; i++)
     {
         frontEnds = new AltingBarrier(this, frontEnds);
         ab[i]     = frontEnds;
     }
     enrolled  += n;
     countdown += n;
     return(ab);
 }
예제 #8
0
        public void run()
        {
            /*final*/
            Alternative clickGroup =
                new Alternative(new Guard[] { click, group });

            const int CLICK = 0, GROUP = 1;

            int n = 0;

            configure.write(n.ToString());

            while (true)
            {
                configure.write(Color.Green); // pretty

                while (!click.pending())
                {
                    n++;                           // work on our own
                    configure.write(n.ToString()); // work on our own
                }

                click.read();               // must consume the click

                configure.write(Color.Red); // pretty

                Boolean group = true;
                while (group)
                {
                    switch (clickGroup.priSelect())
                    {
                    case CLICK:
                        click.read();     // must consume the click
                        group = false;    // end group working
                        break;

                    case GROUP:
                        n--;                           // work with the group
                        configure.write(n.ToString()); // work with the group
                        break;
                    }
                }
            }
        }
예제 #9
0
 public Santa(ChannelOutput openForBusiness, ChannelOutput consultationOver, ChannelInput harness,
              ChannelOutput harnessed, ChannelOutput returned, ChannelOutputList unharnessList, AltingBarrier stable,
              ChannelInput consult, ChannelOutputList consulting, ChannelInput negotiating, ChannelOutputList consulted,
              int deliveryTime, int consultationTime)
 {
     this.openForBusiness  = openForBusiness;
     this.consultationOver = consultationOver;
     this.harness          = harness;
     this.harnessed        = harnessed;
     this.returned         = returned;
     this.unharnessList    = unharnessList;
     this.stable           = stable;
     this.consult          = consult;
     this.consulting       = consulting;
     this.negotiating      = negotiating;
     this.consulted        = consulted;
     this.deliveryTime     = deliveryTime;
     this.consultationTime = consultationTime;
 }
예제 #10
0
 /**
  * Record resignation.
  * <p>
  * <I>Note: this method should only be called by an AltingBarrier synchronised
  * on this AltingBarrierBase.</I>
  * <p>
  */
 internal void resign()
 {
     enrolled--;
     countdown--;
     if (countdown == 0)
     {
         countdown = enrolled;
         if (enrolled > 0)
         {
             AltingBarrierCoordinate.startEnable();
             AltingBarrierCoordinate.startDisable(enrolled);
             AltingBarrier fe = frontEnds;
             while (fe != null)
             {
                 fe.schedule();
                 fe = fe.next;
             }
         }
     }
 }
예제 #11
0
 /**
  * Record the offer to synchronise.
  * <P>
  * <I>Note: this method should only be called by an AltingBarrier synchronised
  * on this AltingBarrierBase.</I>
  * <p>
  *
  * @return true if all the offers are in.
  */
 internal Boolean enable()
 {
     countdown--;
     if (countdown == 0)
     {
         countdown = enrolled;
         AltingBarrierCoordinate.startDisable(enrolled);
         AltingBarrier fe = frontEnds;
         while (fe != null)
         {
             fe.schedule();
             fe = fe.next;
         }
         return(true);
     }
     else
     {
         return(false);
     }
 }
예제 #12
0
        public static void Main(String[] argv)
        {
            int nUnits = 8;

            Console.WriteLine("Enter number of units: ");
            nUnits = Int32.Parse(Console.ReadLine());

            // make the buttons

            One2OneChannel[] a = Channel.one2oneArray(nUnits);

            One2OneChannel[] b = Channel.one2oneArray(nUnits);

            ProcessesArray processesArray =
                new ProcessesArray(
                    nUnits, Channel.getInputArray(b),
                    Channel.getOutputArray(a)
                    );

            // construct an array of front-ends to a single alting barrier

            AltingBarrier[] group = AltingBarrier.create(nUnits);

            // make the gadgets
            AltingBarrierExampleProcess[] barriers = new AltingBarrierExampleProcess[nUnits];
            for (int i = 0; i < barriers.Length; i++)
            {
                barriers[i] = new AltingBarrierExampleProcess(a[i].In(), group[i], b[i].Out(), i + 65);
            }

            // run everything

            new CSPParallel(
                new IamCSProcess[]
            {
                processesArray, new CSPParallel(barriers)
            }
                ).run();
        }
예제 #13
0
        public static void main(String[] argv)
        {
            int nUnits = 8;

            nUnits = (int)Console.Read(); //Ask.Int("\nnUnits = ", 3, 10);

            // make the buttons
            One2OneChannel[] _event     = Channel.one2oneArray(nUnits);
            One2OneChannel[] configure  = Channel.one2oneArray(nUnits);
            Boolean          horizontal = true;

            FramedButtonArray buttons =
                new FramedButtonArray(
                    "AltingBarrier: Gadget 0, Demo 0", nUnits, 120, nUnits * 100,
                    horizontal, Channel.getInputArray(configure), Channel.getOutputArray(_event)
                    );

            // construct an array of front-ends to a single alting barrier
            AltingBarrier[] group = AltingBarrier.create(nUnits);

            // make the gadgets
            AltingBarrierGadget0[] gadgets = new AltingBarrierGadget0[nUnits];
            for (int i = 0; i < gadgets.Length; i++)
            {
                gadgets[i] = new AltingBarrierGadget0(_event[i].In(), group[i], configure[i].Out());
            }

            // run everything

            new CSPParallel(
                new IamCSProcess[]
            {
                buttons, new CSPParallel(gadgets)
            }
                ).run();
        }
예제 #14
0
 public AltingChannelOutputSymmetricImpl(
     AltingBarrier ab, ChannelOutput Out)
 {
     this.ab  = ab;
     this.Out = Out;
 }
        static void Main(string[] args)
        {
            // synchronisation components
            AltingBarrier[] stable    = AltingBarrier.create(10);
            var             elfGroups = Bucket.create(4);

            // Santa to Vestibule Channels
            One2OneChannel openForBusiness  = Channel.createOne2One();
            One2OneChannel consultationOver = Channel.createOne2One();

            //reindeer channels
            Any2OneChannel harness   = Channel.createAny2One();
            One2AnyChannel harnessed = Channel.createOne2Any();
            One2AnyChannel returned  = Channel.createOne2Any();

            One2OneChannel[]  unharness     = Channel.createOne2One(9);
            ChannelOutputList unharnessList = new ChannelOutputList(unharness);

            // elf channels, including Vestibule channels
            Any2OneChannel needToConsult = Channel.createAny2One();
            One2AnyChannel joinGroup     = Channel.createOne2Any();
            Any2OneChannel consult       = Channel.createAny2One();
            Any2OneChannel negotiating   = Channel.createAny2One();

            One2OneChannel[]  consulting     = Channel.createOne2One(10);
            One2OneChannel[]  consulted      = Channel.createOne2One(10);
            ChannelOutputList consultingList = new ChannelOutputList(consulting);
            ChannelOutputList consultedList  = new ChannelOutputList(consulted);

            List <IamCSProcess> grottoList = new List <IamCSProcess>();

            Reindeer[] herd = new Reindeer[9];
            for (int i = 0; i < 9; i++)
            {
                herd[i] = new Reindeer(number: i,
                                       stable: stable[i],
                                       harness: harness.Out(),
                                       harnessed:
                                       harnessed.In(),
                                       returned:
                                       returned.In(),
                                       unharness:
                                       unharness[i].In(),
                                       holidayTime:
                                       15000
                                       );
                grottoList.Add(herd[i]);
            }

            Elf[]          elves        = new Elf[9];
            IamCSProcess[] elvesNetwork = new IamCSProcess[8];
            for (int i = 0; i < 9; i++)
            {
                elves[i] = new Elf(number: i,
                                   groups: elfGroups,
                                   needToConsult: needToConsult.Out(),
                                   joinGroup:
                                   joinGroup.In(),
                                   consult:
                                   consult.Out(),
                                   consulting:
                                   consulting[i].In(),
                                   negotiating:
                                   negotiating.Out(),
                                   consulted:
                                   consulted[i].In(),
                                   workingTime:
                                   1000
                                   );
                grottoList.Add(elves[i]);
            }

            Santa santa = new Santa(openForBusiness: openForBusiness.Out(),
                                    consultationOver: consultationOver.Out(),
                                    harness:
                                    harness.In(),
                                    harnessed:
                                    harnessed.Out(),
                                    returned:
                                    returned.Out(),
                                    unharnessList:
                                    unharnessList,
                                    stable:
                                    stable[9],
                                    consult:
                                    consult.In(),
                                    consulting:
                                    consultingList,
                                    negotiating:
                                    negotiating.In(),
                                    consulted:
                                    consultedList,
                                    deliveryTime:
                                    1000,
                                    consultationTime:
                                    1000
                                    );

            Vestibule vestibule = new Vestibule(groups: elfGroups,
                                                needToConsult: needToConsult.In(),
                                                joinGroup: joinGroup.Out(),
                                                openForBusiness:
                                                openForBusiness.In(),
                                                consultationOver:
                                                consultationOver.In()
                                                );

            grottoList.Add(santa);
            grottoList.Add(vestibule);
            IamCSProcess[] grotto = grottoList.ToArray();

            new CSPParallel(grotto).run();
        }
예제 #16
0
        /**
         * This removes the given <i>front-ends</i> chained to this <i>alting</i> barrier.
         * It also nulls all of them - to prevent any attempted reuse!
         * <p>
         * <i>Note: this method should only be called by an AltingBarrier synchronised
         * on this AltingBarrierBase.</I>
         * <p>
         *
         * @param ab the <i>front-ends</i> being discarded from this barrier.
         *   This array must be unaltered from one previously delivered by
         *   an {@link #expand expand}.
         */
        internal void contract(AltingBarrier[] ab)
        {
            // assume: (ab != null) && (ab.Length > 0)
            AltingBarrier first = ab[0];

            // counts the number of front-ends whose (hopefully terminated) processes
            // were still enrolled.
            int discard = 0;

            AltingBarrier fa = null;
            AltingBarrier fb = frontEnds;

            while ((fb != null) && (fb != first))
            {
                fa = fb;
                fb = fb.next;
            }

            if (fb == null)
            {
                throw new AltingBarrierError(
                          "\n*** Could not find first front-end in AltingBarrier contract."
                          );
            }

            // Below, we will null elements of "ab" as we pass though the array.
            // However, the formal "deduce" and "invariant" comments that follow
            // relate to code that does not do this.  This is safe since the logic
            // of the code does not depend on values of "ab", subsequent to their
            // anullment.

            ab[0].baseClass = null;
            ab[0]           = null;

            // deduce: (fb == ab[0]) && (fb != null)
            // deduce: (fa == null) || (fa.next == fb)
            // deduce: (fa == null) <==> (frontEnds == ab[0])
            // deduce: (fa != null) <==> (fa.next == ab[0])

            for (int i = 1; i < ab.Length; i++)
            {
                // invariant: (fb == ab[i-1]) && (fb != null)
                if (fb.enrolled)
                {
                    discard++;
                }
                fb = fb.next;
                if (fb == null)
                {
                    throw new AltingBarrierError(
                              "\n*** Could not find second (or later) front-end in AltingBarrier contract."
                              );
                }
                if (fb != ab[i])
                {
                    throw new AltingBarrierError(
                              "\n*** Removal array in AltingBarrier contract not one delivered by expand."
                              );
                }
                // deduce: (fb == ab[i]) && (fb != null)
                ab[i].baseClass = null;
                ab[i]           = null;
            }

            // deduce: (fb == ab[(ab.Length) - 1]) && (fb != null)

            if (fb.enrolled)
            {
                discard++;
            }

            // deduce: (fa == null) <==> (frontEnds == ab[0])   [NO CHANGE]
            // deduce: (fa != null) <==> (fa.next == ab[0])     [NO CHANGE]

            if (fa == null)
            {
                frontEnds = fb.next;
            }
            else
            {
                fa.next = fb.next;
            }

            enrolled  -= discard;
            countdown -= discard;
            if (countdown == 0)
            {
                countdown = enrolled;
                if (enrolled > 0)
                {
                    AltingBarrierCoordinate.startEnable();
                    AltingBarrierCoordinate.startDisable(enrolled);
                    AltingBarrier fe = frontEnds;
                    while (fe != null)
                    {
                        fe.schedule();
                        fe = fe.next;
                    }
                }
            }
            else if (countdown < 0)
            {
                throw new JCSP_InternalError(
                          "Please report the circumstances to [email protected] - thanks!"
                          );
            }
        }
예제 #17
0
        /**
         * This removes the given <i>front-end</i> chained to this <i>alting</i> barrier.
         * It also nulls its reference to this base - to prevent any attempted reuse!
         * <p>
         * <i>Note: this method should only be called by an AltingBarrier synchronised
         * on this AltingBarrierBase.</I>
         * <p>
         *
         * @param ab the <i>front-end</i> being discarded from this barrier.
         *   This array must be unaltered from one previously delivered by
         *   an {@link #expand expand}.
         */
        internal void contract(AltingBarrier ab)
        {
            // assume: (ab != null)

            AltingBarrier fa = null;
            AltingBarrier fb = frontEnds;

            while ((fb != null) && (fb != ab))
            {
                fa = fb;
                fb = fb.next;
            }

            if (fb == null)
            {
                throw new AltingBarrierError(
                          "\n*** Could not find front-end in AltingBarrier contract."
                          );
            }

            // deduce: (fb == ab) && (fb != null)
            // deduce: (fa == null) || (fa.next == fb)
            // deduce: (fa == null) <==> (frontEnds == ab)
            // deduce: (fa != null) <==> (fa.next == ab)

            if (fa == null)
            {
                frontEnds = fb.next;
            }
            else
            {
                fa.next = fb.next;
            }

            ab.baseClass = null;

            if (ab.enrolled)
            {
                enrolled--;
                countdown--;
            }
            if (countdown == 0)
            {
                countdown = enrolled;
                if (enrolled > 0)
                {
                    AltingBarrierCoordinate.startEnable();
                    AltingBarrierCoordinate.startDisable(enrolled);
                    AltingBarrier fe = frontEnds;
                    while (fe != null)
                    {
                        fe.schedule();
                        fe = fe.next;
                    }
                }
            }
            else if (countdown < 0)
            {
                throw new JCSP_InternalError(
                          "Please report the circumstances to [email protected] - thanks!"
                          );
            }
        }