API

compileAllFormulas()

Returns a FormulaFieldsDescriptor
and has one parameter:

  • CalculationContext calculationContext

FormulaFieldsDescriptor compileAllFormulas(CalculationContext calculationContext)

This method compiles a set of formulas contained in the formula fields residing in the 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. The completion of this method is considered successful if each formula of the set was successfully compiled. If at least one formula compilation failed, the completion of this method is considered in error. This method returns a FormulaFieldsDescriptor summary of its successful or in error results. This method is used to analyse formula dependencies, verify their syntactical correctness, and validate the integrity of a set of formulas.

The first example shows how to compile the following expression A and 2 formulas, B and C:

A=AVERAGE(20,30)

B=SUM(A,30)

C=A/B

In this example B is dependent on A and C is dependent on A and B. The correct order of these formulas evaluation is A, B, and C.

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

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

        calculationContext.put("A", new FormulaField(ComputeFieldType.NUMERIC, myExpressionText));
        calculationContext.put("B", new FormulaField(ComputeFieldType.NUMERIC, myFormulaText1));
        calculationContext.put("C", new FormulaField(ComputeFieldType.NUMERIC, myFormulaText2));
        formulaFieldsDescriptor = HybridFormulaEvaluator.compileAllFormulas(calculationContext);
        if (!formulaFieldsDescriptor.isError()) {
            System.out.println("Formula dependencies: ");
            formulaFieldsDescriptor.getFormulaFieldsDependencies().
                    forEach((key, value) -> {
                        System.out.println(key + " is a precedent of " + String.join(" and ", value));
                    });
            System.out.println("\nFormulas evaluation order is " +
                    String.join(", ", formulaFieldsDescriptor.getFormulaFieldsEvaluationOrder()));
        }
    }
}

Formula dependencies: 
A is a precedent of B and C
B is a precedent of C
Formulas evaluation order is A, B, C

The second example makes an intentional mistake in the previous example by not putting the expression A into the computeFields HashMap, which results in a compilation failure because the expression A is now missing:

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

public class CompileAllFormulasExample2 {
    public static void main(String[] args) {
        String myFormulaText1 = "SUM(A,30)";
        String myFormulaText2 = "A/B";
        CalculationContext calculationContext = new CalculationContext("CompileAllFormulasExample2");
        FormulaFieldsDescriptor formulaFieldsDescriptor;

        calculationContext.put("B", new FormulaField(ComputeFieldType.NUMERIC, myFormulaText1));
        calculationContext.put("C", new FormulaField(ComputeFieldType.NUMERIC, myFormulaText2));
        formulaFieldsDescriptor = HybridFormulaEvaluator.compileAllFormulas(calculationContext);
        if (formulaFieldsDescriptor.isError()) {
            if (formulaFieldsDescriptor.getMissingComputeFieldsIndicator()) {
                System.out.println("Missing compute fields : " +
                        String.join(", ", formulaFieldsDescriptor.getMissingComputeFields()));
            }
            if (formulaFieldsDescriptor.getComputeFieldsInErrorIndicator()) {
                System.out.println("Compute fields in error : " +
                        String.join(", ", formulaFieldsDescriptor.getComputeFieldsInError()));
            }
        }
        else {
            System.out.println("Successful compilation");
        }
    }
}

Missing compute fields : A

The third example makes an intentional mistake by adding an extra bracket to the myFormulaText1 variable from the Example 1.
This results in the compilation failure because now the formula field B is in error:

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

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

        calculationContext.put("A", new FormulaField(ComputeFieldType.NUMERIC, myExpressionText));
        calculationContext.put("B", new FormulaField(ComputeFieldType.NUMERIC, myFormulaText1));
        calculationContext.put("C", new FormulaField(ComputeFieldType.NUMERIC, myFormulaText2));
        formulaFieldsDescriptor = HybridFormulaEvaluator.compileAllFormulas(calculationContext);
        if (formulaFieldsDescriptor.isError()) {
            if (formulaFieldsDescriptor.getMissingComputeFieldsIndicator()) {
                System.out.println("Missing compute fields : " +
                        String.join(", ", formulaFieldsDescriptor.getMissingComputeFields()));
            }
            if (formulaFieldsDescriptor.getComputeFieldsInErrorIndicator()) {
                System.out.println("Compute fields in error : " +
                        String.join(", ", formulaFieldsDescriptor.getComputeFieldsInError()));
            }
        }
        else {
            System.out.println("Successful compilation");
        }
    }
}

Compute fields in error : B