Skip to content

Bio Expression Functions

Rajab Davudov edited this page Mar 13, 2019 · 3 revisions

Functions

It is very easy to define functions. You need to implement com.linkedlogics.bio.BioFunction interface and add @BioFunc annotation.

public interface BioFunction {
	public Object getValue(Object value, Object... parameters) ;
	
	default boolean validate(Object value, Object... parameters) throws ValidationException {
		return true ;
	}
}

getValue() must be overridden and provide function result. validate can be overridden to validate input values.

getValue() has one or more input parameters:

  • value comes from left side of . as in car.engine.calculateHp(). Here value will be engine.
  • parameters come from inside parenthesis as in car.calculatePrice(goverment.tax). Here parameters will be government.tax.

@BioFunc

This annotation is used to mark class as function and provide function name.

  • name() indicates name of function to be used inside expressions.
  • cached() caching flag to cache function instance or each time create new instance of function class.
  • dictionary() functions also are like Bio Objects, each may belong to dictionary.

Here is an example:

@BioFunc(name="calculateHp", isCached=true)
public class CalculateHorsePower implements BioFunction {
	@Override
	public Object getValue(Object value, Object... parameters) {
		Engine engine = (Engine) value ;
		return engine.getInt(Engine.CAPACITY) / 100 * engine.getInt(Engine.NUMBER_OF_CYLINDERS) ;
	}

	@Override
	public boolean validate(Object value, Object... parameters) throws ValidationException {
		if (!(value instanceof Engine)) {
			throw new ValidationException("value type is incorrect must be Engine") ;
		}
		return true ;
	}
}

Here we just simply divide engine capacity by 10 and multiply by number of cylinders. Let's see how it works.

	Engine e = new Engine() ;
	e.set(Engine.CAPACITY, 4000);
	e.set(Engine.NUMBER_OF_CYLINDERS, 6);
	
	Car c = new Car() ;
	c.set(Car.ENGINE, e) ;
	c.set(Car.PRODUCER, "Ford") ;
	c.set(Car.YEAR_OF_PRODUCTION, 2019) ;
	c.set(Car.FUEL_EFFICIENCY, 17.8) ;
BioExpression expr = BioExpression.parse("car.engine.calculateHp()") ;
System.out.println(expr.getValue(c)) ;
240

But if tried to evaluate expression in a different way.

BioExpression expr = BioExpression.parse("car.producer.calculateHp()") ;
System.out.println(expr.getValue(c)) ;
Exception in thread "main" com.linkedlogics.bio.exception.ValidationException: value type is incorrect must be Engine
	at com.linkedlogics.bio/com.linkedlogics.bio.test.car.CalculateHorsePower.validate(CalculateHorsePower.java:18)
	at com.linkedlogics.bio/com.linkedlogics.bio.expression.Function.getValue(Function.java:51)
	at com.linkedlogics.bio/com.linkedlogics.bio.expression.Expression.getValueWithNext(Expression.java:26)
	at com.linkedlogics.bio/com.linkedlogics.bio.expression.Expression.getValueWithNext(Expression.java:29)
	at com.linkedlogics.bio/com.linkedlogics.bio.expression.Expression.getValueWithNext(Expression.java:29)
	at com.linkedlogics.bio/com.linkedlogics.bio.expression.Expression.getValue(Expression.java:16)
	at com.linkedlogics.bio/com.linkedlogics.bio.test.car.BioFunctionTest.main(BioFunctionTest.java:25)

then validation will fail.