<artifactId>asm-all</artifactId> | <artifactId>asm-all</artifactId> | ||||
<version>3.2</version> | <version>3.2</version> | ||||
</dependency> | </dependency> | ||||
<dependency> | |||||
<groupId>com.tinkerpop.blueprints</groupId> | |||||
<artifactId>blueprints-core</artifactId> | |||||
<!-- | |||||
Do not upgrade because of compatibility with Java 5. | |||||
See https://github.com/tinkerpop/blueprints/pull/352 | |||||
--> | |||||
<version>2.0.0</version> | |||||
<exclusions> | |||||
<!-- we use our own GraphSON implementation based on json-simple --> | |||||
<exclusion> | |||||
<groupId>org.codehaus.jettison</groupId> | |||||
<artifactId>jettison</artifactId> | |||||
</exclusion> | |||||
<exclusion> | |||||
<groupId>org.codehaus.jackson</groupId> | |||||
<artifactId>jackson-jaxrs</artifactId> | |||||
</exclusion> | |||||
</exclusions> | |||||
</dependency> | |||||
<dependency> | |||||
<!-- | |||||
Do not upgrade because of compatibility with Java 5. | |||||
See https://github.com/tinkerpop/pipes/pull/74 | |||||
--> | |||||
<groupId>com.tinkerpop.gremlin</groupId> | |||||
<artifactId>gremlin-java</artifactId> | |||||
<version>2.0.0</version> | |||||
</dependency> | |||||
<dependency> | <dependency> | ||||
<groupId>org.codehaus.plexus</groupId> | <groupId>org.codehaus.plexus</groupId> | ||||
<artifactId>plexus-classworlds</artifactId> | <artifactId>plexus-classworlds</artifactId> |
<dependency> | <dependency> | ||||
<groupId>com.tinkerpop.blueprints</groupId> | <groupId>com.tinkerpop.blueprints</groupId> | ||||
<artifactId>blueprints-core</artifactId> | <artifactId>blueprints-core</artifactId> | ||||
<version>2.2.0-java5</version> | |||||
<scope>system</scope> | |||||
<systemPath>${project.basedir}/lib/blueprints-core-2.2.0-java5.jar</systemPath> | |||||
</dependency> | </dependency> | ||||
<dependency> | <dependency> | ||||
<groupId>com.tinkerpop.gremlin</groupId> | |||||
<artifactId>gremlin-java</artifactId> | |||||
<!-- Temporary dependency as long as blueprints-core is system scope --> | |||||
<groupId>colt</groupId> | |||||
<artifactId>colt</artifactId> | |||||
<version>1.2.0</version> | |||||
</dependency> | </dependency> | ||||
<!-- logging --> | <!-- logging --> | ||||
<build> | <build> | ||||
<plugins> | <plugins> | ||||
<plugin> | |||||
<groupId>org.sonatype.plugins</groupId> | |||||
<artifactId>jarjar-maven-plugin</artifactId> | |||||
<version>1.7</version> | |||||
<executions> | |||||
<execution> | |||||
<phase>package</phase> | |||||
<goals> | |||||
<goal>jarjar</goal> | |||||
</goals> | |||||
<configuration> | |||||
<includes> | |||||
<include>com.tinkerpop.blueprints:blueprints-core</include> | |||||
</includes> | |||||
<rules> | |||||
<keep> | |||||
<pattern>*.**</pattern> | |||||
</keep> | |||||
</rules> | |||||
</configuration> | |||||
</execution> | |||||
</executions> | |||||
</plugin> | |||||
<plugin> | <plugin> | ||||
<groupId>org.apache.maven.plugins</groupId> | <groupId>org.apache.maven.plugins</groupId> | ||||
<artifactId>maven-jar-plugin</artifactId> | <artifactId>maven-jar-plugin</artifactId> |
package org.sonar.core.graph; | package org.sonar.core.graph; | ||||
import com.google.common.base.Joiner; | import com.google.common.base.Joiner; | ||||
import com.google.common.collect.Lists; | |||||
import com.tinkerpop.blueprints.Direction; | import com.tinkerpop.blueprints.Direction; | ||||
import com.tinkerpop.blueprints.Edge; | |||||
import com.tinkerpop.blueprints.Element; | import com.tinkerpop.blueprints.Element; | ||||
import com.tinkerpop.blueprints.Vertex; | import com.tinkerpop.blueprints.Vertex; | ||||
import com.tinkerpop.blueprints.impls.tg.TinkerGraph; | |||||
import com.tinkerpop.blueprints.util.ElementHelper; | |||||
import com.tinkerpop.gremlin.java.GremlinPipeline; | |||||
import org.hibernate.engine.JoinSequence; | |||||
import javax.annotation.CheckForNull; | import javax.annotation.CheckForNull; | ||||
import javax.annotation.Nullable; | import javax.annotation.Nullable; | ||||
import java.util.Iterator; | import java.util.Iterator; | ||||
import java.util.List; | |||||
public class GraphUtil { | public class GraphUtil { | ||||
} | } | ||||
} | } | ||||
public static void subGraph(GremlinPipeline path, TinkerGraph toGraph) { | |||||
List<Edge> edges = Lists.newArrayList(); | |||||
if (path.hasNext()) { | |||||
for (Object element : (Iterable) path.next()) { | |||||
if (element instanceof Vertex) { | |||||
Vertex v = (Vertex) element; | |||||
Vertex toVertex = toGraph.addVertex(v.getId()); | |||||
ElementHelper.copyProperties(v, toVertex); | |||||
} else if (element instanceof Edge) { | |||||
edges.add((Edge) element); | |||||
} | |||||
} | |||||
for (Edge edge : edges) { | |||||
Vertex from = edge.getVertex(Direction.IN); | |||||
Vertex to = edge.getVertex(Direction.OUT); | |||||
Edge copyEdge = toGraph.addEdge(edge.getId(), toGraph.getVertex(from.getId()), toGraph.getVertex(to.getId()), edge.getLabel()); | |||||
ElementHelper.copyProperties(edge, copyEdge); | |||||
} | |||||
} | |||||
} | |||||
} | } |
import com.tinkerpop.blueprints.Direction; | import com.tinkerpop.blueprints.Direction; | ||||
import com.tinkerpop.blueprints.Vertex; | import com.tinkerpop.blueprints.Vertex; | ||||
import com.tinkerpop.blueprints.impls.tg.TinkerGraph; | import com.tinkerpop.blueprints.impls.tg.TinkerGraph; | ||||
import com.tinkerpop.gremlin.java.GremlinPipeline; | |||||
import org.junit.Rule; | import org.junit.Rule; | ||||
import org.junit.Test; | import org.junit.Test; | ||||
import org.junit.rules.ExpectedException; | import org.junit.rules.ExpectedException; | ||||
GraphUtil.singleAdjacent(a, Direction.OUT, "likes", "hates"); | GraphUtil.singleAdjacent(a, Direction.OUT, "likes", "hates"); | ||||
} | } | ||||
@Test | |||||
public void subGraph() { | |||||
TinkerGraph graph = new TinkerGraph(); | |||||
Vertex a = graph.addVertex("1"); | |||||
Vertex b = graph.addVertex("2"); | |||||
Vertex c = graph.addVertex("3"); | |||||
graph.addEdge("4", a, b, "likes"); | |||||
graph.addEdge("5", b, c, "has"); | |||||
TinkerGraph subGraph = new TinkerGraph(); | |||||
GremlinPipeline pipeline = new GremlinPipeline(a).outE("likes").inV().path(); | |||||
GraphUtil.subGraph(pipeline, subGraph); | |||||
assertThat(subGraph.getVertices()).hasSize(2); | |||||
assertThat(subGraph.getEdges()).hasSize(1); | |||||
} | |||||
} | } |