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 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';
// 1. The model — abstract syntax.
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();
// 2. The diagram — concrete syntax.
const const diagram: Diagramdiagram = const project: Projectproject.Project.createDiagram(name?: string): DiagramCreates a new
Diagram
and adds it to this project.createDiagram('Overview');
const const personView: ClassViewpersonView = const diagram: Diagramdiagram.Diagram.addClass(clazz: Class): ClassViewCreates a
ClassView
for the given class and adds it to the
diagram.addClass(const person: Classperson);
const personView: ClassViewpersonView.ClassView.rectangle: RectangleThe rectangle that renders the class on the diagram canvas.rectangle.RectangularShape.setX(x: number): voidSets the horizontal coordinate of the shape's top-left corner.setX(60);
const personView: ClassViewpersonView.ClassView.rectangle: RectangleThe rectangle that renders the class on the diagram canvas.rectangle.RectangularShape.setY(y: number): voidSets the vertical coordinate of the shape's top-left corner.setY(60);
const const schoolView: ClassViewschoolView = const diagram: Diagramdiagram.Diagram.addClass(clazz: Class): ClassViewCreates a
ClassView
for the given class and adds it to the
diagram.addClass(const school: Classschool);
const schoolView: ClassViewschoolView.ClassView.rectangle: RectangleThe rectangle that renders the class on the diagram canvas.rectangle.RectangularShape.setX(x: number): voidSets the horizontal coordinate of the shape's top-left corner.setX(360);
const schoolView: ClassViewschoolView.ClassView.rectangle: RectangleThe rectangle that renders the class on the diagram canvas.rectangle.RectangularShape.setY(y: number): voidSets the vertical coordinate of the shape's top-left corner.setY(60);
const const enrollmentView: ClassViewenrollmentView = const diagram: Diagramdiagram.Diagram.addClass(clazz: Class): ClassViewCreates a
ClassView
for the given class and adds it to the
diagram.addClass(const enrollment: Classenrollment);
const enrollmentView: ClassViewenrollmentView.ClassView.rectangle: RectangleThe rectangle that renders the class on the diagram canvas.rectangle.RectangularShape.setX(x: number): voidSets the horizontal coordinate of the shape's top-left corner.setX(210);
const enrollmentView: ClassViewenrollmentView.ClassView.rectangle: RectangleThe rectangle that renders the class on the diagram canvas.rectangle.RectangularShape.setY(y: number): voidSets the vertical coordinate of the shape's top-left corner.setY(220);
const const studiesAtView: BinaryRelationViewstudiesAtView = const diagram: Diagramdiagram.Diagram.addBinaryRelation(rel: BinaryRelation): BinaryRelationViewCreates 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: PathThe polyline that renders the connector on the diagram canvas.path.Path.moveTo(x: number, y: number): voidAppends a new
Point
with the given coordinates to the path.moveTo(160, 85);
const studiesAtView: BinaryRelationViewstudiesAtView.BinaryConnectorView<Relation>.path: PathThe polyline that renders the connector on the diagram canvas.path.Path.moveTo(x: number, y: number): voidAppends 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: 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);
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 diagram: Diagramdiagram.Diagram.getClassViews(): ClassView[]Returns the
ClassView
instances in the diagram.getClassViews().Array<ClassView>.length: numberGets 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.
Show the solution
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 theirpath. - 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.