Class OverloadedFunctionByStructure
java.lang.Object
com.singularsys.jep.functions.PostfixMathCommand
com.singularsys.jep.misc.overloadedfunctions.OverloadedFunctionByStructure
- All Implemented Interfaces:
CallbackEvaluationI,JepComponent,PostfixMathCommandI,Serializable
public class OverloadedFunctionByStructure
extends PostfixMathCommand
implements CallbackEvaluationI, JepComponent
Allows function to be overloaded with two candidate functions
and a predicate that tests the structure of the parse tree
to see which function should be applied.
For example in the extensions package the dot operator can be overloaded
with a custom function
ChainingOpPfmc
that allows a method chaining syntax so a syntax like [1..10] . map(x->x^2).
This is used when the second argument is a
ChainedFunctionI.
Otherwise the regular dot product function is used.
Operator dot_op = jep.getOperatorTable().getDot();
dot_op.setPFMC(new OverloadedFunctionByStructure(
new ChainingOpPfmc(),
new Dot(),
n -> n.jjtGetChild(1).getPFMC() instanceof ChainedFunctionI));
jep.reinitializeComponents();
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 PostfixMathCommandIprotected final PostfixMathCommandIprotected NodeFactoryprotected OperatorTableIFields inherited from class com.singularsys.jep.functions.PostfixMathCommand
curNumberOfParameters, description, name, NaN, numberOfParameters -
Constructor Summary
ConstructorsConstructorDescriptionOverloadedFunctionByStructure(PostfixMathCommandI function1, PostfixMathCommandI function2, Predicate<Node> argTest) Constructor. -
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.getFunctionFor(Node node) Get the actual function that will be used for a given node.Gets a light-weight instance suitable for using in multiple threads.intCalculates the possible number of parameters.voidInitialize the component.Methods inherited from class com.singularsys.jep.functions.PostfixMathCommand
asArray, asBool, asDouble, asInt, asLong, asStrictInt, asString, asType, getDescription, getDescriptionWithType, getName, run, setCurNumberOfParameters, setDescription, setName, toString, toString
-
Field Details
-
function1
-
function2
-
argTest
-
nf
-
ot
-
-
Constructor Details
-
OverloadedFunctionByStructure
public OverloadedFunctionByStructure(PostfixMathCommandI function1, PostfixMathCommandI function2, Predicate<Node> argTest) Constructor. Any type of function can be used, including those that implementCallbackEvaluationI.- Parameters:
function1- the first functionfunction2- the second functionargTest- a predicate that tests the structure of the parse tree, the first function is used if this returns true.
-
-
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.
-
getNumberOfParameters
public int getNumberOfParameters()Calculates the possible number of parameters. If both functions have the same number of parameters then return than, otherwise return -1.- Specified by:
getNumberOfParametersin interfacePostfixMathCommandI- Overrides:
getNumberOfParametersin classPostfixMathCommand- Returns:
- the possible number of parameters
-
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:
-
getFunctionFor
Get the actual function that will be used for a given node.- Parameters:
node- the node to test- Returns:
- either function1 or function2 depending on the test
-
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'.
-
getFunction1
-
getFunction2
-
getPredicate
-
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
-