Modify the behaviour of the robot agent so that painting takes a specific period of time to complete.
In the previous exercise, parts were painted the requested colours. However, painting only took the amount of time required to print out a statement indicating that the robot was painting the part a particular colour. We will discover some interesting effects if we allow the plans involved to 'sleep' for a short time while the robot paints the part. To achieve this, we use the reasoning method statement @sleep.
The @sleep statement takes the following form:
@sleep(double timeout);
timeout represents the period of time that the agent must wait before continuing with the plan. The time-out period is specified in 'ticks' on the agent's clock. The actual time depends on the Timer that the agent is using. If the timer is the real-time clock (the default), then it represents a sleep period in seconds.
Note: @sleep only causes the current task to sleep. Any other tasks that the agent is currently executing proceed as normal.
1. Modify the 'painting' plans so that they contain an @sleep(5) statement to sleep for 5 seconds after they print the message to indicate that they are painting a part.
If editing the files as JACK files, save and close them before continuing.
2. Save the project.
3. Compile and run the program with the interaction diagram.
运行结果:
(1) test with red
(2) test with no specified colour
(3) Painting part the requested colour (1st coat) red
(4) Painting part the requested colour (2nd coat) red
(5) test with green
(6) No specified colour. Painting the part red
(7) test with green again
(8) Painting part the requested colour (1st coat) green
(9) Painting part the requested colour (2nd coat) green
(10) painting the part the current colour: green
(11) part1@%portal has been painted green
(12) part2@%portal has been painted green
(13) part3@%portal has been painted green
(14) part4@%portal has been painted green
运行结果分析:
运行结果与例8大致相同,不同之处在于最后4条(11)、(12)、(13)、(14),这是语句@sleep(5) 产生的效果,即在规划完成后阻塞5秒,由于sleep语句只阻塞当前的任务,所以其他任务照常执行,5秒钟足够让其他任务执行完成。
1. How do you explain the output?
2. How can you ensure that the robot does not begin a new task to start painting another part while it is still 'busy'?
Answers
1. 分析结果参考上述运行结果分析
2. (1)在Robot Agent中添加boolean类型的busy字段,用来标识Robot Agent是否处于忙的状态,初始值设为false,即空闲状态。
(2)在body()的推理方法中增加while语句,不断轮询判断当前Robot Agent的状态,如果为忙的状态,则阻塞1秒,形式如下:
while(self.busy)
{
System.out.println("wait for a moment");
@sleep(1);
}
修改原来的sleep时间为2秒,这样结果出来更快一些(也可不修改)
运行结果:
test with red
test with no specified colour
Painting part the requested colour (1st coat) red
Painting part the requested colour (2nd coat) red
test with green
part1@%portal has been painted red
wait for a moment
test with green again
wait for a moment
wait for a moment
wait for a moment
wait for a moment
wait for a moment
No specified colour. Painting the part red
wait for a moment
wait for a moment
part2@%portal has been painted red
wait for a moment
wait for a moment
Painting part the requested colour (1st coat) green
Painting part the requested colour (2nd coat) green
wait for a moment
part3@%portal has been painted green
wait for a moment
Painting part the requested colour (1st coat) green
Painting part the requested colour (2nd coat) green
part4@%portal has been painted green
由上可见,交给Robot Agent的任务是串行执行。