0

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.

1 Answer 1

1

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')
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you for the answer Kelvin. How should I improve, I am not sure I understand the difference of extending in the vertices loop, and how to achieve a similar effect in the edges loop. Thanks again.
If we take a step back, what are the criteria for adding the multiple edges? Is it essentially to add multiple edges from the same starting vertex to the same ending vertex but that each edge has a different (incremental) property?
I updated the answer with a simple template you can copy from. Hope that helps.
Thank you for your reply. I wonder whether the " as_('a') " at the start of the traversal (t = g...) outside the loop is being updated within the loop? If I assign the 'a' inside the loop to i, is that being fed back into 'some-value' (given that I've assigned 'some-property' already and this doesn't update)? I hope this is somewhat clear... Many thanks again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.