Enable the robot agent to select between multiple plans through context, as well as relevance.
The context() method provides the next level of 'filtering' after relevant(). If a plan is relevant to a particular event, the context() method determines whether the plan is applicable given the agent's current knowledge. The context() method does not take any arguments and its body is always a single JACK Agent Language logical expression. (JAL logical expressions are composed of boolean members, logical members and beliefset cursor expressions which can, in general, bind to multiple values. Logical expressions and cursors are discussed in more detail in the Agent Manual.) When evaluating the context() method, the agent will consider all possible alternatives. Note that for every possible set of values that can satisfy the context() method, a separate instance of the plan will be generated and will be available for execution. This concept of multiple possible bindings and plan instances is illustrated in the introductory beliefset exercise found in Practical 2.
The context() method takes the following form:
context()
{
// Logical condition to determine which plan instances are
// applicable (in this example test the value of paintColour).
}
To illustrate the use of context() in a plan, the robot agent will have a String data member called paintColour, which stores the colour being used.
In this example we will also use
#uses interface Robot self;
within a plan. This statement gives the plan access to the members and methods in the Robot class interface. In this particular example, it will allow the plan to access the robot's paintColour member (self.paintColour).
1. Use the design tool to add a new plan called PaintSpecifiedNewColour to the Robot_AD diagram. This plan is to deal with Paint events that request a specific colour, but which could not be handled by the PaintSpecifiedCurrentColour plan, as the colour requested is not the same as the agent's current paintColour. In the PaintSpecifiedNewColour plan, the agent's paintColour member must first be changed to the new colour. In addition, when the colour changes, it is necessary to give the part two coats of paint to ensure that no trace of the previous colour remains. Use the design tool to add the required links between the plan, the agent and the Paint event as follows:
Your design diagram should be similar to the following diagram:
Figure 5: The Robot_AD design diagram with the PaintSpecifiedCurrentColour, PaintAnyColour and PaintSpecifiedNewColour plans
2. Use Edit as a JACK File to modify the Robot agent to:
Ensure that the plans are declared in the following order:
#uses plan PaintSpecifiedCurrentColour;
#uses plan PaintSpecifiedNewColour;
#uses plan PaintAnyColour;
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 the Edit as a JACK File option to make the following changes to the new PaintSpecifiedNewColour plan:
#handles event Paint ev;
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. Use the Edit as a JACK File option to edit the PaintSpecifiedCurrentColour plan.
context()
{
self.paintColour.equals(ev.colour);
}
l change the print statement in body() to
System.out.println("Painting part the current colour "+
self.paintColour);
This will make it easier to distinguish between the alternative plans in the program output.
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.
5. Modify the PaintAnyColour plan so that it now prints the string "No colour specified. Painting the part" followed by the robot's current paintColour. To access the paintColour member inside the plan, add a #uses interface Robot self declaration at the beginning of the plan.
If editing the file as a JACK file, save and close the file before continuing.
6. Modify the main() method in Program.java so that it no longer passes a command line argument to the paintPart() method but instead invokes the method several times as follows:
System.out.println("test with red");
robot1.paintPart("red"); // Should result in two coats.
System.out.println("test with no specified colour (null)");
robot1.paintPart(null); // Should only be one coat –
// slightly different message.
System.out.println("test with green");
robot1.paintPart("green"); // Should result in two coats.
System.out.println("test with green again");
robot1.paintPart("green"); // Should only be one coat.
Save and close the file to apply the changes before continuing.
7. Save the project.
8. Compile and run the program. Check that the output is correct.
运行结果:
test with red
Painting part the requested colour (1st coat) red
Painting part the requested colour (2nd coat) red
test with no specified colour
No specified colour. Painting the part red
test with green
Painting part the requested colour (1st coat) green
Painting part the requested colour (2nd coat) green
test with green again
painting the part the current colour: green
运行结果分析
外部(main())要求Agent绘制红色(red),绘制事件有三个规划与其对应,根据声明的先后顺序,首先调用PaintSpecifiedCurrentColour plan,但其context()中判定,当前的要求绘制的是红色,与Agent中保存的颜色(黑色black)不相符,因此,返回false,不执行该规划。然后去调用PaintSpecifiedNewColour 规划,该规划中context()不判定,直接返回true,因此,调用该规划。在推理方法body()中对Agent的颜色进行设置,将黑色替换为红色,打印输出。
其次,外部要求Agent绘制颜色,但没有指定特定的颜色(null),根据relevant()方法直接将前面两个规划过滤,调用PaintAnyColour 规划。由于之前已将Agent中保存的颜色设置为红色,因此,此时,输出红色。
再次,外部要求Agent绘制绿色,与第一步相同,绿色与Agent中当前的红色不一致,因此,调用PaintSpecifiedNewColour 规划,输出绿色,Agent当前保存的颜色更改为绿色。
最后,外部要求Agent再次绘制绿色,此时绿色与Agent当前保存的颜色一致,因此调用PaintSpecifiedCurrentColour plan,输出绿色。