BAEL-804 - Spring Drools refactor

This commit is contained in:
slavisa-baeldung 2017-06-27 16:02:53 +01:00
parent 52d5fbe603
commit 12166e9016
4 changed files with 18 additions and 91 deletions

View File

@ -14,42 +14,10 @@
</parent> </parent>
<properties> <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.Final</drools-version> <drools-version>7.0.0.Final</drools-version>
<apache-poi-version>3.13</apache-poi-version> <spring-framework.version>4.3.3.RELEASE</spring-framework.version>
</properties> </properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>io.spring.platform</groupId>
<artifactId>platform-bom</artifactId>
<version>${spring-boot-version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<version>${http-component-version}</version>
</dependency>
<!-- ... --> <!-- ... -->
<dependency> <dependency>
<groupId>org.kie</groupId> <groupId>org.kie</groupId>
@ -61,7 +29,6 @@
<artifactId>drools-decisiontables</artifactId> <artifactId>drools-decisiontables</artifactId>
<version>${drools-version}</version> <version>${drools-version}</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.drools</groupId> <groupId>org.drools</groupId>
<artifactId>drools-core</artifactId> <artifactId>drools-core</artifactId>
@ -72,54 +39,18 @@
<artifactId>drools-compiler</artifactId> <artifactId>drools-compiler</artifactId>
<version>${drools-version}</version> <version>${drools-version}</version>
</dependency> </dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>${apache-poi-version}</version>
</dependency>
<dependency> <!--spring integration-->
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>${apache-poi-version}</version>
</dependency>
<dependency> <dependency>
<groupId>org.kie</groupId> <groupId>org.kie</groupId>
<artifactId>kie-spring</artifactId> <artifactId>kie-spring</artifactId>
<version>7.0.0.Final</version> <version>${drools-version}</version>
<exclusions> </dependency>
<exclusion> <dependency>
<groupId>org.springframework</groupId> <groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId> <artifactId>spring-test</artifactId>
</exclusion> <version>${spring-framework.version}</version>
<exclusion> <scope>test</scope>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</exclusion>
</exclusions>
</dependency> </dependency>
</dependencies> </dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project> </project>

View File

@ -1,5 +1,6 @@
package com.baeldung.spring.drools.service; package com.baeldung.spring.drools.service;
import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSession; import org.kie.api.runtime.KieSession;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
@ -9,9 +10,10 @@ import com.baeldung.spring.drools.model.TaxiRide;
public class TaxiFareCalculatorService { public class TaxiFareCalculatorService {
@Autowired @Autowired
private KieSession kieSession; private KieContainer kieContainer;
public Long calculateFare(TaxiRide taxiRide, Fare rideFare) { public Long calculateFare(TaxiRide taxiRide, Fare rideFare) {
KieSession kieSession = kieContainer.newKieSession();
kieSession.setGlobal("rideFare", rideFare); kieSession.setGlobal("rideFare", rideFare);
kieSession.insert(taxiRide); kieSession.insert(taxiRide);
kieSession.fireAllRules(); kieSession.fireAllRules();

View File

@ -16,7 +16,7 @@ public class TaxiFareConfiguration {
public static final String drlFile = "TAXI_FARE_RULE.drl"; public static final String drlFile = "TAXI_FARE_RULE.drl";
@Bean @Bean
public KieSession kieSession() { public KieContainer kieSession() {
KieServices kieServices = KieServices.Factory.get(); KieServices kieServices = KieServices.Factory.get();
KieFileSystem kieFileSystem = kieServices.newKieFileSystem(); KieFileSystem kieFileSystem = kieServices.newKieFileSystem();
@ -26,9 +26,8 @@ public class TaxiFareConfiguration {
KieModule kieModule = kieBuilder.getKieModule(); KieModule kieModule = kieBuilder.getKieModule();
KieContainer kContainer = kieServices.newKieContainer(kieModule.getReleaseId()); KieContainer kContainer = kieServices.newKieContainer(kieModule.getReleaseId());
KieSession ksession = kContainer.newKieSession();
return ksession; return kContainer;
} }
@Bean @Bean

View File

@ -4,6 +4,7 @@ import org.junit.Assert;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.ContextConfiguration;
@ -14,17 +15,11 @@ import com.baeldung.spring.drools.model.TaxiRide;
import com.baeldung.spring.drools.model.Fare; import com.baeldung.spring.drools.model.Fare;
@RunWith(SpringJUnit4ClassRunner.class) @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(loader = AnnotationConfigContextLoader.class) @ContextConfiguration(classes = TaxiFareConfiguration.class)
public class TaxiFareCalculatorServiceTest { public class TaxiFareCalculatorServiceTest {
ApplicationContext context = null; @Autowired
TaxiFareCalculatorService taxiFareCalculatorService = null; TaxiFareCalculatorService taxiFareCalculatorService;
@Before
public void init() {
context = new AnnotationConfigApplicationContext(TaxiFareConfiguration.class);
taxiFareCalculatorService = (TaxiFareCalculatorService) context.getBean(TaxiFareCalculatorService.class);
}
@Test @Test
public void testCalculateFareScenario1() { public void testCalculateFareScenario1() {