Start remote agent

本文介绍如何在JADE平台上创建代理(agent),包括获取容器ID、构造请求消息、发送消息等步骤。通过示例代码展示了整个流程。
//  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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值