N-ary relations
Some connections are irreducibly between three or more classes at once. A medical treatment ties a patient, a physician, and a medication together; splitting it into separate binary links would lose the fact that the three occur as a single unit. That is what an n-ary relation captures.
Reach for an n-ary relation only when the association genuinely holds among
several classes jointly — no single pair carries the meaning on its own. Very
often what looks n-ary is better modeled by reifying it into a «relator»
(a Treatment class) mediated to each participant with binary
«mediation» relations, which lets each link carry its own cardinality. Prefer
the relator pattern when in doubt; use a true n-ary relation when the reified
entity would add nothing.
Building an n-ary relation
Use naryRelationBuilder() and pass all participants to members(...). At
least three are required — fewer throws at build(). Only two stereotypes
apply to n-ary relations: .material() and .comparative().
import { class ProjectAn OntoUML project: the top-level container of an ontology and the entry
point of this library. A project holds every element of a model — classes,
relations, generalizations, packages, properties, notes — as well as the
diagrams and views that depict them, and provides typed access to these
elements by id or by kind.
New elements are created through the fluent builders exposed by the
project (e.g.,
classBuilder
,
binaryRelationBuilder
), which
register the created elements in the project.
A project is also described by the metadata fields of the OntoUML/UFO
Catalog Metadata Vocabulary, such as publisher, license, keywords, themes,
and designed-for tasks.Project } from 'ontouml-js';
const const project: Projectproject = new new Project(): ProjectAn OntoUML project: the top-level container of an ontology and the entry
point of this library. A project holds every element of a model — classes,
relations, generalizations, packages, properties, notes — as well as the
diagrams and views that depict them, and provides typed access to these
elements by id or by kind.
New elements are created through the fluent builders exposed by the
project (e.g.,
classBuilder
,
binaryRelationBuilder
), which
register the created elements in the project.
A project is also described by the metadata fields of the OntoUML/UFO
Catalog Metadata Vocabulary, such as publisher, license, keywords, themes,
and designed-for tasks.Project();
const const model: Packagemodel = const project: Projectproject.Project.packageBuilder(): PackageBuilderCreates a builder for a new
Package
in this project.packageBuilder().PackageBuilder.root(): PackageBuilderSets the package as the root package of its project, i.e., the package
that contains the entire model.root().NamedElementBuilder<PackageBuilder>.name(value: string, language?: string): PackageBuilderAdds a name to the element in the given language.name('Clinic').PackageBuilder.build(): PackageBuilds an instance of
Package
with the parameters passed to the
builder. When no methods are evoked, the created package has the
following defaults:
- `id: "randomly-generated-id",`
- `created: new Date(),`
- `contents: []`build();
const const patient: Classpatient = const model: Packagemodel.Package.classBuilder(): ClassBuilderCreates a builder for a
Class
contained in this package.classBuilder().ClassBuilder.role(): ClassBuilderSets the stereotype of the class to «role», which decorates anti-rigid
classes whose instances are contingently classified by virtue of a
relational condition (e.g., `Student`, `Employee`).role().NamedElementBuilder<ClassBuilder>.name(value: string, language?: string): ClassBuilderAdds a name to the element in the given language.name('Patient').ClassBuilder.build(): ClassBuilds an instance of
Class
with the parameters passed to the
builder. **WARNING:** the ordering in which methods are evoked may
affect the resulting object. When no methods are evoked, the created
class has the following defaults:
- `id: "randomly-generated-id",`
- `created: new Date(),`
- `isAbstract: false,`
- `isDerived: false,`
- `order: 1,`
- `isPowertype: false,`build();
const const physician: Classphysician = const model: Packagemodel.Package.classBuilder(): ClassBuilderCreates a builder for a
Class
contained in this package.classBuilder().ClassBuilder.role(): ClassBuilderSets the stereotype of the class to «role», which decorates anti-rigid
classes whose instances are contingently classified by virtue of a
relational condition (e.g., `Student`, `Employee`).role().NamedElementBuilder<ClassBuilder>.name(value: string, language?: string): ClassBuilderAdds a name to the element in the given language.name('Physician').ClassBuilder.build(): ClassBuilds an instance of
Class
with the parameters passed to the
builder. **WARNING:** the ordering in which methods are evoked may
affect the resulting object. When no methods are evoked, the created
class has the following defaults:
- `id: "randomly-generated-id",`
- `created: new Date(),`
- `isAbstract: false,`
- `isDerived: false,`
- `order: 1,`
- `isPowertype: false,`build();
const const medication: Classmedication = const model: Packagemodel.Package.classBuilder(): ClassBuilderCreates a builder for a
Class
contained in this package.classBuilder().ClassBuilder.kind(): ClassBuilderSets the stereotype of the class to «kind», which decorates rigid
classes that provide an identity principle to their instances, which are
functional complexes (e.g., `Person`, `Car`). Also applies the following
defaults:
- `restrictedTo = [ "functional-complex" ]`
- `order = 1`kind().NamedElementBuilder<ClassBuilder>.name(value: string, language?: string): ClassBuilderAdds a name to the element in the given language.name('Medication').ClassBuilder.build(): ClassBuilds an instance of
Class
with the parameters passed to the
builder. **WARNING:** the ordering in which methods are evoked may
affect the resulting object. When no methods are evoked, the created
class has the following defaults:
- `id: "randomly-generated-id",`
- `created: new Date(),`
- `isAbstract: false,`
- `isDerived: false,`
- `order: 1,`
- `isPowertype: false,`build();
const const treatment: NaryRelationtreatment = const model: Packagemodel
.Package.naryRelationBuilder(): NaryRelationBuilderCreates a builder for a
NaryRelation
contained in this package.naryRelationBuilder()
.NaryRelationBuilder.material(): NaryRelationBuilderSets the stereotype of the relation to «material», which decorates
relations between endurants that are founded on relators, such as being
treated in. Also applies the following defaults:
- `isDerived = true`material()
.NamedElementBuilder<NaryRelationBuilder>.name(value: string, language?: string): NaryRelationBuilderAdds a name to the element in the given language.name('treated with')
.NaryRelationBuilder.members(...members: Classifier<any, any>[]): NaryRelationBuilderSets the
Classifier
instances connected by the ends of the
relation, replacing any previously set members. At least three members
are required to build the relation.members(const patient: Classpatient, const physician: Classphysician, const medication: Classmedication)
.NaryRelationBuilder.build(): NaryRelationBuilds an instance of
NaryRelation
with the parameters passed to
the builder. At least three members are required before `build()` is
evoked. **WARNING:** the ordering in which methods are evoked may affect
the resulting object. When no methods are evoked, the created relation
has the following defaults:
- `id: "randomly-generated-id",`
- `created: new Date(),`
- `isAbstract: false,`
- `isDerived: false,`build();
var console: Consoleconsole.Console.log(...data: any[]): voidThe **`console.log()`** static method outputs a message to the console.
[MDN Reference](https://developer.mozilla.org/docs/Web/API/console/log_static)log(const treatment: NaryRelationtreatment.Classifier<Relation, RelationStereotype>.properties: Property[]The properties owned by this classifier: attributes in the case of
classes, relation ends in the case of relations.properties.Array<Property>.length: numberGets or sets the length of the array. This is a number one higher than the highest index in the array.length); // 3
Ends
An n-ary relation has one end per member, in the order you passed them, and
each end is a Property — so, as with binary relations, cardinality and the
other end meta-properties live there. Reach the ends through properties.
import { class ProjectAn OntoUML project: the top-level container of an ontology and the entry
point of this library. A project holds every element of a model — classes,
relations, generalizations, packages, properties, notes — as well as the
diagrams and views that depict them, and provides typed access to these
elements by id or by kind.
New elements are created through the fluent builders exposed by the
project (e.g.,
classBuilder
,
binaryRelationBuilder
), which
register the created elements in the project.
A project is also described by the metadata fields of the OntoUML/UFO
Catalog Metadata Vocabulary, such as publisher, license, keywords, themes,
and designed-for tasks.Project } from 'ontouml-js';
const const project: Projectproject = new new Project(): ProjectAn OntoUML project: the top-level container of an ontology and the entry
point of this library. A project holds every element of a model — classes,
relations, generalizations, packages, properties, notes — as well as the
diagrams and views that depict them, and provides typed access to these
elements by id or by kind.
New elements are created through the fluent builders exposed by the
project (e.g.,
classBuilder
,
binaryRelationBuilder
), which
register the created elements in the project.
A project is also described by the metadata fields of the OntoUML/UFO
Catalog Metadata Vocabulary, such as publisher, license, keywords, themes,
and designed-for tasks.Project();
const const model: Packagemodel = const project: Projectproject.Project.packageBuilder(): PackageBuilderCreates a builder for a new
Package
in this project.packageBuilder().PackageBuilder.root(): PackageBuilderSets the package as the root package of its project, i.e., the package
that contains the entire model.root().NamedElementBuilder<PackageBuilder>.name(value: string, language?: string): PackageBuilderAdds a name to the element in the given language.name('Clinic').PackageBuilder.build(): PackageBuilds an instance of
Package
with the parameters passed to the
builder. When no methods are evoked, the created package has the
following defaults:
- `id: "randomly-generated-id",`
- `created: new Date(),`
- `contents: []`build();
const const patient: Classpatient = const model: Packagemodel.Package.classBuilder(): ClassBuilderCreates a builder for a
Class
contained in this package.classBuilder().ClassBuilder.role(): ClassBuilderSets the stereotype of the class to «role», which decorates anti-rigid
classes whose instances are contingently classified by virtue of a
relational condition (e.g., `Student`, `Employee`).role().NamedElementBuilder<ClassBuilder>.name(value: string, language?: string): ClassBuilderAdds a name to the element in the given language.name('Patient').ClassBuilder.build(): ClassBuilds an instance of
Class
with the parameters passed to the
builder. **WARNING:** the ordering in which methods are evoked may
affect the resulting object. When no methods are evoked, the created
class has the following defaults:
- `id: "randomly-generated-id",`
- `created: new Date(),`
- `isAbstract: false,`
- `isDerived: false,`
- `order: 1,`
- `isPowertype: false,`build();
const const physician: Classphysician = const model: Packagemodel.Package.classBuilder(): ClassBuilderCreates a builder for a
Class
contained in this package.classBuilder().ClassBuilder.role(): ClassBuilderSets the stereotype of the class to «role», which decorates anti-rigid
classes whose instances are contingently classified by virtue of a
relational condition (e.g., `Student`, `Employee`).role().NamedElementBuilder<ClassBuilder>.name(value: string, language?: string): ClassBuilderAdds a name to the element in the given language.name('Physician').ClassBuilder.build(): ClassBuilds an instance of
Class
with the parameters passed to the
builder. **WARNING:** the ordering in which methods are evoked may
affect the resulting object. When no methods are evoked, the created
class has the following defaults:
- `id: "randomly-generated-id",`
- `created: new Date(),`
- `isAbstract: false,`
- `isDerived: false,`
- `order: 1,`
- `isPowertype: false,`build();
const const medication: Classmedication = const model: Packagemodel.Package.classBuilder(): ClassBuilderCreates a builder for a
Class
contained in this package.classBuilder().ClassBuilder.kind(): ClassBuilderSets the stereotype of the class to «kind», which decorates rigid
classes that provide an identity principle to their instances, which are
functional complexes (e.g., `Person`, `Car`). Also applies the following
defaults:
- `restrictedTo = [ "functional-complex" ]`
- `order = 1`kind().NamedElementBuilder<ClassBuilder>.name(value: string, language?: string): ClassBuilderAdds a name to the element in the given language.name('Medication').ClassBuilder.build(): ClassBuilds an instance of
Class
with the parameters passed to the
builder. **WARNING:** the ordering in which methods are evoked may
affect the resulting object. When no methods are evoked, the created
class has the following defaults:
- `id: "randomly-generated-id",`
- `created: new Date(),`
- `isAbstract: false,`
- `isDerived: false,`
- `order: 1,`
- `isPowertype: false,`build();
const const treatment: NaryRelationtreatment = const model: Packagemodel
.Package.naryRelationBuilder(): NaryRelationBuilderCreates a builder for a
NaryRelation
contained in this package.naryRelationBuilder()
.NaryRelationBuilder.material(): NaryRelationBuilderSets the stereotype of the relation to «material», which decorates
relations between endurants that are founded on relators, such as being
treated in. Also applies the following defaults:
- `isDerived = true`material()
.NamedElementBuilder<NaryRelationBuilder>.name(value: string, language?: string): NaryRelationBuilderAdds a name to the element in the given language.name('treated with')
.NaryRelationBuilder.members(...members: Classifier<any, any>[]): NaryRelationBuilderSets the
Classifier
instances connected by the ends of the
relation, replacing any previously set members. At least three members
are required to build the relation.members(const patient: Classpatient, const physician: Classphysician, const medication: Classmedication)
.NaryRelationBuilder.build(): NaryRelationBuilds an instance of
NaryRelation
with the parameters passed to
the builder. At least three members are required before `build()` is
evoked. **WARNING:** the ordering in which methods are evoked may affect
the resulting object. When no methods are evoked, the created relation
has the following defaults:
- `id: "randomly-generated-id",`
- `created: new Date(),`
- `isAbstract: false,`
- `isDerived: false,`build();
const [const patientEnd: PropertypatientEnd, const physicianEnd: PropertyphysicianEnd, const medicationEnd: PropertymedicationEnd] = const treatment: NaryRelationtreatment.Classifier<Relation, RelationStereotype>.properties: Property[]The properties owned by this classifier: attributes in the case of
classes, relation ends in the case of relations.properties;
// Any number of patients, at least one physician, one medication.
const patientEnd: PropertypatientEnd.Property.cardinality: CardinalityThe multiplicity of this property. Defaults to `0..*`.cardinality.Cardinality.setAsZeroToMany(): voidSets the cardinality to `0..*`.setAsZeroToMany();
const physicianEnd: PropertyphysicianEnd.Property.cardinality: CardinalityThe multiplicity of this property. Defaults to `0..*`.cardinality.Cardinality.setAsOneToMany(): voidSets the cardinality to `1..*`.setAsOneToMany();
const medicationEnd: PropertymedicationEnd.Property.cardinality: CardinalityThe multiplicity of this property. Defaults to `0..*`.cardinality.Cardinality.setAsOneToOne(): voidSets the cardinality to `1..1`.setAsOneToOne();
members(...) replaces the member list each time it is called, and the
builder is single-use. Pass every participant in one call, in the order you
want the ends to appear.
Key takeaways
- An n-ary relation connects three or more classifiers passed to
members(...); fewer than three fails atbuild(). - Only
.material()and.comparative()are valid stereotypes. - It has one
Propertyend per member, in argument order, reachable viarelation.properties. - Consider reifying into a
«relator»when each participation needs its own richer constraints.
Next: generalizations →, where classes are arranged into taxonomies.