Enable the robot agent to select between multiple plans through relevance.
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.
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.
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:
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
Answers
1.按Agent中Plan声明的先后顺序,先声明先先被选中。