Calculated attributes
Copyright 2008-2012 ManyDesigns srl. All rights reserved.
Contents
Purpose
While most attributes are entered directly by the users through create/edit forms, some other attributes can be more conveniently calculated automatically by the system. In database terms, this operation is a de-normalization: certain attributes become functionally dependent from others.
Calculated attributes are convenient for usability, performance and to define constraints.
The modeler can make an attribute calculated by defining a formula, which instructs ManyDesigns Portofino about which operands and operators to use in the calculation.
To make an attribute calculated go to its details page and click on the Formula button.
Calculated attributes are also the foundation for constraints.
Operands
Constant operands:
- Numbers, such as 10 or 12.50
- Strings in double quotes, such as "this is a test"
- Booleans: true and false
Attributes on the same class:
- The system examines the calculated attribute's class and offers all the other attributes in the section "available attributes on the same class X"
Attributes on related classes:
- The system also examines any related classes (through one-to-many or many-to-one relationships with the calculated attribute's class) and offers their attributes in the section "available attributes on relationship Y".
Scalar vs. vector
Certain operands (constants, attributes on the same class and attributes on other classes related through a many-to-one relationship) are scalar. Their value are of basic type: a number, a string, a boolean.
Other operands, namely attributes on other classes related though a one-to-many relationship, are vectors. Their value is a vector of a basic type: a vector of numbers, a vector of strings, a vector of booleans.
Vector operands are used with aggregation operators (see below).
Operators
Arithmetic operators:
- a + b : addition
- a - b : subtraction
- a * b : multiplication
- a / b : division
Comparison operators:
- a = b : equals
- a <> b : different
- a > b : greater
- a < b : lesser
- a >= b : greater or equal
- a <= b : lesser or equal
Logical operators:
- a and b : logical AND
- a or b : logical OR
- not(a) : logical NOT
- if(a, b, c) : IF "a" THEN "b" ELSE "c"
- isnull(a) : argument is null
String operators:
- len(a) : string length in characters
- match(a, b) : match of string 'a' to pattern 'b' (documentation)
- urlencode(a) : encode string for use in URLs
- checkcodicefiscale(a) : argument is a valid 'codice fiscale'
Aggregation operators:
- sum(a, b, ..., n) : sum of multiple values
- sum(V) : sum of vector elements
- cat(s1, s2, ..., sn) : concatenation of strings
- cat(V) : concatenation of vector elements, comma separated
- count(V) : count of vector elements
- max(n1, n2, ..., nn) : max of multiple values
- max(V) : max of vector elements
- min(n1, n2, ..., nn) : min of multiple values
- min(V) : min of vector elements
- mcd(n1, n2, ..., nn) : maximum common divider of multiple values
- mcd(V) : maximum common divider of vector elements
Formulas syntax
Formulas follow the natural syntax of math formulas and of many programming languages. Binary operators (+, -, *, /, etc.) follow the same associativity rules as in the C language.
You can use parentheses to explicitly specify associativity or for readability.
Operands (A1, A2, etc.) are not case-sensitive (can be typed as a1, a2, etc). Operators (and, or, len,match, etc.) must be entered in lower case.
Calculation rules
If you create a new formula or change an existing one, all the attribute's values in the database are (re)calculated. This sometimes confuses new users, who expect the formula to be applied only to new objects. The rule is that formulas are applied to all objects: existing ones and new ones. So beware: old values are overwritten.
A calculated attribute can be defined in terms of other calculated attributes, which in turn can be defined in terms of other calculated attributes. This effectively creates a graph of dependencies between the attributes of the model. ManyDesigns Portofino finds out and applies the correct order of calculation. This "propagation" is automatic so you don't have to worry about it.
No circular definitions (an attribute defined in terms of itself, either directly or indirectly through other calculated attributes) are allowed.
Previous: Workflows
Next: Constraints