KickJava   Java API By Example, From Geeks To Geeks.

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


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 equals operation.
18  *
19  * @author Klaus Meffert
20  * @since 3.0
21  */

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

51   public String JavaDoc getName() {
52     return "Equals";
53   }
54
55   public boolean execute_boolean(ProgramChromosome c, int n, Object JavaDoc[] args) {
56     if (m_type == CommandGene.BooleanClass) {
57       return c.execute_boolean(n, 0, args) == c.execute_boolean(n, 1, args);
58     }
59     else if (m_type == CommandGene.IntegerClass) {
60       return c.execute_int(n, 0, args) == c.execute_int(n, 1, args);
61     }
62     else if (m_type == CommandGene.LongClass) {
63       return c.execute_long(n, 0, args) == c.execute_long(n, 1, args);
64     }
65     else if (m_type == CommandGene.DoubleClass) {
66       return Math.abs(c.execute_double(n, 0, args) -
67                       c.execute_double(n, 1, args)) < DELTA;
68     }
69     else if (m_type == CommandGene.FloatClass) {
70       return Math.abs(c.execute_float(n, 0, args) -
71                       c.execute_float(n, 1, args)) < DELTA;
72     }
73     else if (m_type == CommandGene.VoidClass) {
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 Equals-command!");
78   }
79
80   public Class JavaDoc getChildType(IGPProgram a_ind, int a_index) {
81     return m_type;
82   }
83 }
84
Popular Tags