Class AlternateFunctions
java.lang.Object
com.singularsys.jep.functions.PostfixMathCommand
com.singularsys.jep.misc.overloadedfunctions.AlternateFunctions
- All Implemented Interfaces:
CallbackEvaluationI,JepComponent,PostfixMathCommandI,Serializable
public class AlternateFunctions
extends PostfixMathCommand
implements CallbackEvaluationI, JepComponent
Allows function to be overloaded with multiple candidate functions chosen by the number of arguments.
For example the atan function can be overloaded so that it has a one argument and two argument
version.
UnaryFunction uf = new ArcTangent();
BinaryFunction bf =new ArcTangent2();
AlternateFunctions af = new AlternateFunctions(uf, bf);
jep.addFunction("atan",af);
jep.reinitializeComponents();
Node node = jep.parse("atan(1)");
Object res = jep.evaluate(node);
assertEquals(Math.PI/4,(double) res,1e-9);
Node node2 = jep.parse("atan(1,2)");
Object res2 = jep.evaluate(node2);
assertEquals(Math.atan2(1,2),(double) res2,1e-9);
The above resolution happens at evaluation time. If the
AlternateFunctionGrammerMatcher is used then resolution happens at parse time.
Evaluation of overloaded functions is done by creating a temporary node
with the appropriate function and evaluating this node.
This means any type of function can be used, including those that implement
CallbackEvaluationI.
- Since:
- Jep 4.1
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final PostfixMathCommandI[]protected NodeFactoryprotected OperatorTableIFields inherited from class com.singularsys.jep.functions.PostfixMathCommand
curNumberOfParameters, description, name, NaN, numberOfParameters -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionbooleancheckNumberOfParameters(int n) Checks the number of parameters of the function.Performs some special evaluation on the node.Return a description of the function from the properties file.getFunctionForNArgs(int n) Returns the function that matches the number of arguments.Gets a light-weight instance suitable for using in multiple threads.voidInitialize the component.Methods inherited from class com.singularsys.jep.functions.PostfixMathCommand
asArray, asBool, asDouble, asInt, asLong, asStrictInt, asString, asType, getDescription, getDescriptionWithType, getName, getNumberOfParameters, run, setCurNumberOfParameters, setDescription, setName, toString, toString
-
Field Details
-
functions
-
nf
-
ot
-
-
Constructor Details
-
AlternateFunctions
Constructor. Any type of function can be used, including those that implementCallbackEvaluationI.- Parameters:
functions- the list of function
-
-
Method Details
-
checkNumberOfParameters
public boolean checkNumberOfParameters(int n) Description copied from class:PostfixMathCommandChecks the number of parameters of the function. Functions which set numberOfParameter=-1 should overload this method- Specified by:
checkNumberOfParametersin interfacePostfixMathCommandI- Overrides:
checkNumberOfParametersin classPostfixMathCommand- Parameters:
n- number of parameters function will be called with.- Returns:
- false if an illegal number of parameters is supplied, true otherwise.
-
getFunctionForNArgs
Returns the function that matches the number of arguments.- Parameters:
n- the number of arguments- Returns:
- the function that matches the number of arguments or null if none found.
-
init
Description copied from interface:JepComponentInitialize the component. This method is called whenever a component is added to Jep. Hence, it allows components to keep track of the other components they may rely on.- Specified by:
initin interfaceJepComponent- Parameters:
jep- the current Jep instance
-
getLightWeightInstance
Description copied from interface:JepComponentGets a light-weight instance suitable for using in multiple threads.- Specified by:
getLightWeightInstancein interfaceJepComponent- Returns:
- either a new instance, null or 'this'.
-
evaluate
Description copied from interface:CallbackEvaluationIPerforms some special evaluation on the node. This method has the responsibility for evaluating the children of the node, and it should generally callpv.eval(node.jjtGetChild(i))
for each child. The SymbolTable is not passed as an argument. This is because it is better practice to get and set variable values by using node.getVar().setValue() rather that through the SymbolTable with requires a hashtable lookup.- Specified by:
evaluatein interfaceCallbackEvaluationI- Parameters:
node- The current nodepv- The visitor, can be used evaluate the children- Returns:
- the value after evaluation. This value will be passed to other functions higher up the node tree. The value can be any type including Double or Vector<Object>
- Throws:
EvaluationException- if the calculation cannot be performed- See Also:
-
getDescription
Description copied from class:PostfixMathCommandReturn a description of the function from the properties file. The property name is the class name followed by ".description". The property is found using theJepMessagesclass, which can be configured to add additional properties files. IfPostfixMathCommand.setDescription(String)has been called, return the description set by that method instead.- Specified by:
getDescriptionin interfacePostfixMathCommandI- Overrides:
getDescriptionin classPostfixMathCommand- Returns:
- the description of the function
-