KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgap > data > DataTreeBuilderTest


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.data;
11
12 import org.jgap.*;
13 import org.jgap.impl.*;
14 import junit.framework.*;
15
16 /**
17  * Tests the DataTreeBuilder class
18  *
19  * @author Klaus Meffert
20  * @author Siddhartha Azad
21  * @since 1.0
22  */

23 public class DataTreeBuilderTest
24     extends JGAPTestCase {
25   /** String containing the CVS revision. Read out via reflection!*/
26   private final static String JavaDoc CVS_REVISION = "$Revision: 1.11 $";
27
28   // number of chromosomes used in test case
29
private final static int NUM_CHROMS = 5;
30
31   // number of genes used in test case
32
private final static int NUM_GENES = 2;
33
34   public static Test suite() {
35     TestSuite suite = new TestSuite(DataTreeBuilderTest.class);
36     return suite;
37   }
38
39   public void setUp() {
40     super.setUp();
41     Configuration.reset();
42   }
43
44   /**
45    * Create a Genotype and represent it as a document, and verify that the
46    * representation is as expected.
47    * @throws Exception
48    */

49   public void testRepresentGenotypeAsDocument_0()
50       throws Exception JavaDoc {
51     // configuration setup
52
Configuration conf = new DefaultConfiguration();
53     conf.setFitnessFunction(new StaticFitnessFunction(5));
54     conf.setPopulationSize(NUM_CHROMS);
55     conf.setSampleChromosome(new Chromosome(conf, new Gene[] {
56                                             new IntegerGene(conf, 1, 5),
57                                             new IntegerGene(conf, 1, 3)}));
58     //Create a Genotype with a population of NUM_CHROMS Chromosomes, each
59
//Chromosome with NUM_GENES Genes.
60
Chromosome[] chroms = new Chromosome[NUM_CHROMS];
61     for (int i = 0; i < NUM_CHROMS; i++) {
62       chroms[i] = new Chromosome(conf, new Gene[] {
63                                  new IntegerGene(conf, 1, 5),
64                                  new IntegerGene(conf, 1, 10)});
65       chroms[i].getGene(0).setAllele(new Integer JavaDoc(i + 1));
66       chroms[i].getGene(1).setAllele(new Integer JavaDoc(i + 1));
67     }
68     Population popul = new Population(conf, chroms);
69     Genotype genotype = new Genotype(conf, popul);
70     // write the genotype as a document
71
IDataCreators doc = DataTreeBuilder.getInstance().
72         representGenotypeAsDocument(genotype);
73     // test if it got written as expected
74
IDataElementList tree = doc.getTree();
75     // a single top level element
76
assertTrue(tree.getLength() == 1);
77     IDataElement element = tree.item(0);
78     // a Genotype should be the top level element
79
assertEquals("genotype", element.getTagName());
80     IDataElementList chromList = element.getChildNodes();
81     assertEquals(NUM_CHROMS, chromList.getLength());
82     // for all chromosomes
83
for (int i = 0; i < NUM_CHROMS; i++) {
84       IDataElement chrom = chromList.item(i);
85       assertTrue(chrom.getTagName().equals("chromosome"));
86       IDataElementList genesList = chrom.getChildNodes();
87       assertTrue(genesList.getLength() == 1);
88       IDataElement genes = genesList.item(0);
89       assertTrue(genes.getTagName().equals("genes"));
90       IDataElementList geneList = genes.getChildNodes();
91       assertTrue(geneList.getLength() == NUM_GENES);
92       // for all genes in a chromosome
93
for (int j = 0; j < NUM_GENES; j++) {
94         IDataElement gene = geneList.item(j);
95         assertTrue(gene.getTagName().equals("gene"));
96         IDataElementList alleleList = gene.getChildNodes();
97         assertTrue(alleleList.getLength() == 1);
98         IDataElement allele = alleleList.item(0);
99         assertTrue(allele.getTagName().equals("allele"));
100         assertTrue(allele.getAttribute("value").
101                    equals(chroms[i].getGene(j).
102                           getPersistentRepresentation()));
103       }
104     }
105   }
106
107   /**
108    * @throws Exception
109    * @author Klaus Meffert
110    * @since 2.6
111    */

112   public void testRepresentChromosomeAsDocument_0()
113       throws Exception JavaDoc {
114     Configuration conf = new DefaultConfiguration();
115     Chromosome chrom = new Chromosome(conf, new Gene[] {
116                                       new IntegerGene(conf, 1, 5),
117                                       new IntegerGene(conf, 1, 10)});
118     chrom.getGene(0).setAllele(new Integer JavaDoc(1));
119     chrom.getGene(1).setAllele(new Integer JavaDoc( -3));
120     // write the chromosome as a document
121
IDataCreators doc = DataTreeBuilder.getInstance().
122         representChromosomeAsDocument(chrom);
123     // test if it got written as expected
124
IDataElementList tree = doc.getTree();
125     // a single top level element
126
assertTrue(tree.getLength() == 1);
127     IDataElement element = tree.item(0);
128     // a Chromosome should be the top level element
129
assertTrue(element.getTagName().equals("chromosome"));
130     IDataElementList chromsList = element.getChildNodes();
131     assertEquals(1, chromsList.getLength());
132     IDataElement genes = chromsList.item(0);
133     assertTrue(genes.getTagName().equals("genes"));
134     IDataElementList geneList = genes.getChildNodes();
135     assertTrue(geneList.getLength() == NUM_GENES);
136     // for all genes in a chromosome
137
for (int j = 0; j < NUM_GENES; j++) {
138       IDataElement gene = geneList.item(j);
139       assertTrue(gene.getTagName().equals("gene"));
140       IDataElementList alleleList = gene.getChildNodes();
141       assertTrue(alleleList.getLength() == 1);
142       IDataElement allele = alleleList.item(0);
143       assertTrue(allele.getTagName().equals("allele"));
144       assertTrue(allele.getAttribute("value").
145                  equals(chrom.getGene(j).
146                         getPersistentRepresentation()));
147     }
148   }
149 }
150
Popular Tags