关于多Agent系统的研究 下载本文

@Plan(trigger = @Trigger(goals = TranslateGoal.class)) public String translatePlan(String ewordString) { return wordTable.get(ewordString); } }

然后创建一个BDI文件,直接把capability嵌入到Agent的一个成员使用。非常简单方便。

package a2;

import jadex.bdiv3.BDIAgent;

import jadex.bdiv3.annotation.Capability; import jadex.micro.annotation.Agent; import jadex.micro.annotation.AgentBody; import jadex.micro.annotation.Description; @Agent

@Description(\) publicclassTranslateBDI { @Agent

protected BDIAgent translateAgent;

@Capability

protected TranslateCapa capa = new TranslateCapa();

@AgentBody

publicvoidbody(){

String eword = \;

String cnword = (String) translateAgent.dispatchTopLevelGoal(capa.new TranslateGoal(eword)).get();

System.out.println(\翻译完成:\ + eword + \ + cnword); } }

总结一下:capability就是一个模块,大家都可以调用来使用。好处减少了开发的资源浪费。但是需要很好的设计基础,才可以使用capability。

本篇主要是讲解一下,关于两个或者多个Agent之间,是如何进行通信的。

1. 简单介绍

对于Agent之间的交互是很有必要得。当今社会没有任何一件事情可以让单独的个体来完成。生活在一个community环境里,就需要相互之间的信息传递。那么在Jadex中,交互是一种服务,service。而这种service是被定义为接口类型的。具体的交互实现是需要单独来完成。单单理解起来可能还是比较困难的,可以看一下多代理系统的BDI介绍。

2. 实例讲解

这里主要实现的是两个Agent之间的交互。我们定义一个用户Agent,和一个翻译Agent。用户Agent发出一个翻译请求之后,翻译Agent就会自动查找自己的词典库,知道找到相应的翻译结果之后,返回给用户Agent,进而实现两个Agent之间的交互。 首先是接口,长这个样:

package a3;

import jadex.commons.future.IFuture;

publicinterfaceITranslateServ {

public IFuturetranslateEnglishChinese(String ewordString); }

其次是翻译Agent,长成这个样:

package a3;

import java.util.HashMap; import java.util.Map;

import jadex.bdiv3.BDIAgent;

import jadex.bridge.service.annotation.Service; import jadex.commons.future.Future; import jadex.commons.future.IFuture; import jadex.micro.annotation.Agent;

import jadex.micro.annotation.AgentCreated; import jadex.micro.annotation.Description; import jadex.micro.annotation.ProvidedService; import jadex.micro.annotation.ProvidedServices; @Agent

@Description(\) @Service

@ProvidedServices(@ProvidedService(type = ITranslateServ.class))

publicclassTranslateBDIimplementsITranslateServ { @Agent

protected BDIAgent translateAgent;

protected Map wordTable;

@AgentCreated publicvoidinit() {

this.wordTable = new HashMap(); wordTable.put(\, \牛奶\);

wordTable.put(\, \香蕉\);

wordTable.put(\, \学校\);

wordTable.put(\, \老师\);

wordTable.put(\, \科学\); }

publicTranslateBDI() { }

@Override