Search code examples
tinkerpopjanusgraphtinkerpop3gremlinpython

Verifying iterative edge insertion in Gremlinpython


Trying to iteratively add vertices and edges. It seems to work, there are no errors, but I wish to verify that the edges are also correctly added.

The loops below insert at least the nodes, as the printing of the list length at the end shows, but the edges are either 1) not inserted, or 2) the way to collect them in a list is incorrect.

Any help is much appreciated!

def vertices01(nodename, rangelb, rangeub, prop1name, prop1val, prop2name):
    t = g.addV(nodename).property(prop1name, prop1val).property(prop2name, rangelb)
    for i in range(rangelb + 1, rangeub):
        t.addV(nodename).property(prop1name, prop1val).property(prop2name, i)
    t.iterate()

def edges01(from_propname, from_propval, to_propname, rangelb, rangeub, edge_name, edge_prop1name):
    to_propval = rangelb
    edge_prop1val = rangelb
    t = g.V().has(from_propname, from_propval).as_("a").V().has(to_propname, to_propval).as_("b").addE(edge_name).from_("a").to("b").property(edge_prop1name, edge_prop1val)
    for i in range(rangelb, rangeub):
        to_propval = i + 1
        edge_prop1val = i
        # changing this to t.has(...) seems to not influence the results (still 0 picked up by the loop)
        t.has(from_propname, from_propval).as_("a").V().has(to_propname, to_propval).as_("b").addE(edge_name).from_("a").to("b").property(edge_prop1name, edge_prop1val)
    t.iterate()

vertices01("ABC", 1, 21, "aa01", 1, "bb01")
edges01("aa01", 1, "bb01", 1, 10 , "aa01-to-bb01", "aa01-to-bb01-propX")

ls1 = []
ls1 = g.V().outE("aa01-to-bb01").has("aa01-to-bb01-propX", 2).toList()
print(len(ls1)) 

ls2 = []
ls2 = g.V().has("aa01", 1).toList()
print(len(ls2)) 

> results:
0
20

Expected results:

> results:
1
20

EDIT: I have changed this bit in the edges01 loop:

    t = g.V().has(from_propname, from_propval) ...

to

    t.has(from_propname, from_propval) ...

But the results are still 0.


Solution

  • You are starting the traversal over again each time with t = g.V()... in the code that adds edges. Only the very last traversal created is going to get iterated. In the code that creates the vertices you are extending the traversal. That is the difference.

    UPDATED

    You should be able to do something along these lines

    t = g.V().has('some-property','some-value').as_('a').
          V().has('some-property','some-value').as_('b')
    

    and then inside the loop

    t.addE('myedge').from_('a').to('b')