-
Notifications
You must be signed in to change notification settings - Fork 1
Bio Expression as Function
At previous section you had to write a Java class for a custom function. There is also an alternative way to define functions as Bio Expressions. You need to add function definition into dictionary xml using <func/>
tag.
<dictionary code="0">
<func name="add" version="0" parameters="a,b">
a + b
</func>
</dictionary>
Note that we didn't define a class
attribute here because we have provided an expression. These functions are instantiated using GenericFunction
class and are stored together with other functions.
Usage is same as other functions.
BioExpression e = BioExpression.parse("add(16,26)") ;
System.out.println(e.getValue());
42
Assume that we want to use value of caller in our expression. For example:
car.engine.capacity.boost(2)
Here we want to boost capacity by multiplying by 2. Question here is how we can refer to capacity
inside our expression. Here is the answer:
<dictionary code="0">
<func name="boost" version="0" parameters="f">
self * f
</func>
</dictionary>
self always refers to the left side or the caller value while this function is being evaluated.
You can add your custom functions to a separate XML file and use it while building your dictionary.
new BioDictionaryBuilder().addResource("dictionary.xml").addResource("myfunctions.xml").build();