Constraint agents can be built by directly defining their waking behaviour using the notion of a ``guard''. As an example we take a resource constraint on two tasks, t1 with duration d1 and t2 with duration d2 forcing them not to overlap. The variable denotes the start time of t1 and denotes the start time of t2. Suppose we wish to define the agent constraint thus: if the domain constraints on the start time of t1 and t2 prevent t1 from starting after t2 has finished, constrain it to finish before t2 has started.
This behaviour can be expressed as follows:
agent(ST1,ST2) <==> % agent name and parameters ST1 #< ST2 + d2 | % guard ST1 + d1 #<= ST2 % bodyThe guard will keep the agent suspended until the domains of and are reduced to the point that the inequation holds for every possible value of and . When this is true, it wakes up and executes the body, invoking a new agent
ST1+d1 #<= ST2
.
Of course this guard may never become true, in case task t2 runs
before task t1. To cope with this alternative we add another guard
and body, yielding the final definition:
agent(ST1,ST2) <==> % agent name and parameters ST1 #< ST2 + d2 | % guard1 ST1 + d1 #<= ST2 ; % body1 ST2 #< ST1 + d2 | % guard2 ST2 + d2 #<= ST1 % body2This agent wakes up as soon as either of the guards are satisfied, and executes the relevant body. As soon as one guard is satisfied, the other guard and body are discarded.