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 ExchangeMemory 24 extends CommandGene { 25 26 private final static String CVS_REVISION = "$Revision: 1.2 $"; 27 28 32 private String m_sourceStorageName; 33 34 private String m_targetStorageName; 35 36 public ExchangeMemory(final GPConfiguration a_conf, 37 String a_firstStorageName, 38 String a_secondStorageName) 39 throws InvalidConfigurationException { 40 super(a_conf, 0, CommandGene.VoidClass); 41 if (a_firstStorageName == null || a_firstStorageName.length() < 1) { 42 throw new IllegalArgumentException ( 43 "First memory name must not be empty!"); 44 } 45 if (a_secondStorageName == null || a_secondStorageName.length() < 1) { 46 throw new IllegalArgumentException ( 47 "Second memory name must not be empty!"); 48 } 49 if (a_firstStorageName.equals(a_secondStorageName)) { 50 throw new IllegalArgumentException ( 51 "First and second memory name must be different!"); 52 } 53 m_sourceStorageName = a_firstStorageName; 54 m_targetStorageName = a_secondStorageName; 55 } 56 57 public String toString() { 58 return "exchange(" 59 + m_sourceStorageName 60 + ", " 61 + m_targetStorageName 62 + ")"; 63 } 64 65 71 public String getName() { 72 return "Exchange Memory"; 73 } 74 75 public void execute_void(ProgramChromosome c, int n, Object [] args) { 76 check(c); 77 try { 80 Object value1 = getGPConfiguration().readFromMemory( 81 m_sourceStorageName); 82 Object value2 = getGPConfiguration().readFromMemory( 83 m_targetStorageName); 84 getGPConfiguration().storeInMemory(m_sourceStorageName, value2); 87 getGPConfiguration().storeInMemory(m_targetStorageName, value1); 88 } catch (IllegalArgumentException iex) { 89 throw new IllegalStateException ( 90 "ExchangeMemory without preceeding StoreTerminal"); 91 } 92 } 93 94 public boolean isAffectGlobalState() { 95 96 return true; 97 } 98 99 public boolean isValid(ProgramChromosome a_program) { 100 return true; 101 } 102 103 public Class getChildType(IGPProgram a_ind, int a_chromNum) { 104 return null; 105 } 106 107 116 public int compareTo(Object a_other) { 117 if (a_other == null) { 118 return 1; 119 } 120 else { 121 ExchangeMemory other = (ExchangeMemory) a_other; 122 return new CompareToBuilder() 123 .append(m_sourceStorageName, other.m_sourceStorageName) 124 .append(m_targetStorageName, other.m_targetStorageName) 125 .toComparison(); 126 } 127 } 128 129 138 public boolean equals(Object a_other) { 139 if (a_other == null) { 140 return false; 141 } 142 else { 143 try { 144 ExchangeMemory other = (ExchangeMemory) a_other; 145 return new EqualsBuilder() 146 .append(m_sourceStorageName, other.m_sourceStorageName) 147 .append(m_targetStorageName, other.m_targetStorageName) 148 .isEquals(); 149 } catch (ClassCastException cex) { 150 return false; 151 } 152 } 153 } 154 } 155 | Popular Tags |