Question

I have to make a work with BDI Agents and for that i will use JADEX 2.4 but i have a big problem. The documentation is a bit poor and i can't exchange messages between agents.

I have read this article http://www.activecomponents.org/bin/view/AC+Tutorial/05+Provided+Services

And i'm trying make the same thing on my code but no success. I need to know how to do 2 things for make my work: send a message from one agent to other, and send a message from one agent to all agents. Anyone knows how to do that?

The code that i have is the following:

ChatSystem.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);
            }
          }
        });

    }
}

Anyone can help me?

Regards

Was it helpful?

Solution

In Jadex you work with active components representing enhanced agents, i.e. in addition to sending and receiving messages you can work with services. Agents can expose services using Java interfaces and other agents can simply fetch these services via their type. Using services communication is done without having to know agent identifities. This helps in building more SOA driven solutions dynamic service providers.

If you want to communicate via messages the API depends on the type of component you are using. In case of micro agents (as shown in your snippets) you can just prepare a FIPA message and call sendMessage on the agent API as shown below:

Map msg = new HashMap();
msg.put(SFipa.CONTENT, content);
msg.put(SFipa.PERFORMATIVE, SFipa.QUERY_IF);
msg.put(SFipa.CONVERSATION_ID, "someuniqueid");
msg.put(SFipa.RECEIVERS, new IComponentIdentifier[]{receiver});
msg.put(SFipa.SENDER, getComponentIdentifier());
agent.sendMessage(msg, SFipa.FIPA_MESSAGE_TYPE);

with 'agent' being the injected MicroAgent.

Kind regards Lars

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top