16
© Lifealike Limited, 2013. Kaiso an object persistence framework for Python and Neo4j 1 David Szo>en onefinestay @szo;en github.com/davidszo;en

Kaiso: Modeling Complex Class Hierarchies with Neo4j - David Szotten @ GraphConnect London 2013

Embed Size (px)

DESCRIPTION

In this talk David will summarize business and technical use cases and introduce Kaiso. He will give a basic overview of how to use it, along with some examples of how one might use it to model complex class hierarchies. This will include some interactive code demonstrations. David will explore the main design goals of the project, the current state of the project, and take a look at what’s ahead on Kaiso’s roadmap.

Citation preview

Page 1: Kaiso: Modeling Complex Class Hierarchies with Neo4j - David Szotten @ GraphConnect London 2013

©  Lifealike  Limited,  2013.

Kaiso  -­‐  an  object  persistence  framework  for  Python  and  Neo4j

�1

David  Szo>enonefinestay  

!!!

@szo;en  github.com/davidszo;en

Page 2: Kaiso: Modeling Complex Class Hierarchies with Neo4j - David Szotten @ GraphConnect London 2013

©  Lifealike  Limited,  2013.

Overview

• About  onefinestay  

• We  care  about  homes  

• How  we  track  our  homes  using  Neo4j  

• Kaiso  demo

�2

Page 3: Kaiso: Modeling Complex Class Hierarchies with Neo4j - David Szotten @ GraphConnect London 2013

©  Lifealike  Limited,  2013.�3

onefinestay  -­‐  the  unhotel  

Page 4: Kaiso: Modeling Complex Class Hierarchies with Neo4j - David Szotten @ GraphConnect London 2013

©  Lifealike  Limited,  2013.

onefinestay  -­‐  the  unhotel

• Guests  live  like  a  local  – but  with  services  and  ameniNes  of  a  hotel  

!

• Members  with  disNncNve  homes  earn hassle-­‐free  income  

!

• From  just  a  handful  of  homes  in  2010  to over  1000  homes  in  4  ciNes  in  2013

�4

Page 5: Kaiso: Modeling Complex Class Hierarchies with Neo4j - David Szotten @ GraphConnect London 2013

©  Lifealike  Limited,  2013.

How  do  we  do  it?

• Provision:  Home  ⇒  Unhotel  

– Clean  – Pack  away  certain  items  • Make  wardrobe  space  

• Deprovision:  Unhotel  ⇒  Home  

– Clean  – Put  everything  back

�5

Page 6: Kaiso: Modeling Complex Class Hierarchies with Neo4j - David Szotten @ GraphConnect London 2013

©  Lifealike  Limited,  2013.

We  care  about  our  homes.  A  lot.

• Every  home  is  different  

• We  require  inNmate  knowledge  

!• For  guests:  “I  can’t  walk  up  stairs”,  “Where  is  the  iron?”  

• For  members:  “Please  put  my  fragile  vase  away  before  

the  guests  arrive”  

• For  us:  “How  much  bed  linen  do  I  need  to  take  to  this  

home  for  this  booking?”

�6

Page 7: Kaiso: Modeling Complex Class Hierarchies with Neo4j - David Szotten @ GraphConnect London 2013

©  Lifealike  Limited,  2013.

Previous  system

• Custom  storage  soluNon  

• Dates  back  to  when  we  were  much  smaller  

• Problems:  

– Siloed  

– Hard  to  query  ⇒  denormalised  copies

�7

Page 8: Kaiso: Modeling Complex Class Hierarchies with Neo4j - David Szotten @ GraphConnect London 2013

©  Lifealike  Limited,  2013.

New  system

• Want  to  model  our  homes  – both  layout  and  contents  !

• Need  flexibility  – Our  needs  are  likely  to  grow  and  change  !

• Track  not  only  objects,  but  their  classes  – Ideally  treat  the  taxonomy  itself  as  data

�8

Page 9: Kaiso: Modeling Complex Class Hierarchies with Neo4j - David Szotten @ GraphConnect London 2013

