2015-02-06 53 views
1

我想遍歷一個圖並返回鏈接2個節點的所有路徑,其中第一個關係是傳出,第二個傳入。例如,如果關係是投票,我想看到從節點25到節點86的所有可能的路徑,我有MATCH p=((n {id:"25"}) -[*1..2]-> (m {id:"86"})) RETURN p;然後,我想檢查是否在返回的路徑中,我有同樣類型的屬性在傳出和傳入關係(如果他們擁有相同的投票)。
我嘗試在java中使用圖遍歷api來完成該操作,但是我返回的是單個路徑如何獲取所有可能的路徑以檢查它們?neo4j java遍歷返回多個路徑而不是一個

{它基本上是與所有普通的鄰居問題檢查關係}

int common = 0; 
    int diff = 0; 
    for (Path position : graphDb.traversalDescription() 
      .relationships(Wikilections_user.RelTypes.Voted, Direction.OUTGOING) 
      .relationships(Wikilections_user.RelTypes.Voted, Direction.INCOMING) 
      // .evaluator(Evaluators.fromDepth(1)) 
      .evaluator(Evaluators.toDepth(2)) 
      .evaluator(Evaluators.includeWhereEndNodeIs(node2)) 
      // .evaluator(Evaluators.excludeStartPosition()) 
      .traverse(node1)) 
      { 
     Iterable<Relationship> myRels = position.reverseRelationships(); 

     for (Relationship temp : myRels) { 
      System.out.println((temp.getStartNode()).getProperty("id") + " with " + temp.getProperty("with") + " :" + (temp.getEndNode()).getProperty("id")); 
     } 
      String with = ""; 
        int i = 0; 

     for (Relationship temp : myRels) { 
         if (i == 0) { 
          with = (String) temp.getProperty("with"); 
          i++; 
         } 
         if (i == 1) { 
          if (((String) temp.getProperty("with")).equals(with)) { 
           common++; 
          } else { 
           diff++; 
          } 
         } 
        } 
    } 
    return (double) common * 100/(common + diff); 

回答

2

遍歷有uniqueness rules。閱讀該鏈接,它會介紹遍歷器的工作原理以及如何配置它。默認情況下,唯一性規則設置爲NODE_GLOBAL,這意味着某個節點不能被遍歷多次。

我懷疑這可能是你的問題;如果您正在查找一個目標節點,但您希望指向該節點的所有路徑,則應該使用RELATIONSHIP_GLOBAL,或者使用文檔中列出的其他選項之一。您的遍歷器正在查找一個末端節點,並且默認情況下您只能遍歷該節點一次。

所以要嘗試此修復程序,添加不同的獨特性,以你的遍歷描述:

graphDb.traversalDescription() 
    .relationships(Wikilections_user.RelTypes.Voted, Direction.OUTGOING) 
    .relationships(Wikilections_user.RelTypes.Voted, Direction.INCOMING) 
    .uniqueness(Uniqueness.RELATIONSHIP_GLOBAL); 
+0

你是對我真正需要的關係的獨特性.uniqueness(Uniqueness.RELATIONSHIP_PATH),但我怎麼能做出一定第一個獲得傳出,然後傳入而不是2個傳出或2個傳入或傳出? – anu 2015-02-09 11:13:30

+0

我的意思是我如何使用 遍歷(Traverser.Order traversalOrder,StopEvaluator stopEvaluator,ReturnableEvaluator returnableEvaluator,RelationshipType firstRelationshipType,Direction firstDirection,RelationshipType secondRelationshipType,Direction secondDirection) 爲了得到我想要的這個例子 – anu 2015-02-09 11:44:39

相關問題