API

evaluateAllFormulas()

Returns a boolean value and has one parameter:

  • CalculationContext calculationContext

boolean evaluateAllFormulas(CalculationContext calculationContext)

This method evaluates all formulas contained in the formula fields residing in the CalculationContext parameter's HashMap of named ComputeField objects. The calculationContext parameter's FormulaFieldsDescriptor component directs the evaluateAllFormulas() method execution. It is created during the first invocation of this method by the dynamically invoked compileAllFormulas() method prior to evaluation.

If the evaluation of any formula fails, the evaluateAllFormulas() method stores the corresponding error code in its formula field. It then adds it to the compute-fields-in-error list of the calculationContext parameter's FormulaFieldsDescriptor component without stopping the evaluation process. This method also uses this object to record missing ComputeFields. The calculationContext parameter's HashMap of named TableArray objects is used in formulas that reference lookup functions. The calculationContext parameter's udfClassName is used for the formulas that reference user defined functions.

If all formulas were evaluated without errors this method returns true, otherwise it returns false.

The first example shows how to evaluate the following expression, A and 2 formulas, B and C. The correct order for the evaluation of these formulas is A, B and C:

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

public class EvaluateAllFormulasExample1 {
    public static void main(String[] args) {
        String myFormulaText1 = "AVERAGE(20, 40)";
        String myFormulaText2 = "SUM(A,30)";
        String myFormulaText3 = "A/B";
        CalculationContext calculationContext = new CalculationContext("EvaluateAllFormulasExample1");

        calculationContext.put("A", new FormulaField(ComputeFieldType.NUMERIC, myFormulaText1));
        calculationContext.put("B", new FormulaField(ComputeFieldType.NUMERIC, myFormulaText2));
        calculationContext.put("C", new FormulaField(ComputeFieldType.NUMERIC, myFormulaText3));
        if (HybridFormulaEvaluator.evaluateAllFormulas(calculationContext)) {
            calculationContext.getComputeFields().forEach((computeFieldName, computeField) ->
                    System.out.println(computeFieldName +
                            " calculated value is " + computeField.getDisplayValue()));
        }
        else
            System.out.println("Some formulas were compiled or evaluated with errors");
    }
}

A calculated value is 30
B calculated value is 60
C calculated value is 0.5

The second example shows how to evaluate the following formulas, B and C:

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

public class EvaluateAllFormulasExample2 {
    public static void main(String[] args) {
        Object[][] tableArrayBase = {{10, "Apple"},
                                     {20, "Pear"},
                                     {30, "Orange"}};
        TableArray tableArray = new TableArray(tableArrayBase);
        String myFormulaText1 = "VLOOKUP(A, \"my_table\", 2, 0)";
        String myFormulaText2 = "LEN(B)";
        CalculationContext calculationContext = new CalculationContext("EvaluateAllFormulasExample2");

        calculationContext.put("my_table", tableArray);
        calculationContext.put("A", new InputField(ComputeFieldType.NUMERIC, 20));
        calculationContext.put("B", new FormulaField(ComputeFieldType.ALPHANUMERIC, myFormulaText1));
        calculationContext.put("C", new FormulaField(ComputeFieldType.NUMERIC, myFormulaText2));
        if (HybridFormulaEvaluator.evaluateAllFormulas(calculationContext)) {
            calculationContext.getComputeFields().forEach((computeFieldName, computeField) ->
                    System.out.println(computeFieldName +
                            " calculated value is " + computeField.getDisplayValue()));
        }
        else
            System.out.println("Some formulas were compiled or evaluated with errors");
    }
}
    

A calculated value is 20
B calculated value is Pear
C calculated value is 4