Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Graphml export #198

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@
import nl.knaw.huygens.timbuctoo.v5.dataset.DataSetRepository;
import nl.knaw.huygens.timbuctoo.v5.dropwizard.DataSetFactoryManager;
import nl.knaw.huygens.timbuctoo.v5.dropwizard.contenttypes.CsvWriter;
import nl.knaw.huygens.timbuctoo.v5.dropwizard.contenttypes.GraphMlWriter;
import nl.knaw.huygens.timbuctoo.v5.dropwizard.contenttypes.GraphVizWriter;
import nl.knaw.huygens.timbuctoo.v5.dropwizard.contenttypes.JsonLdWriter;
import nl.knaw.huygens.timbuctoo.v5.dropwizard.contenttypes.JsonWriter;
Expand Down Expand Up @@ -282,7 +283,8 @@ public void run(TimbuctooConfiguration configuration, Environment environment) t
new CsvWriter(),
new JsonLdWriter(),
new JsonWriter(),
new GraphVizWriter()
new GraphVizWriter(),
new GraphMlWriter()
);

final PaginationArgumentsHelper argHelper = new PaginationArgumentsHelper(configuration.getCollectionFilters());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package nl.knaw.huygens.timbuctoo.v5.dropwizard.contenttypes;

import nl.knaw.huygens.timbuctoo.v5.serializable.serializations.GraphMlSerialization;
import javax.ws.rs.Produces;

@Produces(GraphMlWriter.MIME_TYPE)
public class GraphMlWriter extends SerializerWriter {

public static final String MIME_TYPE = "text/graphml";

public GraphMlWriter() {
super(GraphMlSerialization::new);
}

@Override
public String getMimeType() {
return MIME_TYPE;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package nl.knaw.huygens.timbuctoo.v5.serializable.serializations;

import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger;

import nl.knaw.huygens.timbuctoo.v5.serializable.SerializableResult;
import nl.knaw.huygens.timbuctoo.v5.serializable.serializations.base.CollectionsOfEntitiesSerialization;

public class GraphMlSerialization extends CollectionsOfEntitiesSerialization {

private final PrintWriter writer;
private int countEdge = 1;

public GraphMlSerialization(OutputStream outputStream) throws IOException {
writer = new PrintWriter(outputStream);
}

public void serialize(SerializableResult serializableResult) throws IOException {
super.serialize(serializableResult);
Map<String, Integer> entityIds = new HashMap<>();
AtomicInteger nodeCounter = new AtomicInteger();
writeHeader();
for (Map<String, AggregatedEntity> entities : allEntities.values()) {
for (Map.Entry<String, AggregatedEntity> entity : entities.entrySet()) {
Integer nodeId = entityIds.computeIfAbsent(entity.getKey(), k -> nodeCounter.incrementAndGet());
writer.println(" <node id=\"node" + nodeId + "\">");
writer.println(" <data key=\"" + entity.getKey() + "\">" + entity.getValue() + "</data>");
writer.println(" <data key=\"label\">" + entity.getKey() + "</data>");
writer.println(" </node>");
for (Map.Entry<String, Set<String>> relation : entity.getValue().relations.entrySet()) {
for (String otherNodeUri : relation.getValue()) {
Integer otherNodeId = entityIds.computeIfAbsent(otherNodeUri, k -> nodeCounter.incrementAndGet());
writer.println(" <edge id=\"e" + countEdge + "\" source=\"node" +
nodeId + "\" target=\"node" + otherNodeId + "\">");
writer.println(" <data key=\"label\">" + relation.getKey() + "</data>");
writer.println(" </edge>");
countEdge++;
}
}
}
}
writeFooter();
writer.flush();
writer.close();
}


protected void writeHeader() {
String header = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
"<graphml xmlns=\"http://graphml.graphdrawing.org/xmlns\"\n" +
" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n" +
" xsi:schemaLocation=\"http://graphml.graphdrawing.org/xmlns\n" +
" http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd\">";
writer.println(header);
writer.println(" <graph id=\"G\" edgedefault=\"directed\">");
}

protected void writeFooter() {
writer.println(" </graph>");
String footer = "</graphml>";
writer.println(footer);
}

}