Generic Functions in QPR ProcessAnalyzer: Difference between revisions
Line 432: | Line 432: | ||
eventLog.Cases:GetAt(0, Events).RecursiveFind(NextInCase, Organization=="Finance", true, true) | eventLog.Cases:GetAt(0, Events).RecursiveFind(NextInCase, Organization=="Finance", true, true) | ||
Returns: Both return for each case all events whose organization equals to "Finance". | Returns: Both return for each case all events whose organization equals to "Finance". | ||
</pre> | </pre> | ||
|} | |} |
Revision as of 15:14, 5 April 2020
Aggregation functions
There are following aggregation functions: Average, Count, Median, Min, Max, Percentile and Sum. The aggregation operations work for numbers, DateTimes and TimeSpans, except Sum for DateTime is not possible.
Examples:
Sum([3, 2, 4]) Returns: 9 Count([[1, 2], [3, 4, 5]]) Returns: [2, 3] Sum([[1, 2], [3, 4, 5]]) Returns: [3, 12] Sum([]) Returns: null
When aggregating numerical values and there are null values, nulls are treated as zeros. When aggregating DateTimes and TimeSpans, and there are null values, error is given. In the Min and Max functions, nulls are ignored. Null values can be removed before aggregating (the following example shows how).
Average([1, 5, null]) Returns: 2 Average([1, 5, null].(_ ?? _remove)) Returns: 3
Aggregation operations are performed to the leaf level. i.e. the deepest level in the hierarchy. When aggregating, the leaf level arrays are replaced by the aggregated values, and thus the depth of the hierarchy decreases by one. In addition to the aggregation functions, functions that modify the contents of leaf arrays (OrderBy, Distinct, ...), the operation will be performed separately for every leaf array.
OrderByValue([[4, 3], [2, 1]]) Returns: [[3, 4], [1, 2]]
Mathematical functions
Function | Parameters | Description |
---|---|---|
ArgMax |
|
Returns those objects in the array giving maximum values for the given expression when evaluated in the object's context. Examples: ArgMax([5, 1, 6, 4, 3, 3, 6, 5, 4, 1, 1], _) Returns: [6, 6] ArgMax(EventLogById(1).Cases, Duration) Returns: An array of cases having the longest duration. |
ArgMin |
|
Returns those objects in the array giving minimum values for the given expression when evaluated in the object's context. Examples: ArgMin([5, 1, 6, 4, 3, 3, 6, 5, 4, 1, 1], _) Returns: [1, 1, 1] ArgMin(EventLogById(1).Cases, Duration) Returns: An array of cases having the shortest duration. |
Ceiling (Integer) |
|
Returns the smallest integer that is greater than or equal to the specified number. Example: Ceiling(1.3) Returns: 2 |
Floor (Integer) |
|
Returns the largest integer that is less than or equal to the specified number. Example: Floor(1.9) Returns: 1 |
Round (Float) |
|
For numbers, rounds the number to the specified number of decimal. Number of decimals can also be negative, and then the number is rounded to the nearest tens, hundreds, thousands, etc. For DateTimes, rounds given date time by given time span or given number of seconds. Note that the number/DateTime to be rounded needs to be provided as a context object (not as a parameter). Examples: (1.254).Round(1) Returns: 1.3 (162.111111).Round(-2) Returns: 200 DateTime(2017, 1, 2, 3, 4, 5).Round(10) Returns: DateTime(2017, 1, 2, 3, 4, 10) DateTime(2017, 1, 2, 3, 4, 5).Round(TimeSpan(1)) Returns: DateTime(2017, 1, 2) |
Conversion functions
Function | Parameters | Description |
---|---|---|
ToFloat (Number) |
Object to convert |
Converts given object to a decimal number. |
ToInteger (Integer) |
Object to convert |
Converts given object into an integer. If the object can not be converted into an integer, an exception will be thrown. If the object as a decimal number, it will also be rounded to the nearest integer. Examples: ToInteger(1.234) Returns: 1 ToInteger(-1.5) Returns: -2 ToInteger(1313.6) Returns: 1314 ToInteger(123456789012345678) Returns: 123456789012345678 ToInteger("5") Returns: 5 ToInteger("-5") Returns: -5 |
ToString (String) |
Object to convert |
Converts given object to a string. Examples: ToString(1) + " < " + ToString(2) Returns: "1 < 2". "Case=\'" + ToString(a) + "\'" Returns: "Case='<string representation of object a>'" ToString(DateTime(2017,1,2,3,4,5,6)) Returns: "2017-01-02T03:04:05" |
Ordering functions
Function | Parameters | Description |
---|---|---|
OrderBy (array) |
|
Orders the given array using values from the given order expression. The order expression is evaluated once for each item in the array. The order expression supports all atomic (=not collection) primitive value types. Examples: OrderBy(["a", "x", "b", "z", "n", "l", "a"], _) Return: ["a", "a", "b", "l", "n", "x", "z"] OrderBy([9,8,7,6,5,4,3,2,1], _%3) Returns: [9,6,3,7,4,1,8,5,2] OrderBy([9,8,7,6,5,4,3,2,1], _%3 + _/30) Returns: [3,6,9,1,4,7,2,5,8] |
OrderByDescending (array) |
|
Result is same as in the OrderBy function, except the order is reverse. |
OrderByTop (array) |
|
Orders given top-level array by the value of given expression using ascending order. Supports all value types, including multi-dimensional arrays. Order expression is evaluated in the context of each array item whose value determines the order of that item. Examples: OrderByTop([[1, 2, 3], [2, 2, 2], [3, 2, 1]], _[2]) Returns: [[3, 2, 1], [2, 2, 2], [1, 2, 3]] |
OrderByValue (array) |
|
Orders the given array using the natural order of items, for example numbers it's the increasing order, and for strings it's text ordering. OrderByValue(["a", "x", "b", "z", "n", "l", "a"]) Return: ["a", "a", "b", "l", "n", "x", "z"] |
OrderByValueDescending (array) |
|
Result is same as in the OrderByValue function, except the order is reverse. |
Looping functions
Function | Parameters | Description |
---|---|---|
For |
|
Iterates the given expression until the given condition is met, and returns the results of the iterated expressions for every iteration as an array. Examples: For("i", 0, i < 4, i + 1, i) Returns: [0, 1, 2, 3] For("x", 0, x < 3, x + 1, For("y", 0, y < 3, y + 1, StringJoin(",", [x, y])) Returns: [["0,0", "0,1", "0,2"], ["1,0", "1,1", "1,2"], ["2,0", "2,1", "2,2"]] For("i", 0, i < 4, i + 1, DateTime(2010 + i)) Returns: [DateTime(2010), DateTime(2011), DateTime(2012), DateTime(2013)] For("str", "a", str != "aaaaa", str + "a", str) Returns: ["a", "aa", "aaa", "aaaa"] |
ForEach |
|
Repeats the given expression as many times there are items in the given array. Item in the array is available as the given variable in the expression. Examples: ForEach("i", [1,2,3], "Value is: " + i) Returns: Value is: 1 Value is: 2 Value is: 3 ForEach("item", EventLogById(1).Cases, Let("c" + item.Name, item)) Results: Creates expression variable variables like "c<casename>" for every case in the model. ForEach("myVariable", ["a", "b", "c"], myVariable + myVariable) Returns: aa bb cc |
NumberRange |
|
Creates an array of numbers within given range with the given interval. Interval parameter is optional, and by default it is one. Examples: NumberRange(1, 3) Returns: [1, 2, 3] NumberRange(1, 3, 0.5) Returns: [1, 1.5, 2, 2.5, 3] NumberRange(1, 3, 0.8) Returns: [1, 1.8, 2.6] |
Repeat |
|
Repeats the defined expression the defined number of times. Examples: Repeat(3, "Repeat me!") Returns: "Repeat me!" "Repeat me!" "Repeat me!" Repeat(1, 5) Returns 5 |
TimeRange |
|
Generates a timestamp array starting from the start timestamp with the defined interval, and ending when the end timestamp is reached. Note that this function only creates timestamps with equal durations, so it's not possible to e.g. create timestamps for each month (to do that, you can use the loops). Generate datetimes starting from Monday 2017-01-01 and ending to Monday 2017-12-31 including all Mondays between them: Timerange(Datetime(2018,1,1), Datetime(2018,1,1), Timespan(7)) |
Recursion functions
Function | Parameters | Description |
---|---|---|
Recurse |
|
Evaluates the given expression recursively until given condition or recursion depth is met. The function returns all the traversed objects in a single array. When the stop condition expression evaluates to false, it will stop the current recursion without including the false evaluated object into the result. Default stop condition is !IsNull(_). The default maximum depth is 1000. Examples: (1).Recurse(_ + 1, _ < 5) Returns: [1, 2, 3, 4] event.Recurse(NextInCase) Returns: An array of all the events following given event inside the same case. event.Recurse(NextInCase, Type.Name != "Invoice") Returns: An array of all the events following given event inside the same case until event whose type name is "Invoice", which will itself not be included into the result. event.Recurse(NextInCase, Type.Name != "Invoice", 2) Returns: An array of all the events following given event inside the same case until event whose type name is "Invoice" or until the recursion depth of 2 has been reached, which will itself not be included into the result. |
RecurseWithHierarchy |
|
Evaluates the given expression recursively until given condition or recursion depth is met. The function returns the traversed object hierarchy. When the stop condition expression evaluates to false, it will stop the current recursion without including the false evaluated object into the result. Default stop condition is !IsNull(_). The default maximum depth is 1000. Examples: [1,2].RecurseWithHierarchy([1,2], false, 2) Returns: [1:[1:[1,2],2:[1,2]],2:[1:[1,2], 2:[1,2]]] (1).RecurseWithHierarchy(_ + 1, _ < 5) Returns: 1:[2:[3:[4]]] RemoveLeaves(eventLog.Flows:From.Where(IsNull(_))).To.RecurseWithHierarchy(OutgoingFlows.To, !IsNull(_), 2) Returns: A hierarchy consisting of all the starter events of given event log and recursively all the event types reachable from them via flows until depth of 2 is reached. |
RecursiveFind |
|
Evaluates given expression recursively until given condition or recursion depth is met. The function collects all the traversed objects that match the given find expression along the way. When the find condition expression evaluates to true for the current object, it causes the following:
When the stop condition expression evaluates to false, it will stop the current recursion without including the false evaluated object into the result. Default stop condition is !IsNull(_). The default maximum depth is 1000. Continue after finding tells should the recursion be continued after a match has been found in the current branch. Examples: (1).RecursiveFind(_ + 1, _ == 100) Returns: 100 eventLog.Cases:GetAt(0, Events).RecursiveFind(NextInCase, Organization=="Finance", !IsNull(_)) Returns: For each case, returns the first (by time) event whose organization equals to "Finance". eventLog.Cases:Events.Where(Organization=="Finance") eventLog.Cases:GetAt(0, Events).RecursiveFind(NextInCase, Organization=="Finance", true, true) Returns: Both return for each case all events whose organization equals to "Finance". |
Hierachical object functions
Function | Parameters | Description |
---|---|---|
FindRepeats |
|
Searches for repeating patterns in given array. Parameters:
Returns an array of arrays indicating the repeating patterns and their positions in the array formatted as follows:
Examples: FindRepeats([0, 1, 2, 0, 1, 2]) Returns: [ [[0, 1, 2], [0, 3]], [[0, 1], [0, 3]], [[1, 2], [1, 4]], [[0], [0, 3]], [[1], [1, 4]], [[2], [2, 5]] ] FindRepeats([0, 1, 2, 0, 1, 2], 0, true) Returns: [[[0, 1, 2], [0, 3]]] FindRepeats([1, "b", DateTime(2017), 1, "b", DateTime(2017)], 3) Returns: [[1, "b", DateTime(2017)], [0, 3]] |
Flatten |
|
Collects all the actual leaf values from given array, array of arrays or hierarchical object and returns them in a single array. If given a hierarchical object, this function collects actual leaf values instead of leaf level values. Elements in the returned array are in the same order they were found when traversing the input object using depth first search. When context object is included, should context objects in the internal nodes of a hierarchical object be also included into the result. Default is false. Examples: Flatten(1) Returns: 1 Flatten([1, 2]) Returns: [1,2] Flatten([[[1, 2],[3, 4]],[[5, 6]]]) Returns: [1, 2, 3, 4, 5, 6] Flatten([[1, 2], 3]) Returns: [1, 2, 3] Flatten([[1,2,3,4], null, [5], [1, null]]) Returns: [1, 2, 3, 4, null, 5, 1, null] Flatten(["a":1, "b":2]) Flatten(["a":1, "b":2], false) Returns: [1, 2] Flatten(["a":1, "b":2], true) Returns: ["a", 1, "b", 2] |
RemoveLeaves |
|
Remove one level of hierarchy from a hierarchical object replacing all the bottom level hierarchical arrays with the context objects of the hierarchical arrays. Returns the given object with all the bottom level hierarchical arrays with the context objects of the hierarchical arrays. Examples: RemoveLeaves(["foo":1, "bar":2]) Returns: ["foo", "bar"] RemoveLeaves(eventLog.Flows:From.Where(IsNull(_))) Results: All the flows starting the cases in the event log. |
ReplaceLeafValues |
|
Replace all leaf values of given array or hierarchical object at given levels up from the leaf level with results of given expression. Examples: ReplaceLeafValues([1,2, null], "x", If(IsNull(x), null, x+1), 0) Result: [2, 4, null] ReplaceLeafValues([[[1,2],[2,3]],[[3,4],[4,5]]], "x", Flatten(x), 0) Result: [[[[1],[2]],[[2],[3]]],[[[3],[4]],[[4],[5]]]] ReplaceLeafValues([[[1,2],[2,3]],[[3,4],[4,5]]], "x", Flatten(x), 1) Result: [[[1,2],[2,3]],[[3,4],[4,5]]] ReplaceLeafValues([[[1,2],[2,3]],[[3,4],[4,5]]], "x", Flatten(x), 2) Result: [[1,2,2,3],[3,4,4,5]] ReplaceLeafValues([[[1,2],[2,3]],[[3,4],[4,5]]], "x", Flatten(x), 3) Result: [1,2,2,3,3,4,4,5] |
SliceMiddle |
|
Extracts a continuous range of an array or hierarchical object. If given a hierarchical object, applies the function at the level that is specified levels level up from the leaf level. Start index: Negative value means that the index is counting from the end of the array. If array does not have element at this given index, empty array is returned. End index: not included into the extracted range. Negative value means that the index is counting from the end of the array. If array does not have element at this given index, all the elements to the end from the start index will be extracted. Levels up: At which level of the hierarchical object are we operating (number of levels up from the leaf level). Should be at least 1 (since 0 level does not contain arrays). Examples: SliceMiddle(1, 2, 1, [[0, 1, 2, 3], [4, 5, 6, 7]]) Returns: [[1], [5]] SliceMiddle(2, 4, 1, [[0, 1, 2, 3], [4, 5, 6, 7]]) Returns: [[2, 3], [6, 7]] SliceMiddle(0, 1, 2, [[0, 1, 2, 3], [4, 5, 6, 7]]) Returns: [0, 1, 2, 3] SliceMiddle(3, 5, 1, [0, 1, 2, 3, 4, 5, 6, 7]) Returns: [3, 4] SliceMiddle(-3, -1, 1, [0, 1, 2, 3, 4, 5, 6, 7]) Returns: [5, 6] SliceMiddle(3, -1, 1, [0, 1, 2, 3, 4, 5, 6, 7]) Returns: [3, 4, 5, 6] |
Variable handling functions
Function | Parameters | Description |
---|---|---|
Def |
|
Creates a new user defined function. Parameters starting from the second, are the parameters that user gives when calling the function. The last parameter is the expression to evaluate when the function is called. In that definition expression the named parameters are used. Instead of the Def function, a recommended way to define functions is the lambda syntax. Examples: Def("Inc", "a", a + 1); Inc(2); Returns: 3 Def("Add", "a", "b", a + b); [1, 2, 3].Add(_, 2); Returns: [3, 4, 5] Def("AddSelf", "b", _ + b); [1, 2, 3].AddSelf(2); Returns: [3, 4, 5] Def("Fib", "a", If(a < 2, 1, Fib(a - 1) + Fib(a - 2))); Fib(10); Returns: 89 |
Let |
|
Defines a user defined variable to the scope where the Let function is located. Note that the function name needs to starts with a capital letter, because otherwise it's interpreted as a let operator. Examples: Let("var1", "Orange"); "Value is " + var1; Returns: Value is Orange |
Set |
|
Sets a value for an existing user defined variable. The variable to set must have been created previously in some visible scope. Instead of the Set function, a recommended way to set variables, is the assignment operator. |
Variable |
|
Function to get a variable value. The function is needed when there are spaces in a variable name, because that variable cannot be referenced otherwise in the expressions. |