Repository navigation
uml4net.xmi.project
The uml4net.xmi project/library reads an XMI 2.5.1 document that contains a UML model and returns a fully dereferenced object graph of its contents, and writes such a graph back to XMI. The entry points are the XmiReaderBuilder, which returns an IXmiReader, and the XmiWriterBuilder, which returns an IXmiWriter.
The following code snippet shows how the XmiReaderBuilder is used to read an XMI document:
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Debug()
.WriteTo.Console()
.CreateLogger();
var loggerFactory = LoggerFactory.Create(builder =>
{
builder.AddSerilog();
});
var rootPath = <...path to folder where external references are stored>
var reader = XmiReaderBuilder.Create()
.UsingSettings(x => x.LocalReferenceBasePath = rootPath)
.WithLogger(loggerFactory)
.Build();
var xmiReaderResult = reader.Read(<...path to XMI file...>);
var rootPackage = xmiReaderResult.QueryRoot("<xmi:id-of-the-root-package-you-are-interested-in>");NOTE: an XMI document may contain more than one element at the root. The
QueryRootmethod returns the Model (or Package or Profile) you are interested in to operate on,QueryRootElement<T>returns a top-level element of any other type.
The .UsingSettings extension method sets up the IXmiReaderSettings. The following properties can be set:
| Setting | Default | Meaning |
|---|---|---|
LocalReferenceBasePath |
The folder where external documents (referenced UML models) are looked for. | |
PathMaps |
empty | Maps virtual paths to physical file locations, e.g. pathmap://UML_LIBRARIES/UMLPrimitiveTypes.library.uml as used by Eclipse UML2 models. |
ValueSeparator |
, |
The separator of property values that are serialized as a single string. |
MultiReferenceValueSeparator |
space | The separator of multi-valued references that are serialized as a single string. |
UseStrictReading |
true |
Whether the reader rejects content that is not valid XMI 2.5.1, see below. |
ThrowOnUnresolvedReferences |
false |
Whether references that cannot be resolved end the read with a single UnresolvedReferencesException, instead of being logged. |
var reader = XmiReaderBuilder.Create()
.UsingSettings(x =>
{
x.PathMaps = new Dictionary<string, string> { ["pathmap://UML_LIBRARIES/UMLPrimitiveTypes.library.uml"] = <...path to physical file location...> };
x.UseStrictReading = false;
})
.Build();With UseStrictReading = true (the default) the reader throws when the document is not valid according to XMI 2.5.1; otherwise it logs the problem and continues with a defined result:
| Content | Strict | Non-strict |
|---|---|---|
Unknown element or attribute, e.g. EMF eAnnotations in an Eclipse UML2 model |
NotSupportedException |
skipped with a warning |
Invalid value: a numeric or wrongly cased enumeration literal, a duplicate xmi:id, a single-valued reference or composite property given more than once, a MOF tag of the wrong type |
XmiReadException naming the element, property and line position |
logged as an error, the first value is kept |
| Serialized derived property (XMI 2.5.1 clause 7.8.10) | ignored | ignored |
Namespaces are resolved, not matched literally: a document may bind the UML, XMI or MOF namespace to any prefix, or use the UML namespace as default namespace. Boolean, Integer and Real values are read in their XML Schema lexical forms, independent of the culture, and xsi:nil is supported.
| Member | Content |
|---|---|
QueryRoot(xmiId, name) |
a top-level Package, Model or Profile of the document |
QueryRootElement<T>(xmiId) |
a top-level element of any type |
DocumentRootElements |
the top-level elements of the document that was read, the elements to write back to reproduce it |
ExternalRootElements |
the top-level elements of the external documents loaded while resolving references |
RootElements |
the union of the two |
Packages |
all top-level packages that were read, including those of external documents |
XmiRoot |
the document-level content of the document, see below |
ExternalXmiRoots |
the XmiRoot of each external document |
The XmiRoot holds what an xmi:XMI element contains besides the model:
| Property | Content |
|---|---|
Content |
the top-level model elements |
Documentation |
the xmi:Documentation (exporter, exporterVersion, ...) |
Extensions |
the document-level xmi:Extension elements |
Tags |
the MOF tags (mofext:Tag), such as org.omg.xmi.nsPrefix
|
StereoTypeApplications |
the stereotype applications, see [[Profiles and StereoTypes |
DiagramInterchange |
the UML Diagram Interchange content, captured as raw XML |
UnprocessedContent |
other content that uml4net does not model, captured as raw XML |
Captured content is written back unchanged, so a document survives a read-write cycle without losing it.
External references are references (href) to elements of another XMI document. A good example is PrimitiveTypes.xmi, which is referenced from UML.xmi. The ExternalReferenceResolver loads every referenced document that is available at the LocalReferenceBasePath (or through a PathMaps entry), also the documents referenced from those, and reads their elements into the same XmiElementCache. Relative hrefs are resolved against the referencing document; xmi:uuid-based XPointer references are supported as well.
Once all documents are read, the Assembler turns the recorded identifiers into object references. References that cannot be resolved are recorded as resolution failures (NotFound, UnexpectedType, AlreadyOwned, MultiplicityExceeded), logged, and preserved so the writers can write them back. Read more on the Assembler and XmiElement pages.
For each concrete UML class an IXmiElementReader is code-generated. This reader is responsible for reading the XML fragment of that element and its contained elements, and returns the element. For each owned attribute the IXmiElementReaderFacade resolves the correct IXmiElementReader.
The
IXmiElementReaderFacadeand theIXmiElementReaderimplementations are code-generated using the uml4net libraries and are stored in the AutoGenXmiReaders project folder.
The following code snippet shows how a model that was read is written back:
var writer = XmiWriterBuilder.Create()
.UsingSettings(x => x.ExternalReferenceResolution = ExternalReferenceResolutionKind.Href)
.WithLogger(loggerFactory)
.Build();
// write one package
writer.Write(rootPackage, <...path to output file...>);
// write the whole document: its top-level elements and its document-level content
writer.Write(xmiReaderResult.DocumentRootElements, <...path to output file...>, xmiReaderResult.XmiRoot);The IXmiWriter writes to a file or a Stream, synchronously or asynchronously. Overloads take the Documentation, document-level XmiExtensions and Tags separately; the XmiRoot overload writes all document-level parts: documentation, top-level elements, tags, stereotype applications, captured content and extensions.
| Setting | Default | Meaning |
|---|---|---|
ExternalReferenceResolution |
Href |
How references to elements outside the written package are written: Href writes an href to the original document; Include adds the root packages that contain them to the written document, which makes it self-contained. |
UmlNamespaceUri, XmiNamespaceUri, MofExtNamespaceUri
|
the XMI 2.5.1 / UML 2.5.1 URIs | The namespace URIs declared on the root element. |
Indent |
true |
Whether the XML is indented. |
UseCanonicalXmi |
false |
Whether the document is written as Canonical XMI (XMI 2.5.1 Annex B). |
A referenced element that is not written and has no xmi:id, or no document name, makes the writer throw, since it could not be referred to.
By default the writer writes the model as it was read: the xmi:ids and xmi:uuids are not changed, single values are written as attributes, the properties in alphabetical order, and the extensions are kept.
With UseCanonicalXmi = true the writer writes Canonical XMI (XMI 2.5.1 Annex B): every property as an element in the canonical order, an xmi:id derived from the model (Annex B.6) and an xmi:uuid on every object, reference elements without xmi:type, and no documentation, extensions or captured content. The same model always gives the same document.
The deliberate serialization choices, such as the property order, xmi:type on href elements and owner ends that are not written, are recorded in design-decisions.md.
Like the readers, an XmiElementWriter is code-generated for each concrete UML class, with a WriteDefault and a WriteCanonical path, in the AutoGenXmiWriters project folder.
copyright @ Starion Group S.A.
- Home
- uml4net Tools
- Code Generation Tutorial
- Primitive Types Mapping
- Profiles and StereoTypes
- Code Generation Conventions
- UML Handlebars Helpers
- Generating uml4net
- Developers Experience
- SBOM
- Design Decisions