KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgap > gp > function > ForLoop


1 /*
2  * This file is part of JGAP.
3  *
4  * JGAP offers a dual license model containing the LGPL as well as the MPL.
5  *
6  * For licencing information please see the file license.txt included with JGAP
7  * or have a look at the top of class org.jgap.Chromosome which representatively
8  * includes the JGAP license policy applicable for any file delivered with JGAP.
9  */

10 package org.jgap.gp.function;
11
12 import org.jgap.*;
13 import org.jgap.gp.*;
14 import org.apache.commons.lang.builder.*;
15 import org.jgap.gp.impl.*;
16
17 /**
18  * The for-loop.
19  *
20  * @author Klaus Meffert
21  * @since 3.0
22  */

23 public class ForLoop
24     extends CommandGene {
25   /** String containing the CVS revision. Read out via reflection!*/
26   private final static String JavaDoc CVS_REVISION = "$Revision: 1.13 $";
27
28   private static String JavaDoc INTERNAL_COUNTER_STORAGE = "FORLOOPSTORAGE_INT";
29
30   private Class JavaDoc m_typeVar;
31
32   private int m_startIndex;
33
34   private int m_endIndex;
35
36   private int m_increment;
37
38   private int m_maxLoop;
39
40   private String JavaDoc m_memory_name_int;
41
42   private String JavaDoc m_varName;
43
44   /**
45    * Constructor.
46    *
47    * @param a_conf the configuration to use
48    * @param a_typeVar Class of the loop counter terminakl (e.g. IntegerClass)
49    * @param a_maxLoop the maximum number of loops to perform
50    * @throws InvalidConfigurationException
51    *
52    * @author Klaus Meffert
53    * @since 3.0
54    */

55   public ForLoop(final GPConfiguration a_conf, Class JavaDoc a_typeVar, int a_maxLoop)
56       throws InvalidConfigurationException {
57     this(a_conf, a_typeVar, 0, a_maxLoop);
58   }
59
60   /**
61    * Constructor allowing to preset the starting index of the loop.
62    *
63    * @param a_conf the configuration to use
64    * @param a_typeVar Class of the loop counter terminakl (e.g. IntegerClass)
65    * @param a_startIndex index to start the loop with (normally 0)
66    * @param a_maxLoop the maximum number of loops to perform
67    * @throws InvalidConfigurationException
68    *
69    * @author Klaus Meffert
70    * @since 3.0
71    */

72   public ForLoop(final GPConfiguration a_conf, Class JavaDoc a_typeVar,
73                  int a_startIndex, int a_maxLoop)
74       throws InvalidConfigurationException {
75     this(a_conf, a_typeVar, a_startIndex, a_maxLoop, "i");
76   }
77
78   public ForLoop(final GPConfiguration a_conf, Class JavaDoc a_typeVar,
79                  int a_startIndex, int a_maxLoop, String JavaDoc a_varName)
80       throws InvalidConfigurationException {
81     super(a_conf, 2, CommandGene.VoidClass);
82     m_typeVar = a_typeVar;
83     m_maxLoop = a_maxLoop;
84     m_startIndex = a_startIndex;
85     m_endIndex = -1;
86     m_increment = 1;
87     m_varName = a_varName;
88     init();
89   }
90
91   /**
92    * Constructor allowing to preset the starting and the ending index of the
93    * loop.
94    *
95    * @param a_conf the configuration to use
96    * @param a_typeVar Class of the loop counter terminakl (e.g. IntegerClass)
97    * @param a_startIndex index to start the loop with
98    * @param a_endIndex index to end the loop with
99    * @param a_increment the maximum number of loops to perform
100    * @param a_varName informal textual name of the loop counter variable
101    * @throws InvalidConfigurationException
102    *
103    * @author Klaus Meffert
104    * @since 3.2
105    */

106   public ForLoop(final GPConfiguration a_conf, Class JavaDoc a_typeVar,
107                  int a_startIndex, int a_endIndex, int a_increment,
108                  String JavaDoc a_varName)
109       throws InvalidConfigurationException {
110     this(a_conf, a_typeVar, a_startIndex, a_endIndex, a_increment, a_varName, 0,
111          0);
112   }
113
114   public ForLoop(final GPConfiguration a_conf, Class JavaDoc a_typeVar,
115                  int a_startIndex, int a_endIndex, int a_increment,
116                  String JavaDoc a_varName, int a_subReturnType, int a_subChildType)
117       throws InvalidConfigurationException {
118     super(a_conf, 1, CommandGene.VoidClass, a_subReturnType, a_subChildType);
119     m_typeVar = a_typeVar;
120     m_increment = a_increment;
121     m_startIndex = a_startIndex;
122     m_endIndex = a_endIndex;
123     m_varName = a_varName;
124     init();
125   }
126
127   protected void init() {
128     super.init();
129     // Generate unique name.
130
// ---------------------
131
m_memory_name_int = INTERNAL_COUNTER_STORAGE;
132     m_memory_name_int += m_varName;
133     m_memory_name_int += getGPConfiguration().getRandomGenerator().nextDouble();
134   }
135
136   public String JavaDoc toString() {
137     if (m_endIndex == -1) {
138       return "for(int i=" + m_startIndex + ";i<&1;i++) { &2 }";
139     }
140     else {
141       String JavaDoc incrString;
142       if (m_increment == 1) {
143         incrString = m_varName + "++";
144       }
145       else {
146         incrString = m_varName + "=" + m_varName + "+1";
147       }
148       return "for(int " + m_varName + "=" + m_startIndex + ";" + m_varName +
149           "<" + m_endIndex + ";" +
150           incrString + ") { &1 }";
151     }
152   }
153
154   /**
155    * @return textual name of this command
156    *
157    * @author Klaus Meffert
158    * @since 3.2
159    */

160   public String JavaDoc getName() {
161     return "ForLoop";
162   }
163
164   public void execute_void(ProgramChromosome c, int n, Object JavaDoc[] args) {
165     // Determine the end index of the loop (child at index 0).
166
// -------------------------------------------------------
167
int x;
168     if (m_endIndex == -1) {
169       if (m_typeVar == CommandGene.IntegerClass) {
170         x = c.execute_int(n, 0, args);
171       }
172       else if (m_typeVar == CommandGene.LongClass) {
173         x = (int) c.execute_long(n, 0, args);
174       }
175       else if (m_typeVar == CommandGene.DoubleClass) {
176         x = (int) Math.round(c.execute_double(n, 0, args));
177       }
178       else if (m_typeVar == CommandGene.FloatClass) {
179         x = (int) Math.round(c.execute_float(n, 0, args));
180       }
181       else {
182         throw new RuntimeException JavaDoc("Type "
183                                    + m_typeVar
184                                    + " not supported by ForLoop");
185       }
186       if (x > m_maxLoop) {
187         x = m_maxLoop;
188       }
189       // Repeatedly execute the second child (index = 1).
190
// ------------------------------------------------
191
for (int i = m_startIndex; i < x; i++) {
192         c.execute_void(n, 1, args);
193       }
194     }
195     else {
196       // Repeatedly execute the first child (index = 0).
197
// -----------------------------------------------
198
for (int i = m_startIndex; i < m_endIndex; i = i + m_increment) {
199         // Store counter in memory.
200
// ------------------------
201
getGPConfiguration().storeInMemory(ForLoop.INTERNAL_COUNTER_STORAGE,
202             new Integer JavaDoc(i));
203         c.execute_void(n, 0, args);
204       }
205     }
206   }
207
208   public boolean isValid(ProgramChromosome a_program) {
209     return true;
210   }
211
212   public Class JavaDoc getChildType(IGPProgram a_ind, int a_chromNum) {
213     if (m_endIndex == -1) {
214       // Variant A: dynamic end index
215
if (a_chromNum == 0) {
216         // Loop counter variable.
217
// ----------------------
218
return m_typeVar;
219       }
220       else {
221         // Subprogram.
222
// -----------
223
return CommandGene.VoidClass;
224       }
225     }
226     else {
227       // Variant B: fixed end index
228
return CommandGene.VoidClass;
229     }
230   }
231
232   /**
233    * The compareTo-method.
234    *
235    * @param a_other the other object to compare
236    * @return -1, 0, 1
237    *
238    * @author Klaus Meffert
239    * @since 3.0
240    */

241   public int compareTo(Object JavaDoc a_other) {
242     if (a_other == null) {
243       return 1;
244     }
245     else {
246       ForLoop other = (ForLoop) a_other;
247       return new CompareToBuilder()
248           .append(m_typeVar, other.m_typeVar)
249           .append(m_maxLoop, other.m_maxLoop)
250           .toComparison();
251     }
252   }
253
254   /**
255    * The equals-method.
256    *
257    * @param a_other the other object to compare
258    * @return true if the objects are seen as equal
259    *
260    * @author Klaus Meffert
261    * @since 3.0
262    */

263   public boolean equals(Object JavaDoc a_other) {
264     if (a_other == null) {
265       return false;
266     }
267     else {
268       try {
269         ForLoop other = (ForLoop) a_other;
270         return new EqualsBuilder()
271             .append(m_typeVar, other.m_typeVar)
272             .append(m_maxLoop, other.m_maxLoop)
273             .isEquals();
274       } catch (ClassCastException JavaDoc cex) {
275         return false;
276       }
277     }
278   }
279
280   /**
281    * @return Name of the memory cell where the current value of the loop
282    * variable is stored
283    *
284    * @author Klaus Meffert
285    * @since 3.2
286    */

287   public String JavaDoc getCounterMemoryName() {
288     return m_memory_name_int;
289   }
290 }
291
Popular Tags