Author: bpoussin Date: 2014-07-24 17:07:49 +0200 (Thu, 24 Jul 2014) New Revision: 4067 Url: http://forge.codelutin.com/projects/isis-fish/repository/revisions/4067 Log: replace string with const and move some code in helper Modified: trunk/src/main/java/fr/ifremer/isisfish/simulator/SimulationParameterImpl.java trunk/src/main/java/fr/ifremer/isisfish/simulator/SimulationParameterPropertiesHelper.java Modified: trunk/src/main/java/fr/ifremer/isisfish/simulator/SimulationParameterImpl.java =================================================================== --- trunk/src/main/java/fr/ifremer/isisfish/simulator/SimulationParameterImpl.java 2014-07-24 14:44:06 UTC (rev 4066) +++ trunk/src/main/java/fr/ifremer/isisfish/simulator/SimulationParameterImpl.java 2014-07-24 15:07:49 UTC (rev 4067) @@ -54,7 +54,6 @@ import fr.ifremer.isisfish.IsisConfig; import fr.ifremer.isisfish.IsisFishDAOHelper; -import fr.ifremer.isisfish.IsisFishException; import fr.ifremer.isisfish.datastore.ExportStorage; import fr.ifremer.isisfish.datastore.ObjectiveStorage; import fr.ifremer.isisfish.datastore.OptimizationStorage; @@ -76,12 +75,13 @@ import fr.ifremer.isisfish.rule.RuleHelper; import fr.ifremer.isisfish.simulator.sensitivity.SensitivityAnalysis; import fr.ifremer.isisfish.util.ConverterUtil; -import fr.ifremer.isisfish.util.StringConverter; -import fr.ifremer.isisfish.util.TopiaEntityConverter; +import java.util.Collections; import org.apache.commons.beanutils.ConvertUtilsBean; import static org.nuiton.i18n.I18n.n; +import static fr.ifremer.isisfish.simulator.SimulationParameterPropertiesHelper.DOT; + /** * Real {@link SimulationParameter} implementation. * @@ -733,7 +733,7 @@ if (propertiesParameters != null) { // exports - String[] exportList = propertiesParameters.getProperty("exports", "").split(","); + String[] exportList = SimulationParameterPropertiesHelper.getExportNames(propertiesParameters); for (String name : exportList) { if (name != null && !"".equals(name)) { exportNames.add(name); @@ -761,8 +761,8 @@ if (numberOfSensitivitySimulation == null) { if (propertiesParameters != null) { - numberOfSensitivitySimulation = Integer.valueOf(propertiesParameters.getProperty( - "numberOfSensitivitySimulation", "-1")); + numberOfSensitivitySimulation = + SimulationParameterPropertiesHelper.getNumberOfSensitivitySimulation(propertiesParameters); } else { numberOfSensitivitySimulation = -1; } @@ -925,7 +925,7 @@ if (VersionUtil.smallerThan(getIsisFishVersion(), "4.3.0.0")) { useOptimization = Boolean.FALSE; } else { - useOptimization = Boolean.valueOf(propertiesParameters.getProperty("useOptimization", "false")); + useOptimization = SimulationParameterPropertiesHelper.getUseOptimization(propertiesParameters); } } else { useOptimization = Boolean.FALSE; @@ -1046,7 +1046,7 @@ if (useSimulationPlan == null) { if (propertiesParameters != null) { - useSimulationPlan = Boolean.valueOf(propertiesParameters.getProperty("useSimulationPlan", "false")); + useSimulationPlan = SimulationParameterPropertiesHelper.getUseSimulationPlan(propertiesParameters); } else { useSimulationPlan = Boolean.FALSE; } @@ -1071,7 +1071,7 @@ if (simulationPlanNumber == null) { if (propertiesParameters != null) { - simulationPlanNumber = Integer.valueOf(propertiesParameters.getProperty("simulationPlanNumber", "-1")); + simulationPlanNumber = SimulationParameterPropertiesHelper.getSimulationPlanNumber(propertiesParameters); } else { simulationPlanNumber = -1; } @@ -1093,7 +1093,7 @@ if (sensitivityAnalysisOnlyKeepFirst == null) { if (propertiesParameters != null) { - sensitivityAnalysisOnlyKeepFirst = Boolean.valueOf(propertiesParameters.getProperty("sensitivityAnalysisOnlyKeepFirst", "-1")); + sensitivityAnalysisOnlyKeepFirst = SimulationParameterPropertiesHelper.isSensitivityAnalysisOnlyKeepFirst(propertiesParameters); } else { sensitivityAnalysisOnlyKeepFirst = Boolean.FALSE; } @@ -1141,7 +1141,7 @@ if (usePreScript == null) { if (propertiesParameters != null) { - usePreScript = Boolean.valueOf(propertiesParameters.getProperty("usePreScript", "false")); + usePreScript = SimulationParameterPropertiesHelper.getUsePreScript(propertiesParameters); } else { usePreScript = Boolean.FALSE; } @@ -1167,7 +1167,7 @@ if (preScript == null) { if (propertiesParameters != null) { - preScript = propertiesParameters.getProperty("preScript", ""); + preScript = SimulationParameterPropertiesHelper.getPreScript(propertiesParameters); } else { preScript = ""; } @@ -1190,7 +1190,7 @@ if (generatedPreScript == null) { if (propertiesParameters != null) { - generatedPreScript = propertiesParameters.getProperty("generatedPreScript", ""); + generatedPreScript = SimulationParameterPropertiesHelper.getGeneratedPreScript(propertiesParameters); } else { generatedPreScript = ""; } @@ -1266,12 +1266,9 @@ resultEnabled = new LinkedList<String>(); if (propertiesParameters != null) { - String[] resultList = propertiesParameters.getProperty("resultNames", "").split(","); - for (String name : resultList) { - if (name != null && !"".equals(name)) { - resultEnabled.add(name); - } - } + String[] resultList = + SimulationParameterPropertiesHelper.getResultNames(propertiesParameters); + Collections.addAll(resultEnabled, resultList); } } return resultEnabled; @@ -1292,16 +1289,10 @@ public Map<String, String> getTagValue() { if (tagValue == null) { - tagValue = new LinkedHashMap<String, String>(); - if (propertiesParameters != null) { - for (Object k : propertiesParameters.keySet()) { - if (k.toString().startsWith("tagValue.")) { - String key = k.toString().substring("tagValue.".length()); - String value = propertiesParameters.getProperty(k.toString()); - getTagValue().put(key, value); - } - } + tagValue = SimulationParameterPropertiesHelper.getTagValue(propertiesParameters); + } else { + tagValue = new LinkedHashMap<String, String>(); } } @@ -1325,7 +1316,7 @@ if (simulLogLevel == null) { if (propertiesParameters != null) { - simulLogLevel = propertiesParameters.getProperty("simulLogLevel", "info"); + simulLogLevel = SimulationParameterPropertiesHelper.getSimulLogLevel(propertiesParameters); } else { simulLogLevel = "info"; } @@ -1354,8 +1345,7 @@ if (scriptLogLevel == null) { if (propertiesParameters != null) { - scriptLogLevel = propertiesParameters.getProperty( - "scriptLogLevel", "info"); + scriptLogLevel = SimulationParameterPropertiesHelper.getScriptLogLevel(propertiesParameters); } else { scriptLogLevel = "info"; } @@ -1383,7 +1373,7 @@ public String getLibLogLevel() { if (libLogLevel == null) { if (propertiesParameters != null) { - libLogLevel = propertiesParameters.getProperty("libLogLevel", "error"); + libLogLevel = SimulationParameterPropertiesHelper.getLibLogLevel(propertiesParameters); } else { libLogLevel = "error"; } @@ -1614,25 +1604,29 @@ public Properties toProperties() { Properties result = new SortedProperties(); - result.setProperty("isisFishVersion", getIsisFishVersion()); - result.setProperty("description", getDescription()); - result.setProperty("regionName", getRegionName()); - result.setProperty("numberOfYear", String.valueOf(getNumberOfYear())); - result.setProperty("simulatorName", getSimulatorName()); - result.setProperty("useCache", String.valueOf(getUseCache())); - result.setProperty("useStatistic", String.valueOf(getUseStatistic())); + result.setProperty(SimulationParameterPropertiesHelper.ISIS_FISH_VERSION_KEY, getIsisFishVersion()); + result.setProperty(SimulationParameterPropertiesHelper.DESCRIPTION_KEY, getDescription()); + result.setProperty(SimulationParameterPropertiesHelper.REGION_NAME_KEY, getRegionName()); + result.setProperty(SimulationParameterPropertiesHelper.NUMBER_OF_YEAR_KEY, String.valueOf(getNumberOfYear())); + result.setProperty(SimulationParameterPropertiesHelper.SIMULATOR_NAME_KEY, getSimulatorName()); + result.setProperty(SimulationParameterPropertiesHelper.USE_CACHE_KEY, String.valueOf(getUseCache())); + result.setProperty(SimulationParameterPropertiesHelper.USE_STATISTIC_KEY, String.valueOf(getUseStatistic())); // strategies if (strategies != null) { String strategyList = ""; for (Strategy str : getStrategies()) { - strategyList += str.getName() + ","; + strategyList += str.getName() + SimulationParameterPropertiesHelper.LIST_SEPARATOR; } - result.setProperty("strategies", StringUtil.substring(strategyList, 0, -1)); + result.setProperty(SimulationParameterPropertiesHelper.STRATEGIES_KEY, + StringUtil.substring(strategyList, 0, -1)); } else { - if (propertiesParameters != null - && propertiesParameters.containsKey("strategies")) { - result.setProperty("strategies", propertiesParameters.getProperty("strategies")); + if (propertiesParameters != null) { + String strs = propertiesParameters.getProperty( + SimulationParameterPropertiesHelper.STRATEGIES_KEY); + if (strs != null) { + result.setProperty(SimulationParameterPropertiesHelper.STRATEGIES_KEY, strs); + } } } @@ -1643,18 +1637,20 @@ populationList += pop.getName() + ","; MatrixND number = getNumberOf(pop); String numberAsString = String.valueOf(number.toList()); - result.setProperty("population." + pop.getName() + ".number", numberAsString); + result.setProperty( + SimulationParameterPropertiesHelper.POPULATION_KEY+ DOT + pop.getName() + + DOT + SimulationParameterPropertiesHelper.NUMBER_KEY, numberAsString); } - result.setProperty("populations", StringUtil.substring( - populationList, 0, -1)); + result.setProperty(SimulationParameterPropertiesHelper.POPULATIONS_KEY, + StringUtil.substring(populationList, 0, -1)); } else { - if (propertiesParameters != null - && propertiesParameters.containsKey("populations")) { - result.setProperty("populations", propertiesParameters.getProperty("populations")); - for (String key : propertiesParameters.stringPropertyNames()) { - if (key.startsWith("population.")) { - result.setProperty(key, propertiesParameters.getProperty(key)); - } + if (propertiesParameters != null) { + String pops = propertiesParameters.getProperty( + SimulationParameterPropertiesHelper.POPULATIONS_KEY); + if (pops != null) { + result.setProperty(SimulationParameterPropertiesHelper.POPULATIONS_KEY, pops); + SimulationParameterPropertiesHelper.copy(propertiesParameters, result, + SimulationParameterPropertiesHelper.POPULATION_KEY + DOT); } } } @@ -1665,20 +1661,19 @@ int ruleIndex = 0; for (Rule rule : getRules()) { ruleList += RuleStorage.getName(rule) + ","; - Properties ruleProp = RuleHelper.getRuleAsProperties(ruleIndex++, getRegion().getStorage(), rule); + Properties ruleProp = RuleHelper.getRuleAsProperties( + ruleIndex++, getRegion().getStorage(), rule); result.putAll(ruleProp); } - result.setProperty("rules", ruleList); + result.setProperty(SimulationParameterPropertiesHelper.RULES_KEY, ruleList); } else { - if (propertiesParameters != null - && propertiesParameters.containsKey("rules")) { - result.setProperty("rules", propertiesParameters - .getProperty("rules")); - for (String key : propertiesParameters.stringPropertyNames()) { - if (key.startsWith("rule.")) { - result.setProperty(key, propertiesParameters - .getProperty(key)); - } + if (propertiesParameters != null) { + String rules = propertiesParameters.getProperty( + SimulationParameterPropertiesHelper.RULES_KEY); + if (rules != null) { + result.setProperty(SimulationParameterPropertiesHelper.RULES_KEY, rules); + SimulationParameterPropertiesHelper.copy(propertiesParameters, result, + SimulationParameterPropertiesHelper.RULE_KEY + DOT); } } } @@ -1690,19 +1685,18 @@ for (SimulationPlan plan : getSimulationPlans()) { planList += SimulationPlanStorage.getName(plan) + ","; Properties planProp = StorageHelper.getParamsAsProperties(planIndex++, - getRegion().getStorage(), plan, "plan"); + getRegion().getStorage(), plan, SimulationParameterPropertiesHelper.PLAN_KEY); result.putAll(planProp); } - result.setProperty("plans", planList); + result.setProperty(SimulationParameterPropertiesHelper.PLANS_KEY, planList); } else { - if (propertiesParameters != null - && propertiesParameters.containsKey("plans")) { - result.setProperty("plans", propertiesParameters - .getProperty("plans")); - for (String key : propertiesParameters.stringPropertyNames()) { - if (key.startsWith("plan.")) { - result.setProperty(key, propertiesParameters.getProperty(key)); - } + if (propertiesParameters != null) { + String plans = propertiesParameters.getProperty( + SimulationParameterPropertiesHelper.PLANS_KEY); + if (plans != null) { + result.setProperty(SimulationParameterPropertiesHelper.PLANS_KEY, plans); + SimulationParameterPropertiesHelper.copy(propertiesParameters, result, + SimulationParameterPropertiesHelper.PLAN_KEY + DOT); } } } @@ -1711,19 +1705,25 @@ if (objective != null) { String objectiveName = ObjectiveStorage.getName(objective); Properties objectiveProp = StorageHelper.getParamsAsProperties(0, - getRegion().getStorage(), objective, "objective"); + getRegion().getStorage(), objective, + SimulationParameterPropertiesHelper.OBJECTIVE_KEY); result.putAll(objectiveProp); - result.setProperty("objective", objectiveName); + result.setProperty(SimulationParameterPropertiesHelper.OBJECTIVE_KEY, + objectiveName); } else { - if (propertiesParameters != null - && propertiesParameters.containsKey("objective")) { - result.setProperty("objective", propertiesParameters - .getProperty("objective")); - for (String key : propertiesParameters.stringPropertyNames()) { - if (key.startsWith("objective.")) { - result.setProperty(key, propertiesParameters.getProperty(key)); + if (propertiesParameters != null) { + String objective = propertiesParameters.getProperty( + SimulationParameterPropertiesHelper.OBJECTIVE_KEY); + if (objective != null) { + result.setProperty(SimulationParameterPropertiesHelper.OBJECTIVE_KEY, objective); + SimulationParameterPropertiesHelper.copy(propertiesParameters, result, + SimulationParameterPropertiesHelper.OBJECTIVE_KEY + DOT); + } for (String key : propertiesParameters.stringPropertyNames()) { + if (key.startsWith(SimulationParameterPropertiesHelper.OBJECTIVE_KEY + DOT)) { + result.setProperty(key, propertiesParameters.getProperty(key)); + } } - } + } } @@ -1731,18 +1731,19 @@ if (optimization != null) { String optimizationName = ObjectiveStorage.getName(optimization); Properties optimizationProp = StorageHelper.getParamsAsProperties(0, - getRegion().getStorage(), optimization, "optimization"); + getRegion().getStorage(), optimization, + SimulationParameterPropertiesHelper.OPTIMIZATION_KEY); result.putAll(optimizationProp); - result.setProperty("optimization", optimizationName); + result.setProperty(SimulationParameterPropertiesHelper.OPTIMIZATION_KEY, + optimizationName); } else { - if (propertiesParameters != null - && propertiesParameters.containsKey("optimization")) { - result.setProperty("optimization", propertiesParameters - .getProperty("optimization")); - for (String key : propertiesParameters.stringPropertyNames()) { - if (key.startsWith("optimization.")) { - result.setProperty(key, propertiesParameters.getProperty(key)); - } + if (propertiesParameters != null) { + String optimization = propertiesParameters.getProperty( + SimulationParameterPropertiesHelper.OPTIMIZATION_KEY); + if (optimization != null) { + result.setProperty(SimulationParameterPropertiesHelper.OPTIMIZATION_KEY, optimization); + SimulationParameterPropertiesHelper.copy(propertiesParameters, result, + SimulationParameterPropertiesHelper.OPTIMIZATION_KEY + DOT); } } } @@ -1759,55 +1760,57 @@ // add in props observation id export index Observation observation = exportObservationEntry.getValue(); if (observation != null) { - result.setProperty("optimizationobservation." + optimizationExportIndex, beanUtils.convert(observation)); + result.setProperty( + SimulationParameterPropertiesHelper.OPTIMIZATION_OBSERVATION_KEY + DOT + + optimizationExportIndex, + beanUtils.convert(observation)); } optimizationExportIndex++; } - result.setProperty("optimizationexports", optimizationExportsList); + result.setProperty(SimulationParameterPropertiesHelper.OPTIMIZATION_EXPORTS_KEY, + optimizationExportsList); } else { if (propertiesParameters != null) { - if (propertiesParameters.containsKey("optimizationexports")) { - result.setProperty("optimizationexports", propertiesParameters.getProperty("optimizationexports")); + String optimizationExports = propertiesParameters.getProperty( + SimulationParameterPropertiesHelper.OPTIMIZATION_EXPORTS_KEY); + if (optimizationExports != null) { + result.setProperty(SimulationParameterPropertiesHelper.OPTIMIZATION_EXPORTS_KEY, + optimizationExports); } - for (String key : propertiesParameters.stringPropertyNames()) { - if (key.startsWith("optimizationobservation.")) { - result.setProperty(key, propertiesParameters.getProperty(key)); - } - } + SimulationParameterPropertiesHelper.copy(propertiesParameters, result, + SimulationParameterPropertiesHelper.OPTIMIZATION_OBSERVATION_KEY + DOT); } } // export names - String exportList = ""; - for (String export : getExportNames()) { - exportList += export + ","; + String exportList = StringUtils.join(getExportNames(), ","); + result.setProperty(SimulationParameterPropertiesHelper.EXPORTS_KEY, exportList); - } - result.setProperty("exports", exportList); - // number of sensitivity simulation - result.setProperty("numberOfSensitivitySimulation", String - .valueOf(getNumberOfSensitivitySimulation())); + result.setProperty(SimulationParameterPropertiesHelper.NUMBER_OF_SENSITIVITY_SIMULATION_KEY, + String.valueOf(getNumberOfSensitivitySimulation())); // analysis name if (sensitivityAnalysis != null) { String analysisName = SensitivityAnalysisStorage .getName(getSensitivityAnalysis()); - result.setProperty("sensitivityanalysis", analysisName); + result.setProperty(SimulationParameterPropertiesHelper.SENSITIVITY_ANALYSIS_KEY, + analysisName); // analysis parameter Properties analysisParams = StorageHelper.getParamsAsProperties(0, - getRegion().getStorage(), getSensitivityAnalysis(), "sensitivity"); + getRegion().getStorage(), getSensitivityAnalysis(), + SimulationParameterPropertiesHelper.SENSITIVITY_KEY); result.putAll(analysisParams); } else { - if (propertiesParameters != null - && propertiesParameters.containsKey("sensitivityanalysis")) { - result.setProperty("sensitivityanalysis", - propertiesParameters.getProperty("sensitivityanalysis")); - for (String key : propertiesParameters.stringPropertyNames()) { - if (key.startsWith("sensitivity.")) { - result.setProperty(key, propertiesParameters.getProperty(key)); - } + if (propertiesParameters != null) { + String sensitivityanalysis = propertiesParameters.getProperty( + SimulationParameterPropertiesHelper.SENSITIVITY_ANALYSIS_KEY); + if (sensitivityanalysis != null) { + result.setProperty(SimulationParameterPropertiesHelper.SENSITIVITY_ANALYSIS_KEY, + sensitivityanalysis); + SimulationParameterPropertiesHelper.copy(propertiesParameters, result, + SimulationParameterPropertiesHelper.SENSITIVITY_KEY + DOT); } } } @@ -1823,43 +1826,53 @@ sensitivityExportIndex++, getRegion().getStorage(), sensitivityExport); result.putAll(exportProp); } - result.setProperty("sensitivityexports", sensitivityExportList); + result.setProperty(SimulationParameterPropertiesHelper.SENSITIVITY_EXPORTS_KEY, + sensitivityExportList); } else { - if (propertiesParameters != null - && propertiesParameters.containsKey("sensitivityexports")) { - result.setProperty("sensitivityexports", propertiesParameters - .getProperty("sensitivityexports")); - for (String key : propertiesParameters.stringPropertyNames()) { - if (key.startsWith("sensitivityexport.")) { - result.setProperty(key, propertiesParameters.getProperty(key)); - } + if (propertiesParameters != null) { + String sensitivityexports = propertiesParameters.getProperty( + SimulationParameterPropertiesHelper.SENSITIVITY_EXPORTS_KEY); + if (sensitivityexports != null) { + result.setProperty(SimulationParameterPropertiesHelper.SENSITIVITY_EXPORTS_KEY, + sensitivityexports); + SimulationParameterPropertiesHelper.copy(propertiesParameters, result, + SimulationParameterPropertiesHelper.SENSITIVITY_EXPORTS_KEY + DOT); } } } // sensitivity params - result.setProperty("sensitivityAnalysisOnlyKeepFirst", String.valueOf(isSensitivityAnalysisOnlyKeepFirst())); + result.setProperty(SimulationParameterPropertiesHelper.SENSITIVITY_ANALYSIS_ONLY_KEEP_FIRST_KEY, + String.valueOf(isSensitivityAnalysisOnlyKeepFirst())); - result.setProperty("generatedPreScript", getGeneratedPreScript()); - result.setProperty("usePreScript", String.valueOf(getUsePreScript())); - result.setProperty("preScript", getPreScript()); - result.setProperty("useSimulationPlan", String.valueOf(getUseSimulationPlan())); - result.setProperty("simulationPlanNumber", String.valueOf(getSimulationPlanNumber())); - result.setProperty("useOptimization", String.valueOf(getUseOptimization())); + result.setProperty(SimulationParameterPropertiesHelper.GENERATED_PRE_SCRIPT_KEY, + getGeneratedPreScript()); + result.setProperty(SimulationParameterPropertiesHelper.USE_PRE_SCRIPT_KEY, + String.valueOf(getUsePreScript())); + result.setProperty(SimulationParameterPropertiesHelper.PRE_SCRIPT_KEY, + getPreScript()); + result.setProperty(SimulationParameterPropertiesHelper.USE_SIMULATION_PLAN_KEY, + String.valueOf(getUseSimulationPlan())); + result.setProperty(SimulationParameterPropertiesHelper.SIMULATION_PLAN_NUMBER_KEY, + String.valueOf(getSimulationPlanNumber())); + result.setProperty(SimulationParameterPropertiesHelper.USE_OPTIMIZATION_KEY, + String.valueOf(getUseOptimization())); - String resultList = ""; - for (String r : getResultEnabled()) { - resultList += r + ","; - } - result.setProperty("resultNames", resultList); + String resultList = StringUtils.join(getResultEnabled(), + SimulationParameterPropertiesHelper.LIST_SEPARATOR); + result.setProperty(SimulationParameterPropertiesHelper.RESULT_NAMES_KEY, resultList); for (Map.Entry<String, String> e : getTagValue().entrySet()) { - result.setProperty("tagValue." + e.getKey(), e.getValue()); + result.setProperty(SimulationParameterPropertiesHelper.TAG_VALUE_KEY+ DOT + + e.getKey(), e.getValue()); } - result.setProperty("simulLogLevel", getSimulLogLevel()); - result.setProperty("scriptLogLevel", getScriptLogLevel()); - result.setProperty("libLogLevel", getLibLogLevel()); + result.setProperty(SimulationParameterPropertiesHelper.SIMUL_LOG_LEVEL_KEY, + getSimulLogLevel()); + result.setProperty(SimulationParameterPropertiesHelper.SCRIPT_LOG_LEVEL_KEY, + getScriptLogLevel()); + result.setProperty(SimulationParameterPropertiesHelper.LIB_LOG_LEVEL_KEY, + getLibLogLevel()); return result; } Modified: trunk/src/main/java/fr/ifremer/isisfish/simulator/SimulationParameterPropertiesHelper.java =================================================================== --- trunk/src/main/java/fr/ifremer/isisfish/simulator/SimulationParameterPropertiesHelper.java 2014-07-24 14:44:06 UTC (rev 4066) +++ trunk/src/main/java/fr/ifremer/isisfish/simulator/SimulationParameterPropertiesHelper.java 2014-07-24 15:07:49 UTC (rev 4067) @@ -2,6 +2,7 @@ import java.util.HashMap; +import java.util.LinkedHashMap; import java.util.Map; import java.util.Properties; import org.apache.commons.lang3.StringUtils; @@ -26,11 +27,18 @@ /** to use log facility, just put in your code: log.info(\"...\"); */ final static private Log log = LogFactory.getLog(SimulationParameterPropertiesHelper.class); + final public static String DOT = "."; + final public static String LIST_SEPARATOR = ","; + final public static String ISIS_FISH_VERSION_KEY = "isisFishVersion"; final public static String SIMULATOR_NAME_KEY = "simulatorName"; final public static String POPULATIONS_KEY = "populations"; final public static String RULES_KEY = "rules"; + final public static String RULE_KEY = "rule"; + final public static String RESULT_NAMES_KEY = "resultNames"; + final public static String EXPORTS_KEY = "exports"; final public static String PLANS_KEY = "plans"; + final public static String PLAN_KEY = "plan"; final public static String STRATEGIES_KEY = "strategies"; final public static String DESCRIPTION_KEY = "description"; final public static String REGION_NAME_KEY = "regionName"; @@ -40,14 +48,27 @@ final public static String OPTIMIZATION_KEY = "optimization"; final public static String OBJECTIVE_KEY = "objective"; final public static String OPTIMIZATION_EXPORTS_KEY = "optimizationexports"; + final public static String OPTIMIZATION_OBSERVATION_KEY = "optimizationobservation"; final public static String USE_CACHE_KEY = "useCache"; + final public static String USE_STATISTIC_KEY = "useStatistic"; final public static String NUMBER_OF_YEAR_KEY = "numberOfYear"; final public static String GENERATED_PRE_SCRIPT_KEY = "generatedPreScript"; + final public static String USE_PRE_SCRIPT_KEY = "usePreScript"; final public static String PRE_SCRIPT_KEY = "preScript"; final public static String SIMUL_LOG_LEVEL_KEY = "simulLogLevel"; - final public static String SCRIPT_LOG_LEVEL_KEY = "simulLogLevel"; - final public static String LIB_LOG_LEVEL_KEY = "simulLogLevel"; + final public static String SCRIPT_LOG_LEVEL_KEY = "scriptLogLevel"; + final public static String LIB_LOG_LEVEL_KEY = "libLogLevel"; + final public static String SENSITIVITY_ANALYSIS_KEY = "sensitivityanalysis"; + final public static String SENSITIVITY_KEY = "sensitivity"; + final public static String SENSITIVITY_EXPORTS_KEY = "sensitivityexports"; + final public static String SENSITIVITY_EXPORT_KEY = "sensitivityexports"; + final public static String POPULATION_KEY = "population"; + final public static String NUMBER_KEY = "number"; + final public static String NUMBER_OF_SENSITIVITY_SIMULATION_KEY = "numberOfSensitivitySimulation"; + final public static String SENSITIVITY_ANALYSIS_ONLY_KEEP_FIRST_KEY = "sensitivityAnalysisOnlyKeepFirst"; + final public static String TAG_VALUE_KEY = "tagValue"; + public static String getIsisFishVersion(Properties prop) { String result = prop.getProperty(ISIS_FISH_VERSION_KEY, ""); return result; @@ -94,26 +115,36 @@ } public static String[] getPopulationNames(Properties prop) { - String[] result = prop.getProperty(POPULATIONS_KEY, "").split(","); + String[] result = StringUtils.split(prop.getProperty(POPULATIONS_KEY, ""), LIST_SEPARATOR); return result; } public static String[] getStrategieNames(Properties prop) { - String[] result = prop.getProperty(STRATEGIES_KEY, "").split(","); + String[] result = StringUtils.split(prop.getProperty(STRATEGIES_KEY, ""), LIST_SEPARATOR); return result; } public static String[] getRuleNames(Properties prop) { - String[] result = prop.getProperty(RULES_KEY, "").split(","); + String[] result = StringUtils.split(prop.getProperty(RULES_KEY, ""), LIST_SEPARATOR); return result; } + public static String[] getResultNames(Properties prop) { + String[] result = StringUtils.split(prop.getProperty(RESULT_NAMES_KEY, ""), LIST_SEPARATOR); + return result; + } + + public static String[] getExportNames(Properties prop) { + String[] result = StringUtils.split(prop.getProperty(EXPORTS_KEY, ""), LIST_SEPARATOR); + return result; + } + public static String[] getSimulationPlanNames(Properties prop) { - String[] result = prop.getProperty(PLANS_KEY, "").split(","); + String[] result = StringUtils.split(prop.getProperty(PLANS_KEY, ""), LIST_SEPARATOR); return result; } - private static boolean getUseOptimization(Properties prop) { + public static boolean getUseOptimization(Properties prop) { boolean result; // en version < 4.3, le parametre était nommé 'useOptimization' // qualifiant le cache, il a été renommé ensuite @@ -136,12 +167,12 @@ } public static String[] getOptimizationExportNames(Properties prop) { - String[] result = prop.getProperty(OPTIMIZATION_EXPORTS_KEY, "").split(","); + String[] result = StringUtils.split(prop.getProperty(OPTIMIZATION_EXPORTS_KEY, ""), LIST_SEPARATOR); return result; } public static String getOptimizationObservationName(Properties prop, int index) { - String result = prop.getProperty("optimizationobservation" + "." + index, ""); + String result = prop.getProperty(OPTIMIZATION_OBSERVATION_KEY + DOT + index, ""); if (result.matches("[\\w.]+#[\\d.]+#[\\d.]+:(.+)")) { result = result.replaceFirst("[\\w.]+#[\\d.]+#[\\d.]+:(.+)", "$1"); } @@ -149,11 +180,29 @@ return result; } + public static int getNumberOfSensitivitySimulation(Properties prop) { + int result = Integer.valueOf(prop.getProperty( + NUMBER_OF_SENSITIVITY_SIMULATION_KEY, "-1")); + return result; + } + + public static boolean isSensitivityAnalysisOnlyKeepFirst(Properties prop) { + boolean result = Boolean.valueOf(prop.getProperty( + SENSITIVITY_ANALYSIS_ONLY_KEEP_FIRST_KEY, "-1")); + return result; + } + + public static String getGeneratedPreScript(Properties prop) { String result = prop.getProperty(GENERATED_PRE_SCRIPT_KEY, ""); return result; } + public static boolean getUsePreScript(Properties prop) { + boolean result = Boolean.valueOf(prop.getProperty(USE_PRE_SCRIPT_KEY, "false")); + return result; + } + public static String getPreScript(Properties prop) { String result = prop.getProperty(PRE_SCRIPT_KEY, ""); return result; @@ -174,6 +223,20 @@ return result; } + public static Map<String, String> getTagValue(Properties prop) { + int tagValueLength = (TAG_VALUE_KEY + DOT).length(); + // preserve order + Map<String, String> result = new LinkedHashMap<String, String>(); + for (String k : prop.stringPropertyNames()) { + if (k.startsWith(TAG_VALUE_KEY + DOT)) { + String key = k.substring(tagValueLength); + String value = prop.getProperty(k); + result.put(key, value); + } + } + return result; + } + /** * Retourne les parametres sous forme de String pour une regles. Les regles * sont numeroter dans l'ordre dans lequel on les recupere via #getRuleNames(). @@ -186,7 +249,7 @@ public static Map<String, String> getParamAsString(Properties prop, String prefix, int index) { Map<String, String> result = new HashMap<String, String>(); if (prop != null) { - String paramTag = prefix + "." + index + ".parameter."; + String paramTag = prefix + DOT + index + ".parameter."; int paramTagLength = paramTag.length(); for (String key : prop.stringPropertyNames()) { @@ -205,6 +268,21 @@ return result; } + /** + * Copy all entry beginning with prefix from source to target + * + * @param source + * @param target + * @param prefix + */ + public static void copy(Properties source, Properties target, String prefix) { + for (String key : source.stringPropertyNames()) { + if (StringUtils.startsWith(key, prefix)) { + target.setProperty(key, source.getProperty(key)); + } + } + } + public static String toString(Properties prop) { StringBuilder result = new StringBuilder(); result.append( @@ -241,7 +319,7 @@ String sep = ""; for (String str : strs) { result.append(sep).append(str); - sep = ","; + sep = LIST_SEPARATOR; } result.append("\n\n"); @@ -250,7 +328,7 @@ sep = ""; for (String pop : getPopulationNames(prop)) { result.append(sep).append(pop); - sep = ","; + sep = LIST_SEPARATOR; } result.append("\n\n"); @@ -324,13 +402,16 @@ .append(":\n"); result.append(genScript); } - String prescript = getPreScript(prop); - if (StringUtils.isNotBlank(prescript)) { - result.append('\n'); - result.append(t("isisfish.params.toString.script.presimulation")) - .append(":\n"); - result.append(prescript); + if (getUsePreScript(prop)) { + String prescript = getPreScript(prop); + if (StringUtils.isNotBlank(prescript)) { + result.append('\n'); + result.append(t("isisfish.params.toString.script.presimulation")) + .append(":\n"); + result.append(prescript); + } } + result.append('\n'); result.append(t("isisfish.params.toString.simul.logger.level", getSimulLogLevel(prop)));