Author: echatellier Date: 2013-11-03 21:47:26 +0100 (Sun, 03 Nov 2013) New Revision: 3829 Url: http://forge.codelutin.com/projects/isis-fish/repository/revisions/3829 Log: Add unit test to test AS scripts Added: branches/4.0.1/src/test/java/fr/ifremer/isisfish/simulator/sensitivity/SensitivityAnalysisTest.java Added: branches/4.0.1/src/test/java/fr/ifremer/isisfish/simulator/sensitivity/SensitivityAnalysisTest.java =================================================================== --- branches/4.0.1/src/test/java/fr/ifremer/isisfish/simulator/sensitivity/SensitivityAnalysisTest.java (rev 0) +++ branches/4.0.1/src/test/java/fr/ifremer/isisfish/simulator/sensitivity/SensitivityAnalysisTest.java 2013-11-03 20:47:26 UTC (rev 3829) @@ -0,0 +1,262 @@ +/* + * #%L + * IsisFish + * + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2013 Ifremer, Code Lutin, Chatellier Eric + * %% + * 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 3 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, see + * <http://www.gnu.org/licenses/gpl-3.0.html>. + * #L% + */ +package fr.ifremer.isisfish.simulator.sensitivity; + +import java.io.File; +import java.io.IOException; + +import org.apache.commons.io.FileUtils; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.aspectj.util.FileUtil; +import org.junit.After; +import org.junit.Assert; +import org.junit.Assume; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.nuiton.j2r.RException; +import org.nuiton.j2r.RProxy; +import org.nuiton.math.matrix.MatrixFactory; +import org.nuiton.math.matrix.MatrixND; + +import fr.ifremer.isisfish.AbstractIsisFishTest; +import fr.ifremer.isisfish.IsisFishException; +import fr.ifremer.isisfish.datastore.SensitivityAnalysisStorage; +import fr.ifremer.isisfish.simulator.sensitivity.domain.ContinuousDomain; +import fr.ifremer.isisfish.simulator.sensitivity.domain.DiscreteDomain; + +/** + * Test les scripts d'AS disponible dans les ressources de tests. + * + * @author Eric Chatellier + */ +public class SensitivityAnalysisTest extends AbstractIsisFishTest { + + private static final Log log = LogFactory.getLog(SensitivityAnalysisTest.class); + + /** Simulation dir used to save Rdata files. */ + protected File simulationsDir; + + @BeforeClass + public static void init() { + System.setProperty("R.type", "jni"); + } + + /** + * Ces tests utilisent directement le moteur R, s'il n'est pas disponible, + * on va eviter de faire les tests. + */ + @Before + public void setUp() { + try { + new RProxy(); + } catch (RException e) { + if (log.isDebugEnabled()) { + log.debug("R not available. Skipping current test"); + } + Assume.assumeNoException(e); + } + simulationsDir = FileUtil.getTempDir("simulations"); + } + + /** + * Build a test design plan with only continuous factors. + * + * @return test design plan + */ + protected DesignPlan getContinuousDesignPlan() { + + DesignPlan result = new DesignPlan(); + + // factor 1, min/max on int + Factor factor1 = new Factor("testint"); + ContinuousDomain domain1 = new ContinuousDomain(Distribution.QUNIFMM); + domain1.addDistributionParam("min", 0.0); + domain1.addDistributionParam("max", 50.0); + factor1.setDomain(domain1); + factor1.setPath("org.nuiton.factor#1234567890#0.12242345354#name"); + result.addFactor(factor1); + + // matrix 1 + MatrixND matrix1 = MatrixFactory.getInstance().create("test1", + new int[] { 3, 2 }, new String[] { "col1", "col2" }); + matrix1.setValue(new int[] { 0, 0 }, 1); + matrix1.setValue(new int[] { 0, 1 }, -14); + matrix1.setValue(new int[] { 1, 0 }, 21); + matrix1.setValue(new int[] { 1, 1 }, 2); + matrix1.setValue(new int[] { 2, 0 }, 3); + matrix1.setValue(new int[] { 2, 1 }, -1); + + // factor + Factor factor2 = new Factor("testmatrix"); + ContinuousDomain domain2 = new ContinuousDomain(Distribution.QUNIFPC); + domain2.addDistributionParam("reference", matrix1); + domain2.addDistributionParam("coefficient", 0.1); + factor2.setDomain(domain2); + factor2.setPath("org.nuiton.math.matrix.MatrixND#563456293453#2.456347646#dim"); + result.addFactor(factor2); + + return result; + } + + /** + * Test tmp files/dir. + * + * @throws IOException + */ + @After + public void clear() throws IOException { + FileUtils.deleteDirectory(simulationsDir); + } + + /** + * Test de génération des scenarios via la methode Morris. + * + * @throws IsisFishException + * @throws SensitivityException + */ + @Test + public void testMorris() throws IsisFishException, SensitivityException { + SensitivityAnalysis script = SensitivityAnalysisStorage.getSensitivityAnalysis("Morris").getNewSensitivityAnalysisInstance(); + Assert.assertNotNull("Morris script not found in test data", script); + + DesignPlan designPlan = getContinuousDesignPlan(); + SensitivityScenarios scenarii = script.compute(designPlan, simulationsDir); + + } + + /** + * Test de génération des scenarios via la methode DOptimal. + * + * @throws IsisFishException + * @throws SensitivityException + */ + @Test + public void testDOptimal() throws IsisFishException, SensitivityException { + SensitivityAnalysis script = SensitivityAnalysisStorage.getSensitivityAnalysis("DOptimal").getNewSensitivityAnalysisInstance(); + Assert.assertNotNull("DOptimal script not found in test data", script); + + DesignPlan designPlan = getContinuousDesignPlan(); + SensitivityScenarios scenarii = script.compute(designPlan, simulationsDir); + + } + + /** + * Test de génération des scenarios via la methode Fast. + * + * @throws IsisFishException + * @throws SensitivityException + */ + @Test + public void testFast() throws IsisFishException, SensitivityException { + SensitivityAnalysis script = SensitivityAnalysisStorage.getSensitivityAnalysis("Fast").getNewSensitivityAnalysisInstance(); + Assert.assertNotNull("Fast script not found in test data", script); + + DesignPlan designPlan = getContinuousDesignPlan(); + SensitivityScenarios scenarii = script.compute(designPlan, simulationsDir); + + } + + /** + * Test de génération des scenarios via la methode OptimumLHS. + * + * @throws IsisFishException + * @throws SensitivityException + */ + @Test + public void testOptimumLHS() throws IsisFishException, SensitivityException { + SensitivityAnalysis script = SensitivityAnalysisStorage.getSensitivityAnalysis("OptimumLHS").getNewSensitivityAnalysisInstance(); + Assert.assertNotNull("OptimumLHS script not found in test data", script); + + DesignPlan designPlan = getContinuousDesignPlan(); + SensitivityScenarios scenarii = script.compute(designPlan, simulationsDir); + + } + + /** + * Test de génération des scenarios via la methode RandomLHS. + * + * @throws IsisFishException + * @throws SensitivityException + */ + @Test + public void testRandomLHS() throws IsisFishException, SensitivityException { + SensitivityAnalysis script = SensitivityAnalysisStorage.getSensitivityAnalysis("RandomLHS").getNewSensitivityAnalysisInstance(); + Assert.assertNotNull("RandomLHS script not found in test data", script); + + DesignPlan designPlan = getContinuousDesignPlan(); + SensitivityScenarios scenarii = script.compute(designPlan, simulationsDir); + + } + + /** + * Test de génération des scenarios via la methode RegularExpandGrid. + * + * @throws IsisFishException + * @throws SensitivityException + */ + @Test + public void testRegularExpandGrid() throws IsisFishException, SensitivityException { + SensitivityAnalysis script = SensitivityAnalysisStorage.getSensitivityAnalysis("RegularExpandGrid").getNewSensitivityAnalysisInstance(); + Assert.assertNotNull("RegularExpandGrid script not found in test data", script); + + DesignPlan designPlan = getContinuousDesignPlan(); + SensitivityScenarios scenarii = script.compute(designPlan, simulationsDir); + + } + + /** + * Test de génération des scenarios via la methode RegularFractions. + * + * @throws IsisFishException + * @throws SensitivityException + */ + @Test + public void testRegularFractions() throws IsisFishException, SensitivityException { + SensitivityAnalysis script = SensitivityAnalysisStorage.getSensitivityAnalysis("RegularFractions").getNewSensitivityAnalysisInstance(); + Assert.assertNotNull("RegularFractions script not found in test data", script); + + DesignPlan designPlan = getContinuousDesignPlan(); + SensitivityScenarios scenarii = script.compute(designPlan, simulationsDir); + + } + + /** + * Test de génération des scenarios via la methode RegularExpandGrid. + * + * @throws IsisFishException + * @throws SensitivityException + */ + @Test + public void testSobol() throws IsisFishException, SensitivityException { + SensitivityAnalysis script = SensitivityAnalysisStorage.getSensitivityAnalysis("Sobol").getNewSensitivityAnalysisInstance(); + Assert.assertNotNull("Sobol script not found in test data", script); + + DesignPlan designPlan = getContinuousDesignPlan(); + SensitivityScenarios scenarii = script.compute(designPlan, simulationsDir); + + } +}