diff --git a/multi-ruleunit-quarkus-example-poc03/README.md b/multi-ruleunit-quarkus-example-poc03/README.md new file mode 100644 index 0000000000..476ea5a156 --- /dev/null +++ b/multi-ruleunit-quarkus-example-poc03/README.md @@ -0,0 +1,29 @@ +### POST /2units + +Custom REST endpoint to execute CommonUnit and LoanUnit. Then reply with "FindApproved" query. + + +1st example) + +```sh +curl -X POST -H 'Accept: application/json' -H 'Content-Type: application/json' -d '{"loanApplications":[{"id":"ABC10001","amount":5000,"deposit":3000,"applicant":{"age":45,"name":"John","creditScore":1000,"occupationCode":"1021","previousOccupationCode":"1025"}}]}' http://localhost:8080/2units +``` + +This loan application is approved. + +response: + +```json +[{"id":"ABC10001","applicant":{"name":"John","age":45,"creditScore":1000,"occupationCode":"1021","previousOccupationCode":"1025","occupationCategory":"A"},"amount":5000,"deposit":3000,"approved":true}] +``` + +2nd example) + +```sh +curl -X POST -H 'Accept: application/json' -H 'Content-Type: application/json' -d '{"loanApplications":[{"id":"ABC10002","amount":5000,"deposit":3000,"applicant":{"age":43,"name":"Paul","creditScore":1000,"occupationCode":null,"previousOccupationCode":"2099"}}]}' http://localhost:8080/2units +``` + +This loan application is expected to be approved because rule "LargeDepositWithoutCurrentOccupation" in LoanUnit should trigger rule "occupationCategoryA2" in CommonUnit in case of "kbase composition". However, this example is "RuleUnit orchestration" so CommonUnit -> LoanUnit is executed sequentially. So "occupationCategoryA2" is not triggered. + + + diff --git a/multi-ruleunit-quarkus-example-poc03/operator/multi-ruleunit-quarkus-example.yaml b/multi-ruleunit-quarkus-example-poc03/operator/multi-ruleunit-quarkus-example.yaml new file mode 100644 index 0000000000..d149fd66a7 --- /dev/null +++ b/multi-ruleunit-quarkus-example-poc03/operator/multi-ruleunit-quarkus-example.yaml @@ -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 \ No newline at end of file diff --git a/multi-ruleunit-quarkus-example-poc03/pom.xml b/multi-ruleunit-quarkus-example-poc03/pom.xml new file mode 100644 index 0000000000..9ef56504a5 --- /dev/null +++ b/multi-ruleunit-quarkus-example-poc03/pom.xml @@ -0,0 +1,86 @@ + + + 4.0.0 + + org.kie.kogito.examples + kogito-examples + 2.0.0-SNAPSHOT + + multi-ruleunit-quarkus-example-poc03 + Kogito Example :: Multiple RuleUnit PoC 03 - Quarkus + + 2.3.0.Final + quarkus-bom + io.quarkus + 2.3.0.Final + + + + + ${quarkus.platform.group-id} + ${quarkus.platform.artifact-id} + ${quarkus.platform.version} + pom + import + + + + + + org.kie.kogito + kogito-quarkus-rules + + + io.quarkus + quarkus-resteasy-jackson + + + io.quarkus + quarkus-resteasy + + + io.quarkus + quarkus-arc + + + io.quarkus + quarkus-smallrye-openapi + + + org.kie.kogito + kogito-drools + + + io.quarkus + quarkus-junit5 + test + + + io.rest-assured + rest-assured + test + + + io.quarkus + quarkus-smallrye-health + + + + ${project.artifactId} + + + io.quarkus + quarkus-maven-plugin + ${quarkus-plugin.version} + + + + build + + + + + + + diff --git a/multi-ruleunit-quarkus-example-poc03/src/main/java/org/kie/kogito/examples/Applicant.java b/multi-ruleunit-quarkus-example-poc03/src/main/java/org/kie/kogito/examples/Applicant.java new file mode 100644 index 0000000000..c5864f69ee --- /dev/null +++ b/multi-ruleunit-quarkus-example-poc03/src/main/java/org/kie/kogito/examples/Applicant.java @@ -0,0 +1,87 @@ +/* + * 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 String occupationCode; + private String previousOccupationCode; + private String occupationCategory = null; // calculated by common rules + + public Applicant() { + } + + public Applicant(String name, int age, int creditScore, String occupationCode, String previousOccupationCode) { + super(); + this.name = name; + this.age = age; + this.creditScore = creditScore; + this.occupationCode = occupationCode; + this.previousOccupationCode = previousOccupationCode; + } + + 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 String getOccupationCode() { + return occupationCode; + } + + public void setOccupationCode(String occupationCode) { + this.occupationCode = occupationCode; + } + + public String getPreviousOccupationCode() { + return previousOccupationCode; + } + + public void setPreviousOccupationCode(String previousOccupationCode) { + this.previousOccupationCode = previousOccupationCode; + } + + public String getOccupationCategory() { + return occupationCategory; + } + + public void setOccupationCategory(String occupationCategory) { + this.occupationCategory = occupationCategory; + } + +} diff --git a/multi-ruleunit-quarkus-example-poc03/src/main/java/org/kie/kogito/examples/CommonUnit.java b/multi-ruleunit-quarkus-example-poc03/src/main/java/org/kie/kogito/examples/CommonUnit.java new file mode 100644 index 0000000000..a1df0ec8ca --- /dev/null +++ b/multi-ruleunit-quarkus-example-poc03/src/main/java/org/kie/kogito/examples/CommonUnit.java @@ -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 CommonUnit implements RuleUnitData { + + private DataStore loanApplications; + + public CommonUnit() { + this(DataSource.createStore()); + } + + public CommonUnit(DataStore loanApplications) { + this.loanApplications = loanApplications; + } + + public DataStore getLoanApplications() { + return loanApplications; + } + + public void setLoanApplications(DataStore loanApplications) { + this.loanApplications = loanApplications; + } +} diff --git a/multi-ruleunit-quarkus-example-poc03/src/main/java/org/kie/kogito/examples/LoanApplication.java b/multi-ruleunit-quarkus-example-poc03/src/main/java/org/kie/kogito/examples/LoanApplication.java new file mode 100644 index 0000000000..236bb9df68 --- /dev/null +++ b/multi-ruleunit-quarkus-example-poc03/src/main/java/org/kie/kogito/examples/LoanApplication.java @@ -0,0 +1,86 @@ +/* + * 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) { + super(); + this.id = id; + this.applicant = applicant; + this.amount = amount; + this.deposit = deposit; + } + + 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; + } + + @Override + public String toString() { + return "LoanApplication [id=" + id + ", applicant=" + applicant + ", amount=" + amount + ", deposit=" + deposit + ", approved=" + approved + "]"; + } + +} diff --git a/multi-ruleunit-quarkus-example-poc03/src/main/java/org/kie/kogito/examples/LoanUnit.java b/multi-ruleunit-quarkus-example-poc03/src/main/java/org/kie/kogito/examples/LoanUnit.java new file mode 100644 index 0000000000..01471b4949 --- /dev/null +++ b/multi-ruleunit-quarkus-example-poc03/src/main/java/org/kie/kogito/examples/LoanUnit.java @@ -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 loanApplications; + + public LoanUnit() { + this(DataSource.createStore()); + } + + public LoanUnit(DataStore loanApplications) { + this.loanApplications = loanApplications; + } + + public DataStore getLoanApplications() { + return loanApplications; + } + + public void setLoanApplications(DataStore loanApplications) { + this.loanApplications = loanApplications; + } +} diff --git a/multi-ruleunit-quarkus-example-poc03/src/main/java/org/kie/kogito/examples/TwoUnitsEndpoint.java b/multi-ruleunit-quarkus-example-poc03/src/main/java/org/kie/kogito/examples/TwoUnitsEndpoint.java new file mode 100644 index 0000000000..808cd910ba --- /dev/null +++ b/multi-ruleunit-quarkus-example-poc03/src/main/java/org/kie/kogito/examples/TwoUnitsEndpoint.java @@ -0,0 +1,66 @@ +/* + * 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.rules.DataStore; +import org.kie.kogito.rules.RuleUnit; +import org.kie.kogito.rules.RuleUnitInstance; + +@Path("/2units") +public class TwoUnitsEndpoint { + + @javax.inject.Inject + RuleUnit commonUnit; + + @javax.inject.Inject + RuleUnit loanUnit; + + public TwoUnitsEndpoint() { + } + + @POST() + @Produces(MediaType.APPLICATION_JSON) + @Consumes(MediaType.APPLICATION_JSON) + public List executeQuery(org.kie.kogito.examples.CommonUnit unitDTO) { + // 1. Execute commonUnit + RuleUnitInstance commonUnitInstance = commonUnit.createInstance(unitDTO); + commonUnitInstance.fire(); + commonUnitInstance.dispose(); + + // 2. Execute LoanUnit + DataStore loanApplications = unitDTO.getLoanApplications(); + LoanUnit loanUnitData = new LoanUnit(loanApplications); + RuleUnitInstance loanUnitInstance = loanUnit.createInstance(loanUnitData); + List response = loanUnitInstance.executeQuery("FindApproved").stream().map(this::toResult).collect(Collectors.toList()); + loanUnitInstance.dispose(); + + return response; + } + + private LoanApplication toResult(Map tuple) { + return (LoanApplication) tuple.get("$l"); + } +} diff --git a/multi-ruleunit-quarkus-example-poc03/src/main/resources/application.properties b/multi-ruleunit-quarkus-example-poc03/src/main/resources/application.properties new file mode 100644 index 0000000000..9557fd6d04 --- /dev/null +++ b/multi-ruleunit-quarkus-example-poc03/src/main/resources/application.properties @@ -0,0 +1,7 @@ +# Packaging +# quarkus.package.type=fast-jar + +quarkus.swagger-ui.always-include=true + +# Maximum Java heap to be used during the native image generation +quarkus.native.native-image-xmx=4g diff --git a/multi-ruleunit-quarkus-example-poc03/src/main/resources/org/kie/kogito/examples/CommonUnit.drl b/multi-ruleunit-quarkus-example-poc03/src/main/resources/org/kie/kogito/examples/CommonUnit.drl new file mode 100644 index 0000000000..c5a75372a3 --- /dev/null +++ b/multi-ruleunit-quarkus-example-poc03/src/main/resources/org/kie/kogito/examples/CommonUnit.drl @@ -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; +unit CommonUnit; + +import org.kie.kogito.examples.LoanApplication; + +// ---------------------- +// common format check +// ---------------------- + +rule formatCheckName +salience 1000 +when + $l: /loanApplications[ applicant.name == null || applicant.name == "" ] +then + modify($l) { setApproved(false) }; +end + +// more... + +// ---------------------- +// common code conversion +// ---------------------- + +rule occupationCategoryA1 +when + $l: /loanApplications[ applicant.occupationCategory == null, applicant.occupationCode == "1021" ] +then + Applicant $a = $l.getApplicant(); + $a.setOccupationCategory("A"); + modify($l) { setApplicant($a) }; +end + +rule occupationCategoryA2 +when + $l: /loanApplications[ applicant.occupationCategory == null, applicant.occupationCode == "2099" ] +then + System.out.println("*** occupationCategoryA2"); + Applicant $a = $l.getApplicant(); + $a.setOccupationCategory("A"); + modify($l) { setApplicant($a) }; +end + +rule occupationCategoryB +when + $l: /loanApplications[ applicant.occupationCategory == null, applicant.occupationCode == "1025" ] +then + Applicant $a = $l.getApplicant(); + $a.setOccupationCategory("B"); + modify($l) { setApplicant($a) }; +end + +// more.... diff --git a/multi-ruleunit-quarkus-example-poc03/src/main/resources/org/kie/kogito/examples/LoanUnit.drl b/multi-ruleunit-quarkus-example-poc03/src/main/resources/org/kie/kogito/examples/LoanUnit.drl new file mode 100644 index 0000000000..786b9ea67e --- /dev/null +++ b/multi-ruleunit-quarkus-example-poc03/src/main/resources/org/kie/kogito/examples/LoanUnit.drl @@ -0,0 +1,69 @@ +/** + * 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; +unit LoanUnit; + +import org.kie.kogito.examples.LoanApplication; + +// ---------------------- +// loan approval +// ---------------------- + +rule SmallDepositApprove when + $l: /loanApplications[ deposit < 1000, amount <= 2000, applicant.occupationCategory == "A" ] +then + modify($l) { setApproved(true) }; +end + +rule SmallDepositReject when + $l: /loanApplications[ deposit < 1000, amount > 2000, applicant.occupationCategory == "A" ] +then + modify($l) { setApproved(false) }; +end + +rule LargeDepositApprove when + $l: /loanApplications[ deposit >= 1000, amount <= 10000, applicant.occupationCategory == "A" ] +then + modify($l) { setApproved(true) }; +end + +rule LargeDepositReject when + $l: /loanApplications[ deposit >= 1000, amount > 10000, applicant.occupationCategory == "A" ] +then + modify($l) { setApproved(false) }; +end + +// more... + +// ---------------------- +// special cases +// ---------------------- +rule LargeDepositWithoutCurrentOccupation +no-loop +when + $l: /loanApplications[ deposit >= 1000, applicant.occupationCode == null ] +then + System.out.println("*** LargeDepositWithoutCurrentOccupation"); + Applicant $a = $l.getApplicant(); + $a.setOccupationCode($a.getPreviousOccupationCode()); // evaluate based on previous occupation + modify($l) { setApplicant($a) }; // this triggers common rule "occupationCategory*" +end + +query FindApproved + $l: /loanApplications[ approved ] +end + + diff --git a/multi-ruleunit-quarkus-example-poc03/src/test/resources/application.properties b/multi-ruleunit-quarkus-example-poc03/src/test/resources/application.properties new file mode 100644 index 0000000000..8c5dc5bd35 --- /dev/null +++ b/multi-ruleunit-quarkus-example-poc03/src/test/resources/application.properties @@ -0,0 +1,5 @@ +# Quarkus +quarkus.http.test-port=0 + +# Maximum Java heap to be used during the native image generation +quarkus.native.native-image-xmx=4g