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

The following examples show how to use org.apache.tinkerpop.gremlin.process.computer.Messenger. 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: DegreeStatisticsVertexProgram.java    From grakn with GNU Affero General Public License v3.0 6 votes vote down vote up
@Override
public void safeExecute(final Vertex vertex, Messenger<Long> messenger, final Memory memory) {
    switch (memory.getIteration()) {
        case 0:
            degreeStatisticsStepResourceOwner(vertex, messenger, ofLabelIds);
            break;
        case 1:
            degreeStatisticsStepResourceRelation(vertex, messenger, ofLabelIds);
            break;
        case 2:
            degreeStatisticsStepResource(vertex, messenger, ofLabelIds);
            break;
        default:
            throw GraknAnalyticsException.unreachableStatement("Exceeded expected maximum number of iterations");
    }
}
 
Example #2
Source File: PageRankVertexProgram.java    From titan1withtp3.1 with Apache License 2.0 6 votes vote down vote up
@Override
public void execute(Vertex vertex, Messenger<Double> messenger, Memory memory) {
    if (memory.isInitialIteration()) {
        messenger.sendMessage(inE, 1D);
    } else if (1 == memory.getIteration()) {
        double initialPageRank = 1D / vertexCount;
        double edgeCount = IteratorUtils.stream(messenger.receiveMessages()).reduce(0D, (a, b) -> a + b);
        vertex.property(VertexProperty.Cardinality.single, PAGE_RANK, initialPageRank);
        vertex.property(VertexProperty.Cardinality.single, OUTGOING_EDGE_COUNT, edgeCount);
        messenger.sendMessage(outE, initialPageRank / edgeCount);
    } else {
        double newPageRank = IteratorUtils.stream(messenger.receiveMessages()).reduce(0D, (a, b) -> a + b);
        newPageRank =  (dampingFactor * newPageRank) + ((1D - dampingFactor) / vertexCount);
        vertex.property(VertexProperty.Cardinality.single, PAGE_RANK, newPageRank);
        messenger.sendMessage(outE, newPageRank / vertex.<Double>value(OUTGOING_EDGE_COUNT));
    }
}
 
Example #3
Source File: ShortestPathVertexProgram.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
private void processEdges(final Vertex vertex, final Path currentPath, final Number currentDistance,
                          final Messenger<Triplet<Path, Edge, Number>> messenger) {

    final Traversal.Admin<Vertex, Edge> edgeTraversal = this.edgeTraversal.getPure();
    edgeTraversal.addStart(edgeTraversal.getTraverserGenerator().generate(vertex, edgeTraversal.getStartStep(), 1));

    while (edgeTraversal.hasNext()) {
        final Edge edge = edgeTraversal.next();
        final Number distance = getDistance(edge);

        Vertex otherV = edge.inVertex();
        if (otherV.equals(vertex))
            otherV = edge.outVertex();

        // only send message if the adjacent vertex is not yet part of the current path
        if (!currentPath.objects().contains(otherV)) {
            messenger.sendMessage(MessageScope.Global.of(otherV),
                    Triplet.with(currentPath, this.includeEdges ? edge : null,
                            NumberHelper.add(currentDistance, distance)));
        }
    }
}
 
Example #4
Source File: KCoreVertexProgram.java    From grakn with GNU Affero General Public License v3.0 6 votes vote down vote up
static void filterByDegree(Vertex vertex, Messenger<String> messenger, Memory memory, boolean persistId) {
    if ((vertex.label().equals(Schema.BaseType.ENTITY.name()) ||
            vertex.label().equals(Schema.BaseType.ATTRIBUTE.name())) &&
            Iterators.size(messenger.receiveMessages()) >= memory.<Long>get(K)) {
        String id = vertex.id().toString();

        // coreness query doesn't require id
        if (persistId) {
            vertex.property(K_CORE_LABEL, id);
        } else {
            vertex.property(K_CORE_LABEL, true);
        }
        memory.add(K_CORE_EXIST, true);

        // send ids from now on, as we want to count connected entities, not relations
        sendMessage(messenger, id);
    }
}
 
