Drools access object inside HashMap during iteration

I added an object from a custom class in a HashMap. When you insert the Drools code, I can iterate through the HashMap and get a pair of keys and values. But I cannot access the attributes inside the user class, which is a section of HashMap values.

This is a POJO file used to store data. This POJO will be inserted into LinkedHashMap using a separate key. Currently, this key is only being created using a simple 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 LinkedHashMap. I used LinkedHashMap because I need to access the elements in the correct order. The HashMap key is int, and the value will be a DateSet.

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

This is the Drools rule that I use to handle the HashMap. The commented part of the code is how I would like to use an object inside Drools. "entry.getValue ()" prints a DateSet, but I cannot access the 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 
+5
source share
1 answer

This rule does what it seems to you (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, the question arises as to why you are not inserting a DateSet as separate facts, which simplifies access and filtering. Artificial numbering (incrementedId) is not part of the data, so what is its purpose?

Edit If you need to compare one DateSet with the next (sorted by date), you must add the ordinal attribute to the DateSet and insert 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 

Its trick is that the ordinal attribute allows you to select consecutive pairs of DateSet objects (since they were created and numbered 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.

+2
source

Source: https://habr.com/ru/post/1235668/


All Articles