Skip to content

Commit 12166e9

Browse files
BAEL-804 - Spring Drools refactor
1 parent 52d5fbe commit 12166e9

4 files changed

Lines changed: 18 additions & 91 deletions

File tree

‎spring-drools/pom.xml‎

Lines changed: 9 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -14,42 +14,10 @@
1414
</parent>
1515

1616
<properties>
17-
<spring-boot-version>1.1.1.RELEASE</spring-boot-version>
18-
<http-component-version>4.0-alpha6</http-component-version>
1917
<drools-version>7.0.0.Final</drools-version>
20-
<apache-poi-version>3.13</apache-poi-version>
18+
<spring-framework.version>4.3.3.RELEASE</spring-framework.version>
2119
</properties>
22-
<dependencyManagement>
23-
<dependencies>
24-
<dependency>
25-
<groupId>io.spring.platform</groupId>
26-
<artifactId>platform-bom</artifactId>
27-
<version>${spring-boot-version}</version>
28-
<type>pom</type>
29-
<scope>import</scope>
30-
</dependency>
31-
</dependencies>
32-
</dependencyManagement>
3320
<dependencies>
34-
<dependency>
35-
<groupId>org.springframework.boot</groupId>
36-
<artifactId>spring-boot-starter-web</artifactId>
37-
</dependency>
38-
39-
<dependency>
40-
<groupId>org.springframework.boot</groupId>
41-
<artifactId>spring-boot-starter-test</artifactId>
42-
<scope>test</scope>
43-
</dependency>
44-
<dependency>
45-
<groupId>org.springframework.boot</groupId>
46-
<artifactId>spring-boot-starter-actuator</artifactId>
47-
</dependency>
48-
<dependency>
49-
<groupId>org.apache.httpcomponents</groupId>
50-
<artifactId>httpcore</artifactId>
51-
<version>${http-component-version}</version>
52-
</dependency>
5321
<!-- ... -->
5422
<dependency>
5523
<groupId>org.kie</groupId>
@@ -61,7 +29,6 @@
6129
<artifactId>drools-decisiontables</artifactId>
6230
<version>${drools-version}</version>
6331
</dependency>
64-
6532
<dependency>
6633
<groupId>org.drools</groupId>
6734
<artifactId>drools-core</artifactId>
@@ -72,54 +39,18 @@
7239
<artifactId>drools-compiler</artifactId>
7340
<version>${drools-version}</version>
7441
</dependency>
75-
<dependency>
76-
<groupId>org.apache.poi</groupId>
77-
<artifactId>poi</artifactId>
78-
<version>${apache-poi-version}</version>
79-
</dependency>
8042

81-
<dependency>
82-
<groupId>org.apache.poi</groupId>
83-
<artifactId>poi-ooxml</artifactId>
84-
<version>${apache-poi-version}</version>
85-
</dependency>
43+
<!--spring integration-->
8644
<dependency>
8745
<groupId>org.kie</groupId>
8846
<artifactId>kie-spring</artifactId>
89-
<version>7.0.0.Final</version>
90-
<exclusions>
91-
<exclusion>
92-
<groupId>org.springframework</groupId>
93-
<artifactId>spring-tx</artifactId>
94-
</exclusion>
95-
<exclusion>
96-
<groupId>org.springframework</groupId>
97-
<artifactId>spring-beans</artifactId>
98-
</exclusion>
99-
<exclusion>
100-
<groupId>org.springframework</groupId>
101-
<artifactId>spring-core</artifactId>
102-
</exclusion>
103-
<exclusion>
104-
<groupId>org.springframework</groupId>
105-
<artifactId>spring-context</artifactId>
106-
</exclusion>
107-
</exclusions>
47+
<version>${drools-version}</version>
48+
</dependency>
49+
<dependency>
50+
<groupId>org.springframework</groupId>
51+
<artifactId>spring-test</artifactId>
52+
<version>${spring-framework.version}</version>
53+
<scope>test</scope>
10854
</dependency>
10955
</dependencies>
110-
<build>
111-
<plugins>
112-
<plugin>
113-
<groupId>org.springframework.boot</groupId>
114-
<artifactId>spring-boot-maven-plugin</artifactId>
115-
<executions>
116-
<execution>
117-
<goals>
118-
<goal>repackage</goal>
119-
</goals>
120-
</execution>
121-
</executions>
122-
</plugin>
123-
</plugins>
124-
</build>
12556
</project>

‎spring-drools/src/main/java/com/baeldung/spring/drools/service/TaxiFareCalculatorService.java‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.baeldung.spring.drools.service;
22

3+
import org.kie.api.runtime.KieContainer;
34
import org.kie.api.runtime.KieSession;
45
import org.springframework.beans.factory.annotation.Autowired;
56

@@ -9,9 +10,10 @@
910
public class TaxiFareCalculatorService {
1011

1112
@Autowired
12-
private KieSession kieSession;
13+
private KieContainer kieContainer;
1314

1415
public Long calculateFare(TaxiRide taxiRide, Fare rideFare) {
16+
KieSession kieSession = kieContainer.newKieSession();
1517
kieSession.setGlobal("rideFare", rideFare);
1618
kieSession.insert(taxiRide);
1719
kieSession.fireAllRules();

‎spring-drools/src/main/java/com/baeldung/spring/drools/service/TaxiFareConfiguration.java‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public class TaxiFareConfiguration {
1616
public static final String drlFile = "TAXI_FARE_RULE.drl";
1717

1818
@Bean
19-
public KieSession kieSession() {
19+
public KieContainer kieSession() {
2020
KieServices kieServices = KieServices.Factory.get();
2121

2222
KieFileSystem kieFileSystem = kieServices.newKieFileSystem();
@@ -26,9 +26,8 @@ public KieSession kieSession() {
2626
KieModule kieModule = kieBuilder.getKieModule();
2727

2828
KieContainer kContainer = kieServices.newKieContainer(kieModule.getReleaseId());
29-
KieSession ksession = kContainer.newKieSession();
3029

31-
return ksession;
30+
return kContainer;
3231
}
3332

3433
@Bean

‎spring-drools/src/test/java/com/baeldung/spring/drools/service/TaxiFareCalculatorServiceTest.java‎

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import org.junit.Before;
55
import org.junit.Test;
66
import org.junit.runner.RunWith;
7+
import org.springframework.beans.factory.annotation.Autowired;
78
import org.springframework.context.ApplicationContext;
89
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
910
import org.springframework.test.context.ContextConfiguration;
@@ -14,17 +15,11 @@
1415
import com.baeldung.spring.drools.model.Fare;
1516

1617
@RunWith(SpringJUnit4ClassRunner.class)
17-
@ContextConfiguration(loader = AnnotationConfigContextLoader.class)
18+
@ContextConfiguration(classes = TaxiFareConfiguration.class)
1819
public class TaxiFareCalculatorServiceTest {
1920

20-
ApplicationContext context = null;
21-
TaxiFareCalculatorService taxiFareCalculatorService = null;
22-
23-
@Before
24-
public void init() {
25-
context = new AnnotationConfigApplicationContext(TaxiFareConfiguration.class);
26-
taxiFareCalculatorService = (TaxiFareCalculatorService) context.getBean(TaxiFareCalculatorService.class);
27-
}
21+
@Autowired
22+
TaxiFareCalculatorService taxiFareCalculatorService;
2823

2924
@Test
3025
public void testCalculateFareScenario1() {

0 commit comments

Comments
 (0)