4

谁能告诉我如何通过 java 代码找到可用的代理容器?我正在使用 JADE 代理框架,并且我已经弄清楚如何创建新容器但找不到现有容器(以便可以在其中部署代理)。

4

1 回答 1

5

有两种方法可以做到这一点,具体取决于您是希望通过正在进行的服务还是通过消息中的当前快照接收信息。

要获取当前可用代理容器的 ID 快照,请向代理管理服务 (AMS) 发送请求消息并等待其回复。使用 JADE 管理本体和QueryPlatformLocationsAction术语,发送和接收方法将是:

private void queryAMS() throws CodecException, OntologyException {
    QueryPlatformLocationsAction query = new QueryPlatformLocationsAction();
    Action action = new Action(myAgent.getAID(), query);

    ACLMessage message = new ACLMessage(ACLMessage.REQUEST);
    message.addReceiver(myAgent.getAMS());
    message.setLanguage(FIPANames.ContentLanguage.FIPA_SL);
    message.setOntology(JADEManagementOntology.getInstance().getName());
    myAgent.getContentManager().fillContent(message, action);
    myAgent.send(message);
}
private void listenForAMSReply() throws UngroundedException, CodecException, 
OntologyException {
    ACLMessage receivedMessage = myAgent.blockingReceive(MessageTemplate
            .MatchSender(myAgent.getAMS()));
    ContentElement content = myAgent.getContentManager().extractContent(
        receivedMessage);

    // received message is a Result object, whose Value field is a List of
    // ContainerIDs
    Result result = (Result) content;
    List listOfPlatforms = (List) result.getValue();

    // use it
    Iterator iter = listOfPlatforms.iterator();
    while (iter.hasNext()) {
        ContainerID next = (ContainerID) iter.next();
        System.out.println(next.getID());
    }
}

要将此信息作为持续服务获取,并在每个容器向 AMS 注册时接收它的 ContainerID,请创建一个扩展 AMSSubscriber 的行为。为 AdditionalContainer 事件注册一个处理程序,您将能够访问新可用容器的 ContainerID:

public class AMSListenerBehaviour extends AMSSubscriber {
@Override
public void installHandlers(Map handlersTable) {
    handlersTable.put(AddedContainer.NAME, addedContainerHandler);
}

public final class AddedContainerHandler implements EventHandler {
@Override
public void handle(Event ev) {
    AddedContainer event = (AddedContainer) ev;
    ContainerID addedContainer = event.getContainer();
    System.out.println(addedContainer.getID());
}

希望这可以帮助,

拉斯

于 2009-12-10T09:26:51.507 回答