编程语言
首页 > 编程语言> > java – 在ACLMessage中发送参数

java – 在ACLMessage中发送参数

作者:互联网

我从一个代理发送消息到另一个代理

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

之后我需要手动解析它.是否有更方便的方式来传输参数而不转换为字符串?例如,发送一些容器..

解决方法:

你最好使用本体.这是比较方便的方式.
例如.

创建容器:

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

创建你的本体:

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

像这样注册你的本体(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);
}

创建这样的消息:

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

现在你可以像这样解析消息(其他代理的代码):

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

或者你可以使用json与第三个json库.

标签:java,agents-jade
来源: https://codeday.me/bug/20190731/1587283.html