Class TickerBehaviour

  • All Implemented Interfaces:
    Serializable, Serializable

    public abstract class TickerBehaviour
    extends SimpleBehaviour
    This abstract class implements a Behaviour that periodically executes a user-defined piece of code. The user is expected to extend this class re-defining the method onTick() and including the piece of code that must be periodically executed into it.
    Author:
    Giovanni Caire - TILAB
    See Also:
    Serialized Form
    • Constructor Detail

      • TickerBehaviour

        public TickerBehaviour​(Agent a,
                               long period)
        Construct a TickerBehaviour that call its onTick() method every period ms.
        Parameters:
        a - is the pointer to the agent
        period - the tick period in ms
    • Method Detail

      • onStart

        public void onStart()
        Description copied from class: Behaviour
        This method is just an empty placeholders for subclasses. It is executed just once before starting behaviour execution. Therefore, it acts as a prolog to the task represented by this Behaviour.
        Overrides:
        onStart in class Behaviour
      • action

        public final void action()
        Description copied from class: Behaviour
        Runs the behaviour. This abstract method must be implemented by Behavioursubclasses to perform ordinary behaviour duty. An agent schedules its behaviours calling their action() method; since all the behaviours belonging to the same agent are scheduled cooperatively, this method must not enter in an endless loop and should return as soon as possible to preserve agent responsiveness. To split a long and slow task into smaller section, recursive behaviour aggregation may be used.
        Specified by:
        action in class Behaviour
        See Also:
        CompositeBehaviour
      • done

        public final boolean done()
        Description copied from class: Behaviour
        Check if this behaviour is done. The agent scheduler calls this method to see whether a Behaviour still need to be run or it has completed its task. Concrete behaviours must implement this method to return their completion state. Finished behaviours are removed from the scheduling queue, while others are kept within to be run again when their turn comes again.
        Specified by:
        done in class Behaviour
        Returns:
        true if the behaviour has completely executed.
      • onTick

        protected abstract void onTick()
        This method is invoked periodically with the period defined in the constructor. Subclasses are expected to define this method specifying the action that must be performed at every tick.
      • setFixedPeriod

        public void setFixedPeriod​(boolean fixedPeriod)
        Turn on/off the "fixed period" mode. Given a period P, when fixed period mode is off (default), this behaviour will wait for P milliseconds from the end of the n-th execution of the onTick() method to the beginning of the n+1-th execution. When fixed period is on, this behaviour will execute the onTick() method exactly every P milliseconds.
        Parameters:
        fixedPeriod - A boolean value indicating whether the fixed period mode must be turned on or off.
      • reset

        public void reset​(long period)
        This method must be called to reset the behaviour and starts again
        Parameters:
        period - the new tick time
      • reset

        public void reset()
        This method must be called to reset the behaviour and starts again
        Overrides:
        reset in class SimpleBehaviour
        Parameters:
        timeout - indicates in how many milliseconds from now the behaviour must be waken up again.
      • stop

        public void stop()
        Make this TickerBehaviour terminate. Calling stop() has the same effect as removing this TickerBehaviour, but is Thread safe
      • getTickCount

        public final int getTickCount()
        Retrieve how many ticks were done (i.e. how many times this behaviour was executed) since the last reset.
        Returns:
        The number of ticks since the last reset
      • getPeriod

        protected long getPeriod()