Skip to content

Instantly share code, notes, and snippets.

@luanne
Last active August 29, 2015 13:56
Show Gist options
  • Save luanne/8932364 to your computer and use it in GitHub Desktop.
Save luanne/8932364 to your computer and use it in GitHub Desktop.
Animal Party Graph Gist

Animal Party

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:

  1. Must have every animal from his LOVE list attend the party, if he has animals that he loves

  2. Must have at least one animal from his LIKES list attend the party, if he has animals that he likes

  3. 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.

Setup

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)

The graph

match n return n

Queries:

I’m throwing a party where I will have kittens, puppies, guppies and manatees. Who will come?

  • 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

I’m throwing a party where I will have kittens,guppies and manatees. Who will come?

  • 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

I’m throwing a party where I will have just an echidna. Who will come?

  • 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

I’m throwing a party where I will have a basilisk and echidna. Who will come?

  • 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:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment