使用数据库在Drools Engine中创建动态规则

时间:2017-12-14 20:57:39

标签: dynamic drools rules

我正在尝试创建动态Drools Rule Engine Springboot应用程序。我已将规则参数存储在Oracle DB中并创建了规则模板。我无法从数据库正确加载数据并通过模板文件运行它。 作为一个Springboot应用程序,我将模板保存在src / main / resources

我尝试转换为动态的静态drl文件是: -

import com.droolspoc.model.TaxiRide;
import com.droolspoc.model.Fare;
global com.telstra.droolspoc.model.Fare rideFare;
dialect "java"

rule "Calculate Taxi Fare - Scenario 1"
when
    taxiRideInstance:TaxiRide(isNightSurcharge == false,distanceInMile < 10);
then
    rideFare.setNightSurcharge(0);
    rideFare.setRideFare(70);
end

rule "Calculate Taxi Fare - Scenario 2"
when
    taxiRideInstance:TaxiRide(isNightSurcharge == true, distanceInMile < 10);
then
    rideFare.setNightSurcharge(30);
    rideFare.setRideFare(70);
end

我的规则模板是 -

template header
isNightSurcharge
distanceInMile
nightSurchargeFare
rideFare

import com.droolspoc.model.TaxiRide;
import com.droolspoc.model.Fare;
global com.droolspoc.model.Fare rideFare;
dialect "mvel"

template FareTemplate
rule "FareTemplate_@{row.rowNumber}"
when
  taxiRideInstance:TaxiRide(isNightSurcharge == @{isNightSurcharge} && distanceInMile == @{distanceInMile});
then
  rideFare.setNightSurcharge(@{nightSurchargeFare});
  rideFare.setRideFare(@{rideFare});
end

end template

我在模板中保持== @ distanceInMile}的原因是因为在DB中我将它存储为&lt; 10将是1,依此类推。 PFB DB截图 - DB screenshot

这是我的Drools配置文件 -

public static final String drlFile = "FareTemplate.drl";

@Bean
public KieContainer kieContainer() {    
    KieServices kieServices = KieServices.Factory.get();
    KieFileSystem kieFileSystem = kieServices.newKieFileSystem();
    kieFileSystem.write(ResourceFactory.newClassPathResource(drlFile));
    KieBuilder kieBuilder = kieServices.newKieBuilder(kieFileSystem);
    kieBuilder.buildAll();
    KieModule kieModule = kieBuilder.getKieModule();
    return kieServices.newKieContainer(kieModule.getReleaseId());

}

如何将数据库数据映射到模板并加载到Kie Container?请指教。

0 个答案:

没有答案