How to iterate a List, ArrayList of String in DRL drools - Java @ Desk

Sunday, November 24, 2013

How to iterate a List, ArrayList of String in DRL drools



DRL provides a from keyword that is used to iterate a list of String or objects.

Consider there is a List of String objects, then DRL will allow to iterate through the list using below syntax

// $listOfString is List
value : String() from $listOfString

So if the size of list is 3, it will iterate three times and value will hold each String value during iteration.

Consider there is a List of Person class objects, then DRL will allow to iterate through the list using below syntax

// $listOfPerson is List
person : Person() from $listOfPerson

There are different ways to iterate through the list of objects and that is shown in the below DRL file.

Client file to run the DRL:

 package com.sample;  
 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;  
 public class DRLListIteration {  
      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 !  
                PojoDRLListIteration pojoDRLListIteration = new PojoDRLListIteration();  
                pojoDRLListIteration.addString("India");  
                pojoDRLListIteration.addString("Australia");  
                pojoDRLListIteration.addString("Pakistan");  
                ksession.insert(pojoDRLListIteration);  
                ksession.fireAllRules();  
                logger.close();  
           } catch (Throwable t) {  
                t.printStackTrace();  
           }  
      }  
      private static KnowledgeBase readKnowledgeBase() throws Exception {  
           KnowledgeBuilder kbuilder = KnowledgeBuilderFactory  
                     .newKnowledgeBuilder();  
           kbuilder  
                     .add(ResourceFactory  
                               .newClassPathResource("DRLListIteration.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;  
      }  
 }  

Pojo Class:

 package com.sample;  
 import java.util.ArrayList;  
 import java.util.List;  
 public class PojoDRLListIteration {  
      private List<String> listOfString;  
      public List<String> getListOfString() {  
           return listOfString;  
      }  
      public void setListOfString(List<String> listOfString) {  
           this.listOfString = listOfString;  
      }  
      public void addString(String value) {  
           if (listOfString == null) {  
                listOfString = new ArrayList<String>();  
           }  
           listOfString.add(value);  
      }  
 }  

DRL file:

 package com.sample  
 import com.sample.PojoDRLListIteration;  
 import java.util.ArrayList;  
 import java.util.List;  
 // This rule will iterate through the list  
 // and print the String objects  
 rule "Rule List Iteration"  
 salience 100  
   when  
     pojoDRLListIteration : PojoDRLListIteration($listOfString : listOfString)  
     value : String() from $listOfString  
   then  
     System.out.println("Country from list is - " +value);  
 end  



 // This rule iterates through the list using the from  
 // keyword and checks for value equality with each String  
 // object from list  
 rule "Rule List Contains check with iteration"  
 salience 50  
   when  
     pojoDRLListIteration : PojoDRLListIteration($listOfString : listOfString)  
     value : String() from $listOfString  
     Boolean(booleanValue == true) from value == "India"  
   then  
     System.out.println("List contains country India");  
 end  

 // This rule simply checks if the list  
 // contains any particular country  
 rule "Rule List Contains check without iteration"  
 salience 40  
   when  
     pojoDRLListIteration : PojoDRLListIteration($listOfString : listOfString,   
          $listOfString contains "India")  
   then  
     System.out.println("List contains country India");  
 end  







2 comments:

  1. I'm getting following error
    no viable alternative at input 'value'

    This is my drl
    package org.model;
    import org.mule.MessageExchangePattern;
    global org.mule.module.bpm.MessageService mule;
    import org.model.FileInformation;
    import java.util.List;
    import java.util.ArrayList;

    function String[] pathList() {
    return (new String[] {"Annual Reports", "Calculatre", "TJ"} );
    }

    rule "FileInfo_1"
    salience 65522
    dialect "mvel"
    when
    a: String[]() from pathList()
    fileInformation : FileInformation($fullPathList : fullPathList)
    value : String() from $fullPathList
    Boolean(booleanValue == true ) from value == "Annual Report"
    //$obj : FileInformation(fileName == "Description")
    then
    System.out.println( "You are in the Annual Report");

    ReplyDelete
    Replies
    1. is $fullPathList a generic List object of type String?
      Like in my case "private List listOfString; "

      Delete