Neo4j:节点A和F之间的每个分支的耗时如何?

时间:2018-09-15 20:01:17

标签: graph neo4j cypher

下图给出:

AMGO

所有节点均为任务类型。作为属性,它们具有开始时间和结束时间(均为数据类型DateTime)。 所有关系均为CONNECT_TO,并指向右侧。关系没有属性。

有人可以帮助我,以下查询在Cypher中的外观如何: 节点A和F之间的每个分支的耗时如何?

将列表作为结果就可以了:

    -> B --> E -
   /            \ 
A -              -> F
   \            /
    -> C --> D -

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

创建图形

第一个语句创建节点,第二个语句创建它们之间的关系。

CREATE
  (TaskA:Task {name: 'TaskA', time:10}),
  (TaskB:Task {name: 'TaskB', time:20}),
  (TaskC:Task {name: 'TaskC', time:30}),
  (TaskD:Task {name: 'TaskD', time:10}),
  (TaskE:Task {name: 'TaskE', time:40}),
  (TaskF:Task {name: 'TaskF', time:10})

CREATE
  (TaskA)-[:CONNECT_TO]->(TaskB),
  (TaskB)-[:CONNECT_TO]->(TaskE),
  (TaskE)-[:CONNECT_TO]->(TaskF),
  (TaskA)-[:CONNECT_TO]->(TaskC),
  (TaskC)-[:CONNECT_TO]->(TaskD),
  (TaskD)-[:CONNECT_TO]->(TaskF);

您想要的解决方案

  1. 定义起始节点(任务A)
  2. 可变长度的查找路径
  3. 定义终端节点(任务F)
  4. 检索每个路径的所有任务节点
  5. 每个路径的所有任务的持续时间之和
  6. 奖金:每条路径的任务量

Neo4j语句:

//           |----------- 1 -----------|  |----- 2 ----|  |----------- 3 -----------|
MATCH path = (taskA:Task {name: 'TaskA'})-[:CONNECT_TO*]->(taskF:Task {name: 'TaskF'})
UNWIND
// |-- 4 -|
nodes(path) AS task
//           |---- 5 -----|                  |--- 6 ----|
RETURN path, sum(task.time) AS timeConsumed, length(path)+1 AS taskAmount;

结果

╒══════════════════════════════════════════════════════════════════════╤════════════════╤════════════╕
│"path"                                                                │ "timeConsumed" │"taskAmount"│
╞══════════════════════════════════════════════════════════════════════╪════════════════╪════════════╡
│[{"name":"TaskA","time":10},{},{"name":"TaskB","time":20},{"name":"Tas│80              │4           │
│kB","time":20},{},{"name":"TaskE","time":40},{"name":"TaskE","time":40│                │            │
│},{},{"name":"TaskF","time":10}]                                      │                │            │
├──────────────────────────────────────────────────────────────────────┼────────────────┼────────────┤
│[{"name":"TaskA","time":10},{},{"name":"TaskC","time":30},{"name":"Tas│60              │4           │
│kC","time":30},{},{"name":"TaskD","time":10},{"name":"TaskD","time":10│                │            │
│},{},{"name":"TaskF","time":10}]                                      │                │            │
└──────────────────────────────────────────────────────────────────────┴────────────────┴────────────┘