View Javadoc
1   /*
2    * #%L
3    * IsisFish
4    * 
5    * $Id: SimulationServiceTest.java 4277 2015-06-21 17:24:03Z echatellier $
6    * $HeadURL: https://svn.codelutin.com/isis-fish/tags/isis-fish-4.4.0.2/src/test/java/fr/ifremer/isisfish/simulator/launcher/SimulationServiceTest.java $
7    * %%
8    * Copyright (C) 2006 - 2014 Ifremer, Code Lutin, Chatellier Eric
9    * %%
10   * This program is free software: you can redistribute it and/or modify
11   * it under the terms of the GNU General Public License as
12   * published by the Free Software Foundation, either version 3 of the 
13   * License, or (at your option) any later version.
14   * 
15   * This program is distributed in the hope that it will be useful,
16   * but WITHOUT ANY WARRANTY; without even the implied warranty of
17   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18   * GNU General Public License for more details.
19   * 
20   * You should have received a copy of the GNU General Public 
21   * License along with this program.  If not, see
22   * <http://www.gnu.org/licenses/gpl-3.0.html>.
23   * #L%
24   */
25  
26  package fr.ifremer.isisfish.simulator.launcher;
27  
28  import java.io.File;
29  import java.util.ArrayList;
30  import java.util.Collections;
31  import java.util.List;
32  import java.util.Properties;
33  import java.util.SortedMap;
34  import java.util.TreeMap;
35  
36  import org.apache.commons.logging.Log;
37  import org.apache.commons.logging.LogFactory;
38  import org.junit.Assert;
39  import org.junit.Ignore;
40  import org.junit.Test;
41  import org.nuiton.math.matrix.MatrixFactory;
42  import org.nuiton.math.matrix.MatrixHelper;
43  import org.nuiton.math.matrix.MatrixND;
44  import org.nuiton.topia.TopiaContext;
45  import org.nuiton.topia.TopiaException;
46  
47  import fr.ifremer.isisfish.AbstractIsisFishTest;
48  import fr.ifremer.isisfish.IsisFishException;
49  import fr.ifremer.isisfish.datastore.RegionStorage;
50  import fr.ifremer.isisfish.datastore.RuleStorage;
51  import fr.ifremer.isisfish.datastore.ScriptStorage;
52  import fr.ifremer.isisfish.datastore.SimulationStorage;
53  import fr.ifremer.isisfish.datastore.SimulatorStorage;
54  import fr.ifremer.isisfish.datastore.StorageException;
55  import fr.ifremer.isisfish.entities.Population;
56  import fr.ifremer.isisfish.entities.PopulationGroup;
57  import fr.ifremer.isisfish.entities.PopulationImpl;
58  import fr.ifremer.isisfish.entities.Strategy;
59  import fr.ifremer.isisfish.entities.StrategyImpl;
60  import fr.ifremer.isisfish.entities.Zone;
61  import fr.ifremer.isisfish.rule.Rule;
62  import fr.ifremer.isisfish.rule.RuleHelper;
63  import fr.ifremer.isisfish.simulator.SimulationParameter;
64  import fr.ifremer.isisfish.simulator.SimulationParameterImpl;
65  import fr.ifremer.isisfish.simulator.sensitivity.DesignPlan;
66  import fr.ifremer.isisfish.simulator.sensitivity.Distribution;
67  import fr.ifremer.isisfish.simulator.sensitivity.Factor;
68  import fr.ifremer.isisfish.simulator.sensitivity.FactorGroup;
69  import fr.ifremer.isisfish.simulator.sensitivity.FactorHelper;
70  import fr.ifremer.isisfish.simulator.sensitivity.Scenario;
71  import fr.ifremer.isisfish.simulator.sensitivity.SensitivityAnalysis;
72  import fr.ifremer.isisfish.simulator.sensitivity.SensitivityAnalysisRandomMock;
73  import fr.ifremer.isisfish.simulator.sensitivity.domain.ContinuousDomain;
74  import fr.ifremer.isisfish.simulator.sensitivity.domain.DiscreteDomain;
75  import fr.ifremer.isisfish.simulator.sensitivity.domain.EquationDiscreteDomain;
76  import fr.ifremer.isisfish.simulator.sensitivity.domain.RuleDiscreteDomain;
77  import fr.ifremer.isisfish.types.Month;
78  
79  /**
80   * Test for {@link SimulationService}.
81   * 
82   * @author chatellier
83   * @version $Revision: 4277 $
84   * 
85   * Last update : $Date: 2015-06-21 19:24:03 +0200 (Sun, 21 Jun 2015) $ By : $Author: echatellier $
86   */
87  public class SimulationServiceTest extends AbstractIsisFishTest {
88  
89      /** Commons logging log. */
90      private static Log log = LogFactory.getLog(SimulationServiceTest.class);
91  
92      /**
93       * Lance une simulation simple avec le simulateur par cellule.
94       * 
95       * Test is wrong if it take more than 2 min.
96       * 
97       * @throws InterruptedException 
98       */
99      @Test(timeout=120 * 1000)
100     public void testSimulationEffortByCell() throws InterruptedException {
101 
102         SimulationParameter params = new SimulationParameterImpl();
103         // set params region
104         params.setRegionName("DemoRegion");
105         // simulator
106         params.setSimulatorName("SimulatorEffortByCell.java");
107         // set population
108         List<Population> pops = new ArrayList<Population>();
109         Population pop = new PopulationImpl();
110         pop.setName("test population");
111         pop.setPopulationGroup(new ArrayList<PopulationGroup>());
112         pop.setPopulationZone(new ArrayList<Zone>());
113         pops.add(pop);
114         params.setPopulations(pops);
115         // strat
116         List<Strategy> strats = new ArrayList<Strategy>();
117         Strategy strategy = new StrategyImpl();
118         strategy.setName("stratest");
119         strats.add(strategy);
120         params.setStrategies(strats);
121         // number of year
122         params.setNumberOfYear(1);
123         // add first result only (cicular deps only)
124         params.setResultEnabled(Collections.singletonList("MatrixTestDep1"));
125 
126         // run simulation
127         SimulationService service = SimulationService.getService();
128         SimulatorLauncher launcher = new InProcessSimulatorLauncher();
129         service.addSimulationLauncher(launcher);
130         String simulationId = "effort by cell test number 1";
131         service.submit("effort by cell test number 1", params, launcher, 0);
132 
133         // temporise la fin de test , sinon junit tue tous les process
134         do {
135             Thread.sleep(2000);
136         } while (!service.jobs.isEmpty());
137 
138         // suite au changement de la compilation, on vérifie que certains fichier java
139         // doivent être automatiquement inclut dans les sources
140         File simDir = SimulationStorage.getSimulationDirectory(simulationId);
141         File simulatorDir = new File(simDir, SimulatorStorage.SIMULATOR_PATH);
142         File scriptDir = new File(simDir, ScriptStorage.SCRIPT_PATH);
143         Assert.assertTrue(new File(simulatorDir, "DefaultSimulator.java").exists());
144         Assert.assertTrue(new File(simulatorDir, "SimulatorEffortByCell.java").exists());
145         Assert.assertTrue(new File(scriptDir, "ResultName.java").exists());
146         Assert.assertTrue(new File(scriptDir, "SiMatrix.java").exists());
147         Assert.assertTrue(new File(scriptDir, "GravityModel.java").exists());
148         Assert.assertFalse(new File(scriptDir, "RuleUtil.java").exists());
149     }
150 
151     /**
152      * Build a test {@link DesignPlan}.
153      * 
154      * @return a test {@link DesignPlan}
155      */
156     protected DesignPlan getTestDesignPlan() {
157         DesignPlan designPlan = new DesignPlan();
158 
159         // factor 1
160         Factor factor1 = new Factor("factor 1 (double)");
161         ContinuousDomain domain1 = new ContinuousDomain(Distribution.QUNIFMM);
162         domain1.addDistributionParam("min", 0.0);
163         domain1.addDistributionParam("max", 50.0);
164         factor1.setCardinality(4);
165         factor1.setDomain(domain1);
166         factor1.setPath("fr.ifremer.isisfish.entities.PopulationGroup#1156461521013#0.1715620681984218#maxLength");
167 
168         // factor 2
169         Factor factor2 = new Factor("factor 2 (double)");
170         DiscreteDomain domain2 = new DiscreteDomain();
171         domain2.getValues().put(0.0, 12.3);
172         domain2.getValues().put(1.0, 70.9);
173         domain2.getValues().put(2.0, 21.0);
174         domain2.getValues().put(3.0, -12.1);
175         domain2.getValues().put(4.0, -8.45);
176         factor2.setDomain(domain2);
177         factor2.setPath("fr.ifremer.isisfish.entities.PopulationGroup#1156461521064#0.022976136053553198#minLength");
178 
179         // factor 3
180         Factor factor3 = new Factor("factor 3 (double)");
181         ContinuousDomain domain3 = new ContinuousDomain(Distribution.QUNIFMM);
182         domain3.addDistributionParam("min", 12.0);
183         domain3.addDistributionParam("max", 99.0);
184         factor3.setCardinality(4);
185         factor3.setDomain(domain3);
186         factor3.setPath("fr.ifremer.isisfish.entities.PopulationGroup#1156461521076#0.6526656643346673#minLength");
187 
188         designPlan.addFactor(factor1);
189         //designPlan.add(factor2);
190         designPlan.addFactor(factor3);
191 
192         return designPlan;
193     }
194 
195     /**
196      * Build a test {@link DesignPlan} filled with matrix.
197      * 
198      * @return a test {@link DesignPlan}
199      */
200     protected DesignPlan getTestMatrixDesignPlan() {
201         DesignPlan designPlan = new DesignPlan();
202 
203         // matrix 1
204         MatrixND matrix1 = MatrixFactory.getInstance().create("test1",
205                 new int[] { 3, 2 }, new String[] { "col1", "col2" });
206         matrix1.setValue(new int[] { 0, 0 }, 13);
207         matrix1.setValue(new int[] { 0, 1 }, -14);
208         matrix1.setValue(new int[] { 1, 0 }, 21);
209         matrix1.setValue(new int[] { 1, 1 }, 2);
210         matrix1.setValue(new int[] { 2, 0 }, 12);
211         matrix1.setValue(new int[] { 2, 1 }, -1);
212 
213         // matrix 2
214         MatrixND matrix2 = MatrixFactory.getInstance().create("test1",
215                 new int[] { 3, 2 }, new String[] { "col1", "col2" });
216         matrix2.setValue(new int[] { 0, 0 }, 13);
217         matrix2.setValue(new int[] { 0, 1 }, -14);
218         matrix2.setValue(new int[] { 1, 0 }, 21);
219         matrix2.setValue(new int[] { 1, 1 }, 2);
220         matrix2.setValue(new int[] { 2, 0 }, 12);
221         matrix2.setValue(new int[] { 2, 1 }, -1);
222 
223         // factor 1
224         Factor factor1 = new Factor("factor 1 (matrixND)");
225         DiscreteDomain domain1 = new DiscreteDomain();
226         domain1.getValues().put(0.0, matrix1);
227         domain1.getValues().put(1.0, matrix2);
228         factor1.setDomain(domain1);
229         factor1.setPath("fr.ifremer.isisfish.entities.StrategyMonthInfo#1156808754768#0.7282750856395208#proportionMetier");
230 
231         designPlan.getFactors().add(factor1);
232 
233         return designPlan;
234     }
235 
236     /**
237      * Lance une simulation avec des facteurs de sensibilité.
238      * 
239      * Test is wrong if it take more than 2 min.
240      * 
241      * @throws InterruptedException 
242      */
243     @Test(timeout=120 * 1000)
244     public void testRunSensivitySimulation() throws InterruptedException {
245 
246         SimulationParameter params = new SimulationParameterImpl();
247         // set params region
248         params.setRegionName("DemoRegion");
249         // set population
250         List<Population> pops = new ArrayList<Population>();
251         Population pop = new PopulationImpl();
252         pop.setName("test population");
253         pop.setPopulationGroup(new ArrayList<PopulationGroup>());
254         pop.setPopulationZone(new ArrayList<Zone>());
255         pops.add(pop);
256         params.setPopulations(pops);
257         // strat
258         List<Strategy> strats = new ArrayList<Strategy>();
259         Strategy strategy = new StrategyImpl();
260         strategy.setName("stratest");
261         strats.add(strategy);
262         params.setStrategies(strats);
263         // number of year
264         params.setNumberOfMonths(Month.NUMBER_OF_MONTH);
265 
266         SimulationService service = SimulationService.getService();
267         SimulatorLauncher launcher = new InProcessSimulatorLauncher();
268         service.addSimulationLauncher(launcher);
269 
270         SensitivityAnalysis sensitivityAnalysis = new SensitivityAnalysisRandomMock();
271 
272         DesignPlan designPlan = getTestDesignPlan();
273 
274         service.submit("sensitivity test number 1", params, launcher, 0,
275                 sensitivityAnalysis, designPlan);
276 
277         // temporise la fin de test , sinon junit tue tous les process
278         do {
279             Thread.sleep(2000);
280         } while (!service.jobs.isEmpty());
281 
282     }
283 
284     /**
285      * Test la generation de facteur simple.
286      * 
287      * @throws StorageException 
288      * @throws TopiaException
289      */
290     @Test
291     public void testFactorPreScriptFactor() throws StorageException, TopiaException {
292         
293         // factor
294         Factor factor = new Factor("test");
295         ContinuousDomain domain = new ContinuousDomain(Distribution.QUNIFMM);
296         domain.addDistributionParam("min", 0.0);
297         domain.addDistributionParam("max", 50.0);
298         factor.setDomain(domain);
299         factor.setPath("fr.ifremer.entities.Cell#1234567890#length");
300         factor.setValueForIdentifier(0.5);
301         
302         Assert.assertEquals(25.0, factor.getValue());
303         
304         // scenario
305         Scenario scenario = new Scenario();
306         List<Factor> factors = new ArrayList<Factor>();
307         factors.add(factor);
308         scenario.setFactors(factors);
309 
310         String scriptContent = FactorHelper.generatePreScript(scenario);
311         Assert.assertTrue(scriptContent.contains("java.lang.Double"));
312         Assert.assertTrue(scriptContent.contains("db.findByTopiaId(\"fr.ifremer.entities.Cell#1234567890\")"));
313         Assert.assertTrue(scriptContent.contains("BeanUtils.setProperty(entity0, \"length\""));
314     }
315     
316     /**
317      * Creer un simulation.
318      * Creer un populationSeasonInfoDAO.
319      * 
320      * @throws StorageException 
321      * @throws TopiaException
322      */
323     @Test
324     public void testFactorPreScriptFactorMatrix() throws StorageException, TopiaException {
325 
326         // matrix 1
327         MatrixND matrix1 = MatrixFactory.getInstance().create("test1",
328                 new int[] { 3, 2 }, new String[] { "col1", "col2" });
329         matrix1.setValue(new int[] { 0, 0 }, 13);
330         matrix1.setValue(new int[] { 0, 1 }, -14);
331         matrix1.setValue(new int[] { 1, 0 }, 21);
332         matrix1.setValue(new int[] { 1, 1 }, 2);
333         matrix1.setValue(new int[] { 2, 0 }, 12);
334         matrix1.setValue(new int[] { 2, 1 }, -1);
335 
336         // matrix 2
337         MatrixND matrix2 = MatrixFactory.getInstance().create("test2",
338                 new int[] { 2, 3 }, new String[] { "col1", "col2" });
339         matrix2.setValue(new int[] { 0, 0 }, 9999);
340         matrix2.setValue(new int[] { 0, 1 }, 15000);
341         matrix2.setValue(new int[] { 0, 2 }, -40000);
342         matrix2.setValue(new int[] { 1, 0 }, 21345);
343         matrix2.setValue(new int[] { 1, 1 }, 81000);
344         matrix2.setValue(new int[] { 1, 2 }, -13000);
345 
346         // factor
347         Factor factor1 = new Factor("testmatrix");
348         DiscreteDomain domain1 = new DiscreteDomain();
349         domain1.getValues().put("m1", matrix1);
350         factor1.setDomain(domain1);
351         factor1.setPath("org.nuiton.math.matrix.MatrixND#563456293453#2.456347646#dim");
352         factor1.setValueForIdentifier("m1");
353 
354         // factor
355         Factor factor2 = new Factor("testmatrix");
356         DiscreteDomain domain2 = new DiscreteDomain();
357         domain2.getValues().put("m2", matrix1);
358         factor2.setDomain(domain2);
359         factor2.setPath("org.nuiton.math.matrix.MatrixND#563456293453#2.456347646#dim");
360         factor2.setValueForIdentifier("m2");
361 
362         // scenario
363         Scenario scenario = new Scenario();
364         List<Factor> factors = new ArrayList<Factor>();
365         factors.add(factor1);
366         factors.add(factor2);
367         scenario.setFactors(factors);
368 
369         String scriptContent = FactorHelper.generatePreScript(scenario);
370         //log.info("Script = " + scriptContent);
371         Assert.assertTrue(scriptContent.contains("org.nuiton.math.matrix.MatrixNDImpl "));
372         Assert.assertTrue(scriptContent.contains("db.findByTopiaId(\"org.nuiton.math.matrix.MatrixND#563456293453#2.456347646\");"));
373         Assert.assertTrue(scriptContent.contains("BeanUtils.setProperty(entity0, \"dim\""));
374     }
375 
376     /**
377      * Creer un scenario basé sur les EquationDomain.
378      * 
379      * @throws StorageException 
380      * @throws TopiaException 
381      */
382     @Test
383     public void testFactorPreScriptEquation() throws StorageException, TopiaException {
384 
385         // factor
386         Factor factorEquation = new Factor("test.equation.name");
387         ContinuousDomain domain1 = new ContinuousDomain(Distribution.QUNIFPC);
388         domain1.addDistributionParam("reference", 10.0);
389         domain1.addDistributionParam("coefficient", 0.1);
390         factorEquation.setEquationVariableName("K1");
391         factorEquation.setDomain(domain1);
392         factorEquation.setPath("fr.ifremer.equation1#testFactorPreScriptEquation");
393         factorEquation.setValueForIdentifier(0.4);
394 
395         // scenario
396         Scenario scenario = new Scenario();
397         List<Factor> factors = new ArrayList<Factor>();
398         factors.add(factorEquation);
399         scenario.setFactors(factors);
400 
401         String scriptContent = FactorHelper.generatePreScript(scenario);
402         //log.info("Script = " + scriptContent);
403 
404         // le nom de la valeur de la variable n'apparait pas "en elle meme"
405         // dans le prescript, il y a seulement le nom
406         // du facteur. Charge à l'UI de savoir comment l'afficher
407         Assert.assertTrue(scriptContent.contains("context.setComputeValue(\"test.equation.name.K1\",9.8)"));
408     }
409 
410     /**
411      * Test que la generation du prescript avec l'ajout des populations
412      * de départ et des regles de IsisFish 3.4.0.0 fonctionne correctement.
413      * 
414      * @throws IsisFishException 
415      */
416     @Test
417     public void testPrescriptGenerationPopAndRules() throws IsisFishException {
418 
419         // get a context to a database
420         RegionStorage regionStorage = RegionStorage.getRegion("BaseMotosICA");
421         TopiaContext regionContext = regionStorage.getStorage();
422 
423         // build test scenario
424         Scenario scenario1 = new Scenario();
425 
426         // factor pop
427         Factor factorPop = new Factor("myPopulationParams");
428         factorPop.setDomain(new ContinuousDomain());
429         factorPop.setPath("parameters.population.xxx.number");
430         MatrixND mat = MatrixFactory.getInstance().create(new int[]{2, 3});
431         MatrixHelper.convertToId(mat);
432         factorPop.setValue(mat);
433 
434         // factor rule, species = c'est une des especes de la base BaseMotosICA
435         Rule ruleTacPoids = RuleStorage.getRule("TACpoids").getNewInstance();
436         Properties props = new Properties();
437         props.setProperty("rule.0.parameter.species", "fr.ifremer.isisfish.entities.Species#1169028645830#0.022262364425031378");
438         RuleHelper.populateRule(0, regionContext, ruleTacPoids, props);
439         Rule ruleTailleMin = RuleStorage.getRule("TailleMin").getNewInstance();
440         props = new Properties();
441         props.setProperty("rule.0.parameter.species", "fr.ifremer.isisfish.entities.Species#1169028645830#0.022262364425031378");
442         props.setProperty("rule.0.parameter.propSurvie", "42");
443         RuleHelper.populateRule(0, regionContext, ruleTailleMin, props);
444         Factor factorRule = new Factor("myRuleParams");
445         factorPop.setDomain(new RuleDiscreteDomain());
446         factorRule.setPath("parameters.rules");
447         List<Rule> rules = new ArrayList<Rule>();
448         rules.add(ruleTacPoids);
449         rules.add(ruleTailleMin);
450         factorRule.setValue(rules);
451 
452         List<Factor> factors = new ArrayList<Factor>();
453         factors.add(factorPop);
454         factors.add(factorRule);
455         scenario1.setFactors(factors);
456 
457         // generate prescript for that wonderfull scenario
458         String preScriptContent = FactorHelper.generatePreScript(scenario1);
459         log.info("Script = " + preScriptContent);
460 
461         Assert.assertTrue(preScriptContent.contains("params.setProperty(\"population.xxx.number\""));
462         Assert.assertTrue(preScriptContent.contains("params.setProperty(\"rule.0.parameter.species\",\"fr.ifremer.isisfish.entities.Species#1169028645830#0.022262364425031378:EngEnc\");"));
463         Assert.assertTrue(preScriptContent.contains("params.setProperty(\"rule.1.parameter.propSurvie\",\"42.0\");"));
464         Assert.assertTrue(preScriptContent.contains("rules\",\"TACpoids,TailleMin\")"));
465     }
466     
467     /**
468      * Test que la generation du prescript avec l'ajout des facteurs sur
469      * les parametres de regles.
470      * 
471      * @since 4.0.0.0
472      * @throws IsisFishException 
473      */
474     @Test
475     public void testPrescriptGenerationRulesParameters() throws IsisFishException {
476 
477         // build test scenario
478         Scenario scenario1 = new Scenario();
479 
480         // factor pop
481         Factor factorPop = new Factor("parameters of rule TACpoids");
482         factorPop.setDomain(new ContinuousDomain());
483         factorPop.setPath("parameters.rule.0.parameter.tacInTons");
484         factorPop.setValue(42.0);
485 
486         List<Factor> factors = new ArrayList<Factor>();
487         factors.add(factorPop);
488         scenario1.setFactors(factors);
489 
490         // generate prescript for that wonderfull scenario
491         String preScriptContent = FactorHelper.generatePreScript(scenario1);
492         log.info("Script = " + preScriptContent);
493 
494         Assert.assertTrue(preScriptContent.contains("params.setProperty(\"rule.0.parameter.tacInTons\",\"42.0\");"));
495     }
496     
497     /**
498      * Test que la generation du prescript avec l'ajout des groupes des facteurs
499      * depuis IsisFish 4.0.0.0 fonctionne correctement.
500      * 
501      * @throws IsisFishException
502      * 
503      * FIXME echatellier replace setValueForIdentifier code
504      */
505     @Ignore
506     public void testPrescriptGenerationGroups() throws IsisFishException {
507 
508         // build test scenario
509         Scenario scenario1 = new Scenario();
510 
511         // factor 1
512         Factor factor1 = new Factor("factor 1 (double)");
513         ContinuousDomain domain1 = new ContinuousDomain(Distribution.QUNIFMM);
514         domain1.addDistributionParam("min", 0.0);
515         domain1.addDistributionParam("max", 50.0);
516         factor1.setCardinality(4);
517         factor1.setDomain(domain1);
518         factor1.setPath("fr.ifremer.isisfish.entities.PopulationGroup#1156461521013#0.1715620681984218#maxLength");
519 
520         // factor 2
521         Factor factor2 = new Factor("factor 2 (double)");
522         ContinuousDomain domain2 = new ContinuousDomain(Distribution.QUNIFMM);
523         domain2.addDistributionParam("min", 1.0);
524         domain2.addDistributionParam("max", 9.0);
525         factor2.setCardinality(3);
526         factor2.setDomain(domain2);
527         factor2.setPath("fr.ifremer.isisfish.entities.PopulationGroup#1156461521064#0.022976136053553198#minLength");
528 
529         // factor 3
530         Factor factor3 = new Factor("factor 3 (double)");
531         DiscreteDomain domain3 = new DiscreteDomain();
532         domain3.getValues().put(0.0, 12.3);
533         domain3.getValues().put(1.0, 70.9);
534         domain3.getValues().put(2.0, 21.0);
535         domain3.getValues().put(3.0, -12.1);
536         domain3.getValues().put(4.0, -8.45);
537         factor3.setDomain(domain3);
538         factor3.setPath("fr.ifremer.isisfish.entities.PopulationGroup#1156461521013#0.1715620681984218#maxLength");
539 
540         // factor 4
541         Factor factor4 = new Factor("factor 4 (double)");
542         DiscreteDomain domain4 = new DiscreteDomain();
543         domain4.getValues().put(0.0, -7.3);
544         domain4.getValues().put(1.0, 4.9);
545         domain4.getValues().put(2.0, -42.0);
546         domain4.getValues().put(3.0, 27.1);
547         domain4.getValues().put(4.0, 53.32);
548         factor4.setDomain(domain4);
549         factor4.setPath("fr.ifremer.isisfish.entities.PopulationGroup#1156461521064#0.022976136053553198#minLength");
550         
551         // group continu
552         FactorGroup group1 = new FactorGroup("myContinuousGroup");
553         group1.addFactor(factor1);
554         group1.addFactor(factor2);
555         
556         // group discret
557         FactorGroup group2 = new FactorGroup("myDiscreteGroup");
558         group2.addFactor(factor3);
559         group2.addFactor(factor4);
560         
561         // compute AS => scenario
562         group1.setValueForIdentifier(0.75);
563         scenario1.addFactor(group1);
564         group2.setValueForIdentifier(2.0);
565         scenario1.addFactor(group2);
566 
567         // generate prescript for that wonderfull scenario
568         String preScriptContent = FactorHelper.generatePreScript(scenario1);
569         //log.info("Script = " + preScriptContent);
570 
571         Assert.assertTrue(preScriptContent.contains("/* factor group : myContinuousGroup */"));
572         Assert.assertTrue(preScriptContent.contains("beanUtils.convert(\"37.5\""));
573         Assert.assertTrue(preScriptContent.contains("beanUtils.convert(\"8.5\""));
574         Assert.assertTrue(preScriptContent.contains("/* factor group : myDiscreteGroup */"));
575         Assert.assertTrue(preScriptContent.contains("beanUtils.convert(\"21.0\""));
576         Assert.assertTrue(preScriptContent.contains("value4 = beanUtils.convert(\"-42.0\""));
577     }
578     
579     /**
580      * Test que la generation des prescript des equation discrete donne un résultat attendu.
581      * 
582      * @throws IsisFishException
583      */
584     @Test
585     public void testPrescriptGenerationDiscreteEquation() throws IsisFishException {
586 
587         // build test scenario
588         Scenario scenario1 = new Scenario();
589 
590         // factor 1
591         Factor factor1 = new Factor("factor 1 (equation)");
592         EquationDiscreteDomain domain1 = new EquationDiscreteDomain();
593         SortedMap<Object, Object> values = new TreeMap<Object, Object>();
594         values.put("1", "String myValue=\"test\";\nreturn 0.0;");
595         values.put("2", "String myValue=\"test2\";\nreturn 0.2;");
596         domain1.setValues(values);
597         factor1.setCardinality(2);
598         factor1.setDomain(domain1);
599         factor1.setPath("fr.ifremer.isisfish.entities.PopulationGroup#1156461521013#0.1715620681984218#growthEquation");
600         
601         // compute AS => scenario
602         factor1.setValueForIdentifier("2");
603 
604         scenario1.addFactor(factor1);
605 
606         // generate prescript for that wonderfull scenario
607         String preScriptContent = FactorHelper.generatePreScript(scenario1);
608         log.info("Script = " + preScriptContent);
609 
610         Assert.assertTrue(preScriptContent.contains("beanUtils.convert(\"String myValue=\\\"test2\\\""));
611         Assert.assertTrue(preScriptContent.contains("BeanUtils.setProperty(entity0, \"growthEquationContent\""));
612     }
613 }