@card annotation
The @card annotation is used
to specify cardinality ranges for roles and ownerships.
Syntax
<type label> <interface> <type label 2> @card(N..M);
<type label> <interface> <type label 2> @card(N..);
<type label> <interface> <type label 2> @card(N);
The @card annotation can be defined for any <interface> of: owns, relates, or plays.
It accepts either a range argument or a single scalar argument. Arguments must be integers.
-
Both bounds are inclusive.
-
A partially unbounded range
N..has an infinite upper bound. -
The minimal lower bound is
0and must be explicitly specified (e.g., use0..Minstead of..M). -
A single scalar argument
Nis shorthand forN..Nand means "exactly N".
Usage
Every interface has a default cardinality constraint, accessible in Default interface cardinalities.
When defined, the @card annotation overrides the default cardinality with the specified arguments.
The cardinality constraint regulates the number of data instances that can appear in a specific context (like owning an attribute).
For example, the following definition specifies that an instance of the page type must have at least one and no more than three `name`s:
#!test[schema]
#{{
define
attribute name value string;
#}}
#!test[schema]
define
entity page owns name @card(1..3);
At the same time, the page type can play the posting:page role an unlimited number of times:
#!test[schema]
#{{
define relation posting relates page;
#}}
#!test[schema, rollback]
define
page plays posting:page @card(0..);
However, an instance of the posting relation type can have up to a thousand page instances:
#!test[schema]
define
relation posting relates page @card(0..1000);
Subtyping
Similarly to inherited type constraints, interface constraints also affect subtypes from both sides. For example, the following query defines that:
-
A
pageinstance must have between 1 and 3 names. -
A
profileinstance (subtype ofpage) inherits the same constraint and must have between 1 and 3 names. -
A
userinstance can own exactly 1 first name and up to 2 surnames (first-nameandsurnameare subtypes ofname).
#!test[schema]
define
entity page owns name @card(1..3);
entity profile sub page;
entity user sub profile,
owns first-name @card(1),
owns surname @card(0..);
attribute name @abstract, value string;
attribute first-name sub name;
attribute surname sub name;
Valid statements based on this schema include:
-
It is impossible to insert a
page,profile, oruserwithout a name. -
It is impossible to insert a
pageorprofilebecause they do not own non-abstract names. -
A
userinstance can be inserted with just a first name. -
A
userinstance cannot have more than two surnames.
Limitations
Cannot be used with the @key annotation as they both produce cardinality constraints.