Example #5
Source File: KCoreVertexProgram.java    From grakn with GNU Affero General Public License v3.0 5 votes vote down vote up
static void relayOrSaveMessages(Vertex vertex, Messenger<String> messenger) {
    if (messenger.receiveMessages().hasNext()) {
        if (vertex.label().equals(Schema.BaseType.RELATION.name())) {
            // relay the messages
            messenger.receiveMessages().forEachRemaining(msg -> sendMessage(messenger, msg));
        } else if ((vertex.label().equals(Schema.BaseType.ENTITY.name()) ||
                vertex.label().equals(Schema.BaseType.ATTRIBUTE.name())) &&
                vertex.property(K_CORE_LABEL).isPresent()) {
            // messages received via implicit edge, save the count for next iteration
            vertex.property(IMPLICIT_MESSAGE_COUNT, (long) newHashSet(messenger.receiveMessages()).size());
        }
    }
}
 
Example #6
Source File: CountVertexProgram.java    From grakn with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public void safeExecute(final Vertex vertex, Messenger<Long> messenger, final Memory memory) {
    switch (memory.getIteration()) {
        case 0:
            messenger.sendMessage(messageScopeOut, 1L);
            break;
        case 1:
            if (messenger.receiveMessages().hasNext()) {
                vertex.property(EDGE_COUNT, getMessageCount(messenger));
            }
            break;
        default:
            throw GraknAnalyticsException.unreachableStatement("Exceeded expected maximum number of iterations");
    }
}
 
Example #7
Source File: ConnectedComponentVertexProgram.java    From grakn with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public void safeExecute(final Vertex vertex, Messenger<Boolean> messenger, final Memory memory) {
    if (memory.isInitialIteration()) {
        if (vertex.id().toString().equals(persistentProperties.get(SOURCE))) {
            update(vertex, messenger, memory, persistentProperties.get(SOURCE).toString());
        }
    } else {
        if (messenger.receiveMessages().hasNext() && !vertex.property(CLUSTER_LABEL).isPresent()) {
            update(vertex, messenger, memory, persistentProperties.get(SOURCE).toString());
        }
    }
}
 
Example #8
Source File: DegreeStatisticsVertexProgram.java    From grakn with GNU Affero General Public License v3.0 5 votes vote down vote up
static void degreeStatisticsStepResource(Vertex vertex, Messenger<Long> messenger,
                                         Set<LabelId> ofLabelIds) {
    if (vertexHasSelectedTypeId(vertex, ofLabelIds)) {
        vertex.property(DEGREE, vertex.property(DEGREE).isPresent() ?
                getMessageCount(messenger) + (Long) vertex.value(DEGREE) : getMessageCount(messenger));
    }
}
 
Example #9
Source File: DegreeStatisticsVertexProgram.java    From grakn with GNU Affero General Public License v3.0 5 votes vote down vote up
static void degreeStatisticsStepResourceRelation(Vertex vertex, Messenger<Long> messenger, Set<LabelId> ofLabelIds) {
    if (messenger.receiveMessages().hasNext()) {
        if (vertex.label().equals(Schema.BaseType.RELATION.name())) {
            messenger.sendMessage(messageScopeOut, 1L);
        } else if (ofLabelIds.contains(Utility.getVertexTypeId(vertex))) {
            vertex.property(DEGREE, getMessageCount(messenger));
        }
    }
}
 
Example #10
Source File: DegreeStatisticsVertexProgram.java    From grakn with GNU Affero General Public License v3.0 5 votes vote down vote up
static void degreeStatisticsStepResourceOwner(Vertex vertex, Messenger<Long> messenger, Set<LabelId> ofLabelIds) {
    LabelId labelId = Utility.getVertexTypeId(vertex);
    if (labelId.isValid() && !ofLabelIds.contains(labelId)) {
        messenger.sendMessage(messageScopeShortcutIn, 1L);
        messenger.sendMessage(messageScopeResourceOut, 1L);
    }
}
 
Example #11
Source File: ShortestDistanceVertexProgram.java    From titan1withtp3.1 with Apache License 2.0 5 votes vote down vote up
@Override
public void execute(final Vertex vertex, Messenger<Long> messenger, final Memory memory) {
    if (memory.isInitialIteration()) {
        if (vertex.id().equals(Long.valueOf(seed).longValue())) {
            // The seed sends a single message to start the computation
            log.debug("Sent initial message from {}", vertex.id());
            // The seed's distance to itself is zero
            vertex.property(VertexProperty.Cardinality.single, DISTANCE, 0L);
            messenger.sendMessage(incidentMessageScope, 0L);
        }
    } else {
        Iterator<Long> distances = messenger.receiveMessages();

        // Find minimum distance among all incoming messages, or null if no messages came in
        Long shortestDistanceSeenOnThisIteration =
                IteratorUtils.stream(distances).reduce((a, b) -> Math.min(a, b)).orElse(null);

        if (null == shortestDistanceSeenOnThisIteration)
            return; // no messages to process or forward on this superstep

        VertexProperty<Long> currentShortestVP = vertex.property(DISTANCE);

        if (!currentShortestVP.isPresent() ||
                currentShortestVP.value() > shortestDistanceSeenOnThisIteration) {
            // First/shortest distance seen by this vertex: store it and forward to neighbors
            vertex.property(VertexProperty.Cardinality.single, DISTANCE, shortestDistanceSeenOnThisIteration);
            messenger.sendMessage(incidentMessageScope, shortestDistanceSeenOnThisIteration);
        }
        // else: no new winner, ergo no reason to send message to neighbors
    }
}
 
Example #12
Source File: DegreeVertexProgram.java    From grakn with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public void safeExecute(Vertex vertex, Messenger<Long> messenger, Memory memory) {
    switch (memory.getIteration()) {
        case 0:
            degreeMessagePassing(messenger);
            break;
        case 1:
            degreeMessageCounting(messenger, vertex);
            break;
        default:
            throw GraknAnalyticsException.unreachableStatement("Exceeded expected maximum number of iterations");
    }
}
 
Example #13
Source File: PageRankVertexProgram.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Override
public void execute(final Vertex vertex, Messenger<Double> messenger, final Memory memory) {
    if (memory.isInitialIteration()) {
        messenger.sendMessage(this.countMessageScope, 1.0d);
        memory.add(VERTEX_COUNT, 1.0d);
    } else {
        final double vertexCount = memory.<Double>get(VERTEX_COUNT);
        final double edgeCount;
        double pageRank;
        if (1 == memory.getIteration()) {
            edgeCount = IteratorUtils.reduce(messenger.receiveMessages(), 0.0d, (a, b) -> a + b);
            vertex.property(VertexProperty.Cardinality.single, EDGE_COUNT, edgeCount);
            pageRank = null == this.initialRankTraversal ?
                    0.0d :
                    TraversalUtil.apply(vertex, this.initialRankTraversal.get()).doubleValue();
        } else {
            edgeCount = vertex.value(EDGE_COUNT);
            pageRank = IteratorUtils.reduce(messenger.receiveMessages(), 0.0d, (a, b) -> a + b);
        }
        //////////////////////////
        final double teleporationEnergy = memory.get(TELEPORTATION_ENERGY);
        if (teleporationEnergy > 0.0d) {
            final double localTerminalEnergy = teleporationEnergy / vertexCount;
            pageRank = pageRank + localTerminalEnergy;
            memory.add(TELEPORTATION_ENERGY, -localTerminalEnergy);
        }
        final double previousPageRank = vertex.<Double>property(this.property).orElse(0.0d);
        memory.add(CONVERGENCE_ERROR, Math.abs(pageRank - previousPageRank));
        vertex.property(VertexProperty.Cardinality.single, this.property, pageRank);
        memory.add(TELEPORTATION_ENERGY, (1.0d - this.alpha) * pageRank);
        pageRank = this.alpha * pageRank;
        if (edgeCount > 0.0d)
            messenger.sendMessage(this.incidentMessageScope, pageRank / edgeCount);
        else
            memory.add(TELEPORTATION_ENERGY, pageRank);
    }
}
 
Example #14
Source File: KCoreVertexProgram.java    From grakn with GNU Affero General Public License v3.0 5 votes vote down vote up
private static void relayClusterLabel(Messenger<String> messenger, Memory memory) {
    String firstMessage = messenger.receiveMessages().next();
    String max = IteratorUtils.reduce(messenger.receiveMessages(), firstMessage,
            (a, b) -> a.compareTo(b) > 0 ? a : b);
    sendMessage(messenger, max);
    memory.add(VOTE_TO_HALT, false);
}
 
Example #15
Source File: KCoreVertexProgram.java    From grakn with GNU Affero General Public License v3.0 5 votes vote down vote up
private static void updateClusterLabel(Vertex vertex, Messenger<String> messenger, Memory memory) {
    String currentMax = vertex.value(K_CORE_LABEL);
    String max = IteratorUtils.reduce(messenger.receiveMessages(), currentMax,
            (a, b) -> a.compareTo(b) > 0 ? a : b);
    if (!max.equals(currentMax)) {
        LOGGER.trace("Cluster label of {} changed from {} to {}", vertex, currentMax, max);
        vertex.property(K_CORE_LABEL, max);
        sendMessage(messenger, max);
        memory.add(VOTE_TO_HALT, false);
    } else {
        LOGGER.trace("Cluster label of {} is still {}", vertex, currentMax);
    }
}
 
Example #16
Source File: KCoreVertexProgram.java    From grakn with GNU Affero General Public License v3.0 5 votes vote down vote up
static void updateEntityAndAttribute(Vertex vertex, Messenger<String> messenger,
                                     Memory memory, boolean persistMessageCount) {
    if (vertex.property(K_CORE_LABEL).isPresent()) {
        String id = vertex.id().toString();
        long messageCount = (long) getMessageCountExcludeSelf(messenger, id);
        if (vertex.property(IMPLICIT_MESSAGE_COUNT).isPresent()) {
            messageCount += vertex.<Long>value(IMPLICIT_MESSAGE_COUNT);
            // need to remove implicit count as the vertex may not receive msg via implicit edge
            vertex.property(IMPLICIT_MESSAGE_COUNT).remove();
        }

        if (messageCount >= memory.<Long>get(K)) {
            LOGGER.trace("Sending msg from {}", id);
            sendMessage(messenger, id);
            memory.add(K_CORE_EXIST, true);

            if (persistMessageCount) {
                // message count may help eliminate unqualified vertex in earlier iterations
                vertex.property(MESSAGE_COUNT, messageCount);
            }
        } else {
            LOGGER.trace("Removing label of {}", id);
            vertex.property(K_CORE_LABEL).remove();
            memory.add(K_CORE_STABLE, false);
        }
    }
}
 
Example #17
Source File: KCoreVertexProgram.java    From grakn with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public void safeExecute(final Vertex vertex, Messenger<String> messenger, final Memory memory) {
    switch (memory.getIteration()) {
        case 0:
            sendMessage(messenger, EMPTY_MESSAGE);
            break;

        case 1: // get degree first, as degree must >= k
            filterByDegree(vertex, messenger, memory, true);
            break;

        default:
            if (memory.<Boolean>get(CONNECTED_COMPONENT_STARTED)) {
                if (messenger.receiveMessages().hasNext()) {
                    if (vertex.property(K_CORE_LABEL).isPresent()) {
                        updateClusterLabel(vertex, messenger, memory);
                    } else if (vertex.label().equals(Schema.BaseType.RELATION.name())) {
                        relayClusterLabel(messenger, memory);
                    }
                }
            } else {
                // relay message through relation vertices in even iterations
                // send message from regular entities in odd iterations
                if (atRelations(memory)) {
                    relayOrSaveMessages(vertex, messenger);
                } else {
                    updateEntityAndAttribute(vertex, messenger, memory, false);
                }
            }
            break;
    }
}
 
Example #18
Source File: CorenessVertexProgram.java    From grakn with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public void safeExecute(final Vertex vertex, Messenger<String> messenger, final Memory memory) {
    switch (memory.getIteration()) {
        case 0:
            sendMessage(messenger, EMPTY_MESSAGE);
            break;

        case 1: // get degree first, as degree must >= k
            filterByDegree(vertex, messenger, memory, false);
            break;

        default:
            if (memory.<Boolean>get(PERSIST_CORENESS) && vertex.property(K_CORE_LABEL).isPresent()) {
                // persist coreness
                vertex.property(CORENESS, memory.<Long>get(K) - 1L);

                // check if the vertex should included for the next k value
                if (vertex.<Long>value(MESSAGE_COUNT) < memory.<Long>get(K)) {
                    vertex.property(K_CORE_LABEL).remove();
                    break;
                }
            }

            // relay message through relation vertices in even iterations
            // send message from regular entities in odd iterations
            if (atRelations(memory)) {
                relayOrSaveMessages(vertex, messenger);
            } else {
                updateEntityAndAttribute(vertex, messenger, memory, true);
            }
            break;
    }
}
 
Example #19
Source File: ConnectedComponentsVertexProgram.java    From grakn with GNU Affero General Public License v3.0 5 votes vote down vote up
private static void updateClusterLabel(Vertex vertex, Messenger<String> messenger, Memory memory) {
    String currentMax = vertex.value(CLUSTER_LABEL);
    String max = IteratorUtils.reduce(messenger.receiveMessages(), currentMax,
            (a, b) -> a.compareTo(b) > 0 ? a : b);
    if (max.compareTo(currentMax) > 0) {
        vertex.property(CLUSTER_LABEL, max);
        messenger.sendMessage(messageScopeIn, max);
        messenger.sendMessage(messageScopeOut, max);
        memory.add(VOTE_TO_HALT, false);
    }
}
 
Example #20
Source File: ConnectedComponentsVertexProgram.java    From grakn with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public void safeExecute(final Vertex vertex, Messenger<String> messenger, final Memory memory) {
    if (memory.isInitialIteration()) {
        String id = vertex.id().toString();
        vertex.property(CLUSTER_LABEL, id);
        messenger.sendMessage(messageScopeIn, id);
        messenger.sendMessage(messageScopeOut, id);
    } else {
        updateClusterLabel(vertex, messenger, memory);
    }
}
 
Example #21
Source File: GraknVertexProgram.java    From grakn with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public void execute(Vertex vertex, Messenger<T> messenger, Memory memory) {
    // try to deal with ghost vertex issues by ignoring them
    if (Utility.isAlive(vertex)) {
        safeExecute(vertex, messenger, memory);
    }
}
 
Example #22
Source File: ShortestPathVertexProgram.java    From grakn with GNU Affero General Public License v3.0 5 votes vote down vote up
private void broadcastSourceMessages(Messenger<VertexMessage> messenger, Memory memory, String vertexId, List<MessageFromSource> incomingSourceMsg) {
    if (!incomingSourceMsg.isEmpty()) {
        MessageFromSource msg = incomingSourceMsg.get(0);
        MessageFromSource outgoingSrcMsg = new MessageFromSource(vertexId, msg.pathLength() + 1);
        broadcastToNeighbors(messenger, outgoingSrcMsg);
        LOG.debug("Iteration {}, Vertex {}: Relaying message {}.", memory.getIteration(), vertexId, outgoingSrcMsg);
    }
}
 
Example #23
Source File: ShortestPathVertexProgram.java    From grakn with GNU Affero General Public License v3.0 5 votes vote down vote up
private void broadcastDestinationMessages(Messenger<VertexMessage> messenger, Memory memory, String vertexId, List<MessageFromDestination> incomingDestMsg) {
    if (!incomingDestMsg.isEmpty()) {
        MessageFromDestination msg = incomingDestMsg.get(0);
        MessageFromDestination outgoingDstMsg = new MessageFromDestination(vertexId, msg.pathLength() + 1);
        broadcastToNeighbors(messenger, outgoingDstMsg);
        LOG.debug("Iteration {}, Vertex {}: Relaying message {}", memory.getIteration(), vertexId, outgoingDstMsg);
    }
}
 
Example #24
Source File: ProgramTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Override
public void execute(final Vertex vertex, final Messenger messenger, final Memory memory) {
    assertFalse(memory.exists(TraversalVertexProgram.HALTED_TRAVERSERS));
    final TraverserGenerator generator = this.traversal.get().getTraverserGenerator();
    MemoryTraversalSideEffects.setMemorySideEffects(this.traversal.get(), memory, ProgramPhase.EXECUTE);
    this.checkSideEffects();
    final TraverserSet<Vertex> activeTraversers = memory.get(TraversalVertexProgram.ACTIVE_TRAVERSERS);
    if (vertex.label().equals("software")) {
        assertEquals(1, activeTraversers.stream().filter(v -> v.get().equals(vertex)).count());
        if (memory.isInitialIteration()) {
            assertFalse(vertex.property(TraversalVertexProgram.HALTED_TRAVERSERS).isPresent());
            vertex.property(
                    TraversalVertexProgram.HALTED_TRAVERSERS,
                    new TraverserSet<>(generator.generate(vertex.value("name"), this.programStep, 1l)));
        } else {
            assertTrue(vertex.property(TraversalVertexProgram.HALTED_TRAVERSERS).isPresent());
        }
    } else {
        assertFalse(vertex.property(TraversalVertexProgram.HALTED_TRAVERSERS).isPresent());
        assertEquals(0, activeTraversers.stream().filter(v -> v.get().equals(vertex)).count());
        if (!memory.isInitialIteration()) {
            if (vertex.value("name").equals("marko"))
                memory.add(TraversalVertexProgram.HALTED_TRAVERSERS, new TraverserSet<>(generator.generate("marko-is-my-name", this.programStep, 1l)));
            else if (vertex.value("name").equals("vadas"))
                this.traversal.get().getSideEffects().add(TraversalVertexProgram.HALTED_TRAVERSERS, new TraverserSet<>(generator.generate("the-v-o-double-g", this.programStep, 1l)));
        }
    }
}
 
Example #25
Source File: KCoreVertexProgram.java    From grakn with GNU Affero General Public License v3.0 4 votes vote down vote up
static void sendMessage(Messenger<String> messenger, String message) {
    messenger.sendMessage(messageScopeIn, message);
    messenger.sendMessage(messageScopeOut, message);
}
 
Example #26
Source File: ConnectedComponentVertexProgram.java    From tinkerpop with Apache License 2.0 4 votes vote down vote up
@Override
public void execute(final Vertex vertex, final Messenger<String> messenger, final Memory memory) {
    if (memory.isInitialIteration()) {
        copyHaltedTraversersFromMemory(vertex);

        // on the first pass, just initialize the component to its own id then pass it to all adjacent vertices
        // for evaluation
        vertex.property(VertexProperty.Cardinality.single, property, vertex.id().toString());

        // vertices that have no edges remain in their own component - nothing to message pass here
        if (vertex.edges(Direction.BOTH).hasNext()) {
            // since there was message passing we don't want to halt on the first round. this should only trigger
            // a single pass finish if the graph is completely disconnected (technically, it won't even really
            // work in cases where halted traversers come into play
            messenger.sendMessage(scope, vertex.id().toString());
            memory.add(VOTE_TO_HALT, false);
        }
    } else {
        // by the second iteration all vertices that matter should have a component assigned
        String currentComponent = vertex.value(property);
        boolean different = false;

        // iterate through messages received and determine if there is a component that has a lesser value than
        // the currently assigned one
        final Iterator<String> componentIterator = messenger.receiveMessages();
        while(componentIterator.hasNext()) {
            final String candidateComponent = componentIterator.next();
            if (candidateComponent.compareTo(currentComponent) < 0) {
                currentComponent = candidateComponent;
                different = true;
            }
        }

        // if there is a better component then assign it and notify adjacent vertices. triggering the message
        // passing should not halt future executions
        if (different) {
            vertex.property(VertexProperty.Cardinality.single, property, currentComponent);
            messenger.sendMessage(scope, currentComponent);
            memory.add(VOTE_TO_HALT, false);
        }
    }
}
 
Example #27
Source File: TraversalVertexProgram.java    From tinkerpop with Apache License 2.0 4 votes vote down vote up
@Override
public void execute(final Vertex vertex, final Messenger<TraverserSet<Object>> messenger, final Memory memory) {
    // if any global halted traversers, simply don't use them as they were handled by master setup()
    // these halted traversers are typically from a previous OLAP job that yielded traversers at the master traversal
    if (null != this.haltedTraversers)
        this.haltedTraversers = null;
    // memory is distributed
    MemoryTraversalSideEffects.setMemorySideEffects(this.traversal.get(), memory, ProgramPhase.EXECUTE);
    // if a barrier was completed in another worker, it is also completed here (ensure distributed barriers are synchronized)
    final Set<String> completedBarriers = memory.get(COMPLETED_BARRIERS);
    for (final String stepId : completedBarriers) {
        final Step<?, ?> step = this.traversalMatrix.getStepById(stepId);
        if (step instanceof Barrier)
            ((Barrier) this.traversalMatrix.getStepById(stepId)).done();
    }
    // define halted traversers
    final VertexProperty<TraverserSet<Object>> property = vertex.property(HALTED_TRAVERSERS);
    final TraverserSet<Object> haltedTraversers;
    if (property.isPresent()) {
        haltedTraversers = property.value();
    } else {
        haltedTraversers = new TraverserSet<>();
        vertex.property(VertexProperty.Cardinality.single, HALTED_TRAVERSERS, haltedTraversers);
    }
    //////////////////
    if (memory.isInitialIteration()) {    // ITERATION 1
        final TraverserSet<Object> activeTraversers = new TraverserSet<>();
        // if halted traversers are being sent from a previous VertexProgram in an OLAP chain (distributed traversers), get them into the flow
        IteratorUtils.removeOnNext(haltedTraversers.iterator()).forEachRemaining(traverser -> {
            traverser.setStepId(this.traversal.get().getStartStep().getId());
            activeTraversers.add(traverser);
        });
        assert haltedTraversers.isEmpty();
        // for g.V()/E()
        if (this.traversal.get().getStartStep() instanceof GraphStep) {
            final GraphStep<Element, Element> graphStep = (GraphStep<Element, Element>) this.traversal.get().getStartStep();
            graphStep.reset();
            activeTraversers.forEach(traverser -> graphStep.addStart((Traverser.Admin) traverser));
            activeTraversers.clear();
            if (graphStep.returnsVertex())
                graphStep.setIteratorSupplier(() -> ElementHelper.idExists(vertex.id(), graphStep.getIds()) ? (Iterator) IteratorUtils.of(vertex) : EmptyIterator.instance());
            else
                graphStep.setIteratorSupplier(() -> (Iterator) IteratorUtils.filter(vertex.edges(Direction.OUT), edge -> ElementHelper.idExists(edge.id(), graphStep.getIds())));
            graphStep.forEachRemaining(traverser -> {
                if (traverser.isHalted()) {
                    if (this.returnHaltedTraversers)
                        memory.add(HALTED_TRAVERSERS, new TraverserSet<>(this.haltedTraverserStrategy.halt(traverser)));
                    else
                        haltedTraversers.add((Traverser.Admin) traverser.detach());
                } else
                    activeTraversers.add((Traverser.Admin) traverser);
            });
        }
        memory.add(VOTE_TO_HALT, activeTraversers.isEmpty() || WorkerExecutor.execute(vertex, new SingleMessenger<>(messenger, activeTraversers), this.traversalMatrix, memory, this.returnHaltedTraversers, haltedTraversers, this.haltedTraverserStrategy));
    } else   // ITERATION 1+
        memory.add(VOTE_TO_HALT, WorkerExecutor.execute(vertex, messenger, this.traversalMatrix, memory, this.returnHaltedTraversers, haltedTraversers, this.haltedTraverserStrategy));
    // save space by not having an empty halted traversers property
    if (this.returnHaltedTraversers || haltedTraversers.isEmpty())
        vertex.<TraverserSet>property(HALTED_TRAVERSERS).remove();
}
 
Example #28
Source File: CloneVertexProgram.java    From tinkerpop with Apache License 2.0 4 votes vote down vote up
@Override
public void execute(final Vertex sourceVertex, final Messenger<Tuple> messenger, final Memory memory) {
}
 
Example #29
Source File: SingleMessenger.java    From tinkerpop with Apache License 2.0 4 votes vote down vote up
public SingleMessenger(final Messenger<M> baseMessenger, final M message) {
    this.baseMessenger = baseMessenger;
    this.message = message;
}
 
Example #30
Source File: ConnectedComponentVertexProgram.java    From grakn with GNU Affero General Public License v3.0 4 votes vote down vote up
private static void update(Vertex vertex, Messenger<Boolean> messenger, Memory memory, String label) {
    messenger.sendMessage(messageScopeIn, MESSAGE);
    messenger.sendMessage(messageScopeOut, MESSAGE);
    vertex.property(CLUSTER_LABEL, label);
    memory.add(VOTE_TO_HALT, false);
}