r1990 - in isis-fish/trunk/src/main/java/fr/ifremer/isisfish: datastore export simulator
Author: chatellier Date: 2009-03-17 17:26:32 +0000 (Tue, 17 Mar 2009) New Revision: 1990 Modified: isis-fish/trunk/src/main/java/fr/ifremer/isisfish/datastore/ExportStorage.java isis-fish/trunk/src/main/java/fr/ifremer/isisfish/export/Export.java isis-fish/trunk/src/main/java/fr/ifremer/isisfish/export/ExportHelper.java isis-fish/trunk/src/main/java/fr/ifremer/isisfish/export/SensitivityExport.java isis-fish/trunk/src/main/java/fr/ifremer/isisfish/simulator/SimulationParameter.java Log: Gestion des parametres des exports. Modified: isis-fish/trunk/src/main/java/fr/ifremer/isisfish/datastore/ExportStorage.java =================================================================== --- isis-fish/trunk/src/main/java/fr/ifremer/isisfish/datastore/ExportStorage.java 2009-03-17 17:22:24 UTC (rev 1989) +++ isis-fish/trunk/src/main/java/fr/ifremer/isisfish/datastore/ExportStorage.java 2009-03-17 17:26:32 UTC (rev 1990) @@ -19,25 +19,29 @@ package fr.ifremer.isisfish.datastore; +import static org.codelutin.i18n.I18n._; + +import java.io.File; +import java.lang.reflect.Field; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; + +import org.apache.commons.collections.map.ReferenceMap; +import org.apache.commons.lang.ObjectUtils; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +import fr.ifremer.isisfish.IsisFish; import fr.ifremer.isisfish.IsisFishException; import fr.ifremer.isisfish.IsisFishRuntimeException; -import fr.ifremer.isisfish.IsisFish; import fr.ifremer.isisfish.export.Export; +import fr.ifremer.isisfish.rule.Rule; import fr.ifremer.isisfish.util.Doc; import fr.ifremer.isisfish.util.DocHelper; import fr.ifremer.isisfish.util.Docable; import fr.ifremer.isisfish.vcs.VCSException; -import static org.codelutin.i18n.I18n._; - -import org.apache.commons.collections.map.ReferenceMap; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; - -import java.io.File; -import java.util.List; -import java.util.Map; - /** * Gestion des fichers VCS de type {@link Export} * (appartenant au module exports). @@ -50,19 +54,21 @@ * Last update: $Date$ * by : $Author$ */ -public class ExportStorage extends JavaSourceStorage implements Docable { +public class ExportStorage extends JavaSourceStorage implements Docable { - /** to use log facility, just put in your code: log.info(\"...\"); */ + /** Class logger. */ private static Log log = LogFactory.getLog(ExportStorage.class); - + + /** Prefix des noms de paramètres. */ + private final static String PARAM_PREFIX = "param_"; + public static final String EXPORT_PATH = "exports"; /** Template freemarker pour les scripts d'export . */ public static final String EXPORT_TEMPLATE = "templates/script/export.ftl"; - + @SuppressWarnings("unchecked") - private static Map<String, ExportStorage> scriptsCache = - (Map<String, ExportStorage>) new ReferenceMap(); + private static Map<String, ExportStorage> scriptsCache = (Map<String, ExportStorage>) new ReferenceMap(); /** * Constructeur. @@ -81,7 +87,7 @@ result.mkdirs(); return result; } - + /** * Retourne une nouvelle instance de la regle. Compile le fichier si besoin * @@ -99,7 +105,7 @@ * @param name le nom de la regle souhaitée * @return Le storage pour la regle */ - static public ExportStorage getExport(String name) { + public static ExportStorage getExport(String name) { ExportStorage result = scriptsCache.get(name); if (result == null) { result = new ExportStorage(IsisFish.config.getDatabaseDirectory(), @@ -114,12 +120,12 @@ * * @return la liste des noms de toutes les régions disponible en local */ - static public List<String> getExportNames() { + public static List<String> getExportNames() { File dir = getExportDirectory(); return getStorageNames(dir); } - static public void checkout() throws VCSException { + public static void checkout() throws VCSException { checkout(IsisFish.config.getDatabaseDirectory(), EXPORT_PATH); } @@ -130,17 +136,17 @@ * @see Docable */ public Doc getClassDoc() { - Doc result=null; + Doc result = null; try { - Class klazz = getCodeClass(); + Class<?> klazz = getCodeClass(); result = DocHelper.getClassDoc(klazz); } catch (IsisFishException e) { - log.warn(_("isisfish.error.not.found.code",this)); + log.warn(_("isisfish.error.not.found.code", this)); } return result; } - /** + /** * @param fieldName name of the field to inspect * @return the @Doc of the given field of the underlied Rule class * @see DocHelper @@ -148,21 +154,90 @@ * @see Docable */ public Doc getFieldDoc(String fieldName) { - throw new IsisFishRuntimeException(_("isisfish.error.not.found.class" + - "support Docable#getFieldDoc(String) method %1$s",this)); + throw new IsisFishRuntimeException(_("isisfish.error.not.found.class" + + "support Docable#getFieldDoc(String) method %1$s", this)); } - /** - * <b>Be ware this method require to instanciate a Rule, so + /** + * <b>Be ware this method require to instanciate a Export, so * it would be better to call as often as possible.</b> * - * @return the descript of the instanciate AnalysePlan + * @return the description of the instanciate Export * @see Doc * @see Docable */ public String getDescription() { - throw new IsisFishRuntimeException(_("isisfish.error.not.support.class" + - "Docable#getDescription() method %1$s",this)); + throw new IsisFishRuntimeException(_("isisfish.error.not.support.class" + + "Docable#getDescription() method %1$s", this)); } + /** + * Recherche par introspection tous les parametres de la classe + * commencant par {@link #PARAM_PREFIX}. + * + * @param export the export to inspect + * @return the list of parameters for a given export + */ + public static Map<String, Class<?>> getParameterNames(Export export) { + Map<String, Class<?>> result = new LinkedHashMap<String, Class<?>>(); + for (Field field : export.getClass().getFields()) { + if (field.getName().startsWith(PARAM_PREFIX)) { + result.put(field.getName().substring(PARAM_PREFIX.length()), + field.getType()); + } + } + return result; + } + + /** + * Donne la valeur d'un parametre par introspection. + * + * @param name le nom du parametre + * @param export the export to inspect + * @return la valeur courante du parametre + * @throws IsisFishException if any exception + */ + public static Object getParameterValue(Export export, String name) throws IsisFishException { + if (export == null || name == null || "".equals(name)) { + return null; + } + try { + String fieldName = PARAM_PREFIX + name; + Field field = export.getClass().getDeclaredField(fieldName); + return field.get(export); + } catch (IllegalAccessException eee) { + throw new IsisFishException("Can't get rule parameter: " + name, eee); + } catch (NoSuchFieldException eee) { + throw new IsisFishException("Can't get rule parameter: " + name, eee); + } + } + + /** + * Modifie la valeur d'un attribut par introspection. + * + * @param name le nom de l'attribut + * @param value la valeur de l'attribut + * @param export the export to inspect + * @throws IsisFishException if any exception + */ + public static void setParameterValue(Export export, String name, + Object value) throws IsisFishException { + try { + String fieldName = PARAM_PREFIX + name; + Field field = export.getClass().getDeclaredField(fieldName); + field.set(export, value); + } catch (IllegalAccessException eee) { + throw new IsisFishException("Can't modify export parameter: " + + name + " with '" + value + "'(" + + ObjectUtils.identityToString(value) + ")", eee); + } catch (NoSuchFieldException eee) { + throw new IsisFishException("Can't modify export parameter: " + + name + " with '" + value + "'(" + + ObjectUtils.identityToString(value) + ")", eee); + } catch (IllegalArgumentException eee) { + throw new IsisFishException("Can't modify export parameter: " + + name + " with '" + value + "'(" + + ObjectUtils.identityToString(value) + ")", eee); + } + } } Modified: isis-fish/trunk/src/main/java/fr/ifremer/isisfish/export/Export.java =================================================================== --- isis-fish/trunk/src/main/java/fr/ifremer/isisfish/export/Export.java 2009-03-17 17:22:24 UTC (rev 1989) +++ isis-fish/trunk/src/main/java/fr/ifremer/isisfish/export/Export.java 2009-03-17 17:26:32 UTC (rev 1990) @@ -1,5 +1,5 @@ /* *##% - * Copyright (C) 2006 + * Copyright (C) 2006 - 2009 * Code Lutin, Cédric Pineau, Benjamin Poussin * * This program is free software; you can redistribute it and/or @@ -17,68 +17,59 @@ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *##%*/ -/* * - * Export.java - * - * Created: 22 janv. 2006 21:12:58 - * - * @author poussin - * @version $Revision$ - * - * Last update: $Date$ - * by : $Author$ - */ - package fr.ifremer.isisfish.export; import java.io.Writer; import fr.ifremer.isisfish.datastore.SimulationStorage; - /** * Interface que doivent implanter les classes d'export de resultats * * Le nom du fichier d'export qui sera utilisé sera le basé sur le nom de la * classe d'export * + * Created: 22 janv. 2006 21:12:58 + * * @author poussin + * @version $Revision$ * + * Last update: $Date$ + * by : $Author$ */ - public interface Export { - + /** * @return the necessaryResult name */ public String[] getNecessaryResult(); - + /** * Return Export description * @return string displayable to the end user */ public String getDescription(); - + /** * Return filename used to contains export data * @return filename by example "myexport" */ public String getExportFilename(); - + /** * Return extension used as filename name extension * @return extension by example ".csv" */ public String getExtensionFilename(); - + /** * Exporte les resultat * * @param simulation la simulation dont on souhaite exporter les resultats * @param out la sortie sur lequel il faut ecrire l'export + * @throws Exception if export fail */ - public void export(SimulationStorage simulation, Writer out) throws Exception; - -} + public void export(SimulationStorage simulation, Writer out) + throws Exception; - +} Modified: isis-fish/trunk/src/main/java/fr/ifremer/isisfish/export/ExportHelper.java =================================================================== --- isis-fish/trunk/src/main/java/fr/ifremer/isisfish/export/ExportHelper.java 2009-03-17 17:22:24 UTC (rev 1989) +++ isis-fish/trunk/src/main/java/fr/ifremer/isisfish/export/ExportHelper.java 2009-03-17 17:26:32 UTC (rev 1990) @@ -1,5 +1,5 @@ /* *##% - * Copyright (C) 2006 + * Copyright (C) 2006 - 2009 * Code Lutin, Cédric Pineau, Benjamin Poussin * * This program is free software; you can redistribute it and/or @@ -17,18 +17,6 @@ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *##%*/ -/* * - * ExportHelper.java - * - * Created: 20 janv. 2006 01:52:04 - * - * @author poussin - * @version $Revision$ - * - * Last update: $Date$ - * by : $Author$ - */ - package fr.ifremer.isisfish.export; import java.io.BufferedWriter; @@ -38,33 +26,44 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Properties; +import org.apache.commons.beanutils.ConvertUtilsBean; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.codelutin.topia.persistence.TopiaEntity; import fr.ifremer.isisfish.datastore.ExportStorage; +import fr.ifremer.isisfish.datastore.RegionStorage; import fr.ifremer.isisfish.datastore.SimulationStorage; +import fr.ifremer.isisfish.util.ConverterUtil; - /** + * Export helper. + * + * Created: 20 janv. 2006 01:52:04 + * * @author poussin + * @version $Revision$ * + * Last update: $Date$ + * by : $Author$ */ - public class ExportHelper { - /** to use log facility, just put in your code: log.info(\"...\"); */ - static private Log log = LogFactory.getLog(ExportHelper.class); + /** Class logger. */ + private static Log log = LogFactory.getLog(ExportHelper.class); /** - * Permet de faire les exports pour une simulation + * Permet de faire les exports pour une simulation. * * @param simulation La simulation pour lequel il faut faire les exports * @param destdir le repertoire dans lequel il faut ecrire les exports * @param exportNames le nom des exports a faire * @param compileDir le nom du répertoire ou les classes d'export sont compilées */ - static public void doExport(SimulationStorage simulation, File destdir, List<String> exportNames, File compileDir) { + public static void doExport(SimulationStorage simulation, File destdir, + List<String> exportNames, File compileDir) { Map<String, Integer> usingFilename = new HashMap<String, Integer>(); long timeStart = System.currentTimeMillis(); for (String exportName : exportNames) { @@ -72,10 +71,10 @@ try { ExportStorage storage = ExportStorage.getExport(exportName); Export export = storage.getNewExportInstance(); - + String filename = export.getExportFilename(); String extension = export.getExtensionFilename(); - + // prevent two export with same name // name MyExport.csv become MyExport_1.csv Integer val = usingFilename.get(filename + extension); @@ -85,7 +84,7 @@ } else { usingFilename.put(filename + extension, 1); } - + File file = new File(destdir, filename + extension); Writer out = new BufferedWriter(new FileWriter(file)); export.export(simulation, out); @@ -95,10 +94,50 @@ log.warn("Can't export object: " + exportName, eee); } } - simulation.getInformation().addExportTime(exportName, System.currentTimeMillis()-time); + simulation.getInformation().addExportTime(exportName, + System.currentTimeMillis() - time); } - simulation.getInformation().addExportTime("All export", System.currentTimeMillis()-timeStart); + simulation.getInformation().addExportTime("All export", + System.currentTimeMillis() - timeStart); } -} + /** + * Recupere dans prop les valeurs des champs specifique au export et met a + * jour les champs de l'export. + * + * @param exportIndex l'index de l'export + * @param export l'export a mettre à jour + * @param region le topia context dont on a besoin + * @param props les proprietes contenant les parametre de l'export + */ + public static void populateExport(int exportIndex, RegionStorage region, + Export export, Properties props) { + ConvertUtilsBean beanUtils = ConverterUtil.getConverter(region + .getStorage()); + String exportName = ExportStorage.getName(export); + String paramTag = "export." + exportIndex + ".parameter."; + for (Map.Entry<String, Class<?>> entry : ExportStorage + .getParameterNames(export).entrySet()) { + String propName = entry.getKey(); + Class<?> type = entry.getValue(); + if (TopiaEntity.class.isAssignableFrom(type)) { + type = TopiaEntity.class; + } + String valueString = "not initialized"; + try { + valueString = props.getProperty(paramTag + propName); + Object value = beanUtils.convert(valueString, type); + log.info("DEBUG: set export param: " + paramTag + propName + + " = " + value + "(" + valueString + ")"); + ExportStorage.setParameterValue(export, propName, value); + } catch (Exception eee) { + if (log.isWarnEnabled()) { + log.info("Properties: " + props); + log.warn("Can't reload field " + propName + " for export " + + exportName + " with value " + valueString, eee); + } + } + } + } +} Modified: isis-fish/trunk/src/main/java/fr/ifremer/isisfish/export/SensitivityExport.java =================================================================== --- isis-fish/trunk/src/main/java/fr/ifremer/isisfish/export/SensitivityExport.java 2009-03-17 17:22:24 UTC (rev 1989) +++ isis-fish/trunk/src/main/java/fr/ifremer/isisfish/export/SensitivityExport.java 2009-03-17 17:26:32 UTC (rev 1990) @@ -1,14 +1,33 @@ -/** - * - */ +/* *##% + * Copyright (C) 2009 Ifremer, Code Lutin + * + * 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.ifremer.isisfish.export; /** * Interface that need to be implemented by the sensitivity result export classes * * The export filename that will be used will be based on the export class name. + * * @author couteau + * @version $Revision$ * + * Last update: $Date$ + * by : $Author$ */ public interface SensitivityExport extends Export { Property changes on: isis-fish/trunk/src/main/java/fr/ifremer/isisfish/export/SensitivityExport.java ___________________________________________________________________ Name: svn:keywords + Revision Date Author Modified: isis-fish/trunk/src/main/java/fr/ifremer/isisfish/simulator/SimulationParameter.java =================================================================== --- isis-fish/trunk/src/main/java/fr/ifremer/isisfish/simulator/SimulationParameter.java 2009-03-17 17:22:24 UTC (rev 1989) +++ isis-fish/trunk/src/main/java/fr/ifremer/isisfish/simulator/SimulationParameter.java 2009-03-17 17:26:32 UTC (rev 1990) @@ -46,6 +46,7 @@ import fr.ifremer.isisfish.IsisFishDAOHelper; import fr.ifremer.isisfish.IsisFishException; import fr.ifremer.isisfish.datastore.AnalysePlanStorage; +import fr.ifremer.isisfish.datastore.ExportStorage; import fr.ifremer.isisfish.datastore.RegionStorage; import fr.ifremer.isisfish.datastore.RuleStorage; import fr.ifremer.isisfish.datastore.SensitivityStorage; @@ -53,6 +54,9 @@ import fr.ifremer.isisfish.entities.PopulationDAO; import fr.ifremer.isisfish.entities.Strategy; import fr.ifremer.isisfish.entities.StrategyDAO; +import fr.ifremer.isisfish.export.Export; +import fr.ifremer.isisfish.export.ExportHelper; +import fr.ifremer.isisfish.export.SensitivityExport; import fr.ifremer.isisfish.rule.Rule; import fr.ifremer.isisfish.rule.RuleHelper; import fr.ifremer.isisfish.simulator.sensitivity.SensitivityCalculator; @@ -140,7 +144,7 @@ protected int analysePlanNumber = -1; /** Export utilisés pour les analyses de sensibilités. */ - protected List<String> sensitivityExportNames = new ArrayList<String>(); + protected List<SensitivityExport> sensitivityExport = new ArrayList<SensitivityExport>(); /** * Nombre de simulation constituant l'analyse de sensibilité. @@ -275,11 +279,13 @@ result.setProperty("plans", planList); String exportList = ""; + int exportIndex = 0; for (String export : getExportNames()) { exportList += export + ","; + } result.setProperty("exports", exportList); - + // number of sensitivity simulation result.setProperty("numberOfSensitivitySimulation", String.valueOf(numberOfSensitivitySimulation)); @@ -290,8 +296,11 @@ } // number of sensitivity simulation String sensitivityExportList = ""; - for (String export : getSensitivityExportNames()) { + // WARN only SENSITIVITY are parametrable for now + for (SensitivityExport export : getSensitivityExport()) { sensitivityExportList += export + ","; + Properties exportProp = exportParametertoProperties(exportIndex++, export); + result.putAll(exportProp); } result.setProperty("sensitivityexports", sensitivityExportList); @@ -436,6 +445,7 @@ } String[] exportList = props.getProperty("exports", "").split(","); + int exportIndex = 0; for (String name : exportList) { if (name != null && !"".equals(name)) { getExportNames().add(name); @@ -459,8 +469,16 @@ // sensitivity export String[] sensitivityExportList = props.getProperty("sensitivityexports", "").split(","); for (String name : sensitivityExportList) { - if (!StringUtils.isEmpty(name)) { - getSensitivityExportNames().add(name); + try { + if (!StringUtils.isEmpty(name)) { + SensitivityExport export = (SensitivityExport)ExportStorage.getExport(name).getNewExportInstance(); + ExportHelper.populateExport(exportIndex++, getRegion(), export, props); + getSensitivityExport().add(export); + } + } catch (IsisFishException eee) { + if (log.isWarnEnabled()) { + log.warn("Can't find SensitivityExport: " + name, eee); + } } } @@ -533,7 +551,7 @@ } /** - * Permet de mettre les parametres de la regle sont une forme String pour + * Permet de mettre les parametres de la regle sous une forme String pour * pouvoir les relire ensuite. * * @param ruleIndex l'index de la rule @@ -556,7 +574,7 @@ } } catch (IsisFishException eee) { if (log.isWarnEnabled()) { - log.warn("Can't convert parametre value to String: " + log.warn("Can't convert parameter value to String: " + paramName, eee); } } @@ -564,7 +582,40 @@ return result; } + + /** + * Permet de mettre les parametres de l'export sous une forme String pour + * pouvoir les relire ensuite. + * + * @param exportIndex l'index de l'export + * @param export L'export dont on souhaite mettre les parametres dans l'objet + * Properties retourne + * @return L'objet Properties contenant les valeurs des parametres de la + * regle + */ + protected Properties exportParametertoProperties(int exportIndex, Export export) { + Properties result = new Properties(); + ConvertUtilsBean beanUtils = ConverterUtil.getConverter(getRegion().getStorage()); + for (String paramName : ExportStorage.getParameterNames(export).keySet()) { + String paramValueString; + try { + Object value = ExportStorage.getParameterValue(export, paramName); + paramValueString = beanUtils.convert(value); + if (paramValueString != null) { + result.setProperty("export." + exportIndex + ".parameter." + + paramName, paramValueString); + } + } catch (IsisFishException eee) { + if (log.isWarnEnabled()) { + log.warn("Can't convert parameter value to String: " + + paramName, eee); + } + } + } + return result; + } + /** * Permet de mettre les parametres du plan sous une forme String pour * pouvoir les relire ensuite. @@ -815,18 +866,18 @@ /** * @return the sensitivityExportNames */ - public List<String> getSensitivityExportNames() { - if (sensitivityExportNames == null) { - sensitivityExportNames = new ArrayList<String>(); + public List<SensitivityExport> getSensitivityExport() { + if (sensitivityExport == null) { + sensitivityExport = new ArrayList<SensitivityExport>(); } - return sensitivityExportNames; + return sensitivityExport; } /** - * @param sensitivityExportNames the sensitivityExportNames to set + * @param sensitivityExport the sensitivityExportNames to set */ - public void setSensitivityExportNames(List<String> sensitivityExportNames) { - this.sensitivityExportNames = sensitivityExportNames; + public void setSensitivityExport(List<SensitivityExport> sensitivityExport) { + this.sensitivityExport = sensitivityExport; } /** @@ -1056,7 +1107,7 @@ result.scriptLogLevel= scriptLogLevel; result.libLogLevel= libLogLevel; result.numberOfSensitivitySimulation = numberOfSensitivitySimulation; - result.sensitivityExportNames = sensitivityExportNames; + result.sensitivityExport= sensitivityExport; result.sensitivityCalculator = sensitivityCalculator; return result;
participants (1)
-
chatellier@users.labs.libre-entreprise.org