org.apache.tinkerpop.gremlin.process.computer.GraphFilter Java Examples

The following examples show how to use org.apache.tinkerpop.gremlin.process.computer.GraphFilter. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TinkerGraphComputerView.java    From tinkergraph-gremlin with Apache License 2.0 6 votes vote down vote up
public TinkerGraphComputerView(final TinkerGraph graph, final GraphFilter graphFilter, final Set<VertexComputeKey> computeKeys) {
    this.graph = graph;
    this.computeKeys = new HashMap<>();
    computeKeys.forEach(key -> this.computeKeys.put(key.getKey(), key));
    this.computeProperties = new ConcurrentHashMap<>();
    this.graphFilter = graphFilter;
    if (this.graphFilter.hasFilter()) {
        graph.vertices().forEachRemaining(vertex -> {
            boolean legalVertex = false;
            if (this.graphFilter.hasVertexFilter() && this.graphFilter.legalVertex(vertex)) {
                this.legalVertices.add(vertex.id());
                legalVertex = true;
            }
            if ((legalVertex || !this.graphFilter.hasVertexFilter()) && this.graphFilter.hasEdgeFilter()) {
                final Set<Object> edges = new HashSet<>();
                this.legalEdges.put(vertex.id(), edges);
                this.graphFilter.legalEdges(vertex).forEachRemaining(edge -> edges.add(edge.id()));
            }
        });
    }
}
 
Example #2
Source File: TinkerGraphComputerView.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
public TinkerGraphComputerView(final TinkerGraph graph, final GraphFilter graphFilter, final Set<VertexComputeKey> computeKeys) {
    this.graph = graph;
    this.computeKeys = new HashMap<>();
    computeKeys.forEach(key -> this.computeKeys.put(key.getKey(), key));
    this.computeProperties = new ConcurrentHashMap<>();
    this.graphFilter = graphFilter;
    if (this.graphFilter.hasFilter()) {
        graph.vertices().forEachRemaining(vertex -> {
            boolean legalVertex = false;
            if (this.graphFilter.hasVertexFilter() && this.graphFilter.legalVertex(vertex)) {
                this.legalVertices.add(vertex.id());
                legalVertex = true;
            }
            if ((legalVertex || !this.graphFilter.hasVertexFilter()) && this.graphFilter.hasEdgeFilter()) {
                final Set<Object> edges = new HashSet<>();
                this.legalEdges.put(vertex.id(), edges);
                this.graphFilter.legalEdges(vertex).forEachRemaining(edge -> edges.add(edge.id()));
            }
        });
    }
}
 
Example #3
Source File: StarGraphTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Test
@FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.VertexFeatures.FEATURE_ADD_VERTICES)
@FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.VertexFeatures.FEATURE_ADD_PROPERTY)
@FeatureRequirement(featureClass = Graph.Features.EdgeFeatures.class, feature = Graph.Features.EdgeFeatures.FEATURE_ADD_EDGES)
@FeatureRequirement(featureClass = Graph.Features.EdgeFeatures.class, feature = Graph.Features.EdgeFeatures.FEATURE_ADD_PROPERTY)
public void shouldHandleBothEdgesGraphFilterOnSelfLoop() {
    assertEquals(0l, IteratorUtils.count(graph.vertices()));
    assertEquals(0l, IteratorUtils.count(graph.edges()));

    // these vertex label, edge label, and property names/values were copied from existing tests
    StarGraph starGraph = StarGraph.open();
    Vertex vertex = starGraph.addVertex(T.label, "person", "name", "furnace");
    Edge edge = vertex.addEdge("self", vertex);
    edge.property("acl", "private");

    // traversing a self-loop should yield the edge once for inE/outE
    // and the edge twice for bothE (one edge emitted two times, not two edges)
    assertEquals(1L, IteratorUtils.count(starGraph.traversal().V().inE()));
    assertEquals(1L, IteratorUtils.count(starGraph.traversal().V().outE()));
    assertEquals(2L, IteratorUtils.count(starGraph.traversal().V().bothE()));
    
    // Try a filter that retains BOTH
    GraphFilter graphFilter = new GraphFilter();
    graphFilter.setEdgeFilter(__.bothE("self"));
    starGraph = starGraph.applyGraphFilter(graphFilter).get();

    // Retest traversal counts after filtering
    assertEquals(1L, IteratorUtils.count(starGraph.traversal().V().inE()));
    assertEquals(1L, IteratorUtils.count(starGraph.traversal().V().outE()));
    assertEquals(2L, IteratorUtils.count(starGraph.traversal().V().bothE()));
}
 
