Tinkerpop Frames: Query vertices based on interface type

半世苍凉 提交于 2019-12-11 13:05:35

问题


I'm using Tinkerpop Frames to create a set of vertices and edges. Adding a new vertex is simple but retreiving vertices based on type seems a bit difficult.

Assume I have a class A and B and I want to add a new one so:

framedGraph.addVertex(null, A.class);
framedGraph.addVertex(null, B.class);

That's straight forward. But what if I want to retrieve all vertices with type A?

Doing this failed, because it returned all vertices (both A and B).

framedGraph.query().vertices(A.class);

Is there any possible way to do this. I tried to check documentations and test cases with no luck. How can I retreive list of Vertices of type A only


回答1:


This question looks like it's a duplicate of - How to Find Vertices of Specific class with Tinkerpop Frames (also asked today).

As far as I understand, the Tinkerpop Frame framework acts as a wrapper class around a vertex. The vertex isn't actually stored as the interface class. As such, we need a way to identify the vertex as being of a particular type.

My solution, I added @TypeField and @TypeValue annotations to my Frame classes. Then I use these values to query my FramedGraph.

The documentation for these annotations can be found here: https://github.com/tinkerpop/frames/wiki/Typed-Graph

Example Code

@TypeField("type")
@TypeValue("person")
interface Person extends VertexFrame { /* ... */ }

Then define the FramedGraphFactory by adding TypedGraphModuleBuilder like this.

static final FramedGraphFactory FACTORY = new FramedGraphFactory(
    new TypedGraphModuleBuilder()
        .withClass(Person.class)
        //add any more classes that use the above annotations. 
        .build()
);

Then to retrieve vertices of type Person

Iterable<Person> people = framedGraph.getVertices('type', 'person', Person.class);

I'm not sure this is the most efficient/succinct solution (I'd like to see what @stephen mallette suggests). It's not currently available, but it'd be logical to be able to do something like:

// framedGraph.getVertices(Person.class)


来源:https://stackoverflow.com/questions/29636308/tinkerpop-frames-query-vertices-based-on-interface-type

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!