1
votes

Please help me with query on Gremlin lang.

I have a graph with 2 types of vertices: Places and Transitions. I need to find transition vertex by this condition: ALL input vertices (Places) has some flag. If I use "has", this works like ANY input vertex has flag. Not ALL.

Let me show you:

g.V().hasLabel('Transition').as('trans').in().has('Place', 'Token', 1).select('trans')

For example we have one Transition with two input vertices (Places), ONLY ONE of this vertices has flag. This query return Transition - but it's wrong, must be empty result (until we set flag for second input vertex).

In other words, I need to replace "has" to "all". How I can build such query?

Thanks.

1
Can you post a Traversal that also helps create a sample of your graph? This would make it easier for readers to fully understand your data structure and what you're trying to achieve.jbmusso

1 Answers

2
votes

You could do something like this:

gremlin> graph = TinkerGraph.open()
==>tinkergraph[vertices:0 edges:0]
gremlin> g = graph.traversal()
==>graphtraversalsource[tinkergraph[vertices:0 edges:0], standard]
gremlin> g.addV('transition').property('name','transition1').as('t1').
......1>   addV('transition').property('name','transition2').as('t2').
......2>   addV('transition').property('name','transition3').as('t3').
......3>   addV('place').property('token',1).as('p1').
......4>   addV('place').property('token',1).as('p2').
......5>   addV('place').property('token',0).as('p3').
......6>   addV('place').property('token',0).as('p4').
......7>   addE('at').from('p1').to('t1').
......8>   addE('at').from('p1').to('t2').
......9>   addE('at').from('p2').to('t1').
.....10>   addE('at').from('p3').to('t2').
.....11>   addE('at').from('p3').to('t3').
.....12>   addE('at').from('p4').to('t3').iterate()
gremlin> g.V().hasLabel('transition').
......1>   filter(__.in().values('token').
......2>             dedup().fold().
......3>             is(eq([1])))
==>v[0]

Note that in this case, v[0] is the vertex that is labelled "t1" in the sample data I generated. It has two edges from "p1" and "p2" which both have a "token" equivalent to "1" whereas the "t2" vertex has two edges from "p1" and "p3" where "p3" has a token of "0" which forces "t2" to be filtered out. There is also a "t3" which has two places with "token" properties equal to "0".

The basic pattern here is to use a filter() step that gets the unique list of "token" values, then ensures that they are equal to a list with the value "1" in it.