Drools的HelloWord例子

本文介绍如何在项目中引入并使用Drools规则引擎,包括必要的Maven依赖配置及简单示例代码。通过实例展示了如何加载规则文件、创建会话及触发规则执行等关键步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

添加drools框架运行的依赖

<!--Drools 规则相关 -->
        <dependency>
            <groupId>org.drools</groupId>
            <artifactId>drools-core</artifactId>
            <version>6.5.0.Final</version>
        </dependency>

        <dependency>
            <groupId>org.kie</groupId>
            <artifactId>kie-api</artifactId>
            <version>6.5.0.Final</version>
        </dependency>

        <dependency>
            <groupId>org.drools</groupId>
            <artifactId>drools-compiler</artifactId>
            <version>6.5.0.Final</version>
        </dependency>
public class DroolsHelloWorld {
    public static void main(String[] args) {
        // Class.forName( "org.drools.compiler.kie.builder.impl.KieServicesImpl" ).newInstance()
        KieServices kieServices = KieServices.Factory.get();
        //会去 resource/META-INF 下找 kmodule.xml 文件
        KieContainer kieContainer = kieServices.getKieClasspathContainer();
        // kmodule.xml中<ksession name="helloworld"/> name值   
        KieSession kieSession = kieContainer.newKieSession("helloworld");
        //启用规则
        Set set=new HashSet();  
        set.add("helloworld1");  
        set.add("helloworld2");   
        int a = kieSession.fireAllRules();
        /*FactHandle factHandle2 = kieSession.insert("456");
        int b = kieSession.fireAllRules(new RuleNameEndsWithAgendaFilter("helloWorld1"));
        kieSession.delete(factHandle2);*/
        kieSession.dispose();
    }
}

META-INF/kmodule.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<kmodule xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://www.drools.org/xsd/kmodule">
    
    <!-- packages 指向规则文件目录 -->
    <kbase name="hello" packages="com.dinpay.drools.helloworld">
        <!-- name在 创建 KIEsession 会用到 -->
        <ksession name="helloworld"/>
    </kbase>
    
    <!-- packages 指向规则文件目录   -->
    <kbase name="helloUpgraded" packages="com.dinpay.drools.helloworldUpgraded">
        <ksession name="helloworldUpgraded"/>
    </kbase>
    
        <!-- packages 指向规则文件目录   -->
    <kbase name="dinpay" packages="rule2">
        <ksession name="dinpayRule"/>
    </kbase>
</kmodule>

droolsHelloWorld.drl脚本编写

rule helloWorld2
    salience 100
    lock-on-active true
    when
        eval(true)
    then
      System.out.println("Helloworld drools2!  ");
end

rule helloWorld1
    salience 100
    lock-on-active true
    when
        eval(true)
    then
        
      System.out.println("Helloworld drools1!  ");
end

 

转载于:https://www.cnblogs.com/atomicbomb/p/7171475.html

好的,下面是一个简单的 Spring Boot 集成 Drools例子: 1. 首先,需要在 pom.xml 中添加 Drools 的依赖: ```xml <dependencies> <dependency> <groupId>org.drools</groupId> <artifactId>drools-core</artifactId> <version>7.41.0.Final</version> </dependency> </dependencies> ``` 2. 创建一个规则文件,例如 "rules.drl": ```drl package com.example.rules rule "rule1" when $a: String(this == "foo") then System.out.println("Found foo!"); end ``` 3. 创建一个 Java 类,用于调用 Drools 引擎: ```java package com.example; import org.kie.api.KieBase; import org.kie.api.KieServices; import org.kie.api.runtime.KieContainer; import org.kie.api.runtime.KieSession; import org.springframework.stereotype.Component; @Component public class DroolsService { private final KieServices kieServices; private final KieContainer kieContainer; public DroolsService(KieServices kieServices, KieContainer kieContainer) { this.kieServices = kieServices; this.kieContainer = kieContainer; } public void runRules(String message) { KieBase kieBase = kieContainer.getKieBase(); KieSession kieSession = kieBase.newKieSession(); kieSession.setGlobal("out", System.out); kieSession.insert(message); kieSession.fireAllRules(); kieSession.dispose(); } } ``` 4. 在 Spring Boot 应用程序中使用 DroolsService 类: ```java package com.example; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.Bean; import org.kie.api.KieServices; import org.kie.api.runtime.KieContainer; @SpringBootApplication public class ExampleApplication { public static void main(String[] args) { SpringApplication.run(ExampleApplication.class, args); } @Bean public KieServices kieServices() { return KieServices.Factory.get(); } @Bean public KieContainer kieContainer() { return kieServices().getKieClasspathContainer(); } @Bean public DroolsService droolsService(KieServices kieServices, KieContainer kieContainer) { return new DroolsService(kieServices, kieContainer); } } ``` 现在,你可以在应用程序中调用 DroolsService 的 runRules 方法,以运行规则文件中的规则: ```java @Autowired private DroolsService droolsService; public void example() { droolsService.runRules("foo"); } ``` 这将输出 "Found foo!"。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值