All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.tinkerpop.gremlin.pipes.transform.QueryPipe Maven / Gradle / Ivy

package com.tinkerpop.gremlin.pipes.transform;

import com.tinkerpop.blueprints.Direction;
import com.tinkerpop.blueprints.Edge;
import com.tinkerpop.blueprints.Element;
import com.tinkerpop.blueprints.Query;
import com.tinkerpop.blueprints.Vertex;
import com.tinkerpop.pipes.AbstractPipe;
import com.tinkerpop.pipes.util.PipeHelper;

import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

/**
 * QueryPipe makes use of the Vertex.query() method in Blueprints which allows for intelligent edge look ups from the underlying graph.
 * Note that QueryPipe is automatically constructed by a GremlinPipeline when a pattern of the following is seen:
 * 

outE(x).has(x).interval(x).xV()

* The final xV() can be either inV(), outV(), or bothV(). * * @author Marko A. Rodriguez (http://markorodriguez.com) */ public class QueryPipe extends AbstractPipe { private Direction direction = Direction.BOTH; private String[] labels; private List hasContainers; private List intervalContainers; private final Class elementClass; private Iterator currentIterator = PipeHelper.emptyIterator(); public QueryPipe(final Class resultingElementClass, final Direction direction, final List hasContainers, final List intervalContainers, final String... labels) { this.elementClass = resultingElementClass; this.direction = direction; this.hasContainers = hasContainers; this.intervalContainers = intervalContainers; this.labels = labels; if (!resultingElementClass.equals(Vertex.class) && !resultingElementClass.equals(Edge.class)) throw new IllegalArgumentException("The provided element class must be either Vertex or Edge"); } public void reset() { super.reset(); this.currentIterator = PipeHelper.emptyIterator(); } public String toString() { final String extra = "has:" + (this.hasContainers.size() > 0) + ",interval:" + (this.intervalContainers.size() > 0); return PipeHelper.makePipeString(this, this.direction.name().toLowerCase(), Arrays.asList(this.labels), extra, this.elementClass.getSimpleName().toLowerCase()); } public E processNextStart() { while (true) { if (this.currentIterator.hasNext()) return currentIterator.next(); else { final Vertex vertex = this.starts.next(); Query query = vertex.query(); query = query.direction(this.direction); if (this.labels.length > 0) query = query.labels(this.labels); if (this.hasContainers.size() > 0) { for (final HasContainer hasContainer : hasContainers) { if (hasContainer.compare.equals(Query.Compare.EQUAL)) query = query.has(hasContainer.key, hasContainer.value); else query = query.has(hasContainer.key, (Comparable) hasContainer.value, hasContainer.compare); } } if (this.intervalContainers.size() > 0) { for (final IntervalContainer intervalContainer : intervalContainers) { query = query.interval(intervalContainer.key, (Comparable) intervalContainer.startValue, (Comparable) intervalContainer.endValue); } } if (this.elementClass.equals(Vertex.class)) this.currentIterator = (Iterator) query.vertices().iterator(); else this.currentIterator = (Iterator) query.edges().iterator(); } } } public static class HasContainer { public String key; public Object value; public Query.Compare compare; public HasContainer(final String key, final Object value, final Query.Compare compare) { this.key = key; this.value = value; this.compare = compare; } } public static class IntervalContainer { public String key; public Object startValue; public Object endValue; public IntervalContainer(final String key, final Object startValue, final Object endValue) { this.key = key; this.startValue = startValue; this.endValue = endValue; } } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy