0

问题:Rule( 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。我的环境是 Spring-boot 和 drool是 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);
  }
}

规则服务实现:

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'
}
4

1 回答 1

0

我不知道它为什么起作用,但我们在 gradle 文件中保留了最少的 spring drool。我和我的同事所做的只是在 gradle 中只有 kie-spring 和 drool-compiler,它就像一个魅力。我们还需要排除 spring 的一些模块,因为 spring boot 1.5 不适用于 kie 所依赖的 spring 3.2。对于未来的 spring boot drool 开发,这里是 gradle。只需将其粘贴并构建和编码,就像您将为任何其他 spring-boot 项目编码一样。

repositories {
    mavenCentral()
    maven {
        name 'jboss'
        url 'http://repository.jboss.org/nexus/content/groups/public-jboss'
    }
}
buildscript {
    ext {
        springBootVersion = '1.5.3.RELEASE'
    }
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'

version = '0.0.1'
sourceCompatibility = 1.8
targetCompatibility = 1.8

repositories {
    mavenCentral()
}

dependencyManagement {
    imports {
        mavenBom 'org.springframework.cloud:spring-cloud-dependencies:Camden.SR6'
    }
}
dependencies {
    compile ('org.kie:kie-spring:6.5.0.Final'){
        exclude group:'org.springframework', module: 'spring-core'
        exclude group:'org.springframework', module: 'spring-tx'
        exclude group:'org.springframework', module: 'spring-beans'
        exclude group:'org.springframework', module: 'spring-context'
    }
    compile "org.drools:drools-compiler:6.5.0.Final"
    compile ('org.springframework.boot:spring-boot-starter-web')
    compile group: 'org.apache.commons', name: 'commons-lang3', version: '3.0'

}
task createFolder{
  def configDir = new File(project.buildDir.path+"/libs", "config")
  def keystoreDir = new File(project.buildDir.path+"/libs", "keystore")
  def logDir = new File(project.buildDir.path+"/libs", "log")
  def libDir = new File(project.buildDir.path+"/libs", "lib")

  if(!logDir.exists()){
    logDir.mkdirs()
  }
  delete configDir
  delete libDir
  delete keystoreDir

  libDir.mkdirs()
  configDir.mkdirs()
  keystoreDir.mkdirs()
}

 //copy config
task copyConfig(type: Copy) {   
    into project.buildDir.path+"/libs/config"
    from "config"

}
 //copy keystore
task copyKeystore(type: Copy) { 
    into project.buildDir.path+"/libs/keystore"
    from "keystore"
}

//copy dependencies
task copyRuntimeLibs(type: Copy) {
    into project.buildDir.path+"/libs/lib"
    from configurations.compile

}

task bundleAll(type: Jar){
    dependsOn 'createFolder', 'copyRuntimeLibs', 'copyConfig', 'copyKeystore' 

    manifest { 
        def manifestClasspath = configurations.compile.collect { "lib/" + it.getName() }.join(' ') 
        attributes 'Implementation-Title': 'rule-service', 
                   'Implementation-Version': version,
                   'Main-Class': 'com.test.drool.starte.RuleStarter',
                   'Class-Path': manifestClasspath
    }  
    baseName=project.name
  from { (configurations.compile - configurations.compile).collect { it.isDirectory() ? it : zipTree(it) } }
  with jar
     exclude 'META-INF/*.RSA', 'META-INF/*.SF','META-INF/*.DSA' 
} 

task zip(type: Zip){
    dependsOn 'bundleAll'
     from 'build/libs'
}
于 2017-06-22T10:50:19.763 回答