Difference between revisions of "Expressions"

From webCoRE Wiki - Web-enabled Community's own Rule Engine
Jump to: navigation, search
(Introduction)
Line 19: Line 19:
 
==Expression operands==
 
==Expression operands==
 
Expression operands come in different types:
 
Expression operands come in different types:
* literal strings
+
===Literal strings===
*: contain strings, pieces of text that will be output as-is, like that <code>"The sum of 6 and 4 is "</code> above
+
: contain strings, pieces of text that will be output as-is, like that <code>"The sum of 6 and 4 is "</code> above
* numeric values
+
===Numeric values===
*: anything like integer numbers or decimal numbers: <code>6</code> or <code>2.4</code>
+
: anything like integer numbers or decimal numbers: <code>6</code> or <code>2.4</code>
* variables
+
===Variables===
*: the name of any defined variable, local, system, or global, can be simply written and the expression will know to replace that with its actual value, for example <code>$now</code>
+
: the name of any defined variable, local, system, or global, can be simply written and the expression will know to replace that with its actual value, for example <code>$now</code>
* functions
+
===Functions===
*: webCoRE allows functions in expressions, these can help convert values around and extend the power of expressions, for example <code>dewPoint(77, 80)</code>, or <code>round(6.24)</code>
+
: webCoRE allows functions in expressions, these can help convert values around and extend the power of expressions, for example <code>dewPoint(77, 80)</code>, or <code>round(6.24)</code>
* device attributes
+
===Device attributes===
*: use the special construction <code>[name:attribute]</code> or <code>[variable:attribute]</code> to include the attribute value of that device.
+
: use the special construction <code>[name:attribute]</code> or <code>[variable:attribute]</code> to include the attribute value of that device.
* devices
+
===devices===
*: use the special construction <code>[name]</code> or <code>[variable]</code> to point to a device. This is not a device attribute, but the device itself and when converted to string will give the device's name
+
: use the special construction <code>[name]</code> or <code>[variable]</code> to point to a device. This is not a device attribute, but the device itself and when converted to string will give the device's name
  
 
==Expression operators==
 
==Expression operators==
 +
webCoRE supports over 20 operators, with various meanings. Operators generally sit in between two operands and determines the operation that is performed on the two. When only one operand exists, the other may be automatically implied, as described below. Another important factor is the order of operations, also explained below.
 +
 +
For each of the operators explained below, we are assuming we have two operands around it, <code>a</code> <code>operator</code> <code>b</code>. For simplicity, we will be referring to the two operands simply as <code>a</code> and <code>b</code>.
 +
 +
===Addition (<code>a + b</code>)===
 +
Performs the mathematical addition of <code>a</code> plus <code>b</code>. If any of <code>a</code> or <code>b</code> is a string, concatenation is performed, so <code>"test" + 1</code> will result in <code>test1</code>, while <code>6 + 4</code> will result in <code>10</code>. If <code>a</code> and <code>b</code> are device-typed variables, the result will be a new list with all the devices present in either <code>a</code> or <code>b</code>.
 +
 +
===Subtraction (<code>a - b</code>)===
 +
Performs the mathematical subtraction of <code>a</code> minus <code>b</code>. If <code>a</code> and <code>a</code> are device-typed variables, the result will be the list of devices in <code>a</code>, but excluding any devices that exist in <code>b</code>. Otherwise, both <code>a</code> and <code>b</code> will be evaluated as numbers and subtracted.
 +
 +
===Multiplication (<code>a * b</code>)===
 +
Performs the mathematical multiplication of <code>a</code> times <code>b</code>. Both <code>a</code> and <code>b</code> will be evaluated as numbers and multiplied.
 +
 +
===Division (<code>a / b</code>)===
 +
Performs the mathematical division of <code>a</code> divided by <code>b</code>. Both <code>a</code> and <code>b</code> will be evaluated as numbers and multiplied. If any of the two operands is a decimal, the result will be a decimal. If both operands are integers, the result will be an integer.
 +
 +
===Integer division(<code>a \ b</code>)===
 +
Performs the mathematical division of <code>a</code> divided by <code>b</code>. Both <code>a</code> and <code>b</code> will be evaluated as numbers and divided, then the interger part of the result will be returned.

Revision as of 13:17, 4 May 2017

Introduction

An expression is a complex mathematical construction built using operands and operators. The simplest of expressions can have one operand, returning its value: 6 or $now. Generally, expressions contain multiple operands separated by operators. The operands represent the values, while the operators provide the operations to be performed on these values. Here's a simple example of a two-operand expression: 6 + 4. In this example, there are two operands, 6 and 4 and one operator, +, the mathematical addition operator. The result of this expression is obviously 10.

Using an expression

When building a webCoRE piston, there are several ways values can be provided within piston operands. The operand type selection allows for several types, among which the Value and the Expression types. When a Value type is selected, a Value field is displayed right next to the operand type selector. When an Expression type is selected, an Expression field is displayed underneath the selector and provides multiple line editing and autocomplete suggestions. Expressions can be used directly in an Expression field, but they can also be embedded into Value fields, by using the curly brackets {<expression>}. This allows for quick use of expressions while building, say, an output string:

The sum of 6 and 4 is {6 + 4}

webCoRE will parse anything that is between curly brackets as expressions and handle them as such.

Values versus Expressions

Which one you end up using is really a matter of choice, they can both do the same exact thing, although they differ from each-other. A Value field is an output-driven field, meaning it makes it easy to build output strings that need to be displayed in logs, notifications, etc. Think of a Value field like an Expression field that has implied double quotes (") at the start and end. In reverse, an Expression field is similar to a Value field that has curly brackets around it. Here's an example:

We can convert this Value field : The sum of 6 and 4 is {6 + 4} into an Expression field by simply placing double quotes around it: "The sum of 6 and 4 is {6 + 4}"
We can convert this Expression field : "The sum of 6 and 4 is " + (6 + 4) into a Value field by simply placing curly brackets around it: {"The sum of 6 and 4 is " + (6 + 4)}

All four examples above are equivalent and will output the same result: The sum of 6 and 4 is 10

Expression operands

Expression operands come in different types:

Literal strings

contain strings, pieces of text that will be output as-is, like that "The sum of 6 and 4 is " above

Numeric values

anything like integer numbers or decimal numbers: 6 or 2.4

Variables

the name of any defined variable, local, system, or global, can be simply written and the expression will know to replace that with its actual value, for example $now

Functions

webCoRE allows functions in expressions, these can help convert values around and extend the power of expressions, for example dewPoint(77, 80), or round(6.24)

Device attributes

use the special construction [name:attribute] or [variable:attribute] to include the attribute value of that device.

devices

use the special construction [name] or [variable] to point to a device. This is not a device attribute, but the device itself and when converted to string will give the device's name

Expression operators

webCoRE supports over 20 operators, with various meanings. Operators generally sit in between two operands and determines the operation that is performed on the two. When only one operand exists, the other may be automatically implied, as described below. Another important factor is the order of operations, also explained below.

For each of the operators explained below, we are assuming we have two operands around it, a operator b. For simplicity, we will be referring to the two operands simply as a and b.

Addition (a + b)

Performs the mathematical addition of a plus b. If any of a or b is a string, concatenation is performed, so "test" + 1 will result in test1, while 6 + 4 will result in 10. If a and b are device-typed variables, the result will be a new list with all the devices present in either a or b.

Subtraction (a - b)

Performs the mathematical subtraction of a minus b. If a and a are device-typed variables, the result will be the list of devices in a, but excluding any devices that exist in b. Otherwise, both a and b will be evaluated as numbers and subtracted.

Multiplication (a * b)

Performs the mathematical multiplication of a times b. Both a and b will be evaluated as numbers and multiplied.

Division (a / b)

Performs the mathematical division of a divided by b. Both a and b will be evaluated as numbers and multiplied. If any of the two operands is a decimal, the result will be a decimal. If both operands are integers, the result will be an integer.

Integer division(a \ b)

Performs the mathematical division of a divided by b. Both a and b will be evaluated as numbers and divided, then the interger part of the result will be returned.