How to correctly override a playing role?

Hi all,

I’m trying to understand how to correctly override a “plays role”. In the documentation, it says:

By default, a subtype of a relation type inherits all roles that were defined for its supertype. To override an inherited role, use the as keyword. The overridden new role must be a subtype of the inherited role.

What I do not understand is the part about " The overridden new role must be a subtype of the inherited role." I did not come across any subtyping of roles as roles are not standalone defined concepts. So how can one sub-type a role and use it as the overriding role? It would be great if someone could provide me with a testable example :pray:

Many thanks! :slight_smile:

Hi @c-lare ,

base-relation sub relation, relates base-role;
sub-relation sub base-relation, relates sub-role as base-role;

Here, sub-role is a subtype of base-role. Overriding a role means the inherited role is ‘hidden’ i.e., you can no longer insert (base-relation: $x) isa sub-relation

base-entity sub entity, plays base-relation:base-role;
sub-entity  sub base-entity,  plays sub-role as base-role;

This would override the role. I believe the override removes the ability for sub-entity to play base-entity:base-role

1 Like

Many thanks! That makes it clear to me :slight_smile:

One follow-up question (I adapted your code a bit to match the TypeDB documentation for plays roles

If I use this schema definition:

base-relation sub relation, relates base-role;
sub-relation sub base-relation, relates sub-role as base-role;

base-entity sub entity, plays base-relation:base-role;
sub-entity  sub base-entity, plays sub-relation:sub-role as base-role;

sub-entity basically plays only one role “sub-role” and not “base-role” anymore? And when I use it without overriding, like so:

base-relation sub relation, relates base-role;
sub-relation sub base-relation, relates sub-role as base-role;

base-entity sub entity, plays base-relation:base-role;
sub-entity sub base-entity, plays sub-relation:sub-role;

it basically can play base-role and sub-role? If that is the case, I think I understand it then.

Just a second quick follow-up: If you have a practical example where one would not want to override (so allow it to play both roles), that would be great!

And thanks for you quick reply!

And when I use it without overriding, like so (…) it basically can play base-role and sub-role?

When you’re inserting something, this will hold:

(base-role: $e) isa base-relation; # yes
(sub-role: $e) isa sub-relation; # yes
(base-role: $e) isa sub-relation; # no

For the third case, when you’re matching, type-inference will automatically resolve the types to match those playing sub-role

1 Like

Hi, if you’d like more information on role subtyping, you might find it useful to refer to Lessons 9.1 and 9.6 of the learning course.

1 Like

Many thanks! Just to get it right. Can sub-entity play base-role in base-relation if it was not overridden e.g. with “plays sub-relation:sub-role as base-role;” and I only have “plays sub-relation:sub-role;”? I was just assuming that “plays sub-relation:sub-role as base-role;” is still inherited and we add a new playing role “plays sub-relation:sub-role;”

Or with other words, when using:

base-entity sub entity, plays base-relation:base-role;
sub-entity sub base-entity, plays sub-relation:sub-role;

sub-entity can play base role in base relation and sub role in sub relation?

Many thanks again! :slight_smile:

Yes. sub-entity should inherit the ability to play base-relation:base-role.

When you use the as keyword to override. plays sub-role as base-role it stops the ability to play base-role from being inherited.

1 Like

Great, many thanks, that are some really nice courses!

1 Like