REMEMBERING in the first line in Station.cs file is
defined for testing the effect of monitoring the next station's
status using nso.
#define REMEMBERING // for testing the effect of using nso
The class Station has several state variables:
m_phase being one of enum PHASE
{Sending, Empty, Loading, Waiting, Collided};
bool init_occupied indicating the initial occupation
state of the station, bool nso indicating if the next
station is occupied or not; and the constant variable
double loading_t indicating the lifespan of a state
when its phase is Loading.
Station has two input port ipull and
ivehicle, one output port ovehicle. These variables,
including ports, are assigned in the constructor as follows.
public class Station: Atomic
{
enum PHASE {Sending, Empty, Loading, Waiting, Collided}
PHASE m_phase;
readonly bool init_occupied;
bool nso; //next_state_occpied
readonly double loading_t;
public InputPort ipull, ivehicle;
public OutputPort ovehicle;
public Station(string name, bool occupied, double lt):
base(name, TimeUnit.Sec)
{
init_occupied =occupied;
loading_t = lt;
nso =true;
ipull = AddIP("pull"); ivehicle = AddIP("vehicle");
ovehicle = AddOP("vehicle");
init();
}
Station::init() initializes m_phase depending on
init_occupied such that m_phase = Sending if
init_occupied is true, otherwise, m_phase =
Empty.
public override void init()
{
if(init_occupied == true)
m_phase = PHASE.Sending;
else
m_phase = PHASE.Empty;
}
Station::::tau() returns the lifespan of each state; 10 for
Sending; loading_t for Loading;
public override double tau()
{
if (m_phase == PHASE.Sending)
return 10;
else if (m_phase == PHASE.Loading)
return loading_t;
else
return double.MaxValue;
}
Station::delta_x defines the input transition such that if
it receives an input through ipull, it marks nso =
false which means that ``the next station is not occupied
any more''. At that time, if the station's phase is
Waiting, delta_x then changes the phase to
Sending. To remember the next station be occupied by this
Sending action, Station::delta_x sets nso=tru and
returns true.
When a station receives a vehicle through ivehicle port, if
phase is Empty, its phase changes into Loading;
otherwise the phase changes into Collided.
public override bool delta_x(PortValue x)
{
if( x.port == ipull) {
nso = false;
if (m_phase == PHASE.Waiting)
{
#if REMEMBERING
nso = true;
#endif
m_phase = PHASE.Sending;
return true;
}
}
else if(x.port == ivehicle) {
if(m_phase == PHASE.Empty)
m_phase = PHASE.Loading;
else // rest cases lead to Colided!
m_phase = PHASE.Collided;
return true;
}
return false;
}
Station::delta_y defines the output transition behavior
such that, at the end of Loading phase, if nso=true,
then delta_y changes the stations' phase into
Waiting. But if nso=false, delta_y marks
nso=true for remembering the next station's occupation and
changes the station's phase to Sending. At the end of
Sending phase, it sends out the vehicle through
ovehicle port and changes the station's phase to Empty.
public override void delta_y(ref PortValue y)
{
if (m_phase == PHASE.Loading)
{
if(nso == true)
m_phase = PHASE.Waiting;
else {
#if REMEMBERING
nso = true;
#endif
m_phase = PHASE.Sending;
}
}
else if (m_phase == PHASE.Sending)
{
y.Set(ovehicle);
m_phase = PHASE.Empty;
}
}
The displaying function Get_s() is overridden to return a
string containing information about m_phase and nso
as follows.
public override string Get_s()
{
return string.Format("phase= {0}, nso= {1}", m_phase, nso);
}