View Javadoc

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.onto;
26  
27  import JADE_SL.*;
28  import JADE_SL.abs.*;
29  import JADE_SL.schema.*;
30  import java.util.List;
31  import java.util.Iterator;
32  import JADE_SL.onto.basic.*;
33  
34  import java.util.Date;
35  
36  /***
37   * @author Federico Bergenti - Universita` di Parma
38   */
39  class BasicIntrospector implements Introspector {
40  
41      /***
42       * Translate an object of a class representing an element in an
43       * ontology into a proper abstract descriptor 
44       * @param onto The reference ontology 
45       * @param obj The Object to be translated
46       * @return The Abstract descriptor produced by the translation 
47  		 * @throws UnknownSchemaException If no schema for the object to be
48  		 * translated is defined in the ontology that uses this Introspector
49  		 * @throws OntologyException If some error occurs during the translation
50       */
51      public AbsObject externalise(Ontology onto, Ontology referenceOnto, Object obj) 
52      			throws UnknownSchemaException, OntologyException {
53          try {
54              if (obj == null) {
55                  return null;
56              } 
57  
58              if (obj instanceof String) {
59                  return AbsPrimitive.wrap((String) obj);
60              } 
61              if (obj instanceof Boolean) {
62                  return AbsPrimitive.wrap(((Boolean) obj).booleanValue());
63              } 
64              if (obj instanceof Integer) {
65                  return AbsPrimitive.wrap(((Integer) obj).intValue());
66              } 
67              if (obj instanceof Long) {
68                  return AbsPrimitive.wrap(((Long) obj).longValue());
69              } 
70              //__CLDC_UNSUPPORTED__BEGIN
71              if (obj instanceof Float) {
72                  return AbsPrimitive.wrap(((Float) obj).floatValue());
73              } 
74              if (obj instanceof Double) {
75                  return AbsPrimitive.wrap(((Double) obj).doubleValue());
76              } 
77              //__CLDC_UNSUPPORTED__END
78              if (obj instanceof Date) {
79                  return AbsPrimitive.wrap((Date) obj);
80              } 
81              if (obj instanceof byte[]) {
82                  return AbsPrimitive.wrap((byte[]) obj);
83              } 
84  
85  
86              if (obj instanceof List) {
87                return AbsHelper.externaliseList((List) obj, referenceOnto);
88              }
89  
90  	    			if (obj instanceof Iterator) {
91  							return AbsHelper.externaliseIterator((Iterator) obj, referenceOnto);
92  	    			}
93  	    
94  	    			if(obj instanceof AID) {
95  							return AbsHelper.externaliseAID((AID)obj);
96  	    			}
97  
98              if (obj instanceof ContentElementList) {
99              	return AbsHelper.externaliseContentElementList((ContentElementList) obj, referenceOnto);
100             } 
101 	    
102 	    			if(obj instanceof TrueProposition) {
103 	    				AbsPredicate absTrueProp = new AbsPredicate(BasicOntology.TRUE_PROPOSITION);
104 							return absTrueProp;
105 	    			}
106 
107 	    			if(obj instanceof Done) {
108 	    				AbsPredicate absDone = new AbsPredicate(BasicOntology.DONE);
109   						absDone.set(BasicOntology.DONE_ACTION, (AbsAgentAction) referenceOnto.fromObject(((Done) obj).getAction()));
110 							return absDone;
111 	    			}
112 
113 	    			if(obj instanceof Result) {
114 	    				AbsPredicate absResult = new AbsPredicate(BasicOntology.RESULT);
115   						absResult.set(BasicOntology.RESULT_ACTION, (AbsAgentAction) referenceOnto.fromObject(((Result) obj).getAction()));
116   						absResult.set(BasicOntology.RESULT_ITEMS, (AbsAggregate) referenceOnto.fromObject(((Result) obj).getItems()));
117 							return absResult;
118 	    			}
119 
120 	    			if(obj instanceof Equals) {
121 	    				AbsPredicate absEquals = new AbsPredicate(BasicOntology.EQUALS);
122   						absEquals.set(BasicOntology.EQUALS_LEFT, (AbsTerm) referenceOnto.fromObject(((Equals) obj).getLeft()));
123   						absEquals.set(BasicOntology.EQUALS_RIGHT, (AbsTerm) referenceOnto.fromObject(((Equals) obj).getRight()));
124 							return absEquals;
125 	    			}
126 
127 	    			if (obj instanceof Action) {
128 	    				AbsAgentAction absAction = new AbsAgentAction(BasicOntology.ACTION);
129 	    				((Action) obj).externalise(absAction, referenceOnto);
130 	    				return absAction;
131 	    			}
132 	    			
133 	    			if (obj instanceof ACLMessage) {
134 							return AbsHelper.externaliseACLMessage((ACLMessage)obj, referenceOnto);
135 	    			}
136 	    			
137             throw new UnknownSchemaException();
138         } 
139         catch (OntologyException oe) {
140         		// Forward the exception
141             throw oe;
142         } 
143         catch (Throwable t) {
144             throw new OntologyException("Schema and Java class do not match", t);
145         } 
146     } 
147 
148     /***
149      * Translate an abstract descriptor into an object of a proper class 
150      * representing an element in an ontology 
151      * @param onto The reference ontology 
152      * @param abs The abstract descriptor to be translated
153      *
154      * @return The Java object produced by the translation 
155      * @throws UngroundedException If the abstract descriptor to be translated 
156      * contains a variable
157 		 * @throws UnknownSchemaException If no schema for the abstract descriptor
158 		 * to be translated is defined in the ontology that uses this Introspector
159      * @throws OntologyException If some error occurs during the translation
160      */
161     public Object internalise(Ontology onto, Ontology referenceOnto, AbsObject abs) 
162     			throws UngroundedException, UnknownSchemaException, OntologyException {
163 
164         try {
165             if (abs == null) {
166                 return null;
167             } 
168 
169             // PRIMITIVE
170             if (abs instanceof AbsPrimitive) {
171                 return ((AbsPrimitive) abs).getObject();
172             } 
173             // AGGREGATES
174             if (abs instanceof AbsAggregate) {
175                 return AbsHelper.internaliseList((AbsAggregate) abs, referenceOnto);
176             } 
177 						// CONTENT ELEMENT LIST
178             if (abs instanceof AbsContentElementList) {
179             	return AbsHelper.internaliseContentElementList((AbsContentElementList) abs, referenceOnto);
180             } 
181 						// AID
182 	    			if (CaseInsensitiveString.equalsIgnoreCase(abs.getTypeName(), BasicOntology.AID)) { 
183 							return AbsHelper.internaliseAID((AbsConcept) abs);
184 	    			}
185 	    			// TRUE_PROPOSITION
186 	    			if (CaseInsensitiveString.equalsIgnoreCase(abs.getTypeName(), BasicOntology.TRUE_PROPOSITION)) { 
187 							TrueProposition t = new TrueProposition();
188 							return t;
189 	    			}
190 	    			// DONE
191 	    			if (CaseInsensitiveString.equalsIgnoreCase(abs.getTypeName(), BasicOntology.DONE)) { 
192 							Done d = new Done();
193   						d.setAction((AgentAction) referenceOnto.toObject(abs.getAbsObject(BasicOntology.DONE_ACTION))); 
194 							return d;
195 	    			}
196 	    			// RESULT
197 	    			if (CaseInsensitiveString.equalsIgnoreCase(abs.getTypeName(), BasicOntology.RESULT)) { 
198 							Result r = new Result();
199   						r.setAction((AgentAction) referenceOnto.toObject(abs.getAbsObject(BasicOntology.RESULT_ACTION))); 
200   						r.setItems((List) referenceOnto.toObject(abs.getAbsObject(BasicOntology.RESULT_ITEMS))); 
201 							return r;
202 	    			}
203 	    			// EQUALS
204 	    			if (CaseInsensitiveString.equalsIgnoreCase(abs.getTypeName(), BasicOntology.EQUALS)) { 
205 							Equals e = new Equals();
206   						e.setLeft(referenceOnto.toObject(abs.getAbsObject(BasicOntology.EQUALS_LEFT))); 
207   						e.setRight(referenceOnto.toObject(abs.getAbsObject(BasicOntology.EQUALS_RIGHT))); 
208 							return e;
209 	    			}
210 	    			// ACTION
211 	    			if (CaseInsensitiveString.equalsIgnoreCase(abs.getTypeName(), BasicOntology.ACTION)) { 
212 	    				Action a = new Action();
213 	    				a.internalise(abs, referenceOnto);
214 	    				return a;
215 	    			}
216 						// ACLMESSAGE
217 	    			if (CaseInsensitiveString.equalsIgnoreCase(abs.getTypeName(), BasicOntology.ACLMSG)) { 
218 							return AbsHelper.internaliseACLMessage((AbsAgentAction) abs, referenceOnto);
219 	    			}
220 	    			
221 	    			throw new UnknownSchemaException();
222         } 
223         catch (OntologyException oe) {
224         		// Forward the exception
225             throw oe;
226         } 
227         catch (Throwable t) {
228             throw new OntologyException("Schema and Java class do not match", t);
229         } 
230     } 
231 
232     /***
233        This method will never be called 
234      */
235     public void checkClass(ObjectSchema schema, Class javaClass) throws OntologyException {
236     }
237 }