使用 Gremlin-python 和 graphexp 匿名生成子遍历

Rac*_*ghi 2 python gremlin janusgraph graphexp gremlinpython

我正在尝试从头开始创建一个在 graphexp 中可视化的图表,但我正在努力理解匿名遍历的概念以及如何创建它们

我正在使用 python 3.9 和 gremlinpython 3.5.1

  • 创建连接和图表:
from gremlin_python.process.anonymous_traversal import traversal

self.g = traversal().withRemote(DriverRemoteConnection('ws://localhost:8182/gremlin', 'g'))
Run Code Online (Sandbox Code Playgroud)
  • 我导入了静态数据,这样我就可以使用没有 __ 类的步骤:
statics.load_statics(globals())
Run Code Online (Sandbox Code Playgroud)
  • 如果顶点不存在则创建它:
def _add_vertex(self, name):
        return self.g.V().has('name', name).fold().coalesce(unfold(), addV().property('name',name)).next()
Run Code Online (Sandbox Code Playgroud)
  • 如果两个顶点之间不存在边,则创建边:
def _add_edge(self, v1, v2, weight, label):
        return self.g.V(v1).as_("fromVertex").V(v2).coalesce(inE(label).where(outV().as_(
            "fromVertex")), addE(label).property("weight", weight).from_("fromVertex")).next()
Run Code Online (Sandbox Code Playgroud)

但是当我点击一个顶点时,我在 graphexp 中遇到了这个错误

Error retrieving data
The child traversal of [GraphStep(vertex,[696560]), PropertyMapStep(value)] was not spawned anonymously - use the __ class rather than a TraversalSource to construct the child traversal
Run Code Online (Sandbox Code Playgroud)

该文档总体不错,但对于匿名遍历部分没有太大帮助。那么如何使用这种方法生成匿名子遍历呢?它的真正含义是什么?

Kel*_*nce 6

从 TinkerPop 3.5.x 开始,g在遍历开始处以外的任何地方使用都会引发错误。过去可以这样写:

g.V('1').addE('test').from(g.V('2'))

这实际上可能会产生一些不好的副作用,因此在 3.5.x 中,解析器现在强制您必须执行以下操作之一:

g.V('1').addE('test').from(V('2'))

或者

g.V('1').addE('test').from(__.V('2'))

那个“双下划线”类__.就是所谓的匿名遍历源。之所以这样称呼,是因为它没有通过点与前一步连接,而是作为子遍历位于括号内。

我会检查你的代码,看看你是否真的g在遍历中注入了一秒钟。