1 /***
2 * ***************************************************************
3 * JADE - Java Agent DEvelopment Framework is a framework to develop
4 * multi-agent systems in compliance with the FIPA specifications.
5 * Copyright (C) 2000 CSELT S.p.A.
6 *
7 * GNU Lesser General Public License
8 *
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation,
12 * version 2.1 of the License.
13 *
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the
21 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 * Boston, MA 02111-1307, USA.
23 * **************************************************************
24 */
25 package JADE_SL.abs;
26
27 import JADE_SL.onto.*;
28 import JADE_SL.*;
29 import JADE_SL.schema.*;
30 import java.util.List;
31 import java.util.ArrayList;
32 import java.util.Iterator;
33 import JADE_SL.AID;
34 import JADE_SL.OntoAID;
35 import JADE_SL.CaseInsensitiveString;
36 import JADE_SL.ACLMessage;
37
38 import java.io.PrintStream;
39 import java.io.ByteArrayOutputStream;
40
41 /***
42 * @author Federico Bergenti - Universita` di Parma
43 */
44 public class AbsHelper {
45 /***
46 * Converts a <code>List</code> into a <code>AbsAggregate</code> using
47 * the specified ontology.
48 * @param obj the <code>List</code>
49 * @param onto the ontology.
50 * @return the abstract descriptor.
51 * @throws OntologyException
52 */
53 public static AbsAggregate externaliseList(List obj, Ontology onto) throws OntologyException {
54 AbsAggregate ret = new AbsAggregate(BasicOntology.SEQUENCE);
55 try {
56 for (int i = 0; i < obj.size(); i++) {
57 ret.add((AbsTerm) (onto.fromObject(obj.get(i))));
58 }
59 } catch (Exception e) {
60 throw new OntologyException("Non term object in aggregate");}
61
62 return ret;
63 }
64
65 /***
66 * Converts an <code>Iterator</code> into a <code>AbsAggregate</code> using
67 * the specified ontology.
68 * @param obj the <code>Iterator</code>
69 * @param onto the ontology.
70 * @return the abstract descriptor.
71 * @throws OntologyException
72 */
73 public static AbsAggregate externaliseIterator(Iterator obj, Ontology onto) throws OntologyException {
74 AbsAggregate ret = new AbsAggregate(BasicOntology.SEQUENCE);
75
76 try {
77 while(obj.hasNext())
78 ret.add((AbsTerm) (onto.fromObject(obj.next())));
79 }
80 catch (ClassCastException cce) {
81 throw new OntologyException("Non term object in aggregate");
82 }
83 return ret;
84 }
85
86 /***
87 * Converts an <code>AID</code> into an <code>AbsConcept</code>
88 * representing an AID
89 * @param obj the <code>AID</code>
90 * @return the abstract descriptor.
91 */
92 public static AbsConcept externaliseAID(AID obj) {
93 AbsConcept aid = new AbsConcept(BasicOntology.AID);
94
95 aid.set(BasicOntology.AID, obj.getName());
96
97
98 AbsAggregate addresses = new AbsAggregate(BasicOntology.SEQUENCE);
99 for(Iterator i = obj.getAllAddresses(); i.hasNext(); ) {
100 String addr = (String) i.next();
101 addresses.add(AbsPrimitive.wrap(addr));
102 }
103 aid.set(BasicOntology.FIPA_AID_Address_ADDRESSES, addresses);
104
105
106 AbsAggregate resolvers = new AbsAggregate(BasicOntology.SEQUENCE);
107 for(Iterator i = obj.getAllResolvers(); i.hasNext(); ) {
108 AID res = (AID) i.next();
109 resolvers.add(externaliseAID(res));
110 }
111 aid.set(BasicOntology.FIPA_AID_Address_RESOLVERS, resolvers);
112
113 return aid;
114 }
115
116 /***
117 * Converts a <code>ContentElementList</code> into an
118 * <code>AbsContentElementList</code> using
119 * the specified ontology.
120 * @param obj the <code>ContentElementList</code>
121 * @param onto the ontology.
122 * @return the abstract descriptor.
123 * @throws OntologyException
124 */
125 public static AbsContentElementList externaliseContentElementList(ContentElementList obj, Ontology onto) throws OntologyException {
126 AbsContentElementList ret = new AbsContentElementList();
127
128 try {
129 for (int i = 0; i < obj.size(); i++) {
130 ret.add((AbsContentElement) (onto.fromObject(obj.get(i))));
131 }
132 }
133 catch (ClassCastException cce) {
134 throw new OntologyException("Non content element object in content element list");
135 }
136
137 return ret;
138 }
139
140 /***
141 * Converts an <code>ACLMessage</code> into an
142 * <code>AbsAgentAction</code> using
143 * the specified ontology.
144 * @param obj the <code>ACLMessage</code>
145 * @param onto the ontology.
146 * @return the abstract descriptor.
147 * @throws OntologyException
148 */
149 public static AbsAgentAction externaliseACLMessage(ACLMessage obj, Ontology onto) throws OntologyException {
150 try {
151 AbsAgentAction absMsg = new AbsAgentAction(BasicOntology.ACLMSG);
152
153 absMsg.set(BasicOntology.ACLMSG_PERFORMATIVE, AbsPrimitive.wrap(obj.getPerformative()));
154 absMsg.set(BasicOntology.ACLMSG_SENDER, (AbsTerm) onto.fromObject(obj.getSender()));
155
156 AbsAggregate recvs = new AbsAggregate(BasicOntology.SEQUENCE);
157 Iterator it = obj.getAllReceiver();
158 while (it.hasNext()) {
159 recvs.add((AbsTerm) onto.fromObject(it.next()));
160 }
161 absMsg.set(BasicOntology.ACLMSG_RECEIVERS, recvs);
162
163 AbsAggregate repls = new AbsAggregate(BasicOntology.SEQUENCE);
164 it = obj.getAllReceiver();
165 while (it.hasNext()) {
166 repls.add((AbsTerm) onto.fromObject(it.next()));
167 }
168 absMsg.set(BasicOntology.ACLMSG_REPLY_TO, repls);
169
170 absMsg.set(BasicOntology.ACLMSG_LANGUAGE, obj.getLanguage());
171 absMsg.set(BasicOntology.ACLMSG_ONTOLOGY, obj.getOntology());
172 absMsg.set(BasicOntology.ACLMSG_PROTOCOL, obj.getProtocol());
173 absMsg.set(BasicOntology.ACLMSG_IN_REPLY_TO, obj.getInReplyTo());
174 absMsg.set(BasicOntology.ACLMSG_REPLY_WITH, obj.getReplyWith());
175 absMsg.set(BasicOntology.ACLMSG_CONVERSATION_ID, obj.getConversationId());
176 absMsg.set(BasicOntology.ACLMSG_REPLY_BY, obj.getReplyByDate());
177
178 if (obj.hasByteSequenceContent()) {
179 absMsg.set(BasicOntology.ACLMSG_BYTE_SEQUENCE_CONTENT, obj.getByteSequenceContent());
180 }
181 else {
182 absMsg.set(BasicOntology.ACLMSG_CONTENT, obj.getContent());
183 }
184 absMsg.set(BasicOntology.ACLMSG_ENCODING, obj.getEncoding());
185
186 return absMsg;
187 }
188 catch (Exception e) {
189 throw new OntologyException("Error externalising ACLMessage", e);
190 }
191 }
192
193
194 /***
195 * Converts an <code>AbsAggregate</code> into a List using the
196 * specified ontology.
197 * @param onto the ontology
198 * @return the List
199 * @throws OntologyException
200 */
201 public static List internaliseList(AbsAggregate aggregate, Ontology onto) throws OntologyException {
202 List ret = new ArrayList();
203
204 for (int i = 0; i < aggregate.size(); i++) {
205 Object element = onto.toObject(aggregate.get(i));
206
207 Ontology.checkIsTerm(element);
208 ret.add(element);
209 }
210
211 return ret;
212 }
213
214 /***
215 * Converts an <code>AbsConcept</code> representing an AID
216 * into an OntoAID
217 * @return the OntoAID
218 * @throws OntologyException if <code>aid</code> does not
219 * represent a valid AID
220 */
221 public static OntoAID internaliseAID(AbsConcept aid) throws OntologyException {
222 OntoAID ret = new OntoAID();
223
224 try {
225
226 ret.setName(aid.getString(BasicOntology.AID));
227
228
229 AbsAggregate addresses = (AbsAggregate) aid.getAbsObject(BasicOntology.FIPA_AID_Address_ADDRESSES);
230 for (int i = 0; i < addresses.size(); ++i) {
231 String addr = ((AbsPrimitive) addresses.get(i)).getString();
232 ret.addAddresses(addr);
233 }
234
235
236 AbsAggregate resolvers = (AbsAggregate) aid.getAbsObject(BasicOntology.FIPA_AID_Address_RESOLVERS);
237 for (int i = 0; i < resolvers.size(); ++i) {
238 OntoAID res = internaliseAID((AbsConcept) resolvers.get(i));
239 ret.addResolvers(res);
240 }
241
242 return ret;
243 }
244 catch (Exception e) {
245 throw new OntologyException(aid+" is not a valid AID");
246 }
247 }
248
249 /***
250 * Converts to an <code>AbsContentElementList</code> into a
251 * ContentElementList using the
252 * specified ontology.
253 * @param onto the ontology
254 * @return the ContentElementList
255 * @throws OntologyException
256 */
257 public static ContentElementList internaliseContentElementList(AbsContentElementList l, Ontology onto) throws OntologyException {
258 ContentElementList ret = new ContentElementList();
259
260 try {
261 for (int i = 0; i < l.size(); i++) {
262 ContentElement element = (ContentElement) onto.toObject(l.get(i));
263 ret.add(element);
264 }
265 }
266 catch (ClassCastException cce) {
267 throw new OntologyException("Non content element object in content element list");
268 }
269
270 return ret;
271 }
272
273 /***
274 * Converts to an <code>AbsAgentAction</code> representing an ACLMessage
275 * into an OntoACLMessage using the specified ontology.
276 * @param onto the ontology
277 * @return the OntoACLMessage
278 * @throws OntologyException
279 */
280 public static OntoACLMessage internaliseACLMessage(AbsAgentAction absMsg, Ontology onto) throws OntologyException {
281 OntoACLMessage ret = new OntoACLMessage();
282
283 try {
284 ret.setPerformative(absMsg.getInteger(BasicOntology.ACLMSG_PERFORMATIVE));
285 ret.setSender((AID) onto.toObject(absMsg.getAbsObject(BasicOntology.ACLMSG_SENDER)));
286
287 ret.clearAllReceiver();
288 List l = (List) onto.toObject(absMsg.getAbsObject(BasicOntology.ACLMSG_RECEIVERS));
289 Iterator it = l.iterator();
290 while (it.hasNext()) {
291 ret.addReceiver((AID) it.next());
292 }
293
294 ret.clearAllReplyTo();
295 l = (List) onto.toObject(absMsg.getAbsObject(BasicOntology.ACLMSG_REPLY_TO));
296 it = l.iterator();
297 while (it.hasNext()) {
298 ret.addReplyTo((AID) it.next());
299 }
300 ret.setLanguage(absMsg.getString(BasicOntology.ACLMSG_LANGUAGE));
301 ret.setOntology(absMsg.getString(BasicOntology.ACLMSG_ONTOLOGY));
302 ret.setProtocol(absMsg.getString(BasicOntology.ACLMSG_PROTOCOL));
303 ret.setInReplyTo(absMsg.getString(BasicOntology.ACLMSG_IN_REPLY_TO));
304 ret.setReplyWith(absMsg.getString(BasicOntology.ACLMSG_REPLY_WITH));
305 ret.setConversationId(absMsg.getString(BasicOntology.ACLMSG_CONVERSATION_ID));
306 ret.setReplyByDate(absMsg.getDate(BasicOntology.ACLMSG_REPLY_BY));
307 String c = absMsg.getString(BasicOntology.ACLMSG_CONTENT);
308 if (c != null) {
309 ret.setContent(c);
310 }
311 else {
312 byte[] bsc = absMsg.getByteSequence(BasicOntology.ACLMSG_BYTE_SEQUENCE_CONTENT);
313 if (bsc != null) {
314 ret.setByteSequenceContent(bsc);
315 }
316 }
317 ret.setEncoding(absMsg.getString(BasicOntology.ACLMSG_ENCODING));
318
319 return ret;
320 }
321 catch (Exception e) {
322 throw new OntologyException("Error internalising OntoACLMessage", e);
323 }
324 }
325
326 public static String toString(AbsObject abs) {
327 ByteArrayOutputStream str = new ByteArrayOutputStream();
328 ((AbsObjectImpl) abs).dump(0, new PrintStream(str));
329 return new String(str.toByteArray());
330
331 }
332 }
333