KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgap > impl > CompositeGenePersistentReprTest


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.impl;
11
12 import java.util.*;
13
14 import org.jgap.*;
15
16 import junit.framework.*;
17
18 /**
19  * Test persistent representation of the CompositeGene.
20  *
21  * @author Audrius Meskauskas
22  * @author Klaus Meffert
23  * @since 2.1
24  */

25 public class CompositeGenePersistentReprTest extends JGAPTestCase {
26
27   /** String containing the CVS revision. Read out via reflection!*/
28   private final static String JavaDoc CVS_REVISION = "$Revision: 1.2 $";
29
30   public static Test suite() {
31     TestSuite suite = new TestSuite(CompositeGenePersistentReprTest.class);
32     return suite;
33   }
34
35   public void testRepresentation() throws Exception JavaDoc {
36       CompositeGene gene = createSampleNestedGene(5);
37       String JavaDoc representation = gene.getPersistentRepresentation();
38 // System.out.println("Old representation: " + representation);
39
CompositeGene restored = new CompositeGene(conf);
40       restored.setValueFromPersistentRepresentation(representation);
41 // System.out.println("New representation: "
42
// + restored.getPersistentRepresentation());
43
Object JavaDoc allele = gene.getAllele();
44       Gene other = createSampleNestedGene(55);
45       Gene nGene = other.newGene();
46       nGene.setAllele(allele);
47       assertEquals(restored, gene);
48   }
49
50   private CompositeGene createSampleNestedGene(int a_seed) throws Exception JavaDoc {
51     CompositeGene gene = new CompositeGene(conf);
52     Gene i1 = new IntegerGene(conf);
53     Gene i2 = new DoubleGene(conf);
54     i1.setAllele(new Integer JavaDoc(a_seed));
55     i2.setAllele(new Double JavaDoc(a_seed + 0.1));
56     gene.addGene(i1);
57     gene.addGene(i2);
58     CompositeGene nested = new CompositeGene(conf);
59     Gene n1 = new IntegerGene(conf);
60     Gene n2 = new DoubleGene(conf);
61     n1.setAllele(new Integer JavaDoc(10 + a_seed));
62     n2.setAllele(new Double JavaDoc(1));
63     nested.addGene(n1);
64     nested.addGene(n2);
65     gene.addGene(nested);
66     CompositeGene nested2 = new CompositeGene(conf);
67     Gene nn1 = new IntegerGene(conf, 1, 1000);
68     Gene nn2 = new DoubleGene(conf, 0, 1000);
69     Gene nn3 = new StringGene(conf, 1, 10,
70                               StringGene.ALPHABET_CHARACTERS_UPPER
71                               + CompositeGene.GENE_DELIMITER);
72     nn1.setAllele(new Integer JavaDoc(22 + a_seed));
73     nn2.setAllele(new Double JavaDoc(44 + a_seed));
74     nn3.setAllele("ABCCBA" + CompositeGene.GENE_DELIMITER);
75     nested2.addGene(nn1);
76     nested2.addGene(nn2);
77     nested2.addGene(nn3);
78     gene.addGene(nested2);
79     CompositeGene nested3 = new CompositeGene(conf);
80     Gene nnn1 = new IntegerGene(conf, 1, 1000);
81     Gene nnn2 = new DoubleGene(conf, 0, 1000);
82     nnn1.setAllele(new Integer JavaDoc(555 + a_seed));
83     nnn2.setAllele(new Double JavaDoc(777 + a_seed));
84     nested3.addGene(nnn1);
85     nested3.addGene(nnn2);
86     nested2.addGene(nested3);
87     return gene;
88   }
89
90 }
91
Popular Tags