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 * from part.childArtifacts x where isTurningAxis(x)
 
This query expression designates a collection that includes 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 intermediate result. (isTurningAxis, here, is a function that returns true if and only if the argument, x, is a turning axis.)
Here, * indicates that the query should return a collection consisting of each selected element in its entirety. Queries can also return a collection of just the values of a specified field of the selected elements. Here is an example:
 
select 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 the intermediate result; the final result is the collection of the names of each element of the intermediate result.
Query expressions can also include functions that return a specified element of the intermediate results (such as the first or last element) or perform some form of aggregation of the intermediate results (such as the count, or number of elements of the intermediate result). 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 functions on intermediate results:
first: returns the first element of the intermediate query results.
last: returns the last element of the intermediate query results.
sum: returns the sum of the values in the intermediate query results.
min: returns the smallest value in the intermediate query results.
max: returns the largest value in the intermediate query results.
count: returns the number of elements in the intermediate query results.
distinct: returns only distinct elements, eliminating duplicates; so, for example, if the intermediate results include 3, 4, 4, 5, 5, 5, 6, 7, the final result will include only 3, 4, 5, 6, 7.
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 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. The 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 results 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 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.