📄 compositexgene.java.txt
字号:
* Sets the value of the contained Genes to the new given value. This class
* expects the value to be of a Vector type. Each element of the Vector
* must conform with the type of the gene in the CompositeGene's container
* at the corresponding position.
*
* @param a_newValue the new value of this Gene instance
*
* @author Klaus Meffert
* @since 1.1
*/
public void setAllele(Object a_newValue) {
if (! (a_newValue instanceof List)) {
throw new IllegalArgumentException(
"The expected type of the allele"
+ " is a List descendent.");
}
if (m_geneAlleleChecker != null) {
if (!m_geneAlleleChecker.verify(this, a_newValue)) {
return;
}
}
List alleles = (List) a_newValue;
Gene gene;
for (int i = 0; i < alleles.size(); i++) {
gene = (Gene) m_genes.get(i);
gene.setAllele(alleles.get(i));
}
}
/**
* Sets the constraint checker to be used for this gene whenever method
* setAllele(Object a_newValue) is called
* @param a_constraintChecker the constraint checker to be set
*
* @author Klaus Meffert
* @since 2.0
*/
public void setConstraintChecker(IGeneConstraintChecker a_constraintChecker) {
m_geneAlleleChecker = a_constraintChecker;
}
/**
* @return IGeneConstraintChecker the constraint checker to be used whenever
* method setAllele(Object a_newValue) is called
*
* @author Klaus Meffert
* @since 2.0
*/
public IGeneConstraintChecker getConstraintChecker() {
return m_geneAlleleChecker;
}
/**
* Provides an implementation-independent means for creating new Gene
* instances. The new instance that is created and returned should be
* setup with any implementation-dependent configuration that this Gene
* instance is setup with (aside from the actual value, of course). For
* example, if this Gene were setup with bounds on its value, then the
* Gene instance returned from this method should also be setup with
* those same bounds. This is important, as the JGAP core will invoke this
* method on each Gene in the sample Chromosome in order to create each
* new Gene in the same respective gene position for a new Chromosome.
* <p>
* It should be noted that nothing is guaranteed about the actual value
* of the returned Gene and it should therefore be considered to be
* undefined.
*
* @return A new Gene instance of the same type and with the same setup as
* this concrete Gene
*
* @author Klaus Meffert
* @since 1.1
*/
public Gene newGene() {
CompositeXGene compositeGene = new CompositeXGene();
Gene gene;
Integer key;
Iterator it = m_genes.keySet().iterator();
while (it.hasNext()) {
key = (Integer) it.next();
gene = (Gene) m_genes.get(key);
compositeGene.addGene(key, gene.newGene(), false);
}
return compositeGene;
}
/**
* Compares this CompositeGene with the specified object for order. A
* false value is considered to be less than a true value. A null value
* is considered to be less than any non-null value.
*
* @param a_other the CompositeGene to be compared
* @return a negative integer, zero, or a positive integer as this object
* is less than, equal to, or greater than the specified object
*
* @throws ClassCastException if the specified object's type prevents it
* from being compared to this CompositeGene
*
* @author Klaus Meffert
* @since 1.1
*/
public int compareTo(Object a_other) {
// First, if the other gene (or its value) is null, then this is
// the greater allele. Otherwise, just use the contained genes' compareTo
// method to perform the comparison.
// ---------------------------------------------------------------
if (a_other == null) {
return 1;
}
if (! (a_other instanceof CompositeXGene)) {
return this.getClass().getName().compareTo(a_other.getClass().getName());
}
CompositeXGene otherCompositeGene = (CompositeXGene) a_other;
if (otherCompositeGene.isEmpty()) {
// If our value is also null, then we're the same. Otherwise,
// this is the greater gene.
// ----------------------------------------------------------
return isEmpty() ? 0 : 1;
}
else {
// Compare each gene against each other.
// -------------------------------------
int numberGenes = Math.min(size(), otherCompositeGene.size());
Gene gene1;
Gene gene2;
Iterator it = m_genes.keySet().iterator();
while (it.hasNext()) {
Object key = it.next();
gene1 = getGene(key);
gene2 = otherCompositeGene.getGene(key);
if (gene1 == null) {
if (gene2 == null) {
continue;
}
else {
return -1;
}
}
else {
int result = gene1.compareTo(gene2);
if (result != 0) {
return result;
}
}
}
// If everything is equal until now the CompositeGene with more
// contained genes wins.
// ------------------------------------------------------------
if (size() == otherCompositeGene.size()) {
if (isCompareApplicationData()) {
return compareApplicationData(getApplicationData(),
otherCompositeGene.getApplicationData());
}
else {
return 0;
}
}
else {
return size() > otherCompositeGene.size() ? 1 : -1;
}
}
}
/**
* Retrieves a string representation of this CompositeGene's value that
* may be useful for display purposes.
* @return string representation of this CompositeGene's value. Every
* contained gene's string representation is delimited by the given
* delimiter
*
* @author Neil Rotstan
* @author Klaus Meffert
* @author Audrius Meskauskas
* @since 1.1
*/
public String toString() {
if (m_genes.isEmpty()) {
return "CompositeGene=null";
}
else {
String result = "CompositeGene=(";
Iterator it = m_genes.keySet().iterator();
int i=0;
while (it.hasNext()) {
if (i > 0) {
result += GENE_DELIMITER;
}
else {
i++;
}
Object key = it.next();
Gene gene = (Gene)m_genes.get(key);
result += gene;
}
return result + ")";
}
}
/**
* @return true: no genes contained, false otherwise
*
* @author Klaus Meffert
* @since 1.1
*/
public boolean isEmpty() {
return m_genes.isEmpty() ? true : false;
}
/**
* @return the number of genes contained
*
* @author Klaus Meffert
* @since 1.1
*/
public int size() {
return m_genes.size();
}
/**
* Checks whether a specific gene is already contained. The determination
* will be done by checking for identity and not using the equal method!
* @param gene the gene under test
* @return true: the given gene object is contained
*
* @author Klaus Meffert
* @since 1.1
*/
public boolean containsGeneByIdentity(Gene gene) {
return m_genes.containsValue(gene);
}
/**
* Don't use this method, is makes no sense here. It is just there to
* satisfy the Gene interface. Instead, loop over all contained genes and
* call their applyMutation method.
* @param a_index does not matter here
* @param a_percentage does not matter here
*
* @author Klaus Meffert
* @since 1.1
*/
public void applyMutation(int a_index, double a_percentage) {
// problem here: size() of CompositeGene not equal to (different)
// sizes of contained genes.
// Solution: Don't use CompositeGene.applyMutation, instead loop
// over all contained genes and call their method
// -------------------------------------------------------------
throw new RuntimeException("applyMutation may not be called for "
+ "a CompositeGene. Call this method for each"
+ " gene contained in the CompositeGene.");
}
/**
* Encode string, doubling the separators.
* @param a_string the string to encode (arbitrary characters)
* @return the encoded string, containing only characters, valid in URL's
*
* @author Audrius Meskauskas
* @since 2.0
*/
protected static final String encode(String a_string) {
try {
return URLEncoder.encode(a_string, "UTF-8");
}
catch (UnsupportedEncodingException ex) {
throw new Error("This should never happen!");
}
}
/** Decode string, undoubling the separators.
* @param a_encoded the URL-encoded string with restricted character set
* @return decoded the decoded string
*
* @author Audrius Meskauskas
* @since 2.0
*/
protected static final String decode(String a_encoded) {
try {
return URLDecoder.decode(a_encoded, "UTF-8");
}
catch (UnsupportedEncodingException ex) {
throw new Error("This should never happen!");
}
}
/**
* Splits the string a_x into individual gene representations
* @param a_string the string to split
* @return the elements of the returned array are the persistent
* representation strings of the genes - components
* @throws UnsupportedRepresentationException
*
* @author Audrius Meskauskas
* @since 2.0
*/
protected static final ArrayList split(String a_string)
throws UnsupportedRepresentationException {
ArrayList a = new ArrayList();
StringTokenizer st = new StringTokenizer
(a_string, GENE_DELIMITER_HEADING + GENE_DELIMITER_CLOSING, true);
while (st.hasMoreTokens()) {
if (!st.nextToken().equals(GENE_DELIMITER_HEADING)) {
throw new UnsupportedRepresentationException(a_string + " no open tag");
}
String n = st.nextToken();
if (n.equals(GENE_DELIMITER_CLOSING)) {
a.add(""); /* Empty token */
}
else {
a.add(n);
if (!st.nextToken().equals(GENE_DELIMITER_CLOSING))
throw new UnsupportedRepresentationException
(a_string + " no close tag");
}
}
return a;
}
/**
* Retrieves the hash code value for this Gene.
*
* @return this Gene's hash code
*
* @author Klaus Meffert
* @since ":"
*/
public int hashCode() {
int hashCode = 1;
int geneHashcode;
Iterator it = m_genes.values().iterator();
while (it.hasNext()) {
Gene gene = (Gene)it.next();
geneHashcode = gene.hashCode();
hashCode = 31 * hashCode + geneHashcode;
}
return hashCode;
}
protected Object getInternalValue() {
return null;
}
public Gene getGene(Object a_label) {
return (Gene)m_genes.get(a_label);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -