tinkerpop-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ok...@apache.org
Subject [tinkerpop] branch tp4 updated: got TraversalSource, Traversal, and a basic implementation of Pipes/Steps working. Bytecode compilation no longer requires Java reflection -- big ass switch statement.
Date Thu, 07 Mar 2019 12:34:31 GMT
This is an automated email from the ASF dual-hosted git repository.

okram pushed a commit to branch tp4
in repository https://gitbox.apache.org/repos/asf/tinkerpop.git


The following commit(s) were added to refs/heads/tp4 by this push:
     new a472603  got TraversalSource, Traversal, and a basic implementation of Pipes/Steps
working. Bytecode compilation no longer requires Java reflection -- big ass switch statement.
a472603 is described below

commit a472603dcaa9a50e6cd77d4bbd5049b56ccc22c3
Author: Marko A. Rodriguez <okrammarko@gmail.com>
AuthorDate: Thu Mar 7 05:34:22 2019 -0700

    got TraversalSource, Traversal, and a basic implementation of Pipes/Steps working. Bytecode
compilation no longer requires Java reflection -- big ass switch statement.
---
 .../language/{GremlinCore.java => Gremlin.java}    |  6 +--
 .../tinkerpop/{machine => language}/Traversal.java | 19 ++++++--
 .../{GremlinCore.java => TraversalSource.java}     | 20 ++++++--
 .../tinkerpop/machine/{ => bytecode}/Bytecode.java |  7 ++-
 .../machine/{ => bytecode}/Instruction.java        | 15 ++++--
 .../machine/coefficients/LongCoefficients.java     |  6 +++
 .../machine/compiler/BytecodeToFunction.java       | 57 ++++++++++++++++++++++
 .../compiler/Symbols.java}                         | 10 ++--
 .../{GFunction.java => FilterFunction.java}        |  7 ++-
 .../machine/functions/FlatMapFunction.java}        | 26 ++--------
 .../tinkerpop/machine/functions/GFunction.java     | 22 ++++++---
 .../functions/{GFunction.java => IncrMap.java}     | 16 ++++--
 .../{GFunction.java => InitialFunction.java}       |  8 +--
 .../machine/functions/InjectInitial.java}          | 24 +++++----
 .../functions/{GFunction.java => IsFilter.java}    | 24 +++++++--
 .../tinkerpop/machine/functions/MapFunction.java   | 19 +-------
 .../machine/{ => traversers}/Traverser.java        |  6 +--
 .../tinkerpop/machine/traversers/TraverserSet.java |  1 -
 .../apache/tinkerpop/machine/TraversalTest.java    | 17 ++++---
 .../BytecodeToFunctionTest.java}                   | 16 +++---
 .../machine/pipes/{Pipe.java => Pipes.java}        | 30 +++++++++---
 .../org/apache/tinkerpop/machine/pipes/Step.java   | 46 ++++++++++++-----
 .../apache/tinkerpop/machine/pipes/PipesTest.java} | 21 +++++---
 23 files changed, 287 insertions(+), 136 deletions(-)

diff --git a/java/core/src/main/java/org/apache/tinkerpop/language/GremlinCore.java b/java/core/src/main/java/org/apache/tinkerpop/language/Gremlin.java
similarity index 88%
copy from java/core/src/main/java/org/apache/tinkerpop/language/GremlinCore.java
copy to java/core/src/main/java/org/apache/tinkerpop/language/Gremlin.java
index 59c4f69..2f29245 100644
--- a/java/core/src/main/java/org/apache/tinkerpop/language/GremlinCore.java
+++ b/java/core/src/main/java/org/apache/tinkerpop/language/Gremlin.java
@@ -21,9 +21,9 @@ package org.apache.tinkerpop.language;
 /**
  * @author Marko A. Rodriguez (http://markorodriguez.com)
  */
