Java Code Examples for org.apache.tinkerpop.gremlin.process.computer.MessageScope#Local

The following examples show how to use org.apache.tinkerpop.gremlin.process.computer.MessageScope#Local . 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: VertexMemoryHandler.java    From titan1withtp3.1 with Apache License 2.0 6 votes vote down vote up
public Stream<M> receiveMessages(MessageScope messageScope) {
    if (messageScope instanceof MessageScope.Global) {
        M message = vertexMemory.getMessage(vertexId,messageScope);
        if (message == null) return Stream.empty();
        else return Stream.of(message);
    } else {
        final MessageScope.Local<M> localMessageScope = (MessageScope.Local) messageScope;
        final Traversal<Vertex, Edge> reverseIncident = FulgoraUtil.getReverseElementTraversal(localMessageScope,vertex,vertex.tx());
        final BiFunction<M,Edge,M> edgeFct = localMessageScope.getEdgeFunction();

        return IteratorUtils.stream(reverseIncident)
                .map(e -> {
                    M msg = vertexMemory.getMessage(vertexMemory.getCanonicalId(((TitanEdge) e).otherVertex(vertex).longId()), localMessageScope);
                    return msg == null ? null : edgeFct.apply(msg, e);
                })
                .filter(m -> m != null);
    }
}
 
Example 2
Source File: SparkMessenger.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Override
public void sendMessage(final MessageScope messageScope, final M message) {
    if (messageScope instanceof MessageScope.Local) {
        final MessageScope.Local<M> localMessageScope = (MessageScope.Local) messageScope;
        final Traversal.Admin<Vertex, Edge> incidentTraversal = SparkMessenger.setVertexStart(localMessageScope.getIncidentTraversal().get().asAdmin(), this.vertex);
        final Direction direction = SparkMessenger.getOppositeDirection(incidentTraversal);

        // handle processing for BOTH given TINKERPOP-1862 where the target of the message is the one opposite
        // the current vertex
        incidentTraversal.forEachRemaining(edge -> {
            if (direction.equals(Direction.IN) || direction.equals(Direction.OUT))
                this.outgoingMessages.add(new Tuple2<>(edge.vertices(direction).next().id(), localMessageScope.getEdgeFunction().apply(message, edge)));
            else
                this.outgoingMessages.add(new Tuple2<>(edge instanceof StarGraph.StarOutEdge ? edge.inVertex().id() : edge.outVertex().id(), localMessageScope.getEdgeFunction().apply(message, edge)));

        });
    } else {
        ((MessageScope.Global) messageScope).vertices().forEach(v -> this.outgoingMessages.add(new Tuple2<>(v.id(), message)));
    }
}
 
Example 3
Source File: TinkerMessenger.java    From tinkergraph-gremlin with Apache License 2.0 5 votes vote down vote up
@Override
    public Iterator<M> receiveMessages() {
        final MultiIterator<M> multiIterator = new MultiIterator<>();
        for (final MessageScope messageScope : this.messageBoard.receiveMessages.keySet()) {
//        for (final MessageScope messageScope : this.messageBoard.previousMessageScopes) {
            if (messageScope instanceof MessageScope.Local) {
                final MessageScope.Local<M> localMessageScope = (MessageScope.Local<M>) messageScope;
                final Traversal.Admin<Vertex, Edge> incidentTraversal = TinkerMessenger.setVertexStart(localMessageScope.getIncidentTraversal().get().asAdmin(), this.vertex);
                final Direction direction = TinkerMessenger.getDirection(incidentTraversal);
                final Edge[] edge = new Edge[1]; // simulates storage side-effects available in Gremlin, but not Java8 streams
                multiIterator.addIterator(StreamSupport.stream(Spliterators.spliteratorUnknownSize(VertexProgramHelper.reverse(incidentTraversal.asAdmin()), Spliterator.IMMUTABLE | Spliterator.SIZED), false)
                        .map((Edge e) -> {
                            edge[0] = e;
                            Vertex vv;
                            if (direction.equals(Direction.IN) || direction.equals(Direction.OUT)) {
                                vv = e.vertices(direction).next();
                            } else {
                                vv = e.outVertex() == this.vertex ? e.inVertex() : e.outVertex();
                            }
                            return this.messageBoard.receiveMessages.get(messageScope).get(vv);
                        })
                        .filter(q -> null != q)
                        .flatMap(Queue::stream)
                        .map(message -> localMessageScope.getEdgeFunction().apply(message, edge[0]))
                        .iterator());

            } else {
                multiIterator.addIterator(Stream.of(this.vertex)
                        .map(this.messageBoard.receiveMessages.get(messageScope)::get)
                        .filter(q -> null != q)
                        .flatMap(Queue::stream)
                        .iterator());
            }
        }
        return multiIterator;
    }
 
