/** * Marshall an array of Genes to an XML Element representation. * * @param a_geneValues the genes to represent as an XML element * @param a_xmlDocument a Document instance that will be used to create the Element instance. Note * that the element will NOT be added to the document by this method * @return an Element object representing the given genes * @author Neil Rotstan * @author Klaus Meffert * @since 1.0 * @deprecated use XMLDocumentBuilder instead */ public static Element representGenesAsElement( final Gene[] a_geneValues, final Document a_xmlDocument) { // Create the parent genes element. // -------------------------------- Element genesElement = a_xmlDocument.createElement(GENES_TAG); // Now add gene sub-elements for each gene in the given array. // ----------------------------------------------------------- Element geneElement; for (int i = 0; i < a_geneValues.length; i++) { // Create the allele element for this gene. // ---------------------------------------- geneElement = a_xmlDocument.createElement(GENE_TAG); // Add the class attribute and set its value to the class // name of the concrete class representing the current Gene. // --------------------------------------------------------- geneElement.setAttribute(CLASS_ATTRIBUTE, a_geneValues[i].getClass().getName()); // Create a text node to contain the string representation of // the gene's value (allele). // ---------------------------------------------------------- Element alleleRepresentation = representAlleleAsElement(a_geneValues[i], a_xmlDocument); // And now add the text node to the gene element, and then // add the gene element to the genes element. // ------------------------------------------------------- geneElement.appendChild(alleleRepresentation); genesElement.appendChild(geneElement); } return genesElement; }
/** * Marshall a Chromosome instance to an XML Document representation, including its contained Gene * instances. * * @param a_subject the chromosome to represent as an XML document * @return a Document object representing the given Chromosome * @author Neil Rotstan * @since 1.0 * @deprecated use XMLDocumentBuilder instead */ public static Document representChromosomeAsDocument(final IChromosome a_subject) { // DocumentBuilders do not have to be thread safe, so we have to // protect creation of the Document with a synchronized block. // ------------------------------------------------------------- Document chromosomeDocument; synchronized (m_lock) { chromosomeDocument = m_documentCreator.newDocument(); } Element chromosomeElement = representChromosomeAsElement(a_subject, chromosomeDocument); chromosomeDocument.appendChild(chromosomeElement); return chromosomeDocument; }
/** * Marshall a Genotype to an XML Document representation, including its population of Chromosome * instances. * * @param a_subject the genotype to represent as an XML document * @return a Document object representing the given Genotype * @author Neil Rotstan * @since 1.0 * @deprecated use XMLDocumentBuilder instead */ public static Document representGenotypeAsDocument(final Genotype a_subject) { // DocumentBuilders do not have to be thread safe, so we have to // protect creation of the Document with a synchronized block. // ------------------------------------------------------------- Document genotypeDocument; synchronized (m_lock) { genotypeDocument = m_documentCreator.newDocument(); } Element genotypeElement = representGenotypeAsElement(a_subject, genotypeDocument); genotypeDocument.appendChild(genotypeElement); return genotypeDocument; }
/** * Unmarshall a Chromosome instance from a given XML Document representation. Its genes will be * unmarshalled from the gene sub-elements. * * @param a_activeConfiguration the current active Configuration object that is to be used during * construction of the Chromosome instances * @param a_xmlDocument the XML Document representation of the Chromosome * @return a new Chromosome instance setup with the data from the XML Document representation * @throws ImproperXMLException if the given Document is improperly structured or missing data * @throws InvalidConfigurationException if the given Configuration is in an inconsistent state * @throws UnsupportedRepresentationException if the actively configured Gene implementation does * not support the string representation of the alleles used in the given XML document * @throws GeneCreationException if there is a problem creating or populating a Gene instance * @author Neil Rotstan * @since 1.0 */ public static Chromosome getChromosomeFromDocument( Configuration a_activeConfiguration, Document a_xmlDocument) throws ImproperXMLException, InvalidConfigurationException, UnsupportedRepresentationException, GeneCreationException { // Extract the root element, which should be a chromosome element. // After verifying that the root element is not null and that it // in fact is a chromosome element, then convert it into a Chromosome // instance. // ------------------------------------------------------------------ Element rootElement = a_xmlDocument.getDocumentElement(); if (rootElement == null || !(rootElement.getTagName().equals(CHROMOSOME_TAG))) { throw new ImproperXMLException( "Unable to build Chromosome instance from XML Document: " + "'chromosome' element must be at root of Document."); } return getChromosomeFromElement(a_activeConfiguration, rootElement); }
/** * Marshall a Chromosome instance to an XML Element representation, including its contained Genes * as sub-elements. This may be useful in scenarios where representation as an entire Document is * undesirable, such as when the representation of this Chromosome is to be combined with other * elements in a single Document. * * @param a_subject the chromosome to represent as an XML element * @param a_xmlDocument a Document instance that will be used to create the Element instance. Note * that the element will NOT be added to the document by this method * @return an Element object representing the given Chromosome * @author Neil Rotstan * @since 1.0 * @deprecated use XMLDocumentBuilder instead */ public static Element representChromosomeAsElement( final IChromosome a_subject, final Document a_xmlDocument) { // Start by creating an element for the chromosome and its size // attribute, which represents the number of genes in the chromosome. // ------------------------------------------------------------------ Element chromosomeElement = a_xmlDocument.createElement(CHROMOSOME_TAG); chromosomeElement.setAttribute(SIZE_ATTRIBUTE, Integer.toString(a_subject.size())); // Next create the genes element with its nested gene elements, // which will contain string representations of the alleles. // -------------------------------------------------------------- Element genesElement = representGenesAsElement(a_subject.getGenes(), a_xmlDocument); // Add the new genes element to the chromosome element and then // return the chromosome element. // ------------------------------------------------------------- chromosomeElement.appendChild(genesElement); return chromosomeElement; }
/** * Marshall a Genotype instance into an XML Element representation, including its population of * Chromosome instances as sub-elements. This may be useful in scenarios where representation as * an entire Document is undesirable, such as when the representation of this Genotype is to be * combined with other elements in a single Document. * * @param a_subject the genotype to represent as an XML element * @param a_xmlDocument a Document instance that will be used to create the Element instance. Note * that the element will NOT be added to the document by this method * @return an Element object representing the given Genotype * @author Neil Rotstan * @since 1.0 * @deprecated use XMLDocumentBuilder instead */ public static Element representGenotypeAsElement( final Genotype a_subject, final Document a_xmlDocument) { Population population = a_subject.getPopulation(); // Start by creating the genotype element and its size attribute, // which represents the number of chromosomes present in the // genotype. // -------------------------------------------------------------- Element genotypeTag = a_xmlDocument.createElement(GENOTYPE_TAG); genotypeTag.setAttribute(SIZE_ATTRIBUTE, Integer.toString(population.size())); // Next, add nested elements for each of the chromosomes in the // genotype. // ------------------------------------------------------------ for (int i = 0; i < population.size(); i++) { Element chromosomeElement = representChromosomeAsElement(population.getChromosome(i), a_xmlDocument); genotypeTag.appendChild(chromosomeElement); } return genotypeTag; }
/** * @param a_gene Gene * @param a_xmlDocument Document * @return Element * @author Klaus Meffert * @since 2.0 */ private static Element representAlleleAsElement(final Gene a_gene, final Document a_xmlDocument) { Element alleleElement = a_xmlDocument.createElement(ALLELE_TAG); alleleElement.setAttribute("class", a_gene.getClass().getName()); alleleElement.setAttribute("value", a_gene.getPersistentRepresentation()); return alleleElement; }