1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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
46
47
48
49
50
51
52 public class FactorTest {
53
54
55 private static Log log = LogFactory.getLog(FactorTest.class);
56
57
58
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
80
81
82
83 @Test
84 public void testMatrixFactor() {
85
86
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
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
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
133
134
135
136 @Test
137 public void testEquationContinuousFactor() {
138
139
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
151
152
153
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
165
166
167
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
179
180
181
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
193
194
195 if (log.isInfoEnabled()) {
196 log.info("factor#toString() = " + factor);
197 }
198 }
199
200
201
202
203
204
205 @Test
206 public void testRuleFactor() {
207
208
209 Rule ruleA = new RuleMock("hour restriction");
210 Rule ruleB = new RuleMock("zone restriction");
211 Rule ruleC = new RuleMock("ship number restriction");
212
213
214 Collection<Rule> rules1 = new ArrayList<Rule>();
215 Collection<Rule> rules2 = new ArrayList<Rule>();
216 rules2.add(ruleA);
217 Collection<Rule> rules3 = new ArrayList<Rule>();
218 rules3.add(ruleA);
219 rules3.add(ruleB);
220 rules3.add(ruleC);
221
222
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
231 }
232
233
234
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
252 Factor clone = (Factor)factor.clone();
253
254
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 }