JACK——PaintRobot Exercise9

原文链接:http://www.cnblogs.com/6DAN_HUST/archive/2011/06/13/2079725.html

来源: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 and Jill

08-01

DescriptionnnEver since the incident on the hill, Jack and Jill dislike each other and wish to remain as distant as possible. Jack and Jill must attend school each day; Jack attends a boys' school while Jill attends a girls' school. Both schools start at the same time. You have been retained by their lawyers to arrange routes and a schedule that Jack and Jill will adhere to so as to maximize the closest straight-line distance between them at any time during their trip to school. nJack and Jill live in a town laid out as an n by n square grid (n <= 30). It takes 1 minute to walk from one location to an adjacent location. In maximizing the distance between Jack and Jill you need consider only the distance between the locations they visit (i.e. you need not consider any intermediate points on the path they take from grid location to grid location). Some locations are impassable due to being occupied by rivers, buildings, etc. Jack must start at his house and walk continuously until he gets to school. Jill must start at her house at the same time as Jack and walk continuously until she arrives at her school. Jack's house and school are impassable to Jill while Jill's house and school are impassable to Jack. Other grid locations that are impassable to both Jack and Jill are given in the input.nInputnnInput will consist of several test cases. Each test case will consist of n, followed by n lines with n characters representing a map of the town. In the map, Jack's house is represented by 'H', Jack's school is represented by 'S', Jill's house is represented by 'h', Jill's school is represented by 's', impassable locations are represented by '*', and all other locations are represented by '.' You may assume the normal cartographic convention that North is at the top of the page and West is to the left. A line containing 0 follows the last case.nOutputnnFor each input case you should give three lines of output containing: nthe closest that Jack and Jill come during the schedule (to 2 decimal places) nJack's route nJill's route.nnEach route is a sequence of directions that Jack or Jill should follow for each minute from the start time until arriving at school. Each direction is one of 'N', 'S', 'E', or 'W'. If several pairs of routes are possible, any one will do. You may assume there is at least one solution. Leave a blank line between the output for successive cases.nSample Inputnn10n..........n...H......n.**...s...n.**.......n.**.......n.**.......n.**.......n.**.......n...S..h..*n..........n0nSample Outputnn6.71nWWWSSSSSSSEEEnNEEENNNNNWWW 问答

Jack Straws

10-17

DescriptionnnIn the game of Jack Straws, a number of plastic or wooden "straws" are dumped on the table and players try to remove them one-by-one without disturbing the other straws. Here, we are only concerned with if various pairs of straws are connected by a path of touching straws. You will be given a list of the endpoints for some straws (as if they were dumped on a large piece of graph paper) and then will be asked if various pairs of straws are connected. Note that touching is connecting, but also two straws can be connected indirectly via other connected straws.nInputnnInput consist multiple case,each case consists of multiple lines. The first line will be an integer n (1 < n < 13) giving the number of straws on the table. Each of the next n lines contain 4 positive integers,x1,y1,x2 and y2, giving the coordinates, (x1,y1),(x2,y2) of the endpoints of a single straw. All coordinates will be less than 100. (Note that the straws will be of varying lengths.) The first straw entered will be known as straw #1, the second as straw #2, and so on. The remaining lines of the current case(except for the final line) will each contain two positive integers, a and b, both between 1 and n, inclusive. You are to determine if straw a can be connected to straw b. When a = 0 = b, the current case is terminated. nnWhen n=0,the input is terminated. nnThere will be no illegal input and there are no zero-length straws.nOutputnnYou should generate a line of output for each line containing a pair a and b, except the final line where a = 0 = b. The line should say simply "CONNECTED", if straw a is connected to straw b, or "NOT CONNECTED", if straw a is not connected to straw b. For our purposes, a straw is considered connected to itself.nSample Inputnn7n1 6 3 3 n4 6 4 9 n4 5 6 7 n1 4 3 5 n3 5 5 5 n5 2 6 3 n5 4 7 2 n1 4 n1 6 n3 3 n6 7 n2 3 n1 3 n0 0nn2n0 2 0 0n0 0 0 1n1 1n2 2n1 2n0 0nn0nSample OutputnnCONNECTED nNOT CONNECTED nCONNECTED nCONNECTED nNOT CONNECTED nCONNECTEDnCONNECTEDnCONNECTEDnCONNECTED 问答

没有更多推荐了,返回首页