Skip to main content
Version: Next

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 Project
An 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.
@example```typescript const project = new Project(); const person = project.classBuilder().kind().name('Person').build(); const student = project.classBuilder().role().name('Student').build(); project .generalizationBuilder() .general(person) .specific(student) .build(); ```
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.
@example```typescript const json = serializationUtils.serialize(project); const parsed = serializationUtils.parse(json); ```
serializationUtils
} from 'ontouml-js';
const const project: Projectproject = new new Project(): Project
An 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.
@example```typescript const project = new Project(); const person = project.classBuilder().kind().name('Person').build(); const student = project.classBuilder().role().name('Student').build(); project .generalizationBuilder() .general(person) .specific(student) .build(); ```
Project
();
const project: Projectproject.NamedElement.name: LanguageString
The name of this element, possibly in multiple languages.
name
.LanguageString.add(value: string, language?: string): void
Adds a text value in the given language, replacing any existing value for that language.
@paramvalue - the text to add.@paramlanguage - language tag of `value`; defaults to defaultLanguage.@throwsan error if `language` is not a valid language tag.
add
('School enrollment');
const const model: Packagemodel = const project: Projectproject.Project.packageBuilder(): PackageBuilder
Creates a builder for a new Package in this project.
packageBuilder
().PackageBuilder.root(): PackageBuilder
Sets the package as the root package of its project, i.e., the package that contains the entire model.
@returnsthis builder, for method chaining.
root
().NamedElementBuilder<PackageBuilder>.name(value: string, language?: string): PackageBuilder
Adds a name to the element in the given language.
@paramvalue - the name text.@paramlanguage - language tag of `value`; defaults to LanguageString.defaultLanguage.@returnsthis builder, for method chaining.
name
('Model').PackageBuilder.build(): Package
Builds 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: []`
@throwsan error if the builder has already been used.
build
();
const const person: Classperson = const model: Packagemodel.Package.classBuilder(): ClassBuilder
Creates a builder for a Class contained in this package.
classBuilder
().ClassBuilder.kind(): ClassBuilder
Sets 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`
@returnsthis builder, for method chaining.
kind
().NamedElementBuilder<ClassBuilder>.name(value: string, language?: string): ClassBuilder
Adds a name to the element in the given language.
@paramvalue - the name text.@paramlanguage - language tag of `value`; defaults to LanguageString.defaultLanguage.@returnsthis builder, for method chaining.
name
('Person').ClassBuilder.build(): Class
Builds 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,`
@throwsan error if the builder has already been used.
build
();
const const school: Classschool = const model: Packagemodel.Package.classBuilder(): ClassBuilder
Creates a builder for a Class contained in this package.
classBuilder
().ClassBuilder.kind(): ClassBuilder
Sets 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`
@returnsthis builder, for method chaining.
kind
().NamedElementBuilder<ClassBuilder>.name(value: string, language?: string): ClassBuilder
Adds a name to the element in the given language.
@paramvalue - the name text.@paramlanguage - language tag of `value`; defaults to LanguageString.defaultLanguage.@returnsthis builder, for method chaining.
name
('School').ClassBuilder.build(): Class
Builds 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,`
@throwsan error if the builder has already been used.
build
();
const const enrollment: Classenrollment = const model: Packagemodel.Package.classBuilder(): ClassBuilder
Creates a builder for a Class contained in this package.
classBuilder
().ClassBuilder.relator(): ClassBuilder
Sets 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`
@returnsthis builder, for method chaining.
relator
().NamedElementBuilder<ClassBuilder>.name(value: string, language?: string): ClassBuilder
Adds a name to the element in the given language.
@paramvalue - the name text.@paramlanguage - language tag of `value`; defaults to LanguageString.defaultLanguage.@returnsthis builder, for method chaining.
name
('Enrollment').ClassBuilder.build(): Class
Builds 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,`
@throwsan error if the builder has already been used.
build
();
const const studiesAt: BinaryRelationstudiesAt = const model: Packagemodel .Package.binaryRelationBuilder(): BinaryRelationBuilder
Creates a builder for a BinaryRelation contained in this package.
binaryRelationBuilder
()
.BinaryRelationBuilder.material(): BinaryRelationBuilder
Sets 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`
@returnsthis builder, for method chaining.
material
()
.NamedElementBuilder<BinaryRelationBuilder>.name(value: string, language?: string): BinaryRelationBuilder
Adds a name to the element in the given language.
@paramvalue - the name text.@paramlanguage - language tag of `value`; defaults to LanguageString.defaultLanguage.@returnsthis builder, for method chaining.
name
('studies at')
.BinaryRelationBuilder.source(classifier: Classifier<any, any>): BinaryRelationBuilder
Sets the Classifier connected to the source end of the relation.
@returnsthis builder, for method chaining.
source
(const person: Classperson)
.BinaryRelationBuilder.target(classifier: Classifier<any, any>): BinaryRelationBuilder
Sets the Classifier connected to the target end of the relation.
@returnsthis builder, for method chaining.
target
(const school: Classschool)
.BinaryRelationBuilder.build(): BinaryRelation
Builds 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,`
@throwsan error if the builder has already been used.
build
();
const model: Packagemodel .Package.binaryRelationBuilder(): BinaryRelationBuilder
Creates a builder for a BinaryRelation contained in this package.
binaryRelationBuilder
()
.BinaryRelationBuilder.mediation(): BinaryRelationBuilder
Sets 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`
@returnsthis builder, for method chaining.
mediation
()
.BinaryRelationBuilder.source(classifier: Classifier<any, any>): BinaryRelationBuilder
Sets the Classifier connected to the source end of the relation.
@returnsthis builder, for method chaining.
source
(const enrollment: Classenrollment)
.BinaryRelationBuilder.target(classifier: Classifier<any, any>): BinaryRelationBuilder
Sets the Classifier connected to the target end of the relation.
@returnsthis builder, for method chaining.
target
(const person: Classperson)
.BinaryRelationBuilder.build(): BinaryRelation
Builds 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,`
@throwsan error if the builder has already been used.
build
();
const model: Packagemodel .Package.binaryRelationBuilder(): BinaryRelationBuilder
Creates a builder for a BinaryRelation contained in this package.
binaryRelationBuilder
()
.BinaryRelationBuilder.mediation(): BinaryRelationBuilder
Sets 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`
@returnsthis builder, for method chaining.
mediation
()
.BinaryRelationBuilder.source(classifier: Classifier<any, any>): BinaryRelationBuilder
Sets the Classifier connected to the source end of the relation.
@returnsthis builder, for method chaining.
source
(const enrollment: Classenrollment)
.BinaryRelationBuilder.target(classifier: Classifier<any, any>): BinaryRelationBuilder
Sets the Classifier connected to the target end of the relation.
@returnsthis builder, for method chaining.
target
(const school: Classschool)
.BinaryRelationBuilder.build(): BinaryRelation
Builds 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,`
@throwsan error if the builder has already been used.
build
();
const studiesAt: BinaryRelationstudiesAt.BinaryRelation.sourceEnd: Property
The relation end at the source (first) position.
sourceEnd
.Property.cardinality: Cardinality
The multiplicity of this property. Defaults to `0..*`.
cardinality
.Cardinality.setAsOneToMany(): void
Sets the cardinality to `1..*`.
setAsOneToMany
();
const studiesAt: BinaryRelationstudiesAt.BinaryRelation.targetEnd: Property
The relation end at the target (second) position.
targetEnd
.Property.cardinality: Cardinality
The multiplicity of this property. Defaults to `0..*`.
cardinality
.Cardinality.setAsZeroToMany(): void
Sets 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: JSON
An 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.
@paramvalue A JavaScript value, usually an object or array, to be converted.@paramreplacer A function that transforms the results.@paramspace Adds indentation, white space, and line break characters to the return-value JSON text to make it easier to read.@throws{TypeError} If a circular reference or a BigInt value is found.
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.
@example```typescript const json = serializationUtils.serialize(project); const parsed = serializationUtils.parse(json); ```
serializationUtils
.parse: (serializedProject: string) => Project
Parses 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.
@paramserializedProject - the JSON string to parse.@throwsan error if the string is not a schema-valid serialized Project or if references between elements cannot be resolved.
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.parse reads 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.

Loading playground…
Show the solution
Loading playground…

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 →.