KickJava   Java API By Example, From Geeks To Geeks.

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


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.apache.commons.lang.builder.*;
13 import org.jgap.*;
14 import org.jgap.gp.*;
15 import org.jgap.gp.impl.*;
16
17 /**
18  * Reads a value from the internal indexed memory.
19  *
20  * @author Klaus Meffert
21  * @since 3.2
22  */

23 public class ReadTerminalIndexed
24     extends CommandGene {
25   /** String containing the CVS revision. Read out via reflection!*/
26   private final static String JavaDoc CVS_REVISION = "$Revision: 1.1 $";
27
28   /**
29    * Index of the memory cell to read.
30    */

31   private int m_index;
32
33   public ReadTerminalIndexed(final GPConfiguration a_conf, Class JavaDoc a_type,
34                              int a_index)
35       throws InvalidConfigurationException {
36     this(a_conf, a_type, a_index, 0);
37   }
38
39   public ReadTerminalIndexed(final GPConfiguration a_conf, Class JavaDoc a_type,
40                              int a_index, int a_subReturnType)
41       throws InvalidConfigurationException {
42     super(a_conf, 0, a_type, a_subReturnType, null);
43     if (a_index < 0 || a_index > getGPConfiguration().getMemorySize()) {
44       throw new IllegalArgumentException JavaDoc("Memory index invalid!");
45     }
46     m_index = a_index;
47   }
48
49   public String JavaDoc toString() {
50     return "read_from_index(" + m_index + ")";
51   }
52
53   /**
54    * @return textual name of this command
55    *
56    * @author Klaus Meffert
57    * @since 3.2
58    */

59   public String JavaDoc getName() {
60     return "Read Terminal Indexed";
61   }
62
63   public int execute_int(ProgramChromosome c, int n, Object JavaDoc[] args) {
64     check(c);
65     // Read from memory.
66
// -----------------
67
try {
68       try {
69         return ( (Integer JavaDoc) getGPConfiguration().readIndexedMemory(m_index)).
70             intValue();
71       } catch (NullPointerException JavaDoc nex) {
72         throw new IllegalArgumentException JavaDoc();
73       }
74     } catch (IllegalArgumentException JavaDoc iex) {
75       throw new IllegalStateException JavaDoc(
76           "ReadTerminalIndexed without preceeding StoreTerminalIndexed");
77     }
78   }
79
80   public long execute_long(ProgramChromosome c, int n, Object JavaDoc[] args) {
81     check(c);
82     try {
83       try {
84         return ( (Long JavaDoc) getGPConfiguration().readIndexedMemory(m_index)).
85             longValue();
86       } catch (NullPointerException JavaDoc nex) {
87         throw new IllegalArgumentException JavaDoc();
88       }
89     } catch (IllegalArgumentException JavaDoc iex) {
90       throw new IllegalStateException JavaDoc(
91           "ReadTerminalIndexed without preceeding StoreTerminalIndexed");
92     }
93   }
94
95   public double execute_double(ProgramChromosome c, int n, Object JavaDoc[] args) {
96     check(c);
97     try {
98       try {
99         return ( (Double JavaDoc) getGPConfiguration().readIndexedMemory(m_index)).
100             doubleValue();
101       } catch (NullPointerException JavaDoc nex) {
102         throw new IllegalArgumentException JavaDoc();
103       }
104     } catch (IllegalArgumentException JavaDoc iex) {
105       throw new IllegalStateException JavaDoc(
106           "ReadTerminalIndexed without preceeding StoreTerminalIndexed");
107     }
108   }
109
110   public float execute_float(ProgramChromosome c, int n, Object JavaDoc[] args) {
111     check(c);
112     try {
113       try {
114         return ( (Float JavaDoc) getGPConfiguration().readIndexedMemory(m_index)).
115             floatValue();
116       } catch (NullPointerException JavaDoc nex) {
117         throw new IllegalArgumentException JavaDoc();
118       }
119     } catch (IllegalArgumentException JavaDoc iex) {
120       throw new IllegalStateException JavaDoc(
121           "ReadTerminalIndexed without preceeding StoreTerminalIndexed");
122     }
123   }
124
125   public Object JavaDoc execute_object(ProgramChromosome c, int n, Object JavaDoc[] args) {
126     check(c);
127     try {
128       try {
129         return getGPConfiguration().readIndexedMemory(m_index);
130       } catch (NullPointerException JavaDoc nex) {
131         throw new IllegalArgumentException JavaDoc();
132       }
133     } catch (IllegalArgumentException JavaDoc iex) {
134       throw new IllegalStateException JavaDoc(
135           "ReadTerminalIndexed without preceeding StoreTerminalIndexed");
136     }
137   }
138
139   /**
140    * The compareTo-method.
141    *
142    * @param a_other the other object to compare
143    * @return -1, 0, 1
144    *
145    * @author Klaus Meffert
146    * @since 3.0
147    */

148   public int compareTo(Object JavaDoc a_other) {
149     if (a_other == null) {
150       return 1;
151     }
152     else {
153       ReadTerminalIndexed other = (ReadTerminalIndexed) a_other;
154       return new CompareToBuilder()
155           .append(m_index, other.m_index)
156           .toComparison();
157     }
158   }
159
160   /**
161    * The equals-method.
162    *
163    * @param a_other the other object to compare
164    * @return true if the objects are seen as equal
165    *
166    * @author Klaus Meffert
167    * @since 3.0
168    */

169   public boolean equals(Object JavaDoc a_other) {
170     if (a_other == null) {
171       return false;
172     }
173     else {
174       try {
175         ReadTerminalIndexed other = (ReadTerminalIndexed) a_other;
176         return new EqualsBuilder()
177             .append(m_index, other.m_index)
178             .isEquals();
179       } catch (ClassCastException JavaDoc cex) {
180         return false;
181       }
182     }
183   }
184 }
185
Popular Tags