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(); } } }
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#.
Player
is a concrete class derived from Atomic
which
is an abstract class. We will see the class Atomic
in
Section 2.2.2.
Port
, the reader can refer to
Section 2.1.2
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.
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.
Atomic
are
supposed to override the function init()
in which the
associated atomic model is reset to the initial state
. 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.
Atomic
should override the characteristic functions:
and
.
Player
returns the
random number from [0.1, 1.2] when the state is Send
,
otherwise it returns
that is represented by
double.MaxValue
.
Player
changes
the state Wait
to Send
when receiving the input
event receive
.
Player
generates
the output event send
, at the same time, it changes the
state Send
to Wait
.
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) } } }
Coupled
in DEVS# for the
ping-pong game.
Player
having
different initial states.
A
and
B
by calling the function AddModel
of the class
Coupled
.
A
and B
calling the function AddCP
of the
class Coupled
.
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-----
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.
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.