KickJava   Java API By Example, From Geeks To Geeks.

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


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.CompareToBuilder;
15 import org.apache.commons.lang.builder.EqualsBuilder;
16 import org.jgap.gp.impl.*;
17
18 /**
19  * A loop.
20  *
21  * @author Klaus Meffert
22  * @since 3.01
23  */

24 public class Loop
25     extends CommandGene {
26   /** String containing the CVS revision. Read out via reflection!*/
27   private final static String JavaDoc CVS_REVISION = "$Revision: 1.4 $";
28
29   private Class JavaDoc m_typeVar;
30
31   private int m_count;
32
33   /**
34    * Constructor.
35    *
36    * @param a_conf the configuration to use
37    * @param a_typeVar Class of the loop counter terminakl (e.g. IntegerClass)
38    * @param a_count the number of loops to perform
39    * @throws InvalidConfigurationException
40    *
41    * @author Klaus Meffert
42    * @since 3.01
43    */

44   public Loop(final GPConfiguration a_conf, Class JavaDoc a_typeVar, int a_count)
45       throws InvalidConfigurationException {
46     this(a_conf, a_typeVar, a_count, 0, 0);
47   }
48
49   public Loop(final GPConfiguration a_conf, Class JavaDoc a_typeVar, int a_count,
50               int a_subReturnType, int a_subChildType)
51       throws InvalidConfigurationException {
52     super(a_conf, 1, CommandGene.VoidClass, a_subReturnType, a_subChildType);
53     m_typeVar = a_typeVar;
54     m_count = a_count;
55   }
56
57   public String JavaDoc toString() {
58     return "loop(" + m_count + ", &1 }";
59   }
60
61   /**
62    * @return textual name of this command
63    *
64    * @author Klaus Meffert
65    * @since 3.2
66    */

67   public String JavaDoc getName() {
68     return "Loop";
69   }
70
71   public void execute_void(ProgramChromosome c, int n, Object JavaDoc[] args) {
72     // Repeatedly execute the child.
73
// -----------------------------
74
for (int i = 0; i < m_count; i++) {
75       c.execute_void(n, 0, args);
76     }
77   }
78
79   public boolean isValid(ProgramChromosome a_program) {
80     return true;
81   }
82
83   public Class JavaDoc getChildType(IGPProgram a_ind, int a_chromNum) {
84     return CommandGene.VoidClass;
85   }
86
87   /**
88    * The compareTo-method.
89    *
90    * @param a_other the other object to compare
91    * @return -1, 0, 1
92    *
93    * @author Klaus Meffert
94    * @since 3.01
95    */

96   public int compareTo(Object JavaDoc a_other) {
97     if (a_other == null) {
98       return 1;
99     }
100     else {
101       Loop other = (Loop) a_other;
102       return new CompareToBuilder()
103           .append(m_typeVar, other.m_typeVar)
104           .append(m_count, other.m_count)
105           .toComparison();
106     }
107   }
108
109   /**
110    * The equals-method.
111    *
112    * @param a_other the other object to compare
113    * @return true if the objects are seen as equal
114    *
115    * @author Klaus Meffert
116    * @since 3.01
117    */

118   public boolean equals(Object JavaDoc a_other) {
119     if (a_other == null) {
120       return false;
121     }
122     else {
123       try {
124         Loop other = (Loop) a_other;
125         return new EqualsBuilder()
126             .append(m_typeVar, other.m_typeVar)
127             .append(m_count, other.m_count)
128             .isEquals();
129       } catch (ClassCastException JavaDoc cex) {
130         return false;
131       }
132     }
133   }
134 }
135
Popular Tags