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
31 /***
32 * @author Giovanni Caire - TILAB
33 */
34 public class MicroIntrospector implements Introspector {
35
36 /***
37 * Translate an object of a class representing an element in an
38 * ontology into a proper abstract descriptor
39 * @param onto The reference ontology
40 * @param obj The Object to be translated
41 * @return The Abstract descriptor produced by the translation
42 * @throws UnknownSchemaException If no schema for the object to be
43 * translated is defined in the ontology that uses this Introspector
44 * @throws OntologyException If some error occurs during the translation
45 */
46 public AbsObject externalise(Ontology onto, Ontology referenceOnto, Object obj)
47 throws UnknownSchemaException, OntologyException {
48
49 try {
50 Class javaClass = obj.getClass();
51 ObjectSchema schema = onto.getSchema(javaClass);
52 if (schema == null) {
53 throw new UnknownSchemaException();
54 }
55
56 AbsObject abs = schema.newInstance();
57
58 Introspectable intro = (Introspectable) obj;
59 intro.externalise(abs, referenceOnto);
60 return abs;
61 }
62 catch (OntologyException oe) {
63
64 throw oe;
65 }
66 catch (ClassCastException cce) {
67 throw new OntologyException("Object "+obj+" is not Introspectable");
68 }
69 catch (Exception e) {
70 throw new OntologyException("Unexpected error");
71 }
72 }
73
74 /***
75 * Translate an abstract descriptor into an object of a proper class
76 * representing an element in an ontology
77 * @param onto The reference ontology
78 * @param abs The abstract descriptor to be translated
79 *
80 * @return The Java object produced by the translation
81 * @throws UngroundedException If the abstract descriptor to be translated
82 * contains a variable
83 * @throws UnknownSchemaException If no schema for the abstract descriptor
84 * to be translated is defined in the ontology that uses this Introspector
85 * @throws OntologyException If some error occurs during the translation
86 */
87 public Object internalise(Ontology onto, Ontology referenceOnto, AbsObject abs)
88 throws UngroundedException, UnknownSchemaException, OntologyException {
89 try {
90 String type = abs.getTypeName();
91
92
93 ObjectSchema schema = onto.getSchema(type, false);
94 if (schema == null) {
95 throw new UnknownSchemaException();
96 }
97
98 if (schema instanceof IRESchema || schema instanceof VariableSchema) {
99 throw new UngroundedException();
100 }
101
102 Class javaClass = onto.getClassForElement(type);
103 if (javaClass == null) {
104 throw new OntologyException("No java class associated to type "+type);
105 }
106
107
108 Object obj = javaClass.newInstance();
109
110
111 Introspectable intro = (Introspectable) obj;
112 intro.internalise(abs, referenceOnto);
113 return intro;
114 }
115 catch (OntologyException oe) {
116
117 throw oe;
118 }
119 catch (ClassCastException cce) {
120 throw new OntologyException("Class for type "+abs.getTypeName()+" is not Introspectable");
121 }
122 catch (Exception e) {
123 throw new OntologyException("Unexpected error");
124 }
125 }
126
127 /***
128 Check the structure of a java class associated to an ontological element
129 to ensure that translations to/from abstract descriptors and java objects
130 (instances of that class) can be accomplished by this introspector.
131 This is the case if <code>javaClass</code> implements the
132 <code>Introspectable</code>
133 @param schema The schema of the ontological element
134 @param javaClass The java class associated to the ontologcal element
135 @throws OntologyException if the java class does not have the correct
136 structure
137 */
138 public void checkClass(ObjectSchema schema, Class javaClass) throws OntologyException {
139
140 }
141 }