如何在drools文件中迭代List <hashmap>并更新Hash Map对象并返回更新的List

时间:2015-12-31 13:17:02

标签: drools

在drl文件中遇到列表迭代问题。我需要检索每个HashMap对象并检查“问题”键。如果'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();
.............

需要更新HashMap并从drl文件返回更新的List。任何人都可以帮助我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();
       }

1 个答案:

答案 0 :(得分:1)

规则中的(function () { 'use strict'; function logThis() { console.log("Hello!"); } logThis(); })(); 会自动循环 JSHint failed for: D:\Work & Resources\Projects\portfolio\web-starter-kit-0.5.4\portfolio\app\scripts\animations.js [14:27:20] 'default' errored after 5.91 s [14:27:20] Error: [object Object] at formatError (C:\Users\Bart\AppData\Roaming\npm\node_modules\gulp\bin\gulp.js:169:10) at Gulp.<anonymous> (C:\Users\Bart\AppData\Roaming\npm\node_modules\gulp\bin\gulp.js:195:15) at Gulp.emit (events.js:129:20) at Gulp.Orchestrator._emitTaskDone (D:\Work & Resources\Projects\portfolio\web-starter-kit-0.5.4\portfolio\node_modules\gulp\node_modules\orchestrator\index.js:264:8) at D:\Work & Resources\Projects\portfolio\web-starter-kit-0.5.4\portfolio\node_modules\gulp\node_modules\orchestrator\index.js:275:23 at finish (D:\Work & Resources\Projects\portfolio\web-starter-kit-0.5.4\portfolio\node_modules\gulp\node_modules\orchestrator\lib\runTask.js:21:8) at cb (D:\Work & Resources\Projects\portfolio\web-starter-kit-0.5.4\portfolio\node_modules\gulp\node_modules\orchestrator\lib\runTask.js:29:3) at finish (D:\Work & Resources\Projects\portfolio\web-starter-kit-0.5.4\portfolio\node_modules\run-sequence\index.js:53:5) at Gulp.onError (D:\Work & Resources\Projects\portfolio\web-starter-kit-0.5.4\portfolio\node_modules\run-sequence\index.js:60:4) at Gulp.emit (events.js:129:20) at Gulp.Orchestrator._emitTaskDone (D:\Work & Resources\Projects\portfolio\web-starter-kit-0.5.4\portfolio\node_modules\gulp\node_modules\orchestrator\index.js:264:8) at D:\Work & Resources\Projects\portfolio\web-starter-kit-0.5.4\portfolio\node_modules\gulp\node_modules\orchestrator\index.js:275:23 at finish (D:\Work & Resources\Projects\portfolio\web-starter-kit-0.5.4\portfolio\node_modules\gulp\node_modules\orchestrator\lib\runTask.js:21:8) at DestroyableTransform.<anonymous> (D:\Work & Resources\Projects\portfolio\web-starter-kit-0.5.4\portfolio\node_modules\gulp\node_modules\orchestrator\lib\runTask.js:52:4) at DestroyableTransform.f (D:\Work & Resources\Projects\portfolio\web-starter-kit-0.5.4\portfolio\node_modules\gulp\node_modules\orchestrator\node_modules\end-of-stream\node_modules\once\once.js:17:25) at DestroyableTransform.emit (events.js:129:20) at DestroyableTransform.flush (D:\Work & Resources\Projects\portfolio\web-starter-kit-0.5.4\portfolio\node_modules\gulp-jshint\src\reporters\fail.js:41:14) at DestroyableTransform._flush (D:\Work & Resources\Projects\portfolio\web-starter-kit-0.5.4\portfolio\node_modules\gulp-jshint\src\stream.js:41:13) at DestroyableTransform.<anonymous> (D:\Work & Resources\Projects\portfolio\web-starter-kit-0.5.4\portfolio\node_modules\gulp-jshint\node_modules\through2\node_modules\readable-stream\lib\_stream_transform.js:135:12) at DestroyableTransform.g (events.js:199:16) at DestroyableTransform.emit (events.js:129:20) at finishMaybe (D:\Work & Resources\Projects\portfolio\web-starter-kit-0.5.4\portfolio\node_modules\gulp-jshint\node_modules\through2\node_modules\readable-stream\lib\_stream_writable.js:371:12) 返回的列表。这意味着您需要在from左侧使用的模式为$rAlerts.reservationMap,而不是from。 获得Map模式后,您可以添加关于'issue'键的约束。

尝试这样的事情:

Map

希望它有所帮助,