Operators Functions API Pricelist Company Blog Contact Us

API

compileFormula()

Returns an int value and has the following parameters:

  • String formulaFieldName
  • , CalculationContext calculationContext

int compileFormula(String formulaFieldName, CalculationContext calculationContext)

This method compiles a formula contained in a formula field identified by the formulaFieldName and residing in the CalculationContext parameter's HashMap of named ComputeField objects. The calculationContext parameter's udfClassName property is used for the formulas that reference user defined functions. If the formula compilation fails this method returns the relevant error code and stores it in its formula field. This method is used to verify the correctness of a formula syntax.

The first example shows how to compile the following formula my_formula, which refers to two input fields, the number and num_digits:

my_formula=ROUND(number, num_digits)

import com.crystalprism.ce.formula.HybridFormulaEvaluator;
import com.crystalprism.ce.usermodel.*;

public class CompileFormulaExample1 {
    public static void main(String[] args) {
        String myFormulaText = "ROUND(number, num_digits)";
        CalculationContext calculationContext = new CalculationContext("CompileFormulaExample1");
        int rc;

        calculationContext.put("number", new InputField(ComputeFieldType.NUMERIC));
        calculationContext.put("num_digits", new InputField(ComputeFieldType.NUMERIC));
        calculationContext.put("my_formula", new FormulaField(ComputeFieldType.NUMERIC, myFormulaText));
        rc = HybridFormulaEvaluator.compileFormula("my_formula", calculationContext);
        System.out.println("Formula compilation return code: " + rc);
    }
}

Formula compilation return code: 0

This example makes an intentional mistake by adding an extra bracket to the myFormulaText variable from the previous example. This results in an error code 101, corresponding to an unbalanced parentheses condition:

import com.crystalprism.ce.formula.HybridFormulaEvaluator;
import com.crystalprism.ce.usermodel.*;

public class CompileFormulaExample2 {
    public static void main(String[] args) {
        String myFormulaText = "ROUND(number), num_digits)";
        CalculationContext calculationContext = new CalculationContext("CompileFormulaExample2");
        int rc;

        calculationContext.put("number", new InputField(ComputeFieldType.NUMERIC, 165.78));
        calculationContext.put("num_digits", new InputField(ComputeFieldType.NUMERIC, 1));
        calculationContext.put("my_formula", new FormulaField(ComputeFieldType.NUMERIC, myFormulaText));
        rc = HybridFormulaEvaluator.compileFormula("my_formula", calculationContext);
        System.out.println("Compilation error code: " + rc);
    }
}

Compilation error code: 101