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 2015/05/15 22:49:55 UTC

incubator-tinkerpop git commit: fixed up docs around where() and has(). Added ConjunctionP as an abtract class to AndP and OrP. TraveralBiPredicate is now cloneable.

Repository: incubator-tinkerpop
Updated Branches:
  refs/heads/master c901da1f6 -> 04e45b304


fixed up docs around where() and has(). Added ConjunctionP as an abtract class to AndP and OrP. TraveralBiPredicate is now cloneable.


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

Branch: refs/heads/master
Commit: 04e45b304770e88e48cf467e2b5365cd468f75fe
Parents: c901da1
Author: Marko A. Rodriguez <ok...@gmail.com>
Authored: Fri May 15 14:49:48 2015 -0600
Committer: Marko A. Rodriguez <ok...@gmail.com>
Committed: Fri May 15 14:49:48 2015 -0600

----------------------------------------------------------------------
 docs/src/the-traversal.asciidoc                 |  3 +
 .../traversal/lambda/TraversalBiPredicate.java  |  6 +-
 .../traversal/step/filter/WhereStep.java        |  9 ++-
 .../apache/tinkerpop/gremlin/structure/P.java   | 32 +++++++-
 .../tinkerpop/gremlin/structure/util/AndP.java  | 34 +-------
 .../gremlin/structure/util/ConjunctionP.java    | 84 ++++++++++++++++++++
 .../tinkerpop/gremlin/structure/util/OrP.java   | 53 ++++++++++++
 7 files changed, 180 insertions(+), 41 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/04e45b30/docs/src/the-traversal.asciidoc
----------------------------------------------------------------------
diff --git a/docs/src/the-traversal.asciidoc b/docs/src/the-traversal.asciidoc
index daa7e83..98cef72 100644
--- a/docs/src/the-traversal.asciidoc
+++ b/docs/src/the-traversal.asciidoc
@@ -1446,12 +1446,15 @@ g.V().where(out('created')).values('name') <1>
 g.V().out('knows').where(out('created')).values('name') <2>
 g.V().where(out('created').count().is(gte(2L))).values('name') <3>
 g.V().where(out('knows').where(out('created'))).values('name') <4>
+g.V().where(not(out('created'))).where(in('knows')).values('name') <5>
 ----
 
 <1> What are the names of the people who have created a project?
 <2> What are the names of the people that are known by someone one and have created a project?
 <3> What are the names of the people how have created two or more projects?
 <4> What are the names of the people who know someone that has created a project? (This only works in OLTP -- see the `WARNING` below)
+<5> What are the names of the people who have not created anything, but are known by someone?
+
 
 WARNING: The anonymous traversal of `where()` processes the current object "locally". In OLAP, where the atomic unit of computing is the the vertex and its local "star graph," it is important that the anonymous traversal does not leave the confines of the vertex's star graph. In other words, it can not traverse to an adjacent vertex's properties or edges.
 

http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/04e45b30/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/lambda/TraversalBiPredicate.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/lambda/TraversalBiPredicate.java b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/lambda/TraversalBiPredicate.java
index ec4cca5..ef61afa 100644
--- a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/lambda/TraversalBiPredicate.java
+++ b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/lambda/TraversalBiPredicate.java
@@ -29,7 +29,7 @@ import java.util.function.BiPredicate;
 /**
  * @author Marko A. Rodriguez (http://markorodriguez.com)
  */
