How to compare attribute of current row to attribute of previous row in Drools

Andrew

Let's say I've got a simple pojo

    public class foo {
      private String id;
      private Date asOfDate;
      private String product;

      ...getters and setters...
}

I want to feed an bunch of foos into a rule, sort them based on the date, and then compare the current product to the previous product. Out of that compare, I want to create a new fact with an additional attribute called ProductChange. If the current product = the previous product, productChange will be set to "no change". If the current product <> the previous product, set it to "change".

Here's some sample sorted data:

| id |  asOfDate  | product |
+----+------------+---------+
|  1 | 2017-01-01 | A       |
|  1 | 2017-02-01 | A       |
|  1 | 2017-03-01 | B       |
|  1 | 2017-04-01 | C       |
+----+------------+---------+

And here's what the new facts would look like:

+----+------------+---------+---------------+
| id |  asOfDate  | product | productChange |
+----+------------+---------+---------------+
|  1 | 2017-01-01 | A       | No Change     |
|  1 | 2017-02-01 | A       | No change     |
|  1 | 2017-03-01 | B       | change        |
|  1 | 2017-04-01 | C       | change        |
+----+------------+---------+---------------+

Here's what I've got so far in the rule:

rule "compare"
    when 
      $foo : foo ($id : id, $asOfDate : asOfDate, $product : product)
      not foo (asOfDate < $asOfDate)
     then
        System.out.println("id: " + $id + " date: " + $asOfDate + "product: " + $product);
     end

This gets the set sorted correctly, but I have no idea how to approach looking at a previous row.

Esteban Aliverti

The easiest way is to create 2 rules: one for when 2 consecutives Foos have the same product and one for when the don't.

rule "Same Product"
when 
  $foo1 : foo ($id : id, $asOfDate1 : asOfDate, $product : product)
  $foo2 : foo (asOfDate > $asOfDate1, $asOfDate2:asOfDate, product == $product)
  not foo (asOfDate > $asOfDate, asOfDate < $asOfDate2)  //This will make sure that $foo1 and $foo2 are consecutive
then
    $foo2.setProductChange("No change");
end

rule "Different Product"
when 
  $foo1 : foo ($id : id, $asOfDate1 : asOfDate, $product : product)
  $foo2 : foo (asOfDate > $asOfDate1, $asOfDate2:asOfDate, product != $product)
  not foo (asOfDate > $asOfDate, asOfDate < $asOfDate2)  //This will make sure that $foo1 and $foo2 are consecutive
then
    $foo2.setProductChange("change");
end

Be careful because these rules don't take into account different Foos with the same timestamp. You will need to tweek the < and > to make it work in this case.

You will also need a separate rule for the first Foo, or you can add an OR in the first rule.

Hope it helps,

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to Compare the Current Row to the Previous Row by Category Using DAX

How to compare current row with the previous row and increment the Mode value

Compare the previous row with the current one

Update pandas dataframe current row attribute based on its value in the previous row for each row

Compare current row value to previous row values

Compare Current Row with Previous row in SQL Server

How to compare current event with the previous event in Drools?

how to compare a current row with all previous rows in r

How to compare current row with previous and next n rows in pandas?

How to use row as an attribute

Compare dataframe previous and current row in Python

Compare Value of Current and Previous Row in Spark

How to compare row to previous row in CSV?

python/beautifulsoup: find previous row with particular attribute

VBScript and SQL Server 2005 compare current row to previous row

Compare current row value to all 6 previous row values

How to compare the current row with the next row in NgFor

current row + previous row in sql

How to compare td with td in previous row

Pandas - Take current row, compare value to X previous rows and return how many matches (within x% range)

How to compare current row with previous 2 rows based on certain conditions in Python

Compare the previous N rows to the current row in a pandas column

Compare Value of Current and Previous Row, and after for Column if required in Spark

SAS Do-Loop and IF Statement to compare Current and previous row values

Compare value of current row to average of all previous rows

The attribute rowspan causes the succeeding <tr> element to be displayed in the previous row in browser

How to compare the current row with all the others in PostgreSQL?

How to calculate the previous row and show in the current row of the table?

How to multiply value in previous row with a value from current row