View Javadoc
1   /*
2    * #%L
3    * IsisFish
4    * 
5    * $Id: PopulationSeasonInfoTest.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/entity/PopulationSeasonInfoTest.java $
7    * %%
8    * Copyright (C) 2006 - 2010 Ifremer, Code Lutin, Cédric Pineau, Benjamin Poussin
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.entity;
27  
28  import static org.nuiton.i18n.I18n.t;
29  
30  import java.awt.HeadlessException;
31  import java.util.ArrayList;
32  import java.util.List;
33  
34  import javax.swing.Box;
35  import javax.swing.JDialog;
36  import javax.swing.JOptionPane;
37  import javax.swing.SwingUtilities;
38  
39  import org.apache.commons.logging.Log;
40  import org.apache.commons.logging.LogFactory;
41  import org.nuiton.math.matrix.MatrixFactory;
42  import org.nuiton.math.matrix.MatrixIterator;
43  import org.nuiton.math.matrix.MatrixND;
44  import org.nuiton.math.matrix.gui.MatrixPanelEditor;
45  import org.junit.Test;
46  
47  import fr.ifremer.isisfish.AbstractIsisFishTest;
48  
49  /**
50   * PopulationSeasonInfoTest.
51   *
52   * Created: 29 juin 2006 20:19:32
53   *
54   * @author poussin
55   * @version $Revision: 4156 $
56   *
57   * Last update: $Date: 2014-12-09 12:27:18 +0100 (Tue, 09 Dec 2014) $
58   * by : $Author: echatellier $
59   */
60  public class PopulationSeasonInfoTest extends AbstractIsisFishTest {
61  
62      /** Logger for this class */
63      private static final Log log = LogFactory.getLog(PopulationSeasonInfoTest.class);
64  
65      /*
66       * Test method for 'fr.ifremer.isisfish.entities.PopulationSeasonInfoImpl.getGroupChangeMatrix(Month)'
67       */
68      @Test
69      public void testGetGroupChangeMatrix() throws InterruptedException {
70  
71          int nbrAge = 3;
72          int nbrZone = 2;
73          boolean groupplus = true;
74  
75          List<String> sem = new ArrayList<String>();
76  
77          for (int i = 0; i < nbrAge; i++) {
78              for (int j = 0; j < nbrZone; j++) {
79                  sem.add("g" + i + "/z" + j);
80              }
81          }
82  
83          MatrixND mat = MatrixFactory.getInstance().create(
84                  new List[] { sem, sem });
85          for (MatrixIterator mi = mat.iterator(); mi.next();) {
86              int[] dim = mi.getCoordinates();
87              int i = dim[0];
88              int j = dim[1];
89  
90              if (
91              // un element de la diagonale dans le block choisi
92              (i + nbrZone == j) || // calcul pour savoir s'il y a le groupe plus
93                      (groupplus
94                      // regarde si on est bien dans le dernier block
95                              && (nbrAge - 1 == i / nbrZone)
96                      // regarde si on est bien sur la diagonal
97                      && (i == j))) {
98                  mi.setValue(1);
99              }
100         }
101 
102         final MatrixPanelEditor panel = new MatrixPanelEditor(false, 800, 300);
103         panel.setMatrix(mat);
104         try {
105             final JDialog dialog = new JDialog();
106             SwingUtilities.invokeLater(new Runnable() {
107                 @Override
108                 public void run() {
109 
110                     JOptionPane.showMessageDialog(dialog, panel,
111                             t("Spacialized visualisation"),
112                             JOptionPane.INFORMATION_MESSAGE);
113 
114                 }
115             });
116 
117             Thread t = new Thread(new Runnable() {
118                 public void run() {
119                     try {
120                         Thread.sleep(1000);
121                     } catch (InterruptedException e) {
122                         // do nothing
123                     }
124                     dialog.dispose();
125                 }
126             });
127             t.start();
128 
129             // it would really be nice to wait the thread, otherwise the test
130             // means nothing!!!
131             t.join();
132         } catch (HeadlessException he) {
133             if (log.isErrorEnabled()) {
134                 log.error("No X11 display available", he);
135             }
136         }
137     }
138 
139     /**
140      * Converte no spacialized matrix to spacialized matrix
141      */
142     @Test
143     public void testSpacializeLengthChangeMatrix() throws InterruptedException {
144 
145         int nbsecteurs = 2;
146         int nbclasses = 3;
147 
148         MatrixND mat = MatrixFactory.getInstance().create(
149                 new int[] { nbclasses, nbclasses });
150         int i = 1;
151         for (MatrixIterator mi = mat.iterator(); mi.next();) {
152             mi.setValue(i++);
153         }
154 
155         List<String> sem = new ArrayList<String>();
156 
157         for (i = 0; i < nbclasses; i++) {
158             for (int j = 0; j < nbsecteurs; j++) {
159                 sem.add("g" + i + "/z" + j);
160             }
161         }
162 
163         MatrixND bigmat = MatrixFactory.getInstance().create(
164                 new List[] { sem, sem });
165 
166         for (i = 0; i < nbclasses; i++) {
167             for (int j = 0; j < nbclasses; j++) {
168                 MatrixND matId = MatrixFactory.getInstance().matrixId(
169                         nbsecteurs);
170                 matId.mults(mat.getValue(i, j));
171                 bigmat.paste(new int[] { i * nbsecteurs, j * nbsecteurs },
172                         matId);
173             }
174         }
175 
176         MatrixPanelEditor panel = new MatrixPanelEditor(false, 800, 300);
177         panel.setMatrix(mat);
178         MatrixPanelEditor bigpanel = new MatrixPanelEditor(false, 800, 300);
179         bigpanel.setMatrix(bigmat);
180 
181         final Box box = Box.createVerticalBox();
182         box.add(panel);
183         box.add(bigpanel);
184 
185         try {
186             final JDialog dialog = new JDialog();
187             SwingUtilities.invokeLater(new Runnable() {
188                 @Override
189                 public void run() {
190 
191                     JOptionPane.showMessageDialog(dialog, box,
192                             t("Spacialized visualisation"),
193                             JOptionPane.INFORMATION_MESSAGE);
194 
195                 }
196             });
197 
198             Thread t = new Thread(new Runnable() {
199                 public void run() {
200                     try {
201                         Thread.sleep(1000);
202                     } catch (InterruptedException e) {
203                         // do nothing
204                     }
205                     dialog.dispose();
206                 }
207             });
208             t.start();
209 
210             // it would really be nice to wait the thread, otherwise the test
211             // means nothing!!!
212             t.join();
213         } catch (HeadlessException he) {
214             if (log.isErrorEnabled()) {
215                 log.error("No X11 display available", he);
216             }
217         }
218     }
219 
220 }