JACK——PaintRobot Exercise9
來源:http://aosgrp.com/
?
Exercise 9
Modify the behaviour of the robot agent so that painting takes a specific period of time to complete.
?
Introduction
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.
?
Instructions
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秒鐘足夠讓其他任務執行完成。
?
Questions
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的任務是串行執行。
轉載于:https://www.cnblogs.com/6DAN_HUST/archive/2011/06/13/2079725.html
總結
以上是生活随笔為你收集整理的JACK——PaintRobot Exercise9的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 实现SQL Server 2012 镜像
- 下一篇: Windows 8让程序员们忧心忡忡