KickJava   Java API By Example, From Geeks To Geeks.

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


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  * Transfers a memory value to another memory cell.
19  *
20  * @author Klaus Meffert
21  * @since 3.0
22  */

23 public class TransferMemory
24     extends CommandGene {
25   /** String containing the CVS revision. Read out via reflection!*/
26   private final static String JavaDoc CVS_REVISION = "$Revision: 1.9 $";
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 TransferMemory(final GPConfiguration a_conf,
37                         String JavaDoc a_sourceStorageName,
38                         String JavaDoc 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 JavaDoc(
43           "Source memory name must not be empty!");
44     }
45     if (a_targetStorageName == null || a_targetStorageName.length() < 1) {
46       throw new IllegalArgumentException JavaDoc(
47           "Target memory name must not be empty!");
48     }
49     if (a_sourceStorageName.equals(a_targetStorageName)) {
50       throw new IllegalArgumentException JavaDoc(
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 JavaDoc toString() {
58     return "transfer("
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 "Transfer 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 value = getGPConfiguration().readFromMemory(m_sourceStorageName);
81       // Store in memory.
82
// ----------------
83
getGPConfiguration().storeInMemory(m_targetStorageName, value);
84     } catch (IllegalArgumentException JavaDoc iex) {
85       throw new IllegalStateException JavaDoc(
86           "TransferMemory without preceeding StoreTerminal");
87     }
88   }
89
90   public boolean isAffectGlobalState() {
91     /**@todo subclass from new abstract class MemoryCommand?*/
92     return true;
93   }
94
95   public boolean isValid(ProgramChromosome a_program) {
96     return true;
97   }
98
99   public Class JavaDoc getChildType(IGPProgram a_ind, int a_chromNum) {
100     return null;
101   }
102
103   /**
104    * The compareTo-method.
105    *
106    * @param a_other the other object to compare
107    * @return -1, 0, 1
108    *
109    * @author Klaus Meffert
110    * @since 3.0
111    */

112   public int compareTo(Object JavaDoc 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   /**
126    * The equals-method.
127    *
128    * @param a_other the other object to compare
129    * @return true if the objects are seen as equal
130    *
131    * @author Klaus Meffert
132    * @since 3.0
133    */

134   public boolean equals(Object JavaDoc 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 JavaDoc cex) {
146         return false;
147       }
148     }
149   }
150 }
151
Popular Tags