org.apache.tinkerpop.gremlin.process.computer.MessageScope Java Examples

The following examples show how to use org.apache.tinkerpop.gremlin.process.computer.MessageScope. 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: ConnectedComponentVertexProgram.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Override
public void loadState(final Graph graph, final Configuration config) {
    configuration = new BaseConfiguration();
    if (config != null) {
        ConfigurationUtils.copy(config, configuration);
    }

    if (configuration.containsKey(EDGE_TRAVERSAL)) {
        this.edgeTraversal = PureTraversal.loadState(configuration, EDGE_TRAVERSAL, graph);
        this.scope = MessageScope.Local.of(() -> this.edgeTraversal.get().clone());
    }

    scopes = new HashSet<>(Collections.singletonList(scope));

    this.property = configuration.getString(PROPERTY, COMPONENT);

    this.haltedTraversers = TraversalVertexProgram.loadHaltedTraversers(configuration);
    this.haltedTraversersIndex = new IndexedTraverserSet<>(v -> v);
    for (final Traverser.Admin<Vertex> traverser : this.haltedTraversers) {
        this.haltedTraversersIndex.add(traverser.split());
    }
}
 
Example #2
Source File: VertexMemoryHandler.java    From titan1withtp3.1 with Apache License 2.0 6 votes vote down vote up
public Stream<M> receiveMessages(MessageScope messageScope) {
    if (messageScope instanceof MessageScope.Global) {
        M message = vertexMemory.getMessage(vertexId,messageScope);
        if (message == null) return Stream.empty();
        else return Stream.of(message);
    } else {
        final MessageScope.Local<M> localMessageScope = (MessageScope.Local) messageScope;
        final Traversal<Vertex, Edge> reverseIncident = FulgoraUtil.getReverseElementTraversal(localMessageScope,vertex,vertex.tx());
        final BiFunction<M,Edge,M> edgeFct = localMessageScope.getEdgeFunction();

        return IteratorUtils.stream(reverseIncident)
                .map(e -> {
                    M msg = vertexMemory.getMessage(vertexMemory.getCanonicalId(((TitanEdge) e).otherVertex(vertex).longId()), localMessageScope);
                    return msg == null ? null : edgeFct.apply(msg, e);
                })
                .filter(m -> m != null);
    }
}
 
Example #3
Source File: VertexState.java    From titan1withtp3.1 with Apache License 2.0 6 votes vote down vote up
public synchronized void addMessage(M message, MessageScope scope, Map<MessageScope,Integer> scopeMap,
                                    MessageCombiner<M> combiner) {
    assert message!=null && scope!=null && combiner!=null;
    Preconditions.checkArgument(scopeMap.containsKey(scope),"Provided scope was not declared in the VertexProgram: %s",scope);
    assert scopeMap.containsKey(scope);
    initializeCurrentMessages(scopeMap);
    if (scopeMap.size()==1) {
        if (currentMessages==null) currentMessages = message;
        else currentMessages = combiner.combine(message,(M)currentMessages);
    } else {
        int pos = scopeMap.get(scope);
        Object[] msgs =  (Object[])currentMessages;
        if (msgs[pos]==null) msgs[pos]=message;
        else msgs[pos] = combiner.combine(message,(M)msgs[pos]);
    }
}
 
Example #4
Source File: PageRankVertexProgram.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Override
public void loadState(final Graph graph, final Configuration configuration) {
    if (configuration.containsKey(INITIAL_RANK_TRAVERSAL))
        this.initialRankTraversal = PureTraversal.loadState(configuration, INITIAL_RANK_TRAVERSAL, graph);
    if (configuration.containsKey(EDGE_TRAVERSAL)) {
        this.edgeTraversal = PureTraversal.loadState(configuration, EDGE_TRAVERSAL, graph);
        this.incidentMessageScope = MessageScope.Local.of(() -> this.edgeTraversal.get().clone());
        this.countMessageScope = MessageScope.Local.of(new MessageScope.Local.ReverseTraversalSupplier(this.incidentMessageScope));
    }
    this.alpha = configuration.getDouble(ALPHA, this.alpha);
    this.epsilon = configuration.getDouble(EPSILON, this.epsilon);
    this.maxIterations = configuration.getInt(MAX_ITERATIONS, 20);
    this.property = configuration.getString(PROPERTY, PAGE_RANK);
    this.vertexComputeKeys = new HashSet<>(Arrays.asList(
            VertexComputeKey.of(this.property, false),
            VertexComputeKey.of(EDGE_COUNT, true)));
    this.memoryComputeKeys = new HashSet<>(Arrays.asList(
            MemoryComputeKey.of(TELEPORTATION_ENERGY, Operator.sum, true, true),
            MemoryComputeKey.of(VERTEX_COUNT, Operator.sum, true, true),
            MemoryComputeKey.of(CONVERGENCE_ERROR, Operator.sum, false, true)));
}
 
Example #5
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 #6
Source File: SparkMessenger.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Override
public void sendMessage(final MessageScope messageScope, final M message) {
    if (messageScope instanceof MessageScope.Local) {
        final MessageScope.Local<M> localMessageScope = (MessageScope.Local) messageScope;
        final Traversal.Admin<Vertex, Edge> incidentTraversal = SparkMessenger.setVertexStart(localMessageScope.getIncidentTraversal().get().asAdmin(), this.vertex);
        final Direction direction = SparkMessenger.getOppositeDirection(incidentTraversal);

        // handle processing for BOTH given TINKERPOP-1862 where the target of the message is the one opposite
        // the current vertex
        incidentTraversal.forEachRemaining(edge -> {
            if (direction.equals(Direction.IN) || direction.equals(Direction.OUT))
                this.outgoingMessages.add(new Tuple2<>(edge.vertices(direction).next().id(), localMessageScope.getEdgeFunction().apply(message, edge)));
            else
                this.outgoingMessages.add(new Tuple2<>(edge instanceof StarGraph.StarOutEdge ? edge.inVertex().id() : edge.outVertex().id(), localMessageScope.getEdgeFunction().apply(message, edge)));

        });
    } else {
        ((MessageScope.Global) messageScope).vertices().forEach(v -> this.outgoingMessages.add(new Tuple2<>(v.id(), message)));
    }
}
 
Example #7
Source File: TinkerMessenger.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Override
    public Iterator<M> receiveMessages() {
        final MultiIterator<M> multiIterator = new MultiIterator<>();
        for (final MessageScope messageScope : this.messageBoard.receiveMessages.keySet()) {
//        for (final MessageScope messageScope : this.messageBoard.previousMessageScopes) {
            if (messageScope instanceof MessageScope.Local) {
                final MessageScope.Local<M> localMessageScope = (MessageScope.Local<M>) messageScope;
                final Traversal.Admin<Vertex, Edge> incidentTraversal = TinkerMessenger.setVertexStart(localMessageScope.getIncidentTraversal().get().asAdmin(), this.vertex);
                final Direction direction = TinkerMessenger.getDirection(incidentTraversal);
                final Edge[] edge = new Edge[1]; // simulates storage side-effects available in Gremlin, but not Java8 streams
                multiIterator.addIterator(StreamSupport.stream(Spliterators.spliteratorUnknownSize(VertexProgramHelper.reverse(incidentTraversal.asAdmin()), Spliterator.IMMUTABLE | Spliterator.SIZED), false)
                        .map((Edge e) -> {
                            edge[0] = e;
                            Vertex vv;
                            if (direction.equals(Direction.IN) || direction.equals(Direction.OUT)) {
                                vv = e.vertices(direction).next();
                            } else {
                                vv = e.outVertex() == this.vertex ? e.inVertex() : e.outVertex();
                            }
                            return this.messageBoard.receiveMessages.get(messageScope).get(vv);
                        })
                        .filter(q -> null != q)
                        .flatMap(Queue::stream)
                        .map(message -> localMessageScope.getEdgeFunction().apply(message, edge[0]))
                        .iterator());

            } else {
                multiIterator.addIterator(Stream.of(this.vertex)
                        .map(this.messageBoard.receiveMessages.get(messageScope)::get)
                        .filter(q -> null != q)
                        .flatMap(Queue::stream)
                        .iterator());
            }
        }
        return multiIterator;
    }
 
Example #8
Source File: TinkerMessenger.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Override
    public void sendMessage(final MessageScope messageScope, final M message) {
//        this.messageBoard.currentMessageScopes.add(messageScope);
        if (messageScope instanceof MessageScope.Local) {
            addMessage(this.vertex, message, messageScope);
        } else {
            ((MessageScope.Global) messageScope).vertices().forEach(v -> addMessage(v, message, messageScope));
        }
    }
 
