Skip to content

Commit

Permalink
[KOGITO-6039] Example: Compare RuleUnit coordination to KieBase compo…
Browse files Browse the repository at this point in the history
…sition

- Custom REST endpoint example where 2 RuleUnits are executed.
  • Loading branch information
tkobayas committed Oct 7, 2021
1 parent 02eeee0 commit 61f5151
Show file tree
Hide file tree
Showing 13 changed files with 645 additions and 0 deletions.
48 changes: 48 additions & 0 deletions multi-ruleunit-quarkus-example-poc/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
### POST /find-invalid-applicant

Returns applicants who are considered as invalid by ApplicantValidationUnit rules:

```sh
curl -X POST -H 'Accept: application/json' -H 'Content-Type: application/json' -d '{"loanApplications":[{"id":"ABC10001","amount":2000,"deposit":100,"applicant":{"age":45,"name":"John","creditScore":20}}, {"id":"ABC10002","amount":500,"deposit":100,"applicant":{"age":25,"name":"Paul","creditScore":200}}, {"id":"ABC10015","amount":1000,"deposit":100,"applicant":{"age":12,"name":"George","creditScore":200}}, {"id":"ABC10020","amount":5000,"deposit":100,"applicant":{"age":30,"name":"Ringo","creditScore":200}}]}' http://localhost:8080/find-invalid-applicant
```

George is invalid due to his age. John is invalid due to his creditScore.

Example response:
```json
[{"name":"George","age":12,"creditScore":200,"invalid":true},{"name":"John","age":45,"creditScore":20,"invalid":true}]
```


### POST /find-approved

Returns approved loan applications by LoanUnit rules:

```sh
curl -X POST -H 'Accept: application/json' -H 'Content-Type: application/json' -d '{"loanApplications":[{"id":"ABC10001","amount":2000,"deposit":100,"applicant":{"age":45,"name":"John","creditScore":20}}, {"id":"ABC10002","amount":500,"deposit":100,"applicant":{"age":25,"name":"Paul","creditScore":200}}, {"id":"ABC10015","amount":1000,"deposit":100,"applicant":{"age":12,"name":"George","creditScore":200}}, {"id":"ABC10020","amount":5000,"deposit":100,"applicant":{"age":30,"name":"Ringo","creditScore":200}}]}' http://localhost:8080/find-approved
```

Ringo was rejected but other 3 loan applications are approved because LoanUnit doesn't validate applicant's age and creditScore.

Example response:

```json
[{"id":"ABC10002","applicant":{"name":"Paul","age":25,"creditScore":200,"invalid":false},"amount":500,"deposit":100,"approved":true},{"id":"ABC10001","applicant":{"name":"John","age":45,"creditScore":20,"invalid":false},"amount":2000,"deposit":100,"approved":true},{"id":"ABC10015","applicant":{"name":"George","age":12,"creditScore":200,"invalid":false},"amount":1000,"deposit":100,"approved":true}]
```

### POST /2units

Custom REST endpoint to execute ApplicantValidationUnit and LoanUnit. Then reply with "FindApproved" query.

```sh
curl -X POST -H 'Accept: application/json' -H 'Content-Type: application/json' -d '{"loanApplications":[{"id":"ABC10001","amount":2000,"deposit":100,"applicant":{"age":45,"name":"John","creditScore":20}}, {"id":"ABC10002","amount":500,"deposit":100,"applicant":{"age":25,"name":"Paul","creditScore":200}}, {"id":"ABC10015","amount":1000,"deposit":100,"applicant":{"age":12,"name":"George","creditScore":200}}, {"id":"ABC10020","amount":5000,"deposit":100,"applicant":{"age":30,"name":"Ringo","creditScore":200}}]}' http://localhost:8080/2units
```

Only Paul is approved because it passes both ApplicantValidationUnit and LoanUnit rules.

Example response:

```json
[{"id":"ABC10002","applicant":{"name":"Paul","age":25,"creditScore":200,"invalid":false},"amount":500,"deposit":100,"approved":true}]
```

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-poc/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-poc</artifactId>
<name>Kogito Example :: Multiple RuleUnit - Quarkus</name>
<properties>
<quarkus-plugin.version>2.3.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.3.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,67 @@
/*
* 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;

public class Applicant {

private String name;
private int age;
private int creditScore;
private boolean invalid = false; // false by default

public Applicant() {
}

public Applicant(String name, int age, int creditScore) {
super();
this.name = name;
this.age = age;
this.creditScore = creditScore;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}

public int getCreditScore() {
return creditScore;
}

public void setCreditScore(int creditScore) {
this.creditScore = creditScore;
}

public boolean isInvalid() {
return invalid;
}

public void setInvalid(boolean invalid) {
this.invalid = invalid;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* 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.rules.DataSource;
import org.kie.kogito.rules.DataStore;
import org.kie.kogito.rules.RuleUnitData;

public class ApplicantValidationUnit implements RuleUnitData {

private DataStore<LoanApplication> loanApplications;

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

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

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

public void setLoanApplications(DataStore<LoanApplication> loanApplications) {
this.loanApplications = loanApplications;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* 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;

public class LoanApplication {

private String id;

private Applicant applicant;

private int amount;

private int deposit;

private boolean approved = false; // false by default

public LoanApplication() {
}

public LoanApplication(String id, Applicant applicant, int amount, int deposit, boolean approved) {
super();
this.id = id;
this.applicant = applicant;
this.amount = amount;
this.deposit = deposit;
this.approved = approved;
}

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

public Applicant getApplicant() {
return applicant;
}

public void setApplicant(Applicant applicant) {
this.applicant = applicant;
}

public boolean isApproved() {
return approved;
}

public void setApproved(boolean approved) {
this.approved = approved;
}

public int getAmount() {
return amount;
}

public void setAmount(int amount) {
this.amount = amount;
}

public int getDeposit() {
return deposit;
}

public void setDeposit(int deposit) {
this.deposit = deposit;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* 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.rules.DataSource;
import org.kie.kogito.rules.DataStore;
import org.kie.kogito.rules.RuleUnitData;

public class LoanUnit implements RuleUnitData {

private DataStore<LoanApplication> loanApplications;

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

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

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

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

0 comments on commit 61f5151

Please sign in to comment.