我必须与 BDI 代理合作,为此我将使用 JADEX 2.4,但我有一个大问题。文档有点差,我无法在代理之间交换消息。
我已经阅读了这篇文章http://www.activecomponents.org/bin/view/AC+Tutorial/05+Provided+Services
我正在尝试在我的代码上做同样的事情,但没有成功。我需要知道如何做两件事来完成我的工作:从一个代理向另一个代理发送消息,以及从一个代理向所有代理发送消息。有谁知道该怎么做?
我拥有的代码如下:
聊天系统.java
package agents;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import ....
@Service
public class ChatSystem implements IChatService{
@ServiceComponent
protected IInternalAccess agent;
protected IClockService clock;
protected DateFormat format;
@ServiceStart
public IFuture<IClockService> startService(){
format = new SimpleDateFormat("hh:mm:ss");
final Future<IClockService> ret = new Future<IClockService>();
IFuture<IClockService> fut = agent.getServiceContainer().getRequiredService("clockservice");
fut.addResultListener(new DelegationResultListener<IClockService>(ret)
{
public void customResultAvailable(IClockService result)
{
clock = result;
super.customResultAvailable(null);
}
});
return ret;
}
@Override
public IFuture<Void> message(String nick, String text,
boolean privatemessage) {
// TODO Auto-generated method stub
//System.out.println(" received at" + text);
System.out.println(agent.getComponentIdentifier().getLocalName()+" received at "
+" from: "+nick+" message: "+text);
return null;
}
}
HelperAgent.java
package agents;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;
import .....
@Agent
@Service
@RequiredServices({@RequiredService(name="clockservice", type=IClockService.class,binding=@Binding(scope=RequiredServiceInfo.SCOPE_PLATFORM)),@RequiredService(name="chatservices", type=IClockService.class,binding=@Binding(scope=RequiredServiceInfo.SCOPE_PLATFORM,dynamic=true),multiple=true)})
@ProvidedServices(@ProvidedService(type=IChatService.class, implementation=@Implementation(ChatSystem.class)))
public class HelperAgent {
@Agent
protected MicroAgent agent;
@AgentBody
public void AgentBody()
{
IFuture<IClockService> fut = agent.getServiceContainer().getRequiredService("clockservice");
fut.addResultListener(new DefaultResultListener<IClockService>()
{
public void resultAvailable(IClockService cs)
{
System.out.println("Time for a chat, buddy: "+new Date(cs.getTime()));
}
});
IFuture<Collection<IChatService>> chatservices = agent.getServiceContainer().getRequiredServices("chatservices");
chatservices.addResultListener(new DefaultResultListener<Collection<IChatService>>()
{
public void resultAvailable(Collection<IChatService> result)
{
for(Iterator<IChatService> it=result.iterator(); it.hasNext(); )
{
IChatService cs = it.next();
cs.message(agent.getComponentIdentifier().getName(), "test",false);
}
}
});
}
}
任何人都可以帮助我吗?
问候