~keheliya-gallaba/gephi/maven-build

« back to all changes in this revision

Viewing changes to exporter.plugin/src/main/java/org/gephi/io/exporter/plugin/ExporterCSV.java

  • Committer: Keheliya Gallaba
  • Date: 2011-08-01 13:01:30 UTC
  • Revision ID: keheliya.gallaba@gmail.com-20110801130130-0u2qgcufi8bvqwxv
Adding Export Plugin, Export Plugin UI, Filters Impl

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
Copyright 2008-2010 Gephi
 
3
Authors : Mathieu Bastian <mathieu.bastian@gephi.org>
 
4
Website : http://www.gephi.org
 
5
 
 
6
This file is part of Gephi.
 
7
 
 
8
Gephi is free software: you can redistribute it and/or modify
 
9
it under the terms of the GNU Affero General Public License as
 
10
published by the Free Software Foundation, either version 3 of the
 
11
License, or (at your option) any later version.
 
12
 
 
13
Gephi is distributed in the hope that it will be useful,
 
14
but WITHOUT ANY WARRANTY; without even the implied warranty of
 
15
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
16
GNU Affero General Public License for more details.
 
17
 
 
18
You should have received a copy of the GNU Affero General Public License
 
19
along with Gephi.  If not, see <http://www.gnu.org/licenses/>.
 
20
 */
 
21
package org.gephi.io.exporter.plugin;
 
22
 
 
23
import java.io.IOException;
 
24
import java.io.Writer;
 
25
import java.util.ArrayList;
 
26
import java.util.List;
 
27
import org.gephi.graph.api.DirectedGraph;
 
28
import org.gephi.graph.api.Edge;
 
29
import org.gephi.graph.api.Graph;
 
30
import org.gephi.graph.api.GraphModel;
 
31
import org.gephi.graph.api.HierarchicalDirectedGraph;
 
32
import org.gephi.graph.api.HierarchicalGraph;
 
33
import org.gephi.graph.api.HierarchicalMixedGraph;
 
34
import org.gephi.graph.api.HierarchicalUndirectedGraph;
 
35
import org.gephi.graph.api.MixedGraph;
 
36
import org.gephi.graph.api.Node;
 
37
import org.gephi.graph.api.UndirectedGraph;
 
38
import org.gephi.io.exporter.spi.GraphExporter;
 
39
import org.gephi.io.exporter.spi.CharacterExporter;
 
40
import org.gephi.project.api.Workspace;
 
41
import org.gephi.utils.longtask.spi.LongTask;
 
42
import org.gephi.utils.progress.Progress;
 
43
import org.gephi.utils.progress.ProgressTicket;
 
44
 
 
45
/**
 
46
 *
 
47
 * @author Mathieu Bastian
 
48
 */
 
