2

I have a list of vertex ids. I want to get all possible edges among them.

I understand methods like filter/where would be of help here, but since I'm using gremlin-python, their implementation must be different.

I have tried :

a = [0,2,4,6,8,10]

g.V(a).outE().inV().hasId(a).toList()
# This gives me []

g.V(a).bothE().filter(otherV().hasId(a)).toSet()
# Traceback otherV is not defined

g.V(a).bothE().otherV().hasId(a).toList()
# This gives me []

# Some information on edges :
g.E().toList()
# [e[16][0-Likes->8], e[17][8-Likes->0], e[18][4-Likes->8], e[19][2-Likes->6], e[20][6-Likes->2], e[21][12-Likes->10], e[22][10-Likes->12], e[23][10-Likes->14], e[24][14-Likes->8], e[25][6-Likes->4]]

How can I achieve this? It seems like an easy problem, but still I am stuck on it.

Wolfgang Fahl
  • 15,016
  • 11
  • 93
  • 186
Tushar Aggarwal
  • 827
  • 1
  • 10
  • 26

1 Answers1

3

There are number of ways to do this - how about this one?

gremlin> a = ['marko','josh','lop']
==>marko
==>josh
==>lop
gremlin> g.V().has('name',within(a)).
......1>   aggregate('a').
......2>   outE().
......3>   filter(inV().where(within('a')))
==>e[9][1-created->3]
==>e[8][1-knows->4]
==>e[11][4-created->3]

This used the modern TinkerPop toy graph as an example. First, we find those starting vertices and aggregate them to a list side-effect called "a". Then, we traverse the outgoing edges of each and filter them for matches with those vertices in "a".

stephen mallette
  • 45,298
  • 5
  • 67
  • 135
  • Since I am using gremlin-python. There is an error in this statement : `NameError: name 'inV' is not defined` (in line ..3>) and similarly, `within` is not defined – Tushar Aggarwal Apr 18 '18 at 11:17
  • 1
    Gremlin is just Gremlin. The language you use doesn't matter except for specific language syntax differences (for example, python counts `in` as a reserved word, so for Gremlin it is `in_`). In your case, I think that you just need to get the proper imports - please see the imports listed here: http://tinkerpop.apache.org/docs/current/reference/#gremlin-python – stephen mallette Apr 18 '18 at 11:44
  • 1
    Just to be a bit more specific `inV()` is just a short for `__.inV()` and `within()` is just short for `P.within()`. For python, if you want to use the same syntax as i have above you would just assign those to functions like `inV = __.inV` – stephen mallette Apr 18 '18 at 11:50
  • `__.inV()` works, but `within` or `P.within` or `__.within` do not – Tushar Aggarwal Apr 18 '18 at 12:30
  • are you saying that you are still getting an error saying that "within is not defined"? or a different error? if you are still getting the "not defined" problem I'm not sure what could be wrong: https://github.com/apache/tinkerpop/blob/3.3.2/gremlin-python/src/main/jython/gremlin_python/process/traversal.py#L232-L234 – stephen mallette Apr 18 '18 at 12:35
  • I get `within is not defined` or `P is not defined` or `type object '__' has no attribute 'within'` respectively – Tushar Aggarwal Apr 18 '18 at 12:37
  • I think I have not imported this file, as I am aldo getting `Traversal is not defined`. Here are my imports, I got them from the same link you shared earlier. - `from gremlin_python import statics` `from gremlin_python.structure.graph import Graph` `from gremlin_python.process.graph_traversal import __` `from gremlin_python.process.strategies import *` `from gremlin_python.driver.driver_remote_connection import DriverRemoteConnection` – Tushar Aggarwal Apr 18 '18 at 12:45
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/169244/discussion-between-tushar-aggarwal-and-stephen-mallette). – Tushar Aggarwal Apr 18 '18 at 12:48