Main Page   Modules   Namespace List   Class Hierarchy   Alphabetical List   Data Structures   File List   Namespace Members   Data Fields   Globals   Related Pages   Examples  

Process Class Reference
[Base]

Inherits Coroutine, Observable< ProcessObserver >, and TraceProducer.

Inherited by Arrival, Assistant, Boat, Customer, CustomerGen, Matrioschka::Proc, Continuous, Shopkeeper, SimA::Master, SimA::Slave, SimB::Loop, Tide, and TimerA.


Detailed Description

Process is the base class for all user processes in a model.

Author:
Ralf Gerstenberger

Note:
Process supports Observation.

Process supports Trace.

See also:
Condition, Selection and Simulation
Process is the base class for all user defined processes in a model. A user creates a derived class for every different type of process of its model. In its classes the user has to provide the behaviour by implementing the main() method. The behaviour is executed according to an execution schedule (ExecutionList) at a certain point in simulation time. If multiple processes share the same execution time, the order of execution is defined by the way the processes have been scheduled. A process can be scheduled with respect to its priority FirstInFirstOut or LastInFirstOut at a point in time, or in relation to an already scheduled partner process (immediately after or before that process).

Example:
//----------------------------------------------------------------------------
//      Copyright (C) 2002, 2003 Humboldt-Universitaet zu Berlin
//
//      This library is free software; you can redistribute it and/or
//      modify it under the terms of the GNU Lesser General Public
//      License as published by the Free Software Foundation; either
//      version 2.1 of the License, or (at your option) any later version.
//
//      This library is distributed in the hope that it will be useful,
//      but WITHOUT ANY WARRANTY; without even the implied warranty of
//      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//      Lesser General Public License for more details.
//
//      You should have received a copy of the GNU Lesser General Public
//      License along with this library; if not, write to the Free Software
//      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//
//----------------------------------------------------------------------------
#include <odemx/base/Process.h>

#include <iostream>

using namespace std;
using namespace odemx;

//
// The first Process is a simple SimTime timer.
// It does write a '.' every full (1.0) tic in SimTime.
//
class TimerA : public Process {
public:
        //
        // We label all instances of this class 'TimerA'.
        // ODEMx changes the label on its own to prevent 
        // confusion. It will add a number to the label
        // if more than one Timer is created in the
        // simulation. This procedure guarantees that
        // every label is unique in a simulation.
        //
        // When we create a Timer object we will have to
        // provide a pointer to the Simulation we want it
        // to participate with.
        //
        TimerA(Simulation* sim) : Process(sim, "TimerA") {}

        //
        // The behaviour of a user process must be defined
        // by an implementation of the pure virtual function
        // 'int main()'. The return value of this function
        // is stored and can be accessed with 'getReturnValue()'.
        // As long as 'main()' hasn't finished, the return
        // value is not valid. You can use 'hasReturned()'
        // to check this condition.
        //
        virtual int main() {
                while (true) {
                        //
                        // We use 'holdFor()' to let the SimTime pass.
                        // 'holdFor()' requires a time period. We could
                        // also use 'activateIn()'.
                        // The difference between these two is seen if
                        // there is more than one process scheduled at
                        // the same time. 'holdFor' would than schedule
                        // the current process as the last while 'activateIn'
                        // would schedule the current process as the first one.
                        //
                        holdFor(1.0);

                        cout << '.';
                }

                return 0;
        }
};

int main(int argc, char* argv[]) {
        //
        // For this example we can use the DefaultSimulation.
        // In more complex applications it is recommended to
        // provide a custom-made simulation class.
        //
        TimerA* myTimer=new TimerA(getDefaultSimulation());

        //
        // The new process is just created by now.
        // It won't be executed because it is not activated.
        // A process can be activated by any of these methods:
        //
        // 'hold()'
        // 'holdFor()'
        // 'holdUntil()'
        // 'activate()'
        // 'activateIn()'
        // 'acitvateAt()'
        //
        // 'hold()' and 'activate()' are equal to 'holdFor(0.0)'
        // and 'activateIn(0.0)'. 'holdUntil(t)' and 'activateAt(t)'
        // are equal to 'holdFor(t-now)' and 'activateIn(t-now)'.
        //
        myTimer->activate();

        //
        // There are three ways to compute a simulation.
        // Firstly, you can 'run()' the simulation until it is
        // finished. A simulation is finished if there is no active 
        // process left or it is stopped with 'exitSimulation()'.
        // Secondly, you can compute a simulation 'step()' by step.
        // Thirdly, you can run a simulation until a given SimTime is
        // reached or passed with 'runUntil()'.
        //
        // Because TimerA is running for ever we shouldn't use
        // 'run()'. Instead we try both 'step()' and 'runUntil()'.
        //
        cout << "Basic Simulation Example" << endl;
        cout << "========================" << endl;

        for (int i=1; i<5; ++i) {
                getDefaultSimulation()->step();
                cout << endl << i << ". step time=" << getDefaultSimulation()->getTime() << endl;       
        }

        cout << endl;
        cout << "continue until SimTime 13.0 is reached or passed:";
        getDefaultSimulation()->runUntil(13.0);
        cout << endl  << "time=" << getDefaultSimulation()->getTime() << endl;

        cout << "========================" << endl;

        return 0;
}

Since:
1.0


Public Types

enum  ProcessState {
  CREATED, CURRENT, RUNNABLE, IDLE,
  TERMINATED
}
 Process states. More...


Public Member Functions

 Process (Simulation *s, Label l, ProcessObserver *o=0)
 Construction.

 ~Process ()
 Destruction.

ProcessState getProcessState () const
 Get process state.

bool hasReturned () const
 main() function status

int getReturnValue () const
 Get return value of main() function.

virtual TracegetTrace () const
 Get pointer to Trace.

ProcessQueue * getQueue () const
 Get queue.

SimTime getEnqueueTime () const
 Get enqueue-time.

SimTime getDequeueTime () const
 Get dequeue-time.

Process Scheduling
These functions are used to schedule a process in simulation time.

void activate ()
 Immediate activation.

void activateIn (SimTime t)
 Activation in (relative) time t.

void activateAt (SimTime t)
 Activation at (absolute) time t.

void activateBefore (Process *p)
 Activation before process p.

void activateAfter (Process *p)
 Activation after process p.

void hold ()
 Immediate activation (FIFO).

void holdFor (SimTime t)
 Activation (FIFO) in (relative) time t.

void holdUntil (SimTime t)
 Activation (FIFO) at (absolute) time t.

void sleep ()
 Deactivation.

virtual void interrupt ()
 Interrupt.

void cancel ()
 Termination.

Priority
If the priority is changed the position of the process in the execution schedule and in queues is updated. This can cause a switch to another process.

Priority getPriority () const
 Get priority.

Priority setPriority (Priority newPriority)
 Set new priority.

Execution Time
SimTime getExecutionTime () const
 Get execution time.

Current System State
Process * getCurrentProcess ()
 Get currently active process.

SimTime getCurrentTime ()
 Get current simtime.

SimulationgetSimulation ()
 Get simulation of this process.

Interrupt handling
An Interrupt is caused by the scheduling function interrupt(). The interrupter (Process) is stored. The interrupt-state is automatically reset by the next scheduling function called. If the Process is interrupted but the interrupter is 0 than the Process was interrupted by the simulation (environment).

bool isInterrupted () const
 Get interrupt state.

Process * getInterrupter () const
 Get process which called interrupt().

void resetInterrupt ()
 Reset interrupt state.


Static Public Attributes

Trace MarkTypes
These MarkTypes and Tags are used to trace Process events. A TraceConsumer can use these constants to identify trace events send by Process.

