[Tinkerpop 3] Best way to do a traversal

112 views
Skip to first unread message

Dave

unread,
Nov 15, 2014, 7:47:57 PM11/15/14
to gremli...@googlegroups.com
Hi, 

I have a simple test graph that has vertices related to other vertices by "next" edges - each edge contains a "timestamp" property. Given the new traversal steps in Tinkerpop 3 I was wondering what the best way to write the following would be:-

1) Given a set of property keys return the matched properties on the vertex
2) Follow the outgoing "next" edges in order of "timestamp" and repeat step 1 for the unmatched keys until:-
    a) all properties have been found for all keys
    b) there are no more outgoing "next" edges i.e. vertex is a leaf.


The timestamp is necessary to guarantee repeatable results i.e. since there may be multiple outgoing "next" edges from a vertex and more than one related vertex could contain a property with the same key e.g. "name".
  . 

Thanks,
Dave

Daniel Kuppitz

unread,
Nov 16, 2014, 10:01:44 AM11/16/14
to gremli...@googlegroups.com
Hi Dave,

1) Given a set of property keys return the matched properties on the vertex

what is the vertex? Does that mean your traversal starts at exactly one given vertex?

It would be nice if you could provide a small sample graph, the aforementioned given set of property keys and the result you're expecting.

Cheers,
Daniel


--
You received this message because you are subscribed to the Google Groups "Gremlin-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to gremlin-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/gremlin-users/6714a038-dfc5-495e-8dcc-a0b8c2c8b36c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Dave

unread,
Nov 16, 2014, 2:16:39 PM11/16/14
to gremli...@googlegroups.com
Hi Daniel,

Probably better explained with an example that closely represents what I'm trying to do. Think class inheritance and properties on a class. Say I have something like this:-

class Child extends Parent {
    name = "Child"
    prop1 = "some child prop 1"
}

class Parent extends Grandparent {
    name = "Parent"
    prop1 = "some parent prop 1"
    prop2 = "some parent prop 2"
}

class Grandparent {
    name = "Grandparent"
    prop2 = "some grandparent prop 2"
    prop3 = "some grandparent prop 3"
}

Each class is a vertex. They are related by "Extends" relationships. I want to do two types of traversals, one being an extension of the other:-

1) Give me all the properties for the "Child" class. Results will be:-
        name = "Child"                                (from Child)
        prop1 = "some child prop1"               (from Child)
        prop2 = "some parent prop2"             (from Parent)
        prop3 = "some grandparent prop3"     (from Grandparent)

2) Give me a subset of properties, for example, the "name" and "prop3" properties for "Child". Results will be:-
        name = "Child"                                (from Child)
        prop3 = "some grandparent prop3"     (from Grandparent)


And for completeness:-   Give me the "name" and "prop3" properties for "Parent" (i.e. change starting point). Results will be:-
        name = "Parent"                                (from Parent)
        prop3 = "some grandparent prop3"       (from Grandparent)

Hopefully this does a better job of explaining what I'm trying to do. I appreciate your help.

Thanks
Dave
P.S. Another way to think of it is that I need to buy 10 things from the store (traversal 2 above). I go to the first store and they only have 5. I go to the second store and find 3 more. I go to the 3rd store and find one more. There's no more stores in my little town so I return home without the last item but am happy I found 9 out of 10. Essentially, it's a pattern that I'm trying to understand the best way to traverse with Tinkerpop 3. Thx.

Daniel Kuppitz

unread,
Nov 16, 2014, 4:53:09 PM11/16/14
to gremli...@googlegroups.com
Hi Dave,

here's one way to do it...

Init:

g = TinkerGraph.open()

v1 = g.addVertex(id, "child", "name", "Child", "prop1", "some child prop 1")
v2 = g.addVertex(id, "parent", "name", "Parent", "prop1", "some parent prop 1", "prop2", "some parent prop 2")
v3 = g.addVertex(id, "grandparent", "name", "Grandparent", "prop1", "some grandparent prop 1", "prop2", "some grandparent prop 2", "prop3", "some grandparent prop 3")

v1.addEdge("extends", v2)

v2.addEdge("extends", v3)

determineProps = { v, p = null ->
  v.with('m', { [:] }).as('x').sideEffect {
    def m = it.get('m')
    def props = it.get().propertyMap().next().collectEntries { [it.getKey(), it.getValue().head().value()] }
    props.keySet().removeAll(m.keySet())
    m.putAll(props.grep { p == null || it.getKey() in p })
  }.jump('y', { !it.get().out('extends').hasNext()  }).out('extends').jump('x').as('y').cap('m').next()
}

Test:

gremlin> determineProps(g.v("child"))
==>prop1=some child prop 1
==>name=Child
==>prop2=some parent prop 2
==>prop3=some grandparent prop 3

gremlin> determineProps(g.v("child"), ["name", "prop3"])
==>name=Child
==>prop3=some grandparent prop 3

gremlin> determineProps(g.v("parent"), ["name", "prop3"])
==>name=Parent
==>prop3=some grandparent prop 3

Since TP3 is still very new, I can't guarantee that this is the best way, but a) it works and b) it was easier to write than to read :)

Cheers,
Daniel


Dave

unread,
Nov 16, 2014, 5:54:11 PM11/16/14
to gremli...@googlegroups.com
Hi Daniel,

A big thank you for this. It's _exactly_ what I was looking for!   I was certainly heading in a different direction and didn't expect the solution to look like this. Greatly appreciated!

Regards,
Dave

Dave

unread,
Nov 16, 2014, 6:38:16 PM11/16/14
to gremli...@googlegroups.com
HI Daniel,

I spoke a little too soon. Functionally the solution is perfect but I'm now struggling to convert it to Java (I'm still learning Java 8). Any help would be appreciated. Also, as a side note, more Java examples in the docs would be really helpful, especially explaining the Gremlin steps. 

Thanks,
Dave

Daniel Kuppitz

unread,
Nov 16, 2014, 9:49:07 PM11/16/14
to gremli...@googlegroups.com

Dave

unread,
Nov 19, 2014, 9:56:25 AM11/19/14
to gremli...@googlegroups.com
Hi Daniel,

I learned a lot from this example. It was extremely helpful, especially seeing how to use side effects beyond something basic. Again, a big thank you.

Regards,
Dave
Reply all
Reply to author
Forward
0 new messages