如何在Python Gremlin变体中添加边

时间:2016-12-09 21:43:47

标签: python titan gremlin tinkerpop

我试图使用gremlin-python创建一个图形,但我似乎无法弄清楚如何添加边缘。

使用标准Gremlin控制台,我可以执行以下操作:

gremlin> a = g.addV().next()
==>v[0]
gremlin> b = g.addV().next()
==>v[1]
gremlin> g.V()
==>v[0]
==>v[1]
gremlin> a.addEdge('conn', b)
==>e[2][0-conn->1]
gremlin> g.E()
==>e[2][0-conn->1]
gremlin> 

但是当试图通过连接到gremlin服务器的python来做同样的事情时,我似乎无法做同样的事情:

>>> a = g.addV().next()
>>> b = g.addV().next()
>>> g.V().toList()
[v[1519], v[1520]]
>>> a.addEdge('conn', b)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'Vertex' object has no attribute 'addEdge'

我尝试了各种各样的咒语,但似乎无法解决问题,并且无法在任何地方找到任何例子。另外,我在Gremlin文档中看到addEaddEdge的参考,但不能解决差异(上面似乎都不起作用)。

编辑:进一步,但仍然没有运气。似乎GraphTraversal.addE()存在,所以如果我不打电话给next(),那么我可以致电addE ...但我似乎无法获得争论它喜欢的东西。

>>> a = g.addV()
>>> b = g.addV()
>>> a.addE('foo', b).toList()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Development/matt/lib/python2.7/site-packages/gremlin_python/process/traversal.py", line 52, in toList
    return list(iter(self))
  File "/Development/matt/lib/python2.7/site-packages/gremlin_python/process/traversal.py", line 70, in next
    return self.__next__()
  File "/Development/matt/lib/python2.7/site-packages/gremlin_python/process/traversal.py", line 43, in __next__
    self.traversal_strategies.apply_strategies(self)
  File "/Development/matt/lib/python2.7/site-packages/gremlin_python/process/traversal.py", line 284, in apply_strategies
    traversal_strategy.apply(traversal)
  File "/Development/matt/lib/python2.7/site-packages/gremlin_python/driver/remote_connection.py", line 95, in apply
    remote_traversal = self.remote_connection.submit(traversal.bytecode)
  File "/Development/matt/lib/python2.7/site-packages/gremlin_python/driver/driver_remote_connection.py", line 53, in submit
    traversers = self._loop.run_sync(lambda: self.submit_traversal_bytecode(request_id, bytecode))
  File "/Development/matt/lib/python2.7/site-packages/tornado/ioloop.py", line 457, in run_sync
    return future_cell[0].result()
  File "/Development/matt/lib/python2.7/site-packages/tornado/concurrent.py", line 237, in result
    raise_exc_info(self._exc_info)
  File "/Development/matt/lib/python2.7/site-packages/tornado/gen.py", line 1021, in run
    yielded = self.gen.throw(*exc_info)
  File "/Development/matt/lib/python2.7/site-packages/gremlin_python/driver/driver_remote_connection.py", line 73, in submit_traversal_bytecode
    traversers = yield self._execute_message(message)
  File "/Development/matt/lib/python2.7/site-packages/tornado/gen.py", line 1015, in run
    value = future.result()
  File "/Development/matt/lib/python2.7/site-packages/tornado/concurrent.py", line 237, in result
    raise_exc_info(self._exc_info)
  File "/Development/matt/lib/python2.7/site-packages/tornado/gen.py", line 1021, in run
    yielded = self.gen.throw(*exc_info)
  File "/Development/matt/lib/python2.7/site-packages/gremlin_python/driver/driver_remote_connection.py", line 149, in _execute_message
    recv_message = yield response.receive()
  File "/Development/matt/lib/python2.7/site-packages/tornado/gen.py", line 1015, in run
    value = future.result()
  File "/Development/matt/lib/python2.7/site-packages/tornado/concurrent.py", line 237, in result
    raise_exc_info(self._exc_info)
  File "/Development/matt/lib/python2.7/site-packages/tornado/gen.py", line 1024, in run
    yielded = self.gen.send(value)
  File "/Development/matt/lib/python2.7/site-packages/gremlin_python/driver/driver_remote_connection.py", line 236, in receive
    "{0}: {1}".format(status_code, recv_message["status"]["message"]))
gremlin_python.driver.driver_remote_connection.GremlinServerError: 599: Could not locate method: DefaultGraphTraversal.addE([foo, [AddVertexStep({})]])

2 个答案:

答案 0 :(得分:2)

似乎以下语法有效:

>>> a = g.addV()
>>> b = g.addV()
>>> a.addE('foo').to(b).toList()
[e[1534][1532-foo->1533]]

我仍然不清楚addEaddEdge之间的区别,但我猜后者在python中不可用,我对它们的签名感到困惑。

答案 1 :(得分:2)

据我所知,addEdge()适用于图形对象,addE()适用于图形遍历对象。由于你使用的是g(),后者需要addE()。