Aggregate

Aggregate #

This page contains the following content:

Aggregate Objects #

The Aggregate class is used to combine several classes into one class in an object-oriented manner. This combination is realized with attributes that consist of a name and a corresponding data class similar to instance variables of classes in object-oriented programming languages. Arbitrary data classes including other aggregate classes can be used. Consequently, the modeling of deep aggregation hierarchies is possible. As the principle of Aggregate classes is based on object-oriented programming, it is also possible to generate abstract classes by using the XML attribute abstract. In this case, it is not possible to instantiate objects of this class directly.

To create an aggregate class in the model.xml configuration, a new subclass of class Aggregate using the tag AggregateClass must be specified. The attributes are defined using the inner tags Attribute.

model.xml

    <AggregateClass name="customAggregateClass">
        <Attribute name="name" class="String"/>
        <Attribute name="reference" class="customAggregateClass"/>
    </AggregateClass>

To create this class during runtime, the following java code has to be used:

Wiki_AggregateTest.java

    AggregateClass aggregateClass = (AggregateClass) ModelFactory.getDefaultModel().getAggregateSystemClass();
    AggregateClass customAggregateClass = (AggregateClass) aggregateClass.createSubclass("customAggregateClass");
    customAggregateClass.addAttribute("name", ModelFactory.getDefaultModel().getStringSystemClass());
    customAggregateClass.addAttribute("reference", ModelFactory.getDefaultModel().getClass("customAggregateClass"));
    customAggregateClass.setAbstract(false);
    customAggregateClass.finishEditing();
It should be noted that for an aggregate class using the setAbstract(false) method call, the class must be set to non-abstract. Otherwise, it cannot be instantiated.

To create an object of this aggregate class, the following java code has to be used:

Wiki_AggregateTest.java

    AggregateObject aggregateObject = (AggregateObject) ModelFactory.getDefaultModel().createObject("customAggregateClass");

    StringObject name = (StringObject) ModelFactory.getDefaultModel().getStringSystemClass().newObject();
    name.setNativeString("aggregate 1");
    aggregateObject.setAttributeValue("name", name);

    AggregateObject referenceObject = (AggregateObject) ModelFactory.getDefaultModel().createObject("customAggregateClass");
    StringObject nameReference = (StringObject) ModelFactory.getDefaultModel().getStringSystemClass().newObject();
    nameReference.setNativeString("aggregate 2");
    referenceObject.setAttributeValue("name", nameReference);
    VoidObject referenceReference = (VoidObject) ModelFactory.getDefaultModel().getVoidSystemClass().newObject();
    referenceObject.setAttributeValue("reference", referenceReference);

    aggregateObject.setAttributeValue("reference", referenceObject);

Aggregate objects are typically used as cases in a casebase, e.g., for attribute-value based case bases. Thus, the objects can also be written into the casebase, and they can also directly created in the casebase. The following example shows the above created aggregate object in the casebase.xml file for the given class definition:

casebase.xml

    <cdol:Agg c="customAggregateClass">
        <cdol:AA n="name" v="aggregate 1"/>
        <cdol:OA n="reference">
            <cdol:Agg>
                <cdol:AA n="name" v="aggregate 2"/>
                <cdol:OA n="reference">
                    <cdol:V c="Void"/>
                </cdol:OA>
            </cdol:Agg>
        </cdol:OA>
    </cdol:Agg>

Properties #

Additionally, to the properties for data classes it’s possible to define properties for aggregate attributes. Like the data classes properties, properties can be defined in properties. It’s also not possible to set two different values here.

For example, such a property definition can look like:

model.xml

    <AggregateClass name="customAggregateClassProperty">
        <Attribute name="name" class="String"/>
        <Attribute name="reference" class="customAggregateClass">
            <Property name="note">Example commentary</Property>
        </Attribute>
    </AggregateClass>

During runtime, this can look like:

Wiki_AggregateTest.java

    AggregateClass aggregateClass = (AggregateClass) ModelFactory.getDefaultModel().getAggregateSystemClass();
    AggregateClass customAggregateClass = (AggregateClass) aggregateClass.createSubclass("customAggregateClassProperty");
    customAggregateClass.addAttribute("name", ModelFactory.getDefaultModel().getStringSystemClass());
    customAggregateClass.addAttribute("reference", ModelFactory.getDefaultModel().getClass("customAggregateClassProperty"));

    customAggregateClass.addAttributeProperty("reference", "note", "Example commentary");

    customAggregateClass.finishEditing();

To get the value of a property afterwards, the command getAttributeProperty(String attributeName, String propertyName) can be used. To change the value of the property, this property must first be removed and then added again. This can look like:

Wiki_AggregateTest.java

    customAggregateClass.removeAttributeProperty("reference", "note");
    customAggregateClass.addAttributeProperty("reference", "note", "Some content");