简体   繁体   English

Neo4j:后退遍历

[英]Neo4j: Step back in traversal

Let's assume a very simple example graph of people (as nodes) and KNOWS- and FRIEND_OF-relationships inbetween. 让我们假设一个非常简单的示例人物图(作为节点)以及它们之间的KNOWS-和FRIEND_OF-关系。 Now I want to get every node that KNOWS a random person and is FRIEND_OF (this or) another person. 现在,我想让每个节点都知道一个随机的人并且是FRIEND_OF(这个或另一个)人。 Please consider, that I want to use a traversal to check those relations instead of a simple node.hasRelationship(). 请考虑一下,我想使用遍历来检查这些关系,而不是简单的node.hasRelationship()。

Therefore I would describe a TraversalDescription that checks the path-length in its Evaluator and chooses the KNOWS-relation on step 1. If there is one I (automatically) go to the connected KNOWS-node in the path and would then have to make a step back to the outgoing-node. 因此,我将描述一个TraversalDescription,它检查其评估器中的路径长度并在步骤1中选择KNOWS关系。如果有一个I(自动),请转到路径中连接的KNOWS节点,然后必须创建一个返回到传出节点。 Therefore I would just check when path.length == 2 for incoming KNOWS-relation and set the continue-variable to true if the current traversed node has the id of the path-startnode. 因此,我只检查传入的KNOWS关系的path.length == 2时,如果当前遍历的节点具有path-startnode的id,则将continue-variable设置为true。

The problem: I never come back to the path-startnode (the one I checked for the KNOWS-relation), it never appears in the evaluator; 问题是:我再也没有回到path-startnode(我检查过KNOWS关系的那个),它永远不会出现在评估器中。 I also tried to user different UNIQUENESS-parameters but none of then worked. 我也尝试使用不同的UNIQUENESS参数,但是没有一个起作用。

/edit: Here's my traversal description (pseudo code more or less): / edit:这是我的遍历描述(伪代码或多或少):

TraversalDescription td = Traversal.description()
    .relationships(RelTypes.KNOWS, Direction.BOTH)
    .relationships(RelTypes.FRIEND_OF, Direction.OUTGOING)
    .uniqueness(Uniqueness.NONE)
    .evaluate(new Evaluator(){
        public Object evaluate(final Path path){
            if (path.length == 0) return EXCLUDE_CONTINUE;
            Relation currentRel = path.lastRelationship();
            boolean continue = false;
            boolean include = false;
            if (path.length == 1){
                // check for outgoing knows-relation
                continue = currentRel.isType(KNOWS) && currentRel.startNode().getId == path.startNode().getId();
            }
            else if (path.length == 2){
                // check for outgoing knows-relation to the original start node
                // SECOND PART OF STATEMENT NEVER GETS TRUE!
                toContinue = currentRel.isType(KNOWS) && currentRel.startNode().getId == path.startNode().getId();
            }
            else if (path.length == 3){
                // ...
            }
            return Evaluation.of(toContinue, include);
        }

try 尝试

private static Traverser getHomies(final Node ofPerson) {
        TraversalDescription td = Traversal.description().breadthFirst().relationships(RelTypes.KNOWS, Direction.INCOMING)
                .relationships(RelTypes.FRIEND_OF, Direction.OUTGOING).evaluator(Evaluators.includingDepths(1, 2))
                .evaluator(Evaluators.returnWhereLastRelationshipTypeIs(RelTypes.FRIEND_OF));

        return td.traverse(ofPerson);
    }

Your pseudocode: 您的伪代码:

  • says that no path should be included - field include is everytime false 说,没有路径应包括-场包括为每次
  • your second part of statement is no comparison (==), it is an assignment (=) 您语句的第二部分是无比较(==),它是赋值(=)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM