2
Copyright 2008-2010 Gephi
3
Authors : Mathieu Bastian <mathieu.bastian@gephi.org>
4
Website : http://www.gephi.org
6
This file is part of Gephi.
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.
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.
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/>.
21
package org.gephi.io.exporter.plugin;
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;
47
* @author Mathieu Bastian
49
public class ExporterCSV implements GraphExporter, CharacterExporter, LongTask {
51
private static final String SEPARATOR = ";";
52
private static final String EOL = "\n";
54
private boolean edgeWeight = true;
55
private boolean writeZero = true;
56
private boolean header = true;
57
private boolean list = false;
59
private Workspace workspace;
60
private Writer writer;
61
private boolean exportVisible;
62
private boolean cancel = false;
63
private ProgressTicket progressTicket;
65
public boolean execute() {
66
GraphModel graphModel = workspace.getLookup().lookup(GraphModel.class);
69
graph = graphModel.getGraphVisible();
71
graph = graphModel.getGraph();
75
} catch (Exception e) {
76
throw new RuntimeException(e);
82
private void exportData(Graph graph) throws Exception {
83
int max = graph.getNodeCount();
85
Progress.start(progressTicket, max);
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);
99
Node[] nodes = graph.getNodes().toArray();
100
for (int i = 0; i < nodes.length; 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);
110
for (Edge e : ((HierarchicalGraph) graph).getMetaEdges(n)) {
111
if (!e.isDirected() || (e.isDirected() && n == e.getSource())) {
112
Node m = graph.getOpposite(n, e);
117
writeListNode(n, !neighbours.isEmpty());
118
for (int j = 0; j < neighbours.size(); j++) {
119
writeListNode(neighbours.get(j), j < neighbours.size() - 1);
125
if (graph instanceof DirectedGraph) {
126
DirectedGraph directedGraph = (DirectedGraph) graph;
127
Node[] nodes = graph.getNodes().toArray();
128
for (Node n : nodes) {
132
writeMatrixNode(n, true);
133
for (int j = 0; j < nodes.length; 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);
139
Progress.progress(progressTicket);
142
} else if (graph instanceof UndirectedGraph) {
143
UndirectedGraph undirectedGraph = (UndirectedGraph) graph;
144
Node[] nodes = graph.getNodes().toArray();
145
for (Node n : nodes) {
149
writeMatrixNode(n, true);
150
for (int j = 0; j < nodes.length; 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);
156
Progress.progress(progressTicket);
160
MixedGraph mixedGraph = (MixedGraph) graph;
161
Node[] nodes = graph.getNodes().toArray();
162
for (Node n : graph.getNodes()) {
166
writeMatrixNode(n, true);
167
for (int j = 0; j < nodes.length; 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);
173
Progress.progress(progressTicket);
179
graph.readUnlockAll();
181
Progress.finish(progressTicket);
184
private void writeEdge(Edge edge, boolean writeSeparator) throws IOException {
187
writer.append(Float.toString(edge.getWeight()));
189
writer.append(Float.toString(1f));
191
if (writeSeparator) {
192
writer.append(SEPARATOR);
199
if (writeSeparator) {
200
writer.append(SEPARATOR);
205
private void writeMatrixNode(Node node, boolean writeSeparator) throws IOException {
207
String label = node.getNodeData().getLabel();
209
label = node.getNodeData().getId();
211
writer.append(label);
212
if (writeSeparator) {
213
writer.append(SEPARATOR);
218
private void writeListNode(Node node, boolean writeSeparator) throws IOException {
219
String label = node.getNodeData().getLabel();
221
label = node.getNodeData().getId();
223
writer.append(label);
224
if (writeSeparator) {
225
writer.append(SEPARATOR);
229
public boolean cancel() {
234
public void setProgressTicket(ProgressTicket progressTicket) {
235
this.progressTicket = progressTicket;
238
public boolean isEdgeWeight() {
242
public void setEdgeWeight(boolean edgeWeight) {
243
this.edgeWeight = edgeWeight;
246
public boolean isHeader() {
250
public void setHeader(boolean header) {
251
this.header = header;
254
public boolean isWriteZero() {
258
public void setWriteZero(boolean writeZero) {
259
this.writeZero = writeZero;
262
public boolean isList() {
266
public void setList(boolean list) {
270
public boolean isExportVisible() {
271
return exportVisible;
274
public void setExportVisible(boolean exportVisible) {
275
this.exportVisible = exportVisible;
278
public void setWriter(Writer writer) {
279
this.writer = writer;
282
public Workspace getWorkspace() {
286
public void setWorkspace(Workspace workspace) {
287
this.workspace = workspace;