com.salesforce.jgrapht.ext.CSVExporter Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of AptSpringProcessor Show documentation
Show all versions of AptSpringProcessor Show documentation
This project contains the apt processor that implements all the checks enumerated in @Verify. It is a self contained, and
shaded jar.
/*
* (C) Copyright 2016-2017, by Dimitrios Michail and Contributors.
*
* JGraphT : a free Java graph-theory library
*
* This program and the accompanying materials are dual-licensed under
* either
*
* (a) the terms of the GNU Lesser General Public License version 2.1
* as published by the Free Software Foundation, or (at your option) any
* later version.
*
* or (per the licensee's choosing)
*
* (b) the terms of the Eclipse Public License v1.0 as published by
* the Eclipse Foundation.
*/
package com.salesforce.jgrapht.ext;
import java.io.*;
import java.util.*;
import com.salesforce.jgrapht.*;
/**
* Exports a graph into a CSV Format or any other Delimiter-separated value format.
*
*
* The exporter supports three different formats which can be adjusted using the
* {@link #setFormat(CSVFormat) setFormat} method. The supported formats are the same CSV formats
* used by Gephi . For some
* of the formats, the behavior of the exporter can be adjusted using the
* {@link #setParameter(com.salesforce.jgrapht.ext.CSVFormat.Parameter, boolean) setParameter} method. See
* {@link CSVFormat} for a description of the formats.
*
*
*
* The default output respects rfc4180. The caller
* can also adjust the separator to something like semicolon or pipe instead of comma. In such a
* case, all fields are escaped using the new separator. See
* Delimiter- separated
* values for more information.
*
*
* @see CSVFormat
*
* @param the graph vertex type
* @param the graph edge type
*
* @author Dimitrios Michail
* @since August 2016
*/
public class CSVExporter
implements GraphExporter
{
private static final char DEFAULT_DELIMITER = ',';
private final ComponentNameProvider vertexIDProvider;
private final Set parameters;
private CSVFormat format;
private char delimiter;
/**
* Creates a new CSVExporter with {@link CSVFormat#ADJACENCY_LIST} format and integer name
* provider for the vertices.
*/
public CSVExporter()
{
this(new IntegerComponentNameProvider<>(), CSVFormat.ADJACENCY_LIST, DEFAULT_DELIMITER);
}
/**
* Creates a new CSVExporter with integer name providers for the vertices.
*
* @param format the format to use
*/
public CSVExporter(CSVFormat format)
{
this(new IntegerComponentNameProvider<>(), format, DEFAULT_DELIMITER);
}
/**
* Creates a new CSVExporter with integer name providers for the vertices.
*
* @param format the format to use
* @param delimiter delimiter to use
*/
public CSVExporter(CSVFormat format, char delimiter)
{
this(new IntegerComponentNameProvider<>(), format, delimiter);
}
/**
* Constructs a new CSVExporter with the given ID providers and format.
*
* @param vertexIDProvider for generating vertex IDs. Must not be null.
* @param format the format to use
* @param delimiter delimiter to use
*/
public CSVExporter(ComponentNameProvider vertexIDProvider, CSVFormat format, char delimiter)
{
if (vertexIDProvider == null) {
throw new IllegalArgumentException("Vertex id provider cannot be null");
}
this.vertexIDProvider = vertexIDProvider;
this.format = format;
if (!DSVUtils.isValidDelimiter(delimiter)) {
throw new IllegalArgumentException("Character cannot be used as a delimiter");
}
this.delimiter = delimiter;
this.parameters = new HashSet<>();
}
/**
* Exports a graph
*
* @param g the graph
* @param writer the writer
*/
@Override
public void exportGraph(Graph g, Writer writer)
{
PrintWriter out = new PrintWriter(writer);
switch (format) {
case EDGE_LIST:
exportAsEdgeList(g, out);
break;
case ADJACENCY_LIST:
exportAsAdjacencyList(g, out);
break;
case MATRIX:
exportAsMatrix(g, out);
break;
}
out.flush();
}
/**
* Return if a particular parameter of the exporter is enabled
*
* @param p the parameter
* @return {@code true} if the parameter is set, {@code false} otherwise
*/
public boolean isParameter(CSVFormat.Parameter p)
{
return parameters.contains(p);
}
/**
* Set the value of a parameter of the exporter
*
* @param p the parameter
* @param value the value to set
*/
public void setParameter(CSVFormat.Parameter p, boolean value)
{
if (value) {
parameters.add(p);
} else {
parameters.remove(p);
}
}
/**
* Get the format of the exporter
*
* @return the format of the exporter
*/
public CSVFormat getFormat()
{
return format;
}
/**
* Set the format of the exporter
*
* @param format the format to use
*/
public void setFormat(CSVFormat format)
{
this.format = format;
}
/**
* Get the delimiter (comma, semicolon, pipe, etc).
*
* @return the delimiter
*/
public char getDelimiter()
{
return delimiter;
}
/**
* Set the delimiter (comma, semicolon, pipe, etc).
*
* @param delimiter the delimiter to use
*/
public void setDelimiter(char delimiter)
{
if (!DSVUtils.isValidDelimiter(delimiter)) {
throw new IllegalArgumentException("Character cannot be used as a delimiter");
}
this.delimiter = delimiter;
}
private void exportAsEdgeList(Graph g, PrintWriter out)
{
for (E e : g.edgeSet()) {
exportEscapedField(out, vertexIDProvider.getName(g.getEdgeSource(e)));
out.print(delimiter);
exportEscapedField(out, vertexIDProvider.getName(g.getEdgeTarget(e)));
out.println();
}
}
private void exportAsAdjacencyList(Graph g, PrintWriter out)
{
if (g instanceof DirectedGraph, ?>) {
for (V v : g.vertexSet()) {
exportEscapedField(out, vertexIDProvider.getName(v));
for (E e : ((DirectedGraph) g).outgoingEdgesOf(v)) {
V w = Graphs.getOppositeVertex(g, e, v);
out.print(delimiter);
exportEscapedField(out, vertexIDProvider.getName(w));
}
out.println();
}
} else {
for (V v : g.vertexSet()) {
exportEscapedField(out, vertexIDProvider.getName(v));
for (E e : g.edgesOf(v)) {
V w = Graphs.getOppositeVertex(g, e, v);
out.print(delimiter);
exportEscapedField(out, vertexIDProvider.getName(w));
}
out.println();
}
}
}
private void exportAsMatrix(Graph g, PrintWriter out)
{
boolean exportNodeId = parameters.contains(CSVFormat.Parameter.MATRIX_FORMAT_NODEID);
boolean exportEdgeWeights =
parameters.contains(CSVFormat.Parameter.MATRIX_FORMAT_EDGE_WEIGHTS);
boolean zeroWhenNoEdge =
parameters.contains(CSVFormat.Parameter.MATRIX_FORMAT_ZERO_WHEN_NO_EDGE);
if (exportNodeId) {
for (V v : g.vertexSet()) {
out.print(delimiter);
exportEscapedField(out, vertexIDProvider.getName(v));
}
out.println();
}
int n = g.vertexSet().size();
for (V v : g.vertexSet()) {
if (exportNodeId) {
exportEscapedField(out, vertexIDProvider.getName(v));
out.print(delimiter);
}
int i = 0;
for (V u : g.vertexSet()) {
E e = g.getEdge(v, u);
if (e == null) {
if (zeroWhenNoEdge) {
exportEscapedField(out, "0");
}
} else {
if (exportEdgeWeights) {
exportEscapedField(out, String.valueOf(g.getEdgeWeight(e)));
} else {
exportEscapedField(out, "1");
}
}
if (i++ < n - 1) {
out.print(delimiter);
}
}
out.println();
}
}
private void exportEscapedField(PrintWriter out, String field)
{
out.print(DSVUtils.escapeDSV(field, delimiter));
}
}
// End CSVExporter.java
© 2015 - 2025 Weber Informatics LLC | Privacy Policy