Setup
You can follow this whole tutorial without installing anything — the live
editors in each chapter run ontouml-js in your browser. But to build real
projects you'll want it locally.
Install
ontouml-js runs on Node.js 18+. Add it to a project:
npm install ontouml-js
A first script
Create model.ts (or .js) and import from the package:
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 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('My first project');
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 project: Projectproject.NamedElement.name: LanguageStringThe name of this element, possibly in multiple languages.name.LanguageString.get(language?: string): string | nullReturns the text value for the given language.
If no language is provided, returns the value of the first language in
languagePreference
for which a value exists. If no such value
exists, returns the first value it finds. If no values are defined,
returns `null`.get());
Run it with your favourite TypeScript runner (tsx, ts-node) or compile with
tsc. If you see My first project, you're ready.
TypeScript recommended
ontouml-js is written in TypeScript and ships full type definitions. Using it
from TypeScript means your editor autocompletes stereotypes and builder
methods, and catches mistakes before you run anything. Every example in this
tutorial is type-checked, so what you read is guaranteed to compile.