Example #9
Source File: PeerPressureVertexProgram.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Override
public void loadState(final Graph graph, final Configuration configuration) {
    if (configuration.containsKey(INITIAL_VOTE_STRENGTH_TRAVERSAL))
        this.initialVoteStrengthTraversal = PureTraversal.loadState(configuration, INITIAL_VOTE_STRENGTH_TRAVERSAL, graph);
    if (configuration.containsKey(EDGE_TRAVERSAL)) {
        this.edgeTraversal = PureTraversal.loadState(configuration, EDGE_TRAVERSAL, graph);
        this.voteScope = MessageScope.Local.of(() -> this.edgeTraversal.get().clone());
        this.countScope = MessageScope.Local.of(new MessageScope.Local.ReverseTraversalSupplier(this.voteScope));
    }
    this.property = configuration.getString(PROPERTY, CLUSTER);
    this.maxIterations = configuration.getInt(MAX_ITERATIONS, 30);
    this.distributeVote = configuration.getBoolean(DISTRIBUTE_VOTE, false);
}
 
Example #10
Source File: VertexState.java    From titan1withtp3.1 with Apache License 2.0 5 votes vote down vote up
public synchronized void setMessage(M message, MessageScope scope, Map<MessageScope,Integer> scopeMap) {
    assert message!=null && scope!=null;
    Preconditions.checkArgument(scopeMap.containsKey(scope),"Provided scope was not declared in the VertexProgram: %s",scope);
    initializeCurrentMessages(scopeMap);
    if (scopeMap.size()==1) currentMessages = message;
    else ((Object[])currentMessages)[scopeMap.get(scope)]=message;
}
 
Example #11
Source File: TinkerMessenger.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
private void addMessage(final Vertex vertex, final M message, MessageScope messageScope) {
    this.messageBoard.sendMessages.compute(messageScope, (ms, messages) -> {
        if(null==messages) messages = new ConcurrentHashMap<>();
        return messages;
    });
    this.messageBoard.sendMessages.get(messageScope).compute(vertex, (v, queue) -> {
        if (null == queue) queue = new ConcurrentLinkedQueue<>();
        queue.add(null != this.combiner && !queue.isEmpty() ? this.combiner.combine(queue.remove(), message) : message);
        return queue;
    });
}
 
Example #12
Source File: FulgoraUtil.java    From titan1withtp3.1 with Apache License 2.0 5 votes vote down vote up
private static FulgoraElementTraversal<Vertex,Edge> getReverseTraversal(final MessageScope.Local<?> scope,
                                                  final TitanTransaction graph, @Nullable final Vertex start) {
    Traversal.Admin<Vertex,Edge> incident = scope.getIncidentTraversal().get().asAdmin();
    FulgoraElementTraversal<Vertex,Edge> result = FulgoraElementTraversal.of(graph);

    for (Step step : incident.getSteps()) result.addStep(step);
    Step<Vertex,?> startStep = result.getStartStep();
    assert startStep instanceof VertexStep;
    ((VertexStep) startStep).reverseDirection();

    if (start!=null) result.addStep(0, new StartStep<>(incident, start));
    result.asAdmin().setStrategies(FULGORA_STRATEGIES);
    return result;
}
 
Example #13
Source File: FulgoraUtil.java    From titan1withtp3.1 with Apache License 2.0 5 votes vote down vote up
public static TitanVertexStep<Vertex> getReverseTitanVertexStep(final MessageScope.Local<?> scope,
                                                                   final TitanTransaction graph) {
    FulgoraElementTraversal<Vertex,Edge> result = getReverseTraversal(scope,graph,null);
    result.asAdmin().applyStrategies();
    verifyIncidentTraversal(result);
    return (TitanVertexStep)result.getStartStep();
}
 
