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

匿名 (未验证) 提交于 2019-12-03 01:46:01

问题:

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.

回答1:

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


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!