KickJava   Java API By Example, From Geeks To Geeks.

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


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.jgap.gp.terminal.*;
15 import org.apache.commons.lang.builder.CompareToBuilder;
16 import org.apache.commons.lang.builder.EqualsBuilder;
17 import org.jgap.gp.impl.*;
18
19 /**
20  * The for-loop loop from 0 to X-1.
21  *
22  * @author Klaus Meffert
23  * @since 3.0
24  */

25 public class ForXLoop
26     extends CommandGene {
27   /** String containing the CVS revision. Read out via reflection!*/
28   private final static String JavaDoc CVS_REVISION = "$Revision: 1.8 $";
29
30   private Class JavaDoc m_type;
31
32   public ForXLoop(final GPConfiguration a_conf, Class JavaDoc a_type)
33       throws InvalidConfigurationException {
34     super(a_conf, 1, CommandGene.VoidClass);
35     m_type = a_type;
36   }
37
38   public String JavaDoc toString() {
39     return "for(int i=0;i<X;i++) { &1 }";
40   }
41
42   /**
43    * @return textual name of this command
44    *
45    * @author Klaus Meffert
46    * @since 3.2
47    */

48   public String JavaDoc getName() {
49     return "ForXLoop";
50   }
51
52   public void execute_void(ProgramChromosome c, int n, Object JavaDoc[] args) {
53     check(c);
54     int index = c.getVariableWithReturnType(0, m_type);
55     if (index < 0) {
56       throw new IllegalStateException JavaDoc("Variable missing for forX");
57     }
58     /**@todo only consider variables appearing before FORX in the program tree*/
59     Variable var = (Variable) c.getNode(index);
60     int x;
61     // Get variable's value as integer ("for" cannot do anything else here).
62
// ---------------------------------------------------------------------
63
if (m_type == CommandGene.IntegerClass) {
64       x = ( (Integer JavaDoc) var.getValue()).intValue();
65     }
66     else if (m_type == CommandGene.LongClass) {
67       x = ( (Long JavaDoc) var.getValue()).intValue();
68     }
69     else if (m_type == CommandGene.DoubleClass) {
70       x = ( (Double JavaDoc) var.getValue()).intValue();
71     }
72     else if (m_type == CommandGene.FloatClass) {
73       x = ( (Float JavaDoc) var.getValue()).intValue();
74     }
75     else {
76       throw new RuntimeException JavaDoc("Type " + m_type + " unknown in ForXCommand");
77     }
78     if (x > 15) {
79       x = 15;
80     }
81     for (int i = 0; i < x; i++) {
82       c.execute_void(n, 0, args);
83     }
84   }
85
86   public boolean isValid(ProgramChromosome a_program) {
87     return a_program.getVariableWithReturnType(0, m_type) >= 0;
88   }
89
90   public Class JavaDoc getChildType(IGPProgram a_ind, int a_chromNum) {
91     return CommandGene.VoidClass;
92   }
93
94   public Class JavaDoc getReturnType() {
95     return super.getReturnType();
96   }
97
98   /**
99    * The compareTo-method.
100    * @param a_other the other object to compare
101    * @return -1, 0, 1
102    *
103    * @author Klaus Meffert
104    * @since 3.0
105    */

106   public int compareTo(Object JavaDoc a_other) {
107     if (a_other == null) {
108       return 1;
109     }
110     else {
111       ForXLoop other = (ForXLoop) a_other;
112       return new CompareToBuilder()
113           .append(m_type, other.m_type)
114           .toComparison();
115     }
116   }
117
118   /**
119    * The equals-method.
120    * @param a_other the other object to compare
121    * @return true if the objects are seen as equal
122    *
123    * @author Klaus Meffert
124    * @since 3.0
125    */

126   public boolean equals(Object JavaDoc a_other) {
127     if (a_other == null) {
128       return false;
129     }
130     else {
131       try {
132         ForXLoop other = (ForXLoop) a_other;
133         return new EqualsBuilder()
134             .append(m_type, other.m_type)
135             .isEquals();
136       } catch (ClassCastException JavaDoc cex) {
137         return false;
138       }
139     }
140   }
141 }
142
Popular Tags