Example #14
Source File: TinkerMessenger.java    From tinkergraph-gremlin with Apache License 2.0 5 votes vote down vote up
@Override
    public Iterator<M> receiveMessages() {
        final MultiIterator<M> multiIterator = new MultiIterator<>();
        for (final MessageScope messageScope : this.messageBoard.receiveMessages.keySet()) {
//        for (final MessageScope messageScope : this.messageBoard.previousMessageScopes) {
            if (messageScope instanceof MessageScope.Local) {
                final MessageScope.Local<M> localMessageScope = (MessageScope.Local<M>) messageScope;
                final Traversal.Admin<Vertex, Edge> incidentTraversal = TinkerMessenger.setVertexStart(localMessageScope.getIncidentTraversal().get().asAdmin(), this.vertex);
                final Direction direction = TinkerMessenger.getDirection(incidentTraversal);
                final Edge[] edge = new Edge[1]; // simulates storage side-effects available in Gremlin, but not Java8 streams
                multiIterator.addIterator(StreamSupport.stream(Spliterators.spliteratorUnknownSize(VertexProgramHelper.reverse(incidentTraversal.asAdmin()), Spliterator.IMMUTABLE | Spliterator.SIZED), false)
                        .map((Edge e) -> {
                            edge[0] = e;
                            Vertex vv;
                            if (direction.equals(Direction.IN) || direction.equals(Direction.OUT)) {
                                vv = e.vertices(direction).next();
                            } else {
                                vv = e.outVertex() == this.vertex ? e.inVertex() : e.outVertex();
                            }
                            return this.messageBoard.receiveMessages.get(messageScope).get(vv);
                        })
                        .filter(q -> null != q)
                        .flatMap(Queue::stream)
                        .map(message -> localMessageScope.getEdgeFunction().apply(message, edge[0]))
                        .iterator());

            } else {
                multiIterator.addIterator(Stream.of(this.vertex)
                        .map(this.messageBoard.receiveMessages.get(messageScope)::get)
                        .filter(q -> null != q)
                        .flatMap(Queue::stream)
                        .iterator());
            }
        }
        return multiIterator;
    }
 
Example #15
Source File: VertexMemoryHandler.java    From titan1withtp3.1 with Apache License 2.0 5 votes vote down vote up
@Override
public Stream<M> receiveMessages(MessageScope messageScope) {
    if (messageScope instanceof MessageScope.Global) {
        return super.receiveMessages(messageScope);
    } else {
        final MessageScope.Local<M> localMessageScope = (MessageScope.Local) messageScope;
        M aggregateMsg = vertexMemory.getAggregateMessage(vertexId,localMessageScope);
        if (aggregateMsg==null) return Stream.empty();
        else return Stream.of(aggregateMsg);
    }
}
 
Example #16
Source File: VertexMemoryHandler.java    From titan1withtp3.1 with Apache License 2.0 5 votes vote down vote up
@Override
public Iterator<M> receiveMessages() {
    Stream<M> combinedStream = Stream.empty();
    for (MessageScope scope : vertexMemory.getPreviousScopes()) {
        combinedStream = Stream.concat(receiveMessages(scope),combinedStream);
    }
    return combinedStream.iterator();
}
 
Example #17
Source File: SparkMessengerTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Test
public void testSparkMessenger() throws Exception {
    // Define scopes
    final MessageScope.Local<String> orderSrcMessageScope = MessageScope.Local
            .of(__::inE, (message, edge) -> {
                if ("mocked_edge_label1".equals(edge.label())) {
                    return message;
                }
                return null;
            });

    // Define star graph
    final StarGraph starGraph = StarGraph.open();
    Object[] vertex0Array = new Object[]{T.id, 0, T.label, "mocked_vertex_label1"};
    Object[] vertex1Array = new Object[]{T.id, 1, T.label, "mocked_vertex_label2"};
    Object[] vertex2Array = new Object[]{T.id, 2, T.label, "mocked_vertex_label2"};
    Vertex vertex0 = starGraph.addVertex(vertex0Array);
    Vertex vertex1 = starGraph.addVertex(vertex1Array);
    Vertex vertex2 = starGraph.addVertex(vertex2Array);
    vertex1.addEdge("mocked_edge_label1", vertex0);
    vertex2.addEdge("mocked_edge_label2", vertex0);

    // Create Spark Messenger
    final SparkMessenger<String> messenger = new SparkMessenger<>();
    final List<String> incomingMessages = Arrays.asList("a", "b", "c");
    messenger.setVertexAndIncomingMessages(vertex0, incomingMessages);

    messenger.sendMessage(orderSrcMessageScope, "a");
    List<Tuple2<Object, String>> outgoingMessages0 = messenger.getOutgoingMessages();

    Assert.assertEquals("a", outgoingMessages0.get(0)._2());
    Assert.assertNull(outgoingMessages0.get(1)._2());
}
 
Example #18
Source File: ShortestDistanceVertexProgram.java    From titan1withtp3.1 with Apache License 2.0 5 votes vote down vote up
@Override
public void loadState(final Graph graph, final Configuration configuration) {
    maxDepth = configuration.getInt(MAX_DEPTH);
    seed = configuration.getLong(SEED);
    weightProperty = configuration.getString(WEIGHT_PROPERTY, "distance");
    incidentMessageScope = MessageScope.Local.of(__::inE, (msg, edge) -> msg + edge.<Integer>value(weightProperty));
    log.debug("Loaded maxDepth={}", maxDepth);
}
 
