0

I am new with Drools and I am using Business Central 7.18.0 to implement some rules using the asset Decision Tables (Spreadsheet).

I have achieve to implement some basic rules with excel and try them with Postman using the REST API but now I need to implement a more complex validation using (if possible) the arrays which come in the input in the when condition.

For example, If I have the next object:

package com.myspace.arraystreatmentconditions;

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

public class Client implements java.io.Serializable {

    static final long serialVersionUID = 1L;

    private java.util.List<java.lang.String> cart;
    private java.util.List<java.lang.String> client_products;
    private java.lang.String client_status;
    private java.lang.String serviceability;
    private java.lang.String result;

    public Client() {
    }

    public java.util.List<java.lang.String> getCart() {
        return this.cart;
    }

    public void setCart(java.util.List<java.lang.String> cart) {
        this.cart = cart;
    }

    public java.util.List<java.lang.String> getClient_products() {
        return this.client_products;
    }

    public void setClient_products(
            java.util.List<java.lang.String> client_products) {
        this.client_products = client_products;
    }

    public java.lang.String getClient_status() {
        return this.client_status;
    }

    public void setClient_status(java.lang.String client_status) {
        this.client_status = client_status;
    }

    public java.lang.String getServiceability() {
        return this.serviceability;
    }

    public void setServiceability(java.lang.String serviceability) {
        this.serviceability = serviceability;
    }

    public java.lang.String getResult() {
        return this.result;
    }

    public void setResult(java.lang.String result) {
        this.result = result;
    }

    public Client(java.util.List<java.lang.String> cart,
            java.util.List<java.lang.String> client_products,
            java.lang.String client_status, java.lang.String serviceability,
            java.lang.String result) {
        this.cart = cart;
        this.client_products = client_products;
        this.client_status = client_status;
        this.serviceability = serviceability;
        this.result = result;
    }

}

Here we have some attributes which are ArrayList.

  • "cart" are the the new products that the client is contracting
  • "client_products" are the products that the client currently has contracted(portfolio)

I would like to build a rule for Drools in a XLSX file where, in the conditions (not in the action if possible) the rule will check the cart and the actual client products. If the client_status is "Active" and we find any product in the cart which is already in the client portfolio then we will fill the attribute "result" with a message.

My principal idea is do it using a decision table but I am open to hear other solutions. The problem is I can't find example.

Jason Aller
  • 3,541
  • 28
  • 38
  • 38

2 Answers2

0

About the requirement:

If the client_status is "Active" and we find any product in the cart which is already in the client portfolio then we will fill the attribute "result" with a message.

The main idea here could be: one pattern is client_status == "Active", then iterate over the cart entries and check if anyone of those is present in the client_products, if that is the case, in the RHS you can set result.

rule R1
when
  $c : Client(client_status == "Active")
  $item: String( $c.client_products.contains(this) ) from $c.cart
then
  modify ($c) { setResult($item + " from the cart in the " + $c.getClient_products()); }
end

you'd need to substitute String() with the actual domain class for product/item in case.

If the above rules matches your requirement, you could re-write it with a decision table.

tarilabs
  • 2,178
  • 2
  • 15
  • 23
  • @tarilalabs Thanks for your quick anwser. Yes if I´ve undestood your code above the logic matches but I´m not familiarized with the drl language and I´m not quite sure of how can I re-write it with a decision table. Could you please help me? Moreover It would be very usefull if you have any documentation to learn more about drools. I have used this official guide but in fact is not really clear for me: https://docs.jboss.org/drools/release/7.18.0.Final/drools-docs/html_single/index.html#_drools_business_central – Jesús RodRu Mar 29 '23 at 15:15
  • Doesn't make much sense to ask decision table for only 1 rule, in the sense I could draft a DT but it's very biased on what I would guess you want to change in other rows (corresponding to other rules). Do you want to add description of similar rules having slight variations? – tarilabs Mar 29 '23 at 15:38
  • For example, let´s suppose that the client has a telephone product, an internet product and a mobile product on his portfolio. Now the Client wants to have a new IPTV service at home so in the cart we have a IPTV product. The main conditions to order that IPTV service must be that the internet speed must be greater than 10 MB which is the minimum speed required to watch IPTV, the client_status must be active and the serviceability must be ADSL or Fiber. if those conditions are not met the result must be an error message. – Jesús RodRu Mar 30 '23 at 07:39
  • Another example could be, the client is triying to order a new service which is not compatible with another service on his portfolio. For example a mobile discount for the fix line. – Jesús RodRu Mar 30 '23 at 07:40
  • doesn't sound to me like "variations" on similar parameters for a similar structure of the rules. I would advise to review the use-case for decision table. For instance, you have a rule that depending if <18, >=18 <21, >=21 <65, >=65 for `age`, would take a different action. https://docs.drools.org/8.36.0.Final/drools-docs/docs-website/drools/language-reference/index.html#decision-tables-use-case-con – tarilabs Mar 30 '23 at 11:42
  • Let me give you more context of the final objetive. We are trying to implement more than 1000 business rules for a Telco company. We will have as input an Arraylist of type Cart, an ArrayList of Type Portfolio, and other attributes for the Client like his name, surname, status, address... so depending on for example how much products of a concret type the client will have, or if the client has a product on cart but not on his portfolio, or a lot of different kind of combinations, the idea is to apply those rules. Just for simplicity for this inicial test I´m using type String. – Jesús RodRu Mar 30 '23 at 12:10
0

I have achieved to make the logic that I was looking for with a drl file:

package com.myspace.arraystreatmentconditions;
import com.myspace.arraystreatmentconditions.Client;
import java.util.*;

rule R1
when
  $c: Client( client_status == "Active", $items: cart)
  $item: String() from $items
  Client( client_products contains $item ) from $c
  
then
  //modify ($c) { setResult($item + " from the cart in the " + $c.getClient_products());}
  $c.setResult("Hay elementos duplicados");
end
General Grievance
  • 4,555
  • 31
  • 31
  • 45