Expressions
CSL supports the following kinds of expressions:
Advanced users can also use foreach expressions and associative collection access—see Foreach Expressions inCost Scripting Language Reference.
Arithmetic
Arithmetic expressions can be formed using binary arithmetic operators, such as * and /. Here are some examples:
 
(injectTime + coolTime + ejectTime ) / numCavities
 
(material.density * material.costPerKG) / 10^9
 
runnerVolume / ((numCavities * part.volume) + runnerVolume)
 
String
In the message clause of a rule, string expressions can be formed using the string concatenation operator +. Here is an example:
 
m.name + ' is not feasible. The mold base horizontal dimension ' + _'(' + _
roundMoldBaseX + _
'mm ) is greater than the horizontal distance between the tie bars (' + _
maxTieBarDistanceH + 'mm)'
See Example in Machine Selection for this example in context.
Boolean
Boolean expressions can be formed using unary and binary logical operators such as and and not, as well as binary arithmetic comparison operators such as == and <=. Here is an example:
 
(gcd.edgeTypeName == 'ROUND' and not(isFlangedHoleEdge))) or _
(gcd.edgeTypeName == 'CHAMFER' and not(previousCounterSunkEdge)
 
Here gcd is a standard input, and isFlangedHole and previousCounterSunkEdge refer to boolean-valued formulas.
See Example in Process and Operation Optionality for this example in context.
Conditional
A conditional expression evaluates to one of several alternative values, depending on the value of the boolean expressions associated with the alternative values. Here are some examples of formulas that use a conditional expression:
 
waste = { percentRun - regrindInput if percentRun > regrindInput
0 otherwise }
 
partLength = {setup.partX if setup.partX != null part.length otherwise}
 
isFlangedHoleEdge = _
{true if _
gcd.parentArtifact != null and _
gcd.parentArtifact.artifactTypeName == 'SimpleHole' and _
gcd.parentArtifact.isFlanged == true
false otherwise _
}
 
(See Example in Process and Operation Optionality for this last example in context.)
The entire right-hand-side of each of these forumlas is a conditional expression. The conditional expression in the first formula designates the value of percentRun - regrindInput if percentRun is greater than regrindInput; otherwise it designates 0.
CSL supports an alternative, equivalent form for conditional expressions. Here is an example:
 
costingMessage = {
if (safeEval(costingMessage.severity.errorOrWarning,false) == false) {
null
} elseif (vars.results.currentCostSummary.preferredErrorMessage == null) {
null
} else {
msg('<html><b style="color:red">', longDescriptionString, '</b>')
}
}
 
A conditional expression can have multiple if or else if lines. It designates the value associated with the first boolean-expression that evaluates to true. If no boolean-expression evaluates to true, the conditional expression designates the value associated with the otherwise or else.
When a condition evaluates to true, the conditions that follow it are not evaluated. So the order of the conditions can affect performance. The first conditions should be those that are most likely to evaluate to true or that are inexpensive to evaluate.
Function Invocation
A function invocation expression evaluates to the result of substituting the invocation’s actual parameters for the formal parameters in the corresponding function definition. Here are some examples of formulas that invoke functions:
 
laborCost = GetLaborCost(laborTime, laborRate)
 
laborTime = _
GetLaborTime_PlasticMolding_InjectionMolding( _
processTime, _
cycleTime, _
numOperators, _
laborTimeStandard _
)
 
The entire right hand side of both these formulas is a function invocation.
Query
A query expression retrieves or aggregates collection elements or values. These expressions have essentially the same semantics as SQL queries. The most basic kind of query expression designates a collection that has those elements of the queried collection that meet a specified condition. Such a query expression has the following constituents:
Expression that designates the collection to be queried
Variable that is, in effect, bound to each collection element in turn
Boolean expression that specifies the query’s selection criterion. This expression includes the query variable. The query result includes the elements of the queried collection that meet the selection criterion.
Here is an example:
select all x from part.childArtifacts x where isTurningAxis(x)
This query expression designates a collection that includes all those child GCDs of the current part that are turning axes. It has the following constituents:
part.childArtifacts: designates the collection to be queried.
x: variable that is, in effect, bound to each element of the collection in turn.
isTurningAxis(x): selection condition that determines which collection elements form the query’s final or intermediate result. (isTurningAxis, here, is a function that returns true if and only if the argument, x, is a turning axis.)
Queries can also return a collection of just the values of a specified field of the selected elements. Here is an example:
select all x.name from part.childArtifacts x where isTurningAxis(x)
This query expression designates a collection containing the names of the current part’s turning axes. For this query, the collection of the current part’s turning axes is an intermediate result; the final result is the collection of the names of each element of the intermediate result.
Queries can also use select distinct instead of select all, in order to return a collection that eliminates duplicates. Here is an example:
op_names = select distinct op.name from myOps op
If myOps contains three operations, one named ‘Side Milling’ and two named ‘Facing’, op_names will contain only two names: ‘Side Milling’ and ‘Facing’.
With select distinct, not only are duplicate elements eliminated from the returned collection, but the returned collection is also flattened (that is, the elements that are themselves collections are replaced with their own elements). In other words, with select distinct, one level of nesting is removed from the result collection. For example, consider again the following expression:
op_names = select distinct op.name from myOps op
And suppose that childOps contains two elements:
One element is a collection whose elements are:
o An operation named ‘Facing’
o An operation named ‘Side Milling’
The other element is a collection whose only element is:
o An operation named ‘Facing’.
In this case, op_names will contain only two names: ‘Side Milling’ and ‘Facing’, as in the previous example.
Queries can also use select flatten to return a collection with one level of nesting removed (see above), but with duplicates intact. This form of select is generally recommended for use only when the expression to the immediate left of from is collection valued. Here is an example:
myChildArtifacts = select flatten a.childArtifacts from myArtifacts
Suppose that myArtifacts contains two GCDs: a MultiStepHole (whose children are two SimpleHoles) and a ComboVoid (whose children are a SimpleHole and Void). In this case, myChildArtifacts will be a flat collection containing three SimpleHoles and one Void.
In other cases (when the expression to the immediate left of from is not collection valued), if flattening is required for the expression to the right of from, consider wrapping it in asFlattenedList (see List Functions), as in the following example:
select all x from asFlattenedList(nestedCollection) x where ...
Query expressions can also include aggregation functions, which either return a specified element of intermediate results (such as the first or last) or return some form of aggregation of intermediate results (such as the count or sum). Here is an example:
select count(x) from part.childArtifacts x where isTurningAxis(x)
(See Example in Template Pruning for this example in context.) This query expression designates the number of child GCDs of the current part that are turning axes.
CSL supports the following aggregation functions:
first: returns the first element of the intermediate query results. Returns null if there are no intermediate results.
last: returns the last element of the intermediate query results. Returns null if there are no intermediate results.
only: returns the only element of the intermediate query results. Use this (instead of first or last) when you know that the intermediate results should contain at most a single element. Signals an error (InvaidResultException) if there are multiple intermediate results. Returns null if there are no intermediate results.
sum: returns the sum of the values in the intermediate query results. Returns null if there are no intermediate results.
min: returns the smallest value in the intermediate query results. Returns null if there are no intermediate results.
max: returns the largest value in the intermediate query results. Returns null if there are no intermediate results.
count: returns the number of elements in the intermediate query results. Returns 0 if there are no intermediate results.
exists: returns a boolean value indicating whether the select expression found any matches.
Here is an example that uses the sum function:
sumOpsCycleTime = select sum(operation.cycleTime) from childOps operation
(See Example in Process and Operation Taxonomy for this example in context.)
Here, childOps is a CSL standard input whose value is the collection of the current process or operation’s children in the process-operation hierarchy. Each element of the collection has a field for each output (such as cycleTime) of the element’s associated CSL modules. This query designates the sum of the cycle times calculated by each child operation’s taxonomy module.
Finally, queries can specify a field of the collection elements by which to order the final or intermediate results. The following query specifies that the intermediate results be ordered by the value of workCenterOverheadRate, from lowest to highest:
 
select first(m) from machines m _
where _
ClampForceCheck and _
tieBarHCheck and _
tieBarVCheck and _
moldHeightCheck and _
shotSizeCheck _
order by m.workCenterOverheadRate
 
(See Example in Machine Selection for this example in context.)
The query above designates the machine with the lowest overhead rate (that satisfies the selection criterion). The selection criterion refers to various rules. Note that the query variable (m, in this case) can appear in rules and formulas referenced by the selection criterion.
Note that if no elements of the queried collection meet the selection criteria, the query expression typically evaluates to null (rather than an empty collection). If you need to check a query result to see if it is empty, use the predefined function isEmpty(collection), which returns true if its argument is either null or an empty collection.
For more examples, see Modifying Machine Selection and Adding a Setup Option—Using List Mode to Access a Lookup Table in Common Task Examples. See also Query Expressions in the CSL Reference chapter of this document.