-public class TraversalBiPredicate<S, E> implements BiPredicate<S, E> {
+public class TraversalBiPredicate<S, E> implements BiPredicate<S, E>, Cloneable {
 
     private Traversal.Admin<S, E> traversal;
     private final boolean negate;
@@ -61,10 +61,6 @@ public class TraversalBiPredicate<S, E> implements BiPredicate<S, E> {
         return new TraversalBiPredicate<>(this.traversal.clone(), !this.negate);
     }
 
-    public boolean isNegated() {
-        return this.negate;
-    }
-
     @Override
     public TraversalBiPredicate<S, E> clone() {
         try {

http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/04e45b30/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/step/filter/WhereStep.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/step/filter/WhereStep.java b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/step/filter/WhereStep.java
index 3b545f3..8336bc8 100644
--- a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/step/filter/WhereStep.java
+++ b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/step/filter/WhereStep.java
@@ -102,10 +102,13 @@ public final class WhereStep<S> extends FilterStep<S> implements TraversalParent
     @Override
     public WhereStep<S> clone() {
         final WhereStep<S> clone = (WhereStep<S>) super.clone();
-        if (this.predicate.getBiPredicate() instanceof TraversalBiPredicate)
-            clone.predicate = ((TraversalBiPredicate) this.predicate.getBiPredicate()).isNegated() ?
+        clone.predicate = this.predicate.clone();
+        if (clone.predicate.getBiPredicate() instanceof TraversalBiPredicate)
+            clone.integrateChild(((TraversalBiPredicate) clone.predicate.getBiPredicate()).getTraversal());
+        /*if (this.predicate.getBiPredicate() instanceof TraversalBiPredicate)
+            clone.predicate = false ?
                     P.not(((TraversalBiPredicate) this.predicate.getBiPredicate()).getTraversal().clone()) :
-                    P.traversal(((TraversalBiPredicate) this.predicate.getBiPredicate()).getTraversal().clone());
+                    P.traversal(((TraversalBiPredicate) this.predicate.getBiPredicate()).getTraversal().clone()); */
         return clone;
     }
 

http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/04e45b30/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/P.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/P.java b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/P.java
index f793159..29da85c 100644
--- a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/P.java
+++ b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/P.java
@@ -24,6 +24,7 @@ package org.apache.tinkerpop.gremlin.structure;
 import org.apache.tinkerpop.gremlin.process.traversal.Traversal;
 import org.apache.tinkerpop.gremlin.process.traversal.lambda.TraversalBiPredicate;
 import org.apache.tinkerpop.gremlin.structure.util.AndP;
+import org.apache.tinkerpop.gremlin.structure.util.OrP;
 
 import java.io.Serializable;
 import java.util.Arrays;
@@ -34,9 +35,9 @@ import java.util.function.Predicate;
 /**
  * @author Marko A. Rodriguez (http://markorodriguez.com)
  */
-public class P<V> implements Predicate<V>, Serializable {
+public class P<V> implements Predicate<V>, Serializable, Cloneable {
 
-    private final BiPredicate<V, V> biPredicate;
+    private BiPredicate<V, V> biPredicate;
     private final V value;
 
     public P(final BiPredicate<V, V> biPredicate, final V value) {
@@ -65,6 +66,7 @@ public class P<V> implements Predicate<V>, Serializable {
     @Override
     public boolean equals(final Object other) {
         return other instanceof P &&
+                ((P)other).getClass().equals(this.getClass()) &&
                 ((P) other).getBiPredicate().equals(this.biPredicate) &&
                 ((((P) other).getValue() == null && this.getValue() == null) || ((P) other).getValue().equals(this.getValue()));
     }
@@ -79,6 +81,14 @@ public class P<V> implements Predicate<V>, Serializable {
         return new P<>(this.biPredicate.negate(), this.value);
     }
 
+    /*public P<V> and(final Traversal<?,?> traversal) {
+        return this.and((Predicate)P.traversal(traversal));
+    }
+
+    public P<V> or(final Traversal<?,?> traversal) {
+        return this.or((Predicate)P.traversal(traversal));
+    }*/
+
     @Override
     public P<V> and(final Predicate<? super V> predicate) {
         if (!(predicate instanceof P))
@@ -86,6 +96,24 @@ public class P<V> implements Predicate<V>, Serializable {
         return new AndP<>(this, (P<V>) predicate);
     }
 
+    @Override
+    public P<V> or(final Predicate<? super V> predicate) {
+        if (!(predicate instanceof P))
+            throw new IllegalArgumentException("Only P predicates can be or'd together");
+        return new OrP<>(this, (P<V>) predicate);
+    }
+
+    public P<V> clone() {
+        try {
+            final P<V> clone = (P<V>) super.clone();
+            if (this.biPredicate instanceof TraversalBiPredicate)
+                clone.biPredicate = ((TraversalBiPredicate) this.biPredicate).clone();
+            return clone;
+        } catch (final CloneNotSupportedException e) {
+            throw new IllegalStateException(e.getMessage(), e);
+        }
+    }
+
     //////////////// statics
 
     public static <V> P<V> eq(final V value) {

http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/04e45b30/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/AndP.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/AndP.java b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/AndP.java
index b096621..e9e14d7 100644
--- a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/AndP.java
+++ b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/AndP.java
@@ -23,33 +23,16 @@ package org.apache.tinkerpop.gremlin.structure.util;
 
 import org.apache.tinkerpop.gremlin.structure.P;
 
-import java.util.ArrayList;
-import java.util.Collections;
 import java.util.List;
 import java.util.function.Predicate;
 
 /**
  * @author Marko A. Rodriguez (http://markorodriguez.com)
  */
-public final class AndP<V> extends P<V> {
-
-    private List<P<V>> predicates;
+public final class AndP<V> extends ConjunctionP<V> {
 
     public AndP(final P<V> predicate, final P<V>... predicates) {
-        super(null, null);
-        this.predicates = new ArrayList<>();
-        this.predicates.add(predicate);
-        for (final P<V> p : predicates) {
-            this.predicates.add(p);
-        }
-    }
-
-    public List<P<V>> getPredicates() {
-        return Collections.unmodifiableList(this.predicates);
-    }
-
-    public void addPredicate(final P<V> predicate) {
-        this.predicates.add(predicate);
+        super(predicate, predicates);
     }
 
     @Override
@@ -65,18 +48,7 @@ public final class AndP<V> extends P<V> {
     public P<V> and(final Predicate<? super V> predicate) {
         if (!(predicate instanceof P))
             throw new IllegalArgumentException("Only P predicates can be and'd together");
-        this.predicates.add((P<V>) predicate);
+        this.predicates.add((P<V>) predicate);   // TODO: clone and add?
         return this;
     }
-
-    @Override
-    public P<V> negate() {
-        final List<P<V>> negated = new ArrayList<>();
-        for (final P<V> predicate : this.predicates) {
-            negated.add(predicate.negate());
-        }
-        this.predicates = negated;
-        return this;
-    }
-
 }

http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/04e45b30/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/ConjunctionP.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/ConjunctionP.java b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/ConjunctionP.java
new file mode 100644
index 0000000..0a10085
--- /dev/null
+++ b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/ConjunctionP.java
@@ -0,0 +1,84 @@
+/*
+ *
+ *  * 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.gremlin.structure.util;
+
+import org.apache.tinkerpop.gremlin.structure.P;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.function.Predicate;
+
+/**
+ * @author Marko A. Rodriguez (http://markorodriguez.com)
+ */
+public abstract class ConjunctionP<V> extends P<V> {
+
+    protected List<P<V>> predicates;
+
+    public ConjunctionP(final P<V> predicate, final P<V>... predicates) {
+        super(null, null);
+        this.predicates = new ArrayList<>();
+        this.predicates.add(predicate);
+        for (final P<V> p : predicates) {
+            this.predicates.add(p);
+        }
+    }
+
+    public List<P<V>> getPredicates() {
+        return Collections.unmodifiableList(this.predicates);
+    }
+
+    @Override
+    public P<V> negate() {
+        final List<P<V>> negated = new ArrayList<>();
+        for (final P<V> predicate : this.predicates) {
+            negated.add(predicate.negate());
+        }
+        this.predicates = negated;
+        return this;
+    }
+
+    @Override
+    public ConjunctionP<V> clone() {
+        final ConjunctionP<V> clone = (ConjunctionP<V>) super.clone();
+        clone.predicates = new ArrayList<>();
+        for (final P<V> p : this.predicates) {
+            clone.predicates.add(p.clone());
+        }
+        return clone;
+    }
+
+    @Override
+    public P<V> and(final Predicate<? super V> predicate) {
+        if (!(predicate instanceof P))
+            throw new IllegalArgumentException("Only P predicates can be and'd together");
+        return new AndP<V>(this, (P<V>) predicate);
+    }
+
+    @Override
+    public P<V> or(final Predicate<? super V> predicate) {
+        if (!(predicate instanceof P))
+            throw new IllegalArgumentException("Only P predicates can be or'd together");
+        return new OrP<V>(this, (P<V>) predicate);
+    }
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/04e45b30/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/OrP.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/OrP.java b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/OrP.java
new file mode 100644
index 0000000..ff74a53
--- /dev/null
+++ b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/OrP.java
@@ -0,0 +1,53 @@
+/*
+ *
+ *  * 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.gremlin.structure.util;
+
+import org.apache.tinkerpop.gremlin.structure.P;
+
+import java.util.function.Predicate;
+
+/**
+ * @author Marko A. Rodriguez (http://markorodriguez.com)
+ */
+public final class OrP<V> extends ConjunctionP<V> {
+
+    public OrP(final P<V> predicate, final P<V>... predicates) {
+        super(predicate, predicates);
+    }
+
+    @Override
+    public boolean test(final V v) {
+        for (final P<V> predicate : this.predicates) {
+            if (predicate.test(v))
+                return true;
+        }
+        return false;
+    }
+
+    @Override
+    public P<V> or(final Predicate<? super V> predicate) {
+        if (!(predicate instanceof P))
+            throw new IllegalArgumentException("Only P predicates can be or'd together");
+        this.predicates.add((P<V>) predicate);   // TODO: clone and add?
+        return this;
+    }
+}
\ No newline at end of file