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

see #199: add support for attr.name and attr.type #200

Open
wants to merge 4 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
18 changes: 17 additions & 1 deletion jung-io/src/main/java/edu/uci/ics/jung/io/GraphMLMetadata.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
*/
package edu.uci.ics.jung.io;

import edu.uci.ics.jung.io.graphml.AttributeType;
import java.util.function.Function;

/**
Expand All @@ -24,16 +25,31 @@ public class GraphMLMetadata<T> {
/** A Function mapping objects to string representations of their values. */
public Function<T, String> transformer;

public String attributeName;

public AttributeType attributeType;

/**
* Creates a new instance with the specified description, default value, and function.
*
* @param description a textual description of the object
* @param default_value the default value for the object, as a String
* @param function maps objects of this type to string representations
*/
public GraphMLMetadata(String description, String default_value, Function<T, String> function) {
public GraphMLMetadata(
String description,
String default_value,
Function<T, String> function,
String attributeName,
AttributeType attributeType) {
this.description = description;
this.transformer = function;
this.default_value = default_value;
this.attributeName = attributeName;
this.attributeType = attributeType;
}

public GraphMLMetadata(String description, String default_value, Function<T, String> function) {
this(description, default_value, function, null, null);
}
}
44 changes: 40 additions & 4 deletions jung-io/src/main/java/edu/uci/ics/jung/io/GraphMLWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

import com.google.common.graph.EndpointPair;
import com.google.common.graph.Network;
import edu.uci.ics.jung.io.graphml.AttributeType;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.Writer;
Expand Down Expand Up @@ -211,6 +212,15 @@ protected void writeEdgeData(Network<N, E> g, Writer w) throws IOException {
protected void writeKeySpecification(
String key, String type, GraphMLMetadata<?> ds, BufferedWriter bw) throws IOException {
bw.write("<key id=\"" + key + "\" for=\"" + type + "\"");

if (null != ds.attributeName) {
bw.write(" attr.name=\"" + ds.attributeName.replace("\"", "&quot;") + "\"");
}

if (null != ds.attributeType) {
bw.write(" attr.type=\"" + ds.attributeType.getValue() + "\"");
}

boolean closed = false;
// write out description if any
String desc = ds.description;
Expand Down Expand Up @@ -318,11 +328,24 @@ public void addGraphData(
* @param node_transformer a mapping from nodes to their string representations
*/
public void addNodeData(
String id, String description, String default_value, Function<N, String> node_transformer) {
String id,
String description,
String default_value,
Function<N, String> node_transformer,
String attributeName,
AttributeType attributeType) {
if (node_data.equals(Collections.EMPTY_MAP)) {
node_data = new HashMap<String, GraphMLMetadata<N>>();
}
node_data.put(id, new GraphMLMetadata<N>(description, default_value, node_transformer));
node_data.put(
id,
new GraphMLMetadata<N>(
description, default_value, node_transformer, attributeName, attributeType));
}

public void addNodeData(
String id, String description, String default_value, Function<N, String> node_transformer) {
addNodeData(id, description, default_value, node_transformer, null, null);
}

/**
Expand All @@ -334,11 +357,24 @@ public void addNodeData(
* @param edge_transformer a mapping from edges to their string representations
*/
public void addEdgeData(
String id, String description, String default_value, Function<E, String> edge_transformer) {
String id,
String description,
String default_value,
Function<E, String> edge_transformer,
String attributeName,
AttributeType attributeType) {
if (edge_data.equals(Collections.EMPTY_MAP)) {
edge_data = new HashMap<String, GraphMLMetadata<E>>();
}
edge_data.put(id, new GraphMLMetadata<E>(description, default_value, edge_transformer));
edge_data.put(
id,
new GraphMLMetadata<E>(
description, default_value, edge_transformer, attributeName, attributeType));
}

public void addEdgeData(
String id, String description, String default_value, Function<E, String> edge_transformer) {
addEdgeData(id, description, default_value, edge_transformer, null, null);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package edu.uci.ics.jung.io.graphml;

public enum AttributeType {
BOOLEAN("boolean"),
INT("int"),
LONG("long"),
FLOAT("float"),
DOUBLE("double"),
STRING("string");

private final String value;

AttributeType(String value) {
this.value = value;
}

public String getValue() {
return value;
}
}