Search code examples
javascaladrools

Drools - check if exists/contains string in a List<String>


I need to create a rule to check if a part of string exists inside a List[String]. Is there a way that i could do it?

I'm trying using contains but it's not working.

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

And here is my code

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

Thanks!

// tests 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;
    }   
}

// tests scala

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

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

Both returns true.


Solution

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