面临 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();
.............
需要更新HashMap并从drl文件返回更新后的List。任何人都可以帮助我吗?
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
。
一旦有了 Map 模式,您就可以添加有关“issue”键的约束。
尝试这样的事情:
rule "Reservation Alert"
when
$rAlerts : ReservationAlerts()
$map : Map(this["issue"] != "") from $rAlerts.reservationMap
then
$map.put("alert", "XXXX");
end
希望对你有帮助
关于drools - 如何迭代 drools 文件中的 List<HashMap> 并更新 Hash Map 对象并返回更新后的 List,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34547067/