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 2015/08/17 20:26:16 UTC

incubator-tinkerpop git commit: Provide a strict parsing option for GraphMLReader.

Repository: incubator-tinkerpop
Updated Branches:
  refs/heads/master 86402aba9 -> 45023be9d


Provide a strict parsing option for GraphMLReader.

This setting allows the GraphMLReader to import data even though data types might not match up to the values in the GraphML itself. TINKERPOP3-756


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

Branch: refs/heads/master
Commit: 45023be9d71e565338da44aa7dbbbbd6708d2afb
Parents: 86402ab
Author: Stephen Mallette <sp...@genoprime.com>
Authored: Mon Aug 17 14:24:18 2015 -0400
Committer: Stephen Mallette <sp...@genoprime.com>
Committed: Mon Aug 17 14:24:18 2015 -0400

----------------------------------------------------------------------
 .../structure/io/graphml/GraphMLReader.java     | 43 +++++++++++++-----
 .../tinkerpop/gremlin/structure/io/IoTest.java  | 46 +++++++++++++++++++-
 .../structure/io/graphml/graph-types-bad.xml    | 42 ++++++++++++++++++
 3 files changed, 119 insertions(+), 12 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/45023be9/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/graphml/GraphMLReader.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/graphml/GraphMLReader.java b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/graphml/GraphMLReader.java
