Author: chatellier Date: 2009-03-09 17:22:53 +0000 (Mon, 09 Mar 2009) New Revision: 1924 Modified: isis-fish/trunk/src/main/java/fr/ifremer/isisfish/simulator/SimulationContext.java Log: Add info on SimulationContext to add equation context values Modified: isis-fish/trunk/src/main/java/fr/ifremer/isisfish/simulator/SimulationContext.java =================================================================== --- isis-fish/trunk/src/main/java/fr/ifremer/isisfish/simulator/SimulationContext.java 2009-03-09 16:55:55 UTC (rev 1923) +++ isis-fish/trunk/src/main/java/fr/ifremer/isisfish/simulator/SimulationContext.java 2009-03-09 17:22:53 UTC (rev 1924) @@ -1,5 +1,5 @@ /* *##% - * Copyright (C) 2006 + * Copyright (C) 2006 - 2009 * Ifremer, Code Lutin, Cédric Pineau, Benjamin Poussin * * This program is free software; you can redistribute it and/or @@ -17,23 +17,10 @@ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *##%*/ -/* * - * SimulationContext.java - * - * Created: 3 juil. 2006 17:05:27 - * - * @author poussin - * @version $Revision$ - * - * Last update: $Date$ - * by : $Author$ - */ - package fr.ifremer.isisfish.simulator; import java.io.File; import java.util.HashMap; -import java.util.HashSet; import java.util.LinkedHashSet; import java.util.Map; import java.util.Set; @@ -45,37 +32,67 @@ import fr.ifremer.isisfish.datastore.SimulationStorage; - /** - * keep all information on one simulation. + * Keep all information on one simulation. + * * <li> Launch parameter * <li> Database (TopiaContext) * <li> SimulationControl * <li> Effectif by pop (N) * <li> Result * + * Created: 3 juil. 2006 17:05:27 + * * @author poussin + * @version $Revision$ * + * Last update: $Date$ + * by : $Author$ */ +public class SimulationContext { -public class SimulationContext { - /** to use log facility, just put in your code: log.info(\"...\"); */ - static private Log log = LogFactory.getLog(SimulationContext.class); + private static Log log = LogFactory.getLog(SimulationContext.class); + protected Map<String, Object> values = new HashMap<String, Object>(); + protected SimulationStorage simulation = null; + protected SimulationControl simulationControl = null; + protected PopulationMonitor populationMonitor = null; + protected MetierMonitor metierMonitor = null; + protected RuleMonitor ruleMonitor = null; + protected ResultManager resultManager = null; + protected Set<SimulationListener> simulationListeners = new LinkedHashSet<SimulationListener>(); + protected ClassLoader classLoader = null; + protected File scriptDirectory; + + /** TopiaContext must be used by rule action to modify data */ + protected TopiaContext db = null; + + /** TopiaContext must be used to save result */ + protected TopiaContext dbResult = null; + + /** Context value used in equation. */ + protected Map<String, Double> contextEquationValue = new HashMap<String, Double>(); + /** Operator used in equation values */ + protected Map<String, String> contextEquationOperator = new HashMap<String, String>(); + private static ThreadLocal<SimulationContext> simulationContext = new ThreadLocal<SimulationContext>() { protected synchronized SimulationContext initialValue() { return new SimulationContext(); } }; + protected SimulationContext() { + + } + /** * @return simulation context for the current simulation (current thread) */ public static SimulationContext get() { return simulationContext.get(); } - + /** * remove simulation associate with current thread */ @@ -85,36 +102,17 @@ current.classLoader = null; simulationContext.remove(); } - - protected Map<String, Object> values = new HashMap<String, Object>(); - protected SimulationStorage simulation = null; - protected SimulationControl simulationControl = null; - protected PopulationMonitor populationMonitor = null; - protected MetierMonitor metierMonitor = null; - protected RuleMonitor ruleMonitor = null; - protected ResultManager resultManager = null; - protected Set<SimulationListener> simulationListeners = new LinkedHashSet<SimulationListener>(); - protected ClassLoader classLoader = null; - protected File scriptDirectory; - - /** TopiaContext must be used by rule action to modify data */ - protected TopiaContext db = null; - - /** TopiaContext must be used to save result */ - protected TopiaContext dbResult = null; - - private SimulationContext() { - } /** * Add simulation listener, if listener is {@link SimulationResultListener}, it's - * automaticaly added as listener on {@link ResultManager} + * automatically added as listener on {@link ResultManager} * @param l */ public void addSimulationListener(SimulationListener l) { simulationListeners.add(l); if (l instanceof SimulationResultListener) { - getResultManager().addSimulationResultListener((SimulationResultListener)l); + getResultManager().addSimulationResultListener( + (SimulationResultListener) l); } } @@ -137,12 +135,13 @@ dbResult.closeContext(); } catch (TopiaException eee) { if (log.isDebugEnabled()) { - log.debug("Can't close simulation result topia context", eee); + log.debug("Can't close simulation result topia context", + eee); } } } } - + /** * Remove simulation listener, if listener is {@link SimulationResultListener}, it's * Automatically removed as listener on {@link ResultManager} @@ -151,35 +150,37 @@ public void removeSimulationListener(SimulationListener l) { simulationListeners.remove(l); if (l instanceof SimulationResultListener) { - getResultManager().addSimulationResultListener((SimulationResultListener)l); + getResultManager().addSimulationResultListener( + (SimulationResultListener) l); } } - + public void fireBeforeSimulation() { - for(SimulationListener l : simulationListeners) { + for (SimulationListener l : simulationListeners) { l.beforeSimulation(this); } } + public void fireAfterSimulation() { - for(SimulationListener l : simulationListeners) { + for (SimulationListener l : simulationListeners) { l.afterSimulation(this); - } + } } - + /** * @return Returns the classLoader. */ public ClassLoader getClassLoader() { return this.classLoader; } - + /** * @param classLoader The classLoader to set. */ public void setClassLoader(ClassLoader classLoader) { this.classLoader = classLoader; } - + /** * Permet de recuperer une valeur prealablement stocker avec un setValue * @param name le nom de la valeur souhaitée @@ -189,7 +190,7 @@ Object result = values.get(name); return result; } - + /** * Permet de stocker une valeur en fonction d'une cle, cela peut-etre util * pour partager des informations entre Rule ou d'autre objet. @@ -199,35 +200,35 @@ public void setValue(String name, Object value) { values.put(name, value); } - + /** * @return Returns the topiaContext. */ public SimulationStorage getSimulationStorage() { return this.simulation; } - + /** * @param simulation The simulation storage to set. */ public void setSimulationStorage(SimulationStorage simulation) { this.simulation = simulation; } - + /** * @return Returns the simulationControl. */ public SimulationControl getSimulationControl() { return this.simulationControl; } - + /** * @param simulationControl The simulationControl to set. */ public void setSimulationControl(SimulationControl simulationControl) { this.simulationControl = simulationControl; } - + /** * @return Returns the populationMonitor. */ @@ -237,7 +238,7 @@ } return this.populationMonitor; } - + /** * @return Returns the metierMonitor. */ @@ -247,7 +248,7 @@ } return this.metierMonitor; } - + /** * @return Returns the ruleMonitor. */ @@ -258,7 +259,6 @@ return this.ruleMonitor; } - /** * @return Returns the resultManager. */ @@ -266,9 +266,9 @@ if (this.resultManager == null) { this.resultManager = new ResultManager(this); } - return this.resultManager; + return this.resultManager; } - + /** * This method return TopiaContext that Rule action must used to modify * data. This TopiaContext is rollbacked after each step. @@ -281,8 +281,7 @@ } return db; } - - + /** * this method return TopiaContext that must be used to save result * @return Returns the dbResult. @@ -294,14 +293,14 @@ } return this.dbResult; } - + /** * @return Returns the scriptDirectory. */ public File getScriptDirectory() { return this.scriptDirectory; } - + /** * @param scriptDirectory */ @@ -318,7 +317,56 @@ getSimulationControl().setText(message); } } + + /** + * Add value/key pair into context. + * + * @param key key + * @param value value + * @param operator operator + */ + public void setValue(String key, Double value, String operator) { + contextEquationValue.put(key, value); + contextEquationOperator.put(key, operator); + } + /** + * Return value from context. + * + * @param key key + * @param defaultValue default value or value to compute + * @return computed value or <tt>defaultValue</tt> if not found + */ + public double getValueAndCompute(String key, double defaultValue) { + + double result = defaultValue; + + if (contextEquationValue.containsKey(key) && contextEquationOperator.containsKey(key)) { + Double value = contextEquationValue.get(key); + String operator = contextEquationOperator.get(key); + + if ("+".equals(operator)) { + result = value + defaultValue; + } + else if ("-".equals(operator)) { + result = value - defaultValue; + } + else if ("*".equals(operator)) { + result = value * defaultValue; + } + else if ("/".equals(operator)) { + result = value / defaultValue; + } + else { + throw new IllegalArgumentException("Unknown operator '" + operator + "'"); + } + } + else { + if (log.isWarnEnabled()) { + log.warn("No key defined for key '" + key + "' in context"); + } + } + + return result; + } } - -