Getting started with Kogito Business Automation

Kogito is an advanced business automation toolkit that derives from the Open Source projects Drools and jBPM. Kogito aims at providing another approach to business automation where the main message is to expose your business knowledge the cloud and the larger Kubernetes ecosystem.

There are already several implementations for Kogito, today we will learn how to set up a Kogito project with Quarkus which uses Drools business rules.

Let’s start from a simple scenario. A Person requires credit access. The variables that will be checked are:

  • The difference between the amount requested and the available credit
  • The existence of an existing loan
package org.acme.kogito

import org.acme.kogito.model.Person;

rule "get credit"

when
    $person: Person((credit > amount) && (!existingLoan))
then
    modify($person) { 
    	setApproved(true)
    };
end

Now let’s write a project for this simple use case.

Setting up the Quarkus Project:

Bootstrap a project which includes the default libraries (resteasy) and the kogito extension:

mvn io.quarkus:quarkus-maven-plugin:0.23.2:create \
    -DprojectGroupId=org.acme \
    -DprojectArtifactId=using-kogito \
    -Dextensions="kogito"

Now let’s add the model class: Person.java

package org.acme.kogito.model;
 
public class Person {

    private int amount;
	private int credit;
	private boolean existingLoan;
    private boolean approved;

	public int getAmount() {
		return amount;
	}

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

	public boolean isExistingLoan() {
		return existingLoan;
	}

	public boolean isApproved() {
		return approved;
	}

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

	public void setCredit(int credit) {
		this.credit = credit;
	}

	public void setExistingLoan(boolean existingLoan) {
		this.existingLoan = existingLoan;
	}


}

Then, a simple REST Endpoint which will capture requests for Credit and fire rules:

package org.acme.kogito.model;

import org.kie.api.runtime.KieSession;
import org.kie.kogito.rules.KieRuntimeBuilder;

import javax.inject.Inject;
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;

@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@Path("/credit")
public class CreditResource {

    @Inject
    KieRuntimeBuilder runtimeBuilder;

    @POST
    @Produces(MediaType.TEXT_PLAIN)
    public Boolean getCredit(Person p) {
        KieSession ksession = runtimeBuilder.newKieSession();
        ksession.insert(p);
        ksession.fireAllRules();
        return p.isApproved();

    }
}

That’s all. Include a kmodule.xml file in resources/META-INF:

<kmodule xmlns="http://www.drools.org/xsd/kmodule" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>

And include as well the Rule file into resources/org.acme.kogito. Here is the tree view of the Project:

kogito tutorial getting started with kogito

We will finish by adding a Test class which will include two tests. The first one is going to pass, the second one will fail:

package org.acme.kogito;

import static io.restassured.RestAssured.given;
import static org.hamcrest.core.Is.is;
import org.junit.jupiter.api.Test;
import io.quarkus.test.junit.QuarkusTest;
import io.restassured.http.ContentType;

@QuarkusTest
public class PersonProcessTest {

     @Test
    public void testPass() {
        given()
               .body("{\"amount\":200, \"credit\": 500, \"existingLoan\": \"false\"}")
               .contentType(ContentType.JSON)
          .when()
               .post("/credit")
          .then()
             .statusCode(200)
             .body(is("true"));
    }


    @Test
    public void testShouldFail() {
        given()
                .body("{\"amount\":200, \"credit\": 500, \"existingLoan\": \"true\"}")
                .contentType(ContentType.JSON)
                .when()
                .post("/credit")
                .then()
                .statusCode(200)
                .body(is("true"));
    }

}

That’s all! Run the test with just:

$mvn clean install

Or try running your own tests:

$mvn quarkus:dev
$ curl -d '{"amount":200, "credit":500, "existingLoan":"false" }' -H "Content-Type: application/json" -X POST http://localhost:8080/credit

That’s all! Enjoy BPM with Kogito!


Recommended Articles

Kogito Rule Engine vs Drools: What's the Difference? - A Comprehensive Guide

Learn how Kogito compares to Drools and discover its unique features, such as modular rules and cloud-native support. Get started with this in-depth tutorial.

Get Started with JBoss Drools: Core Concepts & Tools

Discover how to bootstrap a Drools project, code simple rules, and run Drools in your Java environment. #Java #Drools #KIE #BusinessRules #AIExpertSystem

Installing jBPM Business Central on WildFly 19: A Step-by-Step Guide

Learn how to install jBPM Business Central on WildFly 19 and access the Business Console. Follow this tutorial to set up the Kie Server, Business Central Workbench, and other required components.

Create a Drools Project with kie-drools-archetype in WildFly 31

Learn how to create a Drools project using Maven's kie-drools-archetype and deploy it on WildFly 31. This tutorial covers the basics of creating a Drools project, including rule files, KieSession, and more.