-public interface GremlinCore {
+public final class Gremlin {
 
-    public default GremlinCore select() {
-        return null;
+    public static <C> TraversalSource<C> traversal() {
+        return new TraversalSource<>();
     }
 }
diff --git a/java/core/src/main/java/org/apache/tinkerpop/machine/Traversal.java b/java/core/src/main/java/org/apache/tinkerpop/language/Traversal.java
similarity index 66%
rename from java/core/src/main/java/org/apache/tinkerpop/machine/Traversal.java
rename to java/core/src/main/java/org/apache/tinkerpop/language/Traversal.java
index 964143f..86d307a 100644
--- a/java/core/src/main/java/org/apache/tinkerpop/machine/Traversal.java
+++ b/java/core/src/main/java/org/apache/tinkerpop/language/Traversal.java
@@ -16,9 +16,10 @@
  * specific language governing permissions and limitations
  * under the License.
  */
-package org.apache.tinkerpop.machine;
+package org.apache.tinkerpop.language;
 
-import java.util.function.Function;
+import org.apache.tinkerpop.machine.bytecode.Bytecode;
+import org.apache.tinkerpop.machine.compiler.Symbols;
 
 /**
  * @author Marko A. Rodriguez (http://markorodriguez.com)
@@ -33,8 +34,18 @@ public class Traversal<C, A, B> {
         this.bytecode = new Bytecode<>();
     }
 
-    public <E> Traversal<C, A, E> map(final Function<B, E> mapFunction)
{
-        this.bytecode.addInstruction(this.currentCoefficient, "map", mapFunction);
+    public Traversal<C, A, B> is(final B object) {
+        this.bytecode.addInstruction(this.currentCoefficient, Symbols.IS, object);
+        return this;
+    }
+
+    public Traversal<C, A, Long> incr() {
+        this.bytecode.addInstruction(this.currentCoefficient, Symbols.INCR);
+        return (Traversal) this;
+    }
+
+    public <D> Traversal<C, A, D> inject(final D... objects) {
+        this.bytecode.addInstruction(this.currentCoefficient, Symbols.INJECT, objects);
         return (Traversal) this;
     }
 
diff --git a/java/core/src/main/java/org/apache/tinkerpop/language/GremlinCore.java b/java/core/src/main/java/org/apache/tinkerpop/language/TraversalSource.java
similarity index 59%
copy from java/core/src/main/java/org/apache/tinkerpop/language/GremlinCore.java
copy to java/core/src/main/java/org/apache/tinkerpop/language/TraversalSource.java
index 59c4f69..01c145a 100644
--- a/java/core/src/main/java/org/apache/tinkerpop/language/GremlinCore.java
+++ b/java/core/src/main/java/org/apache/tinkerpop/language/TraversalSource.java
@@ -18,12 +18,26 @@
  */
 package org.apache.tinkerpop.language;
 
+import org.apache.tinkerpop.machine.coefficients.Coefficients;
+import org.apache.tinkerpop.machine.coefficients.LongCoefficients;
+
 /**
  * @author Marko A. Rodriguez (http://markorodriguez.com)
  */
-public interface GremlinCore {
+public class TraversalSource<C> {
+
+    private Coefficients<C> coefficients = (Coefficients) new LongCoefficients();
+
+    protected TraversalSource() {
+    }
+
+    public TraversalSource<C> coefficients(final Coefficients coefficients) {
+        this.coefficients = coefficients;
+        return this;
+    }
 
-    public default GremlinCore select() {
-        return null;
+    public <A> Traversal<C, A, A> inject(final A... objects) {
+        final Traversal<C, A, A> traversal = new Traversal<>(this.coefficients.unity());
+        return traversal.inject(objects);
     }
 }
diff --git a/java/core/src/main/java/org/apache/tinkerpop/machine/Bytecode.java b/java/core/src/main/java/org/apache/tinkerpop/machine/bytecode/Bytecode.java
similarity index 90%
rename from java/core/src/main/java/org/apache/tinkerpop/machine/Bytecode.java
rename to java/core/src/main/java/org/apache/tinkerpop/machine/bytecode/Bytecode.java
index 2778366..f58f719 100644
--- a/java/core/src/main/java/org/apache/tinkerpop/machine/Bytecode.java
+++ b/java/core/src/main/java/org/apache/tinkerpop/machine/bytecode/Bytecode.java
@@ -16,7 +16,7 @@
  * specific language governing permissions and limitations
  * under the License.
  */
-package org.apache.tinkerpop.machine;
+package org.apache.tinkerpop.machine.bytecode;
 
 import java.util.ArrayList;
 import java.util.List;
@@ -39,4 +39,9 @@ public class Bytecode<C> {
     public List<Instruction<C>> getInstructions() {
         return this.instructions;
     }
+
+    @Override
+    public String toString() {
+        return this.instructions.toString();
+    }
 }
diff --git a/java/core/src/main/java/org/apache/tinkerpop/machine/Instruction.java b/java/core/src/main/java/org/apache/tinkerpop/machine/bytecode/Instruction.java
similarity index 79%
rename from java/core/src/main/java/org/apache/tinkerpop/machine/Instruction.java
rename to java/core/src/main/java/org/apache/tinkerpop/machine/bytecode/Instruction.java
index 02b88d2..1361104 100644
--- a/java/core/src/main/java/org/apache/tinkerpop/machine/Instruction.java
+++ b/java/core/src/main/java/org/apache/tinkerpop/machine/bytecode/Instruction.java
@@ -16,7 +16,9 @@
  * specific language governing permissions and limitations
  * under the License.
  */
-package org.apache.tinkerpop.machine;
+package org.apache.tinkerpop.machine.bytecode;
+
+import java.util.Arrays;
 
 /**
  * @author Marko A. Rodriguez (http://markorodriguez.com)
@@ -33,15 +35,20 @@ public final class Instruction<C> {
         this.args = args;
     }
 
-    public C getCoefficient() {
+    public C coefficient() {
         return this.coefficient;
     }
 
-    public String getOp() {
+    public String op() {
         return this.op;
     }
 
-    public Object[] getArgs() {
+    public Object[] args() {
         return this.args;
     }
+
+    @Override
+    public String toString() {
+        return "[" + this.coefficient + "]" + this.op + ":" + Arrays.toString(this.args).replace("[",
"").replace("]", "");
+    }
 }
diff --git a/java/core/src/main/java/org/apache/tinkerpop/machine/coefficients/LongCoefficients.java
b/java/core/src/main/java/org/apache/tinkerpop/machine/coefficients/LongCoefficients.java
index f9d9914..49406cf 100644
--- a/java/core/src/main/java/org/apache/tinkerpop/machine/coefficients/LongCoefficients.java
+++ b/java/core/src/main/java/org/apache/tinkerpop/machine/coefficients/LongCoefficients.java
@@ -23,6 +23,8 @@ package org.apache.tinkerpop.machine.coefficients;
  */
 public final class LongCoefficients implements Coefficients<Long> {
 
+    private static final LongCoefficients INSTANCE = new LongCoefficients();
+
     @Override
     public final Long sum(final Long coefficientA, final Long coefficientB) {
         return coefficientA + coefficientB;
@@ -47,4 +49,8 @@ public final class LongCoefficients implements Coefficients<Long>
{
     public final Long count(final Long coefficient) {
         return coefficient;
     }
+
+    public static LongCoefficients instance() {
+        return INSTANCE;
+    }
 }
diff --git a/java/core/src/main/java/org/apache/tinkerpop/machine/compiler/BytecodeToFunction.java
b/java/core/src/main/java/org/apache/tinkerpop/machine/compiler/BytecodeToFunction.java
new file mode 100644
index 0000000..cd328f8
--- /dev/null
+++ b/java/core/src/main/java/org/apache/tinkerpop/machine/compiler/BytecodeToFunction.java
@@ -0,0 +1,57 @@
+/*
+ * 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.
+ */
+package org.apache.tinkerpop.machine.compiler;
+
+import org.apache.tinkerpop.machine.bytecode.Bytecode;
+import org.apache.tinkerpop.machine.bytecode.Instruction;
+import org.apache.tinkerpop.machine.functions.GFunction;
+import org.apache.tinkerpop.machine.functions.IncrMap;
+import org.apache.tinkerpop.machine.functions.InjectInitial;
+import org.apache.tinkerpop.machine.functions.IsFilter;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * @author Marko A. Rodriguez (http://markorodriguez.com)
+ */
+public final class BytecodeToFunction {
+
+    public static <C> List<GFunction<C>> compile(final Bytecode<C>
bytecode) throws Exception {
+        final List<GFunction<C>> functions = new ArrayList<>();
+        for (final Instruction<C> instruction : bytecode.getInstructions()) {
+            functions.add(BytecodeToFunction.generateFunction(instruction));
+        }
+        return functions;
+    }
+
+    private static <C> GFunction<C> generateFunction(final Instruction<C>
instruction) throws Exception {
+        final String op = instruction.op();
+        switch (op) {
+            case Symbols.INJECT:
+                return new InjectInitial<>(instruction.coefficient(), instruction.args());
+            case Symbols.IS:
+                return new IsFilter<>(instruction.coefficient(), instruction.args()[0]);
+            case Symbols.INCR:
+                return new IncrMap<C>(instruction.coefficient());
+            default:
+                throw new Exception("This is an unknown instruction:" + instruction.op());
+        }
+    }
+}
diff --git a/java/core/src/main/java/org/apache/tinkerpop/language/GremlinCore.java b/java/core/src/main/java/org/apache/tinkerpop/machine/compiler/Symbols.java
similarity index 80%
rename from java/core/src/main/java/org/apache/tinkerpop/language/GremlinCore.java
rename to java/core/src/main/java/org/apache/tinkerpop/machine/compiler/Symbols.java
index 59c4f69..7de197f 100644
--- a/java/core/src/main/java/org/apache/tinkerpop/language/GremlinCore.java
+++ b/java/core/src/main/java/org/apache/tinkerpop/machine/compiler/Symbols.java
@@ -16,14 +16,14 @@
  * specific language governing permissions and limitations
  * under the License.
  */
-package org.apache.tinkerpop.language;
+package org.apache.tinkerpop.machine.compiler;
 
 /**
  * @author Marko A. Rodriguez (http://markorodriguez.com)
  */
-public interface GremlinCore {
+public final class Symbols {
 
-    public default GremlinCore select() {
-        return null;
-    }
+    public static final String IS = "is";
+    public static final String INCR = "incr";
+    public static final String INJECT = "inject";
 }
diff --git a/java/core/src/main/java/org/apache/tinkerpop/machine/functions/GFunction.java
b/java/core/src/main/java/org/apache/tinkerpop/machine/functions/FilterFunction.java
similarity index 84%
copy from java/core/src/main/java/org/apache/tinkerpop/machine/functions/GFunction.java
copy to java/core/src/main/java/org/apache/tinkerpop/machine/functions/FilterFunction.java
index 5766425..2835783 100644
--- a/java/core/src/main/java/org/apache/tinkerpop/machine/functions/GFunction.java
+++ b/java/core/src/main/java/org/apache/tinkerpop/machine/functions/FilterFunction.java
@@ -18,13 +18,12 @@
  */
 package org.apache.tinkerpop.machine.functions;
 
+import org.apache.tinkerpop.machine.traversers.Traverser;
 
-import org.apache.tinkerpop.machine.Traverser;
-
-import java.util.function.Function;
+import java.util.function.Predicate;
 
 /**
  * @author Marko A. Rodriguez (http://markorodriguez.com)
  */
-public interface GFunction<C, S, E> extends Function<Traverser<C, S>, Traverser<C,
E>> {
+public interface FilterFunction<C, A> extends Predicate<Traverser<C, A>>
{
 }
diff --git a/java/machine/pipes/src/main/java/org/apache/tinkerpop/machine/pipes/Pipe.java
b/java/core/src/main/java/org/apache/tinkerpop/machine/functions/FlatMapFunction.java
similarity index 68%
copy from java/machine/pipes/src/main/java/org/apache/tinkerpop/machine/pipes/Pipe.java
copy to java/core/src/main/java/org/apache/tinkerpop/machine/functions/FlatMapFunction.java
index 93e3a0d..6d4db90 100644
--- a/java/machine/pipes/src/main/java/org/apache/tinkerpop/machine/pipes/Pipe.java
+++ b/java/core/src/main/java/org/apache/tinkerpop/machine/functions/FlatMapFunction.java
@@ -16,31 +16,15 @@
  * specific language governing permissions and limitations
  * under the License.
  */
-package org.apache.tinkerpop.machine.pipes;
+package org.apache.tinkerpop.machine.functions;
+
+import org.apache.tinkerpop.machine.traversers.Traverser;
 
-import java.util.ArrayList;
 import java.util.Iterator;
-import java.util.List;
+import java.util.function.Function;
 
 /**
  * @author Marko A. Rodriguez (http://markorodriguez.com)
  */
-public class Pipe <S, E> implements Iterator<E>, AutoCloseable {
-
-    private final List<Step<?, ?>> steps = new ArrayList<>();
-
-    @Override
-    public void close() throws Exception {
-
-    }
-
-    @Override
-    public boolean hasNext() {
-        return false;
-    }
-
-    @Override
-    public E next() {
-        return null;
-    }
+public interface FlatMapFunction<C, A, B> extends Function<Traverser<C, A>,
Iterator<Traverser<C, B>>> {
 }
diff --git a/java/core/src/main/java/org/apache/tinkerpop/machine/functions/GFunction.java
b/java/core/src/main/java/org/apache/tinkerpop/machine/functions/GFunction.java
index 5766425..082b679 100644
--- a/java/core/src/main/java/org/apache/tinkerpop/machine/functions/GFunction.java
+++ b/java/core/src/main/java/org/apache/tinkerpop/machine/functions/GFunction.java
@@ -18,13 +18,23 @@
  */
 package org.apache.tinkerpop.machine.functions;
 
-
-import org.apache.tinkerpop.machine.Traverser;
-
-import java.util.function.Function;
-
 /**
  * @author Marko A. Rodriguez (http://markorodriguez.com)
  */
-public interface GFunction<C, S, E> extends Function<Traverser<C, S>, Traverser<C,
E>> {
+public class GFunction<C> {
+
+    protected final C coefficient;
+
+    public GFunction(final C coefficient) {
+        this.coefficient = coefficient;
+    }
+
+    public C coefficient() {
+        return this.coefficient;
+    }
+
+    @Override
+    public String toString() {
+        return "[" + this.coefficient + "]" + this.getClass().getSimpleName();
+    }
 }
diff --git a/java/core/src/main/java/org/apache/tinkerpop/machine/functions/GFunction.java
b/java/core/src/main/java/org/apache/tinkerpop/machine/functions/IncrMap.java
similarity index 70%
copy from java/core/src/main/java/org/apache/tinkerpop/machine/functions/GFunction.java
copy to java/core/src/main/java/org/apache/tinkerpop/machine/functions/IncrMap.java
index 5766425..2e5fa06 100644
--- a/java/core/src/main/java/org/apache/tinkerpop/machine/functions/GFunction.java
+++ b/java/core/src/main/java/org/apache/tinkerpop/machine/functions/IncrMap.java
@@ -18,13 +18,19 @@
  */
 package org.apache.tinkerpop.machine.functions;
 
-
-import org.apache.tinkerpop.machine.Traverser;
-
-import java.util.function.Function;
+import org.apache.tinkerpop.machine.traversers.Traverser;
 
 /**
  * @author Marko A. Rodriguez (http://markorodriguez.com)
  */
-public interface GFunction<C, S, E> extends Function<Traverser<C, S>, Traverser<C,
E>> {
+public class IncrMap<C> extends GFunction<C> implements MapFunction<C, Long,
Long> {
+
+    public IncrMap(final C coefficient) {
+        super(coefficient);
+    }
+
+    @Override
+    public Traverser<C, Long> apply(final Traverser<C, Long> traverser) {
+        return traverser.split(traverser.coefficient(), traverser.object() + 1);
+    }
 }
diff --git a/java/core/src/main/java/org/apache/tinkerpop/machine/functions/GFunction.java
b/java/core/src/main/java/org/apache/tinkerpop/machine/functions/InitialFunction.java
similarity index 81%
copy from java/core/src/main/java/org/apache/tinkerpop/machine/functions/GFunction.java
copy to java/core/src/main/java/org/apache/tinkerpop/machine/functions/InitialFunction.java
index 5766425..c30c46d 100644
--- a/java/core/src/main/java/org/apache/tinkerpop/machine/functions/GFunction.java
+++ b/java/core/src/main/java/org/apache/tinkerpop/machine/functions/InitialFunction.java
@@ -18,13 +18,13 @@
  */
 package org.apache.tinkerpop.machine.functions;
 
+import org.apache.tinkerpop.machine.traversers.Traverser;
 
-import org.apache.tinkerpop.machine.Traverser;
-
-import java.util.function.Function;
+import java.util.Iterator;
+import java.util.function.Supplier;
 
 /**
  * @author Marko A. Rodriguez (http://markorodriguez.com)
  */
-public interface GFunction<C, S, E> extends Function<Traverser<C, S>, Traverser<C,
E>> {
+public interface InitialFunction<C,A> extends Supplier<Iterator<Traverser<C,A>>>
{
 }
diff --git a/java/machine/pipes/src/main/java/org/apache/tinkerpop/machine/pipes/Pipe.java
b/java/core/src/main/java/org/apache/tinkerpop/machine/functions/InjectInitial.java
similarity index 57%
copy from java/machine/pipes/src/main/java/org/apache/tinkerpop/machine/pipes/Pipe.java
copy to java/core/src/main/java/org/apache/tinkerpop/machine/functions/InjectInitial.java
index 93e3a0d..4a5a8bd 100644
--- a/java/machine/pipes/src/main/java/org/apache/tinkerpop/machine/pipes/Pipe.java
+++ b/java/core/src/main/java/org/apache/tinkerpop/machine/functions/InjectInitial.java
@@ -16,7 +16,9 @@
  * specific language governing permissions and limitations
  * under the License.
  */
-package org.apache.tinkerpop.machine.pipes;
+package org.apache.tinkerpop.machine.functions;
+
+import org.apache.tinkerpop.machine.traversers.Traverser;
 
 import java.util.ArrayList;
 import java.util.Iterator;
@@ -25,22 +27,26 @@ import java.util.List;
 /**
  * @author Marko A. Rodriguez (http://markorodriguez.com)
  */
-public class Pipe <S, E> implements Iterator<E>, AutoCloseable {
+public class InjectInitial<C, A> extends GFunction<C> implements InitialFunction<C,
A> {
 
-    private final List<Step<?, ?>> steps = new ArrayList<>();
+    private final List<Traverser<C, A>> traversers;
 
-    @Override
-    public void close() throws Exception {
+    public InjectInitial(final C coefficient, final A... objects) {
+        super(coefficient);
+        this.traversers = new ArrayList<>();
+        for (final A object : objects) {
+            this.traversers.add(new Traverser<>(this.coefficient, object));
+        }
 
     }
 
     @Override
-    public boolean hasNext() {
-        return false;
+    public Iterator<Traverser<C, A>> get() {
+        return this.traversers.iterator();
     }
 
     @Override
-    public E next() {
-        return null;
+    public String toString() {
+        return "[" + this.coefficient + "]" + this.getClass().getSimpleName() + ":" + this.traversers;
     }
 }
diff --git a/java/core/src/main/java/org/apache/tinkerpop/machine/functions/GFunction.java
b/java/core/src/main/java/org/apache/tinkerpop/machine/functions/IsFilter.java
similarity index 61%
copy from java/core/src/main/java/org/apache/tinkerpop/machine/functions/GFunction.java
copy to java/core/src/main/java/org/apache/tinkerpop/machine/functions/IsFilter.java
index 5766425..892ed0c 100644
--- a/java/core/src/main/java/org/apache/tinkerpop/machine/functions/GFunction.java
+++ b/java/core/src/main/java/org/apache/tinkerpop/machine/functions/IsFilter.java
@@ -18,13 +18,27 @@
  */
 package org.apache.tinkerpop.machine.functions;
 
-
-import org.apache.tinkerpop.machine.Traverser;
-
-import java.util.function.Function;
+import org.apache.tinkerpop.machine.traversers.Traverser;
 
 /**
  * @author Marko A. Rodriguez (http://markorodriguez.com)
  */
-public interface GFunction<C, S, E> extends Function<Traverser<C, S>, Traverser<C,
E>> {
+public class IsFilter<C, A> extends GFunction<C> implements FilterFunction<C,
A> {
+
+    private final A object;
+
+    public IsFilter(final C coefficient, final A object) {
+        super(coefficient);
+        this.object = object;
+    }
+
+    @Override
+    public boolean test(final Traverser<C, A> traverser) {
+        return traverser.object().equals(this.object);
+    }
+
+    @Override
+    public String toString() {
+        return "[" + this.coefficient + "]" + this.getClass().getSimpleName() + ":" + this.object;
+    }
 }
diff --git a/java/core/src/main/java/org/apache/tinkerpop/machine/functions/MapFunction.java
b/java/core/src/main/java/org/apache/tinkerpop/machine/functions/MapFunction.java
index 7634b64..e443772 100644
--- a/java/core/src/main/java/org/apache/tinkerpop/machine/functions/MapFunction.java
+++ b/java/core/src/main/java/org/apache/tinkerpop/machine/functions/MapFunction.java
@@ -18,28 +18,13 @@
  */
 package org.apache.tinkerpop.machine.functions;
 
-import org.apache.tinkerpop.machine.Traverser;
-import org.apache.tinkerpop.machine.coefficients.Coefficients;
+import org.apache.tinkerpop.machine.traversers.Traverser;
 
 import java.util.function.Function;
 
 /**
  * @author Marko A. Rodriguez (http://markorodriguez.com)
  */
-public class MapFunction<C, A, B> implements GFunction<C, A,B> {
+public interface MapFunction<C, A, B> extends Function<Traverser<C, A>, Traverser<C,
B>> {
 
-    private final C coefficient;
-    private final Coefficients<C> coefficients;
-    private final Function<A, B> mapFunction;
-
-    public MapFunction(final Coefficients<C> coefficients, final C coefficient, final
Function<A, B> mapFunction) {
-        this.coefficients = coefficients;
-        this.coefficient = coefficient;
-        this.mapFunction = mapFunction;
-    }
-
-    @Override
-    public Traverser<C, B> apply(final Traverser<C, A> traverser) {
-        return traverser.split(this.coefficients.multiply(traverser.getCoefficient(), this.coefficient),
this.mapFunction.apply(traverser.getObject()));
-    }
 }
diff --git a/java/core/src/main/java/org/apache/tinkerpop/machine/Traverser.java b/java/core/src/main/java/org/apache/tinkerpop/machine/traversers/Traverser.java
similarity index 93%
rename from java/core/src/main/java/org/apache/tinkerpop/machine/Traverser.java
rename to java/core/src/main/java/org/apache/tinkerpop/machine/traversers/Traverser.java
index e57860e..2c8a1ae 100644
--- a/java/core/src/main/java/org/apache/tinkerpop/machine/Traverser.java
+++ b/java/core/src/main/java/org/apache/tinkerpop/machine/traversers/Traverser.java
@@ -16,7 +16,7 @@
  * specific language governing permissions and limitations
  * under the License.
  */
-package org.apache.tinkerpop.machine;
+package org.apache.tinkerpop.machine.traversers;
 
 /**
  * @author Marko A. Rodriguez (http://markorodriguez.com)
@@ -31,11 +31,11 @@ public class Traverser<C, A> {
         this.object = object;
     }
 
-    public C getCoefficient() {
+    public C coefficient() {
         return this.coefficient;
     }
 
-    public A getObject() {
+    public A object() {
         return this.object;
     }
 
diff --git a/java/core/src/main/java/org/apache/tinkerpop/machine/traversers/TraverserSet.java
b/java/core/src/main/java/org/apache/tinkerpop/machine/traversers/TraverserSet.java
index 5e072e5..2f1d18b 100644
--- a/java/core/src/main/java/org/apache/tinkerpop/machine/traversers/TraverserSet.java
+++ b/java/core/src/main/java/org/apache/tinkerpop/machine/traversers/TraverserSet.java
@@ -19,7 +19,6 @@
 package org.apache.tinkerpop.machine.traversers;
 
 
-import org.apache.tinkerpop.machine.Traverser;
 import org.apache.tinkerpop.machine.util.FastNoSuchElementException;
 
 import java.util.AbstractSet;
diff --git a/java/core/src/test/java/org/apache/tinkerpop/machine/TraversalTest.java b/java/core/src/test/java/org/apache/tinkerpop/machine/TraversalTest.java
index e682a05..41f5504 100644
--- a/java/core/src/test/java/org/apache/tinkerpop/machine/TraversalTest.java
+++ b/java/core/src/test/java/org/apache/tinkerpop/machine/TraversalTest.java
@@ -18,19 +18,24 @@
  */
 package org.apache.tinkerpop.machine;
 
+import org.apache.tinkerpop.language.Gremlin;
+import org.apache.tinkerpop.language.Traversal;
+import org.apache.tinkerpop.language.TraversalSource;
+import org.apache.tinkerpop.machine.coefficients.LongCoefficients;
+import org.apache.tinkerpop.machine.compiler.BytecodeToFunction;
 import org.junit.jupiter.api.Test;
 
-import static org.junit.jupiter.api.Assertions.assertEquals;
-
 /**
  * @author Marko A. Rodriguez (http://markorodriguez.com)
  */
 public class TraversalTest {
 
     @Test
-    public void shouldHaveBytecode() {
-        final Traversal<Long, Integer, Integer> traversal = new Traversal<>(1L);
-        traversal.map(a -> a + 1);
-        assertEquals("map", traversal.getBytecode().getInstructions().get(0).getOp());
+    public void shouldHaveBytecode() throws Exception {
+        TraversalSource<Long> g = Gremlin.traversal();
+        g = g.coefficients(LongCoefficients.instance());
+        final Traversal<Long, Long, Long> traversal = g.inject(7L).is(7L).incr().incr();
+        System.out.println(traversal.getBytecode());
+        System.out.println(BytecodeToFunction.compile(traversal.getBytecode()));
     }
 }
diff --git a/java/core/src/test/java/org/apache/tinkerpop/machine/TraversalTest.java b/java/core/src/test/java/org/apache/tinkerpop/machine/compiler/BytecodeToFunctionTest.java
similarity index 68%
copy from java/core/src/test/java/org/apache/tinkerpop/machine/TraversalTest.java
copy to java/core/src/test/java/org/apache/tinkerpop/machine/compiler/BytecodeToFunctionTest.java
index e682a05..f818b44 100644
--- a/java/core/src/test/java/org/apache/tinkerpop/machine/TraversalTest.java
+++ b/java/core/src/test/java/org/apache/tinkerpop/machine/compiler/BytecodeToFunctionTest.java
@@ -16,21 +16,21 @@
  * specific language governing permissions and limitations
  * under the License.
  */
-package org.apache.tinkerpop.machine;
+package org.apache.tinkerpop.machine.compiler;
 
+import org.apache.tinkerpop.language.Traversal;
 import org.junit.jupiter.api.Test;
 
-import static org.junit.jupiter.api.Assertions.assertEquals;
-
 /**
  * @author Marko A. Rodriguez (http://markorodriguez.com)
  */
-public class TraversalTest {
+public class BytecodeToFunctionTest {
 
     @Test
-    public void shouldHaveBytecode() {
-        final Traversal<Long, Integer, Integer> traversal = new Traversal<>(1L);
-        traversal.map(a -> a + 1);
-        assertEquals("map", traversal.getBytecode().getInstructions().get(0).getOp());
+    public void shouldHaveBytecode() throws Exception {
+        final Traversal<Long, Long, Long> traversal = new Traversal<>(1L);
+        traversal.incr().is(2L);
+        System.out.println(traversal.getBytecode());
+        System.out.println(BytecodeToFunction.compile(traversal.getBytecode()));
     }
 }
diff --git a/java/machine/pipes/src/main/java/org/apache/tinkerpop/machine/pipes/Pipe.java
b/java/machine/pipes/src/main/java/org/apache/tinkerpop/machine/pipes/Pipes.java
similarity index 53%
rename from java/machine/pipes/src/main/java/org/apache/tinkerpop/machine/pipes/Pipe.java
rename to java/machine/pipes/src/main/java/org/apache/tinkerpop/machine/pipes/Pipes.java
index 93e3a0d..c370260 100644
--- a/java/machine/pipes/src/main/java/org/apache/tinkerpop/machine/pipes/Pipe.java
+++ b/java/machine/pipes/src/main/java/org/apache/tinkerpop/machine/pipes/Pipes.java
@@ -18,6 +18,10 @@
  */
 package org.apache.tinkerpop.machine.pipes;
 
+import org.apache.tinkerpop.machine.bytecode.Bytecode;
+import org.apache.tinkerpop.machine.compiler.BytecodeToFunction;
+import org.apache.tinkerpop.machine.functions.GFunction;
+
 import java.util.ArrayList;
 import java.util.Iterator;
 import java.util.List;
@@ -25,22 +29,34 @@ import java.util.List;
 /**
  * @author Marko A. Rodriguez (http://markorodriguez.com)
  */
-public class Pipe <S, E> implements Iterator<E>, AutoCloseable {
-
-    private final List<Step<?, ?>> steps = new ArrayList<>();
+public class Pipes<C, S, E> implements Iterator<E> {
 
-    @Override
-    public void close() throws Exception {
+    private final List<Step<?, ?, ?>> steps = new ArrayList<>();
 
+    public Pipes(final Bytecode<C> bytecode) throws Exception {
+        final List<GFunction<C>> functions = BytecodeToFunction.compile(bytecode);
+        Step previousStep = null;
+        for (final GFunction<?> function : functions) {
+            previousStep = new Step(previousStep, function);
+            this.steps.add(previousStep);
+        }
     }
 
     @Override
     public boolean hasNext() {
-        return false;
+        return this.steps.get(this.steps.size() - 1).hasNext();
     }
 
     @Override
     public E next() {
-        return null;
+        return (E) this.steps.get(this.steps.size() - 1).next().object();
+    }
+
+    public List<E> toList() {
+        final List<E> list = new ArrayList<>();
+        while (this.hasNext()) {
+            list.add(this.next());
+        }
+        return list;
     }
 }
diff --git a/java/machine/pipes/src/main/java/org/apache/tinkerpop/machine/pipes/Step.java
b/java/machine/pipes/src/main/java/org/apache/tinkerpop/machine/pipes/Step.java
index fc05c8f..f2085a7 100644
--- a/java/machine/pipes/src/main/java/org/apache/tinkerpop/machine/pipes/Step.java
+++ b/java/machine/pipes/src/main/java/org/apache/tinkerpop/machine/pipes/Step.java
@@ -18,35 +18,57 @@
  */
 package org.apache.tinkerpop.machine.pipes;
 
-import org.apache.tinkerpop.machine.Traverser;
+import org.apache.tinkerpop.machine.traversers.Traverser;
+import org.apache.tinkerpop.machine.functions.FilterFunction;
 import org.apache.tinkerpop.machine.functions.GFunction;
-import org.apache.tinkerpop.machine.traversers.TraverserSet;
+import org.apache.tinkerpop.machine.functions.InitialFunction;
+import org.apache.tinkerpop.machine.functions.MapFunction;
+import org.apache.tinkerpop.machine.util.FastNoSuchElementException;
 
 import java.util.Iterator;
 
 /**
  * @author Marko A. Rodriguez (http://markorodriguez.com)
  */
-public class Step<S, E> implements Iterator<Traverser<?, E>> {
+public class Step<C, S, E> implements Iterator<Traverser<?, E>> {
 
-    private final GFunction<?, S, E> function;
-    private TraverserSet<?, S> set = new TraverserSet<>();
+    private final GFunction<C> function;
+    private final Step previousStep;
+    private Iterator<Traverser<C, E>> currentIterator = null;
 
-    public Step(final GFunction<?, S, E> function) {
+    public Step(final Step previousStep, final GFunction<C> function) {
+        this.previousStep = previousStep;
         this.function = function;
-    }
-
-    public void addStart(final Traverser<?, S> start) {
-        this.set.add((Traverser) start);
+        if (this.function instanceof InitialFunction) {
+            this.currentIterator = ((InitialFunction<C, E>) this.function).get();
+        }
     }
 
     @Override
     public boolean hasNext() {
-        return false;
+        if (null != this.currentIterator)
+            return this.currentIterator.hasNext();
+        else return this.previousStep.hasNext();
     }
 
     @Override
     public Traverser<?, E> next() {
-        return null;
+        if (null != this.currentIterator && this.currentIterator.hasNext())
+            return this.currentIterator.next();
+        if (!this.previousStep.hasNext())
+            throw FastNoSuchElementException.instance();
+
+        if (this.function instanceof MapFunction) {
+            return ((MapFunction<C, ?, E>) this.function).apply(this.previousStep.next());
+        } else {
+            Traverser<C, E> traverser = null;
+            while (this.previousStep.hasNext()) {
+                traverser = this.previousStep.next();
+                if (((FilterFunction<C, E>) this.function).test(traverser))
+                    return traverser;
+            }
+        }
+        throw FastNoSuchElementException.instance();
+
     }
 }
diff --git a/java/core/src/test/java/org/apache/tinkerpop/machine/TraversalTest.java b/java/machine/pipes/src/test/java/org/apache/tinkerpop/machine/pipes/PipesTest.java
similarity index 55%
copy from java/core/src/test/java/org/apache/tinkerpop/machine/TraversalTest.java
copy to java/machine/pipes/src/test/java/org/apache/tinkerpop/machine/pipes/PipesTest.java
index e682a05..1b7e020 100644
--- a/java/core/src/test/java/org/apache/tinkerpop/machine/TraversalTest.java
+++ b/java/machine/pipes/src/test/java/org/apache/tinkerpop/machine/pipes/PipesTest.java
@@ -16,21 +16,26 @@
  * specific language governing permissions and limitations
  * under the License.
  */
-package org.apache.tinkerpop.machine;
+package org.apache.tinkerpop.machine.pipes;
 
+import org.apache.tinkerpop.language.Gremlin;
+import org.apache.tinkerpop.language.Traversal;
+import org.apache.tinkerpop.language.TraversalSource;
+import org.apache.tinkerpop.machine.coefficients.LongCoefficients;
 import org.junit.jupiter.api.Test;
 
-import static org.junit.jupiter.api.Assertions.assertEquals;
-
 /**
  * @author Marko A. Rodriguez (http://markorodriguez.com)
  */
-public class TraversalTest {
+public class PipesTest {
 
     @Test
-    public void shouldHaveBytecode() {
-        final Traversal<Long, Integer, Integer> traversal = new Traversal<>(1L);
-        traversal.map(a -> a + 1);
-        assertEquals("map", traversal.getBytecode().getInstructions().get(0).getOp());
+    public void shouldWork() throws Exception {
+        final TraversalSource<Long> g = Gremlin.<Long>traversal().coefficients(LongCoefficients.instance());
+        final Traversal<Long, Long, Long> traversal = g.inject(7L, 10L, 12L).incr().incr().incr();
+        final Pipes pipes = new Pipes<Long, Long, Long>(traversal.getBytecode());
+        System.out.println(pipes.hasNext());
+        System.out.println(pipes.toList());
+        System.out.println(pipes.hasNext());
     }
 }


Mime
View raw message