Arithmetic
Expressions
Arithmetic expressions can be used to set a value of a value variable based on other values used in a query. To use arithmetic operations, start a statement with a value variable and an equal sign, followed by an expression.
An expression describes a computation of a value in a query.
It can be used only in a match
clause or a condition
of a rule
and can contain any combination of the following elements:
-
a concept variable representing an attribute (e.g.,
$size-kb
), -
a value variable (e.g.,
?sum
), -
a literal value (e.g.,
4
), -
an arithmetic operation (e.g.,
+
), -
or a built-in function (e.g.,
max()
).
?x = $s + 1;
In the above example, the value variable ?x
is bound to the value of the attribute in $s
added 1
.
Operations
TypeQL includes the following arithmetic operations for values, presented in the order of operations in an expression:
-
()
— parentheses. -
^
— exponentiation (power). -
*
— multiplication. -
/
— division. -
%
— modulo (the remainder of a division). -
+
— addition. -
-
— subtraction.
?gb = (($s + ?o)/ 1024) / 1024;
In the above example, the value variable ?gb
is bound to the result of a complex arithmetic expression.
The expression takes the value of concept variable $s
, adds it to a value of value variable ?o
,
then divides the result by 1024
, and finally divides the result by 1024
again.
Arithmetic operations are often used in a combination with comparators and rules.