Defining Grakn Schema using Graql

The page documents use of the Graql define and undefine queries, which will define a specified variable pattern describing a schema. To follow along, or experiment further, with the examples given below, please load the basic-genealogy.gql file, which can be found in the examples directory of the Grakn installation zip, or on Github.

Define

Define queries are used to define your schema. Any variable patterns within them are added to the schema:

 
define
person sub entity, has name;
name sub attribute, datatype string;

 
qb.define(
    label("person").sub("entity").has("name"),
    label("name").sub("attribute").datatype(STRING)
).execute();

This example defines an entity person and an attribute name. name is given the datatype string and a person can have a name.

Undefine

Undefine queries are used to undefine your schema. Any variable patterns within them are removed from your schema:

 
undefine person has name;

 
qb.undefine(label("person").has("name")).execute();

This example will stop instances of a person from having a name. person and name will both still be in the schema.

 

undefine person sub entity;

 

qb.undefine(label("person").sub("entity")).execute();

This example will remove person from the schema entirely.

Properties

id

It is not possible to define a concept with the given id, as this is the job of the system. However, if you attempt to define by id, you will retrieve a concept if one with that id already exists. The created or retrieved concept can then be modified with further properties.

 

define id "1376496" plays parent;

 

qb.define(var().id(ConceptId.of("1376496")).plays("parent")).execute();

sub

Set up a hierarchy.

 
define
man sub person;
woman sub person;

 
qb.define(label("man").sub("person")).execute();
qb.define(label("woman").sub("person")).execute();

relates

Add a role to a relationship.

 
define siblings sub relationship, relates sibling, relates sibling;

 
qb.define(
  label("siblings").sub("relationship")
    .relates("sibling").relates("sibling")
).execute();

as

Equivalent to sub, but only used in relates for defining the hierarchy of roles.

 
define fatherhood sub parentship, relates father as parent, relates son as child;

 
qb.define(
  label("fatherhood").sub("parentship")
    .relates(label("father"), label("parent"))
    .relates(label("son"), label("child"))
).execute();

plays

Allow the concept type to play the given role.

 
define person plays sibling;

 
qb.define(label("person").plays("sibling")).execute();
qb.define(label("person").plays("sibling")).execute();

has

Allow the concept type to have the given attribute.

This is done by creating a specific relationship relating the concept and attribute.

 
define person has nickname;

 
qb.define(label("person").has("nickname")).execute();

Tags: graql