rewrite GraphML importer/exporter

This commit is contained in:
2022-02-26 07:34:07 -06:00
parent b604b1d3cd
commit f032d3e852
3 changed files with 105 additions and 15 deletions

View File

@@ -1,3 +1,4 @@
import org.jgrapht.graph.DefaultWeightedEdge;
import org.jgrapht.graph.SimpleWeightedGraph;
import org.jgrapht.nio.graphml.GraphMLImporter;
@@ -21,7 +22,12 @@ public class GraphMLFileReader {
try(//don't need to close reader bc of try-with-resources auto-closing
BufferedReader reader = Files.newBufferedReader(Path.of(filename));
){
GraphMLImporter<SimpleWeightedGraph, BufferedReader> importer = new GraphMLImporter<>();
GraphMLImporter<Vertex, DefaultWeightedEdge> importer = new GraphMLImporter<>();
importer.addVertexWithAttributesConsumer((vertex, attributes) -> {
vertex.setType(attributes.get("type").getValue());
vertex.setSequence(attributes.get("sequence").getValue());
vertex.setOccupancy((attributes.get("occupancy").getValue()));
});
importer.importGraph(graph, reader);
}
catch (IOException ex) {

View File

@@ -1,12 +1,18 @@
import org.jgrapht.graph.DefaultWeightedEdge;
import org.jgrapht.graph.SimpleWeightedGraph;
import org.jgrapht.nio.dot.DOTExporter;
import org.jgrapht.nio.Attribute;
import org.jgrapht.nio.AttributeType;
import org.jgrapht.nio.DefaultAttribute;
import org.jgrapht.nio.graphml.GraphMLExporter;
import org.jgrapht.nio.graphml.GraphMLExporter.AttributeCategory;
import java.io.BufferedWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.util.HashMap;
import java.util.Map;
public class GraphMLFileWriter {
@@ -14,7 +20,7 @@ public class GraphMLFileWriter {
SimpleWeightedGraph graph;
public GraphMLFileWriter(String filename, SimpleWeightedGraph graph) {
public GraphMLFileWriter(String filename, SimpleWeightedGraph<Vertex, DefaultWeightedEdge> graph) {
if(!filename.matches(".*\\.graphml")){
filename = filename + ".graphml";
}
@@ -25,7 +31,23 @@ public class GraphMLFileWriter {
public void writeGraphToFile() {
try(BufferedWriter writer = Files.newBufferedWriter(Path.of(filename), StandardOpenOption.CREATE_NEW);
){
GraphMLExporter<SimpleWeightedGraph, BufferedWriter> exporter = new GraphMLExporter<>();
//create exporter. Let the vertex labels be the unique ids for the vertices
GraphMLExporter<Vertex, SimpleWeightedGraph<Vertex, DefaultWeightedEdge>> exporter = new GraphMLExporter<>(Vertex::getVertexLabel);
//set to export weights
exporter.setExportEdgeWeights(true);
//set type, sequnce, and occupancy attributes for each vertex
exporter.setVertexAttributeProvider( v -> {
Map<String, Attribute> attributes = new HashMap<>();
attributes.put("type", DefaultAttribute.createAttribute(v.getType()));
attributes.put("sequence", DefaultAttribute.createAttribute(v.getSequence()));
attributes.put("occupancy", DefaultAttribute.createAttribute(v.getOccupancy()));
return attributes;
});
//register the attributes
exporter.registerAttribute("type", AttributeCategory.NODE, AttributeType.STRING);
exporter.registerAttribute("sequence", AttributeCategory.NODE, AttributeType.STRING);
exporter.registerAttribute("occupancy", AttributeCategory.NODE, AttributeType.STRING);
//export the graph
exporter.exportGraph(graph, writer);
} catch(IOException ex){
System.out.println("Could not make new file named "+filename);

View File

@@ -1,10 +1,14 @@
public class Vertex {
private final SequenceType type;
private final Integer vertexLabel;
private final Integer sequence;
private final Integer occupancy;
private SequenceType type;
private Integer vertexLabel;
private Integer sequence;
private Integer occupancy;
public Vertex(String vertexLabel) {
this.vertexLabel = Integer.parseInt((vertexLabel));
}
public Vertex(SequenceType type, Integer sequence, Integer occupancy, Integer vertexLabel) {
this.type = type;
@@ -13,15 +17,73 @@ public class Vertex {
this.occupancy = occupancy;
}
public SequenceType getType() { return type; }
public Integer getVertexLabel() { return vertexLabel; }
public Integer getSequence() {
return sequence;
public String getType() {
return type.name();
}
public Integer getOccupancy() {
return occupancy;
public void setType(String type) {
this.type = SequenceType.valueOf(type);
}
public String getVertexLabel() {
return vertexLabel.toString();
}
public void setVertexLabel(String label) {
this.vertexLabel = Integer.parseInt(label);
}
public String getSequence() {
return sequence.toString();
}
public void setSequence(String sequence) {
this.sequence = Integer.parseInt(sequence);
}
public String getOccupancy() {
return occupancy.toString();
}
public void setOccupancy(String occupancy) {
this.occupancy = Integer.parseInt(occupancy);
}
@Override //adapted from JGraphT example code
public int hashCode()
{
return (sequence == null) ? 0 : sequence.hashCode();
}
@Override //adapted from JGraphT example code
public boolean equals(Object obj)
{
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Vertex other = (Vertex) obj;
if (sequence == null) {
return other.sequence == null;
} else {
return sequence.equals(other.sequence);
}
}
@Override //adapted from JGraphT example code
public String toString()
{
StringBuilder sb = new StringBuilder();
sb.append("(").append(vertexLabel)
.append(", Type: ").append(type.name())
.append(", Sequence: ").append(sequence)
.append(", Occupancy: ").append(occupancy).append(")");
return sb.toString();
}
}