View Javadoc
1   /*
2    * #%L
3    * IsisFish
4    * 
5    * $Id: FactorTest.java 4156 2014-12-09 11:27:18Z echatellier $
6    * $HeadURL: https://svn.codelutin.com/isis-fish/tags/isis-fish-4.4.0.2/src/test/java/fr/ifremer/isisfish/simulator/sensitivity/FactorTest.java $
7    * %%
8    * Copyright (C) 2009 - 2012 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.sensitivity;
27  
28  import java.util.ArrayList;
29  import java.util.Collection;
30  
31  import org.apache.commons.logging.Log;
32  import org.apache.commons.logging.LogFactory;
33  import org.junit.Assert;
34  import org.junit.Test;
35  import org.nuiton.math.matrix.MatrixFactory;
36  import org.nuiton.math.matrix.MatrixND;
37  
38  import fr.ifremer.isisfish.rule.Rule;
39  import fr.ifremer.isisfish.rule.RuleMock;
40  import fr.ifremer.isisfish.simulator.sensitivity.domain.ContinuousDomain;
41  import fr.ifremer.isisfish.simulator.sensitivity.domain.DiscreteDomain;
42  import fr.ifremer.isisfish.simulator.sensitivity.domain.RuleDiscreteDomain;
43  
44  /**
45   * Factors test.
46   * 
47   * @author chatellier
48   * @version $Revision: 4156 $
49   * 
50   * Last update : $Date: 2014-12-09 12:27:18 +0100 (Tue, 09 Dec 2014) $ By : $Author: echatellier $
51   */
52  public class FactorTest {
53  
54      /** to use log facility, just put in your code: log.info(\"...\"); */
55      private static Log log = LogFactory.getLog(FactorTest.class);
56  
57      /**
58       * Test to build new factors with int values.
59       */
60      @Test
61      public void testIntFactor() {
62  
63          Factor factor = new Factor("testint");
64          ContinuousDomain domain = new ContinuousDomain(Distribution.QUNIFMM);
65          domain.addDistributionParam("min", 0.0);
66          domain.addDistributionParam("max", 50.0);
67          factor.setDomain(domain);
68          factor.setPath("org.nuiton.factor#1234567890#0.12242345354#name");
69          factor.setValueForIdentifier(0.5);
70  
71          Assert.assertEquals(25.0, factor.getValue());
72  
73          if (log.isInfoEnabled()) {
74              log.info("factor#toString() = " + factor);
75          }
76      }
77  
78      /**
79       * Test factor with matrix.
80       * 
81       * @see MatrixND
82       */
83      @Test
84      public void testMatrixFactor() {
85  
86          // matrix 1
87          MatrixND matrix1 = MatrixFactory.getInstance().create("test1",
88                  new int[] { 3, 2 }, new String[] { "col1", "col2" });
89          matrix1.setValue(new int[] { 0, 0 }, 13);
90          matrix1.setValue(new int[] { 0, 1 }, -14);
91          matrix1.setValue(new int[] { 1, 0 }, 21);
92          matrix1.setValue(new int[] { 1, 1 }, 2);
93          matrix1.setValue(new int[] { 2, 0 }, 12);
94          matrix1.setValue(new int[] { 2, 1 }, -1);
95  
96          // matrix 2
97          MatrixND matrix2 = MatrixFactory.getInstance().create("test2",
98                  new int[] { 2, 3 }, new String[] { "col1", "col2" });
99          matrix2.setValue(new int[] { 0, 0 }, 9999);
100         matrix2.setValue(new int[] { 0, 1 }, 15000);
101         matrix2.setValue(new int[] { 0, 2 }, -40000);
102         matrix2.setValue(new int[] { 1, 0 }, 21345);
103         matrix2.setValue(new int[] { 1, 1 }, 81000);
104         matrix2.setValue(new int[] { 1, 2 }, -13000);
105 
106         // factor
107         Factor factor = new Factor("testmatrix");
108         DiscreteDomain domain = new DiscreteDomain();
109         domain.getValues().put("m1", matrix1);
110         domain.getValues().put("m2", matrix2);
111         factor.setDomain(domain);
112         factor.setPath("org.nuiton.math.matrix.MatrixND#563456293453#2.456347646#dim");
113         factor.setValueForIdentifier("m2");
114 
115         Assert.assertEquals(matrix2, factor.getValue());
116 
117         try {
118             factor.setValueForIdentifier("blah blah");
119             Assert.fail("Can't set identifier not present in domain");
120         } catch (IllegalArgumentException e) {
121             if (log.isInfoEnabled()) {
122                 log.info("Exception normally thrown");
123             }
124         }
125 
126         if (log.isInfoEnabled()) {
127             log.info("factor#toString() = " + factor);
128         }
129     }
130 
131     /**
132      * Test factor with matrix.
133      * 
134      * @see MatrixND
135      */
136     @Test
137     public void testEquationContinuousFactor() {
138 
139         // factor
140         Factor factor = new Factor("testequation");
141         ContinuousDomain domain = new ContinuousDomain(Distribution.QUNIFPC);
142         domain.addDistributionParam("reference", 3.0);
143         domain.addDistributionParam("coefficient", 0.1);
144         factor.setEquationVariableName("Linf");
145         factor.setDomain(domain);
146         factor.setPath("org.nuiton.math.matrix.MatrixND#563456293453#2.456347646#dim");
147         factor.setValueForIdentifier(0.1);
148 
149         Assert.assertEquals(2.76, (Double)factor.getValue(),0.0000001);
150         //Assert.assertEquals(2.7,(Double)((ContinuousDomain)factor.getDomain()).getMinBound(),0);
151         //Assert.assertEquals(3.3,(Double)((ContinuousDomain)factor.getDomain()).getMaxBound(),0);
152 
153         // factor 2
154         Factor factor2 = new Factor("testequation");
155         ContinuousDomain domain2 = new ContinuousDomain(Distribution.QUNIFPC);
156         domain2.addDistributionParam("reference", 3.0);
157         domain2.addDistributionParam("coefficient", 0.1);
158         factor2.setEquationVariableName("Linf");
159         factor2.setDomain(domain2);
160         factor2.setPath("org.nuiton.math.matrix.MatrixND#563456293453#2.456347646#dim");
161         factor2.setValueForIdentifier(0.1);
162 
163         Assert.assertEquals(2.76, (Double)factor2.getValue(),0.0000001);
164         //Assert.assertEquals(2.7,(Double)((ContinuousDomain)factor.getDomain()).getMinBound(),0);
165         //Assert.assertEquals(3.3,(Double)((ContinuousDomain)factor.getDomain()).getMaxBound(),0);
166 
167         // factor 3
168         Factor factor3 = new Factor("testequation");
169         ContinuousDomain domain3 = new ContinuousDomain(Distribution.QUNIFPC);
170         domain3.addDistributionParam("reference", 3.0);
171         domain3.addDistributionParam("coefficient", 0.1);
172         factor3.setEquationVariableName("Linf");
173         factor3.setDomain(domain3);
174         factor3.setPath("org.nuiton.math.matrix.MatrixND#563456293453#2.456347646#dim");
175         factor3.setValueForIdentifier(0.2);
176 
177         Assert.assertEquals(2.82, (Double)factor3.getValue(),0.0000001);
178         //Assert.assertEquals(2.7,(Double)((ContinuousDomain)factor.getDomain()).getMinBound(),0);
179         //Assert.assertEquals(3.3,(Double)((ContinuousDomain)factor.getDomain()).getMaxBound(),0);
180 
181         // factor 4
182         Factor factor4 = new Factor("testequation");
183         ContinuousDomain domain4 = new ContinuousDomain(Distribution.QUNIFPC);
184         domain4.addDistributionParam("reference", 3.0);
185         domain4.addDistributionParam("coefficient", 0.05);
186         factor4.setEquationVariableName("Linf");
187         factor4.setDomain(domain4);
188         factor4.setPath("org.nuiton.math.matrix.MatrixND#563456293453#2.456347646#dim");
189         factor4.setValueForIdentifier(0.1);
190 
191         Assert.assertEquals(2.88, (Double)factor4.getValue(),0.0000001);
192         //Assert.assertEquals(2.7,(Double)((ContinuousDomain)factor.getDomain()).getMinBound(),0);
193         //Assert.assertEquals(3.3,(Double)((ContinuousDomain)factor.getDomain()).getMaxBound(),0);
194 
195         if (log.isInfoEnabled()) {
196             log.info("factor#toString() = " + factor);
197         }
198     }
199     
200     /**
201      * Test factor with matrix.
202      * 
203      * @see MatrixND
204      */
205     @Test
206     public void testRuleFactor() {
207 
208         // available rules
209         Rule ruleA = new RuleMock("hour restriction");
210         Rule ruleB = new RuleMock("zone restriction");
211         Rule ruleC = new RuleMock("ship number restriction");
212 
213         // rules set
214         Collection<Rule> rules1 = new ArrayList<Rule>(); // 0 rule
215         Collection<Rule> rules2 = new ArrayList<Rule>(); // 1 rule
216         rules2.add(ruleA);
217         Collection<Rule> rules3 = new ArrayList<Rule>(); // 3 rule
218         rules3.add(ruleA);
219         rules3.add(ruleB);
220         rules3.add(ruleC);
221 
222         // factor and domain definition
223         Factor factor = new Factor("testrule");
224         RuleDiscreteDomain ruleDomain = new RuleDiscreteDomain();
225         ruleDomain.getValues().put("rules1", rules1);
226         ruleDomain.getValues().put("rules2", rules2);
227         ruleDomain.getValues().put("rules3", rules3);
228         factor.setDomain(ruleDomain);
229 
230         // TODO post r operation, and some asserts
231     }
232     
233     /**
234      * Assert that clone do work (with inheritance).
235      */
236     @Test
237     public void testClone() {
238         
239         Factor factor = new Factor("testclone");
240         factor.setPath("fr.ifremer.isisfish.entities.Cell#lenght");
241         factor.setComment("answer to life");
242         
243         ContinuousDomain domain = new ContinuousDomain(Distribution.QUNIFMM);
244         domain.addDistributionParam("min", 0.0);
245         domain.addDistributionParam("max", 50.0);
246         factor.setDomain(domain);
247         factor.setCardinality(2);
248 
249         factor.setValueForIdentifier(0.84);
250 
251         // clone
252         Factor clone = (Factor)factor.clone();
253 
254         // test on clone
255         Assert.assertEquals("testclone", clone.getName());
256         Assert.assertEquals("fr.ifremer.isisfish.entities.Cell#lenght", clone.getPath());
257         Assert.assertEquals("answer to life", clone.getComment());
258         Assert.assertEquals(42.0, clone.getValue());
259         Assert.assertNotSame(domain, clone.getDomain());
260         Assert.assertEquals(42.0, clone.getDomain().getValueForIdentifier(0.84));
261         Assert.assertEquals(2, clone.getCardinality());
262     }
263 }