Example #4
Source File: GryoReader.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Override
public Optional<Vertex> readVertex(final InputStream inputStream, final GraphFilter graphFilter) throws IOException {
    StarGraphGryoSerializer serializer = this.graphFilterCache.get(graphFilter);
    if (null == serializer) {
        serializer = StarGraphGryoSerializer.withGraphFilter(graphFilter);
        this.graphFilterCache.put(graphFilter, serializer);
    }
    final Input input = new Input(inputStream);
    this.readHeader(input);
    final StarGraph starGraph = this.kryo.readObject(input, StarGraph.class, serializer);
    // read the terminator
    this.kryo.readClassAndObject(input);
    return Optional.ofNullable(starGraph == null ? null : starGraph.getStarVertex());
}
 
Example #5
Source File: SparkExecutor.java    From tinkerpop with Apache License 2.0 4 votes vote down vote up
public static JavaPairRDD<Object, VertexWritable> applyGraphFilter(final JavaPairRDD<Object, VertexWritable> graphRDD, final GraphFilter graphFilter) {
    return graphRDD.mapPartitionsToPair(partitionIterator -> {
        final GraphFilter gFilter = graphFilter.clone();
        return IteratorUtils.filter(partitionIterator, tuple -> (tuple._2().get().applyGraphFilter(gFilter)).isPresent());
    }, true);
}
 
Example #6
Source File: TinkerHelper.java    From tinkergraph-gremlin with Apache License 2.0 4 votes vote down vote up
public static TinkerGraphComputerView createGraphComputerView(final TinkerGraph graph, final GraphFilter graphFilter, final Set<VertexComputeKey> computeKeys) {
    return graph.graphComputerView = new TinkerGraphComputerView(graph, graphFilter, computeKeys);
}
 
Example #7
Source File: TinkerHelper.java    From tinkerpop with Apache License 2.0 4 votes vote down vote up
public static TinkerGraphComputerView createGraphComputerView(final TinkerGraph graph, final GraphFilter graphFilter, final Set<VertexComputeKey> computeKeys) {
    return graph.graphComputerView = new TinkerGraphComputerView(graph, graphFilter, computeKeys);
}
 
Example #8
Source File: ConnectedComponentVertexProgramStep.java    From tinkerpop with Apache License 2.0 4 votes vote down vote up
@Override
public String toString() {
    return StringFactory.stepString(this, this.clusterProperty, new GraphFilter(this.computer));
}
 
Example #9
Source File: PeerPressureVertexProgramStep.java    From tinkerpop with Apache License 2.0 4 votes vote down vote up
@Override
public String toString() {
    return StringFactory.stepString(this, this.edgeTraversal.get(), this.clusterProperty, this.times, new GraphFilter(this.computer));
}
 
Example #10
Source File: ProgramVertexProgramStep.java    From tinkerpop with Apache License 2.0 4 votes vote down vote up
@Override
public String toString() {
    return StringFactory.stepString(this, this.toStringOfVertexProgram, new GraphFilter(this.computer));
}
 
Example #11
Source File: TraversalVertexProgramStep.java    From tinkerpop with Apache License 2.0 4 votes vote down vote up
@Override
public String toString() {
    return StringFactory.stepString(this, this.computerTraversal.get(), new GraphFilter(this.computer));
}
 
Example #12
Source File: ShortestPathVertexProgramStep.java    From tinkerpop with Apache License 2.0 4 votes vote down vote up
@Override
public String toString() {
    return StringFactory.stepString(this, this.targetVertexFilter.get(), this.edgeTraversal.get(),
            this.distanceTraversal.get(), this.maxDistance, this.includeEdges, new GraphFilter(this.computer));
}
 
Example #13
Source File: PageRankVertexProgramStep.java    From tinkerpop with Apache License 2.0 4 votes vote down vote up
@Override
public String toString() {
    return StringFactory.stepString(this, this.edgeTraversal.get(), this.pageRankProperty, this.times, new GraphFilter(this.computer));
}
 
Example #14
Source File: StarGraphGryoSerializer.java    From tinkerpop with Apache License 2.0 4 votes vote down vote up
public static StarGraphGryoSerializer withGraphFilter(final GraphFilter graphFilter) {
    final StarGraphGryoSerializer serializer = new StarGraphGryoSerializer(Direction.BOTH, graphFilter.clone());
    return serializer;
}
 
Example #15
Source File: StarGraphGryoSerializer.java    From tinkerpop with Apache License 2.0 4 votes vote down vote up
private StarGraphGryoSerializer(final Direction edgeDirectionToSerialize) {
    this(edgeDirectionToSerialize, new GraphFilter());
}
 
