如何在 drools 文件中迭代 List<HashMap> 并更新 Hash Map 对象和 return 更新后的 List

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

在 drl 文件中迭代列表时遇到问题。我需要检索每个 HashMap 对象并检查 'Issue' 键。如果 'issue' 的值不为空则需要向 'alert' 键添加一个值。

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

主要Java程序:

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();
.............

需要从 drl 文件更新 HashMap 和 return 更新列表。任何人都可以帮助我吗

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

您的规则中的 from 将自动循环遍历 $rAlerts.reservationMap 返回的列表。这意味着您需要在 from 左侧使用的模式是 Map 而不是 List。 一旦你有了地图模式,你就可以添加关于 'issue' 键的约束。

尝试这样的事情:

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

希望对您有所帮助,