gpt4 book ai didi

java - 在运行时动态更新规则。流口水 + Spring Boot

转载 作者:行者123 更新时间:2023-12-04 21:30:37 58 4
gpt4 key购买 nike

所以目前我正在练习使用 Drools (7.13.0.Final) 和 Spring-boot(1.5.12.RELEASE)。我在 Spring-boot 中使用以下类制作了一个示例应用程序

模型:TaxiFare.java

@Data
public class TaxiFare {
private double surge;
private double netFare;
private double baseFare;
private double distanceTraveled;
}

服务:TaxiFareService.java
@Service
public class TaxiFareService {

@Autowired
private KieContainer kieContainer;


public TaxiFare getTaxiFare(TaxiFare fare) {

KieSession kieSession = kieContainer.newKieSession("rulesSession");

kieSession.insert(fare);
kieSession.fireAllRules();
kieSession.dispose();
return fare;
}
}

休息 Controller :TaxiFareController.java
@RestController
public class TaxiFareController {

@Autowired
private TaxiFareService taxiFareService;

@RequestMapping(value = "/getFare", method = RequestMethod.GET)
public String getQuestions(@RequestParam(required = true) double baseFare, @RequestParam(required = true) double surge, @RequestParam(required = true) double distanceTraveled) {

TaxiFare fare = new TaxiFare();
fare.setBaseFare(baseFare);
fare.setSurge(surge);
fare.setDistanceTraveled(distanceTraveled);

taxiFareService.getTaxiFare(fare);

return String.format("NET FARE IS: %s", fare.getNetFare());
}


}

配置:
@Bean
public KieContainer kieContainer2() {
return KieServices.Factory.get().getKieClasspathContainer();
}

有明确的规则
rule "No Surge"
when
$fareObject: TaxiFare(surge == 0.0);
then
$fareObject.setNetFare($fareObject.getBaseFare() * $fareObject.getDistanceTraveled());
end
rule "Has Surge"
when
$fareObject: TaxiFare(surge > 0.0);
then
$fareObject.setNetFare($fareObject.getSurge() * $fareObject.getBaseFare() * $fareObject.getDistanceTraveled());
end

我已经使用我在网上找到的各种 Drools + Spring 集成教程构建了这个示例应用程序。目前,规则引擎可以正常工作,但我想要做的是,每次添加新规则或修改现有规则时,我都希望能够在应用程序运行时动态重新加载规则文件。该应用程序打包为 WAR 并部署在 Web 服务器中。

我已经阅读了多篇关于 KieScanner 的使用的文章但是关于它的书面教程都没有意义,甚至是文档本身。按照他们提供的示例代码,
KieScanner kieScanner = kieServices.newKieScanner(kieContainer);
总是导致构建失败,因为 newKieScanner 似乎无法根据提供的 kieContainer 创建 kieScanner。有没有其他方法可以为 Drools 设置动态规则或澄清如何正确实现 KieScanner?

谢谢你。

最佳答案

你可以试试这个,实际上工作正常

 @Test
public void messageRuleFromUTFString() {
String ruleContent =
"package rules\r\n"+
"import mypackage.Message\r\n" +
"rule \"myrule\"\r\n" +
" \twhen\r\n" +
" $id : Message([\"38196-2\"] contains id)\r\n" +
" then\r\n" +
"\t\tSystem.out.println(\"Works!\");\r\n" +
"end\r\n";
KieSession kSession = null;
try {
KnowledgeBuilder kb = KnowledgeBuilderFactory.newKnowledgeBuilder();
kb.add(ResourceFactory.newByteArrayResource(ruleContent.getBytes("utf-8")), ResourceType.DRL);

KnowledgeBuilderErrors errors = kb.getErrors();
for (KnowledgeBuilderError error : errors) {
System.out.println(error);
}
InternalKnowledgeBase kBase = KnowledgeBaseFactory.newKnowledgeBase();
kBase.addPackages(kb.getKnowledgePackages());
kSession = kBase.newKieSession();
kSession.insert(createMessage("38196-2"));
kSession.fireAllRules();

} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} finally {
if (kSession != null)
kSession.dispose();
}
}

感谢其中一位 github 用户。我的例子稍微修改为最新版本的drools:
https://github.com/ityouknow/drools-examples.git

关于java - 在运行时动态更新规则。流口水 + Spring Boot ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53277079/

58 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com