KickJava   Java API By Example, From Geeks To Geeks.

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


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.impl.*;
15
16 /**
17  * The Greater Than (x > y) operation.
18  *
19  * @author Klaus Meffert
20  * @since 3.2
21  */

22 public class GreaterThan
23     extends MathCommand {
24   /** String containing the CVS revision. Read out via reflection!*/
25   private final static String JavaDoc CVS_REVISION = "$Revision: 1.1 $";
26
27   private Class JavaDoc m_type;
28
29   public GreaterThan(final GPConfiguration a_conf, Class JavaDoc a_type)
30       throws InvalidConfigurationException {
31     this(a_conf, a_type, 0, null);
32   }
33
34   public GreaterThan(final GPConfiguration a_conf, Class JavaDoc a_type,
35                      int a_subReturnType,
36                      int[] a_subChildTypes)
37       throws InvalidConfigurationException {
38     super(a_conf, 2, CommandGene.BooleanClass, a_subReturnType, a_subChildTypes);
39     m_type = a_type;
40   }
41
42   public String JavaDoc toString() {
43     return "&1 > &2";
44   }
45
46   /**
47    * @return textual name of this command
48    *
49    * @author Klaus Meffert
50    * @since 3.2
51    */

52   public String JavaDoc getName() {
53     return "GreaterThan";
54   }
55
56   public boolean execute_boolean(ProgramChromosome c, int n, Object JavaDoc[] args) {
57     if (m_type == CommandGene.BooleanClass) {
58       return c.execute_boolean(n, 0, args) && !c.execute_boolean(n, 1, args);
59     }
60     else if (m_type == CommandGene.IntegerClass) {
61       return c.execute_int(n, 0, args) > c.execute_int(n, 1, args);
62     }
63     else if (m_type == CommandGene.LongClass) {
64       return c.execute_long(n, 0, args) > c.execute_long(n, 1, args);
65     }
66     else if (m_type == CommandGene.DoubleClass) {
67       return c.execute_double(n, 0, args) > c.execute_double(n, 1, args);
68     }
69     else if (m_type == CommandGene.FloatClass) {
70       return c.execute_float(n, 0, args) > c.execute_float(n, 1, args);
71     }
72 // else if (m_type == CommandGene.VoidClass) {
73
// // We could support Comparables
74
// return c.execute_object(n, 0, args).equals(c.execute_object(n, 1, args));
75
// }
76
throw new UnsupportedOperationException JavaDoc("Unsupported type " + m_type +
77         " for GreaterThan-command!");
78   }
79
80   public Class JavaDoc getChildType(IGPProgram a_ind, int a_index) {
81     return m_type;
82   }
83 }
84
Popular Tags