1 10 package org.jgap.data; 11 12 import org.jgap.*; 13 14 24 public class DataTreeBuilder { 25 26 27 private final static String CVS_REVISION = "$Revision: 1.10 $"; 28 29 32 private static final String GENOTYPE_TAG = "genotype"; 33 34 37 private static final String CHROMOSOME_TAG = "chromosome"; 38 39 42 private static final String GENES_TAG = "genes"; 43 44 47 private static final String GENE_TAG = "gene"; 48 49 private static final String ALLELE_TAG = "allele"; 50 51 55 private static final String SIZE_ATTRIBUTE = "size"; 56 57 61 private static final String CLASS_ATTRIBUTE = "class"; 62 63 66 private Object m_lock; 67 68 private static DataTreeBuilder m_instance; 69 70 76 public static synchronized DataTreeBuilder getInstance() { 77 if (m_instance == null) { 78 m_instance = new DataTreeBuilder(); 79 m_instance.m_lock = new Object (); 80 } 81 return m_instance; 82 } 83 84 87 private DataTreeBuilder() { 88 } 89 90 101 public IDataCreators representGenotypeAsDocument(final Genotype a_subject) 102 throws Exception { 103 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 132 public IDataElement representGenotypeAsElement(final Genotype a_subject) 133 throws Exception { 134 Population population = a_subject.getPopulation(); 135 IDataElement genotypeTag = new DataElement(GENOTYPE_TAG); 140 genotypeTag.setAttribute(SIZE_ATTRIBUTE, 141 Integer.toString(population.size())); 142 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 164 public IDataCreators representChromosomeAsDocument(final IChromosome a_subject) 165 throws Exception { 166 IDataCreators chromosomeDocument; 170 synchronized (m_lock) { 171 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 199 public IDataElement representChromosomeAsElement(final IChromosome a_subject) 200 throws Exception { 201 IDataElement chromosomeElement = new DataElement(CHROMOSOME_TAG); 205 chromosomeElement.setAttribute(SIZE_ATTRIBUTE, 206 Integer.toString(a_subject.size())); 207 IDataElement genesElement = representGenesAsElement(a_subject.getGenes()); 211 chromosomeElement.appendChild(genesElement); 216 return chromosomeElement; 217 } 218 219 229 public IDataElement representGenesAsElement(final Gene[] a_geneValues) 230 throws Exception { 231 IDataElement genesElement = new DataElement(GENES_TAG); 234 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 254 public IDataElement representGeneAsElement(final Gene a_gene) 255 throws Exception { 256 IDataElement geneElement = new DataElement(GENE_TAG); 259 geneElement.setAttribute(CLASS_ATTRIBUTE, 263 a_gene.getClass().getName()); 264 geneElement.appendChild(representAlleleAsElement(a_gene)); 268 return geneElement; 269 } 270 271 281 private IDataElement representAlleleAsElement(final Gene a_gene) 282 throws Exception { 283 IDataElement alleleElement = new DataElement(ALLELE_TAG); 284 alleleElement.setAttribute("value", a_gene.getPersistentRepresentation()); 285 return alleleElement; 286 } 287 } 288 | Popular Tags |