Example #16
Source File: StarGraphGryoSerializer.java    From tinkerpop with Apache License 2.0 4 votes vote down vote up
private StarGraphGryoSerializer(final Direction edgeDirectionToSerialize, final GraphFilter graphFilter) {
    super(new StarGraphSerializer(edgeDirectionToSerialize, graphFilter));
}
 
Example #17
Source File: StarGraph.java    From tinkerpop with Apache License 2.0 4 votes vote down vote up
public Optional<StarGraph> applyGraphFilter(final GraphFilter graphFilter) {
    if (null == this.starVertex)
        return Optional.empty();
    final Optional<StarGraph.StarVertex> filtered = this.starVertex.applyGraphFilter(graphFilter);
    return filtered.isPresent() ? Optional.of((StarGraph) filtered.get().graph()) : Optional.empty();
}
 
Example #18
Source File: StarGraphSerializer.java    From tinkerpop with Apache License 2.0 4 votes vote down vote up
public StarGraphSerializer(final Direction edgeDirectionToSerialize, final GraphFilter graphFilter) {
    this.edgeDirectionToSerialize = edgeDirectionToSerialize;
    this.graphFilter = graphFilter;
}
 
Example #19
Source File: GraphFilterInputFormat.java    From tinkerpop with Apache License 2.0 4 votes vote down vote up
@Override
public void setGraphFilter(final GraphFilter graphFilter) {
    // do nothing -- loaded via configuration
}
 
Example #20
Source File: HadoopIoStep.java    From tinkerpop with Apache License 2.0 4 votes vote down vote up
@Override
public String toString() {
    return StringFactory.stepString(this, new GraphFilter(this.computer));
}
 
Example #21
Source File: GraphFilterAware.java    From tinkerpop with Apache License 2.0 4 votes vote down vote up
public static void storeGraphFilter(final Configuration apacheConfiguration, final org.apache.hadoop.conf.Configuration hadoopConfiguration, final GraphFilter graphFilter) {
    if (graphFilter.hasFilter()) {
        VertexProgramHelper.serialize(graphFilter, apacheConfiguration, Constants.GREMLIN_HADOOP_GRAPH_FILTER);
        hadoopConfiguration.set(Constants.GREMLIN_HADOOP_GRAPH_FILTER, apacheConfiguration.getString(Constants.GREMLIN_HADOOP_GRAPH_FILTER));
    }
}
 
Example #22
Source File: CommonFileInputFormat.java    From tinkerpop with Apache License 2.0 4 votes vote down vote up
@Override
public void setGraphFilter(final GraphFilter graphFilter) {
    // do nothing. loaded through configuration.
}
 
Example #23
Source File: InputFormatHadoop.java    From grakn with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public void setGraphFilter(GraphFilter graphFilter) {
    // do nothing -- loaded via configuration
}
 
Example #24
Source File: GraknSparkExecutor.java    From grakn with GNU Affero General Public License v3.0 4 votes vote down vote up
public static JavaPairRDD<Object, VertexWritable> applyGraphFilter(final JavaPairRDD<Object, VertexWritable> graphRDD, final GraphFilter graphFilter) {
    return graphRDD.mapPartitionsToPair(partitionIterator -> {
        final GraphFilter gFilter = graphFilter.clone();
        return IteratorUtils.filter(partitionIterator, tuple -> (tuple._2().get().applyGraphFilter(gFilter)).isPresent());
    }, true);
}
 
Example #25
Source File: GraphReader.java    From tinkerpop with Apache License 2.0 2 votes vote down vote up
/**
 * Reads a single vertex from an {@link InputStream}. This method will filter the read the read vertex by the provided
 * {@link GraphFilter}. If the graph filter will filter the vertex itself, then the returned {@link Optional} is empty.
 *
 * @param inputStream a stream containing at least a single vertex as defined by the accompanying
 *                    {@link GraphWriter#writeVertex(OutputStream, Vertex)}.
 * @param graphFilter The {@link GraphFilter} to filter the vertex and its associated edges by.
 * @return the vertex with filtered edges or {@link Optional#empty()}  if the vertex itself was filtered.
 * @throws IOException
 */
public default Optional<Vertex> readVertex(final InputStream inputStream, final GraphFilter graphFilter) throws IOException {
    throw new UnsupportedOperationException(this.getClass().getCanonicalName() + " currently does not support " + GraphFilter.class.getSimpleName() + " deserialization filtering");
}
 
Example #26
Source File: GraphFilterAware.java    From tinkerpop with Apache License 2.0 votes vote down vote up
public void setGraphFilter(final GraphFilter graphFilter);