const MarkTypeId baseMarkId = 1000
const MarkType markCreate
const MarkType markDestroy
const MarkType markActivate
const MarkType markActivateIn
const MarkType markActivateAt
const MarkType markActivateBefore
const MarkType markActivateAfter
const MarkType markHold
const MarkType markHoldFor
const MarkType markHoldUntil
const MarkType markInterrupt
const MarkType markSleep
const MarkType markCancel
const MarkType markExecute
const MarkType markChangeProcessState
const MarkType markChangePriority
const MarkType markChangeExTime
const TagId baseTagId = 1000
const Tag tagCurrent
const Tag tagAbsSimTime
const Tag tagRelSimTime
const Tag tagPartner

Protected Member Functions

virtual int main ()=0
 User defined process behaviour.

SimTime setExecutionTime (const SimTime &time)
 Set execution time.

void execute ()
 Execution of this process.


Friends

class ExecutionList
class Simulation
class ProcessQueue
bool operator< (const Process &first, const Process &second)
 Compare operator for Process objects.


Member Enumeration Documentation

enum ProcessState
 

Process states.

A process passes different process states during its life time (between new and delete). After construction a process is CREATED. In this state a process isn't scheduled for execution and hasn't been executed yet. In RUNNABLE a process is scheduled for execution. If a process is executed its state is CURRENT. In every simulation there is no more than one process executed at a time. A process in IDLE state is not scheduled for execution. It is inactive. If a process has finished or is terminated its state is TERMINATED. Such a process is not scheduled for execution and cannot be scheduled anymore.

Enumeration values:
CREATED  Initial state.
CURRENT  Active state.
RUNNABLE  Scheduled state.
IDLE  Wait state.
TERMINATED  Final state.


Constructor & Destructor Documentation

Process Simulation s,
Label  l,
ProcessObserver o = 0
 

Construction.

Parameters:
s pointer to the Simulation object
l label of this object
o initial observer


Member Function Documentation

Process::ProcessState getProcessState  )  const
 

Get process state.

Returns:
current process state

void activate  ) 
 

Immediate activation.

This function schedules the process at the current time before all other processes with the same priority (LIFO). If the current process (A) does not have a higher priority than the process the function is called at (B) the following will happen:

  • B is scheduled before A
  • the state of A changes to RUNNABLE while the state of B changes to CURRENT
  • the main() function of A is stoped (the point is saved)
  • the main() function of B is executed from the point it has been left last time (or from the beginning)
Examples:
Base1.cpp, continuousExample.cpp, Port2.cpp, and Shop.cpp.

void activateIn SimTime  t  ) 
 

Activation in (relative) time t.

Parameters:
t delay for activation
This function schedules the process at the simtime now + t before all other processes with the same priority (LIFO). If t is 0 this function is equal to activate().

See also:
activate()
Examples:
Port2.cpp.

void activateAt SimTime  t  ) 
 

Activation at (absolute) time t.

Parameters:
t time for activation
This function schedules the process at the simtime t before all other processes with the same priority (LIFO). If t is now this function is equal to activate().

See also:
activate()

void activateBefore Process *  p  ) 
 

Activation before process p.

Parameters:
p pointer to partner process
This function schedules the process just before process p. If necessary the priority of the process is changed. If p is this process this function does nothing. If process p is not scheduled ODEMx reports an error.

void activateAfter Process *  p  ) 
 

Activation after process p.

Parameters:
p pointer to refrence process
This function schedules the process just after process p. If necessary the priority of this process is changed. If p is this process this function does nothing. If process p is not scheduled ODEMx reports an error.

void hold  ) 
 

Immediate activation (FIFO).

This function schedules the process at the current time after all other processes with the same priority.

void holdFor SimTime  t  ) 
 

Activation (FIFO) in (relative) time t.

This function schedules the process at the time now + t after all other processes with the same priority. If t is 0 this function is equal to hold().

See also:
hold()

void holdUntil SimTime  t  ) 
 

Activation (FIFO) at (absolute) time t.

This function schedules the process at simtime t after all other processes with the same priority. If t is now this function is equal to hold().

See also:
hold()

void sleep  ) 
 

Deactivation.

This function removes the process from execution schedule and changes its state into IDLE. The next RUNNABLE process in the schedule is than activated.

void interrupt  )  [virtual]
 

Interrupt.

This function interrupts the process, which results in an immediate activation [activate()]. A Process can determine whether it was interrupted by isInterrupted(). A Process must handle interrupts on its own. It should do so after activate... and hold... operation.

See also:
activateAt(), activateUntil(), holdFor() and holdUntil()

Reimplemented in Continuous.

void cancel  ) 
 

Termination.

This function terminates the process. The process is than removed from execution schedule and its state is changed to TERMINATED.

Priority getPriority  )  const
 

Get priority.

Returns:
process priority
This function returns the current process priority.

Priority setPriority Priority  newPriority  ) 
 

Set new priority.

Returns:
previous process priority
This function changes the process priority. The position in the execution schedule and in queues is effected by priority changes.

SimTime getExecutionTime  )  const
 

Get execution time.

Returns:
execution time; 0 if not RUNNABLE or CURRENT
This function returns the execution time of the process. A process is scheduled for execution at its execution time.

Process * getCurrentProcess  ) 
 

Get currently active process.

Returns:
pointer to the current process
This function returns the currently active process (the one in state CURRENT).

SimTime getCurrentTime  ) 
 

Get current simtime.

Returns:
current simulation time
This function returns the current simulation time.

Simulation * getSimulation  ) 
 

Get simulation of this process.

Returns:
pointer to simulation object of this process
This function returns the pointer to the simulation object of this process.

bool isInterrupted  )  const [inline]
 

Get interrupt state.

Returns:
interrupt state
This function returns the interrupt state of the process.

Process* getInterrupter  )  const [inline]
 

Get process which called interrupt().

Returns:
pointer to process which called interrupt()
This function returns the process which called interrupt().

void resetInterrupt  )  [inline]
 

Reset interrupt state.

Returns:
interrupt state
This function returns the interrupt state of this process. The interrupt state is reset automatically by scheduling functions.

bool hasReturned  )  const [inline]
 

main() function status

Returns:
true if the main() function has returned
This function returns true if the main() function of this process has returned (is finished).

int getReturnValue  )  const
 

Get return value of main() function.

Returns:
return value from main()
This function returns the return value of the main() function. Use the hasReturned() function to check whether the main() function has already returned.

virtual Trace* getTrace  )  const [inline, virtual]
 

Get pointer to Trace.

Returns:
pointer to Trace object

Implements TraceProducer.

ProcessQueue* getQueue  )  const [inline]
 

Get queue.

Returns:
pointer to ProcessQueue
If the process is in a queue this function returns the pointer to the queue.

SimTime getEnqueueTime  )  const [inline]
 

Get enqueue-time.

Returns:
time at which the process entered a queue
If the process is in a queue this function returns the time at which the process entered the queue. This function is used by synchronisation objects.

SimTime getDequeueTime  )  const [inline]
 

Get dequeue-time.

Returns:
time at which the process left a queue
If the process was in a queue this function returns the time it left the queue. This function is used by synchronisation objects.

virtual int main  )  [protected, pure virtual]
 

User defined process behaviour.

This function must be implemented by the user to define the Behaviour of a process. It is executed by ODEMx when the process becomes the current process. If the process calls a scheduling function (direct or indirect, at itself or another process) the main() function is delayed until the process is executed again. The sequence in which different processes are executed is determined by the execution schedule.

See also:
Process Scheduling

Implemented in Continuous.

SimTime setExecutionTime const SimTime time  )  [protected]
 

Set execution time.

Returns:
previous execution time
This function is used by the library. Don't call it directly! Use the scheduling functions instead.

void execute  )  [protected]
 

Execution of this process.

This function is used by the library. Don't call is directly! The execution is managed by the process scheduling.


Friends And Related Function Documentation

bool operator< const Process &  first,
const Process &  second
[friend]
 

Compare operator for Process objects.

first
first Process
second
second Process
This operator compares two process objects. The operator returns true if the execution time of first is less than the second or if both are equal if the priority of the first is higher than the second.


Generated on Mon Aug 11 10:36:07 2003 for ODEMx by doxygen1.3