Neo4js conditional information storage

61 Views Asked by At

I am new to graph database and stuck with the following issue. I'm trying to store below conditional information in the graph.

  1. when a=1 and b=2 then sum=3,
  2. when a=2 and b=3 then sum=5 and mul=6
    Here there are 4 pre-conditions[(a=1, b=2),(a=2, b=3)], 3 post conditions(sum=3,sum=5,mul=6) The number of pre/post conditions can change from sentence to sentence.
    What is the appropriate way to store such information in graphs.

Case 1: enter image description here
Case 2: enter image description here

Or please do suggest any other scalable way to store such info which can be easily queried.

1

There are 1 best solutions below

0
On

One option is to use something like this graph, only Input, Cond and Res nodes:

MERGE (a:Input{key: 'a', value: 2})
MERGE (b:Input{key: 'b', value: 1})
MERGE (c:Res{key: 'sum', value: 5})
MERGE (d:Input{key: 'a', value: 7})
MERGE (e:Res{key: 'sum', value: 19})

MERGE (a)-[:POINTS]-(c)
MERGE (b)-[:POINTS]-(c) 
MERGE (d)-[:POINTS]-(e) 
MERGE (b)-[:POINTS]-(e)

With a result from a query like this:

MATCH (n:Res{key: 'sum'})<-[:POINTS]-(a:Input{key: 'a', value: 2})
WITH n
MATCH (n)<-[:POINTS]-(b:Input{key: 'b', value: 1})
WITH n
MATCH (n)<--(p:Input)
WITH n, COUNT(p) as inputCount
WHERE inputCount=2
RETURN n

Or:

MATCH (res:Res)<--(i:Input)
WITH res, count(i) as inputCount
WHERE EXISTS {MATCH (res)<--(Input{key: 'a', value: 2})}
  AND EXISTS {MATCH (res)<--(Input{key: 'b', value: 1})}
  AND inputCount=2
RETURN res

But keep in mind that this works for 'AND' conditions only