1 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 23 public class Variable 24 extends CommandGene { 25 26 private static final String CVS_REVISION = "$Revision: 1.9 $"; 27 28 public static Hashtable vars = new Hashtable(); 29 30 33 private String m_name; 34 35 private Object m_value; 36 37 public Variable(final GPConfiguration a_conf, String a_varName, Class 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 toString() { 45 return m_name; 46 } 47 48 57 public String getName() { 58 return m_name; 59 } 60 61 public Class getChildType(IGPProgram a_ind, int a_chromNum) { 62 return null; 63 } 64 65 public boolean execute_boolean(ProgramChromosome c, int n, Object [] args) { 66 return ( (Boolean ) m_value).booleanValue(); 67 } 68 69 public int execute_int(ProgramChromosome c, int n, Object [] args) { 70 return ( (Integer ) m_value).intValue(); 71 } 72 73 public long execute_long(ProgramChromosome c, int n, Object [] args) { 74 return ( (Long ) m_value).longValue(); 75 } 76 77 public float execute_float(ProgramChromosome c, int n, Object [] args) { 78 return ( (Float ) m_value).floatValue(); 79 } 80 81 public double execute_double(ProgramChromosome c, int n, Object [] args) { 82 return ( (Double ) m_value).doubleValue(); 83 } 84 85 public Object execute_object(ProgramChromosome c, int n, Object [] args) { 86 return m_value; 87 } 88 89 98 public static Variable getVariable(String name) { 99 return (Variable) vars.get(name); 100 } 101 102 118 public static Variable create(GPConfiguration a_conf, String a_name, 119 Class 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 136 public void set(Object a_value) { 137 m_value = a_value; 138 } 139 140 public Object getValue() { 141 return m_value; 142 } 143 } 144 | Popular Tags |