Simple Selector samples

Topics: Samples and Patterns
Coordinator
Dec 29, 2006 at 9:05 PM
Below is the basic tests to show usage of each Selector.

using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using Wjs.Pcr;
 
namespace Wjs.PcrTest
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Running PCR tests.");
            Console.WriteLine("Note the test result output may be intermixed which is expected.\n");
            
            SelectOneTest();
            SelectManyOnePortTest();
            SelectManyMultiplePortTest();
 
            JoinTest();
            JoinWhereTest();
            ChoiceTest();
            TimerPortTest();
 
            Thread.Sleep(500);  // Sleep guess to allow test output before readline.
            Console.WriteLine("\nPress enter to exit.");
            Console.ReadLine();
        }
     
        /// <summary>
        /// Test single item selector. SelectOne has a FIFO gaureentee. Only one
        /// selector will be scheduled at a time. Selector will run again automatically
        /// if message is available on port.
        /// </summary>
        static void SelectOneTest()
        {
            Port<string> p = new Port<string>();
            for (int i = 0; i < 5; i++)
                p.Push(i.ToString());
            Selector.Register(new DispatchQueue(),
                Selector.SelectOne<string>(true, p,
                    delegate(string s)
                    {
                        Console.WriteLine("SelectOneTest: {0}", s);
                    })
            );
        }
 
        static void SelectManyOnePortTest()
        {
            Port<string> p0 = new Port<string>();
            for (int i = 0; i < 5; i++)
            {
                p0.Push(i.ToString());
            }
 
            Selector.Register(new DispatchQueue(),
                Selector.SelectMany<string>(true, p0,
                    // Handler.
                    delegate(string[] sa)
                    {
                        Console.WriteLine("SelectManyOnePortTest: {0}", string.Join(",", sa));
                    }, 5,
                    // Predicate
                    delegate(string s)
                    {
                        return s.Length >= 1;
                    })
            );
        }
 
        static void SelectManyMultiplePortTest()
        {
            Port<string> p0 = new Port<string>();
            Port<string> p1 = new Port<string>();
            for (int i = 0; i < 5; i++)
            {
                p0.Push(i.ToString());
                p1.Push(i.ToString());
            }
 
            Selector.Register(new DispatchQueue(),
                Selector.SelectMany<string>(true, new Port<string>[] { p0, p1},
                    delegate(string[] sa)
                    {
                        Console.WriteLine("SelectManyMultiplePortTest: {0}", string.Join(",", sa));
                    })
            );
        }
 
        /// <summary>
        /// Basic Join between two ports gets 1 item from each port.
        /// </summary>
        static void JoinTest()
        {
            Port<string> p0 = new Port<string>();
            Port<int> p1 = new Port<int>();
            for (int i = 0; i < 5; i++)
            {
                p0.Push("Name");
                p1.Push(i);
            }
            Selector.Register(new DispatchQueue(),
                Selector.Join<string, int>(true, p0, p1,
                    delegate(string s, int i)
                    {
                        Console.WriteLine("JoinTest: ({0},{1})", s, i);
                    })
            );
        }
 
        /// <summary>
        /// Do a left handed join between two ports.
        /// </summary>
        static void JoinWhereTest()
        {
            Port<string> p0 = new Port<string>();
            Port<string> p1 = new Port<string>();
            for (int i = 0; i < 5; i++)
            {
                p0.Push("First");
                p1.Push("First");
            }
            Selector.Register(new DispatchQueue(),
                Selector.Join<string, string>(true, p0, p1,
                    delegate(string s0, string s1)
                    {
                        Console.WriteLine("JoinWhereTest: ({0},{1})", s0, s1);
                    },
                    delegate(string s0, string s1)
                    {
                        // Where predicate.
                        return true;
                    })
            );
        }
 
        /// <summary>
        /// Run the first selector that gets a message. This is an atomic operation. Other selectors in the 
        /// Choice will not run and messages will stay in the port.
        /// </summary>
        static void ChoiceTest()
        {
            Port<string> p0 = new Port<string>();
            Port<int> p1 = new Port<int>();
 
            // Only 1 will run (i.e. Choice). Selector is then disabled and will not run again.
            Selector.Register(new DispatchQueue(),
                Selector.Choice(
                    Selector.SelectOne(false, p0,
                        delegate(string s)
                        {
                            Console.WriteLine("ChoiceTest: String Handler selected:{0}", s);
                        }),
                    Selector.SelectOne(false, p1,
                        delegate(int i)
                        {
                            Console.WriteLine("ChoiceTest: Int Handler selected:{0}", i);
                        })
                )
            );
 
            p1.Push(100);
            p0.Push("PCR");
        }
 
        static void TimerPortTest()
        {
            Port<DateTime> timerPort = new Port<DateTime>();
            Port<string> somePort = new Port<string>();
            DispatchQueue dq = new DispatchQueue();
 
            // Run a selector on timeout or when "somePort" gets a message - which ever comes first.
            Selector.Register(dq,
                Selector.Choice(
                    Selector.SelectOne<string>(false, somePort,
                        delegate(string s)
                        {
                            Console.WriteLine("TimerPortTest: somePort got msg:{0}", s);
                        }),
                    Selector.SelectOne<DateTime>(false, timerPort,
                        delegate(DateTime dt)
                        {
                            Console.WriteLine("TimerPortTest: {0}", dt.ToString());
                        })
                )
            );
            dq.EnqueueTimer(TimeSpan.FromMilliseconds(50), timerPort);
            //somePort.Push("hello"); // Uncomment to get different result.
        }
    }
}