private static FulgoraElementTraversal<Vertex,Edge> getReverseTraversal(final MessageScope.Local<?> scope, final JanusGraphTransaction 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; }
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 BiFunction<M,Edge,M> edgeFct = localMessageScope.getEdgeFunction(); final List<Edge> edges; try (final Traversal<Vertex, Edge> reverseIncident = FulgoraUtil.getReverseElementTraversal(localMessageScope,vertex,vertex.tx())) { edges = IteratorUtils.list(reverseIncident); } catch (Exception e) { throw new JanusGraphException("Unable to close traversal", e); } return edges.stream() .map(e -> { M msg = vertexMemory.getMessage(vertexMemory.getCanonicalId(((JanusGraphEdge) e).otherVertex(vertex).longId()), localMessageScope); return msg == null ? null : edgeFct.apply(msg, e); }) .filter(Objects::nonNull); } }
@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); }
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 .filter(q -> null != q) .flatMap(Queue::stream) .map(message -> localMessageScope.getEdgeFunction().apply(message, edge[0])) .iterator());
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 .filter(q -> null != q) .flatMap(Queue::stream) .map(message -> localMessageScope.getEdgeFunction().apply(message, edge[0])) .iterator());
@Override public Iterator<M> receiveMessages() { MultiIterator<M> multiIterator = new MultiIterator<>(); for (MessageScope messageScope : messageBoard.getPreviousMessageScopes()) { if (messageScope instanceof MessageScope.Local) { @SuppressWarnings("unchecked") MessageScope.Local<M> localMessageScope = (MessageScope.Local<M>) messageScope; Traversal.Admin<Vertex, Edge> incidentTraversal = DuctileMessenger .setVertexStart(localMessageScope.getIncidentTraversal().get().asAdmin(), vertex); Direction direction = DuctileMessenger.getDirection(incidentTraversal); Edge[] edge = new Edge[1]; multiIterator.addIterator(// StreamSupport // .stream(Spliterators.spliteratorUnknownSize( VertexProgramHelper.reverse(incidentTraversal.asAdmin()), Spliterator.IMMUTABLE | Spliterator.SIZED), false)// .map(e -> messageBoard.getReceiveMessage((edge[0] = e).vertices(direction).next()))// .filter(q -> null != q)// .flatMap(Queue::stream)// .map(message -> localMessageScope.getEdgeFunction().apply(message, edge[0])) .iterator()); } else { multiIterator.addIterator(Stream.of(vertex).map(messageBoard.getReceiveMessages()::get) .filter(q -> null != q).flatMap(Queue::stream).iterator()); } } return multiIterator; }
@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))); }
@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()); } }
@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))); } }
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); } }
public static <M> Local<M> of(final Supplier<? extends Traversal<Vertex, Edge>> incidentTraversal, final BiFunction<M, Edge, M> edgeFunction) { return new Local<>(incidentTraversal, edgeFunction); }
private BulkLoaderVertexProgram() { messageScope = MessageScope.Local.of(__::inE); elementComputeKeys = new HashSet<>(); }
public Traversal<Vertex, Edge> get() { return VertexProgramHelper.reverse(this.localMessageScope.getIncidentTraversal().get().asAdmin()); } }
@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); }
public static <M> Local<M> of(final Supplier<? extends Traversal<Vertex, Edge>> incidentTraversal) { return new Local<>(incidentTraversal); }
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; }
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 .filter(q -> null != q) .flatMap(Queue::stream) .map(message -> localMessageScope.getEdgeFunction().apply(message, edge[0])) .iterator());
@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))); }
@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()); } }
@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))); } }