javaagents-jade

Send parameters in ACLMessage


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..


Solution

  • 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.