Skip to main content
Version: Next

A complete diagram

Time to put the whole chapter together: take the school enrollment model from Hello, model, lay it out on a diagram, and serialize the result — model and concrete syntax in one JSON document.

Model, then depiction​

Notice the two phases. First the abstract syntax (classes and relations), exactly as before; then the concrete syntax (a diagram, its class views, and a routed connector). The two never mix.

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';
// 1. The model — abstract syntax. 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
();
// 2. The diagram — concrete syntax. const const diagram: Diagramdiagram = const project: Projectproject.Project.createDiagram(name?: string): Diagram
Creates a new Diagram and adds it to this project.
@paramname - optional name for the diagram, set in the default language.
createDiagram
('Overview');
const const personView: ClassViewpersonView = const diagram: Diagramdiagram.Diagram.addClass(clazz: Class): ClassView
Creates a ClassView for the given class and adds it to the diagram.
addClass
(const person: Classperson);
const personView: ClassViewpersonView.ClassView.rectangle: Rectangle
The rectangle that renders the class on the diagram canvas.
rectangle
.RectangularShape.setX(x: number): void
Sets the horizontal coordinate of the shape's top-left corner.
setX
(60);
const personView: ClassViewpersonView.ClassView.rectangle: Rectangle
The rectangle that renders the class on the diagram canvas.
rectangle
.RectangularShape.setY(y: number): void
Sets the vertical coordinate of the shape's top-left corner.
setY
(60);
const const schoolView: ClassViewschoolView = const diagram: Diagramdiagram.Diagram.addClass(clazz: Class): ClassView
Creates a ClassView for the given class and adds it to the diagram.
addClass
(const school: Classschool);
const schoolView: ClassViewschoolView.ClassView.rectangle: Rectangle
The rectangle that renders the class on the diagram canvas.
rectangle
.RectangularShape.setX(x: number): void
Sets the horizontal coordinate of the shape's top-left corner.
setX
(360);
const schoolView: ClassViewschoolView.ClassView.rectangle: Rectangle
The rectangle that renders the class on the diagram canvas.
rectangle
.RectangularShape.setY(y: number): void
Sets the vertical coordinate of the shape's top-left corner.
setY
(60);
const const enrollmentView: ClassViewenrollmentView = const diagram: Diagramdiagram.Diagram.addClass(clazz: Class): ClassView
Creates a ClassView for the given class and adds it to the diagram.
addClass
(const enrollment: Classenrollment);
const enrollmentView: ClassViewenrollmentView.ClassView.rectangle: Rectangle
The rectangle that renders the class on the diagram canvas.
rectangle
.RectangularShape.setX(x: number): void
Sets the horizontal coordinate of the shape's top-left corner.
setX
(210);
const enrollmentView: ClassViewenrollmentView.ClassView.rectangle: Rectangle
The rectangle that renders the class on the diagram canvas.
rectangle
.RectangularShape.setY(y: number): void
Sets the vertical coordinate of the shape's top-left corner.
setY
(220);
const const studiesAtView: BinaryRelationViewstudiesAtView = const diagram: Diagramdiagram.Diagram.addBinaryRelation(rel: BinaryRelation): BinaryRelationView
Creates a BinaryRelationView for the given relation and adds it to the diagram, creating views for the relation's source and target classifiers when these are not yet depicted. Throws an error if either end of the relation is untyped.
addBinaryRelation
(const studiesAt: BinaryRelationstudiesAt);
const studiesAtView: BinaryRelationViewstudiesAtView.BinaryConnectorView<Relation>.path: Path
The polyline that renders the connector on the diagram canvas.
path
.Path.moveTo(x: number, y: number): void
Appends a new Point with the given coordinates to the path.
moveTo
(160, 85);
const studiesAtView: BinaryRelationViewstudiesAtView.BinaryConnectorView<Relation>.path: Path
The polyline that renders the connector on the diagram canvas.
path
.Path.moveTo(x: number, y: number): void
Appends a new Point with the given coordinates to the path.
moveTo
(360, 85);
// 3. Round-trip the whole thing. 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);
var console: Consoleconsole.Console.log(...data: any[]): void
The **`console.log()`** static method outputs a message to the console. [MDN Reference](https://developer.mozilla.org/docs/Web/API/console/log_static)
log
(const diagram: Diagramdiagram.Diagram.getClassViews(): ClassView[]
Returns the ClassView instances in the diagram.
getClassViews
().Array<ClassView>.length: number
Gets or sets the length of the array. This is a number one higher than the highest index in the array.
length
); // 3

The serialized JSON now carries a diagrams array alongside the model — the same format the OntoUML visual tools read, so a model you lay out in code opens in the editor already positioned.

Now you try it​

The model and diagram are set up, but Enrollment is stacked on top of Person at the origin. Position its view at (210, 220) so it sits below and between the other two, then check the console.

Loading playground…
Show the solution
Loading playground…

What you learned​

  • A project holds both the model and its diagrams; serializing the project captures both.
  • Build the model first, then create a diagram and add views for the elements you want to depict.
  • Position class views through their rectangle; route relation views through their path.
  • The output is standard OntoUML JSON — a code-built layout opens, positioned, in the visual tools.

That's the end of the diagrams chapter. From here, learn to manipulate a model after it's built, or query one to analyze and transform it.