KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgap > gp > function > ExchangeMemory


1 /*
2  * This file is part of JGAP.
3  *
4  * JGAP offers a dual license model containing the LGPL as well as the MPL.
5  *
6  * For licencing information please see the file license.txt included with JGAP
7  * or have a look at the top of class org.jgap.Chromosome which representatively
8  * includes the JGAP license policy applicable for any file delivered with JGAP.
9  */

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 /**
18  * Exchanges the values of two memory cells.
19  *
20  * @author Klaus Meffert
21  * @since 3.2
22  */

23 public class ExchangeMemory
24     extends CommandGene {
25   /** String containing the CVS revision. Read out via reflection!*/
26   private final static String JavaDoc CVS_REVISION = "$Revision: 1.2 $";
27
28   /**
29    * Symbolic name of the storage. Must correspond with a chosen name for
30    * ReadTerminalCommand.
31    */

32   private String JavaDoc m_sourceStorageName;
33
34   private String JavaDoc m_targetStorageName;
35
36   public ExchangeMemory(final GPConfiguration a_conf,
37                         String JavaDoc a_firstStorageName,
38                         String JavaDoc 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 JavaDoc(
43           "First memory name must not be empty!");
44     }
45     if (a_secondStorageName == null || a_secondStorageName.length() < 1) {
46       throw new IllegalArgumentException JavaDoc(
47           "Second memory name must not be empty!");
48     }
49     if (a_firstStorageName.equals(a_secondStorageName)) {
50       throw new IllegalArgumentException JavaDoc(
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 JavaDoc toString() {
58     return "exchange("
59         + m_sourceStorageName
60         + ", "
61         + m_targetStorageName
62         + ")";
63   }
64
65   /**
66    * @return textual name of this command
67    *
68    * @author Klaus Meffert
69    * @since 3.2
70    */

71   public String JavaDoc getName() {
72     return "Exchange Memory";
73   }
74
75   public void execute_void(ProgramChromosome c, int n, Object JavaDoc[] args) {
76     check(c);
77     // Read from memory.
78
// -----------------
79
try {
80       Object JavaDoc value1 = getGPConfiguration().readFromMemory(
81           m_sourceStorageName);
82       Object JavaDoc value2 = getGPConfiguration().readFromMemory(
83           m_targetStorageName);
84       // Store in memory.
85
// ----------------
86
getGPConfiguration().storeInMemory(m_sourceStorageName, value2);
87       getGPConfiguration().storeInMemory(m_targetStorageName, value1);
88     } catch (IllegalArgumentException JavaDoc iex) {
89       throw new IllegalStateException JavaDoc(
90           "ExchangeMemory without preceeding StoreTerminal");
91     }
92   }
93
94   public boolean isAffectGlobalState() {
95     /**@todo subclass from new abstract class MemoryCommand?*/
96     return true;
97   }
98
99   public boolean isValid(ProgramChromosome a_program) {
100     return true;
101   }
102
103   public Class JavaDoc getChildType(IGPProgram a_ind, int a_chromNum) {
104     return null;
105   }
106
107   /**
108    * The compareTo-method.
109    *
110    * @param a_other the other object to compare
111    * @return -1, 0, 1
112    *
113    * @author Klaus Meffert
114    * @since 3.2
115    */

116   public int compareTo(Object JavaDoc 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   /**
130    * The equals-method.
131    *
132    * @param a_other the other object to compare
133    * @return true if the objects are seen as equal
134    *
135    * @author Klaus Meffert
136    * @since 3.2
137    */

138   public boolean equals(Object JavaDoc 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 JavaDoc cex) {
150         return false;
151       }
152     }
153   }
154 }
155
Popular Tags