//
create the containerid of this container
ContainerID myContainerID;
for
(ContainerID cid:getContainerIDs())
...
{
try ...{
if (cid.getName().equals(
getContainerController().getContainerName())) ...{
myContainerID = cid;
break;
}
} catch (ControllerException e) ...{
e.printStackTrace();
}
}


//
create the request to the ams
CreateAgent ca
=
new
CreateAgent();
ca.setAgentName(
"
some_name
"
);
ca.setClassName(
"
my.agent
"
);
ca.setContainer(myContainerID);
//
create and send the message to the ams
ACLMessage msg
=
new
ACLMessage(ACLMessage.REQUEST);
msg.setOntology(JADEManagementOntology.NAME);
msg.setLanguage(jade.domain.FIPANames.ContentLanguage.FIPA_SL0);
msg.setProtocol(FIPANames.InteractionProtocol.FIPA_REQUEST);
fetContentManager().fillContent(msg,
new
Action(getAMS(), ca));
msg.addReceiver(getAMS());
send(msg);

//
method for getting the containerids from the ams

protected
List
<
ContainerID
>
getContainerIDs()
...
{
ACLMessage msg = new ACLMessage(ACLMessage.REQUEST);
msg.setProtocol(jade.domain.FIPANames.InteractionProtocol.FIPA_REQUEST);
msg.setOntology(JADEManagementOntology.NAME);
msg.setLanguage(jade.domain.FIPANames.ContentLanguage.FIPA_SL0);
try ...{
// send a request to the AMS
getContentManager().fillContent(msg, new Action(getAMS(),
new QueryPlatformLocationsAction()));
msg.addReceiver(getAMS());
send(msg);
// wait for the answer from the ams
msg = blockingReceive(MessageTemplate.MatchOntology(
JADEManagementOntology.NAME));
//extract the content and cast to type Result
ContentElement ce = getContentManager().extractContent(msg);
Result res = null;
if (ce instanceof Result) ...{
res = (Result) ce;
} else...{
return null;
}
// make a list of all ContainerID's given in the result
jade.util.leap.Iterator it = res.getItems().iterator();
List<ContainerID> result = new LinkedList<ContainerID>();
while (it.hasNext()) ...{
result.add((ContainerID) it.next());
}
return result;
} catch (OntologyException e) ...{
return null;
} catch(CodecException e)...{
return null;
}
}

原文地址:http://avalon.tilab.com/pipermail/jade-develop/2006q4/009630.html
本文介绍如何在JADE平台上创建代理(agent),包括获取容器ID、构造请求消息、发送消息等步骤。通过示例代码展示了整个流程。
814

被折叠的 条评论
为什么被折叠?



