Skip to content

Instantly share code, notes, and snippets.

= (Product) Hierarchy GraphGist =
This gist is a complement to http://blog.bruggen.com/2014/03/using-neo4j-to-manage-and-calculate.html[blogpost that I wrote] about managing hierarchical data structures in http://www.neo4j.org[neo4j]..
In this example, we are using a "product hierarchy", essentially holding information about the composition of a product (what is it made of, how many of the components are used, and at the lowest level, what is the price of these components). The model looks like this:
image::http://1.bp.blogspot.com/-XIjEXWHpNmc/Uzbhuoo-9xI/AAAAAAABNWE/7zYyn3Vl3i0/s3200/Screen+Shot+2014-03-29+at+16.04.35.png[]
Note that in the graphgist, I have cut the tree depth to 5 levels (product to costs) instead of 6 in the blogpost - and that I also reduced the width of the tree to make it manageable in a gist.
@rvanbruggen
rvanbruggen / thuis.cql
Last active August 29, 2015 13:58
Thuis GraphGist
= The Belgian Sitcom Graph: Who's Hot in "Thuis" =
This gist is going to show you how you can query the elaborate landscape of relationships in http://www.een.be/programmas/thuis/[Thuis]. It is based on this picture by https://twitter.com/pieter_vh[@pieter_vh]. Very well done actually.
image::http://qsdf.be/thuisopeen/infografiekthuis.png[scaledwidth="50%"]
I took the picture and created a spreadsheet version of it - take a look https://docs.google.com/spreadsheets/d/1l1bfIU6qWvZpU0xeSjJoRBlKWxGCDdLaNlUjeR0r9V0/edit?usp=sharing[over here]. That allowed me to create the graph, which we will also setup in this Gist:
//setup
//hide
[source,cypher]
@rvanbruggen
rvanbruggen / Creating Dem Bones
Created May 28, 2014 22:42
Dem Bones Queries
//create them bones
create (_0:`LEG`:`BONES` {`condition`:"dry", `id`:1, `name`:"toe bone", `side`:"left", `subname`:1})
create (_1:`LEG`:`BONES` {`condition`:"dry", `id`:2, `name`:"toe bone", `side`:"left", `subname`:2})
create (_2:`LEG`:`BONES` {`condition`:"dry", `id`:3, `name`:"toe bone", `side`:"left", `subname`:3})
create (_3:`LEG`:`BONES` {`condition`:"dry", `id`:4, `name`:"toe bone", `side`:"left", `subname`:4})
create (_4:`LEG`:`BONES` {`condition`:"dry", `id`:5, `name`:"toe bone", `side`:"left", `subname`:5})
create (_5:`LEG`:`BONES` {`condition`:"dry", `id`:6, `name`:"toe bone", `side`:"right", `subname`:1})
create (_6:`LEG`:`BONES` {`condition`:"dry", `id`:7, `name`:"toe bone", `side`:"right", `subname`:2})
create (_7:`LEG`:`BONES` {`condition`:"dry", `id`:8, `name`:"toe bone", `side`:"right", `subname`:3})
create (_8:`LEG`:`BONES` {`condition`:"dry", `id`:9, `name`:"toe bone", `side`:"right", `subname`:4})
@rvanbruggen
rvanbruggen / Querying Dem Bones
Last active August 29, 2015 14:01
Querying Dem Bones
//DEM BONES queries
// show dem bones
match (n:BONES) return n;
//condition of the bones
match (n:BONES) return distinct n.condition
//where is the lord?
match (n {name:"Lord"}) return n
@rvanbruggen
rvanbruggen / dembonesgraphgist.adoc
Last active August 29, 2015 14:02
Dem Bones GraphGist
@rvanbruggen
rvanbruggen / Graph Local queries v2.cql
Last active August 29, 2015 14:02
Graph Local queries v2.cql
//the small dataset
create (_0:`OBSERVATION_LOCATION` {`latitude`:50.737160, `location_id`:1378, `longitude`:-3.405790, `name`:"EXETER AIRPORT", `postcode`:"EX5 2"}),
(_1:`OBSERVATION_LOCATION` {`latitude`:50.736540, `location_id`:57263, `longitude`:-3.532200, `name`:"EXETER UNIVERSITY", `postcode`:"EX4 4"}),
(_2:`OBSERVATION_LOCATION` {`latitude`:58.453700, `location_id`:32, `longitude`:-3.089960, `name`:"WICK AIRPORT", `postcode`:"KW1 4"}),
(_3:`OBSERVATION_LOCATION` {`latitude`:58.287710, `location_id`:44, `longitude`:-4.442370, `name`:"ALTNAHARRA NO 2", `postcode`:"IV27 4"}),
(_4:`OBSERVATION_LOCATION` {`latitude`:57.612760, `location_id`:66, `longitude`:-5.306250, `name`:"KINLOCHEWE", `postcode`:"IV22 2"}),
(_5:`OBSERVATION_LOCATION` {`latitude`:56.866560, `location_id`:105, `longitude`:-4.707970, `name`:"TULLOCH BRIDGE", `postcode`:"PH31 4"}),
(_6:`OBSERVATION_LOCATION` {`latitude`:57.711260, `location_id`:137, `longitude`:-3.323350, `name`:"LOSSIEMOUTH", `postcode`:"IV31 6"}),
(_7:`OBSERVATION_LOCATION
@rvanbruggen
rvanbruggen / loadingthetour.cql
Created July 6, 2014 20:15
The 2014 Tour de France
begin
create (_0:`Race` {`distance`:"3663.5", `edition`:"101", `from`:"05/07/2014", `id`:1, `name`:"TOUR DE FRANCE", `number_of_stages`:"21", `to`:"27/07/2014", `website`:"http://www.letour.com/le-tour/2014/us/"})
create (_1:`Team` {`country`:"GBR", `id`:1, `name`:"TEAM SKY", `sportingDirectors`:"PORTAL Nicolas, KNAVEN Servais"})
create (_2:`Rider` {`country`:"GBR", `name`:"FROOME Christopher"})
create (_3:`Rider` {`country`:"AUT", `name`:"EISEL Bernhard"})
create (_4:`Rider` {`country`:"BLR", `name`:"KIRYIENKA Vasili"})
create (_5:`Rider` {`country`:"ESP", `name`:"LOPEZ GARCIA David"})
create (_6:`Rider` {`country`:"ESP", `name`:"NIEVE ITURRALDE Mikel"})
create (_7:`Rider` {`country`:"USA", `name`:"PATE Danny"})
create (_8:`Rider` {`country`:"AUS", `name`:"PORTE Richie"})
@rvanbruggen
rvanbruggen / loadwargraph.cql
Last active August 29, 2015 14:04
Graph Of War
//loading countries
load csv with headers from "https://docs.google.com/a/neotechnology.com/spreadsheets/d/1yGt3jb-tnhPCCpNoyCgJV2ChcxoDToLal4GEQWkwvLI/export?format=csv&id=1yGt3jb-tnhPCCpNoyCgJV2ChcxoDToLal4GEQWkwvLI&gid=1243842209"
as countries
fieldterminator ','
merge (country:Country {short: countries.Short, cowcode: countries.COWcode, name: countries.Name});
//load some more detailed country data
load csv with headers from "https://docs.google.com/a/neotechnology.com/spreadsheets/d/1yGt3jb-tnhPCCpNoyCgJV2ChcxoDToLal4GEQWkwvLI/export?format=csv&id=1yGt3jb-tnhPCCpNoyCgJV2ChcxoDToLal4GEQWkwvLI&gid=10793080"
as countries
fieldterminator ','
//create song
create (n:Song {name:'Waterloo'});
// create songparts
match (s:Song {name:'Waterloo'}) create (s)-[:STARTS_WITH]->(sp:SongPart {name:'SongPart1', type:'Verse'});
match (sp1:SongPart {name:'SongPart1'}) create (sp1)-[:PRECEDES]->(sp2:SongPart {name:'SongPart2', type:'Chorus'});
match (sp2:SongPart {name:'SongPart2'}) create (sp2)-[:PRECEDES]->(sp3:SongPart {name:'SongPart3', type:'Verse'});
match (sp3:SongPart {name:'SongPart3'}) create (sp3)-[:PRECEDES]->(sp4:SongPart {name:'SongPart4', type:'Chorus'});
match (sp4:SongPart {name:'SongPart4'}) create (sp4)-[:PRECEDES]->(sp5:SongPart {name:'SongPart5', type:'Bridge'});
match (sp5:SongPart {name:'SongPart5'}) create (sp5)-[:PRECEDES]->(sp6:SongPart {name:'SongPart6', type:'Chorus'});
@rvanbruggen
rvanbruggen / Middle East GraphGist.adoc
Last active August 29, 2015 14:06
Middle East Gist

Friend-or-Foe Relations in the Middle East

Yesterday night, I stumbled upon yet another interesting graph:

middleeast

The "interactive" version of this image can be found on on this website: they basically try to interactively visualise the interplay of parties that are intervening in the many conflicts currently happening in the Middle East. It’s very, very well done.

So I did a little digging, and found that the original data is in a google doc that you can find over here. Look at the second tab: it’s basically an adjacency matrix of relationships between parties. In other words: a graph.