Expressions enable the creation of logical, mathematical and relational statements, which are used to apply rules, perform mathematical calculations on retrieved data, compare model objects and check the feasibility of a statement. Expressions are also used to access field values of model objects and query data in database.
In Altogic, you use the same expression syntax to query your data; in other words, you do not need to learn a separate data querying language (e.g., SQL). If you know how to write a formula in Excel, you have all the basic know-how to write expressions and data queries in Altogic.
The basic expression types implemented by Altogic platform can be summarized as follows:
- Informational (field value retrieval from model objects)
Expressions are defined as combinations of operands and operators in a logical sequence. Such a sequence may represent a mathematical calculation, logical reasoning, relational comparison or even an informational operation.
Below are some expression examples,
10 - 5
(100 - 20) * 8
'Altogic' + '-' + 'Backend as a service platform'
(shoppingCart.totalPrice / SIZE(shoppingCart.items)) > 25
The first expression above is a mathematical expression, the second one is again
a mathematical expression but it is nested. The third one is a string
manipulation expression and the last one is a combination of relational,
informational and mathematical expressions, including the use of
function. This last expression checks whether the average price of an item in an
imaginary shopping cart object is greater than 25 or not. As seen from the above
examples, the expression types and combinations may differ from one expression
to another, however, the main rule is to keep combinations and types similar
to each other. That is the result of the expression operands evaluation,
namely the values, must be of the same type to have a proper operator
Expressions consist of operands and operators. An operand can be a basic value or an expression defined in terms of other expressions because expressions can be treated as recursive data structures.
Each expression is formed by logically combining values. Values are actually primitive data types, but more complex data types such as lists and geo-points can also be used in expressions. A value can be a:
|List (array of basic values)|
|Geo-point (longitude and latitude)|
|Model field value|
Operators carry out all the computations by using the expression elements and can be classified into two main categories. They can be either unary or binary.
Unary operators carry the calculations on a single value, namely on an operand, however, binary operators carry calculations on two operands, one on the left of the operator (left operand) and the other on the right (right operand).
Arithmetic operators are used for mathematical calculations.
|+ (unary)||+ Expression||Assigns a positive sign to expression|
|+||Expression + expression||Adds the values|
|- (unary)||- Expression||Assigns a negative sign to expression value|
|-||Expression – expression||Subtracts the values|
|*||Expression * expression||Multiplies the values|
|/||Expression / expression||Divides the values|
-(10 + ((10 * 5 - 4) / 100 - 20))
In the above expression, all the arithmetic operators are used. The expression is purely mathematical and it is nested. Moreover, the evaluation result of the expression value sign is converted to negative type by the unary minus operator.
Logical operators are used to evaluating an expression to true (1) or false (0).
|&&||Expression && expression||Logical AND returns true (1) only if both expressions evaluate to a nonzero value; otherwise it returns false (0).|
|||||Expression || expression||Logical OR returns true (1) if either of the expressions evaluates to a nonzero value; otherwise it returns false (0).|
|!||!Expression||Logical negation returns false (0) if the expression evaluates to a nonzero value; otherwise it returns true (1).|
(profile.firstName && profile.lastName) || profile.fullName
As it can be seen that the expression uses a combination of both logical && and || operators. It uses three field values of an imaginary 'profile' model. Initially, the expression in parenthesis will be evaluated, which performs a logical AND conditioning on the first two attributes. The result obtained from this evaluation will next be combined with the evaluation result of the last expression operand (profile.fullName) and the final result will be either evaluated to true or false.
Instead of the logical operators, you can also use AND function for &&, OR function for || and NOT function for ! operator. Using the functions instead of operators the above example can be written as:
OR(AND(profile.firstName, profile.lastName), profile.fullName)
Like logical operators, relational operators are also used to evaluate an expression to true (1) or false (0).
|==||Expression == expression||Checks equality of expressions; if the expressions are equal, it returns true (1) otherwise, it returns false (0)|
|!=||Expression != expression||Checks not equality of expressions; if the expressions are not equal, it returns true (1) otherwise, it returns false (0)|
|<||Expression < expression||Checks whether the first expression is less than the second one; if the first expression is less than the second one, returns true (1) otherwise, it returns false (0)|
|>||Expression > expression||Checks whether the first expression is greater than the second one; if the first expression is greater than the second one, returns true (1) otherwise, it returns false (0)|
|<=||Expression <= expression||Checks whether the first expression is less than or equal to the second one; if the first expression is less than or equal to the second one, returns true (1) otherwise, it returns false (0)|
|>=||Expression >= expression||Checks whether the first expression is greater than or equal to the second one; if the first expression is greater than or equal to the second one, returns true (1) otherwise, it returns false (0)|
task.completionDate > task.dueDate
The expression above uses a relational greater than operator. It checks whether the completion time is later than the due date of an imaginary task object.
Altogic specific operators
|[ ]||[expression, expression, ...]||This bracket operator specifies that the expressions which are separated by commas between the brackets are members of a list (array)|
|" "||"text"||This operator specifies that the expression between the double quotation is a text value.|
|' '||'text'||This operator specifies that the expression between the quotation is a text value.|
'10' + '20' + '30'
'Result : ' + (10 * 5 + 100)
SIZE([45,26,94,73]) == 4
The first expression illustrates a basic string concatenation operation. The
characters between the double quotes are treated as a string. The second and the
third strings are concatenated to the first one, which results in this final
102030. The second expression is a special one and evaluation result
of this expression is
Result : 150. This is again a string concatenation
operation but the second expression is evaluated as a mathematical one and the
result is converted to a string value. The last expression is a relational
equality check whether the left operand value is equal to the right operand
size is a built in function which returns the length of an array. In
this case the array length is 4 and this overall expression evaluates to true
Associativity and Precedence of Operators
In expressions, operators have different precedence. Evaluation of the expressions is carried on according to the precedence of the operators. From simple algebra, multiplication precedes addition or subtraction, however division and multiplication have the same precedence.
The precedence relation between the arithmetical, logical, special and relational operators implemented is as follows.
|( ) " ' [ ]||Left to right|
|+ - !||Left to right|
|* /||Left to right|
|+ -||Left to right|
|< <= > >=||Left to right|
|== !=||Left to right|
|&&||Left to right|
|||||Left to right|
Operators in the same category have equal precedence with each other. Where duplicates of operators appear in the table, the first occurrence is unary, the second binary. Each category has an associativity rule: left to right. In the absence of parentheses, this rule resolves the grouping of expressions with operators of equal precedence. The precedence of each operator in table above is indicated by its order in the table. The first category (on the first line) has the highest precedence. Operators on the same line have equal precedence.
There are several array, logical, text, object, mathematical, date & time, type conversion, validation and geolocation functions implemented as expressions for performing advanced calculations and data manipulations. You can use these functions in your expressions.
Each function has a name and zero or more input parameters. The number and type of input parameters are all specific to a function.
STARTSWITH("Jonh Adams", "Jon")
PRODUCT(number1, number2, ...)
PRODUCT(10, 23, 5, -75)
The first example above is the "STARTSWITH" function. This function checks whether a string starts with the characters of a search string, returning true or false as appropriate. It accepts two text parameters, main text and search text. The second example is the "PRODUCT" function. This function multiplies all the numbers given as arguments and returns the resulting number. There is no fixed number of input parameter for this function. At a minimum it takes two parameters and maximum it can have 100.
Data query expressions
Altogic does not use a separate data query syntax (e.g., SQL) for selecting/filtering data from the database. You use the same expression syntax described above and functions to query your data.
Assuming you have an imaginary product model with quantity (integer), weight (decimal), volume (decimal) and type (text) information. Below are some data query examples that you can create to fetch/filter your products data.
quantity > 100 && quantity < 200 && type == "plastic"
(weight / volume > 2 && type == 'metal') || (weight / volume <= 2 && type == 'plastic')