Drool实战系列(一)之入门程序
Drools官网地址为:https://www.drools.org/
maven环境
入门程序例子如下:
项目结构截图:
一、导入pom文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | < project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> < modelVersion >4.0.0</ modelVersion > < groupId >cn.drools</ groupId > < artifactId >drools-demo</ artifactId > < version >0.0.1-SNAPSHOT</ version > < dependencies > < dependency > < groupId >org.drools</ groupId > < artifactId >drools-core</ artifactId > < version >6.2.0.Final</ version > </ dependency > < dependency > < groupId >org.drools</ groupId > < artifactId >drools-compiler</ artifactId > < version >6.2.0.Final</ version > </ dependency > </ dependencies > </ project > |
二、准备JavaBean
package com.test.bean; public class Person { private String name; private int age; private String desc; public Person(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getDesc() { return desc; } public void setDesc(String desc) { this.desc = desc; } public String toString() { return "[name="+name+",age="+age+",desc="+desc+"]"; } }
三、在resource下建立rule文件夹编写drl文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | package com.person; import com.test.bean.Person; rule "boy" salience 1 when $p : Person(age > 0); then $p.setDesc("少年"); retract($p); end rule "youth" salience 2 when $p : Person(age > 12); then $p.setDesc("青年"); retract($p); end rule "midlife" salience 3 when $p : Person(age > 24); then $p.setDesc("中年"); retract($p); end rule "old" salience 4 when $p : Person(age > 60); then $p.setDesc("老年"); retract($p); end |
四、编写kmodule.xml文件
1 2 3 4 5 6 | <? xml version="1.0" encoding="UTF-8"?> < kmodule xmlns="http://jboss.org/kie/6.0.0/kmodule"> < kbase name="SimpleRuleKBase" packages="rule"> < ksession name="simpleRuleKSession" default="true"/> </ kbase > </ kmodule > |
五、编写测试类
package com.test.bean; import org.kie.api.KieServices; import org.kie.api.runtime.KieContainer; import org.kie.api.runtime.KieSession; public class Test { static KieSession getSession() { KieServices ks = KieServices.Factory.get(); KieContainer kc = ks.getKieClasspathContainer(); return kc.newKieSession("simpleRuleKSession"); } public static void main(String[] args) { KieSession ks = getSession(); Person p1 = new Person("A", 68); Person p2 = new Person("B", 32); Person p3 = new Person("C", 18); Person p4 = new Person("D", 8); System.out.println("before p1 : " + p1); System.out.println("before p2 : " + p2); System.out.println("before p3 : " + p3); System.out.println("before p4 : " + p4); ks.insert(p1); ks.insert(p2); ks.insert(p3); ks.insert(p4); int count = ks.fireAllRules(); System.out.println("总执行了"+count+"条规则"); System.out.println("after p1 : " + p1); System.out.println("after p2 : " + p2); System.out.println("after p3 : " + p3); System.out.println("after p4 : " + p4); ks.dispose(); } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器