org.jetlinks.rule.engine.defaults.LambdaTaskExecutor Maven / Gradle / Ivy
package org.jetlinks.rule.engine.defaults;
import org.jetlinks.rule.engine.api.RuleData;
import org.jetlinks.rule.engine.api.task.ExecutionContext;
import org.reactivestreams.Publisher;
import reactor.core.publisher.Flux;
import java.util.function.Function;
import java.util.function.Supplier;
public class LambdaTaskExecutor extends FunctionTaskExecutor {
Supplier>> supplier;
Function> function;
public LambdaTaskExecutor(String name,
ExecutionContext context,
Function> function) {
this(name, context, () -> function);
}
public LambdaTaskExecutor(String name,
ExecutionContext context,
Supplier>> supplier) {
super(name, context);
this.supplier = supplier;
this.function = this.supplier.get();
}
@Override
public void reload() {
super.reload();
this.function = this.supplier.get();
}
@Override
protected Publisher apply(RuleData input) {
return Flux.from(function.apply(input))
.map(t -> {
if (t instanceof RuleData) {
return context.newRuleData(t);
}
return context.newRuleData(input.newData(t));
});
}
}