Example 4
Source File: TinkerMessenger.java    From tinkergraph-gremlin with Apache License 2.0 5 votes vote down vote up
@Override
    public void sendMessage(final MessageScope messageScope, final M message) {
//        this.messageBoard.currentMessageScopes.add(messageScope);
        if (messageScope instanceof MessageScope.Local) {
            addMessage(this.vertex, message, messageScope);
        } else {
            ((MessageScope.Global) messageScope).vertices().forEach(v -> addMessage(v, message, messageScope));
        }
    }
 
Example 5
Source File: VertexProgramScanJob.java    From titan1withtp3.1 with Apache License 2.0 5 votes vote down vote up
@Override
public void process(TitanVertex vertex, ScanMetrics metrics) {
    PreloadedVertex v = (PreloadedVertex)vertex;
    long vertexId = v.longId();
    VertexMemoryHandler<M> vh = new VertexMemoryHandler(vertexMemory,v);
    v.setAccessCheck(PreloadedVertex.OPENSTAR_CHECK);
    if (idManager.isPartitionedVertex(vertexId)) {
        if (idManager.isCanonicalVertexId(vertexId)) {
            EntryList results = v.getFromCache(SYSTEM_PROPS_QUERY);
            if (results == null) results = EntryList.EMPTY_LIST;
            vertexMemory.setLoadedProperties(vertexId,results);
        }
        for (MessageScope scope : vertexMemory.getPreviousScopes()) {
            if (scope instanceof MessageScope.Local) {
                M combinedMsg = null;
                for (Iterator<M> msgIter = vh.receiveMessages(scope).iterator(); msgIter.hasNext(); ) {
                    M msg = msgIter.next();
                    if (combinedMsg==null) combinedMsg=msg;
                    else combinedMsg = combiner.combine(combinedMsg,msg);
                }
                if (combinedMsg!=null) vertexMemory.aggregateMessage(vertexId,combinedMsg,scope);
            }
        }
    } else {
        v.setPropertyMixing(vh);
        vertexProgram.execute(v, vh, memory);
    }
}
 
Example 6
Source File: VertexMemoryHandler.java    From titan1withtp3.1 with Apache License 2.0 5 votes vote down vote up
@Override
public Stream<M> receiveMessages(MessageScope messageScope) {
    if (messageScope instanceof MessageScope.Global) {
        return super.receiveMessages(messageScope);
    } else {
        final MessageScope.Local<M> localMessageScope = (MessageScope.Local) messageScope;
        M aggregateMsg = vertexMemory.getAggregateMessage(vertexId,localMessageScope);
        if (aggregateMsg==null) return Stream.empty();
        else return Stream.of(aggregateMsg);
    }
}
 
Example 7
Source File: FulgoraUtil.java    From titan1withtp3.1 with Apache License 2.0 5 votes vote down vote up
public static TitanVertexStep<Vertex> getReverseTitanVertexStep(final MessageScope.Local<?> scope,
                                                                   final TitanTransaction graph) {
    FulgoraElementTraversal<Vertex,Edge> result = getReverseTraversal(scope,graph,null);
    result.asAdmin().applyStrategies();
    verifyIncidentTraversal(result);
    return (TitanVertexStep)result.getStartStep();
}
 
Example 8
Source File: FulgoraUtil.java    From titan1withtp3.1 with Apache License 2.0 5 votes vote down vote up
private static FulgoraElementTraversal<Vertex,Edge> getReverseTraversal(final MessageScope.Local<?> scope,
                                                  final TitanTransaction graph, @Nullable final Vertex start) {
    Traversal.Admin<Vertex,Edge> incident = scope.getIncidentTraversal().get().asAdmin();
    FulgoraElementTraversal<Vertex,Edge> result = FulgoraElementTraversal.of(graph);

    for (Step step : incident.getSteps()) result.addStep(step);
    Step<Vertex,?> startStep = result.getStartStep();
    assert startStep instanceof VertexStep;
    ((VertexStep) startStep).reverseDirection();

    if (start!=null) result.addStep(0, new StartStep<>(incident, start));
    result.asAdmin().setStrategies(FULGORA_STRATEGIES);
    return result;
}
 
Example 9
Source File: TinkerMessenger.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Override
    public Iterator<M> receiveMessages() {
        final MultiIterator<M> multiIterator = new MultiIterator<>();
        for (final MessageScope messageScope : this.messageBoard.receiveMessages.keySet()) {
//        for (final MessageScope messageScope : this.messageBoard.previousMessageScopes) {
            if (messageScope instanceof MessageScope.Local) {
                final MessageScope.Local<M> localMessageScope = (MessageScope.Local<M>) messageScope;
                final Traversal.Admin<Vertex, Edge> incidentTraversal = TinkerMessenger.setVertexStart(localMessageScope.getIncidentTraversal().get().asAdmin(), this.vertex);
                final Direction direction = TinkerMessenger.getDirection(incidentTraversal);
                final Edge[] edge = new Edge[1]; // simulates storage side-effects available in Gremlin, but not Java8 streams
                multiIterator.addIterator(StreamSupport.stream(Spliterators.spliteratorUnknownSize(VertexProgramHelper.reverse(incidentTraversal.asAdmin()), Spliterator.IMMUTABLE | Spliterator.SIZED), false)
                        .map((Edge e) -> {
                            edge[0] = e;
                            Vertex vv;
                            if (direction.equals(Direction.IN) || direction.equals(Direction.OUT)) {
                                vv = e.vertices(direction).next();
                            } else {
                                vv = e.outVertex() == this.vertex ? e.inVertex() : e.outVertex();
                            }
                            return this.messageBoard.receiveMessages.get(messageScope).get(vv);
                        })
                        .filter(q -> null != q)
                        .flatMap(Queue::stream)
                        .map(message -> localMessageScope.getEdgeFunction().apply(message, edge[0]))
                        .iterator());

            } else {
                multiIterator.addIterator(Stream.of(this.vertex)
                        .map(this.messageBoard.receiveMessages.get(messageScope)::get)
                        .filter(q -> null != q)
                        .flatMap(Queue::stream)
                        .iterator());
            }
        }
        return multiIterator;
    }
 
Example 10
Source File: TinkerMessenger.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Override
    public void sendMessage(final MessageScope messageScope, final M message) {
//        this.messageBoard.currentMessageScopes.add(messageScope);
        if (messageScope instanceof MessageScope.Local) {
            addMessage(this.vertex, message, messageScope);
        } else {
            ((MessageScope.Global) messageScope).vertices().forEach(v -> addMessage(v, message, messageScope));
        }
    }
 
Example 11
Source File: SparkMessengerTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Test
public void testSparkMessenger() throws Exception {
    // Define scopes
    final MessageScope.Local<String> orderSrcMessageScope = MessageScope.Local
            .of(__::inE, (message, edge) -> {
                if ("mocked_edge_label1".equals(edge.label())) {
                    return message;
                }
                return null;
            });

    // Define star graph
    final StarGraph starGraph = StarGraph.open();
    Object[] vertex0Array = new Object[]{T.id, 0, T.label, "mocked_vertex_label1"};
    Object[] vertex1Array = new Object[]{T.id, 1, T.label, "mocked_vertex_label2"};
    Object[] vertex2Array = new Object[]{T.id, 2, T.label, "mocked_vertex_label2"};
    Vertex vertex0 = starGraph.addVertex(vertex0Array);
    Vertex vertex1 = starGraph.addVertex(vertex1Array);
    Vertex vertex2 = starGraph.addVertex(vertex2Array);
    vertex1.addEdge("mocked_edge_label1", vertex0);
    vertex2.addEdge("mocked_edge_label2", vertex0);

    // Create Spark Messenger
    final SparkMessenger<String> messenger = new SparkMessenger<>();
    final List<String> incomingMessages = Arrays.asList("a", "b", "c");
    messenger.setVertexAndIncomingMessages(vertex0, incomingMessages);

    messenger.sendMessage(orderSrcMessageScope, "a");
    List<Tuple2<Object, String>> outgoingMessages0 = messenger.getOutgoingMessages();

    Assert.assertEquals("a", outgoingMessages0.get(0)._2());
    Assert.assertNull(outgoingMessages0.get(1)._2());
}
 
Example 12
Source File: FulgoraUtil.java    From titan1withtp3.1 with Apache License 2.0 4 votes vote down vote up
public static Traversal<Vertex,Edge> getReverseElementTraversal(final MessageScope.Local<?> scope,
                                                                final Vertex start,
                                                                final TitanTransaction graph) {
    return getReverseTraversal(scope,graph,start);
}