Skip to content

Bio Expression as Function

Rajab Davudov edited this page Apr 4, 2019 · 6 revisions

Bio Expressions as a 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

Reference to caller

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.

A Functions XML file

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();
Clone this wiki locally