Author: glandais Date: 2008-03-20 14:22:26 +0000 (Thu, 20 Mar 2008) New Revision: 1415 Added: trunk/simexplorer-is/simexplorer-is-service/src/java/fr/cemagref/simexplorer/is/service/random/ElementGenerator.java Removed: trunk/simexplorer-is/simexplorer-is-storage/src/java/fr/cemagref/simexplorer/is/storage/ElementGenerator.java Log: Will contain element generators Copied: trunk/simexplorer-is/simexplorer-is-service/src/java/fr/cemagref/simexplorer/is/service/random/ElementGenerator.java (from rev 1414, trunk/simexplorer-is/simexplorer-is-storage/src/java/fr/cemagref/simexplorer/is/storage/ElementGenerator.java) =================================================================== --- trunk/simexplorer-is/simexplorer-is-service/src/java/fr/cemagref/simexplorer/is/service/random/ElementGenerator.java (rev 0) +++ trunk/simexplorer-is/simexplorer-is-service/src/java/fr/cemagref/simexplorer/is/service/random/ElementGenerator.java 2008-03-20 14:22:26 UTC (rev 1415) @@ -0,0 +1,389 @@ +/* +* ##% Copyright (C) 2008 Code Lutin, Gabriel Landais +* +* This program is free software; you can redistribute it and/or +* modify it under the terms of the GNU General Public License +* as published by the Free Software Foundation; either version 2 +* of the License, or (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program; if not, write to the Free Software +* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +* ##% */ +package fr.cemagref.simexplorer.is.storage; + +import java.io.ByteArrayInputStream; +import java.io.InputStream; +import java.util.Date; +import java.util.Random; +import java.util.UUID; + +import org.apache.commons.lang.RandomStringUtils; +import org.codelutin.util.MD5; + +import fr.cemagref.simexplorer.is.entities.Entity; +import fr.cemagref.simexplorer.is.entities.attachment.Attachment; +import fr.cemagref.simexplorer.is.entities.composite.Codes; +import fr.cemagref.simexplorer.is.entities.composite.Components; +import fr.cemagref.simexplorer.is.entities.composite.ConstantValues; +import fr.cemagref.simexplorer.is.entities.composite.Constants; +import fr.cemagref.simexplorer.is.entities.composite.Descriptors; +import fr.cemagref.simexplorer.is.entities.composite.ExplorationDatas; +import fr.cemagref.simexplorer.is.entities.composite.Libraries; +import fr.cemagref.simexplorer.is.entities.composite.SimpleComposite; +import fr.cemagref.simexplorer.is.entities.composite.Structures; +import fr.cemagref.simexplorer.is.entities.data.Code; +import fr.cemagref.simexplorer.is.entities.data.Component; +import fr.cemagref.simexplorer.is.entities.data.Constant; +import fr.cemagref.simexplorer.is.entities.data.ConstantValue; +import fr.cemagref.simexplorer.is.entities.data.Descriptor; +import fr.cemagref.simexplorer.is.entities.data.ExplorationApplication; +import fr.cemagref.simexplorer.is.entities.data.ExplorationData; +import fr.cemagref.simexplorer.is.entities.data.Library; +import fr.cemagref.simexplorer.is.entities.data.LoggableElement; +import fr.cemagref.simexplorer.is.entities.data.Result; +import fr.cemagref.simexplorer.is.entities.metadata.MetaData; +import fr.cemagref.simexplorer.is.entities.metadata.Version; +import fr.cemagref.simexplorer.is.factories.ContentTypeFactory; + +/** + * The Class ElementGenerator. + */ +public class ElementGenerator { + + /** + * The Class RandomStream. + */ + public class RandomStream { + + /** The md5. */ + private String md5; + + /** The data. */ + private byte[] data; + + /** + * Gets the stream. + * + * @return the stream + */ + public InputStream getStream() { + InputStream is; + is = new ByteArrayInputStream(data); + return is; + } + + /** + * Gets the md5. + * + * @return the md5 + */ + public String getMd5() { + return md5; + } + + /** + * Sets the md5. + * + * @param md5 the new md5 + */ + public void setMd5(String md5) { + this.md5 = md5; + } + + /** + * Sets the data. + * + * @param bytes the new data + */ + public void setData(byte[] bytes) { + data = bytes; + } + + } + + /** The r. */ + private Random r = new Random(); + + /** Number of words. */ + private static int cs; + + /** The counter. */ + private static int counter = 0; + + /** The randomstrings. */ + private static String[] randomstrings; + + /** + * Instantiates a new element generator. + */ + public ElementGenerator() { + super(); + initializeStrings(); + } + + /** + * Short string. + * + * @return the string + */ + private String shortString() { + return RandomStringUtils.randomAlphabetic(r.nextInt(5) + 5); + } + + /** + * Hash. + * + * @return the string + */ + private String hash() { + return RandomStringUtils.randomAlphanumeric(32); + } + + /** + * Initialize strings. + */ + private void initializeStrings() { + cs = 150000; + randomstrings = new String[cs]; + for (int i = 0; i < randomstrings.length; i++) { + randomstrings[i] = shortString(); + } + } + + /** + * Update. + * + * @param element the element + * + * @throws Exception the exception + */ + private void update(LoggableElement element) throws Exception { + MetaData metaData = new MetaData(); + metaData.setUuid(UUID.randomUUID().toString()); + element.setName(element.getClass().getSimpleName() + "-" + counter++); + + StringBuffer sb = new StringBuffer(""); + + for (int i = 0, max = 3 + r.nextInt(5); i < max; i++) { + sb.append(randomstrings[r.nextInt(cs)]).append(" "); + } + element.setDescription(sb.toString()); + + Version v = new Version("0"); + + for (int i = 0, max = 2 + r.nextInt(2); i < max; i++) { + v.setVersion(i, r.nextInt(5)); + } + metaData.setVersion(v.toString()); + metaData.setCreationDate(new Date()); + metaData.setHash(shortString()); + metaData.setLatest(true); + + Descriptors descriptors = new Descriptors(); + for (int i = 0, max = 1 + r.nextInt(12); i < max; i++) { + descriptors.add(new Descriptor(shortString(), shortString())); + } + element.setDescriptors(descriptors); + + if (element instanceof Library || element instanceof ExplorationData) { + // generate attachments + for (int i = 0, max = r.nextInt(15); i < max; i++) { + element.getAttachments().add(generateAttachment()); + } + } + + element.setMetaData(metaData); + } + + /** + * Generate composite. + * + * @param <SC> Simple composite wanted + * @param <T> Class of simple composite children + * + * @param compositeClass the composite class + * + * @return the sC + * + * @throws Exception the exception + */ + public <T extends Entity, SC extends SimpleComposite<T>> SC generateComposite(Class<SC> compositeClass) throws Exception { + SC elements = compositeClass.newInstance(); + Class<T> elementClass = elements.getChildrenClass(); + int i = 3 + r.nextInt(2); + for (int j = 0; j < i; j++) { + T element = elementClass.newInstance(); + if (element instanceof LoggableElement) { + update((LoggableElement) element); + } + elements.add(element); + } + return elements; + } + + /** + * Generate random ea. + * + * @return the exploration application + * + * @throws Exception the exception + */ + public ExplorationApplication generateRandomEA() throws Exception { + ExplorationApplication ea = new ExplorationApplication(); + update(ea); + + ExplorationDatas explorationDatas = generateComposite(ExplorationDatas.class); + for (ExplorationData object : explorationDatas) { + processExplorationData(object); + } + ea.setExplorations(explorationDatas); + + Components components = generateComposite(Components.class); + for (Component object : components) { + processComponent(object); + } + ea.setComponents(components); + return ea; + } + + /** + * Process component. + * + * @param component the component + * + * @throws Exception the exception + */ + private void processComponent(Component component) throws Exception { + Constants constants = generateComposite(Constants.class); + for (Constant constant : constants) { + processConstant(constant); + } + component.setConstants(constants); + + component.setStructures(new Structures()); + + Codes codes = generateComposite(Codes.class); + for (Code code : codes) { + processCode(code); + } + component.setCodes(codes); + + Libraries libraries = generateComposite(Libraries.class); + for (Library library : libraries) { + processLibrary(library); + } + component.setLibraries(libraries); + + } + + /** + * Generate attachment. + * + * @return the attachment + * + * @throws Exception the exception + */ + private Attachment generateAttachment() throws Exception { + Attachment attachment = new Attachment(); + attachment.setContentType(ContentTypeFactory.getContentTypeInstance("RawType")); + attachment.setDataHash(hash()); + attachment.setFileName(shortString() + ".txt"); + return attachment; + } + + /** + * Process library. + * + * @param library the library + * + * @throws Exception the exception + */ + private void processLibrary(Library library) throws Exception { + // library.getMetaData().getAttachments().add(generateAttachment()); + } + + /** + * Process code. + * + * @param code the code + */ + private void processCode(Code code) { + code.setCode(shortString()); + code.setLanguage(shortString()); + } + + /** + * Process constant. + * + * @param constant the constant + */ + private void processConstant(Constant constant) { + constant.setName(shortString()); + constant.setType(String.class); + } + + /** + * Process exploration data. + * + * @param explorationData the exploration data + * + * @throws Exception the exception + */ + private void processExplorationData(ExplorationData explorationData) throws Exception { + Result result = new Result(); + explorationData.setResult(result); + // explorationData.getMetaData().getAttachments().add(generateAttachment()); + + ConstantValues constantValues = generateComposite(ConstantValues.class); + for (ConstantValue constantValue : constantValues) { + processConstantValue(constantValue); + } + explorationData.setConstantValues(constantValues); + + } + + /** + * Process constant value. + * + * @param constantValue the constant value + */ + private void processConstantValue(ConstantValue constantValue) { + Constant constant = new Constant(); + constant.setName(shortString()); + constant.setType(Integer.class); + + constantValue.setConstant(constant); + constantValue.setValue(Integer.toString(r.nextInt())); + } + + /** + * Generate text stream. + * + * @return the input stream + * + * @throws Exception the exception + */ + public RandomStream generateTextStream() throws Exception { + RandomStream rs = new RandomStream(); + + int wordcount = 300 + r.nextInt(300); + StringBuffer sb = new StringBuffer(); + for (int i = 0; i < wordcount; i++) { + sb.append(randomstrings[r.nextInt(cs)]).append(" "); + } + + MD5 md5 = new MD5(); + md5.Update(sb.toString().getBytes()); + rs.setMd5(md5.asHex()); + + rs.setData(sb.toString().getBytes()); + + return rs; + } +} Deleted: trunk/simexplorer-is/simexplorer-is-storage/src/java/fr/cemagref/simexplorer/is/storage/ElementGenerator.java =================================================================== --- trunk/simexplorer-is/simexplorer-is-storage/src/java/fr/cemagref/simexplorer/is/storage/ElementGenerator.java 2008-03-20 14:21:56 UTC (rev 1414) +++ trunk/simexplorer-is/simexplorer-is-storage/src/java/fr/cemagref/simexplorer/is/storage/ElementGenerator.java 2008-03-20 14:22:26 UTC (rev 1415) @@ -1,389 +0,0 @@ -/* -* ##% Copyright (C) 2008 Code Lutin, Gabriel Landais -* -* This program is free software; you can redistribute it and/or -* modify it under the terms of the GNU General Public License -* as published by the Free Software Foundation; either version 2 -* of the License, or (at your option) any later version. -* -* This program is distributed in the hope that it will be useful, -* but WITHOUT ANY WARRANTY; without even the implied warranty of -* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -* GNU General Public License for more details. -* -* You should have received a copy of the GNU General Public License -* along with this program; if not, write to the Free Software -* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. -* ##% */ -package fr.cemagref.simexplorer.is.storage; - -import java.io.ByteArrayInputStream; -import java.io.InputStream; -import java.util.Date; -import java.util.Random; -import java.util.UUID; - -import org.apache.commons.lang.RandomStringUtils; -import org.codelutin.util.MD5; - -import fr.cemagref.simexplorer.is.entities.Entity; -import fr.cemagref.simexplorer.is.entities.attachment.Attachment; -import fr.cemagref.simexplorer.is.entities.composite.Codes; -import fr.cemagref.simexplorer.is.entities.composite.Components; -import fr.cemagref.simexplorer.is.entities.composite.ConstantValues; -import fr.cemagref.simexplorer.is.entities.composite.Constants; -import fr.cemagref.simexplorer.is.entities.composite.Descriptors; -import fr.cemagref.simexplorer.is.entities.composite.ExplorationDatas; -import fr.cemagref.simexplorer.is.entities.composite.Libraries; -import fr.cemagref.simexplorer.is.entities.composite.SimpleComposite; -import fr.cemagref.simexplorer.is.entities.composite.Structures; -import fr.cemagref.simexplorer.is.entities.data.Code; -import fr.cemagref.simexplorer.is.entities.data.Component; -import fr.cemagref.simexplorer.is.entities.data.Constant; -import fr.cemagref.simexplorer.is.entities.data.ConstantValue; -import fr.cemagref.simexplorer.is.entities.data.Descriptor; -import fr.cemagref.simexplorer.is.entities.data.ExplorationApplication; -import fr.cemagref.simexplorer.is.entities.data.ExplorationData; -import fr.cemagref.simexplorer.is.entities.data.Library; -import fr.cemagref.simexplorer.is.entities.data.LoggableElement; -import fr.cemagref.simexplorer.is.entities.data.Result; -import fr.cemagref.simexplorer.is.entities.metadata.MetaData; -import fr.cemagref.simexplorer.is.entities.metadata.Version; -import fr.cemagref.simexplorer.is.factories.ContentTypeFactory; - -/** - * The Class ElementGenerator. - */ -public class ElementGenerator { - - /** - * The Class RandomStream. - */ - public class RandomStream { - - /** The md5. */ - private String md5; - - /** The data. */ - private byte[] data; - - /** - * Gets the stream. - * - * @return the stream - */ - public InputStream getStream() { - InputStream is; - is = new ByteArrayInputStream(data); - return is; - } - - /** - * Gets the md5. - * - * @return the md5 - */ - public String getMd5() { - return md5; - } - - /** - * Sets the md5. - * - * @param md5 the new md5 - */ - public void setMd5(String md5) { - this.md5 = md5; - } - - /** - * Sets the data. - * - * @param bytes the new data - */ - public void setData(byte[] bytes) { - data = bytes; - } - - } - - /** The r. */ - private Random r = new Random(); - - /** Number of words. */ - private static int cs; - - /** The counter. */ - private static int counter = 0; - - /** The randomstrings. */ - private static String[] randomstrings; - - /** - * Instantiates a new element generator. - */ - public ElementGenerator() { - super(); - initializeStrings(); - } - - /** - * Short string. - * - * @return the string - */ - private String shortString() { - return RandomStringUtils.randomAlphabetic(r.nextInt(5) + 5); - } - - /** - * Hash. - * - * @return the string - */ - private String hash() { - return RandomStringUtils.randomAlphanumeric(32); - } - - /** - * Initialize strings. - */ - private void initializeStrings() { - cs = 150000; - randomstrings = new String[cs]; - for (int i = 0; i < randomstrings.length; i++) { - randomstrings[i] = shortString(); - } - } - - /** - * Update. - * - * @param element the element - * - * @throws Exception the exception - */ - private void update(LoggableElement element) throws Exception { - MetaData metaData = new MetaData(); - metaData.setUuid(UUID.randomUUID().toString()); - element.setName(element.getClass().getSimpleName() + "-" + counter++); - - StringBuffer sb = new StringBuffer(""); - - for (int i = 0, max = 3 + r.nextInt(5); i < max; i++) { - sb.append(randomstrings[r.nextInt(cs)]).append(" "); - } - element.setDescription(sb.toString()); - - Version v = new Version("0"); - - for (int i = 0, max = 2 + r.nextInt(2); i < max; i++) { - v.setVersion(i, r.nextInt(5)); - } - metaData.setVersion(v.toString()); - metaData.setCreationDate(new Date()); - metaData.setHash(shortString()); - metaData.setLatest(true); - - Descriptors descriptors = new Descriptors(); - for (int i = 0, max = 1 + r.nextInt(12); i < max; i++) { - descriptors.add(new Descriptor(shortString(), shortString())); - } - element.setDescriptors(descriptors); - - if (element instanceof Library || element instanceof ExplorationData) { - // generate attachments - for (int i = 0, max = r.nextInt(15); i < max; i++) { - element.getAttachments().add(generateAttachment()); - } - } - - element.setMetaData(metaData); - } - - /** - * Generate composite. - * - * @param <SC> Simple composite wanted - * @param <T> Class of simple composite children - * - * @param compositeClass the composite class - * - * @return the sC - * - * @throws Exception the exception - */ - public <T extends Entity, SC extends SimpleComposite<T>> SC generateComposite(Class<SC> compositeClass) throws Exception { - SC elements = compositeClass.newInstance(); - Class<T> elementClass = elements.getChildrenClass(); - int i = 3 + r.nextInt(2); - for (int j = 0; j < i; j++) { - T element = elementClass.newInstance(); - if (element instanceof LoggableElement) { - update((LoggableElement) element); - } - elements.add(element); - } - return elements; - } - - /** - * Generate random ea. - * - * @return the exploration application - * - * @throws Exception the exception - */ - public ExplorationApplication generateRandomEA() throws Exception { - ExplorationApplication ea = new ExplorationApplication(); - update(ea); - - ExplorationDatas explorationDatas = generateComposite(ExplorationDatas.class); - for (ExplorationData object : explorationDatas) { - processExplorationData(object); - } - ea.setExplorations(explorationDatas); - - Components components = generateComposite(Components.class); - for (Component object : components) { - processComponent(object); - } - ea.setComponents(components); - return ea; - } - - /** - * Process component. - * - * @param component the component - * - * @throws Exception the exception - */ - private void processComponent(Component component) throws Exception { - Constants constants = generateComposite(Constants.class); - for (Constant constant : constants) { - processConstant(constant); - } - component.setConstants(constants); - - component.setStructures(new Structures()); - - Codes codes = generateComposite(Codes.class); - for (Code code : codes) { - processCode(code); - } - component.setCodes(codes); - - Libraries libraries = generateComposite(Libraries.class); - for (Library library : libraries) { - processLibrary(library); - } - component.setLibraries(libraries); - - } - - /** - * Generate attachment. - * - * @return the attachment - * - * @throws Exception the exception - */ - private Attachment generateAttachment() throws Exception { - Attachment attachment = new Attachment(); - attachment.setContentType(ContentTypeFactory.getContentTypeInstance("RawType")); - attachment.setDataHash(hash()); - attachment.setFileName(shortString() + ".txt"); - return attachment; - } - - /** - * Process library. - * - * @param library the library - * - * @throws Exception the exception - */ - private void processLibrary(Library library) throws Exception { - // library.getMetaData().getAttachments().add(generateAttachment()); - } - - /** - * Process code. - * - * @param code the code - */ - private void processCode(Code code) { - code.setCode(shortString()); - code.setLanguage(shortString()); - } - - /** - * Process constant. - * - * @param constant the constant - */ - private void processConstant(Constant constant) { - constant.setName(shortString()); - constant.setType(String.class); - } - - /** - * Process exploration data. - * - * @param explorationData the exploration data - * - * @throws Exception the exception - */ - private void processExplorationData(ExplorationData explorationData) throws Exception { - Result result = new Result(); - explorationData.setResult(result); - // explorationData.getMetaData().getAttachments().add(generateAttachment()); - - ConstantValues constantValues = generateComposite(ConstantValues.class); - for (ConstantValue constantValue : constantValues) { - processConstantValue(constantValue); - } - explorationData.setConstantValues(constantValues); - - } - - /** - * Process constant value. - * - * @param constantValue the constant value - */ - private void processConstantValue(ConstantValue constantValue) { - Constant constant = new Constant(); - constant.setName(shortString()); - constant.setType(Integer.class); - - constantValue.setConstant(constant); - constantValue.setValue(Integer.toString(r.nextInt())); - } - - /** - * Generate text stream. - * - * @return the input stream - * - * @throws Exception the exception - */ - public RandomStream generateTextStream() throws Exception { - RandomStream rs = new RandomStream(); - - int wordcount = 300 + r.nextInt(300); - StringBuffer sb = new StringBuffer(); - for (int i = 0; i < wordcount; i++) { - sb.append(randomstrings[r.nextInt(cs)]).append(" "); - } - - MD5 md5 = new MD5(); - md5.Update(sb.toString().getBytes()); - rs.setMd5(md5.asHex()); - - rs.setData(sb.toString().getBytes()); - - return rs; - } -}