©  Lifealike  Limited,  2013.

Enter  Neo4j

• Handle  complex  data  structures  

!

• Ensure  extensibility  for  future  needs  

!

• Easy  to  create  powerful  queries  

!

• Makes  for  a  nice  conceptual  model  of  our  data

�9

Page 10: Kaiso: Modeling Complex Class Hierarchies with Neo4j - David Szotten @ GraphConnect London 2013

©  Lifealike  Limited,  2013.

Flexibility  requirements

• We  want  to  associate  informaNon  with  classes  as  well  as  objects  – This  dishwasher  is  of  make  Electropool  

– (All)  appliances  need  PAT  tesNng  !

• Unlike  convenNonal  object  mappers,  the  taxonomy/class  hierarchy  needs  to  be  editable  by  users,  and  so  should  be  part  of  the  data

�10

Page 11: Kaiso: Modeling Complex Class Hierarchies with Neo4j - David Szotten @ GraphConnect London 2013

©  Lifealike  Limited,  2013.

Kaiso  -­‐  an  object  persistence  framework

�11

# (import some stuff)!!class Animal(Entity):! id = Uuid(unique=True)! name = String()! friend_of = Outgoing(FriendOf)!!class Carnivore(Animal): pass!class Herbivore(Animal): pass!!class Penguin(Herbivore):! favourite_ice_cream = String()!!class Lion(Carnivore):! n_siblings = Integer()!

Page 12: Kaiso: Modeling Complex Class Hierarchies with Neo4j - David Szotten @ GraphConnect London 2013

©  Lifealike  Limited,  2013.

Kaiso:  Create  some  instances

�12

# import types, connect to db!!manager = Manager("http://localhost:7474/db/data/")!!manager.save(Lion)!manager.save(Penguin)!!# create some instances!fred = Penguin(name="Fred")!tom = Lion(name="Tom")!!relation = FriendOf(fred, tom)!!manager.save(fred)!manager.save(tom)!manager.save(relation)

Page 13: Kaiso: Modeling Complex Class Hierarchies with Neo4j - David Szotten @ GraphConnect London 2013

©  Lifealike  Limited,  2013.

Instances  are  saved  in  the  graph...

�13

[7]  Lion:  TomFRIENDOF

[6]  Penguin:  Fred

Page 14: Kaiso: Modeling Complex Class Hierarchies with Neo4j - David Szotten @ GraphConnect London 2013

©  Lifealike  Limited,  2013.

...  and  so  is  the  class  hierarchy

�14

FRIENDOF

[1]  Type:  Animal

[7]  Lion:  Tom[6]  Penguin:  Fred

[5]  Type:  Penguin

[4]  Type:  Herbivore [2]  Type:  Carnivore

[3]  Type:  Lion

ISA ISA

ISAISA

INSTANCEOF INSTANCEOF

Page 15: Kaiso: Modeling Complex Class Hierarchies with Neo4j - David Szotten @ GraphConnect London 2013

©  Lifealike  Limited,  2013.

Kaiso:  Queries  based  on  the  type  hierarchy

�15

START! Herbivore=node:persistabletype(id="Herbivore"),! Carnivore=node:persistabletype(id="Carnivore")!!MATCH! Carnivore <-[:ISA*]-()<-[:INSTANCEOF]-(carnivore),! Herbivore <-[:ISA*]-()<-[:INSTANCEOF]-(herbivore),!! (herbivore)-[:FRIENDOF]->(carnivore)!!RETURN! "The herbivore",! herbivore.name,! “is a friend of the carnivore",! carnivore.name;!!=> +--------------------------------------------------------------------+!=> | "The herbivore" | "Fred" | "is a friend of the carnivore" | “Tom" | => +--------------------------------------------------------------------+

Page 16: Kaiso: Modeling Complex Class Hierarchies with Neo4j - David Szotten @ GraphConnect London 2013

©  Lifealike  Limited,  2013.

Thank  you

QuesNons?  

!

!

Kaiso  

github.com/onefinestay/kaiso  

!github.com/davidszo;en  

@davidszo;en

�16