Skip to content
Tomasz Fijałkowski edited this page Sep 24, 2016 · 14 revisions

epel expression language documentation

Contents

A Brief Example

When evaluating simple expressions, opel use only OpelEngine class and eval method. Remember that opel always return CompletableFuture.

String expression = "2 * 3 + 4";
OpelEngine engine = OpelEngineBuilder.create().build()
engine.eval(expression)
	.whenComplete((result, error) -> System.out.println(result));

this expression is transformed to equivalent code:

CompletableFuture.completedFuture(2)
    .thenCombine(CompletableFuture.completedFuture(3), (l, r) -> l * r)
    .thenCombine(CompletableFuture.completedFuture(4), (l, r) -> l + r)
    .whenComplete((result, error) -> System.out.println(result));

As you see, opel efficiently hides boilerplate of the CompletableFuture API.

Custom functions and values

Simle expressions like 2+2*2 are executed in the same thread in which the method eval was called. To taste the real power of opel asynchrony, you have to add a source of time-consuming operations.

Building custom function

To create custom function, OpelAsyncFunction<T> interface has to be implemented. It is an implementation of the well known adapter design pattern.

public class ExampleTemperatureFunction implements OpelAsyncFunction<BigDecimal> {
    @Override
    public CompletableFuture<BigDecimal> apply(List<CompletableFuture<?>> args) {
        return args.get(0).thenApply(city -> {
            // add code to call external service about temperature in given city
            return BigDecimal.valueOf(22);
        });
    }
}

Register functions in engine

When you build OpelEngine by OpelEngineBuilder you can simply add functions to it (how to implement own function is present in Building custom function section):

OpelEngine engine = OpelEngineBuilder.create()
	.withFunction("myFunctionName", new ExampleTemperatureFunction())
	.build()

Then myFunctionName is avalable in engine and can be call:

String expression = "myFunctionName('arg1', 'arg2') * 100";
engine.eval(expression)
	.whenComplete((result, error) -> System.out.println(result));
Clone this wiki locally