Author: Giovanni Caire (Telecom Italia)
Date: November 10, 2006
Java platform: Sun JDK 1.4 or later
JADE version 3.0 or later
This tutorial describes how to use the XMLCodec add-on in combination with the JADE support for content languages and ontologies to encode and decode content expressions (i.e. expressions that can be included in the content slot of ACL messages) according to an XML-based syntax.
Furthermore the add-on provides utility methods to export and import whatever ontological element (regardless of whether or not it represents a Content Element) to/from an XML format.
In order to install XMLCodec add-on the following steps must be performed:
The XMLCodec add-on can be re-compiled by means of the following ant targets defined in the XMLCodec/build.xml file:
The jade.content.lang.xml.XMLCodec class implemnts the jade.content.lang.Codec interface and can therefore be used as other JADE codecs such as the SLCodec or the LEAPCodec. The code below, where an agent informs another agent that John is the father of Bill and Mary using the PeopleOntology, shows an example (look at the Tutorial on content languages and ontologies available on the JADE web site for details about codecs and ontologies).
...
private Codec xmlCodec = new XMLCodec();
private Ontology onto = PeopleOntology.getInstance();
...
protected void setup() {
...
}
getContentManager().registerLanguage(xmlCodec);
...
ACLMessage msg = new ACLMessage(ACLMessage.INFORM);
// Fill message receiver and other message slots
msg.setOntology(onto.getName())
msg.setLanguage(XMLCodec.NAME);
FatherOf fo = new FatherOf();
Man m = new Man();
m.setName("John");
fo.setFather(m);
List children = new ArrayList();
m = new Man();
m.setName("Bill");
children.add(m);
Woman w = new Woman();
w.setName("Mary");
children.add(w);
fo.setChildren(children);
try {
getContentManager().fillContent(msg, fo);
}
send(msg);
catch (Exception e) {
e.printStackTrace();
}
...
...
If we sniffed the message prepared and sent by the code above and we printed its content we would see the following output.
<father-of>
<man>
<name>John</name>
</man>
<children>
<man>
<name>Bill</name>
</man>
<woman>
<name>Mary</name>
</woman>
</children>
</father-of>
Besides the methods of the jade.content.lang.Codec interface, the jade.content.lang.xml.XMLCodec class also provides utility methods that can be used to export/import whatever ontological element (i.e. whatever object whose class is associated to a schema in an ontology) to/from an XML format regardless of whether or not it represents a content element. As an example the code below shows how to export a DFAgentDescription to XML. It should be noticed that this feature does not involve the agent ContentManager and therefore there is no need to register neither the codec nor the ontologies with it.
...
DFAgentDescription dfd = new DFAgentDescription();
dfd.setName(new AID("a", AID.ISLOCALNAME));
ServiceDescription sd = new ServiceDescription();
sd.setName("foo1");
sd.setType("bar1");
dfd.addServices(sd);
ServiceDescription sd = new ServiceDescription();
sd.setName("foo2");
sd.setType("bar2");
dfd.addServices(sd);
try {
String s = xmlCodec.encodeObject(FIPAManagementOntology.getInstance(),
dfd, true);
System.out.println(s);
}
catch (Exception e) {
e.printStackTrace();
}
...
The output produced by the above code is as follows (the third parameter of the encodeObject() method set to true indicates that the XML must be indented).
<df-agent-description>
<name>
<agent-identifier>
<name>a@foo-platform</name>
</agent-identifier>
</name>
<services>
<service-description>
<name>foo1</name>
<type>bar1</type>
</service-description>
<service-description>
<name>foo2</name>
<type>bar2</type>
</service-description>
</services>
</df-agent-description>
JADE is a trademark of Telecom Italia.