Projects & packages
Everything in ontouml-js lives inside a Project, and every model element
lives inside a Package. Before we get to classes and relations, let's look
at these containers — because they're also how you create everything else.
A package is just a named grouping of model elements. Large ontologies are split into packages by sub-domain (people, products, payments…) exactly as you'd split code into modules. One package per project is the root ("model") package; all others nest inside it.
Creating a project
There are two equivalent ways. Use the constructor for quick work:
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('Library domain');
Or the fluent ProjectBuilder when you want to set catalog metadata (publisher, license, keywords) at creation time:
import { class ProjectBuilderA fluent builder for
Project
instances.
In addition to the identification and naming methods inherited from
NamedElementBuilder
, this builder supports the project-level
metadata fields prescribed by the OntoUML/UFO catalog metadata schema,
such as publisher, license, keywords, and designed-for tasks.ProjectBuilder } from 'ontouml-js';
const const project: Projectproject = new new ProjectBuilder(): ProjectBuilderA fluent builder for
Project
instances.
In addition to the identification and naming methods inherited from
NamedElementBuilder
, this builder supports the project-level
metadata fields prescribed by the OntoUML/UFO catalog metadata schema,
such as publisher, license, keywords, and designed-for tasks.ProjectBuilder()
.NamedElementBuilder<ProjectBuilder>.name(value: string, language?: string): ProjectBuilderAdds a name to the element in the given language.name('Library domain')
.ProjectBuilder.keyword(value: string, language?: string): ProjectBuilderAdds a keyword describing the content of the project.keyword('libraries')
.ProjectBuilder.language(value: string): ProjectBuilderAdds a language in which the project's elements are written, identified
by a language tag (e.g., `"en"`, `"pt"`).language('en')
.ProjectBuilder.license(uri?: string, name?: string, language?: string): ProjectBuilderSets the license under which the project is distributed.license('https://creativecommons.org/licenses/by/4.0/')
.ProjectBuilder.build(): ProjectBuilds an instance of
Project
with the parameters passed to the
builder. When no methods are evoked, the created project has the following
defaults:
- `id: "randomly-generated-id",`
- `created: new Date()`build();
The root package, and nesting
A project's elements hang off a single root package. Create it with
.root(), then nest sub-packages inside it. Remember: a package is a factory
for its contents, so you build classes and sub-packages from the package
they belong to.
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('Library domain');
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 people: Packagepeople = const model: Packagemodel.Package.packageBuilder(): PackageBuilderCreates a builder for a
Package
contained in this package.packageBuilder().NamedElementBuilder<PackageBuilder>.name(value: string, language?: string): PackageBuilderAdds a name to the element in the given language.name('People').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 catalog: Packagecatalog = const model: Packagemodel.Package.packageBuilder(): PackageBuilderCreates a builder for a
Package
contained in this package.packageBuilder().NamedElementBuilder<PackageBuilder>.name(value: string, language?: string): PackageBuilderAdds a name to the element in the given language.name('Catalog').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 people: Packagepeople.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 people: Packagepeople.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('Librarian').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 catalog: Packagecatalog.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('Book').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 allClasses: Class[]allClasses = const project: Projectproject.Project.classes: Class[]The classes contained in this project.classes;
const const peopleClasses: Class[]peopleClasses = const people: Packagepeople.Package.classes: Class[]The classes directly contained in this package.classes;
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 allClasses: Class[]allClasses.Array<Class>.length: numberGets or sets the length of the array. This is a number one higher than the highest index in the array.length); // 3
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 peopleClasses: Class[]peopleClasses.Array<Class>.length: numberGets or sets the length of the array. This is a number one higher than the highest index in the array.length); // 2
Hover allClasses above: the project's classes getter walks every nested
package for you, so you rarely need to traverse the tree by hand. We'll go
deeper on reading a model back in the Querying chapter.
Key takeaways
- A Project is the root; it holds exactly one root Package.
- Packages nest and group elements by sub-domain.
- A container is a factory — build contents from the package they live in,
and their
containeris wired up for you. project.classes(and friends) query across the whole containment tree.
Next: classes & stereotypes →, where the modeling really begins.