Reference‎ > ‎

Rules: Formula

Definition

A Formula is defined by a method with the @Formula annotation:

import com.autobizlogic.abl.businesslogic.annotations.*;

public class CustomerLogic {

    @CurrentBean
    private Customer customer;
    
    @Formula
    public int deriveCreditRating() {
        if (customer.getBalance() > 1000)
           return 17;
        else
            etc...
    }

The name of the method has a special meaning if (and only if) the attributeName parameter is not specified in the annotation. In that case, the name of the method must follow the format deriveX, where X is the name of the attribute.

For instance, a Formula for the attribute price would be defined by a method named derivePrice.

If the attributeName parameter is specified in the annotation, then the name of the method can be anything.

Parameters

 <value> [optional] an expression whose value will be stored in the attribute.
 attributeName [optional] the name of the attribute whose value is computed by this formula. The return value of the method must be the same as the type of the attribute.

Declarative formula

If the annotation contains a value (a boolean expression), the Formula is known as a declarative formula.

For example:

@Formula("unitPrice * quantity")
public void deriveAmount() { }

A more complete example:

@Formula(
        value = "unitPrice * quantity",
        attributeName = "amount"
public void computeTotal() { }


Declarative formulas can also contain code, and that code will be invoked, but it should not make any changes to the object (or any other persistent object). The purpose of invoking the code in this case is mostly for debugging.

Expression language

The expression (if provided) is currently interpreted using Apache JEXL. You do therefore have complete access to its powerful language.

However, it is generally recommended to keep the expression simple. Anything more complex should be handled in code, which is much easier to debug.

In addition, it should be noted that the expression is interpreted, and is therefore much slower than code. If performance is critical, code should be used.


Coded formulas

The value for the fomula can also be expressed in Java or Groovy. In that case, the method must return the appropriate type,  and the annotation cannot contain an expression.

For Groovy methods, returning null means that no changes should be applied to the value. If you wish to set the value of the attribute to null, then you should return LogicContext.nullValue.


Comments