Java Code Examples for org.apache.tinkerpop.gremlin.process.computer.Memory#add()
The following examples show how to use
org.apache.tinkerpop.gremlin.process.computer.Memory#add() .
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: KCoreVertexProgram.java From grakn with GNU Affero General Public License v3.0 | 6 votes |
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 2
Source File: ShortestPathVertexProgram.java From grakn with GNU Affero General Public License v3.0 | 5 votes |
private Map<String, Set<String>> recordShortestPath_AndMarkBroadcasted(Vertex vertex, Memory memory, String vertexId, List<MessageFromDestination> destMsgs, long pathLength) { Map<String, Set<String>> msg = new HashMap<>(Collections.singletonMap(vertexId, destMsgs.stream().map(MessageFromDestination::vertexId).collect(Collectors.toSet()))); memory.add(SHORTEST_PATH, msg); memory.add(shortestPathLength, pathLength); set(vertex, shortestPathRecordedAndBroadcasted, true); LOG.debug("Iteration {}, Vertex {}: is the shortest path. Record({})", memory.getIteration(), vertexId, msg); return msg; }
Example 3
Source File: ShortestPathVertexProgram.java From grakn with GNU Affero General Public License v3.0 | 5 votes |
private void markBroadcasted_TerminateAtTheEndOfThisIeration(Vertex vertex, Memory memory, String vertexId, List<MessageFromSource> incomingSourceMsg, long pathLength) { Map<String, Set<String>> msg = new HashMap<>(Collections.singletonMap(vertexId, incomingSourceMsg.stream().map(MessageFromSource::vertexId).collect(Collectors.toSet()))); // memory.add(SHORTEST_PATH, msg); do not record memory.add(shortestPathLength, pathLength); set(vertex, shortestPathRecordedAndBroadcasted, true); memory.add(allShortestPathsFound_TerminateAtTheEndOfThisIteration, true); LOG.debug("Iteration {}, Vertex {}: received {}. 'compute new-path' finished. Terminating...", memory.getIteration(), vertexId, msg); }
Example 4
Source File: ConnectedComponentsVertexProgram.java From grakn with GNU Affero General Public License v3.0 | 5 votes |
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: KCoreVertexProgram.java From grakn with GNU Affero General Public License v3.0 | 5 votes |
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 6
Source File: KCoreVertexProgram.java From grakn with GNU Affero General Public License v3.0 | 5 votes |
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 7
Source File: KCoreVertexProgram.java From grakn with GNU Affero General Public License v3.0 | 5 votes |
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 8
Source File: PageRankVertexProgram.java From tinkerpop with Apache License 2.0 | 5 votes |
@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 9
Source File: ShortestPathVertexProgram.java From tinkerpop with Apache License 2.0 | 5 votes |
/** * Move any valid path into the VP's memory. * @param vertex The current vertex. * @param memory The VertexProgram's memory. */ private void collectShortestPaths(final Vertex vertex, final Memory memory) { final VertexProperty<Map<Vertex, Pair<Number, Set<Path>>>> pathProperty = vertex.property(PATHS); if (pathProperty.isPresent()) { final Map<Vertex, Pair<Number, Set<Path>>> paths = pathProperty.value(); final List<Path> result = new ArrayList<>(); for (final Pair<Number, Set<Path>> pair : paths.values()) { for (final Path path : pair.getValue1()) { if (isEndVertex(vertex)) { if (this.distanceEqualsNumberOfHops || this.maxDistance == null || NumberHelper.compare(pair.getValue0(), this.maxDistance) <= 0) { result.add(path); } } } } pathProperty.remove(); memory.add(SHORTEST_PATHS, result); } }
Example 10
Source File: ProgramTest.java From tinkerpop with Apache License 2.0 | 5 votes |
@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 11
Source File: MedianVertexProgram.java From grakn with GNU Affero General Public License v3.0 | 4 votes |
private void updateMemoryPositive(Vertex vertex, Memory memory, Number value) { vertex.property(LABEL, memory.getIteration()); memory.add(POSITIVE_COUNT, vertex.value(DEGREE)); memory.add(PIVOT_POSITIVE, value); }
Example 12
Source File: MedianVertexProgram.java From grakn with GNU Affero General Public License v3.0 | 4 votes |
private void updateMemoryNegative(Vertex vertex, Memory memory, Number value) { vertex.property(LABEL, -memory.getIteration()); memory.add(NEGATIVE_COUNT, vertex.value(DEGREE)); memory.add(PIVOT_NEGATIVE, value); }
Example 13
Source File: ConnectedComponentVertexProgram.java From grakn with GNU Affero General Public License v3.0 | 4 votes |
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 |
@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); } } }