JACK——PaintRobot Exercise3

来源:http://aosgrp.com/

 

Exercise 3

Enable the robot agent to select between multiple plans through relevance.

 

Introduction

In this example, the agent has two plans that can handle a Paint event: PaintAnyColour and PaintSpecifiedCurrentColour.

The PaintAnyColour plan prints the string "No colour specified. Painting the part."

The PaintSpecifiedCurrentColour plan prints the string "Painting part the requested colour:" followed by the colour that was requested in the Paint event.

Whenever an event is posted and an agent begins a task to handle the event, the first thing that the agent must do is find an applicable plan to handle the event. Note that each plan is only capable of handling a single event type which is identified by the plan's #handles event declaration. It is possible (as in this example) that there is more than one plan capable of handling a particular event type. To decide which of the plans are applicable, JACK employs the following steps.

  1. Identify the plans which handle the event type.
  2. Use the relevant() method to check additional information regarding the event.
  3. Use the context() method to check information stored as part of the agent's beliefs.
  4. If there are still multiple plans left in the applicable plan set, additional means are used to select one of them. At this stage, we will only be considering prominence (or declaration order). For more details, refer to the Agent Manual.

We will be looking at the relevant() method in this example. The PaintSpecifiedCurrentColour plan will contain a relevant() method to ensure that the plan is only selected if there is a colour specified in the colour data member of the Paint event. If a plan does not specify a relevant() method, the plan is relevant for all instances of the event. The relevant() method takes the following form:

static boolean relevant(EventType eventref)

{

// Code to determine when the plan is relevant to this event.

}

The next level of 'filtering' in plan selection (assuming the plan can handle the event and is relevant for a particular instance of the event) is the context() method. This is discussed in Exercise 4.

Note that the order of plan declarations within the agent (or capability) also has a bearing on plan selection. The order is called prominence, and the most prominent applicable plan will be selected first. This means that if PaintAnyColour is declared before PaintSpecifiedCurrentColour, it will always be the plan selected to handle Paint events as it does not have a relevant() or a context() method associated with it.

 

Instructions

1. Use the design tool to add a new plan called PaintAnyColour to the Robot_AD design diagram.

l Add a link from the Robot agent to the new plan.

l Add a link from the Paint event to the new plan. Your design diagram should be similar to the following diagram:

clip_image001

Figure 4: The Robot_AD design diagram with the PaintSpecifiedCurrentColour and PaintAnyColour plans

2. Use the Edit as a JACK File option to modify the PaintSpecifiedCurrentColour plan so that its relevant() method performs a test that recognises events with non-empty strings. For example,

static boolean relevant(Paint ev)

{

return (ev.colour != null && ev.colour.length() > 0);

}

This plan will be used to paint a part the colour that was requested in the colour member of the Paint event. Note that if the robot is not currently painting with the requested colour an alternative plan will be required. We will not write that plan or test for that condition in this exercise – that is deferred until the next exercise.

Close the file to ensure that there are no conflicts between editing the file in the JDE browser and as a JACK file. In the editor window of the file, click the Save button and then the Close button.

3. Use Edit as a JACK File to edit the new plan to add the following print statement to its body reasoning method:

System.out.println("No specified colour. Painting the part");

This is the plan to be used when a Paint event does not include a specific colour request. The colour string will be null or empty. In this case the part will be painted with the colour being used by the robot at the time of the paint request.

Close the file to ensure that there are no conflicts between editing the file in the JDE browser and as a JACK file. In the editor window of the file, click the Save button and then the Close button.

4. The #uses plan PaintAnyColour declaration should appear after the PaintSpecifiedCurrentColour plan declaration. Check the order by editing the Robot agent. If necessary swap the declarations via the project browser or by editing the file as a JACK file.

If editing the file as a JACK file, save and close the file before continuing.

5. Edit the Robot and modify its paintPart() method so that it now takes a String argument (the colour) and passes it to the paint posting method.

If editing the file as a JACK file, save and close the file before continuing.

6. Edit the main() method in Program.java, so that the paintPart() method is invoked with the first program argument, if any, and null otherwise. For example:

robot1.paintPart( (args.length==0) ? null : args[0] );

Save and close the file to apply the changes before continuing.

7. Save the project.

8. Compile and run the program a few times with and without command line arguments. The command line arguments are added in the Extra Args text box of the Compiler Utility's Run Application window.

9. Swap the plan declarations within the agent. Compile and run the program again (with and without command line arguments).

10. Swap the plan declarations back to normal before moving on to Exercise 4.

 

示例程序

 

运行结果:

(1) 如果在Run Application的Extra Args中输入某种颜色如white,运行结果:painting the part the requested colour: white

(2) 如果为输入任何参数,运行结果:No specified colour. Painting the part

 

Questions

  1. How is JACK choosing which plan to use when both plans are applicable?

Answers

  1.按Agent中Plan声明的先后顺序,先声明先先被选中。

 

转载于:https://www.cnblogs.com/6DAN_HUST/archive/2011/06/09/2076185.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值