49
public class ExporterCSV implements GraphExporter, CharacterExporter, LongTask {
 
50
 
 
51
    private static final String SEPARATOR = ";";
 
52
    private static final String EOL = "\n";
 
53
    //Settings
 
54
    private boolean edgeWeight = true;
 
55
    private boolean writeZero = true;
 
56
    private boolean header = true;
 
57
    private boolean list = false;
 
58
    //Architecture
 
59
    private Workspace workspace;
 
60
    private Writer writer;
 
61
    private boolean exportVisible;
 
62
    private boolean cancel = false;
 
63
    private ProgressTicket progressTicket;
 
64
 
 
65
    public boolean execute() {
 
66
        GraphModel graphModel = workspace.getLookup().lookup(GraphModel.class);
 
67
        Graph graph = null;
 
68
        if (exportVisible) {
 
69
            graph = graphModel.getGraphVisible();
 
70
        } else {
 
71
            graph = graphModel.getGraph();
 
72
        }
 
73
        try {
 
74
            exportData(graph);
 
75
        } catch (Exception e) {
 
76
            throw new RuntimeException(e);
 
77
        }
 
78
 
 
79
        return !cancel;
 
80
    }
 
81
 
 
82
    private void exportData(Graph graph) throws Exception {
 
83
        int max = graph.getNodeCount();
 
84
 
 
85
        Progress.start(progressTicket, max);
 
86
 
 
87
        if (!list) {
 
88
            if (header) {
 
89
                writer.append(SEPARATOR);
 
90
                Node[] nodes = graph.getNodes().toArray();
 
91
                for (int i = 0; i < nodes.length; i++) {
 
92
                    writeMatrixNode(nodes[i], i < nodes.length - 1);
 
93
                }
 
94
                writer.append(EOL);
 
95
            }
 
96
        }
 
97
 
 
98
        if (list) {
 
99
            Node[] nodes = graph.getNodes().toArray();
 
100
            for (int i = 0; i < nodes.length; i++) {
 
101
                Node n = nodes[i];
 
102
                List<Node> neighbours = new ArrayList<Node>();
 
103
                for (Edge e : graph.getEdges(n)) {
 
104
                    if (!e.isDirected() || (e.isDirected() && n == e.getSource())) {
 
105
                        Node m = graph.getOpposite(n, e);
 
106
                        neighbours.add(m);
 
107
                    }
 
108
                }
 
109
 
 
110
                for (Edge e : ((HierarchicalGraph) graph).getMetaEdges(n)) {
 
111
                    if (!e.isDirected() || (e.isDirected() && n == e.getSource())) {
 
112
                        Node m = graph.getOpposite(n, e);
 
113
                        neighbours.add(m);
 
114
                    }
 
115
                }
 
116
 
 
117
                writeListNode(n, !neighbours.isEmpty());
 
118
                for (int j = 0; j < neighbours.size(); j++) {
 
119
                    writeListNode(neighbours.get(j), j < neighbours.size() - 1);
 
120
 
 
121
                }
 
122
                writer.append(EOL);
 
123
            }
 
124
        } else {
 
125
            if (graph instanceof DirectedGraph) {
 
126
                DirectedGraph directedGraph = (DirectedGraph) graph;
 
127
                Node[] nodes = graph.getNodes().toArray();
 
128
                for (Node n : nodes) {
 
129
                    if (cancel) {
 
130
                        break;
 
131
                    }
 
132
                    writeMatrixNode(n, true);
 
133
                    for (int j = 0; j < nodes.length; j++) {
 
134
                        Node m = nodes[j];
 
135
                        Edge e = directedGraph.getEdge(n, m);
 
136
                        e = e == null ? ((HierarchicalDirectedGraph) directedGraph).getMetaEdge(n, m) : e;
 
137
                        writeEdge(e, j < nodes.length - 1);
 
138
                    }
 
139
                    Progress.progress(progressTicket);
 
140
                    writer.append(EOL);
 
141
                }
 
142
            } else if (graph instanceof UndirectedGraph) {
 
143
                UndirectedGraph undirectedGraph = (UndirectedGraph) graph;
 
144
                Node[] nodes = graph.getNodes().toArray();
 
145
                for (Node n : nodes) {
 
146
                    if (cancel) {
 
147
                        break;
 
148
                    }
 
149
                    writeMatrixNode(n, true);
 
150
                    for (int j = 0; j < nodes.length; j++) {
 
151
                        Node m = nodes[j];
 
152
                        Edge e = undirectedGraph.getEdge(n, m);
 
153
                        e = e == null ? ((HierarchicalUndirectedGraph) undirectedGraph).getMetaEdge(n, m) : e;
 
154
                        writeEdge(e, j < nodes.length - 1);
 
155
                    }
 
156
                    Progress.progress(progressTicket);
 
157
                    writer.append(EOL);
 
158
                }
 
159
            } else {
 
160
                MixedGraph mixedGraph = (MixedGraph) graph;
 
161
                Node[] nodes = graph.getNodes().toArray();
 
162
                for (Node n : graph.getNodes()) {
 
163
                    if (cancel) {
 
164
                        break;
 
165
                    }
 
166
                    writeMatrixNode(n, true);
 
167
                    for (int j = 0; j < nodes.length; j++) {
 
168
                        Node m = nodes[j];
 
169
                        Edge e = mixedGraph.getEdge(n, m);
 
170
                        e = e == null ? ((HierarchicalMixedGraph) mixedGraph).getMetaEdge(n, m) : e;
 
171
                        writeEdge(e, j < nodes.length - 1);
 
172
                    }
 
173
                    Progress.progress(progressTicket);
 
174
                    writer.append(EOL);
 
175
                }
 
176
            }
 
177
        }
 
178
 
 
179
        graph.readUnlockAll();
 
180
 
 
181
        Progress.finish(progressTicket);
 
182
    }
 
183
 
 
184
    private void writeEdge(Edge edge, boolean writeSeparator) throws IOException {
 
185
        if (edge != null) {
 
186
            if (edgeWeight) {
 
187
                writer.append(Float.toString(edge.getWeight()));
 
188
            } else {
 
189
                writer.append(Float.toString(1f));
 
190
            }
 
191
            if (writeSeparator) {
 
192
                writer.append(SEPARATOR);
 
193
            }
 
194
 
 
195
        } else {
 
196
            if (writeZero) {
 
197
                writer.append("0");
 
198
            }
 
199
            if (writeSeparator) {
 
200
                writer.append(SEPARATOR);
 
201
            }
 
202
        }
 
203
    }
 
204
 
 
205
    private void writeMatrixNode(Node node, boolean writeSeparator) throws IOException {
 
206
        if (header) {
 
207
            String label = node.getNodeData().getLabel();
 
208
            if (label == null) {
 
209
                label = node.getNodeData().getId();
 
210
            }
 
211
            writer.append(label);
 
212
            if (writeSeparator) {
 
213
                writer.append(SEPARATOR);
 
214
            }
 
215
        }
 
216
    }
 
217
 
 
218
    private void writeListNode(Node node, boolean writeSeparator) throws IOException {
 
219
        String label = node.getNodeData().getLabel();
 
220
        if (label == null) {
 
221
            label = node.getNodeData().getId();
 
222
        }
 
223
        writer.append(label);
 
224
        if (writeSeparator) {
 
225
            writer.append(SEPARATOR);
 
226
        }
 
227
    }
 
228
 
 
229
    public boolean cancel() {
 
230
        cancel = true;
 
231
        return true;
 
232
    }
 
233
 
 
234
    public void setProgressTicket(ProgressTicket progressTicket) {
 
235
        this.progressTicket = progressTicket;
 
236
    }
 
237
 
 
238
    public boolean isEdgeWeight() {
 
239
        return edgeWeight;
 
240
    }
 
241
 
 
242
    public void setEdgeWeight(boolean edgeWeight) {
 
243
        this.edgeWeight = edgeWeight;
 
244
    }
 
245
 
 
246
    public boolean isHeader() {
 
247
        return header;
 
248
    }
 
249
 
 
250
    public void setHeader(boolean header) {
 
251
        this.header = header;
 
252
    }
 
253
 
 
254
    public boolean isWriteZero() {
 
255
        return writeZero;
 
256
    }
 
257
 
 
258
    public void setWriteZero(boolean writeZero) {
 
259
        this.writeZero = writeZero;
 
260
    }
 
261
 
 
262
    public boolean isList() {
 
263
        return list;
 
264
    }
 
265
 
 
266
    public void setList(boolean list) {
 
267
        this.list = list;
 
268
    }
 
269
 
 
270
    public boolean isExportVisible() {
 
271
        return exportVisible;
 
272
    }
 
273
 
 
274
    public void setExportVisible(boolean exportVisible) {
 
275
        this.exportVisible = exportVisible;
 
276
    }
 
277
 
 
278
    public void setWriter(Writer writer) {
 
279
        this.writer = writer;
 
280
    }
 
281
 
 
282
    public Workspace getWorkspace() {
 
283
        return workspace;
 
284
    }
 
285
 
 
286
    public void setWorkspace(Workspace workspace) {
 
287
        this.workspace = workspace;
 
288
    }
 
289
}