How to Find Vertices of Specific class with Tinkerpop Frames

让人想犯罪 __ 提交于 2020-01-05 23:08:03

问题


I have a FramedGraph object with Vertices of different classes, for instance Person and Location. I would like to retrieve all vertices of the "Person" class. Here is my current method.

    public List<Person> listPeople() {
      List<Person> people = new ArrayList<Person>();
      Iterator iterator =  g.getVertices().iterator();
      while (iterator.hasNext()) {
        Vertex v = (Vertex) iterator.next();
        Person p = (Person) g.getVertex(v.getId(), Person.class);
        people.add(p);
      }
      return people;
   }

This feels terrifically inefficient since I am looping over all vertices then dipping back in for one at a time. I looked into using the Gremlin syntax, but I don't see how to restrict by a Frames class. Is there a more efficient retrieval method? Thanks..


回答1:


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)

This question looks likes it's the same as this question (looks like you were first) - Tinkerpop Frames: Query vertices based on interface type.



来源:https://stackoverflow.com/questions/29626160/how-to-find-vertices-of-specific-class-with-tinkerpop-frames

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