Union

Union #

The Union class is a special data class that is used to enhance the expressiveness of the data model. Its purpose is not the instantiation of corresponding data objects, but the usage of several data objects with different data classes as possible attribute values. Such a union can be used in one attribute as in aggregates or as base class like in collections. For example, a set that should contain both, String and integer objects. This is only possible when using a union class, combining. It is not possible to instantiate this base class.

Defining a Union class #

In the following, the usage of UnionClass is illustrated. First, a subclass of the union class needs to be created. This is required because the parent class itself can’t be instantiated directly. Next, define which data classes should be used within this union class.

The class has to be defined in the model.xml file:

model.xml

    <UnionClass name="instantiableUnionClass">
        <ElementClass name="Integer"/>
        <ElementClass name="String"/>
    </UnionClass>

To create this class in the code, the following java code must be used:

Wiki_UnionTest.java

    UnionClass unionClass = ModelFactory.getDefaultModel().getUnionSystemClass();
    UnionClass instantiableUnionClass = (UnionClass) unionClass.createSubclass("instantiableUnionClass");
    instantiableUnionClass.add(ModelFactory.getDefaultModel().getIntegerSystemClass());
    instantiableUnionClass.add(ModelFactory.getDefaultModel().getStringSystemClass());
    instantiableUnionClass.finishEditing();

The usage of this union class within other data classes must be specified within these classes. For the example, described above, this can be performed in the model file as follows:

model.xml

    <SetClass name="customSetClass">
        <ElementClass name="instantiableUnionClass"/>
    </SetClass>

To perform this in the Java code, the following code must be used:

Wiki_UnionTest.java

    SetClass setClass = ModelFactory.getDefaultModel().getSetSystemClass();
    SetClass customSetClass = (SetClass) setClass.createSubclass("customSetClass");
    customSetClass.setElementClass(instantiableUnionClass);
    customSetClass.finishEditing();

Now, an object of this set class can be created in the casebase. This can look as follows:

casebase.xml

    <cdol:C c="customSetClass" id="unionSetObject">
        <cdol:A c="Integer" v="1" id="unionIntegerObject"/>
        <cdol:A c="String" id="unionStringObject" v="Test"/>
    </cdol:C>

To create such an object in Java, the integer and String objects can be created as described here. Afterwards, the following code can be used:

Wiki_UnionTest.java

    SetObject setObject = ModelFactory.getDefaultModel().createObject("customSetClass");
    setObject.addValue(integerObject);
    setObject.addValue(stringObject);

When trying to add another object than the specified ones to the objects using the union class, an exception will be thrown.

Similarity calculation between union objects #

Depending on the object classes used, the similarity calculation between two objects based on union classes can be different. If objects of two same data classes are compared, e.g. integer with integer, the default defined similarity measures are used. If, on the other hand, a similarity calculation is performed between two different objects, such as a string with an integer, a similarity measure is searched for that can be used for the lowest common superclass. So far, only the standard measures ObjectEquals and TableDataClass are provided for this purpose.