Foreach Expressions
Foreach expressions provide collection access. Simple foreach expressions have the following form:
foreach (<identifier-1> : <expression-1>) <identifier-2>(<identifier-3>) {
<identifier-3> = <expression-2>
}
Here is an example:
 
x = foreach (tool : tools) getFirst(t) { _
t = { tool if tool.diameter > 5 null otherwise }
}
 
Such a foreach expression specifies:
Iteration variable (identifier-1, which must be a simple identifier)
Collection (expression-1, which must be collection-valued)
Reduction function (identifier-2, which must name a function)
Formula containing the iteration variable (named by identifier-3)
Evaluation of a foreach expression effectively does the following:
Iterates through the collection elements one at a time, binding each element to the iteration variable in turn. For each iteration, it does this:
o Evaluates the formula, with the current element bound to the iteration variable within the formula.
o Adds the result of the formula evaluation to a results collection.
Applies the reduction function to the results collection, once all elements have been processed, yielding the foreach expression’s final result.
The cost engine uses knowledge of the semantics of the reduction functions to optimize evaluation of foreach expressions, so it does not always actually process every collection element.
CSL supports the following reduction functions:
getFirst: Returns the first non-null element of the results collection.
getAll: Returns the entire results collection.
getAllFlatten: Returns a flattened results collection, that is, returns a collection containing all non-collection elements in the results collection, together with the non-collection elements of any collection elements of the results collection, and so on, recursively.
getMax: Returns the element of the results collection that has the maximum numeric value.
getMin: Returns the element of the results collection that has the minimum numeric value.
getSum : Returns the sum of the elements of the results collection.
getProduct: Returns the product of the elements of the results collection.
getStats: Returns an object that contains one field for each of a variety of statistical properties of the results collection (including mean, standard deviation, minimum, maximum, and count--see http://commons.apache.org/math/apidocs/org/apache/commons/math3/stat/descriptive/DescriptiveStatistics.html)
getObjectWithMax: Returns the value of the iteration variable (an element of the collection being iterated over) associated with the maximum element of the results collection. Consider a foreach expression that evaluates a cycleTime formula for each operation in a collection. While getMax(cycleTime) returns a number, getObjectWithMax(cycleTime) returns the operation with the maximum cycle time.
getObjectWithMin: Returns the value of the iteration variable (an element of the collection being iterated over) associated with the minimum element of the results collection. Similar to getObjectWithMax.
buildGroup
getAllDistinct: Returns the results collection with duplicates removed.
 
Examples:
 
x = foreach (tool : select * from tools where diameter > 5) getFirst(t) { _
t = tool.diameter
}
 
x = foreach (tool : select * from tools where diameter > 5) getFirst(t) { _
t = foreach (t : tool.machines) getAll(q) { q = .... }
}
 
With Clause
Foreach and some if expressions can include auxiliary formulas whose evaluation is required for evaluation of the primary (output) formula:
foreach (<identifier-1> : <expression-1>) <identifier-2>(<identifier-3>) {
with {
formula [
formula]*
}
<identifier-3> = <expression-2>
}
Here is an example:
 
foreach ( t : tools) getAll(f) { _
with { _
z = t.diameter *2
q = select * from t.machines....
r = z * 10
}
f = { t if (r > 5 and q != null) null otherwise }
}