Skip to content
This repository was archived by the owner on May 15, 2019. It is now read-only.

How DEFINE and FUNCEXEC Functions Work

Roman Roschin edited this page Nov 6, 2015 · 5 revisions

How DEFINE and FUNCEXEC Functions Work

Purpose of this document

The purpose of this document is to describe the workflow of DEFINE and FUNCEXEC functions and their implementation in Calculation Engine\POI.

Question: What is DEFINE and FUNCEXEC?
Answer

Described here

Question: How DEFINE function code works?
Answer

There is a separate java class for DEFINE function called DefineFunction.java. It follows all the standards of how custom excel function should be created, but it does nothing. It’s evaluate() method just returns its name DEFINE. This is because DEFINE function should never be called from evaluate() perspective. It can only describe model, not execute it. In other words it just contains some static metadata to be used by define function scanner.

Question: DEFINE function is a meta-property of a workbook (DataModel), how these properties are loaded into POI\Calculation Engine Evaluator?
Answer

Three classes are responsible for it.

  • The first is Functions.java from engine-functions package. Its job is to scan for all (not only DEFINE) functions in a package and load function classes into application cache. Function must be marked with FunctionMeta.java and CustomFunction.java.
  • The second is AttributeFunctionStorage.java from engine-data package. It contains method updateDefineFunctions(). Method updateDefineFunctions() just invokes collectAttributeFunctions() for every IDataModel available in the cache. Method collectAttributeFunctions() does the actual scan for DEFINE in given IDataModel. Result of collectAttributeFunctions() is a Map of String and DefineFunctionMeta.java. Size of the map is number of DEFINE function is given IDataModel.
  • The third is SpreadsheetEvaluator.java from engine-core package. Its method loadCustomFunctions() does loading of all the functions (from Functions.java cache, including DEFINEs) into POI’s function cache.
Question: Is it necessary to do the same steps for FUNCEXEC also?
Answer

No, steps for loading define metadata are necessary for DEFINE functions only, because they are not like all other functions.

Question: How FUNCEXEC function code works?
Answer

Code of FUNCEXEC function can be found in FuncexecFunction.java

Question: What is the FUNCEXEC evaluate() workflow?
Answer

When you want to evaluate a cell that contains FUNCEXEC in it you first need to load all the define metadata described above. Once everything is loaded into memory (cache) you do simple IEvaluator.evaluate(CellAddress) call in java code.
The evaluation begins in IEvaluator and almost immediately comes to POI WorkbookEvaluator.java class. POI evaluator does its standard evaluation steps and when it finds a FUNCEXEC function it calls to instance of FuncexecFunction.java (evaluate() method).

  1. FuncexecFunction does some validation operations:
  • First argument is a String (name of DEFINE function) or reference to a Cell
  • Such DEFINE function is registered (is found in a cache)
  • Number of input arguments of this FUNCEXEC equals to number of input arguments of DEFINE function
  1. FuncexecFunction does resolving of input arguments. It is done with standard POI’s OperandResolver.getSingleValue(), since input arguments can be references to cells only.

  2. FuncexecFunction does create new instance of ForkedEvaluator.java (more specific – its extension called XSSFForkedEvaluator.java) with original XSSFWorkbook from DEFINE function’s model. Then it replaces all the input values in this forked evaluator with provided values.

  3. FuncexecFunction does evaluate() from forked evaluator for every output cell.

The diagram below illustrates the process: PDF

Clone this wiki locally