Added some more animals (Basilisk) and people (Ron and Harry) to test more conditions. From the description:
"However, my focus is on every person in the shape optionally having none, one two or three set of things[*], one of which contains elements which must ALL be matched by the elements of a provided collection, one of which ANY must be matched, and one which contains elements of which NONE must be matched by anything in the (common) provided collection. Implementing this is a core requirement."
Here is what I understood from this-
To attend a party, a person must fulfil at least one of these 3:
-
Must have every animal from his LOVE list attend the party, if he has animals that he loves
-
Must have at least one animal from his LIKES list attend the party, if he has animals that he likes
-
Must have NONE of the animals from his HATES list attending the party, if he has animals that he hates
The query first eliminates the animal haters- since that’s a definite fail condition. Then it optionally matches Likes and Loves. I’d still like to come back and improve this query, this is the first shot at it.
CREATE (kitten:ANIMAL { name: "Kitten" })
CREATE (puppy:ANIMAL { name: "Puppy" })
CREATE (guppy:ANIMAL { name: "Guppy" })
CREATE (goldfish:ANIMAL { name: "Goldfish" })
CREATE (echidna:ANIMAL { name: "Echidna" })
CREATE (hamster:ANIMAL { name: "Hamster" })
CREATE (manatee:ANIMAL { name: "Manatee" })
CREATE (basilisk:ANIMAL { name: "Basilisk" })
CREATE (alice:PERSON { name: "Alice" })
CREATE (bob:PERSON { name: "Bob" })
CREATE (dan:PERSON { name: "Dan" })
CREATE (carol:PERSON { name: "Carol" })
CREATE (ron:PERSON { name: "Ron" })
CREATE (harry:PERSON { name: "Harry" })
CREATE (alice)-[:LOVES]->(kitten)
CREATE (alice)-[:LOVES]->(puppy)
CREATE (alice)-[:LIKES]->(goldfish)
CREATE (alice)-[:LIKES]->(guppy)
CREATE (alice)-[:HATES]->(echidna)
CREATE (bob)-[:LIKES]->(kitten)
CREATE (bob)-[:LIKES]->(hamster)
CREATE (carol)-[:LOVES]->(kitten)
CREATE (carol)-[:LOVES]->(puppy)
CREATE (carol)-[:LIKES]->(goldfish)
CREATE (carol)-[:LIKES]->(guppy)
CREATE (carol)-[:HATES]->(echidna)
CREATE (dan)-[:LIKES]->(kitten)
CREATE (dan)-[:LIKES]->(hamster)
CREATE (ron)-[:LIKES]->(echidna)
CREATE (harry)-[:LIKES]->(echidna)
CREATE (harry)-[:LOVES]->(basilisk)
-
Alice, Carol: Yes (Love kittens and puppies, like guppies, hate echidna)
-
Bob,Dan: Yes (No love list, no hate list, like guppies)
-
Ron: No (No love list or hate list, but likes echidna and he ain’t attending)
-
Harry: No (No hate list, but Loves basilisks and he ain’t attending either)
MATCH (a:ANIMAL) where a.name IN ["Kitten","Puppy","Guppy","Manatee"]
MATCH (p:PERSON) WHERE NOT(p-[:HATES]->a)
WITH p,collect(a.name) as attending
OPTIONAL MATCH (p)-[:LIKES]->(likedAnimals) WITH p,attending, COLLECT(likedAnimals) as anyAttend
WHERE ANY(x in anyAttend WHERE x.name IN attending)
with p,attending
OPTIONAL MATCH (p)-[:LOVES]->(lovedAnimals) WITH p,attending,COLLECT(lovedAnimals) as mustAttend
WHERE ALL(x in mustAttend WHERE x.name IN attending) return p.name
-
Bob,Dan: Yes (likes guppies)
-
Alice, Carol,Harry: No (loves missing)
-
Ron: No (likes missing)
MATCH (a:ANIMAL) where a.name IN ["Kitten","Guppy","Manatee"]
MATCH (p:PERSON) WHERE NOT(p-[:HATES]->a)
WITH p,collect(a.name) as attending
OPTIONAL MATCH (p)-[:LIKES]->(likedAnimals) WITH p,attending, COLLECT(likedAnimals) as anyAttend
WHERE ANY(x in anyAttend WHERE x.name IN attending)
with p,attending
OPTIONAL MATCH (p)-[:LOVES]->(lovedAnimals) WITH p,attending,COLLECT(lovedAnimals) as mustAttend
WHERE ALL(x in mustAttend WHERE x.name IN attending) return p.name
-
Ron: Yes (likes Echidna and no other)
-
Rest: Loves or Likes missing, or Hates present
MATCH (a:ANIMAL) where a.name IN ["Echidna"]
MATCH (p:PERSON) WHERE NOT(p-[:HATES]->a)
WITH p,collect(a.name) as attending
OPTIONAL MATCH (p)-[:LIKES]->(likedAnimals) WITH p,attending, COLLECT(likedAnimals) as anyAttend
WHERE ANY(x in anyAttend WHERE x.name IN attending)
with p,attending
OPTIONAL MATCH (p)-[:LOVES]->(lovedAnimals) WITH p,attending,COLLECT(lovedAnimals) as mustAttend
WHERE ALL(x in mustAttend WHERE x.name IN attending) return p.name
-
Harry: Yes (Loves basilisks, likes echidna, hates nothing)
-
Ron: Yes (likes echidna, loves and hates nothing)
MATCH (a:ANIMAL) where a.name IN ["Echidna","Basilisk"]
MATCH (p:PERSON) WHERE NOT(p-[:HATES]->a)
WITH p,collect(a.name) as attending
OPTIONAL MATCH (p)-[:LIKES]->(likedAnimals) WITH p,attending, COLLECT(likedAnimals) as anyAttend
WHERE ANY(x in anyAttend WHERE x.name IN attending)
with p,attending
OPTIONAL MATCH (p)-[:LOVES]->(lovedAnimals) WITH p,attending,COLLECT(lovedAnimals) as mustAttend
WHERE ALL(x in mustAttend WHERE x.name IN attending) return p.name
Created by Luanne Misquitta: