Skip to content

Commit

Permalink
[KOGITO-5867] RuleUnit real world example
Browse files Browse the repository at this point in the history
- multiple RuleUnit
- Still based on LoanApplication
- WIP
  • Loading branch information
tkobayas committed Sep 17, 2021
1 parent bb0d7f9 commit ea1134e
Show file tree
Hide file tree
Showing 17 changed files with 927 additions and 0 deletions.
135 changes: 135 additions & 0 deletions multi-ruleunit-quarkus-example/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
# Multiple RuleUnit + Quarkus example

## Description

A complex rule service to validate `LoanApplication` fact.

CustomQueryFindApprovedEndpoint is implemented to coordinate multiple RuleUnits. You can insert `LoanApplication` facts and query a result via the REST endpoints.

## Installing and Running

### Prerequisites

You will need:
- Java 11+ installed
- Environment variable JAVA_HOME set accordingly
- Maven 3.6.2+ installed

When using native image compilation, you will also need:
- [GraalVM 19.2.1](https://github.com/oracle/graal/releases/tag/vm-19.2.1) installed
- Environment variable GRAALVM_HOME set accordingly
- Note that GraalVM native image compilation typically requires other packages (glibc-devel, zlib-devel and gcc) to be installed too. You also need 'native-image' installed in GraalVM (using 'gu install native-image'). Please refer to [GraalVM installation documentation](https://www.graalvm.org/docs/reference-manual/aot-compilation/#prerequisites) for more details.

### Compile and Run in Local Dev Mode

```sh
mvn clean compile quarkus:dev
```

### Package and Run in JVM mode

```sh
mvn clean package
java -jar target/quarkus-app/quarkus-run.jar
```

or on windows

```sh
mvn clean package
java -jar target\quarkus-app\quarkus-run.jar
```

### Package and Run using Local Native Image
Note that this requires GRAALVM_HOME to point to a valid GraalVM installation

```sh
mvn clean package -Pnative
```

To run the generated native executable, generated in `target/`, execute

```sh
./target/ruleunit-quarkus-example-runner
```

Note: This does not yet work on Windows, GraalVM and Quarkus should be rolling out support for Windows soon.

## OpenAPI (Swagger) documentation
[Specification at swagger.io](https://swagger.io/docs/specification/about/)

You can take a look at the [OpenAPI definition](http://localhost:8080/openapi?format=json) - automatically generated and included in this service - to determine all available operations exposed by this service. For easy readability you can visualize the OpenAPI definition file using a UI tool like for example available [Swagger UI](https://editor.swagger.io).

In addition, various clients to interact with this service can be easily generated using this OpenAPI definition.

When running in either Quarkus Development or Native mode, we also leverage the [Quarkus OpenAPI extension](https://quarkus.io/guides/openapi-swaggerui#use-swagger-ui-for-development) that exposes [Swagger UI](http://localhost:8080/swagger-ui/) that you can use to look at available REST endpoints and send test requests.

## Example Usage

Once the service is up and running, you can use the following examples to interact with the service. Note that rather than using the curl commands below, you can also use the [swagger UI](http://localhost:8080/swagger-ui/) to send requests.

### POST /custom

Returns approved loan applications from the given facts. Note that "DivisionA" is specified:

```sh
curl -X POST -H 'Accept: application/json' -H 'Content-Type: application/json' -d '{"division":"DivisionA","loanApplications":[{"id":"ABC10001","amount":2000,"deposit":100,"applicant":{"age":45,"name":"John"}}, {"id":"ABC10002","amount":5000,"deposit":100,"applicant":{"age":25,"name":"Paul"}}, {"id":"ABC10015","amount":1000,"deposit":100,"applicant":{"age":12,"name":"George"}}]}' http://localhost:8080/custom
```
As response an array of loan applications is returned.

Example response:

```json
[
{
"id":"ABC10001",
"applicant":{
"name":"John",
"age":45
},
"amount":2000,
"deposit":100,
"approved":true,
"completed":true
}
]
```

If you send the loan applications to "DivisionB":

```sh
curl -X POST -H 'Accept: application/json' -H 'Content-Type: application/json' -d '{"division":"DivisionB","loanApplications":[{"id":"ABC10001","amount":2000,"deposit":100,"applicant":{"age":45,"name":"John"}}, {"id":"ABC10002","amount":5000,"deposit":100,"applicant":{"age":25,"name":"Paul"}}, {"id":"ABC10015","amount":1000,"deposit":100,"applicant":{"age":12,"name":"George"}}]}' http://localhost:8080/custom
```

DivisionB rules are more tolerant so 2 applications are approved.

```json
[
{
"id":"ABC10001",
"applicant":{
"name":"John",
"age":45
},
"amount":2000,
"deposit":100,
"approved":true,
"completed":true
},
{
"id":"ABC10002",
"applicant":{
"name":"Paul",
"age":25
},
"amount":5000,
"deposit":100,
"approved":true,
"completed":true
}
]
```

## Deploying with Kogito Operator

In the [`operator`](operator) directory you'll find the custom resources needed to deploy this example on OpenShift with the [Kogito Operator](https://docs.jboss.org/kogito/release/latest/html_single/#chap_kogito-deploying-on-openshift).
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
apiVersion: app.kiegroup.org/v1beta1
kind: KogitoBuild
metadata:
name: multi-ruleunit-quarkus-example
spec:
type: RemoteSource
#env:
# env can be used to set variables during build
#- name: MY_CUSTOM_ENV
# value: "my value"
gitSource:
contextDir: multi-ruleunit-quarkus-example
uri: 'https://github.com/kiegroup/kogito-examples'
# set your maven nexus repository to speed up the build time
#mavenMirrorURL:
---
apiVersion: app.kiegroup.org/v1beta1
kind: KogitoRuntime
metadata:
name: multi-ruleunit-quarkus-example
86 changes: 86 additions & 0 deletions multi-ruleunit-quarkus-example/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.kie.kogito.examples</groupId>
<artifactId>kogito-examples</artifactId>
<version>2.0.0-SNAPSHOT</version>
</parent>
<artifactId>multi-ruleunit-quarkus-example</artifactId>
<name>Kogito Example :: Multiple RuleUnit - Quarkus</name>
<properties>
<quarkus-plugin.version>2.1.0.Final</quarkus-plugin.version>
<quarkus.platform.artifact-id>quarkus-bom</quarkus.platform.artifact-id>
<quarkus.platform.group-id>io.quarkus</quarkus.platform.group-id>
<quarkus.platform.version>2.1.0.Final</quarkus.platform.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>${quarkus.platform.group-id}</groupId>
<artifactId>${quarkus.platform.artifact-id}</artifactId>
<version>${quarkus.platform.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.kie.kogito</groupId>
<artifactId>kogito-quarkus-rules</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-resteasy-jackson</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-resteasy</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-arc</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-smallrye-openapi</artifactId>
</dependency>
<dependency>
<groupId>org.kie.kogito</groupId>
<artifactId>kogito-drools</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-junit5</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-smallrye-health</artifactId>
</dependency>
</dependencies>
<build>
<finalName>${project.artifactId}</finalName>
<plugins>
<plugin>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-maven-plugin</artifactId>
<version>${quarkus-plugin.version}</version>
<executions>
<execution>
<goals>
<goal>build</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/*
* Copyright 2021 Red Hat, Inc. and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.kie.kogito.examples;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import org.kie.kogito.examples.division.a.LoanDivisionAUnit;
import org.kie.kogito.examples.division.b.LoanDivisionBUnit;
import org.kie.kogito.examples.model.LoanApplication;
import org.kie.kogito.rules.DataStore;
import org.kie.kogito.rules.RuleUnit;
import org.kie.kogito.rules.RuleUnitInstance;

@Path("/custom")
public class CustomQueryFindApprovedEndpoint {

@javax.inject.Inject
RuleUnit<org.kie.kogito.examples.LoanPreprocessUnit> preprocessRuleUnit;

@javax.inject.Inject
RuleUnit<org.kie.kogito.examples.division.a.LoanDivisionAUnit> divisionARuleUnit;

@javax.inject.Inject
RuleUnit<org.kie.kogito.examples.division.b.LoanDivisionBUnit> divisionBRuleUnit;

@javax.inject.Inject
RuleUnit<org.kie.kogito.examples.LoanQueryUnit> queryRuleUnit;

// @javax.inject.Inject
// Application application;

public CustomQueryFindApprovedEndpoint() {
}

public CustomQueryFindApprovedEndpoint(RuleUnit<org.kie.kogito.examples.LoanPreprocessUnit> preprocessRuleUnit) {
this.preprocessRuleUnit = preprocessRuleUnit;
}

@POST()
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public List<org.kie.kogito.examples.model.LoanApplication> executeQuery(org.kie.kogito.examples.LoanPreprocessUnit unitDTO) {
// Loan preprocess
RuleUnitInstance<org.kie.kogito.examples.LoanPreprocessUnit> preprocessInstance = preprocessRuleUnit.createInstance(unitDTO);
preprocessInstance.fire();
preprocessInstance.dispose();
DataStore<LoanApplication> loanApplications = unitDTO.getLoanApplications();

// custom logic to choose the next RuleUnit
String division = unitDTO.getDivision();
if (division.equals("DivisionA")) {
// get LoanDivisionAUnit instance
System.out.println("DivisionA");
LoanDivisionAUnit unitData = new LoanDivisionAUnit(loanApplications);
RuleUnitInstance<LoanDivisionAUnit> divisionAInstance = divisionARuleUnit.createInstance(unitData);
divisionAInstance.fire();
divisionAInstance.dispose();
} else if (division.equals("DivisionB")) {
// get LoanDivisionBUnit instance
System.out.println("DivisionB");
LoanDivisionBUnit unitData = new LoanDivisionBUnit(loanApplications);
RuleUnitInstance<LoanDivisionBUnit> divisionBInstance = divisionBRuleUnit.createInstance(unitData);
divisionBInstance.fire();
divisionBInstance.dispose();
}

// query finally
LoanQueryUnit unitData = new LoanQueryUnit(loanApplications);
RuleUnitInstance<LoanQueryUnit> queryInstance = queryRuleUnit.createInstance(unitData);
List<org.kie.kogito.examples.model.LoanApplication> response = queryInstance.executeQuery("FindApproved").stream().map(this::toResult).collect(Collectors.toList());
queryInstance.dispose();

return response;
}

private org.kie.kogito.examples.model.LoanApplication toResult(Map<String, Object> tuple) {
return (org.kie.kogito.examples.model.LoanApplication) tuple.get("$l");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Copyright 2020 Red Hat, Inc. and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.kie.kogito.examples;

import org.kie.kogito.examples.model.LoanApplication;
import org.kie.kogito.rules.DataSource;
import org.kie.kogito.rules.DataStore;
import org.kie.kogito.rules.RuleUnitData;

public class LoanPreprocessUnit implements RuleUnitData {

private String division;

private DataStore<LoanApplication> loanApplications;

public LoanPreprocessUnit() {
this(DataSource.createStore());
}

public LoanPreprocessUnit(DataStore<LoanApplication> loanApplications) {
this.loanApplications = loanApplications;
}

public DataStore<LoanApplication> getLoanApplications() {
return loanApplications;
}

public void setLoanApplications(DataStore<LoanApplication> loanApplications) {
this.loanApplications = loanApplications;
}

public String getDivision() {
return division;
}

public void setDivision(String division) {
this.division = division;
}

}
Loading

0 comments on commit ea1134e

Please sign in to comment.