You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tinkerpop.apache.org by sp...@apache.org on 2018/08/28 18:08:46 UTC

[1/4] tinkerpop git commit: TINKERPOP-2006 - Fix for valid GraphML export when graph properties of a vertex and edge have similar name

Repository: tinkerpop
Updated Branches:
  refs/heads/master c9fcc139f -> 31deab213


TINKERPOP-2006 - Fix for valid GraphML export when graph properties of a vertex and edge have similar name


Project: http://git-wip-us.apache.org/repos/asf/tinkerpop/repo
Commit: http://git-wip-us.apache.org/repos/asf/tinkerpop/commit/cfc7ecef
Tree: http://git-wip-us.apache.org/repos/asf/tinkerpop/tree/cfc7ecef
Diff: http://git-wip-us.apache.org/repos/asf/tinkerpop/diff/cfc7ecef

Branch: refs/heads/master
Commit: cfc7ecefd370ac438afea03f1e6d718f1eff0fbe
Parents: 3852c87
Author: Svante Schubert <Sv...@gmail.com>
Authored: Sun Jul 15 20:26:10 2018 +0200
Committer: Svante Schubert <Sv...@gmail.com>
Committed: Sun Jul 15 20:26:10 2018 +0200

----------------------------------------------------------------------
 .../structure/io/graphml/GraphMLTokens.java     |  2 +
 .../structure/io/graphml/GraphMLWriter.java     | 77 +++++++++++++-------
 2 files changed, 54 insertions(+), 25 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/cfc7ecef/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/graphml/GraphMLTokens.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/graphml/GraphMLTokens.java b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/graphml/GraphMLTokens.java
index 921a10f..588e464 100644
--- a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/graphml/GraphMLTokens.java
+++ b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/graphml/GraphMLTokens.java
@@ -55,4 +55,6 @@ final class GraphMLTokens {
     public static final String LONG = "long";
     public static final String BOOLEAN = "boolean";
     public static final String INT = "int";
+    public static final String VERTEX_SUFFIX = "V";
+    public static final String EDGE_SUFFIX = "E";
 }

http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/cfc7ecef/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/graphml/GraphMLWriter.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/graphml/GraphMLWriter.java b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/graphml/GraphMLWriter.java
index e2a0952..26c5760 100644
--- a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/graphml/GraphMLWriter.java
+++ b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/graphml/GraphMLWriter.java
@@ -18,6 +18,21 @@
  */
 package org.apache.tinkerpop.gremlin.structure.io.graphml;
 
+import java.io.IOException;
+import java.io.OutputStream;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+import javax.xml.XMLConstants;
+import javax.xml.stream.XMLOutputFactory;
+import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.XMLStreamWriter;
+import org.apache.commons.collections.CollectionUtils;
 import org.apache.tinkerpop.gremlin.structure.Direction;
 import org.apache.tinkerpop.gremlin.structure.Edge;
 import org.apache.tinkerpop.gremlin.structure.Element;
@@ -30,21 +45,6 @@ import org.apache.tinkerpop.gremlin.structure.io.Io;
 import org.apache.tinkerpop.gremlin.structure.util.Comparators;
 import org.apache.tinkerpop.gremlin.util.iterator.IteratorUtils;
 
-import javax.xml.XMLConstants;
-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.ArrayList;
-import java.util.Collection;
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.Iterator;
-import java.util.List;
-import java.util.Map;
-import java.util.Optional;
-
 /**
  * GraphMLWriter writes a Graph to a GraphML OutputStream. Note that this format is lossy, in the sense that data
  * types and features of Gremlin Structure not supported by GraphML are not serialized.  This format is meant for
@@ -65,6 +65,7 @@ public final class GraphMLWriter implements GraphWriter {
     private final Optional<String> xmlSchemaLocation;
     private final String edgeLabelKey;
     private final String vertexLabelKey;
+    private Collection<String> intersection;
 
     private GraphMLWriter(final boolean normalize, final Map<String, String> vertexKeyTypes,
                           final Map<String, String> edgeKeyTypes, final String xmlSchemaLocation,
@@ -75,6 +76,7 @@ public final class GraphMLWriter implements GraphWriter {
         this.xmlSchemaLocation = Optional.ofNullable(xmlSchemaLocation);
         this.edgeLabelKey = edgeLabelKey;
         this.vertexLabelKey = vertexLabelKey;
+        this.intersection = null;
     }
 
     /**
@@ -218,20 +220,33 @@ public final class GraphMLWriter implements GraphWriter {
                             final Map<String, String> identifiedEdgeKeyTypes,
                             final XMLStreamWriter writer) throws XMLStreamException {
         // <key id="weight" for="edge" attr.name="weight" attr.type="float"/>
-        final Collection<String> vertexKeySet = getVertexKeysAndNormalizeIfRequired(identifiedVertexKeyTypes);
+        Collection<String> vertexKeySet = getVertexKeysAndNormalizeIfRequired(identifiedVertexKeyTypes);
+        Collection<String> edgeKeySet = getEdgeKeysAndNormalizeIfRequired(identifiedEdgeKeyTypes);
+        // in case vertex and edge may have the same attribute name, the key id in graphml have to be different
+        intersection = CollectionUtils.intersection(vertexKeySet, edgeKeySet);
+        // speeding-up later checks
+        if(intersection.isEmpty()){
+            intersection = null;
+        }
         for (String key : vertexKeySet) {
             writer.writeStartElement(GraphMLTokens.KEY);
-            writer.writeAttribute(GraphMLTokens.ID, key);
+            if(intersection != null && intersection.contains(key)){
+                writer.writeAttribute(GraphMLTokens.ID, key.concat(GraphMLTokens.VERTEX_SUFFIX));
+            }else{
+                writer.writeAttribute(GraphMLTokens.ID, key);
+            }
             writer.writeAttribute(GraphMLTokens.FOR, GraphMLTokens.NODE);
             writer.writeAttribute(GraphMLTokens.ATTR_NAME, key);
             writer.writeAttribute(GraphMLTokens.ATTR_TYPE, identifiedVertexKeyTypes.get(key));
             writer.writeEndElement();
         }
-
-        final Collection<String> edgeKeySet = getEdgeKeysAndNormalizeIfRequired(identifiedEdgeKeyTypes);
         for (String key : edgeKeySet) {
             writer.writeStartElement(GraphMLTokens.KEY);
-            writer.writeAttribute(GraphMLTokens.ID, key);
+            if(intersection != null && intersection.contains(key)){
+                writer.writeAttribute(GraphMLTokens.ID, key.concat(GraphMLTokens.EDGE_SUFFIX));
+            }else{
+                writer.writeAttribute(GraphMLTokens.ID, key);
+            }
             writer.writeAttribute(GraphMLTokens.FOR, GraphMLTokens.EDGE);
             writer.writeAttribute(GraphMLTokens.ATTR_NAME, key);
             writer.writeAttribute(GraphMLTokens.ATTR_TYPE, identifiedEdgeKeyTypes.get(key));
@@ -260,7 +275,11 @@ public final class GraphMLWriter implements GraphWriter {
 
                 for (String key : keys) {
                     writer.writeStartElement(GraphMLTokens.DATA);
-                    writer.writeAttribute(GraphMLTokens.KEY, key);
+                    if(intersection != null && intersection.contains(key)){
+                        writer.writeAttribute(GraphMLTokens.KEY, key + GraphMLTokens.EDGE_SUFFIX);
+                    }else{
+                        writer.writeAttribute(GraphMLTokens.KEY, key);
+                    }
                     // technically there can't be a null here as gremlin structure forbids that occurrence even if Graph
                     // implementations support it, but out to empty string just in case.
                     writer.writeCharacters(edge.property(key).orElse("").toString());
@@ -284,7 +303,11 @@ public final class GraphMLWriter implements GraphWriter {
 
                 for (String key : edge.keys()) {
                     writer.writeStartElement(GraphMLTokens.DATA);
-                    writer.writeAttribute(GraphMLTokens.KEY, key);
+                    if(intersection != null && intersection.contains(key)){
+                        writer.writeAttribute(GraphMLTokens.KEY, key + GraphMLTokens.EDGE_SUFFIX);
+                    }else{
+                        writer.writeAttribute(GraphMLTokens.KEY, key);
+                    }
                     // technically there can't be a null here as gremlin structure forbids that occurrence even if Graph
                     // implementations support it, but out to empty string just in case.
                     writer.writeCharacters(edge.property(key).orElse("").toString());
@@ -309,7 +332,11 @@ public final class GraphMLWriter implements GraphWriter {
 
             for (String key : keys) {
                 writer.writeStartElement(GraphMLTokens.DATA);
-                writer.writeAttribute(GraphMLTokens.KEY, key);
+                if(intersection != null && intersection.contains(key)){
+                    writer.writeAttribute(GraphMLTokens.KEY, key.concat(GraphMLTokens.VERTEX_SUFFIX));
+                }else{
+                    writer.writeAttribute(GraphMLTokens.KEY, key);
+                }
                 // technically there can't be a null here as gremlin structure forbids that occurrence even if Graph
                 // implementations support it, but out to empty string just in case.
                 writer.writeCharacters(vertex.property(key).orElse("").toString());
@@ -356,7 +383,7 @@ public final class GraphMLWriter implements GraphWriter {
             edgeKeySet = identifiedEdgeKeyTypes.keySet();
 
         return edgeKeySet;
-    }
+                     }
 
     private Collection<String> getVertexKeysAndNormalizeIfRequired(final Map<String, String> identifiedVertexKeyTypes) {
         final Collection<String> keyset;
@@ -368,7 +395,7 @@ public final class GraphMLWriter implements GraphWriter {
             keyset = identifiedVertexKeyTypes.keySet();
 
         return keyset;
-    }
+        }
 
     private void writeXmlNsAndSchema(final XMLStreamWriter writer) throws XMLStreamException {
         writer.writeAttribute(GraphMLTokens.XMLNS, GraphMLTokens.GRAPHML_XMLNS);


[3/4] tinkerpop git commit: Merge branch 'pr-905'

Posted by sp...@apache.org.
Merge branch 'pr-905'


Project: http://git-wip-us.apache.org/repos/asf/tinkerpop/repo
Commit: http://git-wip-us.apache.org/repos/asf/tinkerpop/commit/f7056236
Tree: http://git-wip-us.apache.org/repos/asf/tinkerpop/tree/f7056236
Diff: http://git-wip-us.apache.org/repos/asf/tinkerpop/diff/f7056236

Branch: refs/heads/master
Commit: f70562363899989af25f69c5cc9bc244f9b8fe6d
Parents: c9fcc13 41f2ab8
Author: Stephen Mallette <sp...@genoprime.com>
Authored: Tue Aug 28 12:38:57 2018 -0400
Committer: Stephen Mallette <sp...@genoprime.com>
Committed: Tue Aug 28 12:38:57 2018 -0400

----------------------------------------------------------------------
 .../structure/io/graphml/GraphMLTokens.java     |  2 +
 .../structure/io/graphml/GraphMLWriter.java     | 85 +++++++++++++-------
 .../tinkerpop/gremlin/structure/io/IoTest.java  |  8 +-
 .../io/graphml/tinkerpop-classic-normalized.xml |  4 +
 4 files changed, 69 insertions(+), 30 deletions(-)
----------------------------------------------------------------------



[4/4] tinkerpop git commit: TINKERPOP-2006 Minor refactoring following merge of #905

Posted by sp...@apache.org.
TINKERPOP-2006 Minor refactoring following merge of #905

Added a new test for duplicate edge/vertex property keys and reverted the normalized classic graph data set


Project: http://git-wip-us.apache.org/repos/asf/tinkerpop/repo
Commit: http://git-wip-us.apache.org/repos/asf/tinkerpop/commit/31deab21
Tree: http://git-wip-us.apache.org/repos/asf/tinkerpop/tree/31deab21
Diff: http://git-wip-us.apache.org/repos/asf/tinkerpop/diff/31deab21

Branch: refs/heads/master
Commit: 31deab2133c8ec02ff9e2f62cf2ec47ce841b289
Parents: f705623
Author: Stephen Mallette <sp...@genoprime.com>
Authored: Tue Aug 28 13:42:25 2018 -0400
Committer: Stephen Mallette <sp...@genoprime.com>
Committed: Tue Aug 28 13:42:25 2018 -0400

----------------------------------------------------------------------
 CHANGELOG.asciidoc                              |   1 +
 .../structure/io/graphml/GraphMLWriter.java     |  13 +-
 .../tinkerpop/gremlin/structure/io/IoTest.java  | 121 +++++++++++++++++--
 .../io/graphml/tinkerpop-classic-normalized.xml |   4 -
 4 files changed, 120 insertions(+), 19 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/31deab21/CHANGELOG.asciidoc
----------------------------------------------------------------------
diff --git a/CHANGELOG.asciidoc b/CHANGELOG.asciidoc
index cbb4b21..9e9d4f3 100644
--- a/CHANGELOG.asciidoc
+++ b/CHANGELOG.asciidoc
@@ -30,6 +30,7 @@ This release also includes changes from <<release-3-3-3, 3.3.3>>.
 * Added the `io()` start step and `read()` and `write()` termination steps to the Gremlin language.
 * Added `GraphFeatures.supportsIoRead()` and `GraphFeatures.supportsIoWrite()`.
 * Deprecated `Graph.io()` and related infrastructure.
+* `GraphMLReader` better handles edge and vertex properties with the same name.
 * Bumped to Netty 4.1.25.
 * Bumped to Spark 2.3.1.
 * Deprecated two `submit()`-related methods on the Java driver `Client` class.

http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/31deab21/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/graphml/GraphMLWriter.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/graphml/GraphMLWriter.java b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/graphml/GraphMLWriter.java
index 53c5c12..cc66d37 100644
--- a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/graphml/GraphMLWriter.java
+++ b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/graphml/GraphMLWriter.java
@@ -24,6 +24,7 @@ import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.HashMap;
+import java.util.HashSet;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
@@ -65,7 +66,7 @@ public final class GraphMLWriter implements GraphWriter {
     private final Optional<String> xmlSchemaLocation;
     private final String edgeLabelKey;
     private final String vertexLabelKey;
-    private Collection<String> intersection;
+    private Collection<String> intersection = Collections.emptySet();
 
     private GraphMLWriter(final boolean normalize, final Map<String, String> vertexKeyTypes,
             final Map<String, String> edgeKeyTypes, final String xmlSchemaLocation,
@@ -76,7 +77,6 @@ public final class GraphMLWriter implements GraphWriter {
         this.xmlSchemaLocation = Optional.ofNullable(xmlSchemaLocation);
         this.edgeLabelKey = edgeLabelKey;
         this.vertexLabelKey = vertexLabelKey;
-        this.intersection = null;
     }
 
     /**
@@ -224,13 +224,10 @@ public final class GraphMLWriter implements GraphWriter {
         final Collection<String> edgeKeySet = getEdgeKeysAndNormalizeIfRequired(identifiedEdgeKeyTypes);
         // in case vertex and edge may have the same attribute name, the key id in graphml have to be different
         intersection = CollectionUtils.intersection(vertexKeySet, edgeKeySet);
-        // speeding-up later checks
-        if (intersection.isEmpty()) {
-            intersection = null;
-        }
+
         for (String key : vertexKeySet) {
             writer.writeStartElement(GraphMLTokens.KEY);
-            if (intersection != null && intersection.contains(key)) {
+            if (intersection.contains(key)) {
                 writer.writeAttribute(GraphMLTokens.ID, key.concat(GraphMLTokens.VERTEX_SUFFIX));
             } else {
                 writer.writeAttribute(GraphMLTokens.ID, key);
@@ -242,7 +239,7 @@ public final class GraphMLWriter implements GraphWriter {
         }
         for (String key : edgeKeySet) {
             writer.writeStartElement(GraphMLTokens.KEY);
-            if (intersection != null && intersection.contains(key)) {
+            if (intersection.contains(key)) {
                 writer.writeAttribute(GraphMLTokens.ID, key.concat(GraphMLTokens.EDGE_SUFFIX));
             } else {
                 writer.writeAttribute(GraphMLTokens.ID, key);

http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/31deab21/gremlin-test/src/main/java/org/apache/tinkerpop/gremlin/structure/io/IoTest.java
----------------------------------------------------------------------
diff --git a/gremlin-test/src/main/java/org/apache/tinkerpop/gremlin/structure/io/IoTest.java b/gremlin-test/src/main/java/org/apache/tinkerpop/gremlin/structure/io/IoTest.java
index 729ede7..174de76 100644
--- a/gremlin-test/src/main/java/org/apache/tinkerpop/gremlin/structure/io/IoTest.java
+++ b/gremlin-test/src/main/java/org/apache/tinkerpop/gremlin/structure/io/IoTest.java
@@ -98,7 +98,6 @@ import static org.junit.Assert.assertTrue;
 import static org.junit.Assert.fail;
 import static org.junit.Assume.assumeThat;
 
-
 /**
  * @author Joshua Shinavier (http://fortytwo.net)
  * @author Stephen Mallette (http://stephen.genoprime.com)
@@ -108,7 +107,88 @@ import static org.junit.Assume.assumeThat;
 public class IoTest {
     private static final Logger logger = LoggerFactory.getLogger(IoTest.class);
 
+    private static final String CLASSIC_GRAPH_WITH_COLOR = "<?xml version=\"1.0\" ?>\n" +
+            "<graphml xmlns=\"http://graphml.graphdrawing.org/xmlns\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://graphml.graphdrawing.org/xmlns http://graphml.graphdrawing.org/xmlns/1.1/graphml.xsd\">\n" +
+            "    <key id=\"age\" for=\"node\" attr.name=\"age\" attr.type=\"int\"></key>\n" +
+            "    <key id=\"colorV\" for=\"node\" attr.name=\"color\" attr.type=\"string\"></key>\n" +
+            "    <key id=\"labelV\" for=\"node\" attr.name=\"labelV\" attr.type=\"string\"></key>\n" +
+            "    <key id=\"lang\" for=\"node\" attr.name=\"lang\" attr.type=\"string\"></key>\n" +
+            "    <key id=\"name\" for=\"node\" attr.name=\"name\" attr.type=\"string\"></key>\n" +
+            "    <key id=\"colorE\" for=\"edge\" attr.name=\"color\" attr.type=\"string\"></key>\n" +
+            "    <key id=\"labelE\" for=\"edge\" attr.name=\"labelE\" attr.type=\"string\"></key>\n" +
+            "    <key id=\"weight\" for=\"edge\" attr.name=\"weight\" attr.type=\"float\"></key>\n" +
+            "    <graph id=\"G\" edgedefault=\"directed\">\n" +
+            "        <node id=\"1\">\n" +
+            "            <data key=\"labelV\">vertex</data>\n" +
+            "            <data key=\"age\">29</data>\n" +
+            "            <data key=\"colorV\">#6495ed</data>\n" +
+            "            <data key=\"name\">marko</data>\n" +
+            "        </node>\n" +
+            "        <node id=\"2\">\n" +
+            "            <data key=\"labelV\">vertex</data>\n" +
+            "            <data key=\"age\">27</data>\n" +
+            "            <data key=\"colorV\">#6495ed</data>\n" +
+            "            <data key=\"name\">vadas</data>\n" +
+            "        </node>\n" +
+            "        <node id=\"3\">\n" +
+            "            <data key=\"labelV\">vertex</data>\n" +
+            "            <data key=\"lang\">java</data>\n" +
+            "            <data key=\"colorV\">#6495ed</data>\n" +
+            "            <data key=\"name\">lop</data>\n" +
+            "        </node>\n" +
+            "        <node id=\"4\">\n" +
+            "            <data key=\"labelV\">vertex</data>\n" +
+            "            <data key=\"age\">32</data>\n" +
+            "            <data key=\"colorV\">#6495ed</data>\n" +
+            "            <data key=\"name\">josh</data>\n" +
+            "        </node>\n" +
+            "        <node id=\"5\">\n" +
+            "            <data key=\"labelV\">vertex</data>\n" +
+            "            <data key=\"lang\">java</data>\n" +
+            "            <data key=\"colorV\">#6495ed</data>\n" +
+            "            <data key=\"name\">ripple</data>\n" +
+            "        </node>\n" +
+            "        <node id=\"6\">\n" +
+            "            <data key=\"labelV\">vertex</data>\n" +
+            "            <data key=\"age\">35</data>\n" +
+            "            <data key=\"colorV\">#6495ed</data>\n" +
+            "            <data key=\"name\">peter</data>\n" +
+            "        </node>\n" +
+            "        <edge id=\"10\" source=\"4\" target=\"5\">\n" +
+            "            <data key=\"labelE\">created</data>\n" +
+            "            <data key=\"colorE\">#ee0000</data>\n" +
+            "            <data key=\"weight\">1.0</data>\n" +
+            "        </edge>\n" +
+            "        <edge id=\"11\" source=\"4\" target=\"3\">\n" +
+            "            <data key=\"labelE\">created</data>\n" +
+            "            <data key=\"colorE\">#ee0000</data>\n" +
+            "            <data key=\"weight\">0.4</data>\n" +
+            "        </edge>\n" +
+            "        <edge id=\"12\" source=\"6\" target=\"3\">\n" +
+            "            <data key=\"labelE\">created</data>\n" +
+            "            <data key=\"colorE\">#ee0000</data>\n" +
+            "            <data key=\"weight\">0.2</data>\n" +
+            "        </edge>\n" +
+            "        <edge id=\"7\" source=\"1\" target=\"2\">\n" +
+            "            <data key=\"labelE\">knows</data>\n" +
+            "            <data key=\"colorE\">#ee0000</data>\n" +
+            "            <data key=\"weight\">0.5</data>\n" +
+            "        </edge>\n" +
+            "        <edge id=\"8\" source=\"1\" target=\"4\">\n" +
+            "            <data key=\"labelE\">knows</data>\n" +
+            "            <data key=\"colorE\">#ee0000</data>\n" +
+            "            <data key=\"weight\">1.0</data>\n" +
+            "        </edge>\n" +
+            "        <edge id=\"9\" source=\"1\" target=\"3\">\n" +
+            "            <data key=\"labelE\">created</data>\n" +
+            "            <data key=\"colorE\">#ee0000</data>\n" +
+            "            <data key=\"weight\">0.4</data>\n" +
+            "        </edge>\n" +
+            "    </graph>\n" +
+            "</graphml>";
+
     public static class GraphMLTest extends AbstractGremlinTest {
+
         @Test
         @FeatureRequirement(featureClass = Graph.Features.EdgeFeatures.class, feature = Graph.Features.EdgeFeatures.FEATURE_ADD_EDGES)
         @FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.VertexFeatures.FEATURE_ADD_VERTICES)
@@ -206,6 +286,39 @@ public class IoTest {
             assertEquals(2, IteratorUtils.count(graph.vertices()));
         }
 
+        @Test
+        @FeatureRequirement(featureClass = Graph.Features.EdgeFeatures.class, feature = Graph.Features.EdgeFeatures.FEATURE_ADD_EDGES)
+        @FeatureRequirement(featureClass = Graph.Features.EdgeFeatures.class, feature = Graph.Features.EdgeFeatures.FEATURE_REMOVE_EDGES)
+        @FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.VertexFeatures.FEATURE_ADD_VERTICES)
+        @FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.VertexFeatures.FEATURE_REMOVE_VERTICES)
+        @FeatureRequirement(featureClass = VertexPropertyFeatures.class, feature = FEATURE_STRING_VALUES)
+        @FeatureRequirement(featureClass = VertexPropertyFeatures.class, feature = FEATURE_INTEGER_VALUES)
+        @FeatureRequirement(featureClass = EdgePropertyFeatures.class, feature = EdgePropertyFeatures.FEATURE_FLOAT_VALUES)
+        @FeatureRequirement(featureClass = EdgePropertyFeatures.class, feature = EdgePropertyFeatures.FEATURE_STRING_VALUES)
+        public void shouldReadGraphMLWithCommonVertexAndEdgePropertyNames() throws IOException {
+            final GraphReader reader = GraphMLReader.build().create();
+            try (final InputStream stream = new ByteArrayInputStream(CLASSIC_GRAPH_WITH_COLOR.getBytes("UTF-8"))) {
+                reader.readGraph(stream, graph);
+            }
+
+            // there is also a "color" property on this dataset that is on both edges and vertices
+            graph.vertices().forEachRemaining(v -> assertEquals("#6495ed", v.value("color")));
+            graph.edges().forEachRemaining(e -> assertEquals("#ee0000", e.value("color")));
+
+            final GraphWriter writer = GraphMLWriter.build().create();
+            try (final OutputStream out = new ByteArrayOutputStream()) {
+                writer.writeGraph(out, graph);
+
+                graph.vertices().forEachRemaining(Element::remove);
+                try (final InputStream stream = new ByteArrayInputStream(((ByteArrayOutputStream) out).toByteArray())) {
+                    reader.readGraph(stream, graph);
+                }
+
+                // there is also a "color" property on this dataset that is on both edges and vertices
+                graph.vertices().forEachRemaining(v -> assertEquals("#6495ed", v.value("color")));
+                graph.edges().forEachRemaining(e -> assertEquals("#ee0000", e.value("color")));
+            }
+        }
 
         @Test(expected = NumberFormatException.class)
         @FeatureRequirement(featureClass = Graph.Features.EdgeFeatures.class, feature = Graph.Features.EdgeFeatures.FEATURE_ADD_EDGES)
@@ -239,12 +352,6 @@ public class IoTest {
         public void shouldWriteNormalizedGraphML() throws Exception {
             try (ByteArrayOutputStream bos = new ByteArrayOutputStream()) {
                 final GraphMLWriter w = GraphMLWriter.build().normalize(true).create();
-                final Vertex v = graph.vertices().next();
-                // adding 'color' property to vertex
-                v.property(VertexProperty.Cardinality.single, "color", "#6495ed");
-                final Edge e = graph.edges().next();
-                // adding 'color' property to edge
-                e.property("color", "#ee0000");
                 w.writeGraph(bos, graph);
                 final String expected = streamToString(IoTest.class.getResourceAsStream(TestHelper.convertPackageToResourcePath(GraphMLResourceAccess.class) + "tinkerpop-classic-normalized.xml"));
                 assertEquals(expected.replace("\n", "").replace("\r", ""), bos.toString().replace("\n", "").replace("\r", ""));

http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/31deab21/gremlin-test/src/main/resources/org/apache/tinkerpop/gremlin/structure/io/graphml/tinkerpop-classic-normalized.xml
----------------------------------------------------------------------
diff --git a/gremlin-test/src/main/resources/org/apache/tinkerpop/gremlin/structure/io/graphml/tinkerpop-classic-normalized.xml b/gremlin-test/src/main/resources/org/apache/tinkerpop/gremlin/structure/io/graphml/tinkerpop-classic-normalized.xml
index 45d63c1..19896d9 100644
--- a/gremlin-test/src/main/resources/org/apache/tinkerpop/gremlin/structure/io/graphml/tinkerpop-classic-normalized.xml
+++ b/gremlin-test/src/main/resources/org/apache/tinkerpop/gremlin/structure/io/graphml/tinkerpop-classic-normalized.xml
@@ -1,18 +1,15 @@
 <?xml version="1.0" ?>
 <graphml xmlns="http://graphml.graphdrawing.org/xmlns" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://graphml.graphdrawing.org/xmlns http://graphml.graphdrawing.org/xmlns/1.1/graphml.xsd">
     <key id="age" for="node" attr.name="age" attr.type="int"></key>
-    <key id="colorV" for="node" attr.name="color" attr.type="string"></key>
     <key id="labelV" for="node" attr.name="labelV" attr.type="string"></key>
     <key id="lang" for="node" attr.name="lang" attr.type="string"></key>
     <key id="name" for="node" attr.name="name" attr.type="string"></key>
-    <key id="colorE" for="edge" attr.name="color" attr.type="string"></key>
     <key id="labelE" for="edge" attr.name="labelE" attr.type="string"></key>
     <key id="weight" for="edge" attr.name="weight" attr.type="float"></key>
     <graph id="G" edgedefault="directed">
         <node id="1">
             <data key="labelV">vertex</data>
             <data key="age">29</data>
-            <data key="colorV">#6495ed</data>
             <data key="name">marko</data>
         </node>
         <node id="2">
@@ -54,7 +51,6 @@
         </edge>
         <edge id="7" source="1" target="2">
             <data key="labelE">knows</data>
-            <data key="colorE">#ee0000</data>
             <data key="weight">0.5</data>
         </edge>
         <edge id="8" source="1" target="4">


[2/4] tinkerpop git commit: TINKERPOP-2006 Fixing GraphML serialization with similar named property for edge and vertex

Posted by sp...@apache.org.
TINKERPOP-2006 Fixing GraphML serialization with similar named property for edge and vertex


Project: http://git-wip-us.apache.org/repos/asf/tinkerpop/repo
Commit: http://git-wip-us.apache.org/repos/asf/tinkerpop/commit/41f2ab8a
Tree: http://git-wip-us.apache.org/repos/asf/tinkerpop/tree/41f2ab8a
Diff: http://git-wip-us.apache.org/repos/asf/tinkerpop/diff/41f2ab8a

Branch: refs/heads/master
Commit: 41f2ab8a5cd6bdb60a195489fce38ef38baaf232
Parents: cfc7ece
Author: Svante Schubert <Sv...@gmail.com>
Authored: Thu Aug 2 15:33:04 2018 +0200
Committer: Svante Schubert <Sv...@gmail.com>
Committed: Thu Aug 2 15:33:04 2018 +0200

----------------------------------------------------------------------
 .../structure/io/graphml/GraphMLWriter.java     | 44 ++++++++++----------
 .../tinkerpop/gremlin/structure/io/IoTest.java  |  8 +++-
 .../io/graphml/tinkerpop-classic-normalized.xml |  4 ++
 3 files changed, 33 insertions(+), 23 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/41f2ab8a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/graphml/GraphMLWriter.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/graphml/GraphMLWriter.java b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/graphml/GraphMLWriter.java
index 26c5760..53c5c12 100644
--- a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/graphml/GraphMLWriter.java
+++ b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/graphml/GraphMLWriter.java
@@ -68,8 +68,8 @@ public final class GraphMLWriter implements GraphWriter {
     private Collection<String> intersection;
 
     private GraphMLWriter(final boolean normalize, final Map<String, String> vertexKeyTypes,
-                          final Map<String, String> edgeKeyTypes, final String xmlSchemaLocation,
-                          final String edgeLabelKey, final String vertexLabelKey) {
+            final Map<String, String> edgeKeyTypes, final String xmlSchemaLocation,
+            final String edgeLabelKey, final String vertexLabelKey) {
         this.normalize = normalize;
         this.vertexKeyTypes = Optional.ofNullable(vertexKeyTypes);
         this.edgeKeyTypes = Optional.ofNullable(edgeKeyTypes);
@@ -214,25 +214,25 @@ public final class GraphMLWriter implements GraphWriter {
             return writer;
         } else
             return utf8Writer;
-    }
+        }
 
     private void writeTypes(final Map<String, String> identifiedVertexKeyTypes,
-                            final Map<String, String> identifiedEdgeKeyTypes,
-                            final XMLStreamWriter writer) throws XMLStreamException {
+            final Map<String, String> identifiedEdgeKeyTypes,
+            final XMLStreamWriter writer) throws XMLStreamException {
         // <key id="weight" for="edge" attr.name="weight" attr.type="float"/>
-        Collection<String> vertexKeySet = getVertexKeysAndNormalizeIfRequired(identifiedVertexKeyTypes);
-        Collection<String> edgeKeySet = getEdgeKeysAndNormalizeIfRequired(identifiedEdgeKeyTypes);
+        final Collection<String> vertexKeySet = getVertexKeysAndNormalizeIfRequired(identifiedVertexKeyTypes);
+        final Collection<String> edgeKeySet = getEdgeKeysAndNormalizeIfRequired(identifiedEdgeKeyTypes);
         // in case vertex and edge may have the same attribute name, the key id in graphml have to be different
         intersection = CollectionUtils.intersection(vertexKeySet, edgeKeySet);
         // speeding-up later checks
-        if(intersection.isEmpty()){
+        if (intersection.isEmpty()) {
             intersection = null;
         }
         for (String key : vertexKeySet) {
             writer.writeStartElement(GraphMLTokens.KEY);
-            if(intersection != null && intersection.contains(key)){
+            if (intersection != null && intersection.contains(key)) {
                 writer.writeAttribute(GraphMLTokens.ID, key.concat(GraphMLTokens.VERTEX_SUFFIX));
-            }else{
+            } else {
                 writer.writeAttribute(GraphMLTokens.ID, key);
             }
             writer.writeAttribute(GraphMLTokens.FOR, GraphMLTokens.NODE);
@@ -242,9 +242,9 @@ public final class GraphMLWriter implements GraphWriter {
         }
         for (String key : edgeKeySet) {
             writer.writeStartElement(GraphMLTokens.KEY);
-            if(intersection != null && intersection.contains(key)){
+            if (intersection != null && intersection.contains(key)) {
                 writer.writeAttribute(GraphMLTokens.ID, key.concat(GraphMLTokens.EDGE_SUFFIX));
-            }else{
+            } else {
                 writer.writeAttribute(GraphMLTokens.ID, key);
             }
             writer.writeAttribute(GraphMLTokens.FOR, GraphMLTokens.EDGE);
@@ -275,9 +275,9 @@ public final class GraphMLWriter implements GraphWriter {
 
                 for (String key : keys) {
                     writer.writeStartElement(GraphMLTokens.DATA);
-                    if(intersection != null && intersection.contains(key)){
+                    if (intersection != null && intersection.contains(key)) {
                         writer.writeAttribute(GraphMLTokens.KEY, key + GraphMLTokens.EDGE_SUFFIX);
-                    }else{
+                    } else {
                         writer.writeAttribute(GraphMLTokens.KEY, key);
                     }
                     // technically there can't be a null here as gremlin structure forbids that occurrence even if Graph
@@ -303,9 +303,9 @@ public final class GraphMLWriter implements GraphWriter {
 
                 for (String key : edge.keys()) {
                     writer.writeStartElement(GraphMLTokens.DATA);
-                    if(intersection != null && intersection.contains(key)){
+                    if (intersection != null && intersection.contains(key)) {
                         writer.writeAttribute(GraphMLTokens.KEY, key + GraphMLTokens.EDGE_SUFFIX);
-                    }else{
+                    } else {
                         writer.writeAttribute(GraphMLTokens.KEY, key);
                     }
                     // technically there can't be a null here as gremlin structure forbids that occurrence even if Graph
@@ -332,9 +332,9 @@ public final class GraphMLWriter implements GraphWriter {
 
             for (String key : keys) {
                 writer.writeStartElement(GraphMLTokens.DATA);
-                if(intersection != null && intersection.contains(key)){
+                if (intersection != null && intersection.contains(key)) {
                     writer.writeAttribute(GraphMLTokens.KEY, key.concat(GraphMLTokens.VERTEX_SUFFIX));
-                }else{
+                } else {
                     writer.writeAttribute(GraphMLTokens.KEY, key);
                 }
                 // technically there can't be a null here as gremlin structure forbids that occurrence even if Graph
@@ -383,7 +383,7 @@ public final class GraphMLWriter implements GraphWriter {
             edgeKeySet = identifiedEdgeKeyTypes.keySet();
 
         return edgeKeySet;
-                     }
+    }
 
     private Collection<String> getVertexKeysAndNormalizeIfRequired(final Map<String, String> identifiedVertexKeyTypes) {
         final Collection<String> keyset;
@@ -395,7 +395,7 @@ public final class GraphMLWriter implements GraphWriter {
             keyset = identifiedVertexKeyTypes.keySet();
 
         return keyset;
-        }
+    }
 
     private void writeXmlNsAndSchema(final XMLStreamWriter writer) throws XMLStreamException {
         writer.writeAttribute(GraphMLTokens.XMLNS, GraphMLTokens.GRAPHML_XMLNS);
@@ -431,8 +431,8 @@ public final class GraphMLWriter implements GraphWriter {
             for (String key : edge.keys()) {
                 if (!edgeKeyTypes.containsKey(key))
                     edgeKeyTypes.put(key, GraphMLWriter.getStringType(edge.property(key).value()));
+                }
             }
-        }
 
         return edgeKeyTypes;
     }
@@ -452,7 +452,7 @@ public final class GraphMLWriter implements GraphWriter {
             return GraphMLTokens.BOOLEAN;
         else
             return GraphMLTokens.STRING;
-    }
+        }
 
     public static Builder build() {
         return new Builder();

http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/41f2ab8a/gremlin-test/src/main/java/org/apache/tinkerpop/gremlin/structure/io/IoTest.java
----------------------------------------------------------------------
diff --git a/gremlin-test/src/main/java/org/apache/tinkerpop/gremlin/structure/io/IoTest.java b/gremlin-test/src/main/java/org/apache/tinkerpop/gremlin/structure/io/IoTest.java
index 0221555..729ede7 100644
--- a/gremlin-test/src/main/java/org/apache/tinkerpop/gremlin/structure/io/IoTest.java
+++ b/gremlin-test/src/main/java/org/apache/tinkerpop/gremlin/structure/io/IoTest.java
@@ -98,6 +98,7 @@ import static org.junit.Assert.assertTrue;
 import static org.junit.Assert.fail;
 import static org.junit.Assume.assumeThat;
 
+
 /**
  * @author Joshua Shinavier (http://fortytwo.net)
  * @author Stephen Mallette (http://stephen.genoprime.com)
@@ -238,8 +239,13 @@ public class IoTest {
         public void shouldWriteNormalizedGraphML() throws Exception {
             try (ByteArrayOutputStream bos = new ByteArrayOutputStream()) {
                 final GraphMLWriter w = GraphMLWriter.build().normalize(true).create();
+                final Vertex v = graph.vertices().next();
+                // adding 'color' property to vertex
+                v.property(VertexProperty.Cardinality.single, "color", "#6495ed");
+                final Edge e = graph.edges().next();
+                // adding 'color' property to edge
+                e.property("color", "#ee0000");
                 w.writeGraph(bos, graph);
-
                 final String expected = streamToString(IoTest.class.getResourceAsStream(TestHelper.convertPackageToResourcePath(GraphMLResourceAccess.class) + "tinkerpop-classic-normalized.xml"));
                 assertEquals(expected.replace("\n", "").replace("\r", ""), bos.toString().replace("\n", "").replace("\r", ""));
             }

http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/41f2ab8a/gremlin-test/src/main/resources/org/apache/tinkerpop/gremlin/structure/io/graphml/tinkerpop-classic-normalized.xml
----------------------------------------------------------------------
diff --git a/gremlin-test/src/main/resources/org/apache/tinkerpop/gremlin/structure/io/graphml/tinkerpop-classic-normalized.xml b/gremlin-test/src/main/resources/org/apache/tinkerpop/gremlin/structure/io/graphml/tinkerpop-classic-normalized.xml
index 19896d9..45d63c1 100644
--- a/gremlin-test/src/main/resources/org/apache/tinkerpop/gremlin/structure/io/graphml/tinkerpop-classic-normalized.xml
+++ b/gremlin-test/src/main/resources/org/apache/tinkerpop/gremlin/structure/io/graphml/tinkerpop-classic-normalized.xml
@@ -1,15 +1,18 @@
 <?xml version="1.0" ?>
 <graphml xmlns="http://graphml.graphdrawing.org/xmlns" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://graphml.graphdrawing.org/xmlns http://graphml.graphdrawing.org/xmlns/1.1/graphml.xsd">
     <key id="age" for="node" attr.name="age" attr.type="int"></key>
+    <key id="colorV" for="node" attr.name="color" attr.type="string"></key>
     <key id="labelV" for="node" attr.name="labelV" attr.type="string"></key>
     <key id="lang" for="node" attr.name="lang" attr.type="string"></key>
     <key id="name" for="node" attr.name="name" attr.type="string"></key>
+    <key id="colorE" for="edge" attr.name="color" attr.type="string"></key>
     <key id="labelE" for="edge" attr.name="labelE" attr.type="string"></key>
     <key id="weight" for="edge" attr.name="weight" attr.type="float"></key>
     <graph id="G" edgedefault="directed">
         <node id="1">
             <data key="labelV">vertex</data>
             <data key="age">29</data>
+            <data key="colorV">#6495ed</data>
             <data key="name">marko</data>
         </node>
         <node id="2">
@@ -51,6 +54,7 @@
         </edge>
         <edge id="7" source="1" target="2">
             <data key="labelE">knows</data>
+            <data key="colorE">#ee0000</data>
             <data key="weight">0.5</data>
         </edge>
         <edge id="8" source="1" target="4">