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 ReadTerminal 24 extends CommandGene { 25 26 private final static String CVS_REVISION = "$Revision: 1.10 $"; 27 28 32 private String m_storageName; 33 34 public ReadTerminal(final GPConfiguration a_conf, Class a_type, 35 String a_storageName) 36 throws InvalidConfigurationException { 37 this(a_conf, a_type, a_storageName, 0); 38 } 39 40 public ReadTerminal(final GPConfiguration a_conf, Class a_type, 41 String 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 ("Memory name must not be empty!"); 46 } 47 m_storageName = a_storageName; 48 } 49 50 public String toString() { 51 return "read_from(" + m_storageName + ")"; 52 } 53 54 60 public String getName() { 61 return "Read Terminal"; 62 } 63 64 public int execute_int(ProgramChromosome c, int n, Object [] args) { 65 check(c); 66 try { 69 return ( (Integer ) getGPConfiguration().readFromMemory( 70 m_storageName)).intValue(); 71 } catch (IllegalArgumentException iex) { 72 throw new IllegalStateException ( 73 "ReadTerminal without preceeding StoreTerminal"); 74 } 75 } 76 77 public long execute_long(ProgramChromosome c, int n, Object [] args) { 78 check(c); 79 try { 80 return ( (Long ) getGPConfiguration().readFromMemory( 81 m_storageName)).longValue(); 82 } catch (IllegalArgumentException iex) { 83 throw new IllegalStateException ( 84 "ReadTerminal without preceeding StoreTerminal"); 85 } 86 } 87 88 public double execute_double(ProgramChromosome c, int n, Object [] args) { 89 check(c); 90 try { 91 return ( (Double ) getGPConfiguration().readFromMemory( 92 m_storageName)).doubleValue(); 93 } catch (IllegalArgumentException iex) { 94 throw new IllegalStateException ( 95 "ReadTerminal without preceeding StoreTerminal"); 96 } 97 } 98 99 public float execute_float(ProgramChromosome c, int n, Object [] args) { 100 check(c); 101 try { 102 return ( (Float ) getGPConfiguration().readFromMemory( 103 m_storageName)).floatValue(); 104 } catch (IllegalArgumentException iex) { 105 throw new IllegalStateException ( 106 "ReadTerminal without preceeding StoreTerminal"); 107 } 108 } 109 110 public Object execute_object(ProgramChromosome c, int n, Object [] args) { 111 check(c); 112 try { 113 return getGPConfiguration().readFromMemory(m_storageName); 114 } catch (IllegalArgumentException iex) { 115 throw new IllegalStateException ( 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 134 public int compareTo(Object 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 155 public boolean equals(Object 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 cex) { 166 return false; 167 } 168 } 169 } 170 } 171 | Popular Tags |