检查 Drools 地图中的特定元素

Check for specific element in a map in Drools

我想访问映射键并与包含或不包含的值进行比较。

//Attributeclass包含一个map形式的key和多个value

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 中的值。它显示不匹配任何规则。所以请帮助找到价值。

该列表包含 "email1" 和 "email2",但您正在检查 "email"。

但是规则必须写成

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

如果 'from' 的结果是一个列表,它会自动解开。这很有用,但有时会令人惊讶。 (如果您需要整个列表怎么办?)