Update Instances of Concepts
In a Grakn knowledge graph, each instance is a “fact”. Relations and entities are extensible and reducible, by adding or removing role players to them over time. Each attribute value can be owned and the ownership can be removed or added: updating an attribute value directly therefore means changing the attribute value for ALL owners of this attribute value! Achieving this requires deleting the attribute value and connecting previous owners to a different value instance. Updating the attribute value that is owned by a concept means changing the connection to the value, rather than the value itself. We can achieve this in a single query using an Update query, which consists of a match, a delete, and an insert.
To try the following examples with one of the Grakn clients, follow these Clients Guide.
Update attribute owned by a concept
Usually, we want to change the value of an attribute that is associated to another instance. To do so, we first need to delete the association that the instance has with the attribute of interest and then insert the new instance of the attribute. We can do this in a single update query as follows:
This query first deletes the association that the
organisation with id
V17391 has with the instance of the
registration-number attribute type and then continues to insert the new instance of the
registration-number to be owned by the same instance of
Update all instances of a given attribute
There may also be cases where we need to update the value of all instances of an attribute type. This amounts to rewriting the value of an attribute fact. To do so, we can again make use of an update query.
This query first looks for any instance of type
media that owns the
caption attribute containing an
"inappropriate word", then for each
attribute pair it finds, deletes the caption and inserts a caption reading
deleted in its place.
Modifying a relation’s role player
To replace a role player, we combine the steps for extending the relation, with steps for deleting a role player:
After these queries, all employments by the organisation named
Pharos were replaced employments by the organisation named
Updating data in Graql is usually involves an update query, which takes the form of
match-delete-insert. You can use these queries to
add and remove role players from relations, or add and remove ownerships of attributes. Attribute values themselves can be treated
as immutable, and changing their values amounts to deleting the value and moving all the ownerships of the old value to some
Next, we learn how to aggregate values over a set of data in a Grakn knowledge graph.