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/02/11 01:11:39 UTC

incubator-tinkerpop git commit: implemented ConjunctionTree which supports getting the a complex ConjunctionStep AND, OR nesting in terms of a tree of AND, OR, HasContainers... Fixed #554.

Repository: incubator-tinkerpop
Updated Branches:
  refs/heads/master 4db7d7281 -> 3155d40be


implemented ConjunctionTree which supports getting the a complex ConjunctionStep AND,OR nesting in terms of a tree of AND,OR,HasContainers... Fixed #554.


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

Branch: refs/heads/master
Commit: 3155d40bea9a0781dd2c28c228f5a28846fe69e4
Parents: 4db7d72
Author: Marko A. Rodriguez <ok...@gmail.com>
Authored: Tue Feb 10 17:11:37 2015 -0700
Committer: Marko A. Rodriguez <ok...@gmail.com>
Committed: Tue Feb 10 17:11:37 2015 -0700

----------------------------------------------------------------------
 .../traversal/step/filter/ConjunctionStep.java  | 83 +++++++++++++++--
 .../process/graph/step/util/TreeTest.java       | 97 --------------------
 .../step/filter/ConjunctionStepTest.java        | 63 +++++++++++++
 .../gremlin/process/graph/util/TreeTest.java    | 96 +++++++++++++++++++
 4 files changed, 234 insertions(+), 105 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/3155d40b/gremlin-core/src/main/java/com/tinkerpop/gremlin/process/graph/traversal/step/filter/ConjunctionStep.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/main/java/com/tinkerpop/gremlin/process/graph/traversal/step/filter/ConjunctionStep.java b/gremlin-core/src/main/java/com/tinkerpop/gremlin/process/graph/traversal/step/filter/ConjunctionStep.java
index 82989b9..770408a 100644
--- a/gremlin-core/src/main/java/com/tinkerpop/gremlin/process/graph/traversal/step/filter/ConjunctionStep.java
+++ b/gremlin-core/src/main/java/com/tinkerpop/gremlin/process/graph/traversal/step/filter/ConjunctionStep.java
@@ -30,6 +30,7 @@ import com.tinkerpop.gremlin.process.traverser.TraverserRequirement;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collections;
+import java.util.Iterator;
 import java.util.List;
 import java.util.NoSuchElementException;
 import java.util.Set;
@@ -95,18 +96,22 @@ public abstract class ConjunctionStep<S> extends AbstractStep<S, S> implements T
         return TraversalHelper.makeStepString(this, this.conjunctionTraversals);
     }
 
-    /*public List<HasContainer> getHasContainers() {
-        final List<HasContainer> hasContainers = new ArrayList<>();
+    public boolean isConjunctionHasTree() {
         for (final Traversal.Admin<S, ?> conjunctionTraversal : this.conjunctionTraversals) {
             for (final Step<?, ?> step : conjunctionTraversal.getSteps()) {
-                if (step instanceof HasStep) {
-                    hasContainers.addAll(((HasStep) step).getHasContainers());
-                } else if(step instanceof TraversalParent) {
-
-                }
+                if (step instanceof ConjunctionStep) {
+                    if (!((ConjunctionStep) step).isConjunctionHasTree())
+                        return false;
+                } else if (!(step instanceof HasStep))
+                    return false;
             }
         }
-    }*/
+        return true;
+    }
+
+    public ConjunctionTree getConjunctionHasTree() {
+        return new ConjunctionTree(this);
+    }
 
 
     ////////
@@ -123,4 +128,66 @@ public abstract class ConjunctionStep<S> extends AbstractStep<S, S> implements T
         }
     }
 
