You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tinkerpop.apache.org by ok...@apache.org on 2016/02/11 20:46:46 UTC

[04/19] incubator-tinkerpop git commit: Added ScriptElementFactory::graph() to provide access to the local star graph.

Added ScriptElementFactory::graph() to provide access to the local star graph.

This is currently the only option I see that doesn't introduce breaking changes.

Docs and samples were updated accordingly.


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

Branch: refs/heads/TINKERPOP-1140
Commit: 99b5791077f14e12dfc61f264c2145d0f3acd0f6
Parents: 50b7f82
Author: Daniel Kuppitz <da...@hotmail.com>
Authored: Wed Feb 10 00:01:31 2016 +0100
Committer: Daniel Kuppitz <da...@hotmail.com>
Committed: Wed Feb 10 00:01:31 2016 +0100

----------------------------------------------------------------------
 data/script-input.groovy                            |  8 ++++----
 docs/src/reference/implementations.asciidoc         |  9 +++++----
 .../io/script/script-input-grateful-dead.groovy     |  9 +++++----
 .../gremlin/structure/io/script/script-input.groovy | 15 +++++++--------
 .../structure/io/script/ScriptRecordReader.java     | 16 ++++++++++++----
 5 files changed, 33 insertions(+), 24 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/99b57910/data/script-input.groovy
----------------------------------------------------------------------
diff --git a/data/script-input.groovy b/data/script-input.groovy
index d2df481..3a2dfd8 100644
--- a/data/script-input.groovy
+++ b/data/script-input.groovy
@@ -17,9 +17,10 @@
  * under the License.
  */
 def parse(line, factory) {
+    def graph = factory.graph()
     def parts = line.split(/ /)
     def (id, label, name, x) = parts[0].split(/:/).toList()
-    def v1 = factory.vertex(id, label)
+    def v1 = graph.addVertex(T.id, id, T.label, label)
     if (name != null) v1.property("name", name) // first value is always the name
     if (x != null) {
         // second value depends on the vertex label; it's either
@@ -30,9 +31,8 @@ def parse(line, factory) {
     if (parts.length == 2) {
         parts[1].split(/,/).grep { !it.isEmpty() }.each {
             def (eLabel, refId, weight) = it.split(/:/).toList()
-            def v2 = factory.vertex(refId)
-            def edge = factory.edge(v1, v2, eLabel)
-            edge.property("weight", Double.valueOf(weight))
+            def v2 = graph.addVertex(T.id, refId)
+            v1.addOutEdge(eLabel, v2, "weight", Double.valueOf(weight))
         }
     }
     return v1

http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/99b57910/docs/src/reference/implementations.asciidoc
----------------------------------------------------------------------
diff --git a/docs/src/reference/implementations.asciidoc b/docs/src/reference/implementations.asciidoc
index 439123d..5eadef3 100644
--- a/docs/src/reference/implementations.asciidoc
+++ b/docs/src/reference/implementations.asciidoc
@@ -1547,6 +1547,7 @@ def parse(String line, ScriptElementFactory factory) { ... }
 `ScriptElementFactory` provides the following 4 methods:
 
 [source,java]
+Graph graph(); // get a reference to the local star graph
 Vertex vertex(Object id); // get or create the vertex with the given id
 Vertex vertex(Object id, String label); // get or create the vertex with the given id and label
 Edge edge(Vertex out, Vertex in); // create an edge between the two given vertices
@@ -1556,9 +1557,10 @@ An appropriate `parse()` for the above adjacency list file is:
 
 [source,groovy]
 def parse(line, factory) {
+    def graph = factory.graph()
     def parts = line.split(/ /)
     def (id, label, name, x) = parts[0].split(/:/).toList()
-    def v1 = factory.vertex(id, label)
+    def v1 = graph.addVertex(T.id, id, T.label, label)
     if (name != null) v1.property('name', name) // first value is always the name
     if (x != null) {
         // second value depends on the vertex label; it's either
@@ -1569,9 +1571,8 @@ def parse(line, factory) {
     if (parts.length == 2) {
         parts[1].split(/,/).grep { !it.isEmpty() }.each {
             def (eLabel, refId, weight) = it.split(/:/).toList()
-            def v2 = factory.vertex(refId)
-            def edge = factory.edge(v1, v2, eLabel)
-            edge.property('weight', Double.valueOf(weight))
+            def v2 = graph.addVertex(T.id, refId)
+            v1.addOutEdge(eLabel, v2, 'weight', Double.valueOf(weight))
         }
     }
     return v1

http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/99b57910/gremlin-test/src/main/resources/org/apache/tinkerpop/gremlin/structure/io/script/script-input-grateful-dead.groovy
----------------------------------------------------------------------
diff --git a/gremlin-test/src/main/resources/org/apache/tinkerpop/gremlin/structure/io/script/script-input-grateful-dead.groovy b/gremlin-test/src/main/resources/org/apache/tinkerpop/gremlin/structure/io/script/script-input-grateful-dead.groovy
index 814843c..9dbc5fc 100644
--- a/gremlin-test/src/main/resources/org/apache/tinkerpop/gremlin/structure/io/script/script-input-grateful-dead.groovy
+++ b/gremlin-test/src/main/resources/org/apache/tinkerpop/gremlin/structure/io/script/script-input-grateful-dead.groovy
@@ -18,9 +18,10 @@
  */
 
 def parse(line, factory) {
+    def graph = factory.graph()
     def (vertex, outEdges, inEdges) = line.split(/\t/, 3)
     def (v1id, v1label, v1props) = vertex.split(/,/, 3)
-    def v1 = factory.vertex(v1id.toInteger(), v1label)
+    def v1 = graph.addVertex(T.id, v1id.toInteger(), T.label, v1label)
     switch (v1label) {
         case "song":
             def (name, songType, performances) = v1props.split(/,/)
@@ -43,10 +44,10 @@ def parse(line, factory) {
             } else {
                 (eLabel, otherV, weight) = parts
             }
-            def v2 = factory.vertex(otherV.toInteger())
-            def e = factory.edge(out ? v1 : v2, out ? v2 : v1, eLabel)
+            def v2 = graph.addVertex(T.id, otherV.toInteger())
+            def e = out ? v1.addOutEdge(eLabel, v2) : v1.addInEdge(eLabel, v2)
             if (weight != null) e.property("weight", weight.toInteger())
         }
     }
     return v1
-}
\ No newline at end of file
+}

http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/99b57910/gremlin-test/src/main/resources/org/apache/tinkerpop/gremlin/structure/io/script/script-input.groovy
----------------------------------------------------------------------
diff --git a/gremlin-test/src/main/resources/org/apache/tinkerpop/gremlin/structure/io/script/script-input.groovy b/gremlin-test/src/main/resources/org/apache/tinkerpop/gremlin/structure/io/script/script-input.groovy
index adb32b7..096fdd4 100644
--- a/gremlin-test/src/main/resources/org/apache/tinkerpop/gremlin/structure/io/script/script-input.groovy
+++ b/gremlin-test/src/main/resources/org/apache/tinkerpop/gremlin/structure/io/script/script-input.groovy
@@ -19,9 +19,10 @@ import org.apache.tinkerpop.gremlin.structure.VertexProperty
  * under the License.
  */
 def parse(line, factory) {
+    def graph = factory.graph()
     def parts = line.split(/\t/)
     def (id, label, name, x) = parts[0].split(/:/).toList()
-    def v1 = factory.vertex(id, label)
+    def v1 = graph.addVertex(T.id, id, T.label, label)
     if (name != null) v1.property(VertexProperty.Cardinality.single, "name", name)
     if (x != null) {
         if (label.equals("project")) v1.property(VertexProperty.Cardinality.single, "lang", x)
@@ -31,19 +32,17 @@ def parse(line, factory) {
     if (parts.length >= 2) {
         parts[1].split(/,/).grep { !it.isEmpty() }.each {
             def (eLabel, refId, weight) = it.split(/:/).toList()
-            def v2 = factory.vertex(refId)
-            def edge = factory.edge(v1, v2, eLabel)
-            edge.property("weight", Double.valueOf(weight))
+            def v2 = graph.addVertex(T.id, refId)
+            v1.addOutEdge(eLabel, v2, "weight", Double.valueOf(weight))
         }
     }
     // process in-edges
     if (parts.length == 3) {
         parts[2].split(/,/).grep { !it.isEmpty() }.each {
             def (eLabel, refId, weight) = it.split(/:/).toList()
-            def v2 = factory.vertex(refId)
-            def edge = factory.edge(v2, v1, eLabel)
-            edge.property("weight", Double.valueOf(weight))
+            def v2 = graph.addVertex(T.id, refId)
+            v1.addInEdge(eLabel, v2, "weight", Double.valueOf(weight))
         }
     }
     return v1
-}
\ No newline at end of file
+}

http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/99b57910/hadoop-gremlin/src/main/java/org/apache/tinkerpop/gremlin/hadoop/structure/io/script/ScriptRecordReader.java
----------------------------------------------------------------------
diff --git a/hadoop-gremlin/src/main/java/org/apache/tinkerpop/gremlin/hadoop/structure/io/script/ScriptRecordReader.java b/hadoop-gremlin/src/main/java/org/apache/tinkerpop/gremlin/hadoop/structure/io/script/ScriptRecordReader.java
index 4cc1602..4615b51 100644
--- a/hadoop-gremlin/src/main/java/org/apache/tinkerpop/gremlin/hadoop/structure/io/script/ScriptRecordReader.java
+++ b/hadoop-gremlin/src/main/java/org/apache/tinkerpop/gremlin/hadoop/structure/io/script/ScriptRecordReader.java
@@ -39,6 +39,7 @@ import javax.script.Bindings;
 import javax.script.ScriptEngine;
 import javax.script.ScriptException;
 import java.io.IOException;
+import java.io.InputStream;
 import java.io.InputStreamReader;
 import java.util.Iterator;
 
@@ -68,10 +69,13 @@ public final class ScriptRecordReader extends RecordReader<NullWritable, VertexW
         this.engine = new GremlinGroovyScriptEngine((CompilerCustomizerProvider) new DefaultImportCustomizerProvider());
         //this.engine = ScriptEngineCache.get(configuration.get(SCRIPT_ENGINE, ScriptEngineCache.DEFAULT_SCRIPT_ENGINE));
         final FileSystem fs = FileSystem.get(configuration);
-        try {
-            this.engine.eval(new InputStreamReader(fs.open(new Path(configuration.get(SCRIPT_FILE)))));
-        } catch (final ScriptException e) {
-            throw new IOException(e.getMessage(), e);
+        try (final InputStream stream = fs.open(new Path(configuration.get(SCRIPT_FILE)));
+             final InputStreamReader reader = new InputStreamReader(stream)) {
+            try {
+                this.engine.eval(reader);
+            } catch (ScriptException e) {
+                throw new IOException(e.getMessage(), e);
+            }
         }
     }
 
@@ -138,5 +142,9 @@ public final class ScriptRecordReader extends RecordReader<NullWritable, VertexW
         public Edge edge(final Vertex out, final Vertex in, final String label) {
             return out.addEdge(label, in);
         }
+
+        public StarGraph graph() {
+            return this.graph;
+        }
     }
 }