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

The following examples show how to use org.apache.tinkerpop.gremlin.process.computer.Memory#isInitialIteration() . 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: ConnectedComponentVertexProgram.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Override
public boolean terminate(final Memory memory) {
    if (memory.isInitialIteration() && this.haltedTraversersIndex != null) {
        this.haltedTraversersIndex.clear();
    }

    final boolean voteToHalt = memory.<Boolean>get(VOTE_TO_HALT);
    if (voteToHalt) {
        return true;
    } else {
        // it is basically always assumed that the program will want to halt, but if message passing occurs, the
        // program will want to continue, thus reset false values to true for future iterations
        memory.set(VOTE_TO_HALT, true);
        return false;
    }
}
 
Example 3
Source File: ShortestPathVertexProgram.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Override
public boolean terminate(final Memory memory) {
    if (memory.isInitialIteration() && this.haltedTraversersIndex != null) {
        this.haltedTraversersIndex.clear();
    }
    final boolean voteToHalt = memory.get(VOTE_TO_HALT);
    if (voteToHalt) {
        final int state = memory.get(STATE);
        if (state == COLLECT_PATHS) {
            // After paths were collected,
            // a) the VP is done in standalone mode (paths will be in memory) or
            // b) the halted traversers will be updated in order to have the paths available in the traversal
            if (this.standalone) return true;
            memory.set(STATE, UPDATE_HALTED_TRAVERSERS);
            return false;
        }
        if (state == UPDATE_HALTED_TRAVERSERS) return true;
        else memory.set(STATE, COLLECT_PATHS); // collect paths if no new paths were found
        return false;
    } else {
        memory.set(VOTE_TO_HALT, true);
        return false;
    }
}
 
Example 4
Source File: ProgramTest.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Override
public boolean terminate(final Memory memory) {
    final TraverserGenerator generator = this.traversal.get().getTraverserGenerator();
    MemoryTraversalSideEffects.setMemorySideEffects(this.traversal.get(), memory, ProgramPhase.TERMINATE);
    checkSideEffects();
    if (memory.isInitialIteration()) {
        assertFalse(memory.exists(TraversalVertexProgram.HALTED_TRAVERSERS));
        return false;
    } else {
        ///
        assertTrue(memory.exists(TraversalVertexProgram.HALTED_TRAVERSERS));
        final TraverserSet<String> haltedTraversers = memory.get(TraversalVertexProgram.HALTED_TRAVERSERS);
        haltedTraversers.add(generator.generate("hello", this.programStep, 1l));
        haltedTraversers.add(generator.generate("gremlin", this.programStep, 1l));
        memory.set(TraversalVertexProgram.HALTED_TRAVERSERS, haltedTraversers);
        return true;
    }
}
 
Example 5
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 6
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 7
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 8
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 9
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 10
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 11
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 12
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 13
Source File: DegreeVertexProgram.java    From grakn with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public Set<MessageScope> getMessageScopes(Memory memory) {
    return memory.isInitialIteration() ?
            Sets.newHashSet(messageScopeResourceIn, messageScopeOut) : Collections.emptySet();
}
 
Example 14
Source File: CountVertexProgram.java    From grakn with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public Set<MessageScope> getMessageScopes(final Memory memory) {
    return memory.isInitialIteration() ? messageScopeSetInAndOut : Collections.emptySet();
}
 
Example 15
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 16
Source File: PeerPressureVertexProgram.java    From tinkerpop with Apache License 2.0 4 votes vote down vote up
@Override
public Set<MessageScope> getMessageScopes(final Memory memory) {
    final Set<MessageScope> VOTE_SCOPE = new HashSet<>(Collections.singletonList(this.voteScope));
    final Set<MessageScope> COUNT_SCOPE = new HashSet<>(Collections.singletonList(this.countScope));
    return this.distributeVote && memory.isInitialIteration() ? COUNT_SCOPE : VOTE_SCOPE;
}