Cypher 发散和收敛路径聚合

Cypher diverging and converging path aggregate

我有一个关于多条路径聚合的问题,聚合会收敛然后再次发散。一些聚合应该只考虑路径的一个子集,而其他聚合则更多。

我会用产品制造的例子尽可能地解释这一点。 假设我有一家公司生产 1 种产品,其中包含一些 material,由供应商提供。 更具体地说,该公司生产一种产品类型的 5 种产品,由 10 克 material 组成。因此,在制造过程中,他们使用了 50 克 material。但在生产中存在 material 浪费,他们实际使用了 70 克,浪费了 20 克。

我想计算的是每个产品和供应商的 material 校正重量,考虑到浪费。在这种情况下,这很容易。 70克

当这变得更复杂时会发生什么:

现在 material1 每个产品 1 和供应商 1 的更正重量为 58.82 克。 这是公式:

material composition = sum(production amount * product composition)
corrected weight = (production amount * product composition * 
                     (purchased / (material composition)))

material composition = (5 * 10) + (20 * 40) = 850
corrected weight = (5 * 10 * (1000 / (850))) = 58.82

因此,运行 对此示例的密码查询应该给我 6 个结果,因为这是产品、material 和供应商的排列数。

问题是,如何编写这样的查询。我试过 reduce 函数,repeated with's 等,但它似乎总是聚集在错误的节点集上...

为了完整起见,这里是生成图表的密码:

创建:

create (c:Company {name:'test', id:'c1'}),  
       (p1:Product {name:'product1', id:'p1'}),  
       (p2:Product {name:'product2', id:'p2'}), 
       (m1:Material {name:'material1', id:'m1'}), 
       (m2:Material {name:'material2', id:'m2'}), 
       (s1:Supplier {name:'supplier1', id:'s1'}), 
       (s2:Supplier {name:'supplier2', id:'s2'}), 
       (s3:Supplier {name:'supplier3', id:'s3'})

相关:

match (c:Company {id:'c1'}), 
    (p1:Product {id:'p1'}), 
    (m1:Material {id:'m1'})
    merge (c)<-[pb_r1:PRODUCED_BY {amount:5}]-(p1)-[co_r11:CONSISTS_OF {amount:10}]->(m1)
    with c, p1, m1
    match (p2:Product {id:'p2'})
    merge (c)<-[pb_r2:PRODUCED_BY {amount:20}]-(p2)-[co_r12:CONSISTS_OF {amount:40}]->(m1)
    with p1, p2, m1
    match (s1:Supplier {id:'s1'})
    merge (m1)-[pf_r1:PURCHASED_FROM {amount: 1000}]->(s1)
    with p1, p2
    match (m2:Material {id:'m2'})
    merge (p1)-[co_r21:CONSISTS_OF {amount:30}]->(m2)
    with p2, m2
    merge (p2)-[co_r22:CONSISTS_OF {amount:80}]->(m2)
    with m2
    match (s2:Supplier {id:'s2'})
    merge (m2)-[pf_r2:PURCHASED_FROM {amount: 1000}]->(s2)
    with m2
    match (s3:Supplier {id:'s3'})
    merge (m2)-[pf_r3:PURCHASED_FROM {amount: 1000}]->(s3)
// Selection of the supply chain and production by Company 
//
MATCH (C:Company {id:'c1'})
        <-[pb:PRODUCED_BY]-
      (P:Product)
        -[co:CONSISTS_OF]->
      (M:Material)
        -[pf:PURCHASED_FROM]->
      (S:Supplier)

// Grouping by materials, calculation material composition,
// and the preservation of the chain to the supplier
// 
WITH M,
     S, // group by supplier 
     SUM(pb.amount*co.amount) as mComp, 
     collect({
       product:P,
       prod: pb.amount, 
       comp: co.amount, 
       purchased: pf.amount
     }) as tmps

// Calculating the correct weight by material and supplier
// 
UNWIND tmps as tmp
RETURN M as material, 
       tmp['product'] as product, 
       S as supplier,
       1.0 * tmp['prod'] * tmp['comp'] * tmp['purchased'] / mComp as cWeight