Neo4j:创建一个递归查询/函数

时间:2017-09-22 02:32:49

标签: recursion neo4j cypher

☼你好!

我希望获得活动列表的关键路径,但是要通过Neo4j。

为此,我需要最早的时间(开始和结束)。活动的最早开始等于其前辈的最大最早结束,等等。

我已经有了“工作”的东西。但我的问题是我只需要“回忆起这个功能”。我可以手工下去,但我不能无限期地去做......

Activity List

这是我的代码:

// LEVEL 1
/****** collect (start.successors) as startSucessors *****/
MATCH (project:Project)-[:CONTAINS]->(:Activity{tag:'Start'})-[s:ENABLES]->(:Activity) 
WHERE ID(project)=toInteger(322)
WITH collect(endNode(s)) AS startSuccessors
    /**** foreach node in startSucessors ****/
    UNWIND startSuccessors AS node
    /**** collect (node.predecessors) as nodePredecessors ****/
        MATCH (activity:Activity)-[p:ENABLES]->(node)
        WITH collect(startNode(p)) AS nodePredecessors, node, startSuccessors
            /**** foreach activity in nodePredecessors ****/
            UNWIND nodePredecessors AS activity
                /**** IF (node.ES is null OR node.ES < activity.EF) ****/
                WITH node, activity, startSuccessors,(node.ES = 0) AS cond1, (node.ES < activity.EF) AS cond2 
                MERGE (activity)-[:ENABLES]->(node)
                ON MATCH SET  node.ES =
                  CASE
                    /**if**/ WHEN cond1 OR cond2 
                    /**node.ES = activity.EF**/ THEN activity.EF
                      END
                ON MATCH SET node.EF = node.ES + node.ET
     // LEVEL 2
     /**T.O.D.O. : loop for each node in startSuccessors and their nodes **/
     WITH startSuccessors
     UNWIND  startSuccessors AS node
        MERGE (node)-[s2:ENABLES]->(successor:Activity)
        WITH collect(successor) AS nodeSuccessors,node
        UNWIND nodeSuccessors AS successor 
            CREATE UNIQUE (act:Activity)-[p2:ENABLES]->(successor)
            WITH successor, node,act, (successor.ES = 0) AS cond3, (successor.ES < act.EF) AS cond4
            MERGE (act)-[p2:ENABLES]->(successor)
            ON MATCH SET  successor.ES =
                  CASE
                    /**if**/ WHEN cond3 OR cond4 
                    /**node.ES = activity.EF**/ THEN act.EF
                      END
                ON MATCH SET successor.EF = successor.ES + successor.ET

结果如下 Earliest Times Query Result

第二个问题是,如果我重新运行查询,ES和EF属性会消失......(见下文)

Problem when rerunning the query

要修复此问题,我必须运行此查询:

MATCH (p:Project) WHERE ID(p)=322
MATCH (p)-[:CONTAINS]->(one:Activity{tag:'one'}),(p)-[:CONTAINS]->(zrht:Activity{tag:'zrht'}),(p)-[:CONTAINS]->(ore:Activity{tag:'ore'}),(p)-[:CONTAINS]->(bam:Activity{tag:'bam'}),(p)-[:CONTAINS]->(two:Activity{tag:'two'})
SET one.EF = 0,one.ES = 0,one.LF=0,one.LS=0,zrht.EF = 0,zrht.ES = 0,zrht.LF=0,zrht.LS=0,ore.EF = 0,ore.ES = 0,ore.LF=0,ore.LS=0,bam.EF = 0,bam.ES = 0,bam.LF=0,bam.LS=0,two.EF = 0,two.ES = 0,two.LF=0,two.LS=0

javascript code达到我想做的目的。

非常感谢你的帮助。

1 个答案:

答案 0 :(得分:1)

☼我终于找到了我要找的东西:Project Management with Neo4j

希望能帮助其他人更快地找到;)