1

I am writing python scripts to extract data from multiple sources and put it in a graph in a certain structure.

I am using bulbs models for all the data. I have models for all relevant node types and relationships. My edge models have not additional properties except 'label'.

As it is in development, I run the same script multiple times. I use get_or_create to prevent duplicate nodes but edges do not have that method. I do not have the object for existing edge since it was created in a previous run of the script.

I saw several question talking about similar things with answers from espeed like this, but I could not find a satisfactory answer for my specific issue.

What would be the simplest code for this method?

Presently I am trying to do this via loading a gremlin script; as suggested by Stephen; with following function:

def is_connected(parent, child, edge_label) {
    return g.v(parent).out(edge_label).retain([g.v(child)]).hasNext()
}

And the the following python code.

g.scripts.update('gremlin_scripts/gremlin.groovy')
script = g.scripts.get('gremlin:is_connected')
params = dict(parent=parent_node.eid, child=menu_item_v.eid, edge_label='has_sub_menu_item')
response = g.gremlin.execute(script, params)

I can't quite figure out how to get the bool result into python. I've also tried the g.gremlin.query(script, param)

Community
  • 1
  • 1
Jinal Kothari
  • 117
  • 1
  • 1
  • 11

1 Answers1

0

Here's one way to do it:

parent_v.out(rel_label).retain(child_v).hasNext()

So, from the parent, traverse out to all children (i assume that "out" is the direction of your relationship - how you choose to implement that is specific to your domain) and determine if that child is present at any point via retain.

stephen mallette
  • 45,298
  • 5
  • 67
  • 135
  • Hey stephen, thanks for the quick answer! The code you mentioned, is it meant to be Gremlin? Bulbs does not seem to have a retain() method. I can manage to run gremlin strings via bulbs. – Jinal Kothari Mar 26 '15 at 10:53