Java Code Examples for org.apache.tinkerpop.gremlin.process.computer.Memory#getIteration()

The following examples show how to use org.apache.tinkerpop.gremlin.process.computer.Memory#getIteration() . 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: TraversalVertexProgram.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Override
public void workerIterationEnd(final Memory memory) {
    // store profile metrics in proper ProfileStep metrics
    if (this.profile) {
        final List<ProfileStep> profileSteps = TraversalHelper.getStepsOfAssignableClassRecursively(ProfileStep.class, this.traversal.get());
        // guess the profile step to store data
        int profileStepIndex = memory.getIteration();
        // if we guess wrongly write timing into last step
        profileStepIndex = profileStepIndex >= profileSteps.size() ? profileSteps.size() - 1 : profileStepIndex;
        this.iterationMetrics.finish(0);
        // reset counts
        this.iterationMetrics.setCount(TraversalMetrics.TRAVERSER_COUNT_ID,0);
        if (null != MemoryTraversalSideEffects.getMemorySideEffectsPhase(this.traversal.get())) {
            this.traversal.get().getSideEffects().add(profileSteps.get(profileStepIndex).getId(), this.iterationMetrics);
        }
        this.iterationMetrics = null;
    }
}
 
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: 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 4
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 5
Source File: TraversalVertexProgram.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Override
public void workerIterationStart(final Memory memory) {
    // start collecting profile metrics
    if (this.profile) {
        this.iterationMetrics = new MutableMetrics("iteration" + memory.getIteration(), "Worker Iteration " + memory.getIteration());
        this.iterationMetrics.start();
    }
}
 
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 boolean terminate(final Memory memory) {
    LOGGER.debug("Finished Iteration " + memory.getIteration());
    if (memory.isInitialIteration()) return false;
    if (memory.<Boolean>get(VOTE_TO_HALT)) {
        return true;
    }
    if (memory.getIteration() == MAX_ITERATION) {
        LOGGER.debug("Reached Max Iteration: " + MAX_ITERATION + " !!!!!!!!");
        throw GraqlQueryException.maxIterationsReached(this.getClass());
    }

    memory.set(VOTE_TO_HALT, true);
    return false;
}
 
Example 8
Source File: DegreeStatisticsVertexProgram.java    From grakn with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public Set<MessageScope> getMessageScopes(final Memory memory) {
    switch (memory.getIteration()) {
        case 0:
            return Sets.newHashSet(messageScopeShortcutIn, messageScopeResourceOut);
        case 1:
            return Collections.singleton(messageScopeShortcutOut);
        default:
            return Collections.emptySet();
    }
}
 
Example 9
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 10
Source File: KCoreVertexProgram.java    From grakn with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public boolean terminate(final Memory memory) {
    LOGGER.debug("Finished Iteration {}", memory.getIteration());
    if (memory.isInitialIteration()) return false;

    if (memory.getIteration() == MAX_ITERATION) {
        LOGGER.debug("Reached Max Iteration: {}", MAX_ITERATION);
        throw GraqlQueryException.maxIterationsReached(this.getClass());
    }

    if (memory.<Boolean>get(CONNECTED_COMPONENT_STARTED)) {
        if (memory.<Boolean>get(VOTE_TO_HALT)) {
            LOGGER.debug("KCoreVertexProgram Finished");
            return true; // connected component is done
        } else {
            memory.set(VOTE_TO_HALT, true);
            return false;
        }
    } else {
        if (!atRelations(memory)) {
            if (!memory.<Boolean>get(K_CORE_EXIST)) {
                LOGGER.debug("KCoreVertexProgram Finished");
                LOGGER.debug("No Such Core Areas Found");
                throw new NoResultException();
            } else {
                if (memory.<Boolean>get(K_CORE_STABLE)) {
                    memory.set(CONNECTED_COMPONENT_STARTED, true);
                    LOGGER.debug("Found Core Areas");
                    LOGGER.debug("Starting Connected Components");
                } else {
                    memory.set(K_CORE_EXIST, false);
                    memory.set(K_CORE_STABLE, true);
                }
                return false;
            }
        } else {
            return false; // can not end after relayOrSaveMessages
        }
    }
}
 
Example 11
Source File: PeerPressureVertexProgram.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Override
public boolean terminate(final Memory memory) {
    final boolean voteToHalt = memory.<Boolean>get(VOTE_TO_HALT) || memory.getIteration() >= (this.distributeVote ? this.maxIterations + 1 : this.maxIterations);
    if (voteToHalt) {
        return true;
    } else {
        memory.set(VOTE_TO_HALT, true);
        return false;
    }
}
 
Example 12
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 13
Source File: CorenessVertexProgram.java    From grakn with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public boolean terminate(final Memory memory) {
    if (memory.isInitialIteration()) {
        LOGGER.debug("Finished Iteration {}", memory.getIteration());
        return false;
    }

    if (memory.getIteration() == MAX_ITERATION) {
        LOGGER.debug("Reached Max Iteration: {}", MAX_ITERATION);
        throw GraqlQueryException.maxIterationsReached(this.getClass());
    }

    if (memory.<Boolean>get(PERSIST_CORENESS)) {
        memory.set(PERSIST_CORENESS, false);
    }
    if (!atRelations(memory)) {
        LOGGER.debug("UpdateEntityAndAttribute... Finished Iteration {}", memory.getIteration());
        if (!memory.<Boolean>get(K_CORE_EXIST)) {
            LOGGER.debug("KCoreVertexProgram Finished");
            return true;
        } else {
            if (memory.<Boolean>get(K_CORE_STABLE)) {
                LOGGER.debug("Found Core Areas K = {}\n", memory.<Long>get(K));
                memory.set(K, memory.<Long>get(K) + 1L);
                memory.set(PERSIST_CORENESS, true);
            } else {
                memory.set(K_CORE_STABLE, true);
            }
            memory.set(K_CORE_EXIST, false);
            return false;
        }
    } else {
        LOGGER.debug("RelayOrSaveMessage...       Finished Iteration {}", memory.getIteration());
        return false; // can not end after relayOrSaveMessages
    }
}
 
Example 14
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 15
Source File: MedianVertexProgram.java    From grakn with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public Set<MessageScope> getMessageScopes(final Memory memory) {
    switch (memory.getIteration()) {
        case 0:
            return Sets.newHashSet(messageScopeShortcutIn, messageScopeResourceOut);
        case 1:
            return Collections.singleton(messageScopeShortcutOut);
        default:
            return Collections.emptySet();
    }
}
 
Example 16
Source File: DegreeVertexProgram.java    From grakn with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public boolean terminate(Memory memory) {
    LOGGER.debug("Finished Degree Iteration " + memory.getIteration());
    return memory.getIteration() == 1;
}
 
Example 17
Source File: DegreeStatisticsVertexProgram.java    From grakn with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public boolean terminate(final Memory memory) {
    LOGGER.debug("Finished Degree Iteration {}", memory.getIteration());
    return memory.getIteration() == 2;
}
 
Example 18
Source File: CountVertexProgram.java    From grakn with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public boolean terminate(final Memory memory) {
    LOGGER.debug("Finished Count Iteration {}", memory.getIteration());
    return memory.getIteration() == 1;
}
 
Example 19
Source File: MedianVertexProgram.java    From grakn with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public boolean terminate(final Memory memory) {
    LOGGER.debug("Finished Iteration {}", memory.getIteration());

    if (memory.getIteration() == 2) {
        memory.set(INDEX_START, 0L);
        memory.set(INDEX_END, memory.<Long>get(COUNT) - 1L);
        memory.set(INDEX_MEDIAN, (memory.<Long>get(COUNT) - 1L) / 2L);

        LOGGER.debug("count: {}", memory.<Long>get(COUNT));
        LOGGER.debug("first pivot: {}", memory.<Long>get(PIVOT));

    } else if (memory.getIteration() > 2) {

        long indexNegativeEnd = memory.<Long>get(INDEX_START) + memory.<Long>get(NEGATIVE_COUNT) - 1;
        long indexPositiveStart = memory.<Long>get(INDEX_END) - memory.<Long>get(POSITIVE_COUNT) + 1;

        LOGGER.debug("pivot: {}", memory.<Long>get(PIVOT));

        LOGGER.debug("{}, {}", memory.<Long>get(INDEX_START), indexNegativeEnd);
        LOGGER.debug("{}, {}", indexPositiveStart, memory.<Long>get(INDEX_END));

        LOGGER.debug("negative count: {}", memory.<Long>get(NEGATIVE_COUNT));
        LOGGER.debug("positive count: {}", memory.<Long>get(POSITIVE_COUNT));

        LOGGER.debug("negative pivot: {}", memory.<Long>get(PIVOT_NEGATIVE));
        LOGGER.debug("positive pivot: {}", memory.<Long>get(PIVOT_POSITIVE));

        if (indexNegativeEnd < memory.<Long>get(INDEX_MEDIAN)) {
            if (indexPositiveStart > memory.<Long>get(INDEX_MEDIAN)) {
                memory.set(FOUND, true);
                LOGGER.debug("FOUND IT!!!");
            } else {
                memory.set(INDEX_START, indexPositiveStart);
                memory.set(PIVOT, memory.get(PIVOT_POSITIVE));
                memory.set(LABEL_SELECTED, memory.getIteration());
                LOGGER.debug("new pivot: {}", memory.<Long>get(PIVOT));
            }
        } else {
            memory.set(INDEX_END, indexNegativeEnd);
            memory.set(PIVOT, memory.get(PIVOT_NEGATIVE));
            memory.set(LABEL_SELECTED, -memory.getIteration());
            LOGGER.debug("new pivot: {}", memory.<Long>get(PIVOT));
        }
        memory.set(MEDIAN, memory.get(PIVOT));

        memory.set(POSITIVE_COUNT, 0L);
        memory.set(NEGATIVE_COUNT, 0L);
    }

    return memory.<Boolean>get(FOUND) || memory.getIteration() >= MAX_ITERATION;
}
 
Example 20
Source File: ShortestDistanceVertexProgram.java    From titan1withtp3.1 with Apache License 2.0 4 votes vote down vote up
@Override
public boolean terminate(final Memory memory) {
    return memory.getIteration() >= this.maxDepth;
}