Reputation: 27727
I have a recursive data structure like so:
@Canonical
static class Person {
String name
Set<Person> knows
}
I have a Tinkerpop graph that represents this structure:
(Jon) -- knows --> (Billy) -- knows --> (Jane) -- ... -->
\- knows --> (Suzy) -- ... -->
What is the most efficient way to map an arbitrarily deep Tinkerpop graph to the data structure?
I can imagine the use of the PathStep
, but it seems like there should be a better way, and I am not grokking TP3 well enough to see it.
def 'build recursive person object'() {
when:
def g = TinkerGraph.open()
def jon = g.addVertex(T.label, 'person', 'name', 'jon')
def bill = g.addVertex(T.label, 'person', 'name', 'bill')
def jane = g.addVertex(T.label, 'person', 'name', 'jane')
jon.addEdge('knows', bill)
bill.addEdge('knows', jane)
Multimap<String, Person> relationships = HashMultimap.create()
g.V().has('name', 'jon')
.as('a')
.jump('b', { traverser -> !traverser.get().out('knows').hasNext() })
.outE('knows')
.inV()
.jump('a')
.as('b')
.path(
Function.<Vertex>identity(),
{ Edge e ->
relationships.put(e.outV().next().value('name') as String,
new Person(e.inV().next().value('name') as String))
} as Function
)
.next()
Person root = new Person('jon')
Stack<Person> people = new Stack<>()
Set<String> alreadySeen = new HashSet<>()
people.push(root)
alreadySeen.add('jon')
while(!people.isEmpty()) {
def person = people.pop()
person.knows = relationships.get(person.name)
for(Person related : person.knows) {
if(alreadySeen.add(related.name))
people.push(related)
}
}
then:
root.name == 'jon'
root.knows*.name == ['bill']
root.knows[0].knows*.name == ['jane']
}
Upvotes: 1
Views: 598
Reputation: 10904
I would leverage Gremlin's tree
structure and some recursive method calls:
import com.google.common.collect.Iterators;
import com.tinkerpop.gremlin.process.T;
import com.tinkerpop.gremlin.process.Traverser;
import com.tinkerpop.gremlin.process.graph.util.Tree;
import com.tinkerpop.gremlin.structure.Vertex;
import com.tinkerpop.gremlin.tinkergraph.structure.TinkerGraph;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import java.util.function.Function;
/**
* @author Daniel Kuppitz (daniel at thinkaurelius.com)
*/
public class App {
static class Person {
private String name;
private Set<Person> knows;
public static Person fromTree(final String name, final Tree node) {
final Person person = new Person(name);
final Tree self = (Tree) node.get(name);
final Set<String> children = (Set<String>) self.keySet();
children.forEach(n -> person.knows.add(Person.fromTree(n, self)));
return person;
}
public Person(final String name) {
this.name = name;
this.knows = new HashSet<>();
}
public String name() {
return name;
}
public Set<Person> knows() {
return knows;
}
}
public static void main(final String[] args) throws Exception {
final TinkerGraph g = TinkerGraph.open();
Vertex jon = g.addVertex(T.label, "person", "name", "jon");
Vertex bill = g.addVertex(T.label, "person", "name", "bill");
Vertex jane = g.addVertex(T.label, "person", "name", "jane");
Vertex suzy = g.addVertex(T.label, "person", "name", "suzy");
jon.addEdge("knows", bill);
bill.addEdge("knows", jane);
bill.addEdge("knows", suzy);
final Tree tree = (Tree) g.V().has("name", "jon").as("x").out("knows").until("x", (Traverser<Vertex> t) ->
t.get().outE("knows").hasNext(), t -> true).tree((Function<Vertex, String>) v -> v.value("name")).cap().next();
if (!tree.isEmpty()) {
final Person p1 = Person.fromTree("jon", tree);
assert p1.name().equals("jon");
assert p1.knows().size() == 1;
final Person p2 = p1.knows().iterator().next();
assert p2.name().equals("bill");
assert p2.knows().size() == 2;
final Iterator<Person> pitty = p2.knows().iterator();
Iterators.all(pitty, p -> (p.name().equals("jane") || p.name().equals("suzy")) && p1.knows().isEmpty());
}
}
}
This way it all boils down to a single statement: Person.fromTree("jon", tree)
Upvotes: 6