如何在neo4j节点属性中以字符串形式存储和使用规则

ne5o7dgx  于 11个月前  发布在  其他
关注(0)|答案(1)|浏览(172)
create (a:Class {name:'A'})-[:subClassOf]->(b:Class {name:'B'}) 
create (b)-[:subClassOf]->(c:Class {name:'C'}) 
create (rule:Rule {name:'Rule1',
       condition:'exists( (a:Class)-[:subClassOf]->(b:Class) ) AND exists(  (b:Class)-[:subClassOf]->(c:Class) )', 
       action:'create (a)-[:subClassOf]->(c)'})

字符串
我尝试将规则数据保存为节点属性中的字符串,而不是后端程序中的字符串,然后创建一个查询,读取规则中的条件,如果返回true,则执行操作,例如:如果存在任何A-[:subClassOf]->B AND B-[:subClassOf]->C,则创建A-[:subClassOf]->C。
我写了一个查询如下:

MATCH (rule:Rule)
WHERE rule.name = 'Rule1'
WITH rule, toBoolean(rule.condition) AS condition
WHERE condition
CALL apoc.cypher.run(rule.action) YIELD value
RETURN value


但是这个查询似乎没有返回任何结果,我应该怎么做才正确呢?谢谢帮助!

dldeef67

dldeef671#

我倾向于使用像CALL apoc.cypher.run(rule.condition,{}) YIELD value as condition这样的东西来运行和评估你的条件。
然后你可以使用类似CALL apoc.do.when()的东西来运行基于条件值的操作Cypher。然而,这意味着你需要再次重新选择条件数据,因为运行一个通用的create (a)-[:subClassOf]->(c)将在整个数据集上执行此操作,而不仅仅是where(a)-->(B)-->(c)。因此,我建议更新操作Cypher以选择数据子集并创建新关系。
它看起来像这样:

create (a:Class {name:'A'})-[:subClassOf]->(b:Class {name:'B'}) 
create (b)-[:subClassOf]->(c:Class {name:'C'}) 
create (rule:Rule {name:'Rule1',
       condition:'RETURN exists((:Class)-[:subClassOf]->(:Class)-[:subClassOf]->(:Class)) AS test', 
       action:'MATCH (a:Class)-[:subClassOf]->(b:Class)-[:subClassOf]->(c:Class) MERGE (a)-[r:subClassOf]->(c)'})

MATCH (rule:Rule {name: 'Rule1'})
CALL apoc.cypher.run(rule.condition,{}) YIELD value
WITH rule, value.test as condition
CALL apoc.do.when(condition, "CALL apoc.cypher.runSchema(rule.action,{}) YIELD value RETURN value", "RETURN 'no action taken'", {rule:rule}) YIELD value
RETURN value;

字符串

相关问题