Home
Categories
Dictionnary
Download
Project Details
Changes Log

Functions


It is possible to use global functions in expressions. A global function is a function which is bridged to a real Java method. Evaluating the function in an expression will evaluate the associated Java method.

Global functions definition

The FunctionsDefinitions singleton class manage a global functions repository shared by all expresssions.

To define a global function, you just have to add it to the shared global functions repository:

      FunctionsDefinitions globals = FunctionsDefinitions.getInstance();  
        
      def.addFunction("method", instance, method);  

Note that as for variables, global functions are shared by all expressions.

Definition example

Lets define a function which adds two values:
      public class MyClass {  
        public int myAdd(int a, int b) {  
           return a + b;  
        }  
      }  

And now add it to the global functions repository:

      Method method = MyClass.class.getMethod("myAdd", Integer.TYPE, Integer.TYPE);        
      MyClass instance = new MyClass();  
        
      FunctionsDefinitions globals = FunctionsDefinitions.getInstance();  
      def.addFunction("add", instance, method);  

Usage

When encountering the function name, an expression will use the function. For example, with the previous function example:
      Equation equation = ExpressionJ.parse("add(2, 3) + 2");   
      Objet value =  equation.eval();   

The result of value will be 7.


Categories: concepts

Copyright 2018 Herve Girod. All Rights Reserved