Skip to content

Commit

Permalink
[incubator-kie-issues#1209] Implemented scesim rules springboot example
Browse files Browse the repository at this point in the history
  • Loading branch information
Gabriele-Cardosi committed May 16, 2024
1 parent ef03557 commit 19022fb
Show file tree
Hide file tree
Showing 17 changed files with 783 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@
<artifactId>rules-legacy-scesim-quarkus-example</artifactId>
<name>Kogito Example :: Rules Legacy API HELLO - Quarkus</name>
<properties>
<quarkus-plugin.version>3.2.10.Final</quarkus-plugin.version>
<quarkus-plugin.version>3.8.4</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>3.2.10.Final</quarkus.platform.version>
<quarkus.platform.version>3.8.4</quarkus.platform.version>
<kogito.bom.group-id>org.kie.kogito</kogito.bom.group-id>
<kogito.bom.artifact-id>kogito-bom</kogito.bom.artifact-id>
<kogito.bom.version>999-SNAPSHOT</kogito.bom.version>
Expand All @@ -58,7 +58,7 @@
<dependency>
<groupId>org.drools</groupId>
<artifactId>drools-xml-support</artifactId>
<version>${kogito.bom.version}</version>
<version>${version.org.kie.kogito}</version>
<scope>test</scope>
</dependency>
</dependencies>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package testscenario;

import io.quarkus.test.junit.QuarkusTest;

/**
* Do not remove this file
*/
@QuarkusTest
@org.junit.runner.RunWith(org.drools.scenariosimulation.backend.runner.ScenarioJunitActivator.class)
public class ScenarioJunitActivatorTest {
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,8 @@
#

# Quarkus
quarkus.http.test-port=0
quarkus.http.test-port=0

quarkus.log.level=INFO
quarkus.log.category."org.kie.kogito".level=DEBUG
quarkus.log.category."org.drools".level=DEBUG
1 change: 1 addition & 0 deletions kogito-springboot-examples/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@
<module>process-usertasks-with-security-oidc-springboot</module>
<module>process-usertasks-with-security-springboot</module>
<module>rules-legacy-springboot-example</module>
<module>rules-legacy-scesim-springboot-example</module>
<module>ruleunit-event-driven-springboot</module>
<module>ruleunit-springboot-example</module>
</modules>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
# RuleUnit + Spring Boot example

## Description

A simple rule service to validate `Hello` fact.

An injectable KieRuntimeBuilder is generated, so you can create Drools v7 KieBase and KieSession out of it.

## Installing and Running

### Prerequisites

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

### Compile and Run

```sh
mvn clean compile spring-boot:run
```

### Package and Run

```sh
mvn clean package
java -jar target/ruleunit-springboot-example.jar
```

## 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/v3/api-docs) - 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.

## Example Usage

Once the service is up and running, you can use the following examples to interact with the service.

### POST /find-approved

Returns approved Hello from the given fact:

```sh
curl -X POST -H 'Accept: application/json' -H 'Content-Type: application/json' -d '{"approved":false, "greeting":"foo"}' http://localhost:8080/find-approved
```
or on windows

```sh
curl -X POST -H "Accept: application/json" -H "Content-Type: application/json" -d "{\"approved\":false, \"greeting\":\"foo\"}" http://localhost:8080/find-approved
```

As response the modified Hello is returned.

Example response:

```json
{"greeting":"foo","approved":true}
```

Returns denied Hello from the given fact:

```sh
curl -X POST -H 'Accept: application/json' -H 'Content-Type: application/json' -d '{"approved":false, "greeting":"bar"}' http://localhost:8080/find-approved
```
or on windows

```sh
curl -X POST -H "Accept: application/json" -H "Content-Type: application/json" -d "{\"approved\":false, \"greeting\":\"bar\"}" http://localhost:8080/find-approved
```

As response the modified Hello is returned.

Example response:

```json
{"greeting":"bar","approved":false}
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
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.
-->
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.kie.kogito.examples</groupId>
<artifactId>kogito-springboot-examples</artifactId>
<version>999-SNAPSHOT</version>
</parent>
<artifactId>rules-legacy-scesim-springboot-example</artifactId>
<name>Kogito Example :: Rules Legacy API HELLO - Spring Boot</name>

<properties>
<version.org.kie.kogito>999-SNAPSHOT</version.org.kie.kogito>
<kogito.bom.version>999-SNAPSHOT</kogito.bom.version>
</properties>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.kie.kogito</groupId>
<artifactId>kogito-spring-boot-bom</artifactId>
<version>${kogito.bom.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.drools</groupId>
<artifactId>drools-xml-support</artifactId>
<version>${version.org.kie.kogito}</version>
<scope>test</scope>
</dependency>
</dependencies>
</dependencyManagement>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.drools</groupId>
<artifactId>drools-rules-spring-boot-starter</artifactId>
</dependency>

<!-- test related -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<scope>test</scope>
</dependency>
<!-- SCESIM -->
<dependency>
<groupId>org.kie.kogito</groupId>
<artifactId>kogito-scenario-simulation</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.drools</groupId>
<artifactId>drools-xml-support</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<finalName>${project.artifactId}</finalName>
<plugins>
<plugin>
<groupId>org.kie.kogito</groupId>
<artifactId>kogito-maven-plugin</artifactId>
<version>${version.org.kie.kogito}</version>
<extensions>true</extensions>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package org.kie.kogito.legacy;

/**
* This class was automatically generated by the data modeler tool.
*/

public class Hello implements java.io.Serializable {

static final long serialVersionUID = 1L;

@org.kie.api.definition.type.Label("Greeting")
private java.lang.String greeting;

@org.kie.api.definition.type.Label(value = "Approved")
private java.lang.Boolean approved;

public Hello() {
}

public java.lang.String getGreeting() {
return this.greeting;
}

public void setGreeting(java.lang.String greeting) {
this.greeting = greeting;
}

public java.lang.Boolean getApproved() {
return this.approved;
}

public void setApproved(java.lang.Boolean approved) {
this.approved = approved;
}

public Hello(java.lang.String greeting, java.lang.Boolean approved) {
this.greeting = greeting;
this.approved = approved;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* 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.legacy;

import org.kie.api.runtime.KieRuntimeBuilder;
import org.kie.api.runtime.KieSession;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/find-approved")
public class HelloEndpoint {

private final KieRuntimeBuilder kieRuntimeBuilder;

public HelloEndpoint(KieRuntimeBuilder kieRuntimeBuilder) {
this.kieRuntimeBuilder = kieRuntimeBuilder;
}

@PostMapping(produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
public Hello executeQuery(@RequestBody(required = true) Hello hello) {
KieSession session = kieRuntimeBuilder.newKieSession();

session.insert(hello);
session.fireAllRules();

return hello;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* 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.legacy;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication(scanBasePackages = { "org.kie.kogito.**", "org.drools.**" })
public class KogitoSpringbootApplication {

public static void main(String[] args) {
SpringApplication.run(KogitoSpringbootApplication.class, args);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.kie.kogito.legacy;

/**
* This class was automatically generated by the data modeler tool.
*/

public class Message implements java.io.Serializable {

static final long serialVersionUID = 1L;

public Message() {
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<kmodule xmlns="http://www.drools.org/xsd/kmodule">

<!-- <kbase name="HelloRuleKB" packages="org.kie.kogito.legacy" default="true">-->
<!-- <ksession name="HelloRule" type="stateful" default="true"/>-->
<!-- </kbase>-->
</kmodule>
Loading

0 comments on commit 19022fb

Please sign in to comment.