next up previous contents index
Next: Structure of DEVS# Up: DEVS Formalism and DEVS# Previous: Verbal Description of Coupled   Contents   Index


Building Ping-Pong Game using DEVS#

This section shows what DEVS# code looks like using the ping-pong game introduced in Example 1.2. When you open DEVSsharp\DEVSsharp.sln, you can find Ex_PingPong project in which there are two source files: Player.cs and Program_PingPong.cs. Let's take a look at Player.cs first.

using DEVSsharp; //--- (1)

namespace Ex_PingPong
{
    public class Player : Atomic //-- (2)
    {
        public InputPort receive; //-- (3)
        public OutputPort send;   //-- (3)
        enum PHASE { Wait, Send}
        PHASE m_phase;         //-- (4)
        bool m_width_ball;     //-- (4)
        RVofGeneralPDF rv;     //-- (4)

        public Player(string name, bool with_ball): base(name, TimeUnit.Sec)
        {
            receive = AddIP("receive");  //--(5)
            send = AddOP("send");        //--(5)
            m_width_ball = with_ball;
            rv = new RVofGeneralPDF(); //-- (6)
            init();
        }
        public override void init() {  //-- (7)
            if (m_width_ball)
                m_phase = PHASE.Send;
            else
                m_phase = PHASE.Wait;
        }
        public override double tau()  //-- (8.a)
        {
            if (m_phase == PHASE.Send)
                return rv.Uniform(0.1, 1.2);
            else
                return double.MaxValue;
        }
        public override bool delta_x(PortValue x)  //-- (8.b)
        {
            if (m_phase == PHASE.Wait && x.port == receive)
            {
                m_phase = PHASE.Send;
                return true;
            }
            else
            {
                Console.WriteLine("Do we have more than one ball?");
            }
            return false;
        }
        public override void delta_y(ref PortValue y) //-- (8.c)
        {
            if (m_phase == PHASE.Send)
            {
                y.Set(send);
                m_phase = PHASE.Wait;
            }
        }

        public override string Get_s() //-- (9)
        {
            return m_phase.ToString();
        }
    }
}

Figure 1.4: References of Ex_PingPong
Image explorer

  1. using DEVSsharp: First of all, we can find Ex_PingPong project uses a reference of DEVSsharp project which is indicated in Solution Explorer windows of Visual Studio 2005 as shown in Figure 1.4. 2.3 By using DEVSsharp, we can load information of name space, classes interfaces defined in DEVSsharp that is the kernel project name of DEVS#.

  2. Deriving from Atomic: In this example, Player is a concrete class derived from Atomic which is an abstract class. We will see the class Atomic in Section 2.2.2.

  3. Interfacing Ports: The port pointers are useful to identify the added ports. Without these pointers, we would have to search for each pointer by its name, and that can be a burden. For more information of the class Port, the reader can refer to Section 2.1.2

  4. State Variables: The derived and concrete class of atomic DEVS will have its state variables to describe its dynamic situations. In DEVS#, we use member data of C# for the state variables.

  5. Adding Interfaces: The interfacing port pointers mentioned in (5) are assigned by calling either the AddIP or the AddOP function in which memory allocations and parent assignments are performed. A set of port related functions defined at Atomic can be referred to Section 2.2.2.

  6. Random Variable: The lifespan of Send is a random variable with uniform probability density function (PDF) of [0.1,1.2]. To generate the random number, Player defines a random variable rv as a general PDF random variable in (4), and pick the uniform PDF in the range[0.1, 1.2] in tau() function in (8.a). The PDFs available in DEVS# are addressed in Section 2.4.

  7. The initial State $ s_0$ : To make the initial state $ s_0$ , all concrete classes derived from Atomic are supposed to override the function init() in which the associated atomic model is reset to the initial state $ s_0$ . In this case of Player, the initial phase can be determined as Send or Wait depending on another variable, m_width_ball which is indicating to have a ball initially or not.

  8. Characteristic Functions $ \tau, \delta_x$ and $ \delta_y$ : all concrete classes derived from Atomic should override the characteristic functions: $ \tau, \delta_x,$ and $ \delta_y$ .
    1. $ \tau$ of Player returns the random number from [0.1, 1.2] when the state is Send, otherwise it returns $ \infty$ that is represented by double.MaxValue.
    2. $ \delta_x$ of Player changes the state Wait to Send when receiving the input event receive.
    3. $ \delta_y$ of Player generates the output event send, at the same time, it changes the state Send to Wait.

  9. Displaying Status: To show the current state, we will override the Get_s() function which is supposed to return a string representing the current state. Player returns the string value of m_phase variable.

A ping-pong match we are considering here needs two players that are instances of the previous class Player. We use the coupled DEVS in Program_PingPong.cs to model the match as shown in the following codes.

using DEVSsharp;

namespace Ex_PingPong
{
    class Program_VM
    {
        static Devs MakePingPong(string name)
        {
            Coupled game = new Coupled(name); //-- (1)
            Player A = new Player("A", true); //-- (2)
            Player B = new Player("B", false);//-- (2)

            game.AddModel(A); //-- (3)
            game.AddModel(B); //-- (3)
            game.AddCP(A.send, B.receive); //-- (4)
            game.AddCP(B.send, A.receive); //-- (4)
            game.PrintCouplings(); //-- (5)
            return game;
        }

        static void Main(string[] args)
        {
            Devs md = MakePingPong("PingPong");
            SRTEngine Engine = new SRTEngine(md, 10000, null);//--(6)
            Engine.RunConsoleMenu();//--(7)
        }
    }
}

  1. Making the Ping-Pong Game as coupled DEVS: We make an instance of Coupled in DEVS# for the ping-pong game.

  2. Instancing Two Players The ping-pong game has two sub-components that are instances of Player having different initial states.

  3. Adding Components We add two players A and B by calling the function AddModel of the class Coupled.

  4. Adding Couplings We add couplings between players A and B calling the function AddCP of the class Coupled .

  5. Print Couplings Even though it is not necessary, we can call the function PrintCouplings() of Coupled to check the coupling status. The couplings of the ping-pong game are displayed as follows.

    Inside of PingPong
    
     -- External Input Coupling (EIC) --
     ------ # of EICs: 0-----
    
     -- Internal Coupling (ITC) --
    A.send --> B.receive
    B.send --> A.receive
     ------ # of ITCs: 2-----
    
     -- External Output Coupling (EOC) --
     ------ # of EOCs: 0-----
    

  6. Making a simulation engine Instancing a scalable simulation engine SRTEngine can be done by calling its constructor that needs the model supposed to be simulated. In this example the model is the coupled model of the ping-pong game, pp. For more detailed information of SRTEngine, the reader can refer to Section 2.3.

  7. Running the console menu We can use the console menu of SRTEngine by calling RunConsoleMenu(). After that, we will see the following screen on the selected console.

    DEVS#: C# Open Source of DEVS Formalism, (C) 2005~2007,
    http://xsy-csharp.sourceforge.net/DEVSsharp/
    
    The current date is 5/6/2007.
    The current time is 1:05:52 PM.
    scale, step, run, mrun, [p]ause, pause_at, [c]ontinue, reset,
    rerun, [i]nject, dtmode, animode, print, cls, log, [e]xit
    >
    

    The first part shows the header of DEVS# and current date and time. The second part shows the available command set. Even we don't have clear idea of each command, let's try`` run'' and then ``exit''.

    The detailed information of each command will be provided in Section 2.3.


next up previous contents index
Next: Structure of DEVS# Up: DEVS Formalism and DEVS# Previous: Verbal Description of Coupled   Contents   Index
MHHwang 2007-05-08