Manipulating Metadata

From nUML

Introduction

One of the features of the XMI standard is the possibility of attaching pieces of data to any object, regardless of its metamodel. This piece of data about an element is called metadata; this special name is used in order to differentiate it from the element itsef (which is considered as "data").

One of these pieces of data is well defined by the XMI standard, and it's the Documentation element, which defines fields for storing a short description, a long description, a notice, an owner and other data.

There is also another way of storing data, but for arbitrary content: the Extension element. This element defines only two attributes: the extender and the extenderID. The rest of the content is up to the user.

As always, we recommend reading the specification for gaining a complete understanding of these concepts.

Accessing the Metadata

Access to the metadata is provided through the SerializationDriver, which exposes the method GetXmiMetadata:

public NUml.Xmi2.Model.Metadata GetXmiMetadata(object @object)

Using this method it's possible to get the XMI metadata of any object.

If the object has no metadata, an empty Metadata object will be returned. This instance can be used to set metadata on the object, such as documentation and extension elements:

using NUml.Xmi2;
using NUml.Xmi2.Model;
// ...
SerializationDriver ser = ...
Metadata mdata = ser.GetXmiMetadata(anyObject);
Extension ext = new Extension();
ext.Extender = "myTestClass";
ext.ExtenderID = "some_ID";
mdata.Extensions.Add(ext);

The same happens with the Documentations:

// ... continues from previous snippet
Documentation doc = new Documentation();
doc.ShortDescription.Add("its weird but the OMG defined shortDescription as a collection of strings");
mdata.Documentations.Add(doc);

When the object gets serialized, elements of the form

<xmi:Extension extender="myTestClass" extenderID="some_ID" />
<xmi:Documentation shortDescription="its weird but [...]" />

will be nested within the element that represents anyObject.

Of course, this same procedure can be used to query the object's extensions and documentations:

foreach(Documentation doc in mdata.Documentations)
  // do something with "doc"