translateAgent.adoptPlan(new TranslateEngChPlan()).get();
} catch (Exception e) { // TODO: handle exception e.printStackTrace(); } } }
上面代码的主要功能就是在Plan执行完成之后,其他的Plan方法会相应的根据Plan来执行。如果是成功执行,就执行PlanPassed,不成功就会根据异常的类型执行相应的方法。
3.5 Plan的上下文条件
除了Plan的生命周期方法之外,一个Plan,还有其他的先决条件或者上下文条件的方法。先决条件的方法是在Plan执行之前进行evaluated的。作为对比,上下文条件则是必须在Plan执行的时候来evalued。也就是说,如果在Plan执行的某个时候,上下文条件变成false了,那么该计划就不得不被取消了。
package a1;
import java.util.HashMap; import java.util.Map;
import jadex.bdi.runtime.IPlan; import jadex.bdiv3.BDIAgent;
import jadex.bdiv3.annotation.Belief; import jadex.bdiv3.annotation.Plan;
import jadex.bdiv3.annotation.PlanAPI; import jadex.bdiv3.annotation.PlanAborted; import jadex.bdiv3.annotation.PlanBody;
import jadex.bdiv3.annotation.PlanContextCondition; import jadex.bdiv3.annotation.PlanFailed; import jadex.bdiv3.annotation.PlanPassed; import jadex.micro.annotation.Agent; import jadex.micro.annotation.AgentBody; import jadex.micro.annotation.Description; @Agent
@Description(\) // @Plans(@Plan(body=@Body(TranslatePlan.class))) publicclassTranslateEngChBDI { @Agent
protected BDIAgent translateAgent; @Belief
protectedboolean boolContext = true; @Plan
publicclassTranslateEngChPlan {
protected Map
protected IPlan plan;
@PlanContextCondition(beliefs=\) publicboolean checkContext(){ return boolContext; }
public TranslateEngChPlan() {
this.wordTable = new HashMap
wordTable.put(\, \香蕉\);
wordTable.put(\, \学校\);
wordTable.put(\, \老师\);
wordTable.put(\, \科学\); }
@PlanBody
publicvoid translateEngCh() throws InterruptedException { // Here we only test one example
System.out.println(\); plan.wait(10000);
System.out.println(\);
System.out.println(\se is \
+ wordTable.get(\)); }
@PlanPassed
publicvoid passed(){
System.out.println(\\); } @PlanAborted
publicvoid aborted(){
System.out.println(\); } @PlanFailed
publicvoid failed(){
System.out.println(\); } }
@AgentBody
publicvoid body() { try {
translateAgent.adoptPlan(new TranslateEngChPlan());
translateAgent.waitForDelay(1000);