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 import java.util.Hashtable;
27 import java.util.Enumeration;
28 import java.io.PrintStream;
29
30 /***
31 * Base class for all abstract descriptor classes.
32 * @author Federico Bergenti - Universita` di Parma
33 */
34 public class AbsObjectImpl implements AbsObject {
35 private Hashtable elements = new Hashtable();
36 private String typeName = null;
37
38 /***
39 * Construct an Abstract descriptor to hold an object of
40 * the proper type.
41 * @param typeName The name of the type of the object held by this
42 * abstract descriptor.
43 */
44 protected AbsObjectImpl(String typeName) {
45 this.typeName = typeName;
46 }
47
48 /***
49 * @return The name of the type of the object held by this
50 * abstract descriptor.
51 * @see AbsObject#getTypeName()
52 */
53 public String getTypeName() {
54 return typeName;
55 }
56
57 /***
58 * Sets an attribute of the object held by this
59 * abstract descriptor.
60 * @param name The name of the attribute to be set.
61 * @param value The new value of the attribute. If <code>value</code>
62 * is null the current mapping with <code>name</code> (if any) is
63 * removed.
64 */
65 protected void set(String name, AbsObject value) {
66 if (value == null) {
67 elements.remove(name);
68 }
69 else {
70 elements.put(name.toUpperCase(), (Object)value);
71 }
72 }
73
74 /***
75 * Gets the value of an attribute of the object held by this
76 * abstract descriptor.
77 * @param name The name of the attribute.
78 * @return value The value of the attribute.
79 * @see AbsObject#getAbsObject()
80 */
81 public AbsObject getAbsObject(String name) {
82 return (AbsObject) elements.get(name.toUpperCase());
83 }
84
85 /***
86 * @return the name of all attributes.
87 * @see AbsObject#getNames()
88 */
89 public String[] getNames() {
90 String[] names = new String[getCount()];
91 int count = getCount() - 1;
92
93 for (Enumeration e = elements.keys(); e.hasMoreElements(); ) {
94 names[count--] = (String) e.nextElement();
95 }
96
97 return names;
98 }
99
100 public Enumeration getNameEnumeration () {
101 return (elements.keys());
102 }
103
104 /***
105 * Tests if the object is grounded, i.e., if no one of its attributes
106 * is associated with a variable
107 * @return <code>true</code> if the object is grounded.
108 * @see AbsObject#isGrounded()
109 */
110 public boolean isGrounded() {
111 Enumeration e = elements.elements();
112 while (e.hasMoreElements()) {
113 AbsObject abs = (AbsObject) e.nextElement();
114 if (!abs.isGrounded()) {
115 return false;
116 }
117 }
118 return true;
119 }
120
121 /***
122 * Gets the number of attributes.
123 * @return the number of attributes.
124 * @see AbsObject#getCount()
125 */
126 public int getCount() {
127 return elements.size();
128 }
129
130 protected void dump(int indent, PrintStream ps) {
131 for (int i = 0; i < indent; i++) {
132 ps.print(" "); }
133
134 ps.println(getTypeName());
135 ps.println(this.getClass());
136 String[] names = getNames();
137
138 for (int i = 0; i < getCount(); i++) {
139 for (int j = 0; j < indent; j++) {
140 ps.print(" "); }
141
142 ps.println(":" + names[i]);
143 Object next = getAbsObject(names[i]);
144 try {
145 AbsObjectImpl abs = (AbsObjectImpl) next;
146 abs.dump(indent + 1, ps);
147 }
148 catch (Exception e) {
149 try {
150 if (next instanceof AbsAggregate) {
151 AbsAggregate aggr = (AbsAggregate) next;
152 ps.println(aggr.getTypeName() + " " + aggr.toString()); }
153 else {
154 AbsPrimitive prim = (AbsPrimitive) next;
155 prim.dump (indent +1,ps);
156 }}
157 catch (Exception ne){
158 System.out.println("unknowType? : " + next.toString() +" "+ next.getClass().toString());
159
160 }
161 }
162 }
163 }
164
165 /***
166 * @see AbsObject#dump()
167 */
168 public void dump() {
169 dump(0, System.out);
170 }
171
172 public String toString() {
173 StringBuffer sb = new StringBuffer("(");
174 sb.append(getTypeName());
175
176 String[] names = getNames();
177
178 for (int i = 0; i < names.length; i++) {
179 sb.append(" :"+names[i]+" "+getAbsObject(names[i]));
180 }
181 sb.append(")");
182
183 return sb.toString();
184 }
185 }
186