KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgap > gp > terminal > Variable


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.terminal;
11
12 import java.util.*;
13 import org.jgap.*;
14 import org.jgap.gp.*;
15 import org.jgap.gp.impl.*;
16
17 /**
18  * A terminal represented by a variable (x,y,z...).
19  *
20  * @author Klaus Meffert
21  * @since 3.0
22  */

23 public class Variable
24     extends CommandGene {
25   /** String containing the CVS revision. Read out via reflection!*/
26   private static final String JavaDoc CVS_REVISION = "$Revision: 1.9 $";
27
28   public static Hashtable vars = new Hashtable();
29
30   /**
31    * Unique name of the variable.
32    */

33   private String JavaDoc m_name;
34
35   private Object JavaDoc m_value;
36
37   public Variable(final GPConfiguration a_conf, String JavaDoc a_varName, Class JavaDoc type)
38       throws InvalidConfigurationException {
39     super(a_conf, 0, type);
40     m_name = a_varName;
41     vars.put(a_varName, this);
42   }
43
44   public String JavaDoc toString() {
45     return m_name;
46   }
47
48   /**
49    * Attention: It is important to return m_name here (see
50    * GPGenotype.putVariable).
51    *
52    * @return textual name of this command
53    *
54    * @author Klaus Meffert
55    * @since 3.2
56    */

57   public String JavaDoc getName() {
58     return m_name;
59   }
60
61   public Class JavaDoc getChildType(IGPProgram a_ind, int a_chromNum) {
62     return null;
63   }
64
65   public boolean execute_boolean(ProgramChromosome c, int n, Object JavaDoc[] args) {
66     return ( (Boolean JavaDoc) m_value).booleanValue();
67   }
68
69   public int execute_int(ProgramChromosome c, int n, Object JavaDoc[] args) {
70     return ( (Integer JavaDoc) m_value).intValue();
71   }
72
73   public long execute_long(ProgramChromosome c, int n, Object JavaDoc[] args) {
74     return ( (Long JavaDoc) m_value).longValue();
75   }
76
77   public float execute_float(ProgramChromosome c, int n, Object JavaDoc[] args) {
78     return ( (Float JavaDoc) m_value).floatValue();
79   }
80
81   public double execute_double(ProgramChromosome c, int n, Object JavaDoc[] args) {
82     return ( (Double JavaDoc) m_value).doubleValue();
83   }
84
85   public Object JavaDoc execute_object(ProgramChromosome c, int n, Object JavaDoc[] args) {
86     return m_value;
87   }
88
89   /**
90    * Gets the one instance of a named variable.
91    *
92    * @param name the name of the variable to get
93    * @return the named variable, or null if that name wasn't found.
94    *
95    * @author Klaus Meffert
96    * @since 3.0
97    */

98   public static Variable getVariable(String JavaDoc name) {
99     return (Variable) vars.get(name);
100   }
101
102   /**
103    * Creates an instance of a Variable.
104    * If a Variable of that name already exists, that is returned.
105    * Otherwise a new instance is created, its value is initialized to null, and
106    * it is placed into the static hashtable for later retrieval by name via
107    * getVariable.
108    *
109    * @param a_conf the configuration to use
110    * @param a_name the name of the Variable to create
111    * @param a_type the type of the Variable to create
112    * @return the variable object created
113    * @throws InvalidConfigurationException
114    *
115    * @author Klaus Meffert
116    * @since 3.0
117    */

118   public static Variable create(GPConfiguration a_conf, String JavaDoc a_name,
119                                 Class JavaDoc a_type)
120       throws InvalidConfigurationException {
121     Variable var;
122     if ( (var = getVariable(a_name)) != null) {
123       return var;
124     }
125     return new Variable(a_conf, a_name, a_type);
126   }
127
128   /**
129    * Sets the value of this named variable.
130    *
131    * @param a_value the value to set this variable with
132    *
133    * @author Klaus Meffert
134    * @since 3.0
135    */

136   public void set(Object JavaDoc a_value) {
137     m_value = a_value;
138   }
139
140   public Object JavaDoc getValue() {
141     return m_value;
142   }
143 }
144
Popular Tags