Java Code Examples for org.apache.tinkerpop.gremlin.process.computer.Messenger#sendMessage()

The following examples show how to use org.apache.tinkerpop.gremlin.process.computer.Messenger#sendMessage() . 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: 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 2
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 3
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 4
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 5
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 6
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 7
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 8
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 9
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 10
Source File: ShortestPathVertexProgram.java    From grakn with GNU Affero General Public License v3.0 4 votes vote down vote up
private void broadcastToNeighbors(Messenger<VertexMessage> messenger, VertexMessage message) {
    messenger.sendMessage(inEdge, message);
    messenger.sendMessage(outEdge, message);
}
 
Example 11
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 12
Source File: DegreeVertexProgram.java    From grakn with GNU Affero General Public License v3.0 4 votes vote down vote up
private void degreeMessagePassing(Messenger<Long> messenger) {
    messenger.sendMessage(messageScopeResourceIn, 1L);
    messenger.sendMessage(messageScopeOut, 1L);
}
 
Example 13
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);
}
 
Example 14
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);
        }
    }
}