In some applications, agents are regularly activated at a predefined rate. For example, a clock animator is activated every second, and the scheduler in a time-sharing system switches control to the next process after a certain time quota elapses. In order to facilitate the description of time-related behavior of agents, B-Prolog provides timers.
In order to create a timer, use the predicate
where T is a variable, and Interval is an integer that specifies the rate of the timer. A timer runs as a separate thread. The call timer(T,Interval) binds T to a Prolog term that represents the thread. A timer starts ticking immediately after being created. It posts an event time(T) after every Interval milliseconds. A timer stops posting events after the call timer_stop(T). A stopped timer can be started again. A timer is destroyed after the call timer_kill(T) is executed.
- timer(T,Interval): T is a timer with the rate being set to Interval.
- timer(T): This is equivalent to timer(T,200).
- timer_start(T): Starts the timer T. After a timer is created, it starts ticking immediately. Therefore, it is unnecessary to start a timer with timer_start(T).
- timer_stop(T): Stops the timer.
- timer_kill(T): Kills the timer.
- timer_set_interval(T,Interval): Sets the interval of the timer T to Interval. The update is destructive, and the old value is not restored upon backtracking.
- timer_get_interval(T,Interval): Gets the interval of the timer T.