package com.poc.drools;
public class Applicant {
private String name;
private int age;
private boolean valid;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public boolean isValid() {
return valid;
}
public void setValid(boolean valid) {
this.valid = valid;
}
}
package com.poc.drools import com.poc.drools.Applicant; rule "Is applicant age valid" dialect 'mvel' when $applicant : Applicant( age < 40 ) then $applicant.setValid(false); end
package com.poc.drools;
import java.io.File;
import org.drools.core.common.DefaultFactHandle;
import org.kie.api.KieBase;
import org.kie.api.KieBaseConfiguration;
import org.kie.api.KieServices;
import org.kie.api.builder.KieBuilder;
import org.kie.api.builder.KieFileSystem;
import org.kie.api.builder.ReleaseId;
import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSession;
import org.kie.api.runtime.KieSessionConfiguration;
import org.kie.api.runtime.rule.FactHandle;
import org.kie.internal.io.ResourceFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class RuleProcessor {
Logger logger=LoggerFactory.getLogger(RuleProcessor.class);
public Boolean isApplicantEligible(Applicant applicant) {
// String array was used because the rules can be splited into two or more files
String [] paths= new String[1];
paths[0]="C:\\Users\\AYOBAMI\\Documents\\workplace\\DroolsPOC\\src\\rules\\ageRule.drl";
KieServices kieServices = KieServices.Factory.get();
// Adds the drools file to the KieFileSystem for necessary compilation to occur
KieFileSystem kieFileSystem = kieServices.newKieFileSystem();
for (String path : paths) {
File file= new File(path);
kieFileSystem.write(ResourceFactory.newFileResource(file));
}
// Create the builder for the resources of the File System
KieBuilder kieBuilder = kieServices.newKieBuilder(kieFileSystem);
kieBuilder.buildAll();
// Get the Release ID
ReleaseId relaseId = kieBuilder.getKieModule().getReleaseId();
// Create the Container, wrapping the KieModule with the given ReleaseId
KieContainer kiecontainer =kieServices.newKieContainer(relaseId);
// Configure and create a KieContainer that reads the drools files
KieBaseConfiguration kieBaseConfiguration = kieServices.newKieBaseConfiguration();
KieBase kieBase = kiecontainer.newKieBase(kieBaseConfiguration);
// Configure and create the KieSession
KieSessionConfiguration kieSessionConfiguration = kieServices.newKieSessionConfiguration();
KieSession kieSession= kieBase.newKieSession(kieSessionConfiguration, null);
//FactHandle holds reference to the inserted object
FactHandle applicantFactHandle= kieSession.insert(applicant);
//execute rule
kieSession.fireAllRules();
//get the processed data out of the rule engine
applicant= (Applicant) ((DefaultFactHandle)applicantFactHandle).getObject();
//release and free memory
kieSession.dispose();
return applicant.isValid();
}
}
package com.poc.drools.test;
import static org.junit.Assert.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import com.poc.drools.Applicant;
import com.poc.drools.RuleProcessor;
public class RuleProcessorTest {
private RuleProcessor ruleProcessor;
@Before
public void setUp() throws Exception {
ruleProcessor= new RuleProcessor();
}
@After
public void tearDown() throws Exception {
ruleProcessor=null;
}
@Test
public void testIsApplicantEligible() {
Applicant applicant = new Applicant();
applicant.setAge(16);
applicant.setName("Ayobami Adewole");
assertTrue(ruleProcessor.isApplicantEligible(applicant));
}
}
Share this page on
14
People Like(s) This Page
Permalink
comments powered by Disqus