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.abs;
26  
27  import java.util.*;
28  import java.io.PrintStream;
29  import JADE_SL.lang.Codec;
30  
31  /***
32   * @author Federico Bergenti - Universita` di Parma
33   */
34  public class AbsAggregate extends AbsConcept {
35  		private static final int UNNAMEDPREFIX_LENGTH = Codec.UNNAMEDPREFIX.length();
36      private List elements = new ArrayList();
37  
38      /***
39       * Construct an Abstract descriptor to hold an aggregate of
40       * the proper type (i.e. SET, SEQUENCE...).
41       * @param typeName The name of the type of the aggregate held by 
42       * this abstract descriptor.
43       */
44      public AbsAggregate(String typeName) {
45        //this.typeName = typeName;
46      	super(typeName);
47      }
48  
49      /***
50       * Adds a new element (that must be a term) to this aggregate.
51       * @param element The element to add.
52       */
53      public void add(AbsTerm element) {
54          elements.add(element);
55      } 
56  
57      /***
58       * Retrieves the number of elements in this aggregate.
59       * @return The number of elements.
60       */
61      public int size() {
62          return elements.size();
63      } 
64  
65      /***
66       * Retrieves the <code>i</code>-th element in this aggregate.
67       * @param i The index of the element to retrieve.
68       * @return The element.
69       */
70      public AbsTerm get(int i) {
71          return (AbsTerm) elements.get(i);
72      } 
73  
74      /***
75       * @return An <code>Iterator</code> over the elements of this
76       * aggregate.
77       */
78      public Iterator iterator() {
79          return elements.iterator();
80      } 
81  
82      /***
83       * Clear all the elements in this aggregate.
84       */
85      public void clear() {
86  			elements.clear();
87      }
88  
89     	/***
90       * Test if a given term is contained in this aggregate.
91       * @return <code>true</code> if the given term is contained
92       * in this aggregate.
93       */
94      public boolean contains (AbsTerm element) {
95  			return elements.contains(element);
96      }
97  
98     	/***
99       * Returns the position of an element within this aggregate.
100      * @return The position of an element within this aggregate.
101      */
102     public int indexOf (AbsTerm element) {
103 			return elements.indexOf(element);
104     }
105 
106    	/***
107      * Removes the element at the given position from this aggregate.
108      * @return The removed element.
109      */
110     public AbsTerm remove (int index) {
111 			return (AbsTerm)elements.remove(index);
112     }
113 
114     /***
115      * Removes an element from this aggregate.
116      * @return The removed element.
117      */
118     public boolean remove (AbsTerm element) {
119 			return elements.remove(element);
120     }
121 
122    	/***
123      * Test if the aggregate is empty.
124      * @return <code>true</code> if this aggregate does not contain
125      * any element.
126      */
127     public boolean isEmpty () {
128 			return elements.isEmpty();
129     }
130 
131    	/***
132      * Retrieve all elements in this aggregate in the form of an array.
133      * @return An array containing all elements in this aggregate.
134      */
135     public AbsTerm[] toArray () {
136 			int size = elements.size();
137       AbsTerm[] tmp = new AbsTerm[size];
138       for (int i = 0; i < size; i++)
139       	tmp[i] = (AbsTerm)elements.get(i);
140       return tmp;
141     }
142 
143     /***
144        Overrides this method to check that name is of the form
145        Codec.UNNAMEDPERFIX+index
146        @throws IllegalArgumentException if name is not of the form
147        Codec.UNNAMEDPERFIX+index
148      */
149     public void set(String name, AbsTerm value) {
150     	elements.add(toIndex(name), value);	
151     } 
152 
153     private int toIndex(String name) { 
154     	try {
155 	    	if (name.startsWith(Codec.UNNAMEDPREFIX) ) {
156   	  		String index = name.substring(UNNAMEDPREFIX_LENGTH);
157     			return Integer.parseInt(index);
158 	    	}
159 	    	else {
160 	    		throw new IllegalArgumentException(name+" is not a valid index");
161 	    	}
162     	}
163     	catch (Exception e) {
164 	    	throw new IllegalArgumentException(name+" is not a valid index");
165     	}
166     }
167     			
168     protected void dump(int indent, PrintStream ps) {
169         for (int i = 0; i < indent; i++) {
170             ps.print("  ");
171         }
172 
173         ps.println("(");
174 
175         for (int i = 0; i < elements.size(); i++) {
176             ((AbsObjectImpl) elements.get(i)).dump(indent + 1, ps);
177         }
178 
179         for (int i = 0; i < indent; i++) {
180             ps.print("  ");
181         }
182 
183         ps.println(")");
184     } 
185     
186     
187     /***
188        Overrides this method to check that name is of the form
189        Codec.UNNAMEDPERFIX+index
190      */
191     public AbsObject getAbsObject(String name) {
192     	return (AbsObject) elements.get(toIndex(name));
193     }
194 
195     /***
196        Overrides method in superclass
197      */
198     public String[] getNames() {
199     	String names[] = new String[elements.size()];
200     	for (int i = 0; i < names.length; ++i) {
201     		names[i] = Codec.UNNAMEDPREFIX+i;
202     	}
203     	return names;
204     }
205 
206     /***
207      * Tests if this AbsAggregate is grounded, i.e., if no one of its elements 
208      * is associated with a variable
209      * @return <code>true</code> if the object is grounded.
210      */
211     public boolean isGrounded() {
212     	return true;
213     	//FIXME: Not yet implemented
214     }
215 
216     /***
217        Overrides method in superclass
218      */
219     public int getCount() {
220     	return elements.size();
221     }
222 
223     public void dump() {
224       dump(0, System.out);
225     }
226  
227     public String toString() {
228     	StringBuffer sb = new StringBuffer("(");
229     	sb.append(getTypeName()); // very misleading!
230     	Iterator it = elements.iterator();
231     	int i = 0;
232     	while (it.hasNext()) {
233     		sb.append(" #"+i+" "+it.next());
234     		++i;
235     	}
236     	sb.append(")");
237     	return sb.toString();
238     }
239 }
240