jkalyanc
jkalyanc

Reputation: 147

How to iterate List<HashMap> in drools file and update the Hash Map objects and return the updated List

Facing Problem with Iteration of List in drl file. I need to Retrieve each HashMap object and check for 'Issue' key. If value of 'issue' is not empty then need to add a value to 'alert' key.

public class ReservationAlerts {

       public enum AlertType {
            RESERVATIONDETAILSRESPONSE
        }

        @SuppressWarnings("rawtypes")
        private List<HashMap> reservationMap;

        @SuppressWarnings("rawtypes")
        public List<HashMap> getReservationMap() {
            return reservationMap;
        }

        @SuppressWarnings("rawtypes")
        public void setReservationMap(List<HashMap> reservationMap) {
            this.reservationMap = reservationMap;
        }
        }

Main Java Program:

DroolsTest.java
public class DroolsTest {
   @SuppressWarnings("rawtypes")
public static final void main(String[] args) {
      try {

         // load up the knowledge base
         KnowledgeBase kbase = readKnowledgeBase();
         StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();

         ReservationAlerts rAlerts = new ReservationAlerts();
         List<HashMap> hashMapList = new ArrayList<>();
         HashMap<String, String> hMap = new HashMap<>();
         hMap.put("rId", "101");
         hMap.put("fName", "ABC");
         hMap.put("lName", "DEF");
         hMap.put("issue", "1qaz");
         hMap.put("alert", "");
         hashMapList.add(hMap);

         HashMap<String, String> hMapI = new HashMap<>();
         hMapI.put("rId", "102");
         hMapI.put("fName", "GHI");
         hMapI.put("lName", "JKL");
         hMapI.put("issue", "");
         hMapI.put("alert", "");
         hashMapList.add(hMapI);
         rAlerts.setReservationMap(hashMapList);

         System.out.println("**********BEFORE************");
         System.out.println(hMap.keySet());
         System.out.println("****************************");
         System.out.println(hMapI.keySet());
         System.out.println("****************************");

         ksession.insert(rAlerts);
         ksession.fireAllRules();
.............

Need to update the HashMap and return the updated List from drl file. Can any one Help me plz

Drool File being triggered from Java File
    Reservations.drl

    import com.dwh.poc.ReservationAlerts;
    import java.util.List;
    import java.util.HashMap;
    import java.util.Iterator;

    // declare any global variables here
    dialect "java"
    rule "Reservation Alert"
       // Retrieve List from ReservationAlerts
       when
          $rAlerts : ReservationAlerts()
          $alertsMapList : List() from $rAlerts.reservationMap

       then
       // Iterate List and retrieve HashMap object
       for(Iterator it = $alertsMapList.iterator();it.hasNext();) {
          $alertMap : it.next();
       }

Upvotes: 2

Views: 4275

Answers (1)

Esteban Aliverti
Esteban Aliverti

Reputation: 6322

The from in your rule will automatically loop over the list returned by $rAlerts.reservationMap. This means that the pattern you need to use in the left hand side of your from is Map and not List. Once you have the Map pattern you can add the constraint about the 'issue' key.

Try something like this:

rule "Reservation Alert"
when
    $rAlerts : ReservationAlerts()
    $map : Map(this["issue"] != "") from $rAlerts.reservationMap
then
    $map.put("alert", "XXXX");
end

Hope it helps,

Upvotes: 3

Related Questions