How to match() on an accumulated value?

21 views
Skip to first unread message

veggen

unread,
Jul 20, 2021, 7:46:46 PM7/20/21
to Gremlin-users

Given this ultra-simple graph, with unknown number of vertices between A and Z, and where each edge has a distance (dist) property, I have to answer the following questions:

  1. Does all of the following hold:
  • There is a vertex called A
  • A is eventually (as opposed to directly) followed by a vertex named B at a maximum distance of 500
  • B is eventually followed by C at a maximum distance of 1000?
  1. What is the cumulative distance A->B + B->C?

If I disregard the distance constraints, I can find what I need by doing the following:

g.V().match(
    as("a").has("name", "A"), //A exists
    as("a").out()
        .until(has("name", "B"))
        .repeat(out())
        .as("b"), //A is eventually followed by B
    as("b").out()
         .until(has("name", "C"))
         .repeat(out())
         .as("c")) //B is eventually followed by C
     .select("a", "b", "c").by("name")

From here, I can simply call hasNext() to check if all the conditions are satisfied, and next() to get the actual matching vertices. So far so good. But how do I add distance constraints now?

Note: One reason I'm using match() is to have a stateless query, decoupled from any specific graph, as I have thousands of graphs and run the same query on each. Another is that parts of the query are generated dynamically, and the declarative syntax lends itself much better to this use-case. Yet another is that I absolutely do not understand how to select the matched vertices using the imperative traversal 🤷‍♂️

Disclaimer:  I also posted this same question on StackOverflow. I will make sure to link/cross post the answer not to waste anyone's effort.

graph.png

HadoopMarc

unread,
Jul 21, 2021, 5:07:20 AM7/21/21
to Gremlin-users
Good that you added the link to SO; in the mean time Daniel Kuppitz provided an anwer there.

Op woensdag 21 juli 2021 om 01:46:46 UTC+2 schreef kaqqao:
Reply all
Reply to author
Forward
0 new messages