📄 compositegene.java
字号:
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) 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() {
CompositeGene compositeGene = new CompositeGene();
Gene gene;
int geneSize = genes.size();
for (int i = 0; i < geneSize; i++) {
gene = (Gene) genes.get(i);
compositeGene.addGene(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 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 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 (other == null) {
return 1;
}
if (! (other instanceof CompositeGene)) {
return this.getClass().getName().compareTo(other.getClass().getName());
}
CompositeGene otherCompositeGene = (CompositeGene) 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;
for (int i = 0; i < numberGenes; i++) {
gene1 = geneAt(i);
gene2 = otherCompositeGene.geneAt(i);
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()) {
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 a 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 (genes.isEmpty()) {
return "null";
}
else {
String result = "";
Gene gene;
for (int i = 0; i < genes.size(); i++) {
gene = (Gene) genes.get(i);
result += gene;
if (i < genes.size() - 1) {
result += GENE_DELIMITER;
}
}
// (..) helps to see the nested composite genes
return "(" + result + ")";
}
}
/**
* @return true: no genes contained, false otherwise
*
* @author Klaus Meffert
* @since 1.1
*/
public boolean isEmpty() {
return genes.isEmpty() ? true : false;
}
/**
* Returns the gene at the given index
* @param index sic
* @return the gene at the given index
*
* @author Klaus Meffert
* @since 1.1
*/
public Gene geneAt(int index) {
return (Gene) genes.get(index);
}
/**
* @return the number of genes contained
*
* @author Klaus Meffert
* @since 1.1
*/
public int size() {
return 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) {
boolean result;
int size = size();
if (size < 1) {
result = false;
}
else {
result = false;
for (int i = 0; i < size; i++) {
//check for identity
//------------------
if (geneAt(i) == gene) {
result = true;
break;
}
}
}
return result;
}
/**
* Don't use this method, is makes no sense here. It is just there to
* satisfy the Gene interface.
* Instead, loop over all cotnained genes and call their applyMutation
* method.
* @param index does not matter here
* @param a_percentage does not matter here
*
* @author Klaus Meffert
* @since 1.1
*/
public void applyMutation(int index, double a_percentage) {
for (int i = 0; i < size(); i++) {
// 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 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 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;
for (int i = 0; i < size(); i++) {
geneHashcode = geneAt(i).hashCode();
hashCode = 31 * hashCode + geneHashcode;
}
return hashCode;
}
protected Object getInternalValue() {
return null;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -