簡體   English   中英

TinkerPop gremlin 僅在 path() 中計算頂點

[英]TinkerPop gremlin count vertices only in a path()

當我查詢路徑時,例如:

g.V(1).inE().outV().inE().outV().inE().outV().path()

path() 中既有頂點又有邊,有沒有辦法只計算路徑中的頂點數而忽略邊?

Gremlin 遺漏了一些重要的東西來使這變得非常容易 - 它不能很好地識別類型以進行過濾,因此TINKERPOP-2234 我對您的示例進行了一些更改,以便我們可以使用一些更棘手的東西:

gremlin> g.V(1).repeat(outE().inV()).emit().path()
==>[v[1],e[9][1-created->3],v[3]]
==>[v[1],e[7][1-knows->2],v[2]]
==>[v[1],e[8][1-knows->4],v[4]]
==>[v[1],e[8][1-knows->4],v[4],e[10][4-created->5],v[5]]
==>[v[1],e[8][1-knows->4],v[4],e[11][4-created->3],v[3]]

使用repeat()我們得到可變長度的Path實例,因此頂點的動態計數比您在問題中的固定示例有點棘手,其中路徑的模式是已知的,並且計數很容易從 Gremlin 本身中辨別出來。 因此,使用動態數量的頂點並且沒有 TINKERPOP-2234,您必須發揮創造力。 一個典型的策略是通過一些 label 或頂點特有的屬性值來過濾掉邊緣:

gremlin> g.V(1).repeat(outE().inV()).emit().path().map(unfold().hasLabel('person','software').fold())
==>[v[1],v[3]]
==>[v[1],v[2]]
==>[v[1],v[4]]
==>[v[1],v[4],v[5]]
==>[v[1],v[4],v[3]]
gremlin> g.V(1).repeat(outE().inV()).emit().path().map(unfold().hasLabel('person','software').fold()).count(local)
==>2
==>2
==>2
==>3
==>3

或者也許使用所有邊獨有的屬性:

gremlin> g.V(1).repeat(outE().inV()).emit().path().map(unfold().not(has('weight')).fold())
==>[v[1],v[3]]
==>[v[1],v[2]]
==>[v[1],v[4]]
==>[v[1],v[4],v[5]]
==>[v[1],v[4],v[3]]
gremlin> g.V(1).repeat(outE().inV()).emit().path().map(unfold().not(has('weight')).fold()).count(local)
==>2
==>2
==>2
==>3
==>3

如果您的架構中沒有這些屬性或標簽允許這樣做,您可能會使用您的遍歷模式來提出一些數學來解決它。 就我而言,我知道我的Path將始終為(pathLength + 1) / 2所以:

gremlin> g.V(1).repeat(outE().inV()).emit().path().as('p').math('(p + 1) / 2').by(count(local))
==>2.0
==>2.0
==>2.0
==>3.0
==>3.0

希望其中一種方法能激發您找到解決方案。

+1 用於 Gremlin 中的typeOf謂詞支持 (TINKERPOP-2234)。

除了@stephan 的回答,您還可以僅標記和 select 頂點:

g.V().repeat(outE().inV().as('v')).times(3).select(all,'v')

此外,如果圖形提供程序支持它,您還可以使用{it.class}

g.V().repeat(outE().inV().as('v')).times(3).path()
    .map(unfold().groupCount().by({it.class}))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM