Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Drools access object inside HashMap while iterating

I have added an object from a user defined class into a HashMap. When inserted into Drools code, I can iterate through the HashMap and get the key and value pair. But I cannot access the attributes inside the user class which is the value section of the HashMap.

This is the POJO file used to hold data. This POJO will be inserted to the LinkedHashMap with a separate key. Currently, this key is just being generated using a simple for loop.

package com.sample.pojos;

import java.util.Date;

public class DateSet {

public DateSet() {
    // TODO Auto-generated constructor stub
    super();
}

public DateSet(String trainingType, Date completedDate, Date expirationDate) {
    super();
    this.trainingType = trainingType;
    this.completedDate = completedDate;
    this.expirationDate = expirationDate;
}

private String trainingType;
private Date completedDate;
private Date expirationDate;



public String getTrainingType() {
    return trainingType;
}
public void setTrainingType(String trainingType) {
    this.trainingType = trainingType;
}
public Date getCompletedDate() {
    return completedDate;
}
public void setCompletedDate(Date completedDate) {
    this.completedDate = completedDate;
}
public Date getExpirationDate() {
    return expirationDate;
}
public void setExpirationDate(Date expirationDate) {
    this.expirationDate = expirationDate;
}




}

This is the Java code used to add values to the LinkedHashMap. I have used LinkedHashMap because I need to access the items in the correct order. The key of the HashMap is an int while the value will be a DateSet object.

outHash.put(incrementedId, new DateSet(training.getTrainingType(), training.getCompletionDate(),
                    training.getExpirationDate()));

This is the Drools rule that I'm using to handle the HashMap. The commented part in the code is how I would like to use the object inside Drools. " entry.getValue()" prints the DateSet object but I cannot access attributes inside it.

rule "Validate test"
agenda-group "validate_init"
    when
        someClass: SomeClass($tMap : outHash)                       
        entry : Entry($valueV : value) from $tMap.entrySet()  

        //Boolean(booleanValue == true) from ($valueV.getTrainingType() == "NEW")       

    then
    //System.out.println($valueV.getTrainingType());
    System.out.println(entry.getKey() + "-" + entry.getValue());
 end
like image 459
Dilanga Thalpegama Avatar asked Oct 19 '22 22:10

Dilanga Thalpegama


1 Answers

This rule does what I think you want (version 6.3.0):

rule "Validate test"
when
    SomeClass($tMap : outHash)                       
    e: Map.Entry(k:key, v:value) from $tMap.entrySet()
    DateSet( tt: trainingType == "NEW" ) from v 
then
    System.out.println(e.getKey() + "-" + tt);
end

However, one wonders why you didn't insert the DateSet object as individual facts, which makes accessing and filtering quite easy. The artificial numbering (incrementedId) isn't part of the data, so what's its purpose?

Edit If you need to compare one DateSet object with the next (in sort order by date) you should add an ordinal attribute to DateSet and insert the DateSet objects. Then:

rule "check for overdue training"
when
    $ds1: DateSet( $ord1: ordinal, $exp: expirationDate )
    $ds2: DateSet( ordinal == $ord1+1, completedDate > $exp )
then
    // ds2 is too late
end

The trick her is that the attribute ordinal enables you to select successive pairs of DateSet objects (as they were created and numbered ordered by date): it compares the first with the second, the second with the third, and so on. - You can add trainingType == "NEW" or similar for further selection.

like image 58
laune Avatar answered Oct 22 '22 02:10

laune