Drools - 检查List <string> </string>中是否存在/包含字符串

时间:2015-02-04 16:52:38

标签: java scala drools

我需要创建一个规则来检查List [String]中是否存在字符串的一部分。我有办法做到吗?

我尝试使用包含,但它无效。

rule "New Assistance"
    when
        $room:Room($tags : tags)
        //$room:Room(tags contains "protocol")
        $value:String() from $tags
        //Boolean(booleanValue == true) from $value == "protocol"
        Boolean(booleanValue == true) from $value.contains("protocol")
    then
        System.out.println("Error");
end

这是我的代码

...
val room = new Room(null, List[User](), List[Message](message), 
            new Date, null, List[String]("protocol"))
kieSession.insert(room)
kieSession.fireAllRules()
....

谢谢!

//测试java

import java.util.ArrayList;
import java.util.List;

public class Test { 
    public static void main(String[] args) {    
        List<String> tags = new ArrayList<>();
        tags.add("protocol");
        Room room = new Room(tags);
        System.out.println(room.tags.contains("protocol") );        
    }   
}

class Room {        
    public List<String> tags;

    public Room(List<String> tags){
        this.tags = tags;
    }   
}

//测试scala

val room = new Room(null, List[User](), List[Message](message), 
            new Date, null, List[String]("protocol"))

var xx = room.tags.contains("protocol")

两者都返回 true

1 个答案:

答案 0 :(得分:1)

rule "New Assistance"
when
    $room:Room($tags : tags contains "protocol")
then
    System.out.println("Error");
end