deliveredtechnologies / rulebook

100% Java, Lambda Enabled, Lightweight Rules Engine with a Simple and Intuitive DSL
http://www.deliveredtechnologies.com
Apache License 2.0
716 stars 124 forks source link

Futures/CompletionStage/Async #203

Open ktalebian opened 3 years ago

ktalebian commented 3 years ago

How do you create a role where the condition/evaluation is async? For example, I need to make a HTTP request to verify a fact and return the result.

Clayton7510 commented 3 years ago

If you are using something like Spring Boot to accept the HTTP request then you can just create your RuleBook as a Bean and Autowire it into your Controller.

Check out this example: https://github.com/deliveredtechnologies/rulebook/tree/develop/rulebook-examples/helloworld-spring-web

ktalebian commented 3 years ago

@Clayton7510 I'm not sure how this applies to my original question. The rule itself needs to be async. In the link you provided, the when condition is still sync https://github.com/deliveredtechnologies/rulebook/blob/develop/rulebook-examples/helloworld-spring-web/src/main/java/com/example/rulebook/helloworld/HelloSpringRule.java

Basically, instead of

@When
public boolean when() {
  return hello != null;
}

I need

@When
public CompletionStage<boolean> when() {
  return getRequest().thenApply(result => result != null);
}