Python, bulbs, resxter . Getting bool scalar returned by gremlin script from bulbs

81 views Asked by At

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)

1

There are 1 answers

1
stephen mallette On

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.