KickJava   Java API By Example, From Geeks To Geeks.

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


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  * The add operation that stores the result in internal memory afterwards.
20  *
21  * @author Klaus Meffert
22  * @since 3.0
23  */

24 public class AddAndStore
25     extends MathCommand {
26   /** String containing the CVS revision. Read out via reflection!*/
27   private final static String JavaDoc CVS_REVISION = "$Revision: 1.9 $";
28
29   /**
30    * Symbolic name of the storage. Must correspond with a chosen name for
31    * ReadTerminalCommand.
32    */

33   private String JavaDoc m_storageName;
34
35   private Class JavaDoc m_type;
36
37   public AddAndStore(final GPConfiguration a_conf, Class JavaDoc a_type,
38                      String JavaDoc a_storageName)
39       throws InvalidConfigurationException {
40     super(a_conf, 2, CommandGene.VoidClass);
41     m_type = a_type;
42     m_storageName = a_storageName;
43   }
44
45   public String JavaDoc toString() {
46     return "Store(" + m_storageName + ", &1 + &2)";
47   }
48
49   /**
50    * @return textual name of this command
51    *
52    * @author Klaus Meffert
53    * @since 3.2
54    */

55   public String JavaDoc getName() {
56     return "AddAndStore";
57   }
58
59   public void execute_void(ProgramChromosome c, int n, Object JavaDoc[] args) {
60     Object JavaDoc value = null;
61     if (m_type == CommandGene.IntegerClass) {
62       value = new Integer JavaDoc(c.execute_int(n, 0, args) + c.execute_int(n, 1, args));
63     }
64     else if (m_type == CommandGene.LongClass) {
65       value = new Long JavaDoc(c.execute_long(n, 0, args) + c.execute_long(n, 1, args));
66     }
67     else if (m_type == CommandGene.DoubleClass) {
68       value = new Double JavaDoc(c.execute_double(n, 0, args) +
69                          c.execute_double(n, 1, args));
70     }
71     else if (m_type == CommandGene.FloatClass) {
72       value = new Float JavaDoc(c.execute_float(n, 0, args) +
73                         c.execute_float(n, 1, args));
74     }
75     else {
76       throw new RuntimeException JavaDoc("Type " + m_type +
77                                  " not supported by AddAndStore");
78     }
79     getGPConfiguration().storeInMemory(m_storageName, value);
80   }
81
82   public Class JavaDoc getChildType(IGPProgram a_ind, int a_chromNum) {
83     return m_type;
84   }
85
86   /**
87    * The compareTo-method.
88    *
89    * @param a_other the other object to compare
90    * @return -1, 0, 1
91    *
92    * @author Klaus Meffert
93    * @since 3.0
94    */

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

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