Updated code to use latest version

Modifed code sample to easy scenario
Deleted exisiting Code/files which are not required
This commit is contained in:
mokhan 2017-06-25 13:40:38 +05:30
parent 9e3e240a59
commit f8e8aa0fce
19 changed files with 302 additions and 381 deletions

View File

@ -16,7 +16,7 @@
<properties>
<spring-boot-version>1.1.1.RELEASE</spring-boot-version>
<http-component-version>4.0-alpha6</http-component-version>
<drools-version>7.0.0.CR1</drools-version>
<drools-version>7.0.0.Final</drools-version>
<apache-poi-version>3.13</apache-poi-version>
</properties>
<dependencyManagement>
@ -86,7 +86,7 @@
<dependency>
<groupId>org.kie</groupId>
<artifactId>kie-spring</artifactId>
<version>6.4.0.Final</version>
<version>7.0.0.Final</version>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>

View File

@ -1,30 +0,0 @@
package com.baeldung.spring.drools;
import com.baeldung.spring.drools.model.Applicant;
import com.baeldung.spring.drools.model.Product;
import com.baeldung.spring.drools.model.SuggestedRole;
import com.baeldung.spring.drools.service.ApplicantService;
import com.baeldung.spring.drools.service.ProductService;
import org.kie.api.KieServices;
import org.kie.api.runtime.KieContainer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Import;
@SpringBootApplication
@Import(DroolConfiguration.class)
public class Application {
public static void main(String[] args) {
ApplicationContext ctx = SpringApplication.run(Application.class, args);
ApplicantService applicantService=(ApplicantService)ctx.getBean("applicantService");
applicantService.suggestARoleForApplicant(new Applicant("Baljeet",37,1200000.0,8),new SuggestedRole());
ProductService productService=(ProductService)ctx.getBean("productService");
Product returnedProduct=productService.applyLabelToProduct(new Product("Microwave","Book"));
}
}

View File

@ -1,83 +0,0 @@
package com.baeldung.spring.drools;
import com.baeldung.spring.drools.service.ApplicantService;
import com.baeldung.spring.drools.service.ProductService;
import org.kie.api.KieBase;
import org.kie.api.KieServices;
import org.kie.api.builder.*;
import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSession;
import org.kie.internal.io.ResourceFactory;
import org.kie.spring.KModuleBeanFactoryPostProcessor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import java.io.IOException;
@Configuration
public class DroolConfiguration {
private static final String RULES_PATH = "com/baeldung/spring/drools/rules/";
@Bean
public KieFileSystem kieFileSystem() throws IOException {
KieFileSystem kieFileSystem = getKieServices().newKieFileSystem();
for (Resource file : getRuleFiles()) {
kieFileSystem.write(ResourceFactory.newClassPathResource(RULES_PATH + file.getFilename(), "UTF-8"));
}
return kieFileSystem;
}
private Resource[] getRuleFiles() throws IOException {
ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver();
return resourcePatternResolver.getResources("classpath*:" + RULES_PATH + "**/*.*");
}
@Bean
public KieContainer kieContainer() throws IOException {
final KieRepository kieRepository = getKieServices().getRepository();
kieRepository.addKieModule(new KieModule() {
public ReleaseId getReleaseId() {
return kieRepository.getDefaultReleaseId();
}
});
KieBuilder kieBuilder = getKieServices().newKieBuilder(kieFileSystem());
kieBuilder.buildAll();
return getKieServices().newKieContainer(kieRepository.getDefaultReleaseId());
}
private KieServices getKieServices() {
return KieServices.Factory.get();
}
@Bean
public KieBase kieBase() throws IOException {
return kieContainer().getKieBase();
}
@Bean
public KieSession kieSession() throws IOException {
return kieContainer().newKieSession();
}
@Bean
public ApplicantService getApplicantService(){
return new ApplicantService();
}
@Bean
public ProductService getProductService(KieContainer kieContainer){
return new ProductService();
}
@Bean
public KModuleBeanFactoryPostProcessor kiePostProcessor() {
return new KModuleBeanFactoryPostProcessor();
}
}

View File

@ -0,0 +1,24 @@
package com.baeldung.spring.drools.app;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import com.baeldung.spring.drools.model.TaxiRide;
import com.baeldung.spring.drools.model.Fare;
import com.baeldung.spring.drools.service.TaxiFareCalculatorService;
import com.baeldung.spring.drools.service.TaxiFareConfiguration;
public class ApplicationRunner {
public static void main(String[] args) {
ApplicationContext context = new AnnotationConfigApplicationContext(TaxiFareConfiguration.class);
TaxiFareCalculatorService orderService = (TaxiFareCalculatorService) context.getBean(TaxiFareCalculatorService.class);
TaxiRide taxiRide = new TaxiRide();
taxiRide.setbNightSurcharge(true);
taxiRide.setDistanceInMile(190L);
Fare rideFare = new Fare();
orderService.calculateFare(taxiRide, rideFare);
}
}

View File

@ -1,48 +0,0 @@
package com.baeldung.spring.drools.model;
public class Applicant {
private String name;
private int age;
private double currentSalary;
private int experienceInYears;
public Applicant(String name, int age, Double currentSalary, int experienceInYears) {
this.name = name;
this.age = age;
this.currentSalary = currentSalary;
this.experienceInYears = experienceInYears;
}
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 Double getCurrentSalary() {
return currentSalary;
}
public void setCurrentSalary(Double currentSalary) {
this.currentSalary = currentSalary;
}
public int getExperienceInYears() {
return experienceInYears;
}
public void setExperienceInYears(int experienceInYears) {
this.experienceInYears = experienceInYears;
}
}

View File

@ -0,0 +1,39 @@
package com.baeldung.spring.drools.model;
public class Fare {
private Long nightSurcharge;
private Long rideFare;
private Long totalFare;
public Fare() {
nightSurcharge = 0L;
rideFare = 0L;
totalFare = 0L;
}
public Long getNightSurcharge() {
return nightSurcharge;
}
public void setNightSurcharge(Long nightSurcharge) {
this.nightSurcharge = nightSurcharge;
}
public Long getRideFare() {
return rideFare;
}
public void setRideFare(Long rideFare) {
this.rideFare = rideFare;
}
public Long getTotalFare() {
return nightSurcharge + rideFare;
}
public void setTotalFare(Long totalFare) {
this.totalFare = totalFare;
}
}

View File

@ -1,38 +0,0 @@
package com.baeldung.spring.drools.model;
public class Product {
private String name;
private String type;
private String label;
public Product(String name, String type) {
this.name = name;
this.type = type;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getLabel() {
return label;
}
public void setLabel(String label) {
this.label = label;
}
}

View File

@ -1,14 +0,0 @@
package com.baeldung.spring.drools.model;
public class SuggestedRole {
private String role;
public String getRole() {
return role;
}
public void setRole(String role) {
this.role = role;
}
}

View File

@ -0,0 +1,24 @@
package com.baeldung.spring.drools.model;
public class TaxiRide {
private Boolean bNightSurcharge;
private Long distanceInMile;
public Boolean getbNightSurcharge() {
return bNightSurcharge;
}
public void setbNightSurcharge(Boolean bNightSurcharge) {
this.bNightSurcharge = bNightSurcharge;
}
public Long getDistanceInMile() {
return distanceInMile;
}
public void setDistanceInMile(Long distanceInMile) {
this.distanceInMile = distanceInMile;
}
}

View File

@ -1,25 +0,0 @@
package com.baeldung.spring.drools.service;
import com.baeldung.spring.drools.model.Applicant;
import com.baeldung.spring.drools.model.SuggestedRole;
import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class ApplicantService {
@Autowired
private KieContainer kieContainer;
public SuggestedRole suggestARoleForApplicant(Applicant applicant,SuggestedRole suggestedRole){
KieSession kieSession = kieContainer.newKieSession();
kieSession.insert(applicant);
kieSession.setGlobal("suggestedRole",suggestedRole);
kieSession.fireAllRules();
System.out.println(suggestedRole.getRole());
return suggestedRole;
}
}

View File

@ -1,24 +0,0 @@
package com.baeldung.spring.drools.service;
import com.baeldung.spring.drools.model.Product;
import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class ProductService {
@Autowired
private KieContainer kieContainer;
public Product applyLabelToProduct(Product product){
KieSession kieSession = kieContainer.newKieSession();
kieSession.insert(product);
kieSession.fireAllRules();
System.out.println(product.getLabel());
return product;
}
}

View File

@ -0,0 +1,22 @@
package com.baeldung.spring.drools.service;
import org.kie.api.runtime.KieSession;
import org.springframework.beans.factory.annotation.Autowired;
import com.baeldung.spring.drools.model.Fare;
import com.baeldung.spring.drools.model.TaxiRide;
public class TaxiFareCalculatorService {
@Autowired
private KieSession kieSession;
public Long calculateFare(TaxiRide taxiRide, Fare rideFare) {
kieSession.setGlobal("rideFare", rideFare);
kieSession.insert(taxiRide);
kieSession.fireAllRules();
kieSession.dispose();
System.out.println("!! RIDE FARE !! " + rideFare.getTotalFare());
return rideFare.getTotalFare();
}
}

View File

@ -0,0 +1,39 @@
package com.baeldung.spring.drools.service;
import org.kie.api.KieServices;
import org.kie.api.builder.KieBuilder;
import org.kie.api.builder.KieFileSystem;
import org.kie.api.builder.KieModule;
import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSession;
import org.kie.internal.io.ResourceFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class TaxiFareConfiguration {
public static final String drlFile = "TAXI_FARE_RULE.drl";
@Bean
public KieSession kieSession() {
KieServices kieServices = KieServices.Factory.get();
KieFileSystem kieFileSystem = kieServices.newKieFileSystem();
kieFileSystem.write(ResourceFactory.newClassPathResource(drlFile));
KieBuilder kieBuilder = kieServices.newKieBuilder(kieFileSystem);
kieBuilder.buildAll();
KieModule kieModule = kieBuilder.getKieModule();
KieContainer kContainer = kieServices.newKieContainer(kieModule.getReleaseId());
KieSession ksession = kContainer.newKieSession();
return ksession;
}
@Bean
public TaxiFareCalculatorService taxiFareCalculatorService() {
TaxiFareCalculatorService taxiFareCalculatorService = new TaxiFareCalculatorService();
return taxiFareCalculatorService;
}
}

View File

@ -0,0 +1,57 @@
import com.baeldung.spring.drools.model.TaxiRide;
import com.baeldung.spring.drools.model.Fare;
import java.util.*;
global com.baeldung.spring.drools.model.Fare rideFare;
dialect "mvel"
rule "Calculate Taxi Fare - Scenario 1"
when
taxiRideInstance:TaxiRide(bNightSurcharge == false && distanceInMile < 10);
then
rideFare.setNightSurcharge(0);
rideFare.setRideFare(70);
end
rule "Calculate Taxi Fare - Scenario 2"
when
taxiRideInstance:TaxiRide(bNightSurcharge == true && distanceInMile < 10);
then
rideFare.setNightSurcharge(30);
rideFare.setRideFare(70);
end
rule "Calculate Taxi Fare - Scenario 3"
when
taxiRideInstance:TaxiRide(bNightSurcharge == false && distanceInMile >= 10 && distanceInMile < 100);
then
rideFare.setNightSurcharge(0);
rideFare.setRideFare(70+(2*taxiRideInstance.getDistanceInMile()));
end
rule "Calculate Taxi Fare - Scenario 4"
when
taxiRideInstance:TaxiRide(bNightSurcharge == true && distanceInMile >= 10 && distanceInMile < 100);
then
rideFare.setNightSurcharge(30+taxiRideInstance.getDistanceInMile());
rideFare.setRideFare(70+(2*taxiRideInstance.getDistanceInMile()));
end
rule "Calculate Taxi Fare - Scenario 5"
when
taxiRideInstance:TaxiRide(bNightSurcharge == false && distanceInMile >= 100);
then
rideFare.setNightSurcharge(0);
rideFare.setRideFare(70+(1.5*taxiRideInstance.getDistanceInMile()));
end
rule "Calculate Taxi Fare - Scenario 6"
when
taxiRideInstance:TaxiRide(bNightSurcharge == true && distanceInMile >= 100);
then
rideFare.setNightSurcharge(30+taxiRideInstance.getDistanceInMile());
rideFare.setRideFare(70+(1.5*taxiRideInstance.getDistanceInMile()));
end

View File

@ -1,31 +0,0 @@
package com.baeldung.spring.drools.rules;
import com.baeldung.spring.drools.model.Applicant;
global com.baeldung.spring.drools.model.SuggestedRole suggestedRole;
dialect "mvel"
rule "Suggest Manager Role"
when
Applicant(experienceInYears > 10)
Applicant(currentSalary > 1000000 && currentSalary <= 2500000)
then
suggestedRole.setRole("Manager");
end
rule "Suggest Senior developer Role"
when
Applicant(experienceInYears > 5 && experienceInYears <= 10)
Applicant(currentSalary > 500000 && currentSalary <= 1500000)
then
suggestedRole.setRole("Senior developer");
end
rule "Suggest Developer Role"
when
Applicant(experienceInYears > 0 && experienceInYears <= 5)
Applicant(currentSalary > 200000 && currentSalary <= 1000000)
then
suggestedRole.setRole("Developer");
end

View File

@ -1,52 +0,0 @@
package com.baeldung.spring.drools.service;
import com.baeldung.spring.drools.Application;
import com.baeldung.spring.drools.DroolConfiguration;
import com.baeldung.spring.drools.model.Applicant;
import com.baeldung.spring.drools.model.SuggestedRole;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import static junit.framework.Assert.assertNull;
import static junit.framework.TestCase.assertEquals;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {DroolConfiguration.class})
public class ApplicantServiceIntegrationTest {
@Autowired
ApplicantService applicantService;
@Test
public void whenCriteriaMatching_ThenSuggestManagerRole(){
Applicant applicant=new Applicant("Davis",37,1600000.0,11);
SuggestedRole suggestedRole=new SuggestedRole();
applicantService.suggestARoleForApplicant(applicant,suggestedRole);
assertEquals("Manager",suggestedRole.getRole());
}
@Test
public void whenCriteriaMatching_ThenSuggestSeniorDeveloperRole(){
Applicant applicant=new Applicant("John",37,1200000.0,8);
SuggestedRole suggestedRole=new SuggestedRole();
applicantService.suggestARoleForApplicant(applicant,suggestedRole);
assertEquals("Senior developer",suggestedRole.getRole());
}
@Test
public void whenCriteriaMatching_ThenSuggestDeveloperRole(){
Applicant applicant=new Applicant("Davis",37,800000.0,3);
SuggestedRole suggestedRole=new SuggestedRole();
applicantService.suggestARoleForApplicant(applicant,suggestedRole);
assertEquals("Developer",suggestedRole.getRole());
}
@Test
public void whenCriteriaNotMatching_ThenNoRole(){
Applicant applicant=new Applicant("John",37,1200000.0,5);
SuggestedRole suggestedRole=new SuggestedRole();
applicantService.suggestARoleForApplicant(applicant,suggestedRole);
assertNull(suggestedRole.getRole());
}
}

View File

@ -1,34 +0,0 @@
package com.baeldung.spring.drools.service;
import com.baeldung.spring.drools.Application;
import com.baeldung.spring.drools.DroolConfiguration;
import com.baeldung.spring.drools.model.Product;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import static junit.framework.TestCase.assertEquals;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {DroolConfiguration.class})
public class ProductServiceIntegrationTest {
@Autowired
ProductService productService;
@Test
public void whenProductTypeElectronic_ThenLabelBarcode(){
Product product=new Product("Microwave","Electronic");
product=productService.applyLabelToProduct(product);
assertEquals("BarCode",product.getLabel());
}
@Test
public void whenProductTypeBook_ThenLabelIsbn(){
Product product=new Product("AutoBiography","Book");
product=productService.applyLabelToProduct(product);
assertEquals("ISBN",product.getLabel());
}
}

View File

@ -0,0 +1,95 @@
package com.baeldung.spring.drools.service;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.support.AnnotationConfigContextLoader;
import com.baeldung.spring.drools.model.TaxiRide;
import com.baeldung.spring.drools.model.Fare;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(loader = AnnotationConfigContextLoader.class)
public class TaxiFareCalculatorServiceTest {
ApplicationContext context = null;
TaxiFareCalculatorService taxiFareCalculatorService = null;
@Before
public void init() {
context = new AnnotationConfigApplicationContext(TaxiFareConfiguration.class);
taxiFareCalculatorService = (TaxiFareCalculatorService) context.getBean(TaxiFareCalculatorService.class);
}
@Test
public void testCalculateFareScenario1() {
TaxiRide taxiRide = new TaxiRide();
taxiRide.setbNightSurcharge(false);
taxiRide.setDistanceInMile(9L);
Fare rideFare = new Fare();
Long totalCharge = taxiFareCalculatorService.calculateFare(taxiRide, rideFare);
Assert.assertNotNull(totalCharge);
Assert.assertEquals(Long.valueOf(70), totalCharge);
}
@Test
public void testCalculateFareScenario2() {
TaxiRide taxiRide = new TaxiRide();
taxiRide.setbNightSurcharge(true);
taxiRide.setDistanceInMile(5L);
Fare rideFare = new Fare();
Long totalCharge = taxiFareCalculatorService.calculateFare(taxiRide, rideFare);
Assert.assertNotNull(totalCharge);
Assert.assertEquals(Long.valueOf(100), totalCharge);
}
@Test
public void testCalculateFareScenario3() {
TaxiRide taxiRide = new TaxiRide();
taxiRide.setbNightSurcharge(false);
taxiRide.setDistanceInMile(50L);
Fare rideFare = new Fare();
Long totalCharge = taxiFareCalculatorService.calculateFare(taxiRide, rideFare);
Assert.assertNotNull(totalCharge);
Assert.assertEquals(Long.valueOf(170), totalCharge);
}
@Test
public void testCalculateFareScenario4() {
TaxiRide taxiRide = new TaxiRide();
taxiRide.setbNightSurcharge(true);
taxiRide.setDistanceInMile(50L);
Fare rideFare = new Fare();
Long totalCharge = taxiFareCalculatorService.calculateFare(taxiRide, rideFare);
Assert.assertNotNull(totalCharge);
Assert.assertEquals(Long.valueOf(250), totalCharge);
}
@Test
public void testCalculateFareScenario5() {
TaxiRide taxiRide = new TaxiRide();
taxiRide.setbNightSurcharge(false);
taxiRide.setDistanceInMile(100L);
Fare rideFare = new Fare();
Long totalCharge = taxiFareCalculatorService.calculateFare(taxiRide, rideFare);
Assert.assertNotNull(totalCharge);
Assert.assertEquals(Long.valueOf(220), totalCharge);
}
@Test
public void testCalculateFareScenario6() {
TaxiRide taxiRide = new TaxiRide();
taxiRide.setbNightSurcharge(true);
taxiRide.setDistanceInMile(100L);
Fare rideFare = new Fare();
Long totalCharge = taxiFareCalculatorService.calculateFare(taxiRide, rideFare);
Assert.assertNotNull(totalCharge);
Assert.assertEquals(Long.valueOf(350), totalCharge);
}
}