Example #19
Source File: TinkerMessenger.java    From tinkergraph-gremlin with Apache License 2.0 5 votes vote down vote up
@Override
    public void sendMessage(final MessageScope messageScope, final M message) {
//        this.messageBoard.currentMessageScopes.add(messageScope);
        if (messageScope instanceof MessageScope.Local) {
            addMessage(this.vertex, message, messageScope);
        } else {
            ((MessageScope.Global) messageScope).vertices().forEach(v -> addMessage(v, message, messageScope));
        }
    }
 
Example #20
Source File: TinkerMessenger.java    From tinkergraph-gremlin with Apache License 2.0 5 votes vote down vote up
private void addMessage(final Vertex vertex, final M message, MessageScope messageScope) {
    this.messageBoard.sendMessages.compute(messageScope, (ms, messages) -> {
        if(null==messages) messages = new ConcurrentHashMap<>();
        return messages;
    });
    this.messageBoard.sendMessages.get(messageScope).compute(vertex, (v, queue) -> {
        if (null == queue) queue = new ConcurrentLinkedQueue<>();
        queue.add(null != this.combiner && !queue.isEmpty() ? this.combiner.combine(queue.remove(), message) : message);
        return queue;
    });
}
 
Example #21
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 #22
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 #23
Source File: VertexProgramScanJob.java    From titan1withtp3.1 with Apache License 2.0 5 votes vote down vote up
@Override
public void process(TitanVertex vertex, ScanMetrics metrics) {
    PreloadedVertex v = (PreloadedVertex)vertex;
    long vertexId = v.longId();
    VertexMemoryHandler<M> vh = new VertexMemoryHandler(vertexMemory,v);
    v.setAccessCheck(PreloadedVertex.OPENSTAR_CHECK);
    if (idManager.isPartitionedVertex(vertexId)) {
        if (idManager.isCanonicalVertexId(vertexId)) {
            EntryList results = v.getFromCache(SYSTEM_PROPS_QUERY);
            if (results == null) results = EntryList.EMPTY_LIST;
            vertexMemory.setLoadedProperties(vertexId,results);
        }
        for (MessageScope scope : vertexMemory.getPreviousScopes()) {
            if (scope instanceof MessageScope.Local) {
                M combinedMsg = null;
                for (Iterator<M> msgIter = vh.receiveMessages(scope).iterator(); msgIter.hasNext(); ) {
                    M msg = msgIter.next();
                    if (combinedMsg==null) combinedMsg=msg;
                    else combinedMsg = combiner.combine(combinedMsg,msg);
                }
                if (combinedMsg!=null) vertexMemory.aggregateMessage(vertexId,combinedMsg,scope);
            }
        }
    } else {
        v.setPropertyMixing(vh);
        vertexProgram.execute(v, vh, memory);
    }
}
 
Example #24
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 #25
Source File: FulgoraVertexMemory.java    From titan1withtp3.1 with Apache License 2.0 4 votes vote down vote up
private static MessageScope normalizeScope(MessageScope scope) {
    if (scope instanceof MessageScope.Global) return GLOBAL_SCOPE;
    else return scope;
}
 
Example #26
Source File: FulgoraVertexMemory.java    From titan1withtp3.1 with Apache License 2.0 4 votes vote down vote up
private static Iterable<MessageScope> normalizeScopes(Iterable<MessageScope> scopes) {
    return Iterables.transform(scopes, s -> normalizeScope(s));
}
 
Example #27
Source File: ShortestPathVertexProgram.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 new HashSet<>(Arrays.asList(inEdge, outEdge));
}
 
Example #28
Source File: GraknVertexProgram.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 messageScopeSetInAndOut;
}
 
Example #29
Source File: FulgoraVertexMemory.java    From titan1withtp3.1 with Apache License 2.0 4 votes vote down vote up
public void aggregateMessage(long vertexId, M message, MessageScope scope) {
    getPartitioned(vertexId).addMessage(message,normalizeScope(scope),previousScopes,combiner);
}
 
Example #30
Source File: ProgramTest.java    From tinkerpop with Apache License 2.0 4 votes vote down vote up
@Override
public Set<MessageScope> getMessageScopes(Memory memory) {
    return Collections.emptySet();
}