2017-05-23 45 views
0

我想重複如下圖的列表:列表DRL

[ 
    { 
    "endOffset": 2913, 
    "coreference": "Tehran", 
    "entity": "a country", 
    "beginOffset": 2904 
    }, 
    { 
    "endOffset": 3055, 
    "coreference": "Bashar al-Assad", 
    "beginOffset": 3052 
    }, 
    { 
    "endOffset": 3130, 
    "coreference": "Bashar", 
    "beginOffset": 3128 
    } 
] 

的代碼塊,我執行的是如下:

PojoCorefList pojoCorefList=new PojoCorefList(); 
List<Map<String,String>> corefList=new ArrayList<Map<String,String>>(); 
Map<String,String> testMap=new LinkedHashMap<>(); 
testMap.put("beginOffset", "3052"); 
testMap.put("coreference", "Bashar al-Assad"); 
testMap.put("endOffset", "3055"); 
corefList.add(testMap); 
Map<String,String> testMap2=new LinkedHashMap<>(); 
testMap2.put("beginOffset", "3130"); 
testMap2.put("coreference", "Bashar"); 
testMap2.put("endOffset", "3128"); 
corefList.add(testMap2); 
Map<String,String> testMap3=new LinkedHashMap<>(); 
testMap3.put("beginOffset", "2913"); 
testMap3.put("coreference", "Tehran"); 
testMap3.put("endOffset", "2904"); 

corefList.add(testMap3); 

pojoCorefList.setCityPlaces(corefList); 
ksession.insert(pojoCorefList); 

這裏是PojoCorefList類,如下:

public static class PojoCorefList { 
    private List<Map<String,String>> cityPlaces; 
    public List<Map<String,String>> getCityPlaces() { 
     return cityPlaces; 
    } 

    public void setCityPlaces(List<Map<String,String>> cityPlaces) { 
     this.cityPlaces = cityPlaces; 
    } 
} 

的DRL語法,我已經試過是如下:

rule "List of Maps Testing" 
    salience 1 

    when 
     $c : PojoCorefList($coref : cityPlaces) 
    $item : List() from $coref 
    entry : Entry() from $item.entrySet() 
    //value: String() from entry.getValue() 
    //Boolean(booleanValue :true) from value=="Tehran" 

    then 
     System.out.println("List Maps Testing: list is - "+$item); 
end 

我相信,我循環$ COREF名單,現在我應該有一個地圖,但如果我嘗試從$項目獲得的entrySet(),我得到如下例外:

Unable to Analyse Expression $item.entrySet(): 
[Error: unable to resolve method using strict-mode: java.util.List.entrySet()] 
[Near : {... $item.entrySet() ....}] 
       ^: [Rule name='List of Maps Testing'] 

回答

0

的Drools不懂泛型。但是如果你提供了正確的類,它應該可以工作。請注意0​​重複收集。

$c : PojoCorefList($coref : cityPlaces) 
    $m : HashMap() from $coref // cityPlaces are lists of Maps 
    entry : Entry() from $m.entrySet() 

或使用

value : String() from $m.values() 

(這可能取決於Drools的版本。)

+0

謝謝laune。對你的代碼$ m.entrySet()的一個小修改將返回爲Entry()。 條目:Entry()從$ m.entrySet()而不是String() – user3152549

+0

嗯,是的 - 我正在考慮獲取地圖的值。修正並感謝。 – laune