Skip to content

Instantly share code, notes, and snippets.

@spmallette
Created April 12, 2024 15:42
Show Gist options
  • Save spmallette/a70c2648672e7b34de444812ec1ffc4c to your computer and use it in GitHub Desktop.
Save spmallette/a70c2648672e7b34de444812ec1ffc4c to your computer and use it in GitHub Desktop.
package org.apache.tinkerpop.gremlin.util;
import org.apache.tinkerpop.gremlin.driver.remote.DriverRemoteConnection;
import org.apache.tinkerpop.gremlin.process.traversal.Traversal;
import org.apache.tinkerpop.gremlin.process.traversal.TraversalStrategy;
import org.apache.tinkerpop.gremlin.process.traversal.Traverser;
import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
import org.apache.tinkerpop.gremlin.process.traversal.step.Configuring;
import org.apache.tinkerpop.gremlin.process.traversal.step.map.FlatMapStep;
import org.apache.tinkerpop.gremlin.process.traversal.step.map.VertexStep;
import org.apache.tinkerpop.gremlin.process.traversal.step.util.Parameters;
import org.apache.tinkerpop.gremlin.process.traversal.util.TraversalHelper;
import org.apache.tinkerpop.gremlin.structure.Direction;
import org.apache.tinkerpop.gremlin.structure.Element;
import org.apache.tinkerpop.gremlin.structure.Vertex;
import org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerGraph;
import java.util.Iterator;
import static org.apache.tinkerpop.gremlin.process.traversal.AnonymousTraversalSource.traversal;
import static org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__.V;
import static org.apache.tinkerpop.gremlin.structure.T.id;
public class TinkerMultiGraph {
public static void main(final String[] args) throws Exception {
final TinkerGraph graph = TinkerGraph.open();
final GraphTraversalSource g = graph.traversal();
g.addV().property("reference", "A101").iterate();
g.addV().property("reference", "B1001").iterate();
final GraphTraversalSource gA = traversal().withRemote(DriverRemoteConnection.using("localhost", 8182, "g"));
gA.V().drop().iterate();
gA.addV().property(id, "101").iterate();
gA.addV().property(id,"102").iterate();
gA.addV().property(id,"103").iterate();
gA.addE("link").from(V("101")).to(V("102")).iterate();
gA.addE("link").from(V("101")).to(V("103")).iterate();
final TinkerGraph graphB = TinkerGraph.open();
final GraphTraversalSource gB = graphB.traversal();
gB.addV().property(id, "1001").iterate();
gB.addV().property(id,"1002").iterate();
gB.addV().property(id,"1003").iterate();
gB.addE("link").from(V("1001")).to(V("1002")).iterate();
gB.addE("link").from(V("1001")).to(V("1003")).iterate();
g.withStrategies(new MultiGraphStrategy(gA, gB)).V().out().forEachRemaining(System.out::println);
gA.close();
}
public static class MultiVertexStep<E extends Element> extends FlatMapStep<Vertex, E> implements AutoCloseable, Configuring {
private final GraphTraversalSource gA;
private final GraphTraversalSource gB;
private final Direction direction;
private final String[] edgeLabels;
private final Class returnClass;
public MultiVertexStep(final GraphTraversalSource gA, final GraphTraversalSource gB,
final Traversal.Admin traversal, final Class returnClass, final Direction direction, final String... edgeLabels) {
super(traversal);
this.direction = direction;
this.edgeLabels = edgeLabels;
this.returnClass = returnClass;
this.gA = gA;
this.gB = gB;
}
@Override
protected Iterator flatMap(Traverser.Admin traverser) {
final Vertex v = (Vertex) traverser.get();
final String ref = v.value("reference");
final GraphTraversalSource g = ref.toString().startsWith("A") ? gA : gB;
final String id = ref.substring(1);
if (direction == Direction.OUT) {
if (returnClass == Vertex.class)
return g.V(id).out(edgeLabels);
else
return g.V(id).outE(edgeLabels);
} else if (direction == Direction.IN) {
if (returnClass == Vertex.class)
return g.V(id).in(edgeLabels);
else
return g.V(id).inE(edgeLabels);
} else {
if (returnClass == Vertex.class)
return g.V(id).both(edgeLabels);
else
return g.V(id).bothE(edgeLabels);
}
}
@Override
public void close() throws Exception {
}
@Override
public void configure(Object... keyValues) {
}
@Override
public Parameters getParameters() {
return null;
}
}
public static class MultiGraphStrategy implements TraversalStrategy.DecorationStrategy {
private final GraphTraversalSource gA;
private final GraphTraversalSource gB;
public MultiGraphStrategy(final GraphTraversalSource gA, final GraphTraversalSource gB) {
this.gA = gA;
this.gB = gB;
}
@Override
public void apply(final Traversal.Admin<?, ?> traversal) {
for (final VertexStep originalVertexStep : TraversalHelper.getStepsOfClass(VertexStep.class, traversal)) {
final MultiVertexStep multiVertexStep = new MultiVertexStep(gA, gB, originalVertexStep.getTraversal(), originalVertexStep.getReturnClass(), originalVertexStep.getDirection(), originalVertexStep.getEdgeLabels());
TraversalHelper.replaceStep(originalVertexStep, multiVertexStep, traversal);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment