Skip to content

Instantly share code, notes, and snippets.

@dsisnero
Forked from rvanbruggen/IKEA graphgist
Created February 12, 2014 16:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dsisnero/8958733 to your computer and use it in GitHub Desktop.
Save dsisnero/8958733 to your computer and use it in GitHub Desktop.
= IKEA GraphGist =
This gist is to complement the http://blog.bruggen.com/2013/09/ikea-wardrobes-and-graphs-perfect-fit.html[more elaborate blogpost] that I wrote about using http://neo4j.org[neo4j] to model the partlist and the assembly process of two IKEA wardrobes.
First, we will create a part of the graph using this model:
image::https://lh4.googleusercontent.com/7XxUuCjnFtDhO-JAI5Ia6tcYZkQoMfcv_1pNE0mTA2cx76vIySrBU0z0tnykAPvqsMrZZD-cca3Q7ca-ERI0f5sDsGAUCEJTIx7wt15mxKhFEXeYFrZD_vEGJQ[]
[source,cypher]
----
// create the nodes
create
(shoparticle{id:'1',name:'ShopArticle',type:'ShopArticle'}),
(screw{id:'112996',name:'Screw',type:'Component'}),
(nail{id:'124593',name:'Nail',type:'Component'}),
(left{id:'10',name:'Sidepanel left',type:'StructureComponent'}),
(right{id:'11',name:'Sidepanel right',type:'StructureComponent'}),
(top{id:'12',name:'Top',type:'StructureComponent'}),
(bottom{id:'13',name:'Bottom',type:'StructureComponent'}),
(screwdriver{id:'100',name:'Screwdriver Cross',type:'Tool'}),
(hammer{id:'103',name:'Hammer',type:'Tool'}),
(processone{id:'1001',name:'Step 1',type:'Process Step'}),
(processtwo{id:'1002',name:'Step 2',type:'Process Step'}),
(processthree{id:'1003',name:'Step 3',type:'Process Step'}),
// create the relationships
shoparticle-[:USES_COMPONENT{quantity:12}]->screw,
shoparticle-[:USES_COMPONENT{quantity:40}]->nail,
shoparticle-[:USES_STRUCTURE_COMPONENT{quantity:1}]->left,
shoparticle-[:USES_STRUCTURE_COMPONENT{quantity:1}]->right,
shoparticle-[:USES_STRUCTURE_COMPONENT{quantity:1}]->top,
shoparticle-[:USES_STRUCTURE_COMPONENT{quantity:1}]->bottom,
shoparticle-[:STARTS_CONSTRUCTION]->processone,
processthree-[:COMPLETES_CONSTRUCTION]->shoparticle,
processtwo-[:STARTS_PROCESS{quantity:4}]->screw,
processtwo-[:USES]->screwdriver,
screw-[:CONNECT_TO{quantity:4}]->left,
left-[:ENDS_PROCESS]->processtwo,
processone-[:CONTINUES_PROCESS]->processtwo,
processtwo-[:CONTINUES_PROCESS]->processthree;
----
The actual graph then looks like this:
//graph
Then we can actually start doing some queries, like for example: `Give me the total partlist for a shoparticle`
[source,cypher]
----
START
shoparticle=node:node_auto_index(type="ShopArticle")
match
component<-[:USES_STRUCTURE_COMPONENT|USES_COMPONENT]-shoparticle
return
shoparticle.name as Item, component.name as Component, component.type
order by component.type;
----
//table
Or let's try this one: `Give me the number of installation steps for every ShopArticle`:
[source,cypher]
----
START
shoparticle=node:node_auto_index(type="ShopArticle")
MATCH
p = (shoparticle)-[:STARTS_CONSTRUCTION]->(step)-[:CONTINUES_PROCESS*]->(step2)-[:COMPLETES_CONSTRUCTION]->(shoparticle)
RETURN
shoparticle.name AS Item, (length(p)-1) AS NumberOfSteps;
----
//table
As you can see from the `path` query below, this is indeed the case:
[source,cypher]
----
START shoparticle=node:node_auto_index(type="ShopArticle")
MATCH p =(shoparticle)-[:STARTS_CONSTRUCTION]->(step)-[:CONTINUES_PROCESS*]->(step2)-[:COMPLETES_CONSTRUCTION]->(shoparticle)
RETURN p;
----
//graph
To play some more, use the console below. Enjoy!
//console
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment