Functions

From webCoRE Wiki - Web-enabled Community's own Rule Engine
Revision as of 18:42, 6 May 2017 by Ahndee (talk | contribs) (Fix descriptions for ceil & floor, adding examples)
Jump to: navigation, search
Share on FacebookShare on TwitterShare on Google+Share on LinkedInShare on DiggShare on deliciousShare on redditShare on StumbleUpon

Functions are extensions to expressions that allow data processing and conversion. Here is a list of all defined functions


String functions

concat

Syntax
string concat(dynamic value1, dynamic value2[, .., dynamic valueN])
Returns
Returns a string that is the concatenation of all the supplied values. This function accepts two or more input values.

contains

Syntax
boolean contains(string haystack, string needle)
Returns
Returns true if the needle is found anywhere in the haystack

endsWith

Syntax
boolean endsWith(string haystack, string needle)
Returns
Returns true if haystack ends with the needle

format

Syntax
string format(string formatString[, dynamic value1[, .., dynamic valueN]])
Returns
Returns a string that is built based on the formatString with arguments replaced by their corresponding values. Follows the java syntax for printf(). See a quick reference here, or a more detailed reference Reference here.
Each % in the format string represents an argument and corresponds to a value (in order of appearance) in the value list. Each argument can have formatting settings, according to the reference provided.
Example
format('The temperature outside is %.2f degrees Fahrenheit', [Temp sensor:temperature]) outputs The temperature outside is 71.26 degrees Fahrenheit
The %.2f translates to "format as float and use .2 decimal places.

indexOf

Syntax
integer indexOf(string haystack, string needle)
Returns
Returns the character index of the first occurrence of needle inside haystack
Examples
indexOf("hello world", 'l') outputs 2

lastIndexOf

Syntax
integer lastIndexOf(string haystack, string needle)
Returns
Returns the character index of the last occurrence of needle inside haystack
Examples
lastIndexOf("hello world", 'l') outputs 9

left

Syntax
string left(string value, integer length)
Returns
Returns the first length characters in value
Examples
left("hello world", 2) outputs he

lower

Syntax
string lower(string value)
Returns
Returns the value but with all lower case letters.
Examples
lower("Hello World") outputs hello world

mid

Syntax
string mid(string value, integer start[, integer count = remaining])
Arguments
start - provides the 0-based index of the first character to copy. If start is negative, the counting starts from the end of value.
count - provides the number of characters to copy from value. If not provided, all of the remaining characters from value are returned, starting with character number start. If count is negative, prior characters are returned.
Returns
Returns a substring of value, count characters long, starting at character number start. Note that the first character in any string has index 0.
Examples
mid("Hello World", 2, 3) outputs llo
mid("Hello World", 2) outputs llo World
mid("Hello World", -5, 2) outputs Wo
mid("Hello World", -1, -3) outputs orl

replace

Syntax
string replace(string haystack, string needle1, string replacement1[, .., string needleN, string replacementN])
Returns
Replaces all the occurrences of needle inside haystack with its respective replacement. Multiple pairs of needle/replacement can be provided in one function call.
Regular expressions
Needles support regular expressions when their value starts and ends with a /. For example, '/(\s)/' will match all spaces.
Examples
replace("Hello World", 'World', 'Earth') outputs Hello Earth
replace("Hello World", 'World', 'Earth', 'Hello', 'Hi') outputs Hi Earth
replace("Hello World", 'World', 'Earth', 'Earth', 'Planet') outputs Hello Planet

right

Syntax
string right(string value, integer length)
Returns
Returns the last length characters in value
Examples
right("hello world", 4) outputs orld

startsWith

Syntax
boolean startsWith(string haystack, string needle)
Returns
Returns true if haystack starts with the needle

string

Syntax
string string(dynamic value)
Returns
Returns value as a string.

substr

Alias of mid()

substring

Alias of mid()

title

Syntax
string title(string value)
Returns
Returns the value but in its title case format (each word starts with a capitalized letter).
Examples
lower("Hello there world") outputs Hello There World

upper

Syntax
string upper(string value)
Returns
Returns the value but with all upper case letters.
Examples
lower("Hello World") outputs HELLO WORLD

text

Alias of string()

Numeric functions

avg

Syntax
decimal avg(decimal value1, decimal value2[, .., decimal valueN])
Returns
Returns the mean average of all the values provided.

ceil

Syntax
integer ceil(decimal value)
Returns
Returns the immediately higher integer, equivalent to rounding the decimal away from zero.
Examples
ceil(5.6) outputs 6
ceil(-5.6) outputs -6

ceiling

Alias of ceil

decimal

Syntax
decimal decimal(dynamic value)
Returns
Converts value into a decimal. Returns 0 if value is not numeric.

float

Alias of decimal

floor

Syntax
integer ceil(decimal value)
Returns
Returns the integer part of a decimal, equivalent to rounding the decimal towards zero.
Examples
floor(5.6) outputs 5
floor(-5.6) outputs -5

max

Syntax
dynamic max(dynamic value1, dynamic value2[, .., dynamic valueN])
Returns
Returns the maximum of all the values provided. Note: this function works with both numbers and strings.

median

Syntax
dynamic median(dynamic value1, dynamic value2[, .., dynamic valueN])
Returns
Returns the median average of all the values provided. Note: this function works with both numbers and strings.

min

Syntax
dynamic min(dynamic value1, dynamic value2[, .., dynamic valueN])
Returns
Returns the minimum of all the values provided. Note: this function works with both numbers and strings.

number

Alias of decimal

power

Syntax
decimal power(decimal value, decimal exponent)
Returns
Returns the value at the power of exponent

round

Syntax
decimal round(decimal value[, integer precision = 0])
Arguments
precision - the number of decimal places to be retained. If not provided, defaults to 0
Returns
Returns the rounded value with precision decimal places.

sqr

Syntax
decimal sqr(decimal value)
Returns
Returns the squared value, equivalent to value multiplied by itself.

sqrt

Syntax
decimal sqrt(decimal value)
Returns
Returns the square root of value.

stdev

Syntax
decimal stdev(decimal value1, decimal value2[, .., decimal valueN])
Returns
Returns the standard deviation of all the values provided.

sum

Syntax
decimal sum(decimal value1, decimal value2[, .., decimal valueN])
Returns
Returns the sum of sum of all the values provided.

variance

Syntax
decimal variance(decimal value1, decimal value2[, .., decimal valueN])
Returns
Returns the variance of all the values provided.

Boolean functions

bool

Syntax
bool(dynamic value)
Returns
Returns the truth value of the input. Accepts anything as input and will return true if value is either a non-zero number, a non-empty string (with some exceptions, see below), a non-empty device list, a non-empty date/time/datetime

boolean

This is an alias of bool

eq

Syntax
eq(dynamic value1, dynamic value2)
Returns
Returns true if value1 is equivalent to value2

ge

Syntax
ge(dynamic value1, dynamic value2)
Returns
Returns true if value1 is greater than or equal to value2

gt

Syntax
gt(dynamic value1, dynamic value2)
Returns
Returns true if value1 is greater than value2

isBetween

Syntax
isBetween(dynamic value, dynamic startValue, dynamic endValue)
Returns
Returns true if value is greater then or equal to startValue and less than or equal to endValue

isEmpty

Syntax
isEmpty(dynamic value)
Returns
Returns true if value is not set, an empty string, or zero

le

Syntax
le(dynamic value1, dynamic value2)
Returns
Returns true if value1 is less than or equal to value2

lt

Syntax
lt(dynamic value1, dynamic value2)
Returns
Returns true if value1 is less than value2

not

Syntax
not(dynamic value)
Returns
Returns the negated truth state of value. This is the logical negation of bool(dynamic value).

Date and time functions

addDays

Syntax
addDays(datetime value, integer days)
Returns
Returns a datetime that is days days after the value. For negative values of days, it returns a datetime that is days days before value.

addHours

Syntax
addHours(datetime value, integer hours)
Returns
Returns a datetime that is hours hours after the value. For negative values of hours, it returns a datetime that is hours hours before value.

addMinutes

Syntax
addMinutes(datetime value, integer minutes)
Returns
Returns a datetime that is minutes minutes after the value. For negative values of minutes, it returns a datetime that is minutes minutes before value.

addSeconds

Syntax
addSeconds(datetime value, integer seconds)
Returns
Returns a datetime that is seconds seconds after the value. For negative values of seconds, it returns a datetime that is seconds seconds before value.

addWeeks

Syntax
addWeeks(datetime value, integer weeks)
Returns
Returns a datetime that is weeks weeks after the value. For negative values of weeks, it returns a datetime that is weeks weeks before value.

date

Syntax
date(datetime value)
Returns
Returns the date portion of value by stripping off time information.

datetime

Syntax
datetime datetime(dynamic value)
Returns
Tries to convert any value into a datetime. Accepts strings in common date/time formats.


formatDuration

Syntax
string formatDuration(datetime value[, boolean friendly = false[, string granularity = 's'[, boolean showAdverbs = false]]])
Arguments
friendly - false makes the output compact, in the form of 0d 00:00:00.000, while true makes the output more human friendly, in the form of 0 days, 0 hours, 0 minutes, and 0 seconds. Defaults to false.
granularity - represents the smallest time unit to be output. One of d for days, h for hours, m for minutes, s for seconds, or ms for milliseconds. Any time unit below the selected granularity will not be output. Defaults to s
showAdverbs - only affects the friendly output, when true the words in (for future durations - positive input) or ago (for past durations - negative input) are prepended or appended respectively to the output
Returns
Returns a string that represents the duration in a human readable format
Examples
formatDuration(12029) will output 00:00:12
formatDuration(68493, true) will output 1 minute and 8 seconds
formatDuration(68493, false, 'ms') will output 00:01:08
formatDuration(68493, true, 'm', true) will output in 1 minute

time

Syntax
time time(datetime value)
Returns
Returns the time portion of value by stripping off date information.


Weather related functions

celsius

Syntax
decimal celsius(decimal value)
Returns
Converts a temperature value from Fahrenheit to Celsius.

dewPoint

Syntax
decimal dewPoint(decimal temperature, decimal relativeHumidity[, string scale = 'F'])
Parameters
scale - optional, one of C or F, corresponding to the temperature range used for temperature. The result will be provided in the same scale. If no scale is provided, the Fahrenheit scale will be used.
Returns
Returns the due temperature for a given temperature and relative humidity

fahrenheit

Syntax
decimal fahrenheit(decimal value)
Returns
Converts a temperature value from Celsius to Fahrenheit.


Dynamic

if(condition, valueIfTrue, valueIfFalse)
evaluates a boolean and returns value1 if true, or value2 otherwise
least(values)
returns the value that is least found a series of numeric values
most(values)
returns the value that is most found a series of numeric values
previousValue([device;attribute])
returns the previous value of the attribute

Integers

age([device;attribute])
returns the number of milliseconds an attribute had the current value
count(values)
calculates the number of true/non-zero/non-empty items in a series of numeric values
int()
//todo
integer(decimal or string)
converts a decimal value to it's integer value
newer([device;attribute],[...], [device;attribute], threshold)
returns the number of devices whose attribute had the current value for less than the specified number of milliseconds
older([device;attribute],[...], [device;attribute], threshold)
returns the number of devices whose attribute had the current value for more than the specified number of milliseconds
previousAge([device;attribute])
returns the number of milliseconds an attribute had the previous value


Time

time(value)
returns the value as a time type
addSeconds(dateTime, seconds)
adds seconds to time, returns the value as a time type
addMinutes(dateTime, minutes)
adds minutes to time, returns the value as a time type
addHours(dateTime, hours)
adds hours to time, returns the value as a time type
addDays(dateTime, days)
adds days to time, returns the value as a time type
addWeeks(dateTime, weeks)
adds weeks to time, returns the value as a time type