0

I'm trying to test the solution proposed by @tarilabs (which makes all sense) in this thread, using a drl file from Business Central:

Drools Business Central - Decision tables with spreadsheets, using arrays at WHEN condition

Here is the rule code:

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

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

Here is the Client.java class:

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;
    }

}

This is my JSON:

{
  "commands": [
    {
      "insert": {
        "object": {
          "com.myspace.arraystreatmentconditions.Client": {
            "cart": ["item1", "item2", "item3"],
            "client_products": ["item1", "item2", "item3", "item4"],
            "client_status": "Active",
            "serviceability": "cable",
            "result": ""
          }
        },
        "out-identifier": "client"
      }
    },
    {
      "fire-all-rules": {}
    },
    {
      "get-object": {
        "out-identifier": "client"
      }
    }
  ]
}

But at the time to send the request from Postman, it keeps sending request for hours and I don´t get response.

This is the simple code of the whole project:

https://github.com/jesurod09/DroolsValidation

And here is the problem: enter image description here

Any idea?

2 Answers2

0

That's a very odd way of trying to figure out if the item $c.cart is present in $c.client_products.

It would probably work better like this:

$c: Client( client_status == "Active",
            $item: cart,
            client_products contains $item )

This is assuming that your model, which you haven't shared, has appropriate accessors for client_status, client_products, etc.


If, as mentioned in the comment, $c.cart is actually a collection and not a string, the rule would look something like:

$c: Client( client_status == "Active",
            $items: cart)
$item: String() from $items
Client( client_product contains $item ) from $c

The lack of a Client model in the question makes it a bit difficult to answer. The DRL in the question indicates that $c.cart is a String, but that might be in error as well.

Roddy of the Frozen Peas
  • 14,380
  • 9
  • 49
  • 99
  • "a very odd way" -> I believe because in a prev question `cart` is a Collection, and the goal was to identify which item from the `cart` collection was contained in the `client_products` portfolio collection to be used in the RHS (ref https://stackoverflow.com/a/75878178/893991) – tarilabs Mar 30 '23 at 15:39
  • 1
    So the question is unanswerable in its current form, sounds like. – Roddy of the Frozen Peas Mar 30 '23 at 16:23
  • Added an alternative formulation for a collection. – Roddy of the Frozen Peas Mar 30 '23 at 16:29
  • Sorry, Iḿ new with Drools and with Business Management, and also new in Stack Overflow. I have edited the question, hoping that now is more clear – Jesús RodRu Mar 30 '23 at 16:55
  • if `result` is a string, then it's only going to contain the last matching item in the cart -- eg if there are 3 items in card that are also in client_product, then only the last one will be in a sentence " from the cart in the " as applied by the 'then' clause. – Roddy of the Frozen Peas Mar 30 '23 at 17:59
  • Yes that's true, but anyway you can see that is not returning the last matching and is not returning anything, that's the question – Jesús RodRu Mar 30 '23 at 18:09
  • Working with your last answer!!! Thank you so much!!! – Jesús RodRu Mar 31 '23 at 07:56
0

Yes your solution works, this is my rule now:

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

But now Postman returns a result, but I don´t know why it returns result:"null".

My JSON is the next one: enter image description here

But the response is: enter image description here