index d2fdb39..d45d411 100644
--- a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/graphml/GraphMLReader.java
+++ b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/graphml/GraphMLReader.java
@@ -61,12 +61,13 @@ public final class GraphMLReader implements GraphReader {
     private final String edgeLabelKey;
     private final String vertexLabelKey;
     private final long batchSize;
+    private final boolean strict;
 
-    private GraphMLReader(final String edgeLabelKey, final String vertexLabelKey,
-                          final long batchSize) {
-        this.edgeLabelKey = edgeLabelKey;
-        this.batchSize = batchSize;
-        this.vertexLabelKey = vertexLabelKey;
+    private GraphMLReader(final Builder builder) {
+        this.edgeLabelKey = builder.edgeLabelKey;
+        this.batchSize = builder.batchSize;
+        this.vertexLabelKey = builder.vertexLabelKey;
+        this.strict = builder.strict;
     }
 
     @Override
@@ -143,13 +144,23 @@ public final class GraphMLReader implements GraphReader {
                                 if (isInVertex) {
                                     if (key.equals(vertexLabelKey))
                                         vertexLabel = value;
-                                    else
-                                        vertexProps.put(dataAttributeName, typeCastValue(key, value, keyTypesMaps));
+                                    else {
+                                        try {
+                                            vertexProps.put(dataAttributeName, typeCastValue(key, value, keyTypesMaps));
+                                        } catch (NumberFormatException nfe) {
+                                            if (strict) throw nfe;
+                                        }
+                                    }
                                 } else if (isInEdge) {
                                     if (key.equals(edgeLabelKey))
                                         edgeLabel = value;
-                                    else
-                                        edgeProps.put(dataAttributeName, typeCastValue(key, value, keyTypesMaps));
+                                    else {
+                                        try {
+                                            edgeProps.put(dataAttributeName, typeCastValue(key, value, keyTypesMaps));
+                                        } catch (NumberFormatException nfe) {
+                                            if (strict) throw nfe;
+                                        }
+                                    }
                                 }
                             }
 
@@ -329,9 +340,19 @@ public final class GraphMLReader implements GraphReader {
     public static final class Builder implements ReaderBuilder<GraphMLReader> {
         private String edgeLabelKey = GraphMLTokens.LABEL_E;
         private String vertexLabelKey = GraphMLTokens.LABEL_V;
+        private boolean strict = true;
         private long batchSize = 10000;
 
-        private Builder() {
+        private Builder() { }
+
+        /**
+         * When set to true, exceptions will be thrown if a property value cannot be coerced to the expected data
+         * type. If set to false, then the reader will continue with the import but ignore the failed property key.
+         * By default this value is "true".
+         */
+        public Builder strict(final boolean strict) {
+            this.strict = strict;
+            return this;
         }
 
         /**
@@ -359,7 +380,7 @@ public final class GraphMLReader implements GraphReader {
         }
 
         public GraphMLReader create() {
-            return new GraphMLReader(edgeLabelKey, vertexLabelKey, batchSize);
+            return new GraphMLReader(this);
         }
     }
 }

http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/45023be9/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 ec27504..cdef5b8 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
@@ -79,6 +79,7 @@ import static org.apache.tinkerpop.gremlin.structure.Graph.Features.VertexFeatur
 import static org.apache.tinkerpop.gremlin.structure.Graph.Features.VertexPropertyFeatures.*;
 import static org.apache.tinkerpop.gremlin.structure.io.IoCore.graphson;
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertTrue;
 import static org.junit.Assert.fail;
 
@@ -123,7 +124,7 @@ public class IoTest {
         @FeatureRequirement(featureClass = VertexPropertyFeatures.class, feature = FEATURE_LONG_VALUES)
         @FeatureRequirement(featureClass = VertexPropertyFeatures.class, feature = FEATURE_DOUBLE_VALUES)
         @FeatureRequirement(featureClass = EdgePropertyFeatures.class, feature = EdgePropertyFeatures.FEATURE_FLOAT_VALUES)
-        public void shouldReadGraphMLAnAllSupportedDataTypes() throws IOException {
+        public void shouldReadGraphMLWithAllSupportedDataTypes() throws IOException {
             final GraphReader reader = GraphMLReader.build().create();
             try (final InputStream stream = IoTest.class.getResourceAsStream(TestHelper.convertPackageToResourcePath(GraphMLResourceAccess.class) + "graph-types.xml")) {
                 reader.readGraph(stream, graph);
@@ -139,6 +140,49 @@ public class IoTest {
             assertEquals("junk", v.<String>value("n"));
         }
 
+        @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)
+        @FeatureRequirement(featureClass = VertexPropertyFeatures.class, feature = FEATURE_STRING_VALUES)
+        @FeatureRequirement(featureClass = VertexPropertyFeatures.class, feature = FEATURE_INTEGER_VALUES)
+        @FeatureRequirement(featureClass = VertexPropertyFeatures.class, feature = FEATURE_FLOAT_VALUES)
+        @FeatureRequirement(featureClass = VertexPropertyFeatures.class, feature = FEATURE_BOOLEAN_VALUES)
+        @FeatureRequirement(featureClass = VertexPropertyFeatures.class, feature = FEATURE_LONG_VALUES)
+        @FeatureRequirement(featureClass = VertexPropertyFeatures.class, feature = FEATURE_DOUBLE_VALUES)
+        @FeatureRequirement(featureClass = EdgePropertyFeatures.class, feature = EdgePropertyFeatures.FEATURE_FLOAT_VALUES)
+        public void shouldReadGraphMLWithoutStrictOption() throws IOException {
+            final GraphReader reader = GraphMLReader.build().strict(false).create();
+            try (final InputStream stream = IoTest.class.getResourceAsStream(TestHelper.convertPackageToResourcePath(GraphMLResourceAccess.class) + "graph-types-bad.xml")) {
+                reader.readGraph(stream, graph);
+            }
+
+            final Vertex v = graph.vertices().next();
+            assertFalse(v.values("d").hasNext());
+            assertEquals("some-string", v.<String>value("s"));
+            assertFalse(v.values("i").hasNext());
+            assertEquals(false, v.<Boolean>value("b"));
+            assertFalse(v.values("f").hasNext());
+            assertFalse(v.values("l").hasNext());
+            assertEquals("junk", v.<String>value("n"));
+        }
+
+        @Test(expected = NumberFormatException.class)
+        @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)
+        @FeatureRequirement(featureClass = VertexPropertyFeatures.class, feature = FEATURE_STRING_VALUES)
+        @FeatureRequirement(featureClass = VertexPropertyFeatures.class, feature = FEATURE_INTEGER_VALUES)
+        @FeatureRequirement(featureClass = VertexPropertyFeatures.class, feature = FEATURE_FLOAT_VALUES)
+        @FeatureRequirement(featureClass = VertexPropertyFeatures.class, feature = FEATURE_BOOLEAN_VALUES)
+        @FeatureRequirement(featureClass = VertexPropertyFeatures.class, feature = FEATURE_LONG_VALUES)
+        @FeatureRequirement(featureClass = VertexPropertyFeatures.class, feature = FEATURE_DOUBLE_VALUES)
+        @FeatureRequirement(featureClass = EdgePropertyFeatures.class, feature = EdgePropertyFeatures.FEATURE_FLOAT_VALUES)
+        public void shouldReadGraphMLWithStrictOption() throws IOException {
+            final GraphReader reader = GraphMLReader.build().strict(true).create();
+            try (final InputStream stream = IoTest.class.getResourceAsStream(TestHelper.convertPackageToResourcePath(GraphMLResourceAccess.class) + "graph-types-bad.xml")) {
+                reader.readGraph(stream, graph);
+            }
+        }
+
         /**
          * Only need to execute this test with TinkerGraph or other graphs that support user supplied identifiers.
          */

http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/45023be9/gremlin-test/src/main/resources/org/apache/tinkerpop/gremlin/structure/io/graphml/graph-types-bad.xml
----------------------------------------------------------------------
diff --git a/gremlin-test/src/main/resources/org/apache/tinkerpop/gremlin/structure/io/graphml/graph-types-bad.xml b/gremlin-test/src/main/resources/org/apache/tinkerpop/gremlin/structure/io/graphml/graph-types-bad.xml
new file mode 100644
index 0000000..ee4700b
--- /dev/null
+++ b/gremlin-test/src/main/resources/org/apache/tinkerpop/gremlin/structure/io/graphml/graph-types-bad.xml
@@ -0,0 +1,42 @@
+<!--
+Licensed to the Apache Software Foundation (ASF) under one or more
+contributor license agreements.  See the NOTICE file distributed with
+this work for additional information regarding copyright ownership.
+The ASF licenses this file to You under the Apache License, Version 2.0
+(the "License"); you may not use this file except in compliance with
+the License.  You may obtain a copy of the License at
+
+  http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+-->
+<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.0/graphml.xsd">
+    <key id="id-d" for="node" attr.name="d" attr.type="double"/>
+    <key id="id-s" for="node" attr.name="s" attr.type="string"/>
+    <key id="id-i" for="node" attr.name="i" attr.type="int"/>
+    <key id="id-b" for="node" attr.name="b" attr.type="boolean"/>
+    <key id="id-l" for="node" attr.name="l" attr.type="long"/>
+    <key id="id-f" for="node" attr.name="f" attr.type="float"/>
+    <key id="id-n" for="node" attr.name="n" attr.type="nothing-supported-and-goes-to-string"/>
+    <key id="labelV" for="node" attr.name="labelV" attr.type="string"/>
+    <key id="labelE" for="edge" attr.name="labelE" attr.type="string"/>
+    <graph id="G" edgedefault="directed">
+        <node id="1">
+            <data key="id-d"></data>
+            <data key="id-s">some-string</data>
+            <data key="id-i">not-parseable</data>
+            <data key="id-b">blah</data>
+            <data key="id-l"></data>
+            <data key="id-f"></data>
+            <data key="id-n">junk</data>
+            <data key="label">default</data>
+        </node>
+    </graph>
+</graphml>