BAEL-804 - Spring Drools refactor
This commit is contained in:
+3
-1
@@ -1,5 +1,6 @@
|
||||
package com.baeldung.spring.drools.service;
|
||||
|
||||
import org.kie.api.runtime.KieContainer;
|
||||
import org.kie.api.runtime.KieSession;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
||||
@@ -9,9 +10,10 @@ import com.baeldung.spring.drools.model.TaxiRide;
|
||||
public class TaxiFareCalculatorService {
|
||||
|
||||
@Autowired
|
||||
private KieSession kieSession;
|
||||
private KieContainer kieContainer;
|
||||
|
||||
public Long calculateFare(TaxiRide taxiRide, Fare rideFare) {
|
||||
KieSession kieSession = kieContainer.newKieSession();
|
||||
kieSession.setGlobal("rideFare", rideFare);
|
||||
kieSession.insert(taxiRide);
|
||||
kieSession.fireAllRules();
|
||||
|
||||
+2
-3
@@ -16,7 +16,7 @@ public class TaxiFareConfiguration {
|
||||
public static final String drlFile = "TAXI_FARE_RULE.drl";
|
||||
|
||||
@Bean
|
||||
public KieSession kieSession() {
|
||||
public KieContainer kieSession() {
|
||||
KieServices kieServices = KieServices.Factory.get();
|
||||
|
||||
KieFileSystem kieFileSystem = kieServices.newKieFileSystem();
|
||||
@@ -26,9 +26,8 @@ public class TaxiFareConfiguration {
|
||||
KieModule kieModule = kieBuilder.getKieModule();
|
||||
|
||||
KieContainer kContainer = kieServices.newKieContainer(kieModule.getReleaseId());
|
||||
KieSession ksession = kContainer.newKieSession();
|
||||
|
||||
return ksession;
|
||||
return kContainer;
|
||||
}
|
||||
|
||||
@Bean
|
||||
|
||||
+4
-9
@@ -4,6 +4,7 @@ import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
@@ -14,17 +15,11 @@ import com.baeldung.spring.drools.model.TaxiRide;
|
||||
import com.baeldung.spring.drools.model.Fare;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(loader = AnnotationConfigContextLoader.class)
|
||||
@ContextConfiguration(classes = TaxiFareConfiguration.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);
|
||||
}
|
||||
@Autowired
|
||||
TaxiFareCalculatorService taxiFareCalculatorService;
|
||||
|
||||
@Test
|
||||
public void testCalculateFareScenario1() {
|
||||
|
||||
Reference in New Issue
Block a user