Generate occurrences from occurrences

In section Go through occurrences you learned how to go through a set of occurrences. In section Generate a fixed number of occurrences you learned how to generate as many occurrences as needed to meet the cardinalities defined in the output. Now, discover a tool that combines these two functions, going through a set of occurrences and generating as many as detected.

To tell the integration engine to generate while covering a set of occurrences, you use the for each statement. This statement behaves exactly similar to the times statement, except you specify a list of paths that address the nodes in the inputs to go through, instead of just a number. While covering occurrences, the path to the current node is referred to via this, just similar to in loop…on loops.

The common use of a for each statement is to copy a group that occurs more than one time, just having a group member changed. Consider the following Business Document:

invoice         1..1
   line         1..n
     item_id    1..1, class s
     quantity   1..1, class i
     unit_price 1..1, class r
     amount     1..1, class r

When fed as input to the integration engine, the amount of each line is filled with absent. What you want to accomplish with a DML Block is to fill this field with the appropriate value.

For that task, input and output Business Documents are the same. You create a Map that copies all the input to the output, except for the amount that should be correctly filled. The following is an example of the Map:

Output node Bound expression

invoice

 

line

for each \\line

item_id

this\item_id

quantity

this\quantity

unit_price

this\unit_price

amount

this\quantity * this\unit_price

While processing the expression bound to the output node line, the integration engine counts the number of occurrences of line in the input and creates that number of occurrences of line in the output.

Each time an occurrence of line in the output is created, the integration engine creates each member of the output group line using the corresponding bound expression. As a result, the members item_id, quantity, and unit_price are copied from the input. Notice the use of this to refer to the current input node line. The output value amount is computed using values unit_price and quantity retrieved from the input.

The following graphic illustrates the tasks, with example data values illustrated:

Example data values

The for each … do statement extends the foreach statement the same way timesdo extends times. If the output node lines has been defined as having a value that numbers the lines, you append the same do as you did when numbering groups.

Unlike the loop on statement, for each is not a real loop. This is why break and next instructions cannot be used to interfere with the occurrences coverage. However, you can use ambiguous paths and filters to define the set of node occurrences you cover. In addition, more than one path can be stated to enlarge the set of nodes and occurrences that you want to go through.

All cardinalities and loop statements (loop, loop on, while, times and for each) can be nested and mixed as you require. As long as you address existing nodes and bind expressions appropriately, you are free to use these statements. The reserved word this always refers to the nearest loop on or for each.

When nesting for each and/or loop on, you can use a path starting with this in an inner for each and/or loop on. This means that the inner goes through something related to the current node or occurrence of the outer.

Related Links