+    ////////
+
+    public static class ConjunctionTree implements Iterable<ConjunctionTree.Entry> {
+
+        private List<Entry> tree = new ArrayList<>();
+        private boolean isAnd;
+
+        public ConjunctionTree(final ConjunctionStep<?> conjunctionStep) {
+            this.isAnd = conjunctionStep.isAnd;
+            for (final Traversal.Admin<?, ?> conjunctionTraversal : conjunctionStep.conjunctionTraversals) {
+                for (final Step<?, ?> step : conjunctionTraversal.getSteps()) {
+                    if (step instanceof HasStep) {
+                        (((HasStep<?>) step).getHasContainers()).forEach(container -> this.tree.add(new Entry(HasContainer.class, container)));
+                    } else if (step instanceof ConjunctionStep) {
+                        tree.add(new Entry(ConjunctionTree.class, ((ConjunctionStep) step).getConjunctionHasTree()));
+                    } else {
+                        throw new IllegalStateException("This conjunction supports more complex steps than HasStep");
+                    }
+                }
+            }
+        }
+
+        @Override
+        public String toString() {
+            return (this.isAnd ? "and" : "or") + this.tree.toString();
+        }
+
+        public boolean isAnd() {
+            return this.isAnd;
+        }
+
+        @Override
+        public Iterator<Entry> iterator() {
+            return this.tree.iterator();
+        }
+
+        public static class Entry {
+            private Class entryClass;
+            private Object entryValue;
+
+            public Entry(final Class entryClass, final Object entryValue) {
+                this.entryClass = entryClass;
+                this.entryValue = entryValue;
+            }
+
+            public <V> V getValue() {
+                return (V) this.entryValue;
+            }
+
+            public boolean isHasContainer() {
+                return entryClass.equals(HasContainer.class);
+            }
+
+            public boolean isConjunctionTree() {
+                return entryClass.equals(ConjunctionTree.class);
+            }
+
+            public String toString() {
+                return this.entryValue.toString();
+            }
+        }
+    }
 }

http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/3155d40b/gremlin-test/src/test/java/com/tinkerpop/gremlin/process/graph/step/util/TreeTest.java
----------------------------------------------------------------------
diff --git a/gremlin-test/src/test/java/com/tinkerpop/gremlin/process/graph/step/util/TreeTest.java b/gremlin-test/src/test/java/com/tinkerpop/gremlin/process/graph/step/util/TreeTest.java
deleted file mode 100644
index c50f0cb..0000000
--- a/gremlin-test/src/test/java/com/tinkerpop/gremlin/process/graph/step/util/TreeTest.java
+++ /dev/null
@@ -1,97 +0,0 @@
-/*
- * 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 com.tinkerpop.gremlin.process.graph.step.util;
-
-import com.tinkerpop.gremlin.process.graph.util.Tree;
-import org.junit.Test;
-
-import java.util.AbstractMap;
-import java.util.Arrays;
-import java.util.Map;
-
-import static org.junit.Assert.*;
-
-/**
- * @author Marko A. Rodriguez (http://markorodriguez.com)
- */
-public class TreeTest {
-
-    @Test
-    public void shouldProvideValidDepths() {
-        Tree<String> tree = new Tree<String>();
-        tree.put("marko", new Tree<String>(TreeTest.createTree("a", new Tree<String>("a1", "a2")), TreeTest.createTree("b", new Tree<String>("b1", "b2", "b3"))));
-        tree.put("josh", new Tree<String>("1", "2"));
-
-        assertEquals(0, tree.getObjectsAtDepth(0).size());
-        assertEquals(2, tree.getObjectsAtDepth(1).size());
-        assertEquals(4, tree.getObjectsAtDepth(2).size());
-        assertEquals(5, tree.getObjectsAtDepth(3).size());
-        assertEquals(0, tree.getObjectsAtDepth(4).size());
-        assertEquals(0, tree.getObjectsAtDepth(5).size());
-
-        assertEquals(2, tree.get("josh").size());
-        assertEquals(0, tree.get("marko").get("b").get("b1").size());
-        assertEquals(3, tree.get("marko").get("b").size());
-        assertNull(tree.get("marko").get("c"));
-    }
-
-    @Test
-    public void shouldProvideValidLeaves() {
-        Tree<String> tree = new Tree<String>();
-        tree.put("marko", new Tree<String>(TreeTest.createTree("a", new Tree<String>("a1", "a2")), TreeTest.createTree("b", new Tree<String>("b1", "b2", "b3"))));
-        tree.put("josh", new Tree<String>("1", "2"));
-
-        assertEquals(7, tree.getLeafTrees().size());
-        for (Tree<String> t : tree.getLeafTrees()) {
-            assertEquals(1, t.keySet().size());
-            final String key = t.keySet().iterator().next();
-            assertTrue(Arrays.asList("a1", "a2", "b1", "b2", "b3", "1", "2").contains(key));
-        }
-
-        assertEquals(7, tree.getLeafObjects().size());
-        for (String s : tree.getLeafObjects()) {
-            assertTrue(Arrays.asList("a1", "a2", "b1", "b2", "b3", "1", "2").contains(s));
-        }
-    }
-
-    @Test
-    public void shouldMergeTreesCorrectly() {
-        Tree<String> tree1 = new Tree<>();
-        tree1.put("1", new Tree<String>(TreeTest.createTree("1_1", new Tree<String>("1_1_1")), TreeTest.createTree("1_2", new Tree<String>("1_2_1"))));
-        Tree<String> tree2 = new Tree<>();
-        tree2.put("1", new Tree<String>(TreeTest.createTree("1_1", new Tree<String>("1_1_1")), TreeTest.createTree("1_2", new Tree<String>("1_2_2"))));
-
-        Tree<String> mergeTree = new Tree<>();
-        mergeTree.addTree(tree1);
-        mergeTree.addTree(tree2);
-
-        assertEquals(1, mergeTree.size());
-        assertEquals(0, mergeTree.getObjectsAtDepth(0).size());
-        assertEquals(1, mergeTree.getObjectsAtDepth(1).size());
-        assertEquals(2, mergeTree.getObjectsAtDepth(2).size());
-        assertEquals(3, mergeTree.getObjectsAtDepth(3).size());
-        assertTrue(mergeTree.getObjectsAtDepth(3).contains("1_1_1"));
-        assertTrue(mergeTree.getObjectsAtDepth(3).contains("1_2_1"));
-        assertTrue(mergeTree.getObjectsAtDepth(3).contains("1_2_2"));
-    }
-
-    private static <T> Map.Entry<T, Tree<T>> createTree(T key, Tree<T> tree) {
-        return new AbstractMap.SimpleEntry<>(key, tree);
-    }
-}

http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/3155d40b/gremlin-test/src/test/java/com/tinkerpop/gremlin/process/graph/traversal/step/filter/ConjunctionStepTest.java
----------------------------------------------------------------------
diff --git a/gremlin-test/src/test/java/com/tinkerpop/gremlin/process/graph/traversal/step/filter/ConjunctionStepTest.java b/gremlin-test/src/test/java/com/tinkerpop/gremlin/process/graph/traversal/step/filter/ConjunctionStepTest.java
new file mode 100644
index 0000000..b8b40e1
--- /dev/null
+++ b/gremlin-test/src/test/java/com/tinkerpop/gremlin/process/graph/traversal/step/filter/ConjunctionStepTest.java
@@ -0,0 +1,63 @@
+/*
+ * 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 com.tinkerpop.gremlin.process.graph.traversal.step.filter;
+
+import com.tinkerpop.gremlin.process.graph.traversal.GraphTraversal;
+import com.tinkerpop.gremlin.process.graph.util.HasContainer;
+import com.tinkerpop.gremlin.structure.Compare;
+import org.junit.Test;
+
+import java.util.Iterator;
+
+import static com.tinkerpop.gremlin.process.graph.traversal.__.and;
+import static com.tinkerpop.gremlin.process.graph.traversal.__.has;
+import static org.junit.Assert.*;
+
+/**
+ * @author Marko A. Rodriguez (http://markorodriguez.com)
+ */
+public class ConjunctionStepTest {
+
+    @Test
+    public void shouldGetHasContainers() {
+        final GraphTraversal.Admin<?, ?> traversal = and(has("name"), has("age", Compare.gt, 30).or(has("lang", "java"))).asAdmin();
+        assertTrue(((ConjunctionStep) traversal.getStartStep()).isConjunctionHasTree());
+        final ConjunctionStep.ConjunctionTree conjunctionTree = (((ConjunctionStep<?>) traversal.getStartStep()).getConjunctionHasTree());
+        final Iterator<ConjunctionStep.ConjunctionTree.Entry> iterator = conjunctionTree.iterator();
+        //System.out.println(conjunctionTree);
+        ConjunctionStep.ConjunctionTree.Entry entry = iterator.next();
+        assertTrue(entry.isHasContainer());
+        assertEquals("name", entry.<HasContainer>getValue().key);
+        //
+        entry = iterator.next();
+        assertTrue(entry.isHasContainer());
+        assertEquals("age", entry.<HasContainer>getValue().key);
+        assertEquals(Compare.gt, entry.<HasContainer>getValue().predicate);
+        assertEquals(30, entry.<HasContainer>getValue().value);
+        //
+        entry = iterator.next();
+        assertTrue(entry.isConjunctionTree());
+        assertFalse(entry.<ConjunctionStep.ConjunctionTree>getValue().isAnd());
+        entry = entry.<ConjunctionStep.ConjunctionTree>getValue().iterator().next();
+        assertTrue(entry.isHasContainer());
+        assertEquals("lang", entry.<HasContainer>getValue().key);
+        assertEquals(Compare.eq, entry.<HasContainer>getValue().predicate);
+        assertEquals("java", entry.<HasContainer>getValue().value);
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/3155d40b/gremlin-test/src/test/java/com/tinkerpop/gremlin/process/graph/util/TreeTest.java
----------------------------------------------------------------------
diff --git a/gremlin-test/src/test/java/com/tinkerpop/gremlin/process/graph/util/TreeTest.java b/gremlin-test/src/test/java/com/tinkerpop/gremlin/process/graph/util/TreeTest.java
new file mode 100644
index 0000000..1d3f954
--- /dev/null
+++ b/gremlin-test/src/test/java/com/tinkerpop/gremlin/process/graph/util/TreeTest.java
@@ -0,0 +1,96 @@
+/*
+ * 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 com.tinkerpop.gremlin.process.graph.util;
+
+import org.junit.Test;
+
+import java.util.AbstractMap;
+import java.util.Arrays;
+import java.util.Map;
+
+import static org.junit.Assert.*;
+
+/**
+ * @author Marko A. Rodriguez (http://markorodriguez.com)
+ */
+public class TreeTest {
+
+    @Test
+    public void shouldProvideValidDepths() {
+        Tree<String> tree = new Tree<String>();
+        tree.put("marko", new Tree<String>(TreeTest.createTree("a", new Tree<String>("a1", "a2")), TreeTest.createTree("b", new Tree<String>("b1", "b2", "b3"))));
+        tree.put("josh", new Tree<String>("1", "2"));
+
+        assertEquals(0, tree.getObjectsAtDepth(0).size());
+        assertEquals(2, tree.getObjectsAtDepth(1).size());
+        assertEquals(4, tree.getObjectsAtDepth(2).size());
+        assertEquals(5, tree.getObjectsAtDepth(3).size());
+        assertEquals(0, tree.getObjectsAtDepth(4).size());
+        assertEquals(0, tree.getObjectsAtDepth(5).size());
+
+        assertEquals(2, tree.get("josh").size());
+        assertEquals(0, tree.get("marko").get("b").get("b1").size());
+        assertEquals(3, tree.get("marko").get("b").size());
+        assertNull(tree.get("marko").get("c"));
+    }
+
+    @Test
+    public void shouldProvideValidLeaves() {
+        Tree<String> tree = new Tree<String>();
+        tree.put("marko", new Tree<String>(TreeTest.createTree("a", new Tree<String>("a1", "a2")), TreeTest.createTree("b", new Tree<String>("b1", "b2", "b3"))));
+        tree.put("josh", new Tree<String>("1", "2"));
+
+        assertEquals(7, tree.getLeafTrees().size());
+        for (Tree<String> t : tree.getLeafTrees()) {
+            assertEquals(1, t.keySet().size());
+            final String key = t.keySet().iterator().next();
+            assertTrue(Arrays.asList("a1", "a2", "b1", "b2", "b3", "1", "2").contains(key));
+        }
+
+        assertEquals(7, tree.getLeafObjects().size());
+        for (String s : tree.getLeafObjects()) {
+            assertTrue(Arrays.asList("a1", "a2", "b1", "b2", "b3", "1", "2").contains(s));
+        }
+    }
+
+    @Test
+    public void shouldMergeTreesCorrectly() {
+        Tree<String> tree1 = new Tree<>();
+        tree1.put("1", new Tree<String>(TreeTest.createTree("1_1", new Tree<String>("1_1_1")), TreeTest.createTree("1_2", new Tree<String>("1_2_1"))));
+        Tree<String> tree2 = new Tree<>();
+        tree2.put("1", new Tree<String>(TreeTest.createTree("1_1", new Tree<String>("1_1_1")), TreeTest.createTree("1_2", new Tree<String>("1_2_2"))));
+
+        Tree<String> mergeTree = new Tree<>();
+        mergeTree.addTree(tree1);
+        mergeTree.addTree(tree2);
+
+        assertEquals(1, mergeTree.size());
+        assertEquals(0, mergeTree.getObjectsAtDepth(0).size());
+        assertEquals(1, mergeTree.getObjectsAtDepth(1).size());
+        assertEquals(2, mergeTree.getObjectsAtDepth(2).size());
+        assertEquals(3, mergeTree.getObjectsAtDepth(3).size());
+        assertTrue(mergeTree.getObjectsAtDepth(3).contains("1_1_1"));
+        assertTrue(mergeTree.getObjectsAtDepth(3).contains("1_2_1"));
+        assertTrue(mergeTree.getObjectsAtDepth(3).contains("1_2_2"));
+    }
+
+    private static <T> Map.Entry<T, Tree<T>> createTree(T key, Tree<T> tree) {
+        return new AbstractMap.SimpleEntry<>(key, tree);
+    }
+}