Permalink
Browse files

added WrappingGraph<T> to support impl and oupl graphs.

  • Loading branch information...
1 parent b1360de commit 234c311e2388f7b5e98fb91e9cd519d4edc466e6 @okram okram committed Mar 10, 2012
Showing with 2,113 additions and 2,178 deletions.
  1. +0 −11 blueprints-core/src/main/java/com/tinkerpop/blueprints/pgm/oupls/GraphSource.java
  2. +6 −7 ...prints-core/src/main/java/com/tinkerpop/blueprints/pgm/util/LexicographicalElementComparator.java
  3. +363 −378 blueprints-core/src/main/java/com/tinkerpop/blueprints/pgm/util/io/gml/GMLReader.java
  4. +22 −23 blueprints-core/src/main/java/com/tinkerpop/blueprints/pgm/util/io/gml/GMLTokens.java
  5. +245 −257 blueprints-core/src/main/java/com/tinkerpop/blueprints/pgm/util/io/gml/GMLWriter.java
  6. +249 −264 blueprints-core/src/main/java/com/tinkerpop/blueprints/pgm/util/io/graphml/GraphMLWriter.java
  7. +0 −17 blueprints-core/src/main/java/com/tinkerpop/blueprints/pgm/util/wrappers/GraphWrapper.java
  8. +15 −0 blueprints-core/src/main/java/com/tinkerpop/blueprints/pgm/util/wrappers/WrappingGraph.java
  9. +5 −5 blueprints-core/src/main/java/com/tinkerpop/blueprints/pgm/util/wrappers/event/EventGraph.java
  10. +6 −8 ...ints-core/src/main/java/com/tinkerpop/blueprints/pgm/util/wrappers/event/EventIndexableGraph.java
  11. +6 −6 ...rints-core/src/main/java/com/tinkerpop/blueprints/pgm/util/wrappers/partition/PartitionGraph.java
  12. +9 −8 ...e/src/main/java/com/tinkerpop/blueprints/pgm/util/wrappers/partition/PartitionIndexableGraph.java
  13. +5 −5 blueprints-core/src/main/java/com/tinkerpop/blueprints/pgm/util/wrappers/readonly/ReadOnlyGraph.java
  14. +5 −8 ...ore/src/main/java/com/tinkerpop/blueprints/pgm/util/wrappers/readonly/ReadOnlyIndexableGraph.java
  15. +8 −5 blueprints-core/src/main/java/com/tinkerpop/blueprints/pgm/util/wrappers/wrapped/WrappedGraph.java
  16. +8 −7 ...-core/src/main/java/com/tinkerpop/blueprints/pgm/util/wrappers/wrapped/WrappedIndexableGraph.java
  17. +3 −2 blueprints-dex-graph/src/main/java/com/tinkerpop/blueprints/pgm/impls/dex/DexGraph.java
  18. +5 −5 blueprints-graph-jung/src/main/java/com/tinkerpop/blueprints/pgm/oupls/jung/GraphJung.java
  19. +2 −1 blueprints-graph-jung/src/test/java/com/tinkerpop/blueprints/pgm/oupls/jung/GraphJungTest.java
  20. +7 −21 blueprints-graph-sail/src/main/java/com/tinkerpop/blueprints/pgm/oupls/sail/GraphSail.java
  21. +1 −1 blueprints-graph-sail/src/main/java/com/tinkerpop/blueprints/pgm/oupls/sail/GraphSailConnection.java
  22. +1 −1 blueprints-graph-sail/src/test/java/com/tinkerpop/blueprints/pgm/oupls/sail/GraphSailTest.java
  23. +2 −1 blueprints-neo4j-graph/src/main/java/com/tinkerpop/blueprints/pgm/impls/neo4j/Neo4jGraph.java
  24. +2 −1 ...neo4jbatch-graph/src/main/java/com/tinkerpop/blueprints/pgm/impls/neo4jbatch/Neo4jBatchGraph.java
  25. +2 −1 blueprints-orient-graph/src/main/java/com/tinkerpop/blueprints/pgm/impls/orientdb/OrientGraph.java
  26. +2 −1 blueprints-rexster-graph/src/main/java/com/tinkerpop/blueprints/pgm/impls/rexster/RexsterGraph.java
  27. +2 −1 blueprints-sail-graph/src/main/java/com/tinkerpop/blueprints/pgm/impls/sail/SailGraph.java
  28. +837 −837 blueprints-test/src/main/java/com/tinkerpop/blueprints/pgm/util/io/gml/GMLReaderTestSuite.java
  29. +118 −118 blueprints-test/src/test/java/com/tinkerpop/blueprints/pgm/impls/tg/TinkerGraphTest.java
  30. +82 −83 blueprints-test/src/test/java/com/tinkerpop/blueprints/pgm/util/io/gml/GMLReaderTest.java
  31. +89 −90 blueprints-test/src/test/java/com/tinkerpop/blueprints/pgm/util/io/gml/GMLWriterTest.java
  32. +2 −2 blueprints-test/src/test/java/com/tinkerpop/blueprints/pgm/util/wrappers/event/EventGraphTest.java
  33. +3 −2 ...nts-test/src/test/java/com/tinkerpop/blueprints/pgm/util/wrappers/readonly/ReadOnlyGraphTest.java
  34. +1 −1 ...rints-test/src/test/java/com/tinkerpop/blueprints/pgm/util/wrappers/wrapped/WrappedGraphTest.java
View
11 blueprints-core/src/main/java/com/tinkerpop/blueprints/pgm/oupls/GraphSource.java
@@ -1,11 +0,0 @@
-package com.tinkerpop.blueprints.pgm.oupls;
-
-import com.tinkerpop.blueprints.pgm.Graph;
-
-/**
- * @author Marko A. Rodriguez (http://markorodriguez.com)
- */
-public interface GraphSource {
-
- public Graph getGraph();
-}
View
13 ...ore/src/main/java/com/tinkerpop/blueprints/pgm/util/LexicographicalElementComparator.java
@@ -1,17 +1,16 @@
package com.tinkerpop.blueprints.pgm.util;
-import java.util.Comparator;
-
import com.tinkerpop.blueprints.pgm.Element;
+import java.util.Comparator;
+
/**
* Elements are sorted in lexicographical order of IDs.
- *
*/
public class LexicographicalElementComparator implements Comparator<Element> {
- @Override
- public int compare(final Element a, final Element b) {
- return a.getId().toString().compareTo(b.getId().toString());
- }
+ @Override
+ public int compare(final Element a, final Element b) {
+ return a.getId().toString().compareTo(b.getId().toString());
+ }
}
View
741 blueprints-core/src/main/java/com/tinkerpop/blueprints/pgm/util/io/gml/GMLReader.java
@@ -1,5 +1,11 @@
package com.tinkerpop.blueprints.pgm.util.io.gml;
+import com.tinkerpop.blueprints.pgm.Edge;
+import com.tinkerpop.blueprints.pgm.Element;
+import com.tinkerpop.blueprints.pgm.Graph;
+import com.tinkerpop.blueprints.pgm.TransactionalGraph;
+import com.tinkerpop.blueprints.pgm.Vertex;
+
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
@@ -11,393 +17,372 @@
import java.util.Map;
import java.util.Map.Entry;
-import com.tinkerpop.blueprints.pgm.Edge;
-import com.tinkerpop.blueprints.pgm.Element;
-import com.tinkerpop.blueprints.pgm.Graph;
-import com.tinkerpop.blueprints.pgm.TransactionalGraph;
-import com.tinkerpop.blueprints.pgm.Vertex;
-
/**
* A reader for the Graph Modelling Language (GML).
- *
+ * <p/>
* GML definition taken from
* (http://www.fim.uni-passau.de/fileadmin/files/lehrstuhl/brandenburg/projekte/gml/gml-documentation.tar.gz)
- *
+ * <p/>
* It's not clear that all node have to have id's or that they have to be integers - we assume that this is the case. We
* also assume that only one graph can be defined in a file.
- *
- *
+ *
* @author Stuart Hendren (http://stuarthendren.net)
- *
*/
public class GMLReader {
- public static final String DEFAULT_LABEL = "undefined";
-
- private static final int DEFAULT_BUFFER_SIZE = 1000;
-
- private Map<Object, Object> vertexIds = new HashMap<Object, Object>();
-
- private final Graph graph;
-
- private final String defaultEdgeLabel;
-
- private boolean directed = false;
-
- private int edgeCount = 0;
-
- private String vertexIdKey;
-
- private String edgeIdKey;
-
- private String edgeLabelKey = GMLTokens.LABEL;
-
- /**
- * Create a new GML reader
- *
- * (Uses default edge label DEFAULT_LABEL)
- *
- * @param graph
- * the graph to load data into
- */
- public GMLReader(Graph graph) {
- this(graph, DEFAULT_LABEL);
- }
-
- /**
- * Create a new GML reader
- *
- * @param graph
- * the graph to load data into
- * @param defaultEdgeLabel
- * the default edge label to be used if the GML edge does not define a label
- */
- public GMLReader(Graph graph, String defaultEdgeLabel) {
- this.graph = graph;
- this.defaultEdgeLabel = defaultEdgeLabel;
- }
-
- /**
- * @param vertexIdKey
- * gml property to use as id for verticies
- */
- public void setVertexIdKey(String vertexIdKey) {
- this.vertexIdKey = vertexIdKey;
- }
-
- /**
- * @param edgeIdKey
- * gml property to use as id for edges
- */
- public void setEdgeIdKey(String edgeIdKey) {
- this.edgeIdKey = edgeIdKey;
- }
-
- /**
- * @param edgeLabelKey
- * gml property to assign edge Labels to
- */
- public void setEdgeLabelKey(String edgeLabelKey) {
- this.edgeLabelKey = edgeLabelKey;
- }
-
- /**
- * Read the GML from from the stream.
- *
- * If the file is malformed incomplete data can be loaded.
- *
- * @param inputStream
- * @throws IOException
- */
- public void inputGraph(InputStream inputStream) throws IOException {
- inputGraph(inputStream, DEFAULT_BUFFER_SIZE);
- }
-
- /**
- * Read the GML from from the stream.
- *
- * If the file is malformed incomplete data can be loaded.
- *
- * @param inputStream
- * @throws IOException
- */
- public void inputGraph(InputStream inputStream, int bufferSize) throws IOException {
-
- int previousMaxBufferSize = 0;
- if (graph instanceof TransactionalGraph) {
- previousMaxBufferSize = ((TransactionalGraph) graph).getMaxBufferSize();
- ((TransactionalGraph) graph).setMaxBufferSize(bufferSize);
- }
-
- Reader r = new BufferedReader(new InputStreamReader(inputStream, Charset.forName("ISO-8859-1")));
- StreamTokenizer st = new StreamTokenizer(r);
-
- try {
- st.commentChar(GMLTokens.COMMENT_CHAR);
- st.ordinaryChar('[');
- st.ordinaryChar(']');
-
- String stringCharacters = "/\\(){}<>!£$%^&*-+=,.?:;@_`|~";
- for (int i = 0; i < stringCharacters.length(); i++) {
- st.wordChars(stringCharacters.charAt(i), stringCharacters.charAt(i));
- }
-
- parse(st);
-
- if (graph instanceof TransactionalGraph) {
- ((TransactionalGraph) graph).setMaxBufferSize(previousMaxBufferSize);
- }
- } catch (IOException e) {
- throw new IOException(error(st), e);
- } finally {
- vertexIds.clear();
- }
-
- }
-
- private boolean hasNext(StreamTokenizer st) throws IOException {
- return st.nextToken() != StreamTokenizer.TT_EOF;
- }
-
- private String error(StreamTokenizer st) {
- return "GML malformed line number " + st.lineno() + ": ";
- }
-
- private boolean notLineBreak(int type) {
- return type != StreamTokenizer.TT_EOL;
- }
-
- private void parse(StreamTokenizer st) throws IOException {
- while (hasNext(st)) {
- int type = st.ttype;
- if (notLineBreak(type)) {
- String value = st.sval;
- if (GMLTokens.GRAPH.equals(value)) {
- parseGraph(st);
- if (!hasNext(st)) {
- return;
- }
- }
- }
- }
- throw new IOException("Graph not complete");
- }
-
- private void parseGraph(StreamTokenizer st) throws IOException {
- checkValid(st, GMLTokens.GRAPH);
- while (hasNext(st)) {
- // st.nextToken();
- int type = st.ttype;
- if (notLineBreak(type)) {
- if (type == ']') {
- return;
- } else {
- String key = st.sval;
- if (GMLTokens.NODE.equals(key)) {
- addNode(parseNode(st));
- } else if (GMLTokens.EDGE.equals(key)) {
- addEdge(parseEdge(st));
- } else if (GMLTokens.DIRECTED.equals(key)) {
- directed = parseBoolean(st);
- } else {
- // IGNORE
- parseValue("ignore", st);
- }
- }
- }
- }
- throw new IOException("Graph not complete");
- }
-
- private void addNode(Map<String, Object> map) throws IOException {
- Object id = map.remove(GMLTokens.ID);
- if (id != null) {
- Vertex vertex;
- vertex = createVertex(map, id);
- addProperties(vertex, map);
- } else {
- throw new IOException("No id found for node");
- }
- }
-
- private Vertex createVertex(Map<String, Object> map, Object id) {
- if (vertexIdKey != null) {
- Object vertexId = map.remove(vertexIdKey);
- if (vertexId != null) {
- vertexIds.put(id, vertexId);
- return graph.addVertex(vertexId);
- }
- // if no id try to use default id this will fail if not unique
- vertexIds.put(id, id);
- }
- // Use default id system
- return graph.addVertex(id);
- }
-
- private void addEdge(Map<String, Object> map) throws IOException {
- Object source = map.remove(GMLTokens.SOURCE);
- Object target = map.remove(GMLTokens.TARGET);
- if (source == null) {
- throw new IOException("Edge has no source");
- }
- if (target == null) {
- throw new IOException("Edge has no target");
- }
- if (vertexIdKey != null) {
- source = vertexIds.get(source);
- target = vertexIds.get(target);
- }
-
- Vertex outVertex = graph.getVertex(source);
- Vertex inVertex = graph.getVertex(target);
- if (outVertex == null) {
- throw new IOException("Edge source " + source + " not found");
- }
- if (inVertex == null) {
- throw new IOException("Edge target " + target + " not found");
-
- }
-
- Object label = map.remove(edgeLabelKey);
- if (label == null) {
- // try standard label key
- label = map.remove(GMLTokens.LABEL);
- } else {
- // remove label in case edge label key is not label
- // label is reserved and cannot be added as a property
- // if so this data will be lost
- map.remove(GMLTokens.LABEL);
- }
-
- if (label == null) {
- label = defaultEdgeLabel;
- }
- Object edgeId = edgeCount++;
- if (edgeIdKey != null) {
- Object mappedKey = map.remove(edgeIdKey);
- if (mappedKey != null) {
- edgeId = mappedKey;
- }
- // else use edgecount - could fail if mapped ids overlap with edge count
- }
-
- // remove id as reserved property - can be left is edgeIdKey in not id
- // This data will be lost
- map.remove(GMLTokens.ID);
-
- Edge edge = graph.addEdge(edgeId, outVertex, inVertex, label.toString());
- if (directed) {
- edge.setProperty(GMLTokens.DIRECTED, directed);
- }
- addProperties(edge, map);
-
- }
-
- private void addProperties(Element element, Map<String, Object> map) {
- for (Entry<String, Object> entry : map.entrySet()) {
- element.setProperty(entry.getKey(), entry.getValue());
- }
- }
-
- private Object parseValue(String key, StreamTokenizer st) throws IOException {
- while (hasNext(st)) {
- int type = st.ttype;
- if (notLineBreak(type)) {
- if (type == StreamTokenizer.TT_NUMBER) {
- Double doubleValue = Double.valueOf(st.nval);
- if (doubleValue.equals(Double.valueOf(doubleValue.intValue()))) {
- return doubleValue.intValue();
- } else {
- return doubleValue.floatValue();
- }
- } else {
- if (type == '[') {
- return parseMap(key, st);
- } else if (type == '"') {
- return st.sval;
- }
- }
- }
- }
- throw new IOException("value not found");
- }
-
- private boolean parseBoolean(StreamTokenizer st) throws IOException {
- while (hasNext(st)) {
- int type = st.ttype;
- if (notLineBreak(type)) {
- if (type == StreamTokenizer.TT_NUMBER) {
- return st.nval == 1.0;
- }
- }
- }
- throw new IOException("boolean not found");
- }
-
- private Map<String, Object> parseNode(StreamTokenizer st) throws IOException {
- return parseElement(st, GMLTokens.NODE);
- }
-
- private Map<String, Object> parseEdge(StreamTokenizer st) throws IOException {
- return parseElement(st, GMLTokens.EDGE);
- }
-
- private Map<String, Object> parseElement(StreamTokenizer st, String node) throws IOException {
- checkValid(st, node);
- return parseMap(node, st);
- }
-
- private Map<String, Object> parseMap(String node, StreamTokenizer st) throws IOException {
- Map<String, Object> map = new HashMap<String, Object>();
- while (hasNext(st)) {
- int type = st.ttype;
- if (notLineBreak(type)) {
- if (type == ']') {
- return map;
- } else {
- String key = st.sval;
- Object value = parseValue(key, st);
- map.put(key, value);
- }
- }
- }
- throw new IOException(node + " incomplete");
- }
-
- private void checkValid(StreamTokenizer st, String token) throws IOException {
- if (st.nextToken() != '[') {
- throw new IOException(token + " not followed by [");
- }
- }
-
- /**
- * Load the GML file into the Graph.
- *
- * @param graph
- * to receive the data
- * @param inputStream
- * GML file
- * @throws IOException
- * thrown if the data is not valid
- */
- public static void inputGraph(Graph graph, InputStream inputStream) throws IOException {
- inputGraph(graph, inputStream, DEFAULT_LABEL);
- }
-
- /**
- * Load the GML file into the Graph.
- *
- * @param graph
- * to receive the data
- * @param inputStream
- * GML file
- * @param defaultEdgeLabel
- * default edge label to be used if not defined in the data
- * @throws IOException
- * thrown if the data is not valid
- */
- public static void inputGraph(Graph graph, InputStream inputStream, String defaultEdgeLabel) throws IOException {
- new GMLReader(graph, defaultEdgeLabel).inputGraph(inputStream, DEFAULT_BUFFER_SIZE);
- }
+ public static final String DEFAULT_LABEL = "undefined";
+
+ private static final int DEFAULT_BUFFER_SIZE = 1000;
+
+ private Map<Object, Object> vertexIds = new HashMap<Object, Object>();
+
+ private final Graph graph;
+
+ private final String defaultEdgeLabel;
+
+ private boolean directed = false;
+
+ private int edgeCount = 0;
+
+ private String vertexIdKey;
+
+ private String edgeIdKey;
+
+ private String edgeLabelKey = GMLTokens.LABEL;
+
+ /**
+ * Create a new GML reader
+ * <p/>
+ * (Uses default edge label DEFAULT_LABEL)
+ *
+ * @param graph the graph to load data into
+ */
+ public GMLReader(Graph graph) {
+ this(graph, DEFAULT_LABEL);
+ }
+
+ /**
+ * Create a new GML reader
+ *
+ * @param graph the graph to load data into
+ * @param defaultEdgeLabel the default edge label to be used if the GML edge does not define a label
+ */
+ public GMLReader(Graph graph, String defaultEdgeLabel) {
+ this.graph = graph;
+ this.defaultEdgeLabel = defaultEdgeLabel;
+ }
+
+ /**
+ * @param vertexIdKey gml property to use as id for verticies
+ */
+ public void setVertexIdKey(String vertexIdKey) {
+ this.vertexIdKey = vertexIdKey;
+ }
+
+ /**
+ * @param edgeIdKey gml property to use as id for edges
+ */
+ public void setEdgeIdKey(String edgeIdKey) {
+ this.edgeIdKey = edgeIdKey;
+ }
+
+ /**
+ * @param edgeLabelKey gml property to assign edge Labels to
+ */
+ public void setEdgeLabelKey(String edgeLabelKey) {
+ this.edgeLabelKey = edgeLabelKey;
+ }
+
+ /**
+ * Read the GML from from the stream.
+ * <p/>
+ * If the file is malformed incomplete data can be loaded.
+ *
+ * @param inputStream
+ * @throws IOException
+ */
+ public void inputGraph(InputStream inputStream) throws IOException {
+ inputGraph(inputStream, DEFAULT_BUFFER_SIZE);
+ }
+
+ /**
+ * Read the GML from from the stream.
+ * <p/>
+ * If the file is malformed incomplete data can be loaded.
+ *
+ * @param inputStream
+ * @throws IOException
+ */
+ public void inputGraph(InputStream inputStream, int bufferSize) throws IOException {
+
+ int previousMaxBufferSize = 0;
+ if (graph instanceof TransactionalGraph) {
+ previousMaxBufferSize = ((TransactionalGraph) graph).getMaxBufferSize();
+ ((TransactionalGraph) graph).setMaxBufferSize(bufferSize);
+ }
+
+ Reader r = new BufferedReader(new InputStreamReader(inputStream, Charset.forName("ISO-8859-1")));
+ StreamTokenizer st = new StreamTokenizer(r);
+
+ try {
+ st.commentChar(GMLTokens.COMMENT_CHAR);
+ st.ordinaryChar('[');
+ st.ordinaryChar(']');
+
+ String stringCharacters = "/\\(){}<>!£$%^&*-+=,.?:;@_`|~";
+ for (int i = 0; i < stringCharacters.length(); i++) {
+ st.wordChars(stringCharacters.charAt(i), stringCharacters.charAt(i));
+ }
+
+ parse(st);
+
+ if (graph instanceof TransactionalGraph) {
+ ((TransactionalGraph) graph).setMaxBufferSize(previousMaxBufferSize);
+ }
+ } catch (IOException e) {
+ throw new IOException(error(st), e);
+ } finally {
+ vertexIds.clear();
+ }
+
+ }
+
+ private boolean hasNext(StreamTokenizer st) throws IOException {
+ return st.nextToken() != StreamTokenizer.TT_EOF;
+ }
+
+ private String error(StreamTokenizer st) {
+ return "GML malformed line number " + st.lineno() + ": ";
+ }
+
+ private boolean notLineBreak(int type) {
+ return type != StreamTokenizer.TT_EOL;
+ }
+
+ private void parse(StreamTokenizer st) throws IOException {
+ while (hasNext(st)) {
+ int type = st.ttype;
+ if (notLineBreak(type)) {
+ String value = st.sval;
+ if (GMLTokens.GRAPH.equals(value)) {
+ parseGraph(st);
+ if (!hasNext(st)) {
+ return;
+ }
+ }
+ }
+ }
+ throw new IOException("Graph not complete");
+ }
+
+ private void parseGraph(StreamTokenizer st) throws IOException {
+ checkValid(st, GMLTokens.GRAPH);
+ while (hasNext(st)) {
+ // st.nextToken();
+ int type = st.ttype;
+ if (notLineBreak(type)) {
+ if (type == ']') {
+ return;
+ } else {
+ String key = st.sval;
+ if (GMLTokens.NODE.equals(key)) {
+ addNode(parseNode(st));
+ } else if (GMLTokens.EDGE.equals(key)) {
+ addEdge(parseEdge(st));
+ } else if (GMLTokens.DIRECTED.equals(key)) {
+ directed = parseBoolean(st);
+ } else {
+ // IGNORE
+ parseValue("ignore", st);
+ }
+ }
+ }
+ }
+ throw new IOException("Graph not complete");
+ }
+
+ private void addNode(Map<String, Object> map) throws IOException {
+ Object id = map.remove(GMLTokens.ID);
+ if (id != null) {
+ Vertex vertex;
+ vertex = createVertex(map, id);
+ addProperties(vertex, map);
+ } else {
+ throw new IOException("No id found for node");
+ }
+ }
+
+ private Vertex createVertex(Map<String, Object> map, Object id) {
+ if (vertexIdKey != null) {
+ Object vertexId = map.remove(vertexIdKey);
+ if (vertexId != null) {
+ vertexIds.put(id, vertexId);
+ return graph.addVertex(vertexId);
+ }
+ // if no id try to use default id this will fail if not unique
+ vertexIds.put(id, id);
+ }
+ // Use default id system
+ return graph.addVertex(id);
+ }
+
+ private void addEdge(Map<String, Object> map) throws IOException {
+ Object source = map.remove(GMLTokens.SOURCE);
+ Object target = map.remove(GMLTokens.TARGET);
+ if (source == null) {
+ throw new IOException("Edge has no source");
+ }
+ if (target == null) {
+ throw new IOException("Edge has no target");
+ }
+ if (vertexIdKey != null) {
+ source = vertexIds.get(source);
+ target = vertexIds.get(target);
+ }
+
+ Vertex outVertex = graph.getVertex(source);
+ Vertex inVertex = graph.getVertex(target);
+ if (outVertex == null) {
+ throw new IOException("Edge source " + source + " not found");
+ }
+ if (inVertex == null) {
+ throw new IOException("Edge target " + target + " not found");
+
+ }
+
+ Object label = map.remove(edgeLabelKey);
+ if (label == null) {
+ // try standard label key
+ label = map.remove(GMLTokens.LABEL);
+ } else {
+ // remove label in case edge label key is not label
+ // label is reserved and cannot be added as a property
+ // if so this data will be lost
+ map.remove(GMLTokens.LABEL);
+ }
+
+ if (label == null) {
+ label = defaultEdgeLabel;
+ }
+ Object edgeId = edgeCount++;
+ if (edgeIdKey != null) {
+ Object mappedKey = map.remove(edgeIdKey);
+ if (mappedKey != null) {
+ edgeId = mappedKey;
+ }
+ // else use edgecount - could fail if mapped ids overlap with edge count
+ }
+
+ // remove id as reserved property - can be left is edgeIdKey in not id
+ // This data will be lost
+ map.remove(GMLTokens.ID);
+
+ Edge edge = graph.addEdge(edgeId, outVertex, inVertex, label.toString());
+ if (directed) {
+ edge.setProperty(GMLTokens.DIRECTED, directed);
+ }
+ addProperties(edge, map);
+
+ }
+
+ private void addProperties(Element element, Map<String, Object> map) {
+ for (Entry<String, Object> entry : map.entrySet()) {
+ element.setProperty(entry.getKey(), entry.getValue());
+ }
+ }
+
+ private Object parseValue(String key, StreamTokenizer st) throws IOException {
+ while (hasNext(st)) {
+ int type = st.ttype;
+ if (notLineBreak(type)) {
+ if (type == StreamTokenizer.TT_NUMBER) {
+ Double doubleValue = Double.valueOf(st.nval);
+ if (doubleValue.equals(Double.valueOf(doubleValue.intValue()))) {
+ return doubleValue.intValue();
+ } else {
+ return doubleValue.floatValue();
+ }
+ } else {
+ if (type == '[') {
+ return parseMap(key, st);
+ } else if (type == '"') {
+ return st.sval;
+ }
+ }
+ }
+ }
+ throw new IOException("value not found");
+ }
+
+ private boolean parseBoolean(StreamTokenizer st) throws IOException {
+ while (hasNext(st)) {
+ int type = st.ttype;
+ if (notLineBreak(type)) {
+ if (type == StreamTokenizer.TT_NUMBER) {
+ return st.nval == 1.0;
+ }
+ }
+ }
+ throw new IOException("boolean not found");
+ }
+
+ private Map<String, Object> parseNode(StreamTokenizer st) throws IOException {
+ return parseElement(st, GMLTokens.NODE);
+ }
+
+ private Map<String, Object> parseEdge(StreamTokenizer st) throws IOException {
+ return parseElement(st, GMLTokens.EDGE);
+ }
+
+ private Map<String, Object> parseElement(StreamTokenizer st, String node) throws IOException {
+ checkValid(st, node);
+ return parseMap(node, st);
+ }
+
+ private Map<String, Object> parseMap(String node, StreamTokenizer st) throws IOException {
+ Map<String, Object> map = new HashMap<String, Object>();
+ while (hasNext(st)) {
+ int type = st.ttype;
+ if (notLineBreak(type)) {
+ if (type == ']') {
+ return map;
+ } else {
+ String key = st.sval;
+ Object value = parseValue(key, st);
+ map.put(key, value);
+ }
+ }
+ }
+ throw new IOException(node + " incomplete");
+ }
+
+ private void checkValid(StreamTokenizer st, String token) throws IOException {
+ if (st.nextToken() != '[') {
+ throw new IOException(token + " not followed by [");
+ }
+ }
+
+ /**
+ * Load the GML file into the Graph.
+ *
+ * @param graph to receive the data
+ * @param inputStream GML file
+ * @throws IOException thrown if the data is not valid
+ */
+ public static void inputGraph(Graph graph, InputStream inputStream) throws IOException {
+ inputGraph(graph, inputStream, DEFAULT_LABEL);
+ }
+
+ /**
+ * Load the GML file into the Graph.
+ *
+ * @param graph to receive the data
+ * @param inputStream GML file
+ * @param defaultEdgeLabel default edge label to be used if not defined in the data
+ * @throws IOException thrown if the data is not valid
+ */
+ public static void inputGraph(Graph graph, InputStream inputStream, String defaultEdgeLabel) throws IOException {
+ new GMLReader(graph, defaultEdgeLabel).inputGraph(inputStream, DEFAULT_BUFFER_SIZE);
+ }
}
View
45 blueprints-core/src/main/java/com/tinkerpop/blueprints/pgm/util/io/gml/GMLTokens.java
@@ -2,33 +2,32 @@
/**
* A collection of tokens used for GML related data.
- *
+ * <p/>
* Tokens defined from GML Tags
* (http://www.fim.uni-passau.de/fileadmin/files/lehrstuhl/brandenburg/projekte/gml/gml-documentation.tar.gz)
- *
+ *
* @author Stuart Hendren (http://stuarthendren.net)
- *
*/
public class GMLTokens {
- public static final String GML = "gml";
- public static final String ID = "id";
- public static final String NAME = "name";
- public static final String LABEL = "label";
- public static final String COMMENT = "comment";
- public static final String CREATOR = "Creator";
- public static final String VERSION = "Version";
- public static final String GRAPH = "graph";
- public static final String NODE = "node";
- public static final String EDGE = "edge";
- public static final String SOURCE = "source";
- public static final String TARGET = "target";
- public static final String DIRECTED = "directed"; // directed (0) undirected (1) default is undirected
- public static final String GRAPHICS = "graphics";
- public static final String LABEL_GRAPHICS = "LabelGraphics";
- public static final char COMMENT_CHAR = '#';
+ public static final String GML = "gml";
+ public static final String ID = "id";
+ public static final String NAME = "name";
+ public static final String LABEL = "label";
+ public static final String COMMENT = "comment";
+ public static final String CREATOR = "Creator";
+ public static final String VERSION = "Version";
+ public static final String GRAPH = "graph";
+ public static final String NODE = "node";
+ public static final String EDGE = "edge";
+ public static final String SOURCE = "source";
+ public static final String TARGET = "target";
+ public static final String DIRECTED = "directed"; // directed (0) undirected (1) default is undirected
+ public static final String GRAPHICS = "graphics";
+ public static final String LABEL_GRAPHICS = "LabelGraphics";
+ public static final char COMMENT_CHAR = '#';
- /**
- * Special token used to store Blueprint ids as they may not be integers
- */
- public static final String BLUEPRINTS_ID = "blueprintsId";
+ /**
+ * Special token used to store Blueprint ids as they may not be integers
+ */
+ public static final String BLUEPRINTS_ID = "blueprintsId";
}
View
502 blueprints-core/src/main/java/com/tinkerpop/blueprints/pgm/util/io/gml/GMLWriter.java
@@ -1,5 +1,11 @@
package com.tinkerpop.blueprints.pgm.util.io.gml;
+import com.tinkerpop.blueprints.pgm.Edge;
+import com.tinkerpop.blueprints.pgm.Element;
+import com.tinkerpop.blueprints.pgm.Graph;
+import com.tinkerpop.blueprints.pgm.Vertex;
+import com.tinkerpop.blueprints.pgm.util.LexicographicalElementComparator;
+
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStream;
@@ -13,269 +19,251 @@
import java.util.Map;
import java.util.Map.Entry;
-import com.tinkerpop.blueprints.pgm.Edge;
-import com.tinkerpop.blueprints.pgm.Element;
-import com.tinkerpop.blueprints.pgm.Graph;
-import com.tinkerpop.blueprints.pgm.Vertex;
-import com.tinkerpop.blueprints.pgm.util.LexicographicalElementComparator;
-
/**
* GMLWriter writes a Graph to a GML OutputStream.
- *
+ * <p/>
* GML definition taken from
* (http://www.fim.uni-passau.de/fileadmin/files/lehrstuhl/brandenburg/projekte/gml/gml-documentation.tar.gz)
- *
- *
+ *
* @author Stuart Hendren (http://stuarthendren.net)
- *
*/
public class GMLWriter {
- private static final String DELIMITER = " ";
- private static final String TAB = "\t";
- private static final String NEW_LINE = "\r\n";
- private static final String OPEN_LIST = " [" + NEW_LINE;
- private static final String CLOSE_LIST = "]" + NEW_LINE;
- private final Graph graph;
- private boolean normalize = false;
- private boolean useId = false;
- private String vertexIdKey = GMLTokens.BLUEPRINTS_ID;
- private String edgeIdKey = GMLTokens.BLUEPRINTS_ID;
-
- /**
- * @param graph
- * the Graph to pull the data from
- */
- public GMLWriter(final Graph graph) {
- this.graph = graph;
- }
-
- /**
- * @param normalize
- * whether to normalize the output. Normalized output is deterministic with respect to the order of
- * elements and properties in the resulting XML document, and is compatible with line diff-based tools
- * such as Git. Note: normalized output is memory-intensive and is not appropriate for very large graphs.
- */
- public void setNormalize(final boolean normalize) {
- this.normalize = normalize;
- }
-
- /**
- * @param useId
- * whether to use the blueprints id directly or substitute with a generated integer. To use this option
- * the blueprints ids must all be Integers of String representations of integers
- */
- public void setUseId(final boolean useId) {
- this.useId = useId;
- }
-
- /**
- * @param vertexIdKey
- * gml property to use for the blueprints vertex id, defaults to {@link GMLTokens#BLUEPRINTS_ID}
- */
- public void setVertexIdKey(String vertexIdKey) {
- this.vertexIdKey = vertexIdKey;
- }
-
- /**
- * @param edgeIdKey
- * gml property to use for the blueprints edges id, defaults to {@link GMLTokens#BLUEPRINTS_ID}
- */
- public void setEdgeIdKey(String edgeIdKey) {
- this.edgeIdKey = edgeIdKey;
- }
-
- /**
- * Write the data in a Graph to a GML OutputStream.
- *
- * @param gMLOutputStream
- * the GML OutputStream to write the Graph data to
- * @throws IOException
- * thrown if there is an error generating the GML data
- */
- public void outputGraph(final OutputStream gMLOutputStream) throws IOException {
-
- // ISO 8859-1 as specified in the GML documentation
- Writer writer = new BufferedWriter(new OutputStreamWriter(gMLOutputStream, Charset.forName("ISO-8859-1")));
-
- List<Vertex> verticies = new ArrayList<Vertex>();
- List<Edge> edges = new ArrayList<Edge>();
-
- populateLists(verticies, edges);
-
- if (normalize) {
- LexicographicalElementComparator comparator = new LexicographicalElementComparator();
- Collections.sort(verticies, comparator);
- Collections.sort(edges, comparator);
- }
-
- writeGraph(writer, verticies, edges);
-
- writer.flush();
- writer.close();
- }
-
- private void writeGraph(Writer writer, List<Vertex> verticies, List<Edge> edges) throws IOException {
- Map<Vertex, Integer> ids = new HashMap<Vertex, Integer>();
-
- writer.write(GMLTokens.GRAPH);
- writer.write(OPEN_LIST);
- writeVerticies(writer, verticies, ids);
- writeEdges(writer, edges, ids);
- writer.write(CLOSE_LIST);
-
- }
-
- private void writeVerticies(Writer writer, List<Vertex> verticies, Map<Vertex, Integer> ids) throws IOException {
- int count = 1;
- for (Vertex v : verticies) {
- if (useId) {
- Integer id = Integer.valueOf(v.getId().toString());
- writeVertex(writer, v, id);
- ids.put(v, id);
- } else {
- writeVertex(writer, v, count);
- ids.put(v, count++);
- }
-
- }
- }
-
- private void writeVertex(Writer writer, Vertex v, int id) throws IOException {
- writer.write(TAB);
- writer.write(GMLTokens.NODE);
- writer.write(OPEN_LIST);
- writeKey(writer, GMLTokens.ID);
- writeNumberProperty(writer, id);
- writeVertexProperties(writer, v);
- writer.write(TAB);
- writer.write(CLOSE_LIST);
- }
-
- private void writeEdges(Writer writer, List<Edge> edges, Map<Vertex, Integer> ids) throws IOException {
- for (Edge e : edges) {
- writeEdgeProperties(writer, e, ids.get(e.getOutVertex()), ids.get(e.getInVertex()));
- }
- }
-
- private void writeEdgeProperties(Writer writer, Edge e, Integer source, Integer target) throws IOException {
- writer.write(TAB);
- writer.write(GMLTokens.EDGE);
- writer.write(OPEN_LIST);
- writeKey(writer, GMLTokens.SOURCE);
- writeNumberProperty(writer, source);
- writeKey(writer, GMLTokens.TARGET);
- writeNumberProperty(writer, target);
- writeKey(writer, GMLTokens.LABEL);
- writeStringProperty(writer, e.getLabel());
- writeEdgeProperties(writer, e);
- writer.write(TAB);
- writer.write(CLOSE_LIST);
- }
-
- private void writeVertexProperties(Writer writer, Vertex e) throws IOException {
- Object blueprintsId = e.getId();
- if (!useId) {
- writeKey(writer, vertexIdKey);
- if (blueprintsId instanceof Number) {
- writeNumberProperty(writer, (Number) blueprintsId);
- } else {
- writeStringProperty(writer, blueprintsId);
- }
- }
- writeProperties(writer, e);
- }
-
- private void writeEdgeProperties(Writer writer, Edge e) throws IOException {
- Object blueprintsId = e.getId();
- if (!useId) {
- writeKey(writer, edgeIdKey);
- if (blueprintsId instanceof Number) {
- writeNumberProperty(writer, (Number) blueprintsId);
- } else {
- writeStringProperty(writer, blueprintsId);
- }
- }
- writeProperties(writer, e);
- }
-
- private void writeProperties(Writer writer, Element e) throws IOException {
- for (String key : e.getPropertyKeys()) {
- Object property = e.getProperty(key);
- writeKey(writer, key);
- writeProperty(writer, property, 0);
- }
- }
-
- private void writeProperty(Writer writer, Object property, int tab) throws IOException {
- if (property instanceof Number) {
- writeNumberProperty(writer, (Number) property);
- } else if (property instanceof Map) {
- writeMapProperty(writer, (Map<?, ?>) property, tab);
- } else {
- writeStringProperty(writer, property.toString());
- }
- }
-
- private void writeMapProperty(Writer writer, Map<?, ?> map, int tabs) throws IOException {
- writer.write(OPEN_LIST);
- tabs++;
- for (Entry<?, ?> entry : map.entrySet()) {
- writeTabs(writer, tabs);
- writeKey(writer, entry.getKey().toString());
- writeProperty(writer, entry.getValue(), tabs);
- }
- writeTabs(writer, tabs - 1);
- writer.write(CLOSE_LIST);
-
- }
-
- private void writeTabs(Writer writer, int tabs) throws IOException {
- for (int i = 0; i <= tabs; i++) {
- writer.write(TAB);
- }
- }
-
- private void writeNumberProperty(Writer writer, Number integer) throws IOException {
- writer.write(integer.toString());
- writer.write(NEW_LINE);
- }
-
- private void writeStringProperty(Writer writer, Object string) throws IOException {
- writer.write("\"");
- writer.write(string.toString());
- writer.write("\"");
- writer.write(NEW_LINE);
- }
-
- private void writeKey(Writer writer, String command) throws IOException {
- writer.write(TAB);
- writer.write(TAB);
- writer.write(command);
- writer.write(DELIMITER);
- }
-
- private void populateLists(List<Vertex> verticies, List<Edge> edges) {
- for (Vertex v : graph.getVertices()) {
- verticies.add(v);
- }
- for (Edge e : graph.getEdges()) {
- edges.add(e);
- }
- }
-
- /**
- * Write the data in a Graph to a GML OutputStream.
- *
- * @param graph
- * the Graph to pull the data from
- * @param graphMLOutputStream
- * the GML OutputStream to write the Graph data to
- * @throws IOException
- * thrown if there is an error generating the GML data
- */
- public static void outputGraph(final Graph graph, final OutputStream graphMLOutputStream) throws IOException {
- GMLWriter writer = new GMLWriter(graph);
- writer.outputGraph(graphMLOutputStream);
- }
+ private static final String DELIMITER = " ";
+ private static final String TAB = "\t";
+ private static final String NEW_LINE = "\r\n";
+ private static final String OPEN_LIST = " [" + NEW_LINE;
+ private static final String CLOSE_LIST = "]" + NEW_LINE;
+ private final Graph graph;
+ private boolean normalize = false;
+ private boolean useId = false;
+ private String vertexIdKey = GMLTokens.BLUEPRINTS_ID;
+ private String edgeIdKey = GMLTokens.BLUEPRINTS_ID;
+
+ /**
+ * @param graph the Graph to pull the data from
+ */
+ public GMLWriter(final Graph graph) {
+ this.graph = graph;
+ }
+
+ /**
+ * @param normalize whether to normalize the output. Normalized output is deterministic with respect to the order of
+ * elements and properties in the resulting XML document, and is compatible with line diff-based tools
+ * such as Git. Note: normalized output is memory-intensive and is not appropriate for very large graphs.
+ */
+ public void setNormalize(final boolean normalize) {
+ this.normalize = normalize;
+ }
+
+ /**
+ * @param useId whether to use the blueprints id directly or substitute with a generated integer. To use this option
+ * the blueprints ids must all be Integers of String representations of integers
+ */
+ public void setUseId(final boolean useId) {
+ this.useId = useId;
+ }
+
+ /**
+ * @param vertexIdKey gml property to use for the blueprints vertex id, defaults to {@link GMLTokens#BLUEPRINTS_ID}
+ */
+ public void setVertexIdKey(String vertexIdKey) {
+ this.vertexIdKey = vertexIdKey;
+ }
+
+ /**
+ * @param edgeIdKey gml property to use for the blueprints edges id, defaults to {@link GMLTokens#BLUEPRINTS_ID}
+ */
+ public void setEdgeIdKey(String edgeIdKey) {
+ this.edgeIdKey = edgeIdKey;
+ }
+
+ /**
+ * Write the data in a Graph to a GML OutputStream.
+ *
+ * @param gMLOutputStream the GML OutputStream to write the Graph data to
+ * @throws IOException thrown if there is an error generating the GML data
+ */
+ public void outputGraph(final OutputStream gMLOutputStream) throws IOException {
+
+ // ISO 8859-1 as specified in the GML documentation
+ Writer writer = new BufferedWriter(new OutputStreamWriter(gMLOutputStream, Charset.forName("ISO-8859-1")));
+
+ List<Vertex> verticies = new ArrayList<Vertex>();
+ List<Edge> edges = new ArrayList<Edge>();
+
+ populateLists(verticies, edges);
+
+ if (normalize) {
+ LexicographicalElementComparator comparator = new LexicographicalElementComparator();
+ Collections.sort(verticies, comparator);
+ Collections.sort(edges, comparator);
+ }
+
+ writeGraph(writer, verticies, edges);
+
+ writer.flush();
+ writer.close();
+ }
+
+ private void writeGraph(Writer writer, List<Vertex> verticies, List<Edge> edges) throws IOException {
+ Map<Vertex, Integer> ids = new HashMap<Vertex, Integer>();
+
+ writer.write(GMLTokens.GRAPH);
+ writer.write(OPEN_LIST);
+ writeVerticies(writer, verticies, ids);
+ writeEdges(writer, edges, ids);
+ writer.write(CLOSE_LIST);
+
+ }
+
+ private void writeVerticies(Writer writer, List<Vertex> verticies, Map<Vertex, Integer> ids) throws IOException {
+ int count = 1;
+ for (Vertex v : verticies) {
+ if (useId) {
+ Integer id = Integer.valueOf(v.getId().toString());
+ writeVertex(writer, v, id);
+ ids.put(v, id);
+ } else {
+ writeVertex(writer, v, count);
+ ids.put(v, count++);
+ }
+
+ }
+ }
+
+ private void writeVertex(Writer writer, Vertex v, int id) throws IOException {
+ writer.write(TAB);
+ writer.write(GMLTokens.NODE);
+ writer.write(OPEN_LIST);
+ writeKey(writer, GMLTokens.ID);
+ writeNumberProperty(writer, id);
+ writeVertexProperties(writer, v);
+ writer.write(TAB);
+ writer.write(CLOSE_LIST);
+ }
+
+ private void writeEdges(Writer writer, List<Edge> edges, Map<Vertex, Integer> ids) throws IOException {
+ for (Edge e : edges) {
+ writeEdgeProperties(writer, e, ids.get(e.getOutVertex()), ids.get(e.getInVertex()));
+ }
+ }
+
+ private void writeEdgeProperties(Writer writer, Edge e, Integer source, Integer target) throws IOException {
+ writer.write(TAB);
+ writer.write(GMLTokens.EDGE);
+ writer.write(OPEN_LIST);
+ writeKey(writer, GMLTokens.SOURCE);
+ writeNumberProperty(writer, source);
+ writeKey(writer, GMLTokens.TARGET);
+ writeNumberProperty(writer, target);
+ writeKey(writer, GMLTokens.LABEL);
+ writeStringProperty(writer, e.getLabel());
+ writeEdgeProperties(writer, e);
+ writer.write(TAB);
+ writer.write(CLOSE_LIST);
+ }
+
+ private void writeVertexProperties(Writer writer, Vertex e) throws IOException {
+ Object blueprintsId = e.getId();
+ if (!useId) {
+ writeKey(writer, vertexIdKey);
+ if (blueprintsId instanceof Number) {
+ writeNumberProperty(writer, (Number) blueprintsId);
+ } else {
+ writeStringProperty(writer, blueprintsId);
+ }
+ }
+ writeProperties(writer, e);
+ }
+
+ private void writeEdgeProperties(Writer writer, Edge e) throws IOException {
+ Object blueprintsId = e.getId();
+ if (!useId) {
+ writeKey(writer, edgeIdKey);
+ if (blueprintsId instanceof Number) {
+ writeNumberProperty(writer, (Number) blueprintsId);
+ } else {
+ writeStringProperty(writer, blueprintsId);
+ }
+ }
+ writeProperties(writer, e);
+ }
+
+ private void writeProperties(Writer writer, Element e) throws IOException {
+ for (String key : e.getPropertyKeys()) {
+ Object property = e.getProperty(key);
+ writeKey(writer, key);
+ writeProperty(writer, property, 0);
+ }
+ }
+
+ private void writeProperty(Writer writer, Object property, int tab) throws IOException {
+ if (property instanceof Number) {
+ writeNumberProperty(writer, (Number) property);
+ } else if (property instanceof Map) {
+ writeMapProperty(writer, (Map<?, ?>) property, tab);
+ } else {
+ writeStringProperty(writer, property.toString());
+ }
+ }
+
+ private void writeMapProperty(Writer writer, Map<?, ?> map, int tabs) throws IOException {
+ writer.write(OPEN_LIST);
+ tabs++;
+ for (Entry<?, ?> entry : map.entrySet()) {
+ writeTabs(writer, tabs);
+ writeKey(writer, entry.getKey().toString());
+ writeProperty(writer, entry.getValue(), tabs);
+ }
+ writeTabs(writer, tabs - 1);
+ writer.write(CLOSE_LIST);
+
+ }
+
+ private void writeTabs(Writer writer, int tabs) throws IOException {
+ for (int i = 0; i <= tabs; i++) {
+ writer.write(TAB);
+ }
+ }
+
+ private void writeNumberProperty(Writer writer, Number integer) throws IOException {
+ writer.write(integer.toString());
+ writer.write(NEW_LINE);
+ }
+
+ private void writeStringProperty(Writer writer, Object string) throws IOException {
+ writer.write("\"");
+ writer.write(string.toString());
+ writer.write("\"");
+ writer.write(NEW_LINE);
+ }
+
+ private void writeKey(Writer writer, String command) throws IOException {
+ writer.write(TAB);
+ writer.write(TAB);
+ writer.write(command);
+ writer.write(DELIMITER);
+ }
+
+ private void populateLists(List<Vertex> verticies, List<Edge> edges) {
+ for (Vertex v : graph.getVertices()) {
+ verticies.add(v);
+ }
+ for (Edge e : graph.getEdges()) {
+ edges.add(e);
+ }
+ }
+
+ /**
+ * Write the data in a Graph to a GML OutputStream.
+ *
+ * @param graph the Graph to pull the data from
+ * @param graphMLOutputStream the GML OutputStream to write the Graph data to
+ * @throws IOException thrown if there is an error generating the GML data
+ */
+ public static void outputGraph(final Graph graph, final OutputStream graphMLOutputStream) throws IOException {
+ GMLWriter writer = new GMLWriter(graph);
+ writer.outputGraph(graphMLOutputStream);
+ }
}
View
513 ...prints-core/src/main/java/com/tinkerpop/blueprints/pgm/util/io/graphml/GraphMLWriter.java
@@ -1,5 +1,13 @@
package com.tinkerpop.blueprints.pgm.util.io.graphml;
+import com.tinkerpop.blueprints.pgm.Edge;
+import com.tinkerpop.blueprints.pgm.Graph;
+import com.tinkerpop.blueprints.pgm.Vertex;
+import com.tinkerpop.blueprints.pgm.util.LexicographicalElementComparator;
+
+import javax.xml.stream.XMLOutputFactory;
+import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.XMLStreamWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Collection;
@@ -9,301 +17,278 @@
import java.util.List;
import java.util.Map;
-import javax.xml.stream.XMLOutputFactory;
-import javax.xml.stream.XMLStreamException;
-import javax.xml.stream.XMLStreamWriter;
-
-import com.tinkerpop.blueprints.pgm.Edge;
-import com.tinkerpop.blueprints.pgm.Graph;
-import com.tinkerpop.blueprints.pgm.Vertex;
-import com.tinkerpop.blueprints.pgm.util.LexicographicalElementComparator;
-
/**
* GraphMLWriter writes a Graph to a GraphML OutputStream.
- *
+ *
* @author Marko A. Rodriguez (http://markorodriguez.com)
* @author Joshua Shinavier (http://fortytwo.net)
*/
public class GraphMLWriter {
- private final Graph graph;
- private boolean normalize = false;
- private Map<String, String> vertexKeyTypes = null;
- private Map<String, String> edgeKeyTypes = null;
+ private final Graph graph;
+ private boolean normalize = false;
+ private Map<String, String> vertexKeyTypes = null;
+ private Map<String, String> edgeKeyTypes = null;
- /**
- * @param graph
- * the Graph to pull the data from
- */
- public GraphMLWriter(final Graph graph) {
- this.graph = graph;
- }
+ /**
+ * @param graph the Graph to pull the data from
+ */
+ public GraphMLWriter(final Graph graph) {
+ this.graph = graph;
+ }
- /**
- * @param normalize
- * whether to normalize the output. Normalized output is deterministic with respect to the order of
- * elements and properties in the resulting XML document, and is compatible with line diff-based tools
- * such as Git. Note: normalized output is memory-intensive and is not appropriate for very large graphs.
- */
- public void setNormalize(final boolean normalize) {
- this.normalize = normalize;
- }
+ /**
+ * @param normalize whether to normalize the output. Normalized output is deterministic with respect to the order of
+ * elements and properties in the resulting XML document, and is compatible with line diff-based tools
+ * such as Git. Note: normalized output is memory-intensive and is not appropriate for very large graphs.
+ */
+ public void setNormalize(final boolean normalize) {
+ this.normalize = normalize;
+ }
- /**
- * @param vertexKeyTypes
- * a Map of the data types of the vertex keys
- */
- public void setVertexKeyTypes(final Map<String, String> vertexKeyTypes) {
- this.vertexKeyTypes = vertexKeyTypes;
- }
+ /**
+ * @param vertexKeyTypes a Map of the data types of the vertex keys
+ */
+ public void setVertexKeyTypes(final Map<String, String> vertexKeyTypes) {
+ this.vertexKeyTypes = vertexKeyTypes;
+ }
- /**
- * @param edgeKeyTypes
- * a Map of the data types of the edge keys
- */
- public void setEdgeKeyTypes(final Map<String, String> edgeKeyTypes) {
- this.edgeKeyTypes = edgeKeyTypes;
- }
+ /**
+ * @param edgeKeyTypes a Map of the data types of the edge keys
+ */
+ public void setEdgeKeyTypes(final Map<String, String> edgeKeyTypes) {
+ this.edgeKeyTypes = edgeKeyTypes;
+ }
- /**
- * Write the data in a Graph to a GraphML OutputStream.
- *
- * @param graphMLOutputStream
- * the GraphML OutputStream to write the Graph data to
- * @throws IOException
- * thrown if there is an error generating the GraphML data
- */
- public void outputGraph(final OutputStream graphMLOutputStream) throws IOException {
+ /**
+ * Write the data in a Graph to a GraphML OutputStream.
+ *
+ * @param graphMLOutputStream the GraphML OutputStream to write the Graph data to
+ * @throws IOException thrown if there is an error generating the GraphML data
+ */
+ public void outputGraph(final OutputStream graphMLOutputStream) throws IOException {
- if (null == vertexKeyTypes || null == edgeKeyTypes) {
- Map<String, String> vertexKeyTypes = new HashMap<String, String>();
- Map<String, String> edgeKeyTypes = new HashMap<String, String>();
+ if (null == vertexKeyTypes || null == edgeKeyTypes) {
+ Map<String, String> vertexKeyTypes = new HashMap<String, String>();
+ Map<String, String> edgeKeyTypes = new HashMap<String, String>();
- for (Vertex vertex : graph.getVertices()) {
- for (String key : vertex.getPropertyKeys()) {
- if (!vertexKeyTypes.containsKey(key)) {
- vertexKeyTypes.put(key, GraphMLWriter.getStringType(vertex.getProperty(key)));
- }
- }
- for (Edge edge : vertex.getOutEdges()) {
- for (String key : edge.getPropertyKeys()) {
- if (!edgeKeyTypes.containsKey(key)) {
- edgeKeyTypes.put(key, GraphMLWriter.getStringType(edge.getProperty(key)));
- }
- }
- }
- }
+ for (Vertex vertex : graph.getVertices()) {
+ for (String key : vertex.getPropertyKeys()) {
+ if (!vertexKeyTypes.containsKey(key)) {
+ vertexKeyTypes.put(key, GraphMLWriter.getStringType(vertex.getProperty(key)));
+ }
+ }
+ for (Edge edge : vertex.getOutEdges()) {
+ for (String key : edge.getPropertyKeys()) {
+ if (!edgeKeyTypes.containsKey(key)) {
+ edgeKeyTypes.put(key, GraphMLWriter.getStringType(edge.getProperty(key)));
+ }
+ }
+ }
+ }
- if (null == this.vertexKeyTypes) {
- this.vertexKeyTypes = vertexKeyTypes;
- }
+ if (null == this.vertexKeyTypes) {
+ this.vertexKeyTypes = vertexKeyTypes;
+ }
- if (null == this.edgeKeyTypes) {
- this.edgeKeyTypes = edgeKeyTypes;
- }
- }
+ if (null == this.edgeKeyTypes) {
+ this.edgeKeyTypes = edgeKeyTypes;
+ }
+ }
- XMLOutputFactory inputFactory = XMLOutputFactory.newInstance();
- try {
- XMLStreamWriter writer = inputFactory.createXMLStreamWriter(graphMLOutputStream, "UTF8");
- if (normalize) {
- writer = new GraphMLWriterHelper.IndentingXMLStreamWriter(writer);
- ((GraphMLWriterHelper.IndentingXMLStreamWriter) writer).setIndentStep(" ");
- }
+ XMLOutputFactory inputFactory = XMLOutputFactory.newInstance();
+ try {
+ XMLStreamWriter writer = inputFactory.createXMLStreamWriter(graphMLOutputStream, "UTF8");
+ if (normalize) {
+ writer = new GraphMLWriterHelper.IndentingXMLStreamWriter(writer);
+ ((GraphMLWriterHelper.IndentingXMLStreamWriter) writer).setIndentStep(" ");
+ }
- writer.writeStartDocument();
- writer.writeStartElement(GraphMLTokens.GRAPHML);
- writer.writeAttribute(GraphMLTokens.XMLNS, GraphMLTokens.GRAPHML_XMLNS);
+ writer.writeStartDocument();
+ writer.writeStartElement(GraphMLTokens.GRAPHML);
+ writer.writeAttribute(GraphMLTokens.XMLNS, GraphMLTokens.GRAPHML_XMLNS);
- // <key id="weight" for="edge" attr.name="weight" attr.type="float"/>
- Collection<String> keyset;
+ // <key id="weight" for="edge" attr.name="weight" attr.type="float"/>
+ Collection<String> keyset;
- if (normalize) {
- keyset = new LinkedList<String>();
- keyset.addAll(vertexKeyTypes.keySet());
- Collections.sort((List<String>) keyset);
- } else {
- keyset = vertexKeyTypes.keySet();
- }
- for (String key : keyset) {
- writer.writeStartElement(GraphMLTokens.KEY);
- writer.writeAttribute(GraphMLTokens.ID, key);
- writer.writeAttribute(GraphMLTokens.FOR, GraphMLTokens.NODE);
- writer.writeAttribute(GraphMLTokens.ATTR_NAME, key);
- writer.writeAttribute(GraphMLTokens.ATTR_TYPE, vertexKeyTypes.get(key));
- writer.writeEndElement();
- }
+ if (normalize) {
+ keyset = new LinkedList<String>();
+ keyset.addAll(vertexKeyTypes.keySet());
+ Collections.sort((List<String>) keyset);
+ } else {
+ keyset = vertexKeyTypes.keySet();
+ }
+ for (String key : keyset) {
+ writer.writeStartElement(GraphMLTokens.KEY);
+ writer.writeAttribute(GraphMLTokens.ID, key);
+ writer.writeAttribute(GraphMLTokens.FOR, GraphMLTokens.NODE);
+ writer.writeAttribute(GraphMLTokens.ATTR_NAME, key);
+ writer.writeAttribute(GraphMLTokens.ATTR_TYPE, vertexKeyTypes.get(key));
+ writer.writeEndElement();
+ }
- if (normalize) {
- keyset = new LinkedList<String>();
- keyset.addAll(edgeKeyTypes.keySet());
- Collections.sort((List<String>) keyset);
- } else {
- keyset = edgeKeyTypes.keySet();
- }
- for (String key : keyset) {
- writer.writeStartElement(GraphMLTokens.KEY);
- writer.writeAttribute(GraphMLTokens.ID, key);
- writer.writeAttribute(GraphMLTokens.FOR, GraphMLTokens.EDGE);
- writer.writeAttribute(GraphMLTokens.ATTR_NAME, key);
- writer.writeAttribute(GraphMLTokens.ATTR_TYPE, edgeKeyTypes.get(key));
- writer.writeEndElement();
- }
+ if (normalize) {
+ keyset = new LinkedList<String>();
+ keyset.addAll(edgeKeyTypes.keySet());
+ Collections.sort((List<String>) keyset);
+ } else {
+ keyset = edgeKeyTypes.keySet();
+ }
+ for (String key : keyset) {
+ writer.writeStartElement(GraphMLTokens.KEY);
+ writer.writeAttribute(GraphMLTokens.ID, key);
+ writer.writeAttribute(GraphMLTokens.FOR, GraphMLTokens.EDGE);
+ writer.writeAttribute(GraphMLTokens.ATTR_NAME, key);
+ writer.writeAttribute(GraphMLTokens.ATTR_TYPE, edgeKeyTypes.get(key));
+ writer.writeEndElement();
+ }
- writer.writeStartElement(GraphMLTokens.GRAPH);
- writer.writeAttribute(GraphMLTokens.ID, GraphMLTokens.G);
- writer.writeAttribute(GraphMLTokens.EDGEDEFAULT, GraphMLTokens.DIRECTED);
+ writer.writeStartElement(GraphMLTokens.GRAPH);
+ writer.writeAttribute(GraphMLTokens.ID, GraphMLTokens.G);
+ writer.writeAttribute(GraphMLTokens.EDGEDEFAULT, GraphMLTokens.DIRECTED);
- Iterable<Vertex> vertices;
- if (normalize) {
- vertices = new LinkedList<Vertex>();
- for (Vertex v : graph.getVertices()) {
- ((Collection<Vertex>) vertices).add(v);
- }
- Collections.sort((List<Vertex>) vertices, new LexicographicalElementComparator());
- } else {
- vertices = graph.getVertices();
- }
- for (Vertex vertex : vertices) {
- writer.writeStartElement(GraphMLTokens.NODE);
- writer.writeAttribute(GraphMLTokens.ID, vertex.getId().toString());
- Collection<String> keys;
- if (normalize) {
- keys = new LinkedList<String>();
- keys.addAll(vertex.getPropertyKeys());
- Collections.sort((List<String>) keys);
- } else {
- keys = vertex.getPropertyKeys();
- }
- for (String key : keys) {
- writer.writeStartElement(GraphMLTokens.DATA);
- writer.writeAttribute(GraphMLTokens.KEY, key);
- Object value = vertex.getProperty(key);
- if (null != value) {
- writer.writeCharacters(value.toString());
- }
- writer.writeEndElement();
- }
- writer.writeEndElement();
- }
+ Iterable<Vertex> vertices;
+ if (normalize) {
+ vertices = new LinkedList<Vertex>();
+ for (Vertex v : graph.getVertices()) {
+ ((Collection<Vertex>) vertices).add(v);
+ }
+ Collections.sort((List<Vertex>) vertices, new LexicographicalElementComparator());
+ } else {
+ vertices = graph.getVertices();
+ }
+ for (Vertex vertex : vertices) {
+ writer.writeStartElement(GraphMLTokens.NODE);
+ writer.writeAttribute(GraphMLTokens.ID, vertex.getId().toString());
+ Collection<String> keys;
+ if (normalize) {
+ keys = new LinkedList<String>();
+ keys.addAll(vertex.getPropertyKeys());
+ Collections.sort((List<String>) keys);
+ } else {
+ keys = vertex.getPropertyKeys();
+ }
+ for (String key : keys) {
+ writer.writeStartElement(GraphMLTokens.DATA);
+ writer.writeAttribute(GraphMLTokens.KEY, key);
+ Object value = vertex.getProperty(key);
+ if (null != value) {
+ writer.writeCharacters(value.toString());
+ }
+ writer.writeEndElement();
+ }
+ writer.writeEndElement();
+ }
- if (normalize) {
- List<Edge> edges = new LinkedList<Edge>();
- for (Vertex vertex : graph.getVertices()) {
- for (Edge edge : vertex.getOutEdges()) {
- edges.add(edge);
- }
- }
- Collections.sort(edges, new LexicographicalElementComparator());
+ if (normalize) {
+ List<Edge> edges = new LinkedList<Edge>();
+ for (Vertex vertex : graph.getVertices()) {
+ for (Edge edge : vertex.getOutEdges()) {
+ edges.add(edge);
+ }
+ }
+ Collections.sort(edges, new LexicographicalElementComparator());
- for (Edge edge : edges) {
- writer.writeStartElement(GraphMLTokens.EDGE);
- writer.writeAttribute(GraphMLTokens.ID, edge.getId().toString());
- writer.writeAttribute(GraphMLTokens.SOURCE, edge.getOutVertex().getId().toString());
- writer.writeAttribute(GraphMLTokens.TARGET, edge.getInVertex().getId().toString());
- writer.writeAttribute(GraphMLTokens.LABEL, edge.getLabel());
+ for (Edge edge : edges) {
+ writer.writeStartElement(GraphMLTokens.EDGE);
+ writer.writeAttribute(GraphMLTokens.ID, edge.getId().toString());
+ writer.writeAttribute(GraphMLTokens.SOURCE, edge.getOutVertex().getId().toString());
+ writer.writeAttribute(GraphMLTokens.TARGET, edge.getInVertex().getId().toString());
+ writer.writeAttribute(GraphMLTokens.LABEL, edge.getLabel());
- List<String> keys = new LinkedList<String>();
- keys.addAll(edge.getPropertyKeys());
- Collections.sort(keys);
+ List<String> keys = new LinkedList<String>();
+ keys.addAll(edge.getPropertyKeys());
+ Collections.sort(keys);
- for (String key : keys) {
- writer.writeStartElement(GraphMLTokens.DATA);
- writer.writeAttribute(GraphMLTokens.KEY, key);
- Object value = edge.getProperty(key);
- if (null != value) {
- writer.writeCharacters(value.toString());
- }
- writer.writeEndElement();
- }
- writer.writeEndElement();
- }
- } else {
- for (Vertex vertex : graph.getVertices()) {
- for (Edge edge : vertex.getOutEdges()) {
- writer.writeStartElement(GraphMLTokens.EDGE);
- writer.writeAttribute(GraphMLTokens.ID, edge.getId().toString());
- writer.writeAttribute(GraphMLTokens.SOURCE, edge.getOutVertex().getId().toString());
- writer.writeAttribute(GraphMLTokens.TARGET, edge.getInVertex().getId().toString());
- writer.writeAttribute(GraphMLTokens.LABEL, edge.getLabel());
+ for (String key : keys) {
+ writer.writeStartElement(GraphMLTokens.DATA);
+ writer.writeAttribute(GraphMLTokens.KEY, key);
+ Object value = edge.getProperty(key);
+ if (null != value) {
+ writer.writeCharacters(value.toString());
+ }
+ writer.writeEndElement();
+ }
+ writer.writeEndElement();
+ }
+ } else {
+ for (Vertex vertex : graph.getVertices()) {
+ for (Edge edge : vertex.getOutEdges()) {
+ writer.writeStartElement(GraphMLTokens.EDGE);
+ writer.writeAttribute(GraphMLTokens.ID, edge.getId().toString());
+ writer.writeAttribute(GraphMLTokens.SOURCE, edge.getOutVertex().getId().toString());
+ writer.writeAttribute(GraphMLTokens.TARGET, edge.getInVertex().getId().toString());
+ writer.writeAttribute(GraphMLTokens.LABEL, edge.getLabel());
- for (String key : edge.getPropertyKeys()) {
- writer.writeStartElement(GraphMLTokens.DATA);
- writer.writeAttribute(GraphMLTokens.KEY, key);
- Object value = edge.getProperty(key);
- if (null != value) {
- writer.writeCharacters(value.toString());
- }
- writer.writeEndElement();
- }
- writer.writeEndElement();
- }
- }
- }
+ for (String key : edge.getPropertyKeys()) {
+ writer.writeStartElement(GraphMLTokens.DATA);
+ writer.writeAttribute(GraphMLTokens.KEY, key);
+ Object value = edge.getProperty(key);
+ if (null != value) {
+ writer.writeCharacters(value.toString());
+ }
+ writer.writeEndElement();
+ }
+ writer.writeEndElement();
+ }
+ }
+ }
- writer.writeEndElement(); // graph
- writer.writeEndElement(); // graphml
- writer.writeEndDocument();
+ writer.writeEndElement(); // graph
+ writer.writeEndElement(); // graphml
+ writer.writeEndDocument();
- writer.flush();
- writer.close();
- } catch (XMLStreamException xse) {
- throw new IOException(xse);
- }
- }
+ writer.flush();
+ writer.close();
+ } catch (XMLStreamException xse) {
+ throw new IOException(xse);
+ }
+ }
- /**
- * Write the data in a Graph to a GraphML OutputStream.
- *
- * @param graph
- * the Graph to pull the data from
- * @param graphMLOutputStream
- * the GraphML OutputStream to write the Graph data to
- * @throws IOException
- * thrown if there is an error generating the GraphML data
- */
- public static void outputGraph(final Graph graph, final OutputStream graphMLOutputStream) throws IOException {
- GraphMLWriter writer = new GraphMLWriter(graph);
- writer.outputGraph(graphMLOutputStream);
- }
+ /**
+ * Write the data in a Graph to a GraphML OutputStream.
+ *
+ * @param graph the Graph to pull the data from
+ * @param graphMLOutputStream the GraphML OutputStream to write the Graph data to
+ * @throws IOException thrown if there is an error generating the GraphML data
+ */
+ public static void outputGraph(final Graph graph, final OutputStream graphMLOutputStream) throws IOException {
+ GraphMLWriter writer = new GraphMLWriter(graph);
+ writer.outputGraph(graphMLOutputStream);
+ }
- /**
- * Write the data in a Graph to a GraphML OutputStream.
- *
- * @param graph
- * the Graph to pull the data from
- * @param graphMLOutputStream
- * the GraphML OutputStream to write the Graph data to
- * @param vertexKeyTypes
- * a Map of the data types of the vertex keys
- * @param edgeKeyTypes
- * a Map of the data types of the edge keys
- * @throws IOException
- * thrown if there is an error generating the GraphML data
- */
- public static void outputGraph(final Graph graph, final OutputStream graphMLOutputStream,
- final Map<String, String> vertexKeyTypes, final Map<String, String> edgeKeyTypes) throws IOException {
- GraphMLWriter writer = new GraphMLWriter(graph);
- writer.setVertexKeyTypes(vertexKeyTypes);
- writer.setEdgeKeyTypes(edgeKeyTypes);
- writer.outputGraph(graphMLOutputStream);
- }
+ /**
+ * Write the data in a Graph to a GraphML OutputStream.
+ *
+ * @param graph the Graph to pull the data from
+ * @param graphMLOutputStream the GraphML OutputStream to write the Graph data to
+ * @param vertexKeyTypes a Map of the data types of the vertex keys
+ * @param edgeKeyTypes a Map of the data types of the edge keys
+ * @throws IOException thrown if there is an error generating the GraphML data
+ */
+ public static void outputGraph(final Graph graph, final OutputStream graphMLOutputStream,
+ final Map<String, String> vertexKeyTypes, final Map<String, String> edgeKeyTypes) throws IOException {
+ GraphMLWriter writer = new GraphMLWriter(graph);
+ writer.setVertexKeyTypes(vertexKeyTypes);
+ writer.setEdgeKeyTypes(edgeKeyTypes);
+ writer.outputGraph(graphMLOutputStream);
+ }
- private static String getStringType(final Object object) {
- if (object instanceof String) {
- return GraphMLTokens.STRING;
- } else if (object instanceof Integer) {
- return GraphMLTokens.INT;
- } else if (object instanceof Long) {
- return GraphMLTokens.LONG;
- } else if (object instanceof Float) {
- return GraphMLTokens.FLOAT;
- } else if (object instanceof Double) {
- return GraphMLTokens.DOUBLE;
- } else if (object instanceof Boolean) {
- return GraphMLTokens.BOOLEAN;
- } else {
- return GraphMLTokens.STRING;
- }
- }
+ private static String getStringType(final Object object) {
+ if (object instanceof String) {
+ return GraphMLTokens.STRING;
+ } else if (object instanceof Integer) {
+ return GraphMLTokens.INT;
+ } else if (object instanceof Long) {
+ return GraphMLTokens.LONG;
+ } else if (object instanceof Float) {
+ return GraphMLTokens.FLOAT;
+ } else if (object instanceof Double) {
+ return GraphMLTokens.DOUBLE;
+ } else if (object instanceof Boolean) {
+ return GraphMLTokens.BOOLEAN;
+ } else {
+ return GraphMLTokens.STRING;
+ }
+ }
}
View
17 blueprints-core/src/main/java/com/tinkerpop/blueprints/pgm/util/wrappers/GraphWrapper.java
@@ -1,17 +0,0 @@
-package com.tinkerpop.blueprints.pgm.util.wrappers;
-
-import com.tinkerpop.blueprints.pgm.Graph;
-
-/**
- * A GraphWrapper is a Blueprints Graph that has an underlying Graph object to which it delegates
- * its operations.
- *
- * @author Jordan A. Lewis (http://jordanlewis.org)
- */
-public interface GraphWrapper extends Graph {
- /**
- * Get the graph this wrapper delegates to.
- * @return the underlying Blueprints graph that this GraphWrapper delegates its operations to.
- */
- public Graph getRawGraph();
-}
View
15 blueprints-core/src/main/java/com/tinkerpop/blueprints/pgm/util/wrappers/WrappingGraph.java
@@ -0,0 +1,15 @@
+package com.tinkerpop.blueprints.pgm.util.wrappers;
+
+/**
+ * A WrappingGraph has an underlying Graph object to which it delegates its operations.
+ *
+ * @author Jordan A. Lewis (http://jordanlewis.org)
+ */
+public interface WrappingGraph<T> {
+ /**
+ * Get the graph this wrapper delegates to.
+ *
+ * @return the underlying graph that this WrappingGraph delegates its operations to.
+ */
+ public T getRawGraph();
+}
View
10 ...rints-core/src/main/java/com/tinkerpop/blueprints/pgm/util/wrappers/event/EventGraph.java
@@ -4,7 +4,7 @@
import com.tinkerpop.blueprints.pgm.Graph;
import com.tinkerpop.blueprints.pgm.Vertex;
import com.tinkerpop.blueprints.pgm.impls.StringFactory;
-import com.tinkerpop.blueprints.pgm.util.wrappers.GraphWrapper;
+import com.tinkerpop.blueprints.pgm.util.wrappers.WrappingGraph;
import com.tinkerpop.blueprints.pgm.util.wrappers.event.listener.GraphChangedListener;
import com.tinkerpop.blueprints.pgm.util.wrappers.event.util.EventEdgeSequence;
import com.tinkerpop.blueprints.pgm.util.wrappers.event.util.EventVertexSequence;
@@ -27,12 +27,12 @@
*
* @author Stephen Mallette
*/
-public class EventGraph implements GraphWrapper {
- protected final Graph rawGraph;
+public class EventGraph<T extends Graph> implements Graph, WrappingGraph<T> {
+ protected final T rawGraph;
protected final List<GraphChangedListener> graphChangedListeners = new ArrayList<GraphChangedListener>();
- public EventGraph(final Graph rawGraph) {
+ public EventGraph(final T rawGraph) {
this.rawGraph = rawGraph;
}
@@ -184,7 +184,7 @@ public String toString() {
}
@Override
- public Graph getRawGraph() {
+ public T getRawGraph() {
return this.rawGraph;
}
}
View
14 ...e/src/main/java/com/tinkerpop/blueprints/pgm/util/wrappers/event/EventIndexableGraph.java
@@ -6,6 +6,7 @@
import com.tinkerpop.blueprints.pgm.Index;
import com.tinkerpop.blueprints.pgm.IndexableGraph;
import com.tinkerpop.blueprints.pgm.impls.Parameter;
+import com.tinkerpop.blueprints.pgm.util.wrappers.WrappingGraph;
import com.tinkerpop.blueprints.pgm.util.wrappers.event.util.EventIndexSequence;
import java.util.Set;
@@ -18,8 +19,9 @@
*
* @author Stephen Mallette
*/
-public class EventIndexableGraph extends EventGraph implements IndexableGraph {
- public EventIndexableGraph(final IndexableGraph graph) {
+public class EventIndexableGraph<T extends IndexableGraph> extends EventGraph<T> implements IndexableGraph, WrappingGraph<T> {
+
+ public EventIndexableGraph(final T graph) {
super(graph);
}
@@ -36,7 +38,7 @@ public void dropIndex(final String name) {
}
public <T extends Element> Index<T> getIndex(final String indexName, final Class<T> indexClass) {
- final Index<T> index = ((IndexableGraph) this.rawGraph).getIndex(indexName, indexClass);
+ final Index<T> index = this.rawGraph.getIndex(indexName, indexClass);
if (null == index)
return null;
else {
@@ -48,10 +50,6 @@ public void dropIndex(final String name) {
}
public Iterable<Index<? extends Element>> getIndices() {
- return new EventIndexSequence(((IndexableGraph) this.rawGraph).getIndices().iterator(), this.graphChangedListeners);
- }
-
- public IndexableGraph getRawGraph() {
- return (IndexableGraph) this.rawGraph;
+ return new EventIndexSequence(this.rawGraph.getIndices().iterator(), this.graphChangedListeners);
}
}
View
12 ...re/src/main/java/com/tinkerpop/blueprints/pgm/util/wrappers/partition/PartitionGraph.java
@@ -5,7 +5,7 @@
import com.tinkerpop.blueprints.pgm.Graph;
import com.tinkerpop.blueprints.pgm.Vertex;
import com.tinkerpop.blueprints.pgm.impls.StringFactory;
-import com.tinkerpop.blueprints.pgm.util.wrappers.GraphWrapper;
+import com.tinkerpop.blueprints.pgm.util.wrappers.WrappingGraph;
import com.tinkerpop.blueprints.pgm.util.wrappers.partition.util.PartitionEdgeSequence;
import com.tinkerpop.blueprints.pgm.util.wrappers.partition.util.PartitionVertexSequence;
@@ -16,21 +16,21 @@
/**
* @author Marko A. Rodriguez (http://markorodriguez.com)
*/
-public class PartitionGraph implements GraphWrapper {
+public class PartitionGraph<T extends Graph> implements Graph, WrappingGraph<T> {
- protected Graph rawGraph;
+ protected T rawGraph;
private String writePartition;
private Set<String> readPartitions = new HashSet<String>();
private String partitionKey;
- public PartitionGraph(final Graph rawGraph, final String partitionKey, final String writePartition, final Set<String> readPartitions) {
+ public PartitionGraph(final T rawGraph, final String partitionKey, final String writePartition, final Set<String> readPartitions) {
this.rawGraph = rawGraph;
this.partitionKey = partitionKey;
this.writePartition = writePartition;
this.readPartitions.addAll(readPartitions);
}
- public PartitionGraph(final Graph rawGraph, final String partitionKey, final String readWriteGraph) {
+ public PartitionGraph(final T rawGraph, final String partitionKey, final String readWriteGraph) {
this(rawGraph, partitionKey, readWriteGraph, new HashSet<String>(Arrays.asList(readWriteGraph)));
}
@@ -128,7 +128,7 @@ public void removeVertex(final Vertex vertex) {
}
@Override
- public Graph getRawGraph() {
+ public T getRawGraph() {
return this.rawGraph;
}
View
17 ...in/java/com/tinkerpop/blueprints/pgm/util/wrappers/partition/PartitionIndexableGraph.java
@@ -5,33 +5,34 @@
import com.tinkerpop.blueprints.pgm.Index;
import com.tinkerpop.blueprints.pgm.IndexableGraph;
import com.tinkerpop.blueprints.pgm.impls.Parameter;
+import com.tinkerpop.blueprints.pgm.util.wrappers.WrappingGraph;
import com.tinkerpop.blueprints.pgm.util.wrappers.partition.util.PartitionIndexSequence;
import java.util.Set;
/**
* @author Marko A. Rodriguez (http://markorodriguez.com)
*/
-public class PartitionIndexableGraph extends PartitionGraph implements IndexableGraph {
+public class PartitionIndexableGraph<T extends IndexableGraph> extends PartitionGraph<T> implements IndexableGraph, WrappingGraph<T> {
- public PartitionIndexableGraph(final IndexableGraph rawIndexableGraph, final String writeGraphKey, final String writeGraph, final Set<String> readGraphs) {
+ public PartitionIndexableGraph(final T rawIndexableGraph, final String writeGraphKey, final String writeGraph, final Set<String> readGraphs) {
super(rawIndexableGraph, writeGraphKey, writeGraph, readGraphs);
}
- public PartitionIndexableGraph(final IndexableGraph rawIndexableGraph, final String writeGraphKey, final String readWriteGraph) {
+ public PartitionIndexableGraph(final T rawIndexableGraph, final String writeGraphKey, final String readWriteGraph) {
super(rawIndexableGraph, writeGraphKey, readWriteGraph);
}
public void dropIndex(final String indexName) {
- ((IndexableGraph) rawGraph).dropIndex(indexName);
+ rawGraph.dropIndex(indexName);
}
public Iterable<Index<? extends Element>> getIndices() {
- return new PartitionIndexSequence(((IndexableGraph) rawGraph).getIndices().iterator(), this);
+ return new PartitionIndexSequence(rawGraph.getIndices().iterator(), this);
}
public <T extends Element> Index<T> getIndex(final String indexName, final Class<T> indexClass) {
- final Index<T> index = ((IndexableGraph) rawGraph).getIndex(indexName, indexClass);
+ final Index<T> index = rawGraph.getIndex(indexName, indexClass);
if (null == index)
return null;
else {
@@ -44,11 +45,11 @@ public void dropIndex(final String indexName) {
}
public <T extends Element> Index<T> createManualIndex(final String indexName, final Class<T> indexClass, final Parameter... indexParameters) {
- return new PartitionIndex<T>(((IndexableGraph) rawGraph).createManualIndex(indexName, indexClass, indexParameters), this);
+ return new PartitionIndex<T>(rawGraph.createManualIndex(indexName, indexClass, indexParameters), this);
}
public <T extends Element> AutomaticIndex<T> createAutomaticIndex(final String indexName, final Class<T> indexClass, final Set<String> autoIndexKeys, final Parameter... indexParameters) {
- return new PartitionAutomaticIndex<T>(((IndexableGraph) rawGraph).createAutomaticIndex(indexName, indexClass, autoIndexKeys, indexParameters), this);
+ return new PartitionAutomaticIndex<T>(rawGraph.createAutomaticIndex(indexName, indexClass, autoIndexKeys, indexParameters), this);
}
View
10 ...core/src/main/java/com/tinkerpop/blueprints/pgm/util/wrappers/readonly/ReadOnlyGraph.java
@@ -4,7 +4,7 @@
import com.tinkerpop.blueprints.pgm.Graph;
import com.tinkerpop.blueprints.pgm.Vertex;
import com.tinkerpop.blueprints.pgm.impls.StringFactory;
-import com.tinkerpop.blueprints.pgm.util.wrappers.GraphWrapper;
+import com.tinkerpop.blueprints.pgm.util.wrappers.WrappingGraph;
import com.tinkerpop.blueprints.pgm.util.wrappers.readonly.util.ReadOnlyEdgeSequence;
import com.tinkerpop.blueprints.pgm.util.wrappers.readonly.util.ReadOnlyVertexSequence;
@@ -14,11 +14,11 @@
*
* @author Marko A. Rodriguez (http://markorodriguez.com)
*/
-public class ReadOnlyGraph implements GraphWrapper {
+public class ReadOnlyGraph<T extends Graph> implements Graph, WrappingGraph<T> {
- protected final Graph rawGraph;
+ protected final T rawGraph;
- public ReadOnlyGraph(final Graph rawGraph) {
+ public ReadOnlyGraph(final T