Drools DRL HashMap/Map collection iteration using Map.Entry - Java @ Desk

Saturday, November 23, 2013

Drools DRL HashMap/Map collection iteration using Map.Entry

Drools DRL HashMap/Map collection iteration using Map.Entry In DRL file, we can iterate through the HashMap or Map using the Map.Entry advanced for loop feature.
Consider a map as shown below
private Map<String, List<String>> cityPlaces;

which stores all the stations for particular city. For example,
Mumbai contains Thane, Ghatkopar, etc.
Pune contains Chinchwad, ShivajiNagar, etc.
and so on.

In java, it can be done as shown below:

 for(Map.Entry<String, List<String>> entry : map.entrySet()) {  
      map.getKey();  
      map.getValue();  
 }  

In DRL file, it can be done in the same manner as shown above. Find the session creation and DRL file below

 package com.sample;  
 import java.util.List;  
 import java.util.Map;  
 public class Pojo {  
      private Map<String, List<String>> cityPlaces;  
      public Map<String, List<String>> getCityPlaces() {  
           return cityPlaces;  
      }  
      public void setCityPlaces(Map<String, List<String>> cityPlaces) {  
           this.cityPlaces = cityPlaces;  
      }  
 }  


 package com.sample;  
 import java.util.ArrayList;  
 import java.util.HashMap;  
 import java.util.List;  
 import java.util.Map;  
 import org.drools.KnowledgeBase;  
 import org.drools.KnowledgeBaseFactory;  
 import org.drools.builder.KnowledgeBuilder;  
 import org.drools.builder.KnowledgeBuilderError;  
 import org.drools.builder.KnowledgeBuilderErrors;  
 import org.drools.builder.KnowledgeBuilderFactory;  
 import org.drools.builder.ResourceType;  
 import org.drools.io.ResourceFactory;  
 import org.drools.logger.KnowledgeRuntimeLogger;  
 import org.drools.logger.KnowledgeRuntimeLoggerFactory;  
 import org.drools.runtime.StatefulKnowledgeSession;  
 import com.sample.DroolsTest.Message;  
 public class CollectionInDrl {  
      public static final void main(String[] args) {  
     try {  
       // load up the knowledge base  
       KnowledgeBase kbase = readKnowledgeBase();  
       StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();  
       KnowledgeRuntimeLogger logger = KnowledgeRuntimeLoggerFactory.newFileLogger(ksession, "test");  
       // go !  
       Pojo pojo = new Pojo();  
       Map<String, List<String>> map = new HashMap<String, List<String>>();  
       List<String> cityMumbai = new ArrayList<String>();  
       cityMumbai.add("Thane");  
       cityMumbai.add("Ghatkopar");  
       map.put("Mumbai", cityMumbai);  
       List<String> cityPune = new ArrayList<String>();  
       cityPune.add("Chinchwad");  
       cityPune.add("Shivajinagar");  
       map.put("Pune", cityPune);  
       pojo.setCityPlaces(map);  
       ksession.insert(pojo);  
       ksession.fireAllRules();  
       logger.close();  
     } catch (Throwable t) {  
       t.printStackTrace();  
     }  
   }  
   private static KnowledgeBase readKnowledgeBase() throws Exception {  
     KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();  
     kbuilder.add(ResourceFactory.newClassPathResource("DroolCollection.drl"), ResourceType.DRL);  
     KnowledgeBuilderErrors errors = kbuilder.getErrors();  
     if (errors.size() > 0) {  
       for (KnowledgeBuilderError error: errors) {  
         System.err.println(error);  
       }  
       throw new IllegalArgumentException("Could not parse knowledge.");  
     }  
     KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();  
     kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());  
     return kbase;  
   }  
 }  





 package com.sample  
 import com.sample.Pojo;  
 import java.util.ArrayList;  
 import java.util.HashMap;  
 import java.util.List;  
 import java.util.Map;  
 import java.util.Map.Entry;  
 rule "Get City For Thane Station"  
   when  
     pojo : Pojo($cityPlaces : cityPlaces)  
     entry : Entry() from $cityPlaces.entrySet()  
     value : String() from entry.getValue()  
     // eval(value == "Thane")  Reduce the usage of eval. So use this
     Boolean(booleanValue == true) from value == "Thane"
   then  
     System.out.println(value + " station comes under " + entry.getKey() );  
 end  

As shown above, first we got the Entry() object from $cityPlaces.entrySet()
Secondly we fetched each string from collection of stations
Last I checked if value == "Thane" station. If yes, in then part I retrieved the key from the map entry set







No comments:

Post a Comment