Creata Jade Agent with another agent

2019-09-19 04:39发布

please , I need help. How to create a JADE agent in the body of another agent ?

    Profile p = new ProfileImpl();

    p.setParameter(Profile.MAIN_HOST, "localhost");

    p.setParameter(Profile.MAIN_PORT, "1099");

    AgentContainer ac = rt.createMainContainer(p);
    AgentController agent ac.createNewAgent ("agentBD", "agents.AgentBD");

     agent.start();

标签: agents-jade
1条回答
Summer. ? 凉城
2楼-- · 2019-09-19 05:10

try something like this ("agent" is you current agent from whitch you create other agent):

private static Codec codec = new SLCodec();
private static ContentManager cm = new ContentManager();
static
{
    cm.registerLanguage(codec, FIPANames.ContentLanguage.FIPA_SL0);
    cm.registerOntology(FIPAManagementOntology.getInstance());
    cm.registerOntology(JADEManagementOntology.getInstance());
}

public static AID createNewAgent(Agent agent, String name, String className)
{
    CreateAgent ca = new CreateAgent();
    ca.setAgentName(name);
    ca.setClassName(className);
    ca.setContainer((ContainerID) agent.here());

    ACLMessage request = new ACLMessage(ACLMessage.REQUEST);
    request.addReceiver(agent.getAMS());
    request.setOntology(JADEManagementOntology.getInstance().getName());
    request.setLanguage(FIPANames.ContentLanguage.FIPA_SL0);
    request.setReplyWith("new-agent-" + agent.getName() + System.currentTimeMillis());

    try
    {
        cm.fillContent(request, new Action(agent.getAMS(), ca));
        agent.send(request);

        MessageTemplate mt = MessageTemplate.MatchInReplyTo(request.getReplyWith());
        ACLMessage reply = agent.blockingReceive(mt, 10000);
        if(reply == null)
            throw new RuntimeException("cannot create agent [" + name + "; " + className + "]");

        return new AID(name, AID.ISLOCALNAME);
    }
    catch (Codec.CodecException e)
    {
        throw new RuntimeException(e);
    }
    catch (OntologyException e)
    {
        throw new RuntimeException(e);
    }
}
查看更多
登录 后发表回答