如何在Grakn知识图谱中表示知识库随时间的变化?
Great question! Tracking how facts evolve over time in Grakn (now rebranded as TypeDB) is a common use case, and there are two straightforward, idiomatic approaches to handle your scenario. Let's walk through them with your example:
1. Model Time Intervals Directly in Your Schema
The most explicit way is to add temporal attributes to the relationships that change over time. For your teacher-class association:
Step 1: Define the Schema
First, set up your entities, relations, and attributes to support time tracking:
define person sub entity, has name; class sub entity, has student-count; teaching sub relation, relates teacher, relates taught-class, has start-date, has end-date; name sub attribute, value string; student-count sub attribute, value long; start-date sub attribute, value datetime; end-date sub attribute, value datetime;
Step 2: Insert Version 1 (Jan 2016)
Record Rachel's tenure with a start date and an end date marking when she was replaced:
insert $rachel isa person, has name "Rachel"; $math-class isa class, has student-count 10; $rachel-teaches isa teaching, relates teacher $rachel, relates taught-class $math-class, has start-date 2016-01-01T00:00:00, has end-date 2016-03-01T00:00:00;
Step 3: Insert Version 2 (Mar 2016)
Add Alice's teaching relationship with her start date (leave end-date unset if she's currently the active teacher):
insert $alice isa person, has name "Alice"; $alice-teaches isa teaching, relates teacher $alice, relates taught-class $math-class, has start-date 2016-03-01T00:00:00;
Querying by Time
To get the teacher for the class in Feb 2016:
match $class isa class, has student-count 10; $teaching isa teaching, relates taught-class $class, has start-date $start, has end-date $end; $teacher isa person, has name $name, relates teacher $teaching; $start <= 2016-02-01T00:00:00; $end >= 2016-02-01T00:00:00; get $name;
2. Leverage Grakn's Built-in History Tracking
Grakn automatically logs all changes to your data, so you can query the state of the graph at a specific point in time without explicitly adding time attributes.
Step 1: Insert Initial State
First, insert the Jan 2016 state without explicit time attributes:
insert $rachel isa person, has name "Rachel"; $math-class isa class, has student-count 10; (teacher: $rachel, taught-class: $math-class) isa teaching;
Step 2: Apply the Change (Mar 2016)
When you need to update the teacher, delete the old relationship and insert the new one:
match $rachel isa person, has name "Rachel"; $math-class isa class, has student-count 10; $old-teaching (teacher: $rachel, taught-class: $math-class) isa teaching; delete $old-teaching; insert $alice isa person, has name "Alice"; (teacher: $alice, taught-class: $math-class) isa teaching;
Query Historical State
To retrieve the teacher as of Feb 2016, use Grakn's temporal query capabilities (note: this requires enabling history in your database configuration):
match @as-of 2016-02-01T00:00:00 $class isa class, has student-count 10; $teacher isa person, has name $name; (teacher: $teacher, taught-class: $class) isa teaching; get $name;
Which Approach Should You Use?
- Use time intervals if you need to explicitly model tenure periods, or if you want to query overlapping time ranges easily.
- Use built-in history if you want a simpler schema and just need to audit past states without manual time tracking.
内容的提问来源于stack exchange,提问作者Gregorio Momm

