0

I have a system that is run by an organization to implement a certain function, thus I have the relationship:

Organization → Function → System

but sometimes the function remains unknown, in which case I want to bind the system directly to the organization:

Organization → System

how do I write this kind of thing?

the System contains attributes that are used for creating these relationships e.g.

create (s:System {id: 'x', Organization: 'MST', Function: 'CM'})

or

create (s:System {id: 'x', Organization: 'MST'})

which means I can find the nodes like this:

match (s:System), (o:Organization {Code: s.Organization})
optional match (f:Function {Code: s.Function})

...but how do I create the relationship?

ekkis
  • 9,804
  • 13
  • 55
  • 105
  • A bit offtopic i think but, id consider doing `System -> Organization -> Function` instead, I'm not sure I understand your model. – Felype Apr 13 '15 at 17:25
  • an organization performs a number of functions, each of which is supported by a given system – ekkis Apr 13 '15 at 17:28
  • Does this answer your question? [Cypher Neo4J - CASE Expression with MERGE](https://stackoverflow.com/questions/27576427/cypher-neo4j-case-expression-with-merge) – canbax Apr 17 '20 at 15:24

2 Answers2

1

I found Mark Needham's post:

http://www.markhneedham.com/blog/2014/06/17/neo4j-load-csv-handling-conditionals/

...which offers a torturous syntax but provides a solution. Is there a nicer way to accomplish this?

match (s:System), (o:Organization {Code: s.Organization}) 
optional match (d:Function {Code: s.Function}) 
foreach (n in (case when s.Function is null then [1] else [] end) | 
  create (o)-[:Runs]->(s)
) 
foreach (n in (case when s.Function is not null then [1] else [] end) | 
  create (o)-[:Function]->(f)-[:SupportedBy]->(s)
);
ekkis
  • 9,804
  • 13
  • 55
  • 105
0

Since you always have System nodes, you could re-arrange your data model to look like this

(:Org {Code: 1})-[:HAS_SYSTEM]->(:System {id: 2})-[:HAS_FUNCTION]->(:Function {Code: 3})

The HAS_FUNCTION relationship and the Function node would be optional.

So, if you wanted to find the System(s) for Org Code 1 that have Function Code 3, you could do this:

MATCH (:Org {Code: 1})-[:HAS_SYSTEM]->(s:System)-[:HAS_FUNCTION]->(:Function {Code: 3})
RETURN s;
cybersam
  • 63,203
  • 6
  • 53
  • 76
  • 1
    but the question concerns the creation of the relationship. what I don't know is how to express a conditional create e.g. `IF s.Function is not null THEN create (o)-[:Function]->(f)-[:SupportedBy]->(s) ELSE create (o)-[:Runs]->(s); END` – ekkis Apr 13 '15 at 18:03