1 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 23 public class ReadTerminalIndexed 24 extends CommandGene { 25 26 private final static String CVS_REVISION = "$Revision: 1.1 $"; 27 28 31 private int m_index; 32 33 public ReadTerminalIndexed(final GPConfiguration a_conf, Class 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 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 ("Memory index invalid!"); 45 } 46 m_index = a_index; 47 } 48 49 public String toString() { 50 return "read_from_index(" + m_index + ")"; 51 } 52 53 59 public String getName() { 60 return "Read Terminal Indexed"; 61 } 62 63 public int execute_int(ProgramChromosome c, int n, Object [] args) { 64 check(c); 65 try { 68 try { 69 return ( (Integer ) getGPConfiguration().readIndexedMemory(m_index)). 70 intValue(); 71 } catch (NullPointerException nex) { 72 throw new IllegalArgumentException (); 73 } 74 } catch (IllegalArgumentException iex) { 75 throw new IllegalStateException ( 76 "ReadTerminalIndexed without preceeding StoreTerminalIndexed"); 77 } 78 } 79 80 public long execute_long(ProgramChromosome c, int n, Object [] args) { 81 check(c); 82 try { 83 try { 84 return ( (Long ) getGPConfiguration().readIndexedMemory(m_index)). 85 longValue(); 86 } catch (NullPointerException nex) { 87 throw new IllegalArgumentException (); 88 } 89 } catch (IllegalArgumentException iex) { 90 throw new IllegalStateException ( 91 "ReadTerminalIndexed without preceeding StoreTerminalIndexed"); 92 } 93 } 94 95 public double execute_double(ProgramChromosome c, int n, Object [] args) { 96 check(c); 97 try { 98 try { 99 return ( (Double ) getGPConfiguration().readIndexedMemory(m_index)). 100 doubleValue(); 101 } catch (NullPointerException nex) { 102 throw new IllegalArgumentException (); 103 } 104 } catch (IllegalArgumentException iex) { 105 throw new IllegalStateException ( 106 "ReadTerminalIndexed without preceeding StoreTerminalIndexed"); 107 } 108 } 109 110 public float execute_float(ProgramChromosome c, int n, Object [] args) { 111 check(c); 112 try { 113 try { 114 return ( (Float ) getGPConfiguration().readIndexedMemory(m_index)). 115 floatValue(); 116 } catch (NullPointerException nex) { 117 throw new IllegalArgumentException (); 118 } 119 } catch (IllegalArgumentException iex) { 120 throw new IllegalStateException ( 121 "ReadTerminalIndexed without preceeding StoreTerminalIndexed"); 122 } 123 } 124 125 public Object execute_object(ProgramChromosome c, int n, Object [] args) { 126 check(c); 127 try { 128 try { 129 return getGPConfiguration().readIndexedMemory(m_index); 130 } catch (NullPointerException nex) { 131 throw new IllegalArgumentException (); 132 } 133 } catch (IllegalArgumentException iex) { 134 throw new IllegalStateException ( 135 "ReadTerminalIndexed without preceeding StoreTerminalIndexed"); 136 } 137 } 138 139 148 public int compareTo(Object 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 169 public boolean equals(Object 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 cex) { 180 return false; 181 } 182 } 183 } 184 } 185 | Popular Tags |