簡體   English   中英

Gremlin>遞歸查找通過邊線類型連接的節點

[英]Gremlin > recursively find nodes connected by an edge type

只需使用TinkerGraph ,並嘗試遞歸查找由特定邊緣標簽(在本例中為created )連接的節點。

  1. 有沒有辦法我可以遞歸地(/循環)遍歷節點? 在下面的示例中,我想循環直到沒有更多匹配的邊(而不是硬編碼的3值)。
  2. 無論如何,給定一個圖,是否可以找到並分組連接的頂點?

用於重復數據刪除節點和處理節點循環的額外榮譽。

依賴

compile("com.thinkaurelius.titan:titan-berkeleyje:0.5.4")
compile('com.tinkerpop:gremlin-groovy:2.6.0')

代碼(手動遞歸3次:()

Gremlin.load()
def g = TinkerGraphFactory.createTinkerGraph()
println g.v(5).as('x')
    .both('created')
    .dedup
    .loop(2){it.loops <= 3}
    .path
    .toList().flatten() as Set // groovy code to flatten & dedup

給我:(正確)

[v[5], v[4], v[3], v[1], v[6]]

謝謝!

您不需要任何Groovy代碼,只需使用Gremlin即可完成:

gremlin> g.v(5).as('x').both('created').dedup()
gremlin>     .loop('x') {true} {true}.dedup()
==>v[4]
==>v[3]
==>v[5]
==>v[6]
==>v[1]

這是我目前的解決方案。 這是一項正在進行中的工作,所以我很樂意提出改進和建議。 (當然可以使用Gremlin語法對其進行優化嗎?)

假設:我們得到一個開始節點

Gremlin.load()
def g = TinkerGraphFactory.createTinkerGraph()
def startV = g.v(5)
def seen = [startV] // a list of 'seen' vertices
startV.as('x')
    .both('created')
    .filter { // only traverse 'unseen' vertices
        def unseen = !seen.contains(it)
        if (unseen){
            seen << it
        }
        unseen
    }
    .loop('x'){
        // continue looping while there are still more 'created' edges...
        it.object.both('created').hasNext() // ##
    }
    .toList() // otherwise won't process above pipeline
println seen

##我不確定為什么這種情況有效/找不到以前遍歷的邊緣。 誰能解釋?

給我:

[v[4], v[5], v[3], v[1], v[6]]

暫無
暫無

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

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