Hello, model
Let's build a complete model end to end and serialize it — the classic "students enrolling at a school" example. Read the annotated code, then try it yourself in the live editor at the bottom.
The whole thing at once
We'll model people who study at schools, where each enrollment ties a
person to a school. Person, School, and Enrollment are classes;
studies at is a relation between them.
Hover any identifier below to see its inferred type — this snippet is
type-checked against the real ontouml-js API when the page is built.
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, const serializationUtils: {
parse: (serializedProject: string) => Project;
serialize: (project: Project, space?: string | number) => string;
validate: (serializedProject: object | string) => void;
}
A utility module supporting the serialization, deserialization, and
validation of
Project
objects according to the OntoUML JSON Schema
(https://w3id.org/ontouml/schema). Both
serialize
and
parse
validate the serialization against the schema, throwing on invalid input.serializationUtils } 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 project: Projectproject.NamedElement.name: LanguageStringThe name of this element, possibly in multiple languages.name.LanguageString.add(value: string, language?: string): voidAdds a text value in the given language, replacing any existing value for
that language.add('School enrollment');
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('Model').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 person: Classperson = 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('Person').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 school: Classschool = 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('School').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 enrollment: Classenrollment = const model: Packagemodel.Package.classBuilder(): ClassBuilderCreates a builder for a
Class
contained in this package.classBuilder().ClassBuilder.relator(): ClassBuilderSets the stereotype of the class to «relator», which decorates rigid
classes whose instances are relators, i.e., entities that mediate other
entities and are the truthmakers of material relations (e.g.,
`Marriage`, `Enrollment`). Also applies the following defaults:
- `restrictedTo = [ "relator" ]`
- `order = 1`relator().NamedElementBuilder<ClassBuilder>.name(value: string, language?: string): ClassBuilderAdds a name to the element in the given language.name('Enrollment').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 studiesAt: BinaryRelationstudiesAt = const model: Packagemodel
.Package.binaryRelationBuilder(): BinaryRelationBuilderCreates a builder for a
BinaryRelation
contained in this
package.binaryRelationBuilder()
.BinaryRelationBuilder.material(): BinaryRelationBuilderSets the stereotype of the relation to «material», which decorates
relations between endurants that are founded on relators, such as being
married to or being enrolled at. Also applies the following defaults:
- `isDerived = true`material()
.NamedElementBuilder<BinaryRelationBuilder>.name(value: string, language?: string): BinaryRelationBuilderAdds a name to the element in the given language.name('studies at')
.BinaryRelationBuilder.source(classifier: Classifier<any, any>): BinaryRelationBuilderSets the
Classifier
connected to the source end of the relation.source(const person: Classperson)
.BinaryRelationBuilder.target(classifier: Classifier<any, any>): BinaryRelationBuilderSets the
Classifier
connected to the target end of the relation.target(const school: Classschool)
.BinaryRelationBuilder.build(): BinaryRelationBuilds an instance of
BinaryRelation
with the parameters passed
to the builder. Both the source and the target classifiers 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 model: Packagemodel
.Package.binaryRelationBuilder(): BinaryRelationBuilderCreates a builder for a
BinaryRelation
contained in this
package.binaryRelationBuilder()
.BinaryRelationBuilder.mediation(): BinaryRelationBuilderSets the stereotype of the relation to «mediation», which decorates
relations between a relator and an endurant it mediates, such as a
marriage and one of its spouses. Also applies the following defaults:
- `sourceCardinality = "1..*"`
- `targetCardinality = "1"`
- `targetEnd.isReadOnly = true`mediation()
.BinaryRelationBuilder.source(classifier: Classifier<any, any>): BinaryRelationBuilderSets the
Classifier
connected to the source end of the relation.source(const enrollment: Classenrollment)
.BinaryRelationBuilder.target(classifier: Classifier<any, any>): BinaryRelationBuilderSets the
Classifier
connected to the target end of the relation.target(const person: Classperson)
.BinaryRelationBuilder.build(): BinaryRelationBuilds an instance of
BinaryRelation
with the parameters passed
to the builder. Both the source and the target classifiers 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 model: Packagemodel
.Package.binaryRelationBuilder(): BinaryRelationBuilderCreates a builder for a
BinaryRelation
contained in this
package.binaryRelationBuilder()
.BinaryRelationBuilder.mediation(): BinaryRelationBuilderSets the stereotype of the relation to «mediation», which decorates
relations between a relator and an endurant it mediates, such as a
marriage and one of its spouses. Also applies the following defaults:
- `sourceCardinality = "1..*"`
- `targetCardinality = "1"`
- `targetEnd.isReadOnly = true`mediation()
.BinaryRelationBuilder.source(classifier: Classifier<any, any>): BinaryRelationBuilderSets the
Classifier
connected to the source end of the relation.source(const enrollment: Classenrollment)
.BinaryRelationBuilder.target(classifier: Classifier<any, any>): BinaryRelationBuilderSets the
Classifier
connected to the target end of the relation.target(const school: Classschool)
.BinaryRelationBuilder.build(): BinaryRelationBuilds an instance of
BinaryRelation
with the parameters passed
to the builder. Both the source and the target classifiers 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 studiesAt: BinaryRelationstudiesAt.BinaryRelation.sourceEnd: PropertyThe relation end at the source (first) position.sourceEnd.Property.cardinality: CardinalityThe multiplicity of this property. Defaults to `0..*`.cardinality.Cardinality.setAsOneToMany(): voidSets the cardinality to `1..*`.setAsOneToMany();
const studiesAt: BinaryRelationstudiesAt.BinaryRelation.targetEnd: PropertyThe relation end at the target (second) position.targetEnd.Property.cardinality: CardinalityThe multiplicity of this property. Defaults to `0..*`.cardinality.Cardinality.setAsZeroToMany(): voidSets the cardinality to `0..*`.setAsZeroToMany();
const const classes: Class[]classes = const model: Packagemodel.Package.classes: Class[]The classes directly contained in this package.classes;
const const json: stringjson = var JSON: JSONAn intrinsic object that provides functions to convert JavaScript values to and from the JavaScript Object Notation (JSON) format.JSON.JSON.stringify(value: any, replacer?: (this: any, key: string, value: any) => any, space?: string | number): string (+1 overload)Converts a JavaScript value to a JavaScript Object Notation (JSON) string.stringify(const project: Projectproject);
const const copy: Projectcopy = const serializationUtils: {
parse: (serializedProject: string) => Project;
serialize: (project: Project, space?: string | number) => string;
validate: (serializedProject: object | string) => void;
}
A utility module supporting the serialization, deserialization, and
validation of
Project
objects according to the OntoUML JSON Schema
(https://w3id.org/ontouml/schema). Both
serialize
and
parse
validate the serialization against the schema, throwing on invalid input.serializationUtils.parse: (serializedProject: string) => ProjectParses a
Project
serialized into a string, as defined by the
OntoUML JSON Schema (https://w3id.org/ontouml/schema), and returns the
corresponding instance of
Project
. The input is validated against
the schema before any element is deserialized.
Model elements, diagrams, views, and shapes are fully supported. When the
shapes referenced by a view are absent from the serialization, the view
keeps the serialized shape identifiers but its shapes are recreated with
default dimensions and positions.parse(const json: stringjson);
That's a real, valid OntoUML model in about twenty lines. Notice the shape of the API:
- Containers are factories.
project.packageBuilder(),model.classBuilder(),model.binaryRelationBuilder()— you always build an element from the container it belongs to. - Builders are fluent. Each call returns the builder, so you chain
stereotype → name → references, then
.build()to get the element. - Everything serializes.
JSON.stringify(project)produces standard OntoUML JSON;serializationUtils.parsereads it back.
Now you try it
The editor below has the model almost finished. Complete the three TODOs,
then open the console to see the serialized JSON update.
Show the solution
What you learned
- A model lives in a Project and a root Package.
- Containers act as builders/factories for their contents.
- Classes carry a stereotype (
kind,relator, …). - Relations connect classes; their ends carry cardinalities.
- Models serialize to and from OntoUML JSON.
Next we'll slow down and look at each layer properly, starting with projects & packages →.