Collection

Collection #

This page contains the following content:

The Collection classes are containers that can store each kind of data objects. Typical restrictions are the maximum and minimum number of elements and the base data class of which objects can be instantiated. The Collection class is an abstract class that can not be instantiated directly.

There are two available class types for defining collection objects:

  1. List: A list class is a collection class, where the elements have a specific order. This class can contain duplicates, if they are added to it.
  2. Set: A set class is a collection class, where it is not possible to specify the order of the elements. Thus, each element is unique within the set.

To create a collection class in the model.xml file, a new subclass of the class List or the class Set must be created. The element class is defined using an inner tag ElementClass. The restrictions for the number of elements can be set here. By using the tags maxCardinality and minCardinality, the required maximum and minimum number of elements for each instance is defined.

List #

A List class is a subclass of the Collection class, where it is possible to specify the order of the elements in the List object. Thus, the elements can be accessed by using an index. Furthermore, it is possible to have duplicate objects in lists.

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

model.xml

    <ListClass name="customListClass">
        <ElementClass name="String"/>
    </ListClass>

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

Wiki_CollectionTest.java

    ListClass listClass = (ListClass) ModelFactory.getDefaultModel().getListSystemClass();
    ListClass customListClass = (ListClass) listClass.createSubclass("customListClass");
    customListClass.setElementClass(ModelFactory.getDefaultModel().getStringSystemClass());
    customListClass.finishEditing();

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

Wiki_CollectionTest.java

    ListObject listObject = (ListObject) ModelFactory.getDefaultModel().createObject("customListClass");

To add an object to the list, the following code can be used.

Wiki_CollectionTest.java

    listObject.addValue(stringObject);
    listObject.addValue(stringObject2);
The objects stringObject and stringObject2 must be of the class StringClass.

The XML representation of the list object depicted above is as follows:

casebase.xml

    <cdol:C c="customListClass" id="testList">
        <cdol:A v="test"/>
        <cdol:A v="test2"/>
    </cdol:C>

Set #

A Set class is a subclass of the Collection class, where it is not possible to specify the order of the elements in the Set object. It is also not possible to have duplicates in the set. Duplicate values can be added but if the Set already contains such a value, nothing happens.

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

model.xml

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

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

Wiki_CollectionTest.java

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

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

Wiki_CollectionTest.java

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

To add an object to the set, the following code can be used.

Wiki_CollectionTest.java

    setObject.addValue(stringObject);
    setObject.addValue(stringObject2);
The objects stringObject and stringObject2 must be of the class StringClass.

The XML representation of the set object depicted above is as follows:

casebase.xml

    <cdol:C c="customListClass" id="testList">
        <cdol:A v="test"/>
        <cdol:A v="test2"/>
    </cdol:C>