KickJava   Java API By Example, From Geeks To Geeks.

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


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

23 public class ReadTerminal
24     extends CommandGene {
25   /** String containing the CVS revision. Read out via reflection!*/
26   private final static String JavaDoc CVS_REVISION = "$Revision: 1.10 $";
27
28   /**
29    * Symbolic name of the storage. Must correspond with a chosen name for
30    * an other command working with memory (otherwise it would make no sense).
31    */

32   private String JavaDoc m_storageName;
33
34   public ReadTerminal(final GPConfiguration a_conf, Class JavaDoc a_type,
35                       String JavaDoc a_storageName)
36       throws InvalidConfigurationException {
37     this(a_conf, a_type, a_storageName, 0);
38   }
39
40   public ReadTerminal(final GPConfiguration a_conf, Class JavaDoc a_type,
41                       String JavaDoc a_storageName, int a_subReturnType)
42       throws InvalidConfigurationException {
43     super(a_conf, 0, a_type, a_subReturnType, null);
44     if (a_storageName == null || a_storageName.length() < 1) {
45       throw new IllegalArgumentException JavaDoc("Memory name must not be empty!");
46     }
47     m_storageName = a_storageName;
48   }
49
50   public String JavaDoc toString() {
51     return "read_from(" + m_storageName + ")";
52   }
53
54   /**
55    * @return textual name of this command
56    *
57    * @author Klaus Meffert
58    * @since 3.2
59    */

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

134   public int compareTo(Object JavaDoc a_other) {
135     if (a_other == null) {
136       return 1;
137     }
138     else {
139       ReadTerminal other = (ReadTerminal) a_other;
140       return new CompareToBuilder()
141           .append(m_storageName, other.m_storageName)
142           .toComparison();
143     }
144   }
145
146   /**
147    * The equals-method.
148    *
149    * @param a_other the other object to compare
150    * @return true if the objects are seen as equal
151    *
152    * @author Klaus Meffert
153    * @since 3.0
154    */

155   public boolean equals(Object JavaDoc a_other) {
156     if (a_other == null) {
157       return false;
158     }
159     else {
160       try {
161         ReadTerminal other = (ReadTerminal) a_other;
162         return new EqualsBuilder()
163             .append(m_storageName, other.m_storageName)
164             .isEquals();
165       } catch (ClassCastException JavaDoc cex) {
166         return false;
167       }
168     }
169   }
170 }
171
Popular Tags