JACK——PaintRobot Exercise5

来源:http://aosgrp.com/

 

Exercise 5

Illustrate the reposting of events when a plan fails.

 

Introduction

The way that an agent handles an event depends on the type of event. For example, when a normal event is received by an agent, the agent initiates a task to handle the event. This task involves selecting and executing the first plan that is both relevant and applicable to this event. With normal events the initial selection is the only plan that is executed. In the case of BDI events, the agent can apply more sophisticated reasoning to the plan selection, and can also attempt to achieve its goal using other applicable plans if a plan fails.

This exercise illustrates how a BDIGoalEvent will attempt every applicable plan until it succeeds. It will only fail when no more applicable plan instances remain to be tried. The body() method in a plan is the plan's main reasoning method. It is executed whenever a plan is executed. Reasoning methods do not have the same execution structure as ordinary Java methods. Each statement in a reasoning method is treated like a boolean expression, and each semicolon between statements like an AND connector. This means that if any statement fails, then the reasoning method terminates immediately and fails. In this exercise you can make the plan fail by adding the following statement in its body() method:

   false;
 
Instructions

1. Modify the PaintSpecifiedNewColour plan so that it prints the painting message once and then fails (add false; after the first print statement). To illustrate that the plan actually stops at that point, add System.out.println("After false statement"); after the false statement. This print statement should not be executed.

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

2. Save the project.

3. Compile and run the program.

4. Add a fail() reasoning method that prints the string "PaintSpecifiedNewColour plan failed".

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

5. Save the project.

6. Compile and run the program.

 

示例程序

 

运行结果:

(1) test with red

(2) Painting part the requested colour (1st coat) red

(3) PaintSpecifiedNewColour plan failed

(4) painting the part the current colour: red

(5) test with no specified colour

(6) No specified colour. Painting the part red

(7) test with green

(8) Painting part the requested colour (1st coat) green

(9) PaintSpecifiedNewColour plan failed

(10) painting the part the current colour: green

(11) test with green again

(12) painting the part the current colour: green

 

运行结果分析

首先,外部要求Agent绘制红色red(1),搜索可应用的规划PaintSpecifiedNewColour ,在body()方法中将Agent保存的颜色改为红色,并进行第一次输出(1st coat)(2),紧接着的false语句让其推理失败(3)。此时需要重新搜索出当前方案之外的可应用的方案,选择规划PaintSpecifiedCurrentColour ,打印输出绘制当前颜色(4);

其次,外部要求Agent绘制null(5),选择可应用的规划PaintAnyColour,打印输出(6);

再次,外部要求Agent绘制绿色green(7),与绘制红色同(8)(9)(10)。

最后,外部再次要求Agent绘制绿色(11),此时Agent内部保存的颜色为绿色,选择规划PaintSpecifiedCurrentColour ,打印输出(12)。

 

Questions

1. Which plan is now responsible for printing the second paint message that corresponds to the second coat of paint?

 

Answers

1. PaintSpecifiedCurrentColour plan

你可能感兴趣的:(paint)