tinkerpop-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ok...@apache.org
Subject [tinkerpop] 23/41: added filter(traversal).
Date Wed, 13 Mar 2019 20:51:46 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

commit f684e0f0b7c4f9cbd6f1ecbff265cce79bf85d9e
Author: Marko A. Rodriguez <okrammarko@gmail.com>
AuthorDate: Mon Mar 11 14:16:05 2019 -0600

    added filter(traversal).
---
 .../org/apache/tinkerpop/language/Symbols.java     |  1 +
 .../org/apache/tinkerpop/language/Traversal.java   |  5 +++
 .../java/org/apache/tinkerpop/language/__.java     |  4 ++
 .../tinkerpop/machine/bytecode/BytecodeUtil.java   |  3 ++
 .../UnionFlatMap.java => filter/FilterFilter.java} | 48 +++++++++-------------
 .../machine/functions/filter/IdentityFilter.java   |  2 +-
 .../machine/functions/filter/IsFilter.java         |  3 +-
 .../machine/functions/flatMap/UnionFlatMap.java    |  4 +-
 .../apache/tinkerpop/machine/pipes/PipesTest.java  |  5 +++
 9 files changed, 42 insertions(+), 33 deletions(-)

diff --git a/java/core/src/main/java/org/apache/tinkerpop/language/Symbols.java b/java/core/src/main/java/org/apache/tinkerpop/language/Symbols.java
index 7964d07..81c80bb 100644
--- a/java/core/src/main/java/org/apache/tinkerpop/language/Symbols.java
+++ b/java/core/src/main/java/org/apache/tinkerpop/language/Symbols.java
@@ -33,6 +33,7 @@ public final class Symbols {
     public static final String AS = "as";
     public static final String C = "c";
     public static final String COUNT = "count";
+    public static final String FILTER = "filter";
     public static final String IDENTITY = "identity";
     public static final String IS = "is";
     public static final String INCR = "incr";
diff --git a/java/core/src/main/java/org/apache/tinkerpop/language/Traversal.java b/java/core/src/main/java/org/apache/tinkerpop/language/Traversal.java
index 4eef5cd..0e8f40a 100644
--- a/java/core/src/main/java/org/apache/tinkerpop/language/Traversal.java
+++ b/java/core/src/main/java/org/apache/tinkerpop/language/Traversal.java
@@ -71,6 +71,11 @@ public class Traversal<C, S, E> implements Iterator<E> {
         return (Traversal) this;
     }
 
+    public Traversal<C, S, E> filter(final Traversal<C, E, ?> filterTraversal)
{
+        this.bytecode.addInstruction(this.currentCoefficient, Symbols.FILTER, filterTraversal.bytecode);
+        return this;
+    }
+
     public Traversal<C, S, E> identity() {
         this.bytecode.addInstruction(this.currentCoefficient, Symbols.IDENTITY);
         return this;
diff --git a/java/core/src/main/java/org/apache/tinkerpop/language/__.java b/java/core/src/main/java/org/apache/tinkerpop/language/__.java
index 4c2b4ba..8c8a929 100644
--- a/java/core/src/main/java/org/apache/tinkerpop/language/__.java
+++ b/java/core/src/main/java/org/apache/tinkerpop/language/__.java
@@ -25,6 +25,10 @@ import org.apache.tinkerpop.machine.bytecode.Bytecode;
  */
 public class __ {
 
+    private __() {
+        // static class
+    }
+
     private static <C, S> Traversal<C, S, S> start() {
         return new Traversal<>(new Bytecode<>());
     }
diff --git a/java/core/src/main/java/org/apache/tinkerpop/machine/bytecode/BytecodeUtil.java
b/java/core/src/main/java/org/apache/tinkerpop/machine/bytecode/BytecodeUtil.java
index 10a9b2f..07f2f1d 100644
--- a/java/core/src/main/java/org/apache/tinkerpop/machine/bytecode/BytecodeUtil.java
+++ b/java/core/src/main/java/org/apache/tinkerpop/machine/bytecode/BytecodeUtil.java
@@ -21,6 +21,7 @@ package org.apache.tinkerpop.machine.bytecode;
 import org.apache.tinkerpop.language.Symbols;
 import org.apache.tinkerpop.machine.coefficients.Coefficient;
 import org.apache.tinkerpop.machine.functions.CFunction;
+import org.apache.tinkerpop.machine.functions.filter.FilterFilter;
 import org.apache.tinkerpop.machine.functions.filter.IdentityFilter;
 import org.apache.tinkerpop.machine.functions.filter.IsFilter;
 import org.apache.tinkerpop.machine.functions.flatMap.UnionFlatMap;
@@ -121,6 +122,8 @@ public final class BytecodeUtil {
         switch (op) {
             case Symbols.COUNT:
                 return new CountReduce<>(coefficient, labels);
+            case Symbols.FILTER:
+                return new FilterFilter<>(coefficient, labels, compile((Bytecode<C>)
instruction.args()[0]));
             case Symbols.IDENTITY:
                 return new IdentityFilter<>(coefficient, labels);
             case Symbols.INJECT:
diff --git a/java/core/src/main/java/org/apache/tinkerpop/machine/functions/flatMap/UnionFlatMap.java
b/java/core/src/main/java/org/apache/tinkerpop/machine/functions/filter/FilterFilter.java
similarity index 55%
copy from java/core/src/main/java/org/apache/tinkerpop/machine/functions/flatMap/UnionFlatMap.java
copy to java/core/src/main/java/org/apache/tinkerpop/machine/functions/filter/FilterFilter.java
index cb5502a..a4a18ec 100644
--- a/java/core/src/main/java/org/apache/tinkerpop/machine/functions/flatMap/UnionFlatMap.java
+++ b/java/core/src/main/java/org/apache/tinkerpop/machine/functions/filter/FilterFilter.java
@@ -16,70 +16,62 @@
  * specific language governing permissions and limitations
  * under the License.
  */
-package org.apache.tinkerpop.machine.functions.flatMap;
+package org.apache.tinkerpop.machine.functions.filter;
 
 import org.apache.tinkerpop.machine.coefficients.Coefficient;
 import org.apache.tinkerpop.machine.functions.AbstractFunction;
 import org.apache.tinkerpop.machine.functions.CFunction;
-import org.apache.tinkerpop.machine.functions.FlatMapFunction;
+import org.apache.tinkerpop.machine.functions.FilterFunction;
 import org.apache.tinkerpop.machine.functions.NestedFunction;
 import org.apache.tinkerpop.machine.processor.Processor;
 import org.apache.tinkerpop.machine.processor.ProcessorFactory;
 import org.apache.tinkerpop.machine.traversers.Traverser;
 import org.apache.tinkerpop.machine.traversers.TraverserFactory;
-import org.apache.tinkerpop.util.IteratorUtils;
-import org.apache.tinkerpop.util.MultiIterator;
 import org.apache.tinkerpop.util.StringFactory;
 
-import java.util.ArrayList;
-import java.util.Iterator;
+import java.util.Collections;
 import java.util.List;
 import java.util.Set;
 
 /**
  * @author Marko A. Rodriguez (http://markorodriguez.com)
  */
-public class UnionFlatMap<C, S, E> extends AbstractFunction<C, S, Iterator<E>>
implements FlatMapFunction<C, S, E>, NestedFunction.Branching<C, S, E> {
+public final class FilterFilter<C, S> extends AbstractFunction<C, S, S> implements
FilterFunction<C, S>, NestedFunction.Internal<C, S, S> {
 
-    private final List<List<CFunction<C>>> branchFunctions;
-    private transient List<Processor<C, S, E>> processors;
+    private final List<CFunction<C>> filterFunctions;
     private TraverserFactory<C> traverserFactory;
     private ProcessorFactory processorFactory;
 
-    public UnionFlatMap(final Coefficient<C> coefficient, final Set<String> labels,
final List<List<CFunction<C>>> branchFunctions) {
+    private transient Processor<C, S, S> processor;
+
+    public FilterFilter(final Coefficient<C> coefficient, final Set<String> labels,
final List<CFunction<C>> filterFunctions) {
         super(coefficient, labels);
-        this.branchFunctions = branchFunctions;
+        this.filterFunctions = filterFunctions;
     }
 
     @Override
-    public Iterator<E> apply(final Traverser<C, S> traverser) {
-        if (null == this.processors) {
-            this.processors = new ArrayList<>(this.branchFunctions.size());
-            for (final List<CFunction<C>> functions : this.branchFunctions) {
-                this.processors.add(processorFactory.mint(traverserFactory, functions));
-            }
-        }
-        final MultiIterator<E> iterator = new MultiIterator<>();
-        for (final Processor<C, S, E> processor : this.processors) {
-            processor.reset();
-            processor.addStart(traverser.clone());
-            iterator.addIterator(IteratorUtils.map(processor, Traverser::object));
-        }
-
-        return iterator;
+    public boolean test(final Traverser<C, S> traverser) {
+        if (null == this.processor)
+            this.processor = processorFactory.mint(traverserFactory, this.filterFunctions);
+        else
+            this.processor.reset();
+        this.processor.addStart(traverser);
+        return this.processor.hasNext();
     }
 
+    @Override
     public void setProcessor(final TraverserFactory<C> traverserFactory, final ProcessorFactory
processorFactory) {
         this.traverserFactory = traverserFactory;
         this.processorFactory = processorFactory;
     }
 
+    @Override
     public List<List<CFunction<C>>> getFunctions() {
-        return this.branchFunctions;
+        return Collections.singletonList(this.filterFunctions);
     }
 
     @Override
     public String toString() {
-        return StringFactory.makeFunctionString(this, this.branchFunctions.toArray());
+        return StringFactory.makeFunctionString(this, this.filterFunctions.toArray());
     }
 }
diff --git a/java/core/src/main/java/org/apache/tinkerpop/machine/functions/filter/IdentityFilter.java
b/java/core/src/main/java/org/apache/tinkerpop/machine/functions/filter/IdentityFilter.java
index e630242..4d479ca 100644
--- a/java/core/src/main/java/org/apache/tinkerpop/machine/functions/filter/IdentityFilter.java
+++ b/java/core/src/main/java/org/apache/tinkerpop/machine/functions/filter/IdentityFilter.java
@@ -28,7 +28,7 @@ import java.util.Set;
 /**
  * @author Marko A. Rodriguez (http://markorodriguez.com)
  */
-public class IdentityFilter<C, S> extends AbstractFunction<C, S, S> implements
FilterFunction<C, S> {
+public final class IdentityFilter<C, S> extends AbstractFunction<C, S, S> implements
FilterFunction<C, S> {
 
     public IdentityFilter(final Coefficient<C> coefficient, final Set<String>
labels) {
         super(coefficient, labels);
diff --git a/java/core/src/main/java/org/apache/tinkerpop/machine/functions/filter/IsFilter.java
b/java/core/src/main/java/org/apache/tinkerpop/machine/functions/filter/IsFilter.java
index cbdd8ed..129f40f 100644
--- a/java/core/src/main/java/org/apache/tinkerpop/machine/functions/filter/IsFilter.java
+++ b/java/core/src/main/java/org/apache/tinkerpop/machine/functions/filter/IsFilter.java
@@ -21,7 +21,6 @@ package org.apache.tinkerpop.machine.functions.filter;
 import org.apache.tinkerpop.machine.coefficients.Coefficient;
 import org.apache.tinkerpop.machine.functions.AbstractFunction;
 import org.apache.tinkerpop.machine.functions.FilterFunction;
-import org.apache.tinkerpop.machine.traversers.CompleteTraverser;
 import org.apache.tinkerpop.machine.traversers.Traverser;
 import org.apache.tinkerpop.util.StringFactory;
 
@@ -30,7 +29,7 @@ import java.util.Set;
 /**
  * @author Marko A. Rodriguez (http://markorodriguez.com)
  */
-public class IsFilter<C, S> extends AbstractFunction<C, S, S> implements FilterFunction<C,
S> {
+public final class IsFilter<C, S> extends AbstractFunction<C, S, S> implements
FilterFunction<C, S> {
 
     private S object;
 
diff --git a/java/core/src/main/java/org/apache/tinkerpop/machine/functions/flatMap/UnionFlatMap.java
b/java/core/src/main/java/org/apache/tinkerpop/machine/functions/flatMap/UnionFlatMap.java
index cb5502a..8a5adef 100644
--- a/java/core/src/main/java/org/apache/tinkerpop/machine/functions/flatMap/UnionFlatMap.java
+++ b/java/core/src/main/java/org/apache/tinkerpop/machine/functions/flatMap/UnionFlatMap.java
@@ -39,8 +39,8 @@ import java.util.Set;
 /**
  * @author Marko A. Rodriguez (http://markorodriguez.com)
  */
-public class UnionFlatMap<C, S, E> extends AbstractFunction<C, S, Iterator<E>>
implements FlatMapFunction<C, S, E>, NestedFunction.Branching<C, S, E> {
-
+public final class UnionFlatMap<C, S, E> extends AbstractFunction<C, S, Iterator<E>>
implements FlatMapFunction<C, S, E>, NestedFunction.Branching<C, S, E> {
+    // TODO: we need a branch/ package as these need to be NOT flatMap functions.
     private final List<List<CFunction<C>>> branchFunctions;
     private transient List<Processor<C, S, E>> processors;
     private TraverserFactory<C> traverserFactory;
diff --git a/java/machine/pipes/src/test/java/org/apache/tinkerpop/machine/pipes/PipesTest.java
b/java/machine/pipes/src/test/java/org/apache/tinkerpop/machine/pipes/PipesTest.java
index 1431049..49a802a 100644
--- a/java/machine/pipes/src/test/java/org/apache/tinkerpop/machine/pipes/PipesTest.java
+++ b/java/machine/pipes/src/test/java/org/apache/tinkerpop/machine/pipes/PipesTest.java
@@ -53,5 +53,10 @@ public class PipesTest {
         System.out.println(TraversalUtil.getBytecode(traversal));
         System.out.println(traversal);
         System.out.println(traversal.toList());
+        System.out.println("\n----------\n");
+        traversal = g.inject(7L).incr().filter(__.<Long>incr().incr().is(9L));
+        System.out.println(TraversalUtil.getBytecode(traversal));
+        System.out.println(traversal);
+        System.out.println(traversal.toList());
     }
 }


Mime
View raw message