API

evaluateDependentFormulas()

Returns a boolean value and has the following parameters:

  • String computeFieldName,
  • CalculationContext calculationContext

boolean evaluateDependentFormulas(String computeFieldName, CalculationContext calculationContext)

This method evaluates a set of formulas dependent on an input or a formula field identified by the computeFieldName. A set of such dependent formulas is a subset of 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 evaluateDependentFormulas() 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 of the dependent formula fails, the evaluateDependentFormulas() 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 dependent formulas were evaluated without errors this method returns true, otherwise it returns false.

The first example uses the following formulas, A, B, and C and two input fields, X and Y.

A=AVERAGE(X,40)

B=SUM(Y,30)

C=A*2

The input field X has two dependent formulas A and C, so if X = 20, A will be evaluated as 30 and C will be evaluated as 60. Since formula B is not dependent on X, it is not getting evaluated in this example.

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

public class EvaluateDependentFormulasExample1 {
    public static void main(String[] args) {
        String myFormulaText1 = "AVERAGE(X,40)";
        String myFormulaText2 = "SUM(Y,30)";
        String myFormulaText3 = "A*2";
        CalculationContext calculationContext = new CalculationContext("EvaluateDependentsExample1");

        calculationContext.put("X", new InputField(ComputeFieldType.NUMERIC, 20));
        calculationContext.put("Y", new InputField(ComputeFieldType.NUMERIC));
        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.evaluateDependentFormulas("X", 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 
C calculated value is 60
X calculated value is 20
Y calculated value is 

The second example uses the following formulas A, B, and C and two input fields, X and Y.

A=VLOOKUP(X,"my_table",2,0)

B=VLOOKUP(Y,"my_table",2,0)

C=LEN(A)

A VLOOKUP function in this example uses the TableArray object with the name "my_table", created from the following two-dimensional array:

{{10, "Apple"},

 {20, "Pear"},

 {30, "Orange"}}

The input field X has two dependent formulas A and C, so if X = 20, A will be evaluated as "Pear" and C will be evaluated as 4. Since formula B is not dependent on X, it is not evaluated in this example.

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

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

        calculationContext.put("my_table", tableArray);
        calculationContext.put("X", new InputField(ComputeFieldType.NUMERIC, 20));
        calculationContext.put("Y", new InputField(ComputeFieldType.NUMERIC));
        calculationContext.put("A", new FormulaField(ComputeFieldType.ALPHANUMERIC, myFormulaText1));
        calculationContext.put("B", new FormulaField(ComputeFieldType.ALPHANUMERIC, myFormulaText2));
        calculationContext.put("C", new FormulaField(ComputeFieldType.NUMERIC, myFormulaText3));
        if (HybridFormulaEvaluator.evaluateDependentFormulas("X", 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 Pear
B calculated value is 
C calculated value is 4
X calculated value is 20
Y calculated value is