Comparison of a JSON array based approach vs Jackson Inheritance Annotations
Introduction:
“Kogito is a next generation business automation toolkit that originates from well known Open Source projects Drools (for business rules) and jBPM (for business processes). Kogito aims at providing another approach to business automation where the main message is to expose your business knowledge (processes, rules and decisions) in a domain specific way.” (4)
Kogito rules services can reason over application domain model facts that are represented using “plain old Java objects” (POJO’s). The POJO’s can be used in DRL rules files and additionally the POJO’s may be used by the client applications that call the Kogito rules services. The communication layer between the rules service and the client application often uses RestAPI calls where the POJO’s are serialized and deserialize, to and from JSON.
The POJO’s may use standard Java inheritance. This paper explores two approaches for sharing Java subclasses between the rules service and the client application. The first approach uses JSON arrays to isolate the objects from each subclass into its own array. The second approach uses Jackson inheritance annotations so that the subclass of every object will be embedded during the RestAPI request and response.
Examples of the two approaches are available here:
“For any rule base application, a fact model is needed to drive the rules. The fact model typically overlaps with the applications domain model, but in general it will be decoupled from it (as it makes the rules easier to manage over time). There are no technical limitations on using your domain model as your fact model, however this introduces tighter coupling between your business domain (domain model) and your knowledge domain (fact model). Consequentially if your domain model were to change you would need to, at the very least, revisit your rule definitions.” (8)
“Red Hat Decision Manager supports several assets that you can use to define business decisions for your decision service. Each decision-authoring asset has different advantages, and you might prefer to use one or a combination of multiple assets depending on your goals and needs. DRL (Drools Rule Language) rules are business rules that you define directly in .drl text files.” (9)
Example Shared Fact Inheritance Model:
Class diagram for the common fact model
Goal
: Create DRL rules to identify the overloaded cars and trucks.
First Approach:
Each payload includes a JSON array of each subclass:
First Approach superclass:
package com.example.vehicle.datamodel;
@lombok.Getter
@lombok.Setter
public class Vehicle {
private String color;
private Integer vehicleId;
private Boolean overloaded = false;
First Approach example JSON payload
Five vehicles: One generic , two cars and two trucks. Notice that although every instance shares the same superclass, instances of every subclass are isolated into their own JSON array.
First Approach: Rule Unit Data for JSON array of each subclass
Set up the rule unit data to receive the arrays of subclasses:
public class VehicleUnitData implements RuleUnitData {
public DataStore<Vehicle> vehicleInstances =
DataSource.createStore();
public DataStore<Car> carInstances =
DataSource.createStore();
public DataStore<Truck> truckInstances =
DataSource.createStore();
First Approach: Rules to work with list of subclasses
rule "Car Rule using list of subclasses"
$c : /carInstances[ currentPassengers > maxPassengers ]
modify($c){setOverloaded(true)};
rule "Truck Rule using list of subclasses"
$t : /truckInstances[currentCargoWeight > maxCargoWeight]
modify($t){setOverloaded(true)};
query "GetOverloadedCars"
$c: /carInstances[overloaded]
query "GetOverloadedTrucks"
$t: /truckInstances[overloaded]
query "GetOverloadedVehicles"
$t: /vehicleInstances[overloaded]
Using the HTML code below, you can display this Business Process Incubator page content with the current filter and sorting inside your web site for FREE.
You can click on the Get the BPI Web Feed link on any of our page to create the best possible feed for your site. Here are a few tips to customize your BPI Web Feed.
Customizing the Content Filter
On any page, you can add filter criteria using the MORE FILTERS interface:
Customizing the Content Sorting
Clicking on the sorting options will also change the way your BPI Web Feed will be ordered on your site: