Developing with graph databases has a number of challenges, such as the modelling of complex schemas, and maintaining data consistency in your database.
In this talk, we discuss how TypeDB addresses these challenges, as well as how it compares to property graph databases. We’ll look at how to read and write data, how to model complex domains, and TypeDB’s ability to infer new data.
The main differences between TypeDB and graph databases can be summarised as:
1. TypeDB provides a concept-level schema with a type system that fully implements the Entity-Relationship (ER) model. Graph databases, on the other hand, use vertices and edges without integrity constraints imposed in the form of a schema
2. TypeDB contains a built-in inference engine - graph databases don’t provide native inferencing capabilities
3. TypeDB is an abstraction over a graph, and leverages a graph database under the hood to create a higher-level model, while graph databases work at different levels of abstraction
Tomás Sabat
Tomás is the Chief Operating Officer at Vaticle. He works closely with TypeDB's open source and enterprise users who use TypeDB to build applications in a wide number of industries including financial services, life sciences, cyber security and supply chain management. A graduate of the University of Cambridge, Tomás has spent the last seven years founding and building businesses in the technology industry.
36. MARRIED
Relationship Types
located
marriage
person person
city
spouse spouse
MARRIED
locating
location
Person
Marriage
City:
London
MARRIED
MARRIED
Person
LOCATED_IN
define
city sub entity,
plays located:location;
person sub entity,
plays marriage:spouse;
marriage sub relation,
relates spouse,
plays located:locating;
located sub relation,
relates locating,
relates location;
Nested Relations
40. Type Hierarchies
vehicle
sub
car aircraft truck
sedan coupe
minivan
fixed-wing
jet-aircraft
garbage-
truck
heavy-truck
sub
sub
sub
sub
sub
sub
sub
sub
sub
Type Hierarchies
41. Type Hierarchies
vehicle
sub
car aircraft truck
sedan coupe
minivan
fixed-wing
jet-aircraft
garbage-
truck
heavy-truck
sub
sub
sub
sub
sub
sub
sub
sub
sub
define
vehicle sub entity;
car sub vehicle;
sedan sub car;
coupe sub car;
minivan sub car;
aircraft sub vehicle;
fixed-wing sub aircraft;
jet-aircraft sub aircraft;
truck sub vehicle;
garbage-truck sub truck;
heavy-truck sub truck;
Type Hierarchies
43. Insert type hierarchies
CREATE (suzuki:Vehicle:Car:Minivan {name:"Maruti
Suzuki Ciaz"})
CREATE (audi:Vehicle:Car:Coupe {name:"Audi A5"})
CREATE (airplane:Vehicle:Airplane {name:"Concept
Airplane"})
CREATE (boeing:Vehicle:Airplane:Jet-aircraft
{name:"Boeing 747"})
CREATE (siku:Vehicle:Truck:Garbage-truck {name:"Siku
Super Bin"})
Type Hierarchies
44. Insert type hierarchies
CREATE (suzuki:Vehicle:Car:Minivan {name:"Maruti
Suzuki Ciaz"})
CREATE (audi:Vehicle:Car:Coupe {name:"Audi A5"})
CREATE (airplane:Vehicle:Airplane {name:"Concept
Airplane"})
CREATE (boeing:Vehicle:Airplane:Jet-aircraft
{name:"Boeing 747"})
CREATE (siku:Vehicle:Truck:Garbage-truck {name:"Siku
Super Bin"})
insert
$suzuki isa minivan, has name "Maruti Suzuki Ciaz";
$audi isa coupe, has name "Audi A5";
$aircraft isa aircraft, has name "Concept Airplane";
$boeing isa jet-aircraft, has name "Boeing 747";
$siku isa garbage-truck, has name "Siku Super Bin";
Type Hierarchies
48. MATCH(vehicle:Vehicle) RETURN match $vehicle isa vehicle;
But because your data isn’t validated, this Cypher query is unsafe!
Type Hierarchies
Fetch all vehicles
49. MATCH(vehicle:Vehicle) RETURN vehicle UNION MATCH
(vehicle:Car) RETURN vehicle UNION MATCH
(vehicle:Sedan) RETURN vehicle UNION MATCH
(vehicle:Coupe) RETURN vehicle UNION MATCH
(vehicle:Minivan) RETURN vehicle UNION MATCH
(vehicle:Aircraft) RETURN vehicle UNION MATCH
(vehicle:Fixed-wing) RETURN vehicle UNION MATCH
(vehicle:Jet-aircraft) RETURN vehicle UNION MATCH
(vehicle:Truck) RETURN vehicle UNION MATCH
(vehicle:Garbage-truck) RETURN vehicle UNION MATCH
(vehicle:Heavy-truck) RETURN vehicle
match $vehicle isa vehicle;
Type Hierarchies
Fetch all vehicles
50. MATCH(vehicle:Vehicle) RETURN vehicle UNION MATCH
(vehicle:Car) RETURN vehicle UNION MATCH
(vehicle:Sedan) RETURN vehicle UNION MATCH
(vehicle:Coupe) RETURN vehicle UNION MATCH
(vehicle:Minivan) RETURN vehicle UNION MATCH
(vehicle:Aircraft) RETURN vehicle UNION MATCH
(vehicle:Fixed-wing) RETURN vehicle UNION MATCH
(vehicle:Jet-aircraft) RETURN vehicle UNION MATCH
(vehicle:Truck) RETURN vehicle UNION MATCH
(vehicle:Garbage-truck) RETURN vehicle UNION MATCH
(vehicle:Heavy-truck) RETURN vehicle
match $vehicle isa vehicle;
In Cypher, we’d be forced to decide between a long query with
certainty or a short version without.
Type Hierarchies
Fetch all vehicles
58. Querying for Data
MATCH
(bob:Person {name:"Bob"})-[:KNOWS]->
(susan:Person {name:"Susan"})
-[:KNOWS]->(friend:Person),
(bob)-[:KNOWS]->(friend) RETURN friend
Who are Susan and Bob’s common friends?
59. Querying for Data
MATCH
(bob:Person {name:"Bob"})-[:KNOWS]->
(susan:Person {name:"Susan"})
-[:KNOWS]->(friend:Person),
(bob)-[:KNOWS]->(friend) RETURN friend
match
$bob isa person, has name "Bob";
$susan isa person, has name "Susan";
$friend isa person;
($bob, $susan) isa friendship;
($susan, $friend) isa friendship;
($friend, $bob) isa friendship;
get $friend;
Who are Susan and Bob’s common friends?
61. Querying for Data
MATCH
(cinema:Cinema {name:”Odeon London"}), (london:City
{name:"London"}), (pixar:Cinema {name:"Odeon"}),
(london) <-[:LOCATED_AT]-(cinema)
<-[:RELEASED_AT] -(movie:MOVIE) - [:PRODUCED_BY] ->
(pixar)
WHERE movie.year > 2002
RETURN movie.title
Which movies have been released after 2002 in the Odeon Cinema by Pixar?
62. Querying for Data
MATCH
(cinema:Cinema {name:”Odeon London"}), (london:City
{name:"London"}), (pixar:Cinema {name:"Odeon"}),
(london) <-[:LOCATED_AT]-(cinema)
<-[:RELEASED_AT] -(movie:MOVIE) - [:PRODUCED_BY] ->
(pixar)
WHERE movie.year > 2002
RETURN movie.title
match
$cinema isa cinema, has name "Odeon London";
$london isa city, has name "London";
$studio isa studio, has name "Pixar";
$movie isa movie, has title $title;
($london, $cinema) isa locating;
($cinema, $movie) isa release;
($movie, $studio) isa production, has year $year;
$year > 2002;
get $title;
Which movies have been released after 2002 in the Odeon Cinema by Pixar?
69. Rule Based Inferencing
rule siblingship:
when {
(parent: $parent, child: $child1) isa parenthood;
(parent: $parent, child: $child2) isa parenthood;
} then {
(sibling: $child1, sibling: $child2) isa
siblingship;
};
$child
parenthood
$parent
siblingship
$child
p
a
r
e
n
t
h
o
o
d
Return all siblings
74. Rule Based Inferencing
rule cousin-inferred:
when {
$parent isa person;
$child isa person;
$nephew isa person;
(parent: $parent, child: $child) isa parenthood;
(aunt: $parent, nephew: $nephew) isa auntship;
} then {
(cousin: $child, cousin: $nephew) isa cousinship;
};
$nephew
auntship
$parent
cousinship
$child
p
a
r
e
n
t
s
h
i
p
Return all cousins
75. Rule Based Inferencing
Chaining Rules
rule cousin-inferred:
when {
$parent isa person;
$child isa person;
$nephew isa person;
(parent: $parent, child: $child) isa parenthood;
(aunt: $parent, nephew: $nephew) isa auntship;
} then {
(cousin: $child, cousin: $nephew) isa cousinship;
};
$nephew
auntship
$parent
cousinship
$child
p
a
r
e
n
t
s
h
i
p
But what if one of these relations is inferred?
77. Rule Based Inferencing
rule aunt-niece:
when {
$child isa person, has gender ”female";
$parent isa person;
$sibling isa person;
(parent: $parent, child: $child) isa parenthood;
(sibling: $parent, sibling: $sibling) isa siblingship;
} then {
(niece: $child, aunt: $sibling) isa auntship;
};
$sibling
siblingship
$parent
auntship
$child
p
a
r
e
n
t
h
o
o
d
Chaining Rules
78. But we can also represent more complex business logic in rules
82. Rule Based Inferencing
Overlapping Schedules
rule overlapping-schedule:
when {
$schedule1 isa schedule, has end $1End;
$schedule2 isa schedule, has start $2Start, has end
$2End;
$1End > $2Start;
$1End <= $2End;
} then {
($schedule1, $schedule2) isa overlaps;
};
89. 𝑖𝑓 𝑎 𝑝𝑒𝑟𝑠𝑜𝑛 𝑐𝑜𝑛𝑠𝑢𝑚𝑒𝑠 𝑚𝑜𝑟𝑒 𝑡ℎ𝑎𝑛 10 𝑢𝑛𝑖𝑡𝑠 𝑜𝑓 𝑎𝑙𝑐𝑜ℎ𝑜𝑙 𝑝𝑒𝑟 𝑤𝑒𝑒𝑘
𝑡ℎ𝑒𝑛 𝑡ℎ𝑒𝑦 𝑎𝑟𝑒 𝑎𝑡 𝑟𝑖𝑠𝑘 𝑜𝑓 𝐷𝑖𝑎𝑏𝑒𝑡𝑒𝑠 𝑇𝑦𝑝𝑒 𝐼𝐼 𝑎𝑛𝑑 𝐻𝑦𝑝𝑜𝑙𝑔𝑦𝑐𝑒𝑚𝑖𝑎
rule alcohol-risk-of-diabetes:
rule alcohol-risk-of-diabetes:
when {
$p isa person;
$c (consumer: $p, consumed-substance: $s) isa consumption, has units-per-week $u;
$u >= 10;
$s isa substance, has name "Alcohol";
$d isa disease, has name "Diabetes Type II";
$d2 isa disease, has name "Hypoglycemia";
} then {
(person-at-risk: $p, risked-disease: $d, risked-disease: $d2) isa alcohol-risk-factor;
};
90. 𝑖𝑓 𝑎 𝑝𝑒𝑟𝑠𝑜𝑛 𝑐𝑜𝑛𝑠𝑢𝑚𝑒𝑠 𝑚𝑜𝑟𝑒 𝑡ℎ𝑎𝑛 12 𝑐𝑖𝑔𝑎𝑟𝑒𝑡𝑡𝑒𝑠 𝑝𝑒𝑟 𝑤𝑒𝑒𝑘
𝑡ℎ𝑒𝑛 𝑡ℎ𝑒𝑦 𝑎𝑟𝑒 𝑎𝑡 𝑟𝑖𝑠𝑘 𝑜𝑓 𝑚𝑢𝑙𝑡𝑖𝑝𝑙𝑒 𝑠𝑐𝑙𝑒𝑟𝑜𝑠𝑖𝑠, ℎ𝑖𝑔ℎ 𝑏𝑙𝑜𝑜𝑑 𝑝𝑟𝑒𝑠𝑠𝑢𝑟𝑒, 𝐶𝑂𝑃𝐷, ℎ𝑖𝑔ℎ 𝑐ℎ𝑜𝑙𝑒𝑠𝑡𝑒𝑟𝑜𝑙 𝑎𝑛𝑑 ℎ𝑒𝑎𝑟𝑡 𝑑𝑖𝑠𝑒𝑎𝑠𝑒
rule smoking-risk-of-multiple-sclerosis:
when {
$p isa person;
$c (consumer: $p, consumed-substance: $s) isa consumption, has units-per-week $u;
$u >= 12;
$s isa substance, has name "Cigarettes";
$d isa disease, has name "Multiple Sclerosis";
$d2 isa disease, has name "Lung Cancer";
$d3 isa disease, has name "High Blood Pressure";
$d4 isa disease, has name "Multiple Sclerosis";
$d5 isa disease, has name "Chronic Obstructive Pulmonary Disease";
$d6 isa disease, has name "Heart Disease";
} then {
(person-at-risk: $p, risked-disease: $d, risked-disease: $d2, risked-disease: $d3,
risked-disease: $d4, risked-disease: $d5, risked-disease: $d6) isa smoking-risk-factor;
};
91. 𝑖𝑓 𝑠𝑜𝑚𝑒𝑜𝑛𝑒!𝑠 𝑝𝑎𝑟𝑒𝑛𝑡 ℎ𝑎𝑠 𝑏𝑒𝑒𝑛 𝑑𝑖𝑎𝑔𝑛𝑜𝑠𝑒𝑑 𝑤𝑖𝑡ℎ 𝐷𝑖𝑎𝑏𝑒𝑡𝑒𝑠 𝐼𝐼 𝑎𝑛𝑑/𝑜𝑟 𝐴𝑟𝑡ℎ𝑟𝑖𝑡𝑖𝑠
𝑡ℎ𝑒𝑛 𝑡ℎ𝑒𝑦 𝑎𝑟𝑒 𝑎𝑡 𝑟𝑖𝑠𝑘 𝑜𝑓 𝑡ℎ𝑜𝑠𝑒 𝑑𝑖𝑠𝑒𝑎𝑠𝑒𝑠 𝑡𝑜𝑜
rule hereditary-risk-of-diabetes:
rule hereditary-risk-of-diabetes:
when {
$child isa person;
$parent isa person;
$cause (parent: $parent, child: $child) isa parentship;
$diagnosis (patient: $parent, diagnosed-disease: $diabetes) isa diagnosis;
$diagnosis2 (patient: $parent, diagnosed-disease: $arthritis) isa diagnosis;
$diabetes isa disease, has name "Diabetes Type II";
$arthritis isa disease, has name "Arthritis";
} then {
(person-at-risk: $child, risked-disease: $diabetes, risked-disease: $arthritis) isa hereditary-risk-factor;
};