API

evaluateFormula()

Returns an int value and has the following parameters:

  • String formulaFieldName
  • , CalculationContext calculationContext

int evaluateFormula(String formulaFieldName, CalculationContext calculationContext)

This method evaluates 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 HashMap of named TableArray objects is used in formulas that reference lookup functions. This parameter's udfClassName property is used for the formulas that reference user defined functions. This method compiles the formula prior to the evaluation. If the evaluation of the formula failes this method returns a non-zero error code and stores it in its formula field.

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

my_formula=ROUND(number, num_digits)

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

public class EvaluateFormulaExample1 {
    public static void main(String[] args) {
        String myFormulaText = "ROUND(number, num_digits)";
        CalculationContext calculationContext = new CalculationContext("EvaluateFormulaExample1");
        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.evaluateFormula("my_formula", calculationContext);
        if (rc == 0) {
            System.out.println("Calculated result: " + calculationContext.get("my_formula").getDisplayValue());
        }
        else {
            System.out.println("Evaluation error code: " + rc);
        }
    }
}

Calculated result: 165.8

The second example shows how to evaluate the following formula my_formula, which refers to 3 input fields, lookup_value, col_index_num, range_lookup, and alphanumeric constant "my_table":

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

public class EvaluateFormulaExample2 {
    public static void main(String[] args) {
        Object[][] tableArrayBase = {{10, "Apple"},
                                     {20, "Pear"},
                                     {30, "Orange"}};
        TableArray tableArray = new TableArray(tableArrayBase);
        String myFormulaText = "VLOOKUP(lookup_value, \"my_table\", col_index_num, range_lookup)";
        CalculationContext calculationContext = new CalculationContext("EvaluateFormulaExample2");
        int rc;

        calculationContext.put("my_table", tableArray);
        calculationContext.put("lookup_value", new InputField(ComputeFieldType.NUMERIC, 20));
        calculationContext.put("col_index_num", new InputField(ComputeFieldType.NUMERIC, 2));
        calculationContext.put("range_lookup", new InputField(ComputeFieldType.NUMERIC, 0));
        calculationContext.put("my_formula", new FormulaField(ComputeFieldType.ALPHANUMERIC, myFormulaText));
        rc = HybridFormulaEvaluator.evaluateFormula("my_formula", calculationContext);
        if (rc == 0) {
            System.out.println("Calculated result: " + calculationContext.get("my_formula").getDisplayValue());
        }
        else {
            System.out.println("Evaluation error code: " + rc);
        }
    }
}

Calculated result: Pear

The third example makes an intentional mistake in Example 1 by invoking the evaluateFormula() method with the wrong formula name "my_Formula" instead of the correct name "my_formula". This results in an error code 110, corresponding to an absent formula field condition:

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

public class EvaluateFormulaExample3 {
    public static void main(String[] args) {
        String myFormulaText = "ROUND(number, num_digits)";
        CalculationContext calculationContext = new CalculationContext("EvaluateFormulaExample3");
        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.evaluateFormula("my_Formula", calculationContext);
        if (rc == 0) {
            System.out.println("Calculated result: " + calculationContext.get("my_formula").getDisplayValue());
        }
        else {
            System.out.println("Evaluation error code: " + rc);
        }
    }
}

Evaluation error code: 110