🚀 Welcome to MDriven Learn –  MDriven is now on Discord!  Don’t miss the latest Release Notes.
OCLOperators collectNested
This page was created by Stephanie on 2023-03-27. Last edited by Wikiadmin on 2026-07-29.

You can use collectNested in OCL when you want to evaluate an expression for every item in a collection and preserve each expression result as a nested collection.

Syntax

collection->collectNested(item | expression)
Part Meaning
collection The source collection to evaluate.
item The iterator variable representing one element of the source collection.
expression The OCL expression evaluated for each item.

Result

collectNested returns a collection containing the result of expression for each element in the source collection. It does not flatten the results.

The type of the resulting collection depends on the type of the source collection. If the expression itself returns a collection, the result is therefore a collection of collections.

Example: keep each person's children together

Assume a model has a class Person with a children reference. The following data exists:

  • person1.children = {James, Jane}
  • person2.children = {John}

Evaluate children for every person:

Set{person1, person2}->collectNested(person | person.children)

The result preserves the result for each person as a separate nested collection:

{{James, Jane}, {John}}

The first nested collection is the children of person1; the second is the children of person2. Use this form when the grouping by source person matters—for example, when you need to keep each parent's children associated with that parent.

The example uses a Set to create the source collection. A Set contains no duplicate elements.

When to use collectNested

Use collectNested when:

  • Your expression returns a collection and you need to retain that collection for each source element.
  • You need the result structure to reflect the source structure.
  • You want to distinguish {James, Jane}, the children of one person, from {John}, the children of another person.

Find operators in the OCL Editor

To inspect the operators available for a type, open the OCL Editor and type the class. See Documentation:OCL General Operators for this workflow.

See also