1 10 package org.jgap.gp.function; 11 12 import org.jgap.*; 13 import org.jgap.gp.*; 14 import org.apache.commons.lang.builder.*; 15 import org.jgap.gp.impl.*; 16 17 23 public class TransferMemory 24 extends CommandGene { 25 26 private final static String CVS_REVISION = "$Revision: 1.9 $"; 27 28 32 private String m_sourceStorageName; 33 34 private String m_targetStorageName; 35 36 public TransferMemory(final GPConfiguration a_conf, 37 String a_sourceStorageName, 38 String a_targetStorageName) 39 throws InvalidConfigurationException { 40 super(a_conf, 0, CommandGene.VoidClass); 41 if (a_sourceStorageName == null || a_sourceStorageName.length() < 1) { 42 throw new IllegalArgumentException ( 43 "Source memory name must not be empty!"); 44 } 45 if (a_targetStorageName == null || a_targetStorageName.length() < 1) { 46 throw new IllegalArgumentException ( 47 "Target memory name must not be empty!"); 48 } 49 if (a_sourceStorageName.equals(a_targetStorageName)) { 50 throw new IllegalArgumentException ( 51 "Source and target memory name must be different!"); 52 } 53 m_sourceStorageName = a_sourceStorageName; 54 m_targetStorageName = a_targetStorageName; 55 } 56 57 public String toString() { 58 return "transfer(" 59 + m_sourceStorageName 60 + " -> " 61 + m_targetStorageName 62 + ")"; 63 } 64 65 71 public String getName() { 72 return "Transfer Memory"; 73 } 74 75 public void execute_void(ProgramChromosome c, int n, Object [] args) { 76 check(c); 77 try { 80 Object value = getGPConfiguration().readFromMemory(m_sourceStorageName); 81 getGPConfiguration().storeInMemory(m_targetStorageName, value); 84 } catch (IllegalArgumentException iex) { 85 throw new IllegalStateException ( 86 "TransferMemory without preceeding StoreTerminal"); 87 } 88 } 89 90 public boolean isAffectGlobalState() { 91 92 return true; 93 } 94 95 public boolean isValid(ProgramChromosome a_program) { 96 return true; 97 } 98 99 public Class getChildType(IGPProgram a_ind, int a_chromNum) { 100 return null; 101 } 102 103 112 public int compareTo(Object a_other) { 113 if (a_other == null) { 114 return 1; 115 } 116 else { 117 TransferMemory other = (TransferMemory) a_other; 118 return new CompareToBuilder() 119 .append(m_sourceStorageName, other.m_sourceStorageName) 120 .append(m_targetStorageName, other.m_targetStorageName) 121 .toComparison(); 122 } 123 } 124 125 134 public boolean equals(Object a_other) { 135 if (a_other == null) { 136 return false; 137 } 138 else { 139 try { 140 TransferMemory other = (TransferMemory) a_other; 141 return new EqualsBuilder() 142 .append(m_sourceStorageName, other.m_sourceStorageName) 143 .append(m_targetStorageName, other.m_targetStorageName) 144 .isEquals(); 145 } catch (ClassCastException cex) { 146 return false; 147 } 148 } 149 } 150 } 151 | Popular Tags |