为什么gremlin在我的脚本中输出“null”?

时间:2017-07-26 20:54:32

标签: groovy gremlin

我正在学习使用Gremlin,我刚刚开始使用命令行制作基本图形。我已经知道我可以把它放到一个脚本中,这基本上只是一堆groovy(我也只是熟悉)。我运行以下脚本,一切都很好,除了我得到'==>一旦脚本完成运行(除了有关所做节点/边的所有正确消息),则为null。我做错了什么?

graph = TinkerGraph.open()
g = graph.traversal()
//The nodes
curCharge = graph.addVertex(id, 1, label, "Topic", "name","Current")
pow = graph.addVertex(id, 2, label, "Topic", "name","power")
powFromNRG = graph.addVertex(id, 3, label, "Topic", "name","powerFromNRG")
NRGFromPower = graph.addVertex(id, 4, label, "Topic", "name","NRGFromPower")
powFromVI = graph.addVertex(id,5,label,"Topic","name","powerFromIV")
powInRes = graph.addVertex(id,6,label,"Topic","name","powerInResistors")
passSign = graph.addVertex(id,7,label,"Topic","name","passiveSignPower")
ohmLaw = graph.addVertex(id,8,label,"Topic","name","ohmsLaw")

//the pre-requisite edges
pow.addEdge("prereq",powFromNRG)
pow.addEdge("prereq",NRGFromPower)
pow.addEdge("prereq",powFromVI)
powFromVI.addEdge("prereq",passSign)
passSign.addEdge("prereq",powInRes)
ohmLaw.addEdge("prereq",powInRes)

//just some test queries
nL = g.V().has("name","passiveSignPower").inE("prereq").outV()
if(nL != null)
    println "Hey!"

1 个答案:

答案 0 :(得分:2)

Gremlin控制台返回执行的每一行的结果。在您的情况下,执行的最后一行是println "Hey!"这是void方法,因此返回null

以下是您所看到的结果:

gremlin> if(nL != null)
......1>     println "Hey!"
Hey!
==>null

您可以看到Hey!已打印到屏幕上,但结果为null

如果您更改脚本只是返回字符串Hey!,则可以避免获得null结果并获取字符串结果:

gremlin> if(nL != null)
......1>     "Hey!"
==>Hey!

查看Gremlin Console Tutorial以获取有关自动迭代的更多讨论。