Skip to content

Commit

Permalink
Improved sonataflow-greeting-quarkus dependencies and use of pom prop…
Browse files Browse the repository at this point in the history
…erties + add decisiontable-quarkus-example
  • Loading branch information
fantonangeli committed Mar 15, 2024
1 parent 11640c0 commit b212f13
Show file tree
Hide file tree
Showing 22 changed files with 1,505 additions and 38 deletions.
167 changes: 167 additions & 0 deletions examples/decisiontable-quarkus-example/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
# Decision Table + Quarkus example

| | |
| ------ | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `NOTE` | **Looking for modern decision tables?**<br />Kogito, like Drools, offers support of the [DMN open standard](https://drools.org/learn/dmn.html) for visual and semantic execution of declarative logic. The business rules in this example may be also expressed using DMN decision tables (or other visual paradigm of DMN), instead of Drools XLS decision tables.<br/>For more information about DMN support in Kogito, you may refer to the companion Quarkus guide specific to [Kogito DMN support on Quarkus](https://quarkus.io/guides/kogito-dmn), or the [Kogito DMN documentation](https://docs.kogito.kie.org/latest/html_single/#_using_dmn_models_in_kogito_services). |

## Description

A rule service to validate `LoanApplication` fact. Rules are written as a decision table.

REST endpoints are generated from query rules. You can insert `LoanApplication` facts and query a result via the REST endpoints. Rule resources are assembled as a RuleUnit.

## Installing and Running

### Prerequisites

You will need:

- Java 17+ installed
- Environment variable JAVA_HOME set accordingly
- Maven 3.9.6+ installed

When using native image compilation, you will also need:

- [GraalVM 19.3.1](https://github.com/graalvm/graalvm-ce-builds/releases/tag/vm-19.3.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

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

### Package and Run in JVM mode

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

or on windows

```
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

```
mvn clean package -Pnative
```

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

```
./target/decisiontable-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 /find-approved

Returns approved loan applications from the given facts:

Given facts:

```json
{
"maxAmount": 5000,
"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" }
}
]
}
```

Example curl request (using the JSON above):

```sh
curl -X POST -H 'Accept: application/json' -H 'Content-Type: application/json' -d '{"maxAmount":5000,"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/find-approved
```

or on windows:

```sh
curl -X POST -H "Accept: application/json" -H "Content-Type: application/json" -d "{\"maxAmount\":5000,\"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/find-approved
```

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
}
]
```

### POST /find-not-approved-id-and-amount

Returns ids and amount values of rejected loan applications from the given facts:

Example curl request (using the JSON from previous example):

```sh
curl -X POST -H 'Accept: application/json' -H 'Content-Type: application/json' -d '{"maxAmount":5000,"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/find-not-approved-id-and-amount
```

As response an array of loan application ids and amount values is returned.

Example response:

```json
[
{
"$amount": 5000,
"$id": "ABC10002"
},
{
"$amount": 1000,
"$id": "ABC10015"
}
]
```
31 changes: 31 additions & 0 deletions examples/decisiontable-quarkus-example/env/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.
*/

const { varsWithName, composeEnv, getOrDefault } = require("@kie-tools-scripts/build-env");

module.exports = composeEnv([require("@kie-tools/root-env/env")], {
vars: varsWithName({}),
get env() {
return {
decisiontableQuarkusExample: {
version: require("../package.json").version,
},
};
},
});
27 changes: 27 additions & 0 deletions examples/decisiontable-quarkus-example/install.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.
*/

const buildEnv = require("./env");
const { setup } = require("@kie-tools/maven-config-setup-helper");

setup(`
-Drevision=${buildEnv.env.decisiontableQuarkusExample.version}
-Dquarkus.platform.version=${buildEnv.env.quarkusPlatform.version}
-Dversion.org.kie.kogito=${buildEnv.env.kogitoRuntime.version}
`);
41 changes: 41 additions & 0 deletions examples/decisiontable-quarkus-example/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"private": true,
"name": "@kie-tools-examples/decisiontable-quarkus-example",
"version": "0.0.0",
"description": "",
"license": "Apache-2.0",
"homepage": "https://github.com/apache/incubator-kie-tools",
"repository": {
"type": "git",
"url": "https://github.com/apache/incubator-kie-tools.git"
},
"bugs": {
"url": "https://github.com/apache/incubator-kie-tools/issues"
},
"scripts": {
"build:dev": "run-script-os",
"build:dev:darwin:linux": "mvn clean package -DskipTests",
"build:dev:win32": "pnpm powershell \"mvn clean package -DskipTests \"",
"build:prod": "pnpm lint && run-script-os",
"build:prod:darwin:linux": "mvn clean package -DskipTests=$(build-env tests.run --not) -Dmaven.test.failure.ignore=$(build-env tests.ignoreFailures)",
"build:prod:win32": "pnpm powershell \"mvn clean package `-DskipTests `-Dmaven.test.failure.ignore=$(build-env tests.ignoreFailures)\"",
"install": "node install.js",
"lint": "echo 'Linting'",
"powershell": "@powershell -NoProfile -ExecutionPolicy Unrestricted -Command",
"quarkus:dev": "run-script-os",
"quarkus:dev:darwin:linux": "mvn clean package quarkus:dev -DskipTests",
"quarkus:dev:win32": "mvn clean package quarkus:dev -DskipTests",
"start": "pnpm quarkus:dev"
},
"devDependencies": {
"@kie-tools/maven-config-setup-helper": "workspace:*",
"@kie-tools/root-env": "workspace:*",
"run-script-os": "^1.1.6"
},
"kieTools": {
"requiredPreinstalledCliCommands": [
"java",
"mvn"
]
}
}
Loading

0 comments on commit b212f13

Please sign in to comment.