SourceForge.net Logo

How to set/retrieve reference values?

In compliance with the JMI specification the generator has created typed reference accessor operations for every reference originating from the modeled class. By using these accessor operations you have a simple, type-safe way

  • to retrieve the referenced objects from a given reference

  • to add new objects to a given reference

  • to remove existing objects from a given reference

The add and remove operations are only provided if the UML constraint isFrozen was not set for the corresponding reference. For more information about this constraint please refer to the openMDX Modeling document.

The following example shows how to retrieve the referenced objects for a given reference.

Example 4-7. Retrieve group instances of the reference group of a Person instance

// retrieve all group instances
Collection groups = person.getGroup();

// retrieve the group instance at the specified index
// only for references stored as attribute
Group group = person.getGroup(index);

// retrieve the address instance for a specified qualifier (priority)
// only for references not stored as attribute
Address address = person.getAddress(priority);

The following example shows how to add a new object to a given reference.

Example 4-8. Add a group instance to the reference group of a Person instance

// add a specified group instance
person.addGroup(group);

// add a specified group instance at the specified index
// only for references stored as attribute
person.addGroup(index, group);

// add a specified address instance for a specified qualifier (priority)
// only for references not stored as attribute
person.addAddress(priority, address);

The following example shows how to remove an existing object from a given reference.

Example 4-9. Remove a group instance from the reference group of a Person instance

// remove a specified group instance
person.removeGroup(group);

// remove the group instance at the specified index
// only for references stored as attribute
person.removeGroup(index);

// remove the address instance for a specified qualifier (priority)
// only for references not stored as attribute
person.removeAddress(priority);

A remove does not necessarily remove the object itself. The semantics of the remove operation depends on the aggregation (composite, shared, none) of the modeled reference. openMDX defines the following semantic: none removes only the reference to the object but not the object itself; composite removes the object; shared removes the access to the object. The implementation is application-specific. This can either be implemented by removing the object or by removing the association between the objects. This topic is explained in more detail in the openMDX modeling tutorial.