I'm trying to model my data in Cypher and Neo4j desktop and dynamically create new nodes and relationships based on existing nodes and relationships to improve the data quality.
My MWE looks like this:
// Create nodes
CREATE (a:House {name: "House A", location: 1})
CREATE (b:House {name: "House B", location: 2})
CREATE (c:House {name: "House C", location: 3})
CREATE (d:House {name: "House D", location: 4})
CREATE (e:House {name: "House E", location: 5})
CREATE (f:House {name: "House F", location: 6})
CREATE (g:House {name: "House G", location: 7})
CREATE (h:House {name: "House H", location: 8})
// Create relationships
CREATE (a)-[:PIPE {start: 1, end: 2, id: randomUUID()}]->(b)
CREATE (c)-[:PIPE {start: 1, end: 2, id: randomUUID()}]->(d)
CREATE (c)-[:PIPE {start: 2, end: 3, id: randomUUID()}]->(d)
CREATE (e)-[:PIPE {start: 1, end: 2, id: randomUUID()}]->(f)
CREATE (e)-[:PIPE {start: 2, end: 3, id: randomUUID()}]->(f)
CREATE (e)-[:PIPE {start: 3, end: 4, id: randomUUID()}]->(f)
CREATE (g)-[:PIPE {start: 1, end: 2, id: randomUUID()}]->(h)
CREATE (g)-[:PIPE {start: 2, end: 3, id: randomUUID()}]->(h)
CREATE (g)-[:PIPE {start: 3, end: 4, id: randomUUID()}]->(h)
CREATE (g)-[:PIPE {start: 99, end: 100, id: randomUUID()}]->(h)
RETURN *;
My dataset contains:
- Values for houses and their location
- Which pipes are connecting two houses
- The start and end value of the pipes, which indicate the sequence of the pipes in the ground (If the starting value of pipeY is equal to the end value of pipeX than the direction of is pipeX->pipeY)
What I try to achieve is to replace all the relationships between two houses if there are more than 1 pipe between them and when the pipes are in sequence and insert a junction-node that is missing in the data.
The relationships should look like this in the end:
// Create relationships
CREATE (a)-[:PIPE {start: 1, end: 2, id: randomUUID()}]->(b)
CREATE (c)-[:PIPE {start: 1, end: 2, id: randomUUID()}]->(:Junction {id: randomUUID{})-[:PIPE {start: 2, end: 3, id: randomUUID()}]->(d)
CREATE (e)-[:PIPE {start: 1, end: 2, id: randomUUID()}]->(:Junction {id: randomUUID{})-[:PIPE {start: 2, end: 3, id: randomUUID()}]->(:Junction {id: randomUUID{})- [:PIPE {start: 3, end: 4 id: randomUUID()}]->(f)
CREATE (g)-[:PIPE {start: 1, end: 2, id: randomUUID()}]->(:Junction {id: randomUUID{})-[:PIPE {start: 2, end: 3, id: randomUUID()}]->(:Junction {id: randomUUID{})- [:PIPE {start: 3, end: 4 id: randomUUID()}]->(h)
CREATE (g)-[:PIPE {start: 99, end: 100, id: randomUUID()}]->(h)
RETURN *;
What is import is, that I do not know, how many sequential pipes are in the ground and how many junction-nodes I might need. Furthermore, I need to connect houses, junctions with the pipes in the correct order.