Using drools eval to check size of an ArrayList - Java @ Desk

Tuesday, March 25, 2014

Using drools eval to check size of an ArrayList



Using drools eval to check size of an ArrayList

In drools, eval is used to check the equality conditions and it returns either true or false

You can alternatively use the Boolean class field booleanValue to check the same as shown in the post earlier Drools DRL alternatives of eval is Boolean check


Java Pojo :

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);
 }
}


List Insertion in KnowledgeSession :

PojoDRLListIteration pojoDRLListIteration = new PojoDRLListIteration();
   pojoDRLListIteration.addString("Personal Loan");
   pojoDRLListIteration.addString("Insurance");
   pojoDRLListIteration.addString("Credit Cards");

   ksession.insert(pojoDRLListIteration);

   ksession.fireAllRules();


DRL File:

package com.sample
 
import com.sample.PojoDRLListIteration;
import java.util.ArrayList;
import java.util.List;

// This rule will check the size of an ArrayList Object
rule "Rule List Size Check"
salience 100
    when
        pojoDRLListIteration : PojoDRLListIteration($listOfString : listOfString)
        eval($listOfString.size() > 0)
    then
        System.out.println("List is not empty");
end







No comments:

Post a Comment