VertexStep vstep = (VertexStep)currentStep; if (vstep.returnsEdge() && (direction==Direction.BOTH || direction.equals(vstep.getDirection().opposite()))) {
VertexStep vertexStep = (VertexStep) currentStep; if (vertexStep.returnsEdge() && (direction == Direction.BOTH || direction.equals(vertexStep.getDirection().opposite()))) {
public static org.neo4j.tinkerpop.api.Neo4jDirection mapDirection(final Direction direction) { if (direction.equals(Direction.OUT)) return org.neo4j.tinkerpop.api.Neo4jDirection.OUTGOING; else if (direction.equals(Direction.IN)) return org.neo4j.tinkerpop.api.Neo4jDirection.INCOMING; else return org.neo4j.tinkerpop.api.Neo4jDirection.BOTH; }
public void dropEdges(final Direction direction) { if ((direction.equals(Direction.OUT) || direction.equals(Direction.BOTH)) && null != this.outEdges) { this.outEdges.clear(); this.outEdges = null; } if ((direction.equals(Direction.IN) || direction.equals(Direction.BOTH)) && null != this.inEdges) { this.inEdges.clear(); this.inEdges = null; } }
public void dropEdges(final Direction direction, final String edgeLabel) { if (null != this.outEdges && (direction.equals(Direction.OUT) || direction.equals(Direction.BOTH))) { this.outEdges.remove(edgeLabel); if (this.outEdges.isEmpty()) this.outEdges = null; } if (null != this.inEdges && (direction.equals(Direction.IN) || direction.equals(Direction.BOTH))) { this.inEdges.remove(edgeLabel); if (this.inEdges.isEmpty()) this.inEdges = null; } }
public static Iterator<TinkerEdge> getEdges(final TinkerVertex vertex, final Direction direction, final String... edgeLabels) { final List<Edge> edges = new ArrayList<>(); if (direction.equals(Direction.OUT) || direction.equals(Direction.BOTH)) { if (vertex.outEdges != null) { if (edgeLabels.length == 0) vertex.outEdges.values().forEach(edges::addAll); else if (edgeLabels.length == 1) edges.addAll(vertex.outEdges.getOrDefault(edgeLabels[0], Collections.emptySet())); else Stream.of(edgeLabels).map(vertex.outEdges::get).filter(Objects::nonNull).forEach(edges::addAll); } } if (direction.equals(Direction.IN) || direction.equals(Direction.BOTH)) { if (vertex.inEdges != null) { if (edgeLabels.length == 0) vertex.inEdges.values().forEach(edges::addAll); else if (edgeLabels.length == 1) edges.addAll(vertex.inEdges.getOrDefault(edgeLabels[0], Collections.emptySet())); else Stream.of(edgeLabels).map(vertex.inEdges::get).filter(Objects::nonNull).forEach(edges::addAll); } } return (Iterator) edges.iterator(); }
public static Iterator<TinkerVertex> getVertices(final TinkerVertex vertex, final Direction direction, final String... edgeLabels) { final List<Vertex> vertices = new ArrayList<>(); if (direction.equals(Direction.OUT) || direction.equals(Direction.BOTH)) { if (vertex.outEdges != null) { if (edgeLabels.length == 0) vertex.outEdges.values().forEach(set -> set.forEach(edge -> vertices.add(((TinkerEdge) edge).inVertex))); else if (edgeLabels.length == 1) vertex.outEdges.getOrDefault(edgeLabels[0], Collections.emptySet()).forEach(edge -> vertices.add(((TinkerEdge) edge).inVertex)); else Stream.of(edgeLabels).map(vertex.outEdges::get).filter(Objects::nonNull).flatMap(Set::stream).forEach(edge -> vertices.add(((TinkerEdge) edge).inVertex)); } } if (direction.equals(Direction.IN) || direction.equals(Direction.BOTH)) { if (vertex.inEdges != null) { if (edgeLabels.length == 0) vertex.inEdges.values().forEach(set -> set.forEach(edge -> vertices.add(((TinkerEdge) edge).outVertex))); else if (edgeLabels.length == 1) vertex.inEdges.getOrDefault(edgeLabels[0], Collections.emptySet()).forEach(edge -> vertices.add(((TinkerEdge) edge).outVertex)); else Stream.of(edgeLabels).map(vertex.inEdges::get).filter(Objects::nonNull).flatMap(Set::stream).forEach(edge -> vertices.add(((TinkerEdge) edge).outVertex)); } } return (Iterator) vertices.iterator(); }
@Override public Iterator<Edge> edges(final Direction direction, final String... edgeLabels) { if (direction.equals(Direction.OUT)) { return null == this.outEdges ? Collections.emptyIterator() : edgeLabels.length == 0 ? IteratorUtils.flatMap(this.outEdges.values().iterator(), List::iterator) : this.outEdges.entrySet().stream() .filter(entry -> ElementHelper.keyExists(entry.getKey(), edgeLabels)) .map(Map.Entry::getValue) .flatMap(List::stream) .iterator(); } else if (direction.equals(Direction.IN)) { return null == this.inEdges ? Collections.emptyIterator() : edgeLabels.length == 0 ? IteratorUtils.flatMap(this.inEdges.values().iterator(), List::iterator) : this.inEdges.entrySet().stream() .filter(entry -> ElementHelper.keyExists(entry.getKey(), edgeLabels)) .map(Map.Entry::getValue) .flatMap(List::stream) .iterator(); } else return IteratorUtils.concat(this.edges(Direction.IN, edgeLabels), this.edges(Direction.OUT, edgeLabels)); }
/** * Checks whether a given step is optimizable or not. * * @param step1 an edge-emitting step * @param step2 a vertex-emitting step * @return <code>true</code> if step1 is not labeled and emits edges and step2 emits vertices, * otherwise <code>false</code> */ private static boolean isOptimizable(final Step step1, final Step step2) { if (step1 instanceof VertexStep && ((VertexStep) step1).returnsEdge() && step1.getLabels().isEmpty()) { final Direction step1Dir = ((VertexStep) step1).getDirection(); if (step1Dir.equals(Direction.BOTH)) { return step2 instanceof EdgeOtherVertexStep; } return step2 instanceof EdgeOtherVertexStep || (step2 instanceof EdgeVertexStep && ((EdgeVertexStep) step2).getDirection().equals(step1Dir.opposite())); } return false; }
@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))); } }
@Override public Iterator<Vertex> vertices(final Direction direction, final String... edgeLabels) { if (direction.equals(Direction.OUT)) return IteratorUtils.map(this.edges(direction, edgeLabels), Edge::inVertex); else if (direction.equals(Direction.IN)) return IteratorUtils.map(this.edges(direction, edgeLabels), Edge::outVertex); else return IteratorUtils.concat(this.vertices(Direction.IN, edgeLabels), this.vertices(Direction.OUT, edgeLabels)); }
@Override public Iterator<Vertex> vertices(final Direction direction) { if (direction.equals(Direction.OUT)) return IteratorUtils.of(this.outVertex()); else if (direction.equals(Direction.IN)) return IteratorUtils.of(this.inVertex()); else return IteratorUtils.of(this.outVertex(), this.inVertex()); }
private <O extends OutputShim> void writeEdges(final KryoShim<?, O> kryo, final O output, final StarGraph starGraph, final Direction direction) { // only write edges if there are some AND if the user requested them to be serialized AND if they match // the direction being serialized by the format final Map<String, List<Edge>> starEdges = direction.equals(Direction.OUT) ? starGraph.starVertex.outEdges : starGraph.starVertex.inEdges; final boolean writeEdges = null != starEdges && edgeDirectionToSerialize != null && (edgeDirectionToSerialize == direction || edgeDirectionToSerialize == Direction.BOTH); kryo.writeObject(output, writeEdges); if (writeEdges) { kryo.writeObject(output, starEdges.size()); for (final Map.Entry<String, List<Edge>> edges : starEdges.entrySet()) { kryo.writeObject(output, edges.getKey()); kryo.writeObject(output, edges.getValue().size()); for (final Edge edge : edges.getValue()) { kryo.writeClassAndObject(output, edge.id()); kryo.writeClassAndObject(output, direction.equals(Direction.OUT) ? edge.inVertex().id() : edge.outVertex().id()); } } } }
public static final boolean endsWithElement(Step<?, ?> currentStep) { while (!(currentStep instanceof EmptyStep)) { if (currentStep instanceof VertexStep) // only inE, in, and out send messages return (((VertexStep) currentStep).returnsVertex() || !((VertexStep) currentStep).getDirection().equals(Direction.OUT)); else if (currentStep instanceof EdgeVertexStep) // TODO: add GraphStep but only if its mid-traversal V()/E() return true; else if (currentStep instanceof TraversalFlatMapStep || currentStep instanceof TraversalMapStep || currentStep instanceof LocalStep) return endsWithElement(((TraversalParent) currentStep).getLocalChildren().get(0).getEndStep()); else if (!(currentStep instanceof FilterStep || currentStep instanceof SideEffectStep || currentStep instanceof IdentityStep || currentStep instanceof Barrier)) return false; currentStep = currentStep.getPreviousStep(); } return false; }
@Override public Iterator<Vertex> vertices(final Direction direction, final String... edgeLabels) { return TinkerHelper.inComputerMode(this.graph) ? direction.equals(Direction.BOTH) ? IteratorUtils.concat( IteratorUtils.map(this.edges(Direction.OUT, edgeLabels), Edge::inVertex), IteratorUtils.map(this.edges(Direction.IN, edgeLabels), Edge::outVertex)) : IteratorUtils.map(this.edges(direction, edgeLabels), edge -> edge.vertices(direction.opposite()).next()) : (Iterator) TinkerHelper.getVertices(this, direction, edgeLabels); }
private <I extends InputShim> void readEdges(final KryoShim<I, ?> kryo, final I input, final StarGraph starGraph, final Direction direction) { if (kryo.readObject(input, Boolean.class)) { final int numberOfUniqueLabels = kryo.readObject(input, Integer.class); for (int i = 0; i < numberOfUniqueLabels; i++) { final String edgeLabel = kryo.readObject(input, String.class); final int numberOfEdgesWithLabel = kryo.readObject(input, Integer.class); for (int j = 0; j < numberOfEdgesWithLabel; j++) { final Object edgeId = kryo.readClassAndObject(input); final Object adjacentVertexId = kryo.readClassAndObject(input); if (this.graphFilter.checkEdgeLegality(direction, edgeLabel).positive()) { if (direction.equals(Direction.OUT)) starGraph.starVertex.addOutEdge(edgeLabel, starGraph.addVertex(T.id, adjacentVertexId), T.id, edgeId); else starGraph.starVertex.addInEdge(edgeLabel, starGraph.addVertex(T.id, adjacentVertexId), T.id, edgeId); } else if (null != starGraph.edgeProperties) { starGraph.edgeProperties.remove(edgeId); } } } } } }