Permalink
Browse files

added UniquePathFilterPipe. If a path repeats, it is filtered out.

  • Loading branch information...
1 parent f34d235 commit ad4476f27aae10fbf2e22a8a625e1f240f8fb7a9 @okram okram committed Feb 21, 2011
View
21 CHANGELOG.textile
@@ -13,21 +13,26 @@ bc. <repository>
h3. Version 0.3.1 (Mario -- NOT OFFICIALLY RELEASED YET)
-bc. <dependency>
+```xml
+<dependency>
<groupId>com.tinkerpop</groupId>
<artifactId>pipes</artifactId>
<version>0.3.1</version>
</dependency>
+```
* Added @VertexEdgeLabelFilterPipe@ to reflect Blueprints @Vertex@ API update
+* Added @UniquePathFilterPipe@ to filter paths that have visited the same object twice
h3. Version 0.3 (Mario -- January 22, 2011)
-bc. <dependency>
+```xml
+<dependency>
<groupId>com.tinkerpop</groupId>
<artifactId>pipes</artifactId>
<version>0.3</version>
</dependency>
+```
* Added a general @toString()@ model to articulate pipe construction
* Redesigned path-model to where @enablePaths()@ no longer required
@@ -44,11 +49,13 @@ bc. <dependency>
h3. Version 0.2 (Clogged -- December 14, 2010)
-bc. <dependency>
+```xml
+<dependency>
<groupId>com.tinkerpop</groupId>
<artifactId>pipes</artifactId>
<version>0.2</version>
</dependency>
+```
* Added path functionality to get the path pipe traversed
** Added @PathSequence@ path utility
@@ -61,20 +68,24 @@ bc. <dependency>
h3. Version 0.1.1 (Leaky -- October 28, 2010)
-bc. <dependency>
+```xml
+<dependency>
<groupId>com.tinkerpop</groupId>
<artifactId>pipes</artifactId>
<version>0.1.1</version>
</dependency>
+```
* Fixed 'reset pipe' issue in @FutureFilterPipe@
h3. Version 0.1 (Leaky -- September 22, 2010)
-bc. <dependency>
+```xml
+<dependency>
<groupId>com.tinkerpop</groupId>
<artifactId>pipes</artifactId>
<version>0.1</version>
</dependency>
+```
* Initial release as the core engine for Gremlin 0.5
View
38 src/main/java/com/tinkerpop/pipes/filter/UniquePathFilterPipe.java
@@ -0,0 +1,38 @@
+package com.tinkerpop.pipes.filter;
+
+import com.tinkerpop.pipes.AbstractPipe;
+import com.tinkerpop.pipes.Pipe;
+
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+
+/**
+ * @author Marko A. Rodriguez (http://markorodriguez.com)
+ */
+public class UniquePathFilterPipe<S> extends AbstractPipe<S, S> implements FilterPipe<S> {
+
+ public S processNextStart() {
+ while (true) {
+ final S s = this.starts.next();
+ if (this.starts instanceof Pipe) {
+ final List path = ((Pipe) this.starts).getPath();
+ final Set<Object> temp = new HashSet<Object>();
+ boolean doReturn = true;
+ for (final Object object : path) {
+ if (temp.contains(object)) {
+ doReturn = false;
+ break;
+ } else {
+ temp.add(object);
+ }
+ }
+ if (doReturn)
+ return s;
+ } else {
+ return s;
+ }
+ }
+
+ }
+}
View
35 src/test/java/com/tinkerpop/pipes/filter/UniquePathFilterPipeTest.java
@@ -0,0 +1,35 @@
+package com.tinkerpop.pipes.filter;
+
+import com.tinkerpop.blueprints.pgm.Graph;
+import com.tinkerpop.blueprints.pgm.Vertex;
+import com.tinkerpop.blueprints.pgm.impls.tg.TinkerGraphFactory;
+import com.tinkerpop.pipes.BaseTest;
+import com.tinkerpop.pipes.Pipe;
+import com.tinkerpop.pipes.Pipeline;
+import com.tinkerpop.pipes.SingleIterator;
+import com.tinkerpop.pipes.pgm.EdgeVertexPipe;
+import com.tinkerpop.pipes.pgm.VertexEdgePipe;
+
+/**
+ * @author Marko A. Rodriguez (http://markorodriguez.com)
+ */
+public class UniquePathFilterPipeTest extends BaseTest {
+
+ public void testUniquePathFilter() {
+ Graph graph = TinkerGraphFactory.createTinkerGraph();
+ Pipe pipe1 = new VertexEdgePipe(VertexEdgePipe.Step.OUT_EDGES);
+ Pipe pipe2 = new EdgeVertexPipe(EdgeVertexPipe.Step.IN_VERTEX);
+ Pipe pipe3 = new VertexEdgePipe(VertexEdgePipe.Step.IN_EDGES);
+ Pipe pipe4 = new EdgeVertexPipe(EdgeVertexPipe.Step.OUT_VERTEX);
+ Pipe pipe5 = new UniquePathFilterPipe();
+ Pipe pipeline = new Pipeline(pipe1, pipe2, pipe3, pipe4, pipe5);
+ pipeline.setStarts(new SingleIterator<Vertex>(graph.getVertex(1)));
+ int counter = 0;
+ for (Object object : pipeline) {
+ counter++;
+ assertTrue(object.equals(graph.getVertex(6)) || object.equals(graph.getVertex(4)));
+ }
+ assertEquals(counter, 2);
+ }
+
+}

0 comments on commit ad4476f

Please sign in to comment.