Station
. And a macro
REMEMBERING
is defined for testing the effect of monitoring
the next station's status using nso
.
const string EMPTY="E"; const string LOADING="L"; const string SENDING="S"; const string WAITING="W"; const string COLLIDED="C"; #define REMEMBERING // for testing the effect of using nso
The class Station
has several state variables: a string
m_phase
; bool init_occupied
indicating the initial
occupation state of the station, bool nso
which indicates
if the next station is occupied or not; and the constant variable
TimeSpan
loading_t
indicating the lifespan of a
state when its phase is LOADING
.
Station
has two input port pointers ipull
and
ivehicle
, one output port pointer ovehicle
. These
variables, including ports, are assigned in the constructor as
follows.
class Station: public Atomic { public: string m_phase; bool init_occupied; bool nso;//next_state_occpied const TimeSpan loading_t; InputPort* ipull, *ivehicle; OutputPort* ovehicle; Station(const string& name, bool occupied, TimeSpan lt): Atomic(name), 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
.
/*virtual*/ void init() { if(init_occupied == true) m_phase = SENDING; else m_phase = EMPTY; //cout << Name << ":" << Get_s()<<endl; }
Station::::tau()
returns the lifespan of each state; 10 for
SENDING
; loading_t
for LOADING
;
otherwise.
/*virtual*/ TimeSpan tau() const { if (m_phase == SENDING) return 10; else if (m_phase == LOADING) return loading_t; else return DBL_MAX; }
Station::delta_x
defines the input transition such that if
it receives an input through ipull
, it sets nso =
false
. At that time, if the station's phase is
WAITING
, then nso
had previously been set by
true
for remembering that the next station had been
occupied, delta_x
then changes the phase to SENDING
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
.
/*virtual*/ bool delta_x(const PortValue& x) { if( x.port == ipull) { nso = false; if( m_phase == WAITING){ #ifdef REMEMBERING nso = true; #endif m_phase = SENDING; return true; } } else if(x.port == ivehicle) { if(m_phase == EMPTY) m_phase = LOADING; else // rest cases lead to Colided! m_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 IDLE
.
/*virtual*/ void delta_y(PortValue& y) { if(m_phase == LOADING){ if(nso == true) m_phase = WAITING; else { #ifdef REMEMBERING nso = true; #endif m_phase = SENDING; } } else if(m_phase == SENDING) { y.Set(ovehicle); m_phase = EMPTY; } }The displaying function
Get_s()
is overridden to return a
string containing information about m_phase
and nso
as follows.
/*virtual*/ string Get_s() const { string str = "phase="+m_phase +",nso="; if(nso) str +="true"; else str +="false"; return str; }