0
問題:規則(的InterestRate .drl)在獨立的Java代碼被解僱,並給我準確的結果口水集成彈簧引導工作不正常
package com.test.drool.config;
import org.kie.api.KieServices;
import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSession;
import com.test.drool.facts.InterestRate;
public class RunSampleInterestRate {
public static void main(String[] args) {
// TODO Auto-generated method stub
KieContainer container= KieServices.Factory.get().getKieClasspathContainer();
InterestRate interestRate=new InterestRate();
interestRate.setLender("RBL");
System.out.println("printing session object before inserting"+interestRate.toString());
KieSession kieSession=container.newKieSession("ksession-rules");
kieSession.insert(interestRate);
kieSession.fireAllRules();
System.out.println(interestRate.getRate());
}
}
給了我預期的12.5爲利率。
問題:我必須在休息服務中集成這個,並且我一直試圖在休息環境下測試相同的邏輯,並且它沒有給我預期的結果。在激發規則後,服務 總是返回默認值0.0。我的環境是春季開機和口水是6.5.0最後。
POJO:
package com.test.drool.facts;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
@JsonIgnoreProperties(ignoreUnknown=true)
public class InterestRate {
@Override
public String toString() {
return "InterestRate [lender=" + lender + ", principal=" + principal + ", store=" + store
+ ", dealer=" + dealer + ", rate=" + rate + "]";
}
private String lender;
private String principal;
private String store;
private String dealer;
private double rate;
public double getRate() {
return rate;
}
public void setRate(double rate) {
this.rate = rate;
}
public String getLender() {
return lender;
}
public void setLender(String lender) {
this.lender = lender;
}
public String getPrincipal() {
return principal;
}
public void setPrincipal(String principal) {
this.principal = principal;
}
public String getStore() {
return store;
}
public void setStore(String store) {
this.store = store;
}
public String getDealer() {
return dealer;
}
public void setDealer(String dealer) {
this.dealer = dealer;
}
}
豆配置
package com.test.drool.config;
import org.kie.api.KieServices;
import org.kie.api.runtime.KieContainer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class DroolDependencyConfig {
@Bean(name="kieContainer")
public KieContainer kieContainer() {
return KieServices.Factory.get().getKieClasspathContainer();
}
}
控制器:
package com.test.drool.controllers;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.test.drool.facts.InterestRate;
import com.test.drool.service.RuleExecuteService;
@RestController
@RequestMapping(value="/rule")
public class RuleExecuteController {
@Autowired
private RuleExecuteService executeService;
private static Logger logger=LoggerFactory.getLogger(RuleExecuteController.class);
@PostMapping(value = "/execute", consumes = MediaType.APPLICATION_JSON_VALUE,produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<Double> getInterestRate(@RequestBody InterestRate interestRate){
logger.info(String.format("logging Request Object %s",interestRate.toString()));
return new ResponseEntity<Double>(executeService.executeRule(interestRate),HttpStatus.OK);
}
}
個RuleServiceImpl:
package com.test.drool.service.impl;
import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSession;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.test.drool.facts.InterestRate;
import com.test.drool.service.RuleExecuteService;
@Service
public class RuleExecutorServiceImpl implements RuleExecuteService {
private KieContainer kieContainer;
private static org.slf4j.Logger logger=LoggerFactory.getLogger(RuleExecutorServiceImpl.class);
@Autowired
public RuleExecutorServiceImpl(KieContainer kieContainer) {
this.kieContainer=kieContainer;
}
@Override
public double executeRule(InterestRate interestRate) {
logger.info("firing up session and executing rules");
KieSession kieSession= kieContainer.newKieSession("ksession-rules");
logger.info("Printing object before inserting in session"+interestRate.toString());
kieSession.insert(interestRate);
kieSession.fireAllRules();
System.out.println("returning values from rule execution"+">>>"+interestRate.getRate());
return interestRate.getRate();
}
}
DRL文件:
包com.test.drool.facts
rule "Interest Rate"
when
$interestrate := InterestRate(lender.equals("RBL"))
then
$interestrate.setRate(12.30);
end
搖籃依賴性:
dependencies {
compile "org.kie:kie-spring:${droolsVersion}"
compile "org.kie:kie-api:${droolsVersion}"
compile "org.drools:drools-core:${droolsVersion}"
compile "org.drools:drools-compiler:${droolsVersion}"
//compile('org.springframework.cloud:spring-cloud-starter-eureka')
compile('org.springframework.boot:spring-boot-starter-actuator')
compile ('org.springframework.boot:spring-boot-starter-web')
compile ('org.springframework.boot:spring-boot-starter-data-rest')
compile ('org.springframework.boot:spring-boot-devtools')
testCompile('org.springframework.boot:spring-boot-starter-test')
compile group: 'org.apache.httpcomponents', name: 'httpclient', version: '4.5.2'
compile group: 'org.apache.commons', name: 'commons-lang3', version: '3.0'
}
您是否通過RBL貸款?你可以添加日誌記錄 – pandaadb
是的,我已經添加了記錄器,並確認RBL作爲一個值正從我的其餘控制器傳遞迴我的服務,在這裏被注入到rulesession中。 – MrWayne
我的意思是在你的問題中添加日誌記錄的輸出。請改變你的規則,並刪除'lender.equals(「RBL」)'部分,看看你的規則是否觸發。我期望這個問題會在那裏出現 – pandaadb