KickJava   Java API By Example, From Geeks To Geeks.

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


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
14 /**
15  * Builds a tree structure from Genes, Chrosomes or from a Genotype.
16  * <p>
17  * Generated result is generic data type usable for concrete persistence
18  * strategies, including XML documents, writing an object to a file or
19  * stream etc.
20  *
21  * @author Klaus Meffert
22  * @since 2.0
23  */

24 public class DataTreeBuilder {
25
26   /** String containing the CVS revision. Read out via reflection!*/
27   private final static String JavaDoc CVS_REVISION = "$Revision: 1.10 $";
28
29   /**
30    * Constant representing the name of the genotype element tag.
31    */

32   private static final String JavaDoc GENOTYPE_TAG = "genotype";
33
34   /**
35    * Constant representing the name of the chromosome element tag.
36    */

37   private static final String JavaDoc CHROMOSOME_TAG = "chromosome";
38
39   /**
40    * Constant representing the name of the gene element tag.
41    */

42   private static final String JavaDoc GENES_TAG = "genes";
43
44   /**
45    * Constant representing the name of the gene element tag.
46    */

47   private static final String JavaDoc GENE_TAG = "gene";
48
49   private static final String JavaDoc ALLELE_TAG = "allele";
50
51   /**
52    * Constant representing the name of the size attribute that is
53    * added to genotype and chromosome elements to describe their size.
54    */

55   private static final String JavaDoc SIZE_ATTRIBUTE = "size";
56
57   /**
58    * Constant representing the fully-qualified name of the concrete
59    * Gene class that was marshalled.
60    */

61   private static final String JavaDoc CLASS_ATTRIBUTE = "class";
62
63   /**
64    * Shared lock object used for synchronization purposes.
65    */

66   private Object JavaDoc m_lock;
67
68   private static DataTreeBuilder m_instance;
69
70   /**
71    * @return the singleton instance of this class
72    *
73    * @author Klaus Meffert
74    * @since 2.0
75    */

76   public static synchronized DataTreeBuilder getInstance() {
77     if (m_instance == null) {
78       m_instance = new DataTreeBuilder();
79       m_instance.m_lock = new Object JavaDoc();
80     }
81     return m_instance;
82   }
83
84   /**
85    * Private constructor for Singleton
86    */

87   private DataTreeBuilder() {
88   }
89
90   /**
91    * Represent a Genotype as a generic data document, including its
92    * population of Chromosome instances.
93    *
94    * @param a_subject the genotype to represent
95    * @throws Exception
96    * @return a generic document object representing the given Genotype
97    *
98    * @author Klaus Meffert
99    * @since 2.0
100    */

101   public IDataCreators representGenotypeAsDocument(final Genotype a_subject)
102       throws Exception JavaDoc {
103     // DocumentBuilders do not have to be thread safe, so we have to
104
// protect creation of the Document with a synchronized block.
105
// -------------------------------------------------------------
106
IDataCreators genotypeDocument;
107     synchronized (m_lock) {
108       genotypeDocument = new DataElementsDocument();
109       genotypeDocument.setTree(createTree());
110     }
111     IDataElement genotypeElement = representGenotypeAsElement(a_subject);
112     genotypeDocument.appendChild(genotypeElement);
113     return genotypeDocument;
114   }
115
116   /**
117    * Represent a Genotype as a generic data element, including its
118    * population of Chromosome instances.
119    *
120    * This may be useful in scenarios where representation as an
121    * entire document is undesirable, such as when the representation
122    * of this Genotype is to be combined with other elements in a
123    * single document.
124    *
125    * @param a_subject the genotype to represent
126    * @throws Exception
127    * @return an element object representing the given Genotype
128    *
129    * @author Klaus Meffert
130    * @since 2.0
131    */

132   public IDataElement representGenotypeAsElement(final Genotype a_subject)
133       throws Exception JavaDoc {
134     Population population = a_subject.getPopulation();
135     // Start by creating the genotype element and its size attribute,
136
// which represents the number of chromosomes present in the
137
// genotype.
138
// --------------------------------------------------------------
139
IDataElement genotypeTag = new DataElement(GENOTYPE_TAG);
140     genotypeTag.setAttribute(SIZE_ATTRIBUTE,
141                              Integer.toString(population.size()));
142     // Next, add nested elements for each of the chromosomes in the
143
// genotype.
144
// ------------------------------------------------------------
145
for (int i = 0; i < population.size(); i++) {
146       IDataElement chromosomeElement =
147           representChromosomeAsElement(population.getChromosome(i));
148       genotypeTag.appendChild(chromosomeElement);
149     }
150     return genotypeTag;
151   }
152
153   /**
154    * Represent a Chromosome as a generic data type document, including its
155    * contained Gene instances.
156    *
157    * @param a_subject the chromosome to represent
158    * @throws Exception
159    * @return a document object representing the given Chromosome
160    *
161    * @author Klaus Meffert
162    * @since 2.0
163    */

164   public IDataCreators representChromosomeAsDocument(final IChromosome a_subject)
165       throws Exception JavaDoc {
166     // DocumentBuilders do not have to be thread safe, so we have to
167
// protect creation of the Document with a synchronized block.
168
// -------------------------------------------------------------
169
IDataCreators chromosomeDocument;
170     synchronized (m_lock) {
171       // Build data structure for tree
172
chromosomeDocument = new DataElementsDocument();
173       chromosomeDocument.setTree(createTree());
174     }
175     IDataElement chromosomeElement =
176         representChromosomeAsElement(a_subject);
177     chromosomeDocument.appendChild(chromosomeElement);
178     return chromosomeDocument;
179   }
180
181   protected IDataElementList createTree() {
182     return new DataElementList();
183   }
184
185   /**
186    * Represent a Chromosome as a generic data element, including its
187    * contained Gene instances.
188    * This may be useful in scenarios where representation as an entire document
189    * is undesirable, such as when the representation of this Chromosome is to
190    * be combined with other elements in a single document.
191    *
192    * @param a_subject the chromosome to represent
193    * @throws Exception
194    * @return an element object representing the given Chromosome
195    *
196    * @author Klaus Meffert
197    * @since 2.0
198    */

199   public IDataElement representChromosomeAsElement(final IChromosome a_subject)
200       throws Exception JavaDoc {
201     // Start by creating an element for the chromosome and its size
202
// attribute, which represents the number of genes in the chromosome.
203
// ------------------------------------------------------------------
204
IDataElement chromosomeElement = new DataElement(CHROMOSOME_TAG);
205     chromosomeElement.setAttribute(SIZE_ATTRIBUTE,
206                                    Integer.toString(a_subject.size()));
207     // Next create the genes element with its nested gene elements,
208
// which will contain string representations of the alleles.
209
// --------------------------------------------------------------
210
IDataElement genesElement = representGenesAsElement(a_subject.getGenes());
211     // Add the new genes element to the chromosome element and then
212
// Add the new genes element to the chromosome element and then
213
// return the chromosome element.
214
// -------------------------------------------------------------
215
chromosomeElement.appendChild(genesElement);
216     return chromosomeElement;
217   }
218
219   /**
220    * Represent Genes as a generic data type element.
221    *
222    * @param a_geneValues the genes to represent
223    * @throws Exception
224    * @return an element object representing the given genes
225    *
226    * @author Klaus Meffert
227    * @since 2.0
228    */

229   public IDataElement representGenesAsElement(final Gene[] a_geneValues)
230       throws Exception JavaDoc {
231     // Create the parent genes element.
232
// --------------------------------
233
IDataElement genesElement = new DataElement(GENES_TAG);
234     // Now add gene sub-elements for each gene in the given array.
235
// ---------------------------------------------------------------
236
IDataElement geneElement;
237     for (int i = 0; i < a_geneValues.length; i++) {
238       geneElement = representGeneAsElement(a_geneValues[i]);
239       genesElement.appendChild(geneElement);
240     }
241     return genesElement;
242   }
243
244   /**
245    * Represent a Gene as a generic data element.
246    *
247    * @param a_gene the Gene to represent
248    * @throws Exception
249    * @return an element object representing the given gene
250    *
251    * @author Klaus Meffert
252    * @since 2.0
253    */

254   public IDataElement representGeneAsElement(final Gene a_gene)
255       throws Exception JavaDoc {
256     // Create the allele element for this gene.
257
// ----------------------------------------
258
IDataElement geneElement = new DataElement(GENE_TAG);
259     // Add the class attribute and set its value to the class
260
// name of the concrete class representing the current Gene.
261
// ---------------------------------------------------------
262
geneElement.setAttribute(CLASS_ATTRIBUTE,
263                              a_gene.getClass().getName());
264     // Create a text node to contain the string representation of
265
// the gene's value (allele).
266
// ----------------------------------------------------------
267
geneElement.appendChild(representAlleleAsElement(a_gene));
268     return geneElement;
269   }
270
271   /**
272    * Represent an Allele as a generic data element
273    *
274    * @param a_gene the gene holding the allele
275    * @throws Exception
276    * @return IDataElement created data element
277    *
278    * @author Klaus Meffert
279    * @since 2.0
280    */

281   private IDataElement representAlleleAsElement(final Gene a_gene)
282       throws Exception JavaDoc {
283     IDataElement alleleElement = new DataElement(ALLELE_TAG);
284     alleleElement.setAttribute("value", a_gene.getPersistentRepresentation());
285     return alleleElement;
286   }
287 }
288
Popular Tags