Drools rules decision tables implementation
Drools decision table Implementation with xls files
import java.util.HashMap;
import java.util.Map;
import org.drools.KnowledgeBase;
import org.drools.KnowledgeBaseFactory;
import org.drools.builder.DecisionTableConfiguration;
import org.drools.builder.DecisionTableInputType;
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 BusinessRurlesDroolsService {
Map<String, KnowledgeBase> map = new HashMap<String, KnowledgeBase>();
public void init() {
try {
System.out.println("BusinessRurlesDroolsService -------> Init execution started-----------------------");
map.put("TableOne", readKnowledgeBase("rules/TableOne.xls"));
// here we can add more tables also
System.out.println("BusinessRurlesDroolsService =============>> Init execution Completed===========================");
} catch (Exception e) {
System.out.println("BusinessRurlesDroolsService INIT METHOD :: Got Exception " + "\ne.getMessage() :: " + (e.getMessage()));
}
}
public Map getBusinessRurle(Map insurerattributes, String insurerxml) {
long startTime = 0;
Map results = new HashMap();
try {
startTime = System.currentTimeMillis();
StatefulKnowledgeSession ksession = map.get(insurerxml).newStatefulKnowledgeSession();
KnowledgeRuntimeLogger logger = KnowledgeRuntimeLoggerFactory.newFileLogger(ksession, "test");
ksession.insert(insurerattributes);
ksession.fireAllRules();
logger.close();
} catch (Exception e) {
insurerattributes.put("results", results);
return insurerattributes;
}
long endTime = System.currentTimeMillis();
long duration = (endTime - startTime);
return insurerattributes;
}
private static KnowledgeBase readKnowledgeBase(String insurerxml) throws Exception {
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
DecisionTableConfiguration config = KnowledgeBuilderFactory.newDecisionTableConfiguration();
config.setInputType(DecisionTableInputType.XLS);
kbuilder.add(ResourceFactory.newClassPathResource(insurerxml), ResourceType.DTABLE, config);
KnowledgeBuilderErrors errors = kbuilder.getErrors();
if (errors.size() > 0) {
for (KnowledgeBuilderError error : errors) {
System.out.println(
"BusinessRurlesDroolsService :: Got Errors " + "\ne.getMessage() :: " + (error.getMessage()));
}
throw new IllegalArgumentException("Could not parse knowledge.");
}
KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());
return kbase;
}
public static void main(String[] args) {
BusinessRurlesDroolsService businessRurlesDroolsService = new BusinessRurlesDroolsService();
businessRurlesDroolsService.init();
Map map = new HashMap<>();
map.put("userAge", 37);
map.put("sumInsured", 300000);
map.put("coverType", "NF");
map.put("noOfMembers", 1);
map.put("noOfChildren", 0);
map.put("term", 1);
map.put("addon", "NA");
Map map2 = businessRurlesDroolsService.getBusinessRurle(map, "TableOne");
}
}
import java.util.HashMap;
import java.util.Map;
import org.drools.KnowledgeBase;
import org.drools.KnowledgeBaseFactory;
import org.drools.builder.DecisionTableConfiguration;
import org.drools.builder.DecisionTableInputType;
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 BusinessRurlesDroolsService {
Map<String, KnowledgeBase> map = new HashMap<String, KnowledgeBase>();
public void init() {
try {
System.out.println("BusinessRurlesDroolsService -------> Init execution started-----------------------");
map.put("TableOne", readKnowledgeBase("rules/TableOne.xls"));
// here we can add more tables also
System.out.println("BusinessRurlesDroolsService =============>> Init execution Completed===========================");
} catch (Exception e) {
System.out.println("BusinessRurlesDroolsService INIT METHOD :: Got Exception " + "\ne.getMessage() :: " + (e.getMessage()));
}
}
public Map getBusinessRurle(Map insurerattributes, String insurerxml) {
long startTime = 0;
Map results = new HashMap();
try {
startTime = System.currentTimeMillis();
StatefulKnowledgeSession ksession = map.get(insurerxml).newStatefulKnowledgeSession();
KnowledgeRuntimeLogger logger = KnowledgeRuntimeLoggerFactory.newFileLogger(ksession, "test");
ksession.insert(insurerattributes);
ksession.fireAllRules();
logger.close();
} catch (Exception e) {
insurerattributes.put("results", results);
return insurerattributes;
}
long endTime = System.currentTimeMillis();
long duration = (endTime - startTime);
return insurerattributes;
}
private static KnowledgeBase readKnowledgeBase(String insurerxml) throws Exception {
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
DecisionTableConfiguration config = KnowledgeBuilderFactory.newDecisionTableConfiguration();
config.setInputType(DecisionTableInputType.XLS);
kbuilder.add(ResourceFactory.newClassPathResource(insurerxml), ResourceType.DTABLE, config);
KnowledgeBuilderErrors errors = kbuilder.getErrors();
if (errors.size() > 0) {
for (KnowledgeBuilderError error : errors) {
System.out.println(
"BusinessRurlesDroolsService :: Got Errors " + "\ne.getMessage() :: " + (error.getMessage()));
}
throw new IllegalArgumentException("Could not parse knowledge.");
}
KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());
return kbase;
}
public static void main(String[] args) {
BusinessRurlesDroolsService businessRurlesDroolsService = new BusinessRurlesDroolsService();
businessRurlesDroolsService.init();
Map map = new HashMap<>();
map.put("userAge", 37);
map.put("sumInsured", 300000);
map.put("coverType", "NF");
map.put("noOfMembers", 1);
map.put("noOfChildren", 0);
map.put("term", 1);
map.put("addon", "NA");
Map map2 = businessRurlesDroolsService.getBusinessRurle(map, "TableOne");
}
}
Comments
Post a Comment