Clover coverage report - JGAP 3.1
Coverage timestamp: Mo Dez 11 2006 21:16:18 CET
file stats: LOC: 287   Methods: 10
NCLOC: 95   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
DataTreeBuilder.java 100% 100% 100% 100%
coverage
 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 CVS_REVISION = "$Revision: 1.10 $";
 28   
 29    /**
 30    * Constant representing the name of the genotype element tag.
 31    */
 32    private static final String GENOTYPE_TAG = "genotype";
 33   
 34    /**
 35    * Constant representing the name of the chromosome element tag.
 36    */
 37    private static final String CHROMOSOME_TAG = "chromosome";
 38   
 39    /**
 40    * Constant representing the name of the gene element tag.
 41    */
 42    private static final String GENES_TAG = "genes";
 43   
 44    /**
 45    * Constant representing the name of the gene element tag.
 46    */
 47    private static final String GENE_TAG = "gene";
 48   
 49    private static final String 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 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 CLASS_ATTRIBUTE = "class";
 62   
 63    /**
 64    * Shared lock object used for synchronization purposes.
 65    */
 66    private Object 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  4 public static synchronized DataTreeBuilder getInstance() {
 77  4 if (m_instance == null) {
 78  1 m_instance = new DataTreeBuilder();
 79  1 m_instance.m_lock = new Object();
 80    }
 81  4 return m_instance;
 82    }
 83   
 84    /**
 85    * Private constructor for Singleton
 86    */
 87  1 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  1 public IDataCreators representGenotypeAsDocument(final Genotype a_subject)
 102    throws Exception {
 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  1 IDataCreators genotypeDocument;
 107  1 synchronized (m_lock) {
 108  1 genotypeDocument = new DataElementsDocument();
 109  1 genotypeDocument.setTree(createTree());
 110    }
 111  1 IDataElement genotypeElement = representGenotypeAsElement(a_subject);
 112  1 genotypeDocument.appendChild(genotypeElement);
 113  1 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  1 public IDataElement representGenotypeAsElement(final Genotype a_subject)
 133    throws Exception {
 134  1 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  1 IDataElement genotypeTag = new DataElement(GENOTYPE_TAG);
 140  1 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  1 for (int i = 0; i < population.size(); i++) {
 146  5 IDataElement chromosomeElement =
 147    representChromosomeAsElement(population.getChromosome(i));
 148  5 genotypeTag.appendChild(chromosomeElement);
 149    }
 150  1 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  3 public IDataCreators representChromosomeAsDocument(final IChromosome a_subject)
 165    throws Exception {
 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  3 IDataCreators chromosomeDocument;
 170  3 synchronized (m_lock) {
 171    // Build data structure for tree
 172  3 chromosomeDocument = new DataElementsDocument();
 173  3 chromosomeDocument.setTree(createTree());
 174    }
 175  3 IDataElement chromosomeElement =
 176    representChromosomeAsElement(a_subject);
 177  3 chromosomeDocument.appendChild(chromosomeElement);
 178  3 return chromosomeDocument;
 179    }
 180   
 181  4 protected IDataElementList createTree() {
 182  4 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  8 public IDataElement representChromosomeAsElement(final IChromosome a_subject)
 200    throws Exception {
 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  8 IDataElement chromosomeElement = new DataElement(CHROMOSOME_TAG);
 205  8 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  8 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  8 chromosomeElement.appendChild(genesElement);
 216  8 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  8 public IDataElement representGenesAsElement(final Gene[] a_geneValues)
 230    throws Exception {
 231    // Create the parent genes element.
 232    // --------------------------------
 233  8 IDataElement genesElement = new DataElement(GENES_TAG);
 234    // Now add gene sub-elements for each gene in the given array.
 235    // ---------------------------------------------------------------
 236  8 IDataElement geneElement;
 237  8 for (int i = 0; i < a_geneValues.length; i++) {
 238  16 geneElement = representGeneAsElement(a_geneValues[i]);
 239  16 genesElement.appendChild(geneElement);
 240    }
 241  8 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  16 public IDataElement representGeneAsElement(final Gene a_gene)
 255    throws Exception {
 256    // Create the allele element for this gene.
 257    // ----------------------------------------
 258  16 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  16 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  16 geneElement.appendChild(representAlleleAsElement(a_gene));
 268  16 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  16 private IDataElement representAlleleAsElement(final Gene a_gene)
 282    throws Exception {
 283  16 IDataElement alleleElement = new DataElement(ALLELE_TAG);
 284  16 alleleElement.setAttribute("value", a_gene.getPersistentRepresentation());
 285  16 return alleleElement;
 286    }
 287    }