diff --git a/serverless-workflow-examples/pom.xml b/serverless-workflow-examples/pom.xml index 4fac574db8..962cbd298a 100644 --- a/serverless-workflow-examples/pom.xml +++ b/serverless-workflow-examples/pom.xml @@ -53,6 +53,7 @@ serverless-workflow-custom-type serverless-workflow-data-index-persistence-addon-quarkus serverless-workflow-data-index-quarkus + serverless-workflow-dmn-quarkus serverless-workflow-error-quarkus serverless-workflow-events-quarkus serverless-workflow-expression-quarkus diff --git a/serverless-workflow-examples/serverless-workflow-dmn-quarkus/README.md b/serverless-workflow-examples/serverless-workflow-dmn-quarkus/README.md new file mode 100644 index 0000000000..73a37c0470 --- /dev/null +++ b/serverless-workflow-examples/serverless-workflow-dmn-quarkus/README.md @@ -0,0 +1,115 @@ +# Kogito Serverless Workflow - DMN Example + +## Description + +This example contains a simple workflow service that use DMN. +The services are described using JSON format as defined in the +[CNCF Serverless Workflow specification](https://github.com/serverlessworkflow/specification). + +The workflow expects as JSON input containing driver details and a traffic violation +(see details in the [Submit a request](#Submit-a-request) section). + +The workflow uses that input to execute a decision file which evaluates if the driver should be suspended or not. + +## 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](https://www.graalvm.org/downloads/) 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 + +```sh +mvn clean package quarkus:dev +``` + +### Compile 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 +``` + +### Compile 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/serverless-workflow-dmn-quarkus-{version}-runner +``` + +## Example Usage + +Once the service is up and running, you can use the following example to interact with the service. + +### POST /Traffic Violation + +Returns penalty information from the given inputs -- driver and violation: + +Given inputs: + +```json +{ + "Driver":{"Points":2}, + "Violation":{ + "Type":"speed", + "Actual Speed":120, + "Speed Limit":100 + } +} +``` + +Curl command (using the JSON object above): + +```sh +curl -X POST -H 'Accept: application/json' -H 'Content-Type: application/json' -d '{"Driver":{"Points":2},"Violation":{"Type":"speed","Actual Speed":120,"Speed Limit":100}}' http://localhost:8080/traffic-violation +``` +or on Windows: + +```sh +curl -X POST -H "Accept: application/json" -H "Content-Type: application/json" -d "{\"Driver\":{\"Points\":2},\"Violation\":{\"Type\":\"speed\",\"Actual Speed\":120,\"Speed Limit\":100}}" http://localhost:8080/traffic-violation +``` + +As response, penalty information is returned. + +Example response: + +```json +{"workflowdata": + { + "Violation":{ + "Type":"speed", + "Speed Limit":100, + "Actual Speed":120 + }, + "Driver":{ + "Points":2 + }, + "Fine":{ + "Points":3, + "Amount":500 + }, + "Should the driver be suspended?":"No" + } +} +``` \ No newline at end of file diff --git a/serverless-workflow-examples/serverless-workflow-dmn-quarkus/pom.xml b/serverless-workflow-examples/serverless-workflow-dmn-quarkus/pom.xml new file mode 100644 index 0000000000..626db11bf3 --- /dev/null +++ b/serverless-workflow-examples/serverless-workflow-dmn-quarkus/pom.xml @@ -0,0 +1,182 @@ + + + + 4.0.0 + + + org.kie.kogito.examples + serverless-workflow-examples-parent + 999-SNAPSHOT + ../serverless-workflow-examples-parent/pom.xml + + + org.kie.kogito.examples + serverless-workflow-dmn-quarkus + 1.0-SNAPSHOT + + Kogito Example :: Serverless Workflow :: DMN:: Quarkus + Kogito Serverless Workflow DMN Example - Quarkus + + 3.2.10.Final + quarkus-bom + io.quarkus + 3.2.10.Final + org.kie.kogito + kogito-bom + 999-SNAPSHOT + 17 + 3.8.1 + 3.0.0-M7 + + + + + + ${quarkus.platform.group-id} + ${quarkus.platform.artifact-id} + ${quarkus.platform.version} + pom + import + + + ${kogito.bom.group-id} + ${kogito.bom.artifact-id} + ${kogito.bom.version} + pom + import + + + + + + org.apache.kie.sonataflow + sonataflow-quarkus + + + io.quarkus + quarkus-resteasy + + + org.drools + drools-quarkus-decisions + + + org.kie.kogito + kogito-serverless-workflow-dmn-parser + + + io.quarkus + quarkus-resteasy-jackson + + + org.kie + kie-addons-quarkus-source-files + + + io.quarkus + quarkus-junit5 + test + + + io.rest-assured + rest-assured + test + + + io.quarkus + quarkus-smallrye-health + + + + ${project.artifactId} + + + maven-compiler-plugin + ${version.compiler.plugin} + + ${maven.compiler.release} + + + + ${quarkus.platform.group-id} + quarkus-maven-plugin + ${quarkus-plugin.version} + + + + build + + + + + + maven-failsafe-plugin + ${version.failsafe.plugin} + + + org.jboss.logmanager.LogManager + ${maven.home} + + + + + + integration-test + verify + + + + + + + + + container + + + container + + + + container + + + + io.quarkus + quarkus-container-image-jib + + + + + native + + + native + + + + native + + + + diff --git a/serverless-workflow-examples/serverless-workflow-dmn-quarkus/src/main/resources/Traffic Violation.dmn b/serverless-workflow-examples/serverless-workflow-dmn-quarkus/src/main/resources/Traffic Violation.dmn new file mode 100644 index 0000000000..109d4689e1 --- /dev/null +++ b/serverless-workflow-examples/serverless-workflow-dmn-quarkus/src/main/resources/Traffic Violation.dmn @@ -0,0 +1,230 @@ + + + + + + string + + + number + + + string + + + string + + + number + + + + + string + + + date + + + string + + "speed", "parking", "driving under the influence" + + + + number + + + number + + + + + number + + + number + + + + + + + + + + + + + + Violation.Type + + + + + Violation.Actual Speed - Violation.Speed Limit + + + + + + + "speed" + + + [10..30) + + + 500 + + + 3 + + + + + "speed" + + + >= 30 + + + 1000 + + + 7 + + + + + "parking" + + + - + + + 100 + + + 1 + + + + + "driving under the influence" + + + - + + + 1000 + + + 5 + + + + + + + + + Should the driver be suspended due to points on his license? + "Yes", "No" + + + + + + + + + + + + Driver.Points + Fine.Points + + + + + if Total Points >= 20 then "Yes" else "No" + + + + + + + + + + 50.0 + 254.0 + 329.0 + 119.0 + 100.0 + 186.0 + + + 50.0 + 100.0 + 398.0 + + + 398.0 + + + 398.0 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/serverless-workflow-examples/serverless-workflow-dmn-quarkus/src/main/resources/application.properties b/serverless-workflow-examples/serverless-workflow-dmn-quarkus/src/main/resources/application.properties new file mode 100644 index 0000000000..a67a411033 --- /dev/null +++ b/serverless-workflow-examples/serverless-workflow-dmn-quarkus/src/main/resources/application.properties @@ -0,0 +1,29 @@ +# +# 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. +# + +quarkus.native.native-image-xmx=8g + +quarkus.devservices.enabled=false + +# profile to pack this example into a container, to use it execute activate the maven container profile, -Dcontainer +%container.quarkus.container-image.build=true +%container.quarkus.container-image.push=false +%container.quarkus.container-image.group=${USER} +%container.quarkus.container-image.registry=dev.local +%container.quarkus.container-image.tag=1.0-SNAPSHOT \ No newline at end of file diff --git a/serverless-workflow-examples/serverless-workflow-dmn-quarkus/src/main/resources/traffic violation.sw.json b/serverless-workflow-examples/serverless-workflow-dmn-quarkus/src/main/resources/traffic violation.sw.json new file mode 100644 index 0000000000..b1f615e480 --- /dev/null +++ b/serverless-workflow-examples/serverless-workflow-dmn-quarkus/src/main/resources/traffic violation.sw.json @@ -0,0 +1,24 @@ +{ + "id" : "traffic-violation", + "name" : "traffic-violation", + "version" : "1_0", + "start" : "traffic-violation", + "functions" : [ { + "name" : "DMNTest", + "operation" : "dmn", + "type" : "custom", + "metadata" : { + "file" : "Traffic Violation.dmn", + "namespace" : "https://github.com/kiegroup/drools/kie-dmn/_A4BCA8B8-CF08-433F-93B2-A2598F19ECFF", + "model" : "Traffic Violation" + } + } ], + "states" : [ { + "actions" : [ { + "functionRef" : "DMNTest" + } ], + "name" : "traffic-violation", + "type" : "operation", + "end" : true + } ] +} \ No newline at end of file diff --git a/serverless-workflow-examples/serverless-workflow-dmn-quarkus/src/test/java/org/kie/kogito/examples/DMNGreetRestIT.java b/serverless-workflow-examples/serverless-workflow-dmn-quarkus/src/test/java/org/kie/kogito/examples/DMNGreetRestIT.java new file mode 100644 index 0000000000..0d13fc98cc --- /dev/null +++ b/serverless-workflow-examples/serverless-workflow-dmn-quarkus/src/test/java/org/kie/kogito/examples/DMNGreetRestIT.java @@ -0,0 +1,53 @@ +/* + * 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. + */ +package org.kie.kogito.examples; + +import org.junit.jupiter.api.Test; + +import io.quarkus.test.junit.QuarkusIntegrationTest; +import io.restassured.http.ContentType; + +import static io.restassured.RestAssured.given; +import static org.hamcrest.CoreMatchers.containsString; +import static org.hamcrest.Matchers.is; + +@QuarkusIntegrationTest +class DMNGreetRestIT { + @Test + public void testEvaluateTrafficViolation() { + given() + .body("{\n" + + " \"Driver\": {\n" + + " \"Points\": 2\n" + + " },\n" + + " \"Violation\": {\n" + + " \"Type\": \"speed\",\n" + + " \"Actual Speed\": 120,\n" + + " \"Speed Limit\": 100\n" + + " }\n" + + "}") + .contentType(ContentType.JSON) + .when() + .post("/Traffic Violation") + .then() + .statusCode(200) + .body("'Should the driver be suspended?'", is("No")); + } + +}