Search code examples
drools

how to get values from map in drools


I am inserting a some set of keys and values in map and trying to get those map values in drools. I need to know how to get values from map in Drools

In Java

Map<String,String>mapTest = new HashMap<String,String>();
mapTest.put("name","John Doe");
mapTest.put("id","123");

In Drools

rule "when id equals 1000"

when
    $obj1 :Map(this["id"] == "1000");
then
    System.out.println("ID is equal to 1000" + $obj1);
    //this $obj1 prints map values in console
    //but i need to print only value of name (Print "John Doe")
end

Solution

  • You can use Map method get to access a single map entry. Right hand side code (the "then part" is just Java code (with a few extensions).

    System.out.println("ID is equal to 1000 " + $obj1.get( "name") );