3

I send a message from one agent to another

msg.setContent("price: 30, count: 1");

After that I need to manually parse it. Is there more convenient way to transfer parameters without converting to string? For example, send some container..

2 Answers2

5

You had better use an ontology. It is more convenient way. For example.

Create your container:

public class ParameterConcept implements Predicate{
    private Long price;
    private Long count;
    ... getters and setters
}

Create your ontology:

public class YourOntology extends Ontology {
    public static final String NAME = "YourOntology";
    private static Ontology instance = new YourOntology();
    public static Ontology getInstance() {
        return instance;
    }

    private YourOntology() {
        super(NAME, BasicOntology.getInstance());

        add(new PredicateSchema("ParameterConcept"), ParameterConcept.class);
        PredicateSchema parameterConcept = (PredicateSchema) getSchema("ParameterConcept");
        parameterConcept.add("price",         (PrimitiveSchema) getSchema(BasicOntology.INTEGER), ObjectSchema.MANDATORY);
        parameterConcept.add("count",         (PrimitiveSchema) getSchema(BasicOntology.INTEGER), ObjectSchema.MANDATORY);
    }
}

Register your ontology like this (YourAgent.java):

private static final Codec codec = new SLCodec();
private static final Ontology ontology = YourOntology.getInstance();

protected void setup() {
    getContentManager().registerLanguage(codec, FIPANames.ContentLanguage.FIPA_SL0);
    getContentManager().registerOntology(ontology);
}

Create message like this:

ACLMessage msg = new ACLMessage(ACLMessage.INFORM);
...
msg.setLanguage(FIPANames.ContentLanguage.FIPA_SL0);
msg.setOntology(YourOntology.NAME);
...
try {
    agent.getContentManager().fillContent(msg, parameterConcept);
} catch (Exception e) {
     throw new RuntimeException("cannot fill message.", e);
}

Now you can parse message (code of the other agent) like this:

ContentManager cm = myAgent.getContentManager();
ContentElement contentElement = cm.extractContent(aclMessage);
ParameterConcept pc = (ParameterConcept) contentElement;

Or you can just use json with third json libs.

nikelyn
  • 418
  • 3
  • 12
  • Actually, I can't fill the message. IDEA says that it cannot resolve method 'fillContent(jade.lang.acl.ACLMessage, market.Offer). If I try to cast it to (ContentElement) than an error occurs java.lang.RuntimeException: cannot create fill message. – Артур Гудиев May 21 '18 at 19:52
  • try to type "public class ParameterConcept implements Predicate" – nikelyn May 21 '18 at 20:16
  • Thanks. But it still has a runtime error. ERROR: Agent Consumer1 died without being properly terminated !!! State was 2 *** Uncaught Exception for agent Consumer1 *** java.lang.RuntimeException: cannot create fill message. – Артур Гудиев May 21 '18 at 21:13
  • can you put a breakpoint on "throw new RuntimeException("cannot create fill message.");" and attach stacktrace? – nikelyn May 21 '18 at 21:16
  • Yes, sure. *** Uncaught Exception for agent Consumer1 *** java.lang.RuntimeException: cannot create fill message. at market.ConsumerAgent$1.action(ConsumerAgent.java:58) at jade.core.behaviours.Behaviour.actionWrapper(Behaviour.java:344) at jade.core.Agent$ActiveLifeCycle.execute(Agent.java:1585) at jade.core.Agent.run(Agent.java:1524) at java.lang.Thread.run(Thread.java:748) – Артур Гудиев May 21 '18 at 21:17
  • no, i mean that exception "catch (Exception e) {". Put breakpoint, press alt+f8 and type "e" and press enter. – nikelyn May 21 '18 at 21:19
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/171491/discussion-between-nikelyn-and--). – nikelyn May 21 '18 at 21:24
  • Sorry. jade.content.abs.AbsConcept cannot be cast to jade.content.abs.AbsContentElement jade.content.ContentManager.fillContent(ContentManager.java:205) market.ConsumerAgent$1.action(ConsumerAgent.java:59) jade.core.behaviours.Behaviour.actionWrapper(Behaviour.java:344) jade.core.Agent$ActiveLifeCycle.execute(Agent.java:1585) jade.core.Agent.run(Agent.java:1524) java.lang.Thread.run(Thread.java:748) – Артур Гудиев May 21 '18 at 21:31
  • Ok. Let's continue it in chat – Артур Гудиев May 21 '18 at 21:36
1

You could just use msg.setContentObject() method and pass object with class that implements Serializable interface.