View Javadoc
1   /*
2    * #%L
3    * IsisFish
4    * 
5    * $Id: FactorTreeModelTest.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/ui/sensitivity/FactorTreeModelTest.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.ui.sensitivity;
27  
28  import static org.nuiton.i18n.I18n.t;
29  
30  import java.awt.HeadlessException;
31  import javax.swing.JDialog;
32  import javax.swing.JOptionPane;
33  import javax.swing.JTree;
34  import javax.swing.SwingUtilities;
35  
36  import org.apache.commons.logging.Log;
37  import org.apache.commons.logging.LogFactory;
38  import org.junit.Assume;
39  import org.junit.BeforeClass;
40  import org.junit.Test;
41  import org.nuiton.math.matrix.MatrixFactory;
42  import org.nuiton.math.matrix.MatrixND;
43  
44  import fr.ifremer.isisfish.AbstractIsisFishTest;
45  import fr.ifremer.isisfish.simulator.sensitivity.Distribution;
46  import fr.ifremer.isisfish.simulator.sensitivity.Factor;
47  import fr.ifremer.isisfish.simulator.sensitivity.FactorGroup;
48  import fr.ifremer.isisfish.simulator.sensitivity.domain.ContinuousDomain;
49  import fr.ifremer.isisfish.simulator.sensitivity.domain.DiscreteDomain;
50  import fr.ifremer.isisfish.ui.sensitivity.model.FactorTreeCellRenderer;
51  import fr.ifremer.isisfish.ui.sensitivity.model.FactorTreeModel;
52  
53  /**
54   * Sensitivity tree model test.
55   *
56   * Created: 29 juin 2006 20:19:32
57   *
58   * @author poussin
59   * @version $Revision: 4156 $
60   *
61   * Last update: $Date: 2014-12-09 12:27:18 +0100 (Tue, 09 Dec 2014) $
62   * by : $Author: echatellier $
63   */
64  public class FactorTreeModelTest extends AbstractIsisFishTest {
65  
66      /** Logger for this class */
67      private static final Log log = LogFactory.getLog(FactorTreeModelTest.class);
68  
69      @BeforeClass
70      public static void disableTests() {
71          Assume.assumeTrue(!java.awt.GraphicsEnvironment.isHeadless());
72      }
73  
74      /**
75       * Return une liste de facteur à afficher.
76       * 
77       * @return la liste des facteur
78       */
79      protected FactorGroup getFactorGroup() {
80          
81          Factor factor1 = new Factor(
82                  "testint");
83          ContinuousDomain domain1 = new ContinuousDomain(Distribution.QUNIFMM);
84          domain1.addDistributionParam("min", 0.0);
85          domain1.addDistributionParam("max", 50.0);
86          factor1.setDomain(domain1);
87          factor1.setPath("org.nuiton.factor#1234567890#0.12242345354#name");
88          factor1.setValueForIdentifier(0.5);
89  
90          // matrix 1
91          MatrixND matrix1 = MatrixFactory.getInstance().create("test1",
92                  new int[] { 3, 2 }, new String[] { "col1", "col2" });
93          matrix1.setValue(new int[] { 0, 0 }, 13);
94          matrix1.setValue(new int[] { 0, 1 }, -14);
95          matrix1.setValue(new int[] { 1, 0 }, 21);
96          matrix1.setValue(new int[] { 1, 1 }, 2);
97          matrix1.setValue(new int[] { 2, 0 }, 12);
98          matrix1.setValue(new int[] { 2, 1 }, -1);
99  
100         // matrix 2
101         MatrixND matrix2 = MatrixFactory.getInstance().create("test2",
102                 new int[] { 2, 3 }, new String[] { "col1", "col2" });
103         matrix2.setValue(new int[] { 0, 0 }, 9999);
104         matrix2.setValue(new int[] { 0, 1 }, 15000);
105         matrix2.setValue(new int[] { 0, 2 }, -40000);
106         matrix2.setValue(new int[] { 1, 0 }, 21345);
107         matrix2.setValue(new int[] { 1, 1 }, 81000);
108         matrix2.setValue(new int[] { 1, 2 }, -13000);
109 
110         // factor
111         Factor factor2 = new Factor(
112                 "testmatrix");
113         DiscreteDomain domain2 = new DiscreteDomain();
114         domain2.getValues().put("m1", matrix1);
115         domain2.getValues().put("m2", matrix2);
116         factor2.setDomain(domain2);
117         factor2.setPath("org.nuiton.math.matrix.MatrixND#563456293453#2.456347646#dim");
118         factor2.setValueForIdentifier("m2");
119 
120         FactorGroup factorGroup = new FactorGroup("test");
121         factorGroup.addFactor(factor1);
122         factorGroup.addFactor(factor2);
123 
124         return factorGroup;
125     }
126 
127     /*
128      * Test tree rendering.
129      */
130     @Test
131     public void testJTreeModel() throws InterruptedException {
132 
133         final JTree tree = new JTree();
134         tree.setRootVisible(true);
135         FactorTreeModel model = new FactorTreeModel(getFactorGroup());
136         tree.setModel(model);
137         tree.setCellRenderer(new FactorTreeCellRenderer());
138 
139         try {
140             final JDialog dialog = new JDialog();
141             SwingUtilities.invokeLater(new Runnable() {
142                 @Override
143                 public void run() {
144                     JOptionPane.showMessageDialog(dialog, tree,
145                             t("Tree factor model"),
146                             JOptionPane.INFORMATION_MESSAGE);
147                 }
148             });
149 
150             Thread t = new Thread(new Runnable() {
151                 public void run() {
152                     try {
153                         Thread.sleep(1000);
154                     } catch (InterruptedException e) {
155                         // do nothing
156                     }
157                     dialog.dispose();
158                 }
159             });
160             t.start();
161 
162             // it would really be nice to wait the thread, otherwise the test
163             // means nothing!!!
164             t.join();
165         } catch (HeadlessException he) {
166             if (log.isErrorEnabled()) {
167                 log.error("No X11 display available", he);
168             }
169         }
170     }
171 }