2017-02-22 38 views
0

我想访问地图键并与包含或不包含的值进行比较。检查Drools中的地图中的特定元素

//属性类包含键和多个值

public class Attribute { 
    private Map<String, List<String>> mapAttribute; 

    public Map<String, List<String>> getMapAttribute() { 
     return mapAttribute; 
    } 

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

public class DroolsMain { 

     public static Attribute attribute = new Attribute(); 

     public static void main(String[] args) throws DroolsParserException, IOException { 

     Map<String, List<String>> map = new HashMap<String, List<String>>(); 

     List<String> listSubject = new ArrayList<String>(); 
     listSubject.add("email1"); 
     listSubject.add("email2"); 
     map.put("Subject", listSubject); 

     List<String> listFrom = new ArrayList<String>(); 
     listFrom.add("Sathish Kumar"); 
     map.put("From", listFrom); 

     attribute.setMapAttribute(map); 
    } 
//... 
workingMemory.insert(attribute); 
} 

形式Rules.drl

rule "Get Subject key: with particular value" 
    when 
     attribute : Attribute($mapAttribute : mapAttribute) 
     //I want to compare value of **"Subject"** 
     List(this.contains("email1")) from $mapAttribute.get("Subject") 

    then 
     System.out.println("Rule run successfully Getting key with particular value"); 

    end 

一个地图,我没有得到在Rule.drl值。它显示不符合任何规则。所以请帮助找到价值。

回答

1

该列表包含“email1”和“email2”,但您正在检查“电子邮件”。

但规则必须被写为

rule "Get Subject key: with particular value" 
when 
    attribute : Attribute($mapAttribute : mapAttribute) 
    $values : String(this == "email1") from $mapAttribute.get("Subject") 
then ... end 

如果“从”的结果是一个列表,它是自动解开。这很有用,但偶尔会令人惊讶。 (如果你需要整个清单呢?)

+0

我没有得到结果先生。它显示不符合规则。 – Ashish

+0

我的不好。见编辑的答案。 – laune

+0

谢谢,它工作... – Ashish

相关问题