@card
annotation
The @card
annotation is used
to specify cardinality ranges for roles and ownerships.
Syntax
<type label> <trait> <type label 2> @card(N..M);
<type label> <trait> <type label 2> @card(N..);
<type label> <trait> <type label 2> @card(N);
The @card
annotation can be defined for any <trait>
of: owns
, relates
, or plays
.
It accepts either a range argument or a single scalar argument. Arguments must be integers.
-
A partially unbounded range
N..
has an infinite upper bound. -
The minimal lower bound is
0
and must be explicitly specified (e.g., use0..M
instead of..M
). -
A single scalar argument
N
is shorthand forN..N
and means "exactly N".
Usage
Every trait has a default cardinality
constraint, accessible in Default trait cardinalities.
When defined, the @card
annotation overrides the default cardinality
with the specified arguments.
The cardinality
constraint regulates the number of traits a type instance can have.
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:
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:
define
page plays posting:page @card(0..);
However, an instance of the posting
relation type can have up to a thousand page
instances:
define
relation posting relates page @card(0..1000);
Subtyping
Similarly to inherited type constraints, trait constraints also affect subtypes from both sides. For example, the following query defines that:
-
A
page
instance must have between 1 and 3 names. -
A
profile
instance (subtype ofpage
) inherits the same constraint and must have between 1 and 3 names. -
A
user
instance can own exactly 1 first name and up to 2 surnames (first-name
andsurname
are subtypes ofname
).
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
, oruser
without a name. -
It is impossible to insert a
page
orprofile
because they do not own non-abstract names. -
A
user
instance can be inserted with just a first name. -
A
user
instance cannot have more than two surnames.
Limitations
Cannot be used with the @key
annotation as they both produce cardinality
constraints.