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

The following examples show how to use org.apache.tinkerpop.gremlin.process.computer.Memory#set() . 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: 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 2
Source File: MedianVertexProgram.java    From grakn with GNU Affero General Public License v3.0 6 votes vote down vote up
@Override
public void setup(final Memory memory) {
    LOGGER.debug("MedianVertexProgram Started !!!!!!!!");
    memory.set(COUNT, 0L);
    memory.set(LABEL_SELECTED, memory.getIteration());
    memory.set(NEGATIVE_COUNT, 0L);
    memory.set(POSITIVE_COUNT, 0L);
    memory.set(FOUND, false);
    if (persistentProperties.get(ATTRIBUTE_VALUE_TYPE).equals(Schema.VertexProperty.VALUE_LONG.name())) {
        memory.set(MEDIAN, 0L);
        memory.set(PIVOT, 0L);
        memory.set(PIVOT_NEGATIVE, 0L);
        memory.set(PIVOT_POSITIVE, 0L);
    } else {
        memory.set(MEDIAN, 0D);
        memory.set(PIVOT, 0D);
        memory.set(PIVOT_NEGATIVE, 0D);
        memory.set(PIVOT_POSITIVE, 0D);
    }
}
 
Example 3
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 4
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 5
Source File: ProgramTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Override
public void setup(final Memory memory) {
    MemoryTraversalSideEffects.setMemorySideEffects(this.traversal.get(), memory, ProgramPhase.SETUP);
    final Map<Vertex, Long> map = (Map<Vertex, Long>) this.haltedTraversers.iterator().next().get();
    assertEquals(2, map.size());
    assertTrue(map.values().contains(3l));
    assertTrue(map.values().contains(1l));
    final IndexedTraverserSet<Object,Vertex> activeTraversers = new IndexedTraverserSet.VertexIndexedTraverserSet();
    map.keySet().forEach(vertex -> activeTraversers.add(this.haltedTraversers.peek().split(vertex, EmptyStep.instance())));
    this.haltedTraversers.clear();
    this.checkSideEffects();
    memory.set(TraversalVertexProgram.ACTIVE_TRAVERSERS, activeTraversers);
}
 
Example 6
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 7
Source File: ShortestPathVertexProgram.java    From grakn with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public void setup(final Memory memory) {
    memory.set(atLeastOneVertexActive, false);
    memory.set(shortestPathLength, SHORTEST_PATH_LENGTH_NOT_YET_SET);
    memory.set(allShortestPathsFound_TerminateAtTheEndOfThisIteration, false);
    memory.set(SHORTEST_PATH, new HashMap<String, Set<String>>());
}
 
Example 8
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 9
Source File: KCoreVertexProgram.java    From grakn with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public void setup(final Memory memory) {
    LOGGER.debug("KCoreVertexProgram Started !!!!!!!!");

    // K_CORE_STABLE is true by default, and we reset it after each odd iteration.
    memory.set(K_CORE_STABLE, false);
    memory.set(K_CORE_EXIST, false);
    memory.set(K, persistentProperties.get(K));
    memory.set(VOTE_TO_HALT, true);
    memory.set(CONNECTED_COMPONENT_STARTED, false);
}
 
Example 10
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 11
Source File: CorenessVertexProgram.java    From grakn with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public void setup(final Memory memory) {
    LOGGER.debug("KCoreVertexProgram Started !!!!!!!!");

    // K_CORE_STABLE is true by default, and we reset it after each odd iteration.
    memory.set(K_CORE_STABLE, false);
    memory.set(K_CORE_EXIST, false);
    memory.set(PERSIST_CORENESS, false);
    memory.set(K, persistentProperties.get(K));
}
 
Example 12
Source File: ConnectedComponentsVertexProgram.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.getIteration() < 2) 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 13
Source File: ConnectedComponentVertexProgram.java    From grakn with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public void setup(final Memory memory) {
    LOGGER.debug("ConnectedComponentVertexProgram Started !!!!!!!!");
    memory.set(VOTE_TO_HALT, true);
}
 
Example 14
Source File: PageRankVertexProgram.java    From tinkerpop with Apache License 2.0 4 votes vote down vote up
@Override
public void setup(final Memory memory) {
    memory.set(TELEPORTATION_ENERGY, null == this.initialRankTraversal ? 1.0d : 0.0d);
    memory.set(VERTEX_COUNT, 0.0d);
    memory.set(CONVERGENCE_ERROR, 1.0d);
}
 
Example 15
Source File: ConnectedComponentVertexProgram.java    From tinkerpop with Apache License 2.0 4 votes vote down vote up
@Override
public void setup(final Memory memory) {
    memory.set(VOTE_TO_HALT, true);
}
 
Example 16
Source File: PeerPressureVertexProgram.java    From tinkerpop with Apache License 2.0 4 votes vote down vote up
@Override
public void setup(final Memory memory) {
    memory.set(VOTE_TO_HALT, false);
}
 
Example 17
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 18
Source File: ShortestPathVertexProgram.java    From tinkerpop with Apache License 2.0 4 votes vote down vote up
@Override
public void setup(final Memory memory) {
    memory.set(VOTE_TO_HALT, true);
    memory.set(STATE, SEARCH);
}
 
Example 19
Source File: TraversalVertexProgram.java    From tinkerpop with Apache License 2.0 4 votes vote down vote up
@Override
public boolean terminate(final Memory memory) {
    // memory is local
    MemoryTraversalSideEffects.setMemorySideEffects(this.traversal.get(), memory, ProgramPhase.TERMINATE);
    final boolean voteToHalt = memory.<Boolean>get(VOTE_TO_HALT);
    memory.set(VOTE_TO_HALT, true);
    memory.set(ACTIVE_TRAVERSERS, new IndexedTraverserSet.VertexIndexedTraverserSet());
    if (voteToHalt) {
        // local traverser sets to process
        final TraverserSet<Object> toProcessTraversers = new TraverserSet<>();
        // traversers that need to be sent back to the workers (no longer can be processed locally by the master traversal)
        final IndexedTraverserSet<Object,Vertex> remoteActiveTraversers = new IndexedTraverserSet.VertexIndexedTraverserSet();
        // halted traversers that have completed their journey
        final TraverserSet<Object> haltedTraversers = memory.get(HALTED_TRAVERSERS);
        // get all barrier traversers
        final Set<String> completedBarriers = new HashSet<>();
        MasterExecutor.processMemory(this.traversalMatrix, memory, toProcessTraversers, completedBarriers);
        // process all results from barriers locally and when elements are touched, put them in remoteActiveTraversers
        MasterExecutor.processTraversers(this.traversal, this.traversalMatrix, toProcessTraversers, remoteActiveTraversers, haltedTraversers, this.haltedTraverserStrategy);
        // tell parallel barriers that might not have been active in the last round that they are no longer active
        memory.set(COMPLETED_BARRIERS, completedBarriers);
        if (!remoteActiveTraversers.isEmpty() ||
                completedBarriers.stream().map(this.traversalMatrix::getStepById).filter(step -> step instanceof LocalBarrier).findAny().isPresent()) {
            // send active traversers back to workers
            memory.set(ACTIVE_TRAVERSERS, remoteActiveTraversers);
            return false;
        } else {
            // finalize locally with any last traversers dangling in the local traversal
            final Step<?, Object> endStep = (Step<?, Object>) this.traversal.get().getEndStep();
            while (endStep.hasNext()) {
                haltedTraversers.add(this.haltedTraverserStrategy.halt(endStep.next()));
            }
            // the result of a TraversalVertexProgram are the halted traversers
            memory.set(HALTED_TRAVERSERS, haltedTraversers);
            // finalize profile side-effect steps. (todo: try and hide this)
            for (final ProfileSideEffectStep profileStep : TraversalHelper.getStepsOfAssignableClassRecursively(ProfileSideEffectStep.class, this.traversal.get())) {
                this.traversal.get().getSideEffects().set(profileStep.getSideEffectKey(), profileStep.generateFinalResult(this.traversal.get().getSideEffects().get(profileStep.getSideEffectKey())));
            }
            return true;
        }
    } else {
        return false;
    }
}
 
Example 20
Source File: ConnectedComponentsVertexProgram.java    From grakn with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public void setup(final Memory memory) {
    LOGGER.debug("ConnectedComponentsVertexProgram Started !!!!!!!!");
    memory.set(VOTE_TO_HALT, true);
}