Statistics
| Revision:

root / trunk / extensions / extGraph / src / org / gvsig / graph / gui / ODMatrixTask.java @ 31735

History | View | Annotate | Download (3.96 KB)

1
/* gvSIG. Geographic Information System of the Valencian Government
2
*
3
* Copyright (C) 2007-2008 Infrastructures and Transports Department
4
* of the Valencian Government (CIT)
5
* 
6
* This program is free software; you can redistribute it and/or
7
* modify it under the terms of the GNU General Public License
8
* as published by the Free Software Foundation; either version 2
9
* of the License, or (at your option) any later version.
10
* 
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
* GNU General Public License for more details.
15
* 
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 
19
* MA  02110-1301, USA.
20
* 
21
*/
22

    
23
/*
24
* AUTHORS (In addition to CIT):
25
* 2008 Software Colaborativo (www.scolab.es)   development
26
*/
27
 
28
package org.gvsig.graph.gui;
29

    
30
import java.awt.Component;
31
import java.io.File;
32
import java.io.IOException;
33

    
34
import javax.swing.JOptionPane;
35

    
36
import org.gvsig.graph.IODMatrixFileWriter;
37
import org.gvsig.graph.core.GraphException;
38
import org.gvsig.graph.core.GvFlag;
39
import org.gvsig.graph.core.Network;
40
import org.gvsig.graph.solvers.OneToManySolver;
41

    
42
import com.iver.andami.PluginServices;
43
import com.iver.utiles.swing.threads.AbstractMonitorableTask;
44

    
45
public class ODMatrixTask extends AbstractMonitorableTask{
46

    
47
        private Network net;
48
        private GvFlag[] originFlags;
49
        private GvFlag[] destinationFlags;
50
        private File generatedFile;
51
        private IODMatrixFileWriter selectedWriter;
52

    
53
        public ODMatrixTask(Network net, GvFlag[] originFlags,
54
                        GvFlag[] destinationFlags, File generatedFile, 
55
                        IODMatrixFileWriter writer) {
56
                this.net = net;
57
                this.originFlags = originFlags;
58
                this.destinationFlags = destinationFlags;
59
                this.generatedFile = generatedFile;
60
                this.selectedWriter = writer;
61
                
62
                setInitialStep(0);
63
                setDeterminatedProcess(true);
64
                setStatusMessage(PluginServices.getText(this, "calculating odmatrix"));
65

    
66
                setFinalStep(originFlags.length); // Importante: Fijarlo en el constructor, para que est? establecido antes del run() <br>
67
                
68
        }
69
        
70
        public void run() throws Exception {
71
                try {                                
72
                        selectedWriter.openFile(generatedFile);
73

    
74
                        OneToManySolver solver = new OneToManySolver();
75
                        solver.setNetwork(net);
76
                        solver.putDestinationsOnNetwork(destinationFlags);
77
                        solver.setExploreAllNetwork(true);
78
                        for (int i=0; i < originFlags.length; i++)
79
                        {                                
80
                                if (isCanceled())
81
                                        break;
82
                                
83
                                solver.setSourceFlag(originFlags[i]);
84
                                long t1 = System.currentTimeMillis();
85
                                
86
                                solver.calculate();
87
                                long t2 = System.currentTimeMillis();
88
                                System.out.println("Punto " + i + " de " + originFlags.length + ". " + (t2-t1) + " msecs.");
89
                                
90
                                for (int j=0; j < destinationFlags.length; j++)
91
                                {
92
                                        selectedWriter.saveDist(i, j, destinationFlags[j].getCost()
93
                                                        , destinationFlags[j].getAccumulatedLength());
94
                                }
95
                                long t3 = System.currentTimeMillis();
96
                                System.out.println("T. de escritura: " + (t3-t2) + " msecs.");
97
                                setNote(PluginServices.getText(this, "origin_odmatrix") 
98
                                                + " " + (i +1) + 
99
                                                ": " + destinationFlags.length + " " + 
100
                                                PluginServices.getText(this, "destinations_odmatrix_written") +
101
                                                " " + (t3-t1) + " msecs"
102
                                                ) ; 
103
                                reportStep();        
104
                        }
105
                        solver.removeDestinationsFromNetwork(destinationFlags);
106
                        solver.removeDestinationsFromNetwork(originFlags);
107
                        selectedWriter.closeFile();
108
                        net.removeFlags();
109
                        if (!isCanceled())
110
                                JOptionPane.showMessageDialog((Component) PluginServices.getMainFrame(),
111
                                        PluginServices.getText(this,"file_generated"));
112
                } catch (IOException e) {
113
                        // TODO Auto-generated catch block
114
                        e.printStackTrace();
115
                } catch (GraphException e) {
116
                        // TODO Auto-generated catch block
117
                        e.printStackTrace();
118
                }
119

    
120
                
121
        }
122

    
123
}
124