As you can see, I have 2 groups of nodes (yellow and green nodes). The nodes are linked within each group with "positive" relations ([PARTNER_OF] and [ALLIED_WITH]), while nodes of different groups have just "bad" relations ([ENEMY_OF], in the example).
Is it possible to create a constrain to prevent the creation of "positive" relations ('PARTNER_OF' and 'ALLIED_WITH', in this example) between nodes of different groups?
Cypher does not have a constraint like this. You could try to use an apoc trigger to execute when a new relationship is created. You may be able to raise an exception if the new relationship relates two nodes in different groups.
//avoid enemies
PROFILE
// create wrong relation
MATCH (p1:Person {name: 'Deb'}), (p2:Person {name: 'Mark'})
MERGE (p1)-[r:PARTNER_OF]->(p2)
// check cluster
WITH p1, r
MATCH (p1)-[:PARTNER_OF|ALLIED_WITH*]-(ps)
WITH p1, r, collect(DISTINCT ps) as nodes
UNWIND [p1] + nodes as s
MATCH (s)-[:ENEMY_OF]-(e) WHERE NOT s.name = e.name AND e IN nodes
WITH r, collect(DISTINCT e) as enemies
FOREACH (i in
CASE WHEN not enemies = [] THEN [1] ELSE [] END | DELETE r
)
Could you suggest how to break the UNWIND loop as soon as line 11 MATCH finds the first result?
Thanks!