随着商业发展,业务上经常会发生快速变动。如果还按照以往的开发模式,业务逻辑的每次变动都需要程序员介入开发,那时间成本太高。
为了解决这个问题,Drools这一类可以通过外部配置调整业务逻辑的工具应运而生,本章就说一说如何用springboot集成Drools
第一步:pom.xml
<properties>
<!-- drools 规则引擎 版本 -->
<drools.version>7.23.0.Final</drools.version>
</properties>
<!-- start drools -->
<dependency>
<groupId>org.drools</groupId>
<artifactId>drools-core</artifactId>
<version>${drools.version}</version>
</dependency>
<dependency>
<groupId>org.drools</groupId>
<artifactId>drools-compiler</artifactId>
<version>${drools.version}</version>
</dependency>
<dependency>
<groupId>org.kie</groupId>
<artifactId>kie-spring</artifactId>
<version>${drools.version}</version>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.kie</groupId>
<artifactId>kie-internal</artifactId>
<version>${drools.version}</version>
</dependency>
<dependency>
<groupId>org.kie</groupId>
<artifactId>kie-api</artifactId>
<version>${drools.version}</version>
</dependency>
<dependency>
<groupId>org.drools</groupId>
<artifactId>drools-templates</artifactId>
<version>${drools.version}</version>
</dependency>
<!-- end drools -->
第二步、使用
01、配置规则
package xu.rule;
public class RuleExecute {
public static String activityRule() {
StringBuffer ruleDrl = new StringBuffer();
ruleDrl.append("package rules\r\n" +
"import xu.entity.Person;\r\n" +
"rule \"test person\"\r\n" +
"no-loop true \n"+
"salience 10"+
" when\r\n" +
" $p:Person(name==\"张三\")\r\n" +
" then\r\n" +
" modify($p){\r\n" +
" setName(\"张小三\"),setAge(\"30\")\r\n" +
" }\r\n" +
"end");
return ruleDrl.toString();
}
}
02、把规则写入规则库
package xu.rule;
import org.kie.api.KieBase;
import org.kie.api.io.ResourceType;
import org.kie.internal.utils.KieHelper;
public class NewKieBase {
//将业务规则写到规则库中
public static KieBase rulekieBase(String rule) {//rule值就是我们动态传入的规则内容
System.out.println("规则内容为>>>>>>>>>"+"\n"+rule);
KieHelper helper = new KieHelper();
KieBase kieBase = null;
try {
helper.addContent(rule,ResourceType.DRL);
//为了省事,直接将rule写成activityRule()
kieBase = helper.build();
} catch (Exception e) {
e.printStackTrace();
}
if(kieBase==null) {
System.out.println("kiebase没有正确生成!!!!!");
}
return kieBase;
}
}
03、创建drools服务
package xu.service;
import org.kie.api.KieBase;
import org.springframework.stereotype.Service;
//import com.rule.NewKieBase.rulekieBase;
import xu.rule.NewKieBase;
import xu.rule.RuleExecute;
@Service
public class DroolsService {
/**
* 创建KieSession
* @return
*/
public KieBase newKieBase() {
KieBase kieBase = NewKieBase.rulekieBase(RuleExecute.activityRule());
return kieBase;
}
}
04、在业务逻辑中调用
package xu.service;
import java.util.List;
import org.kie.api.KieBase;
import org.kie.api.runtime.KieSession;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import xu.dao.PersonMapper;
import xu.entity.Person;
/**
*@date edit at 2020年4月21日
*/
@Service
public class PersonService {
@Autowired
private PersonMapper pm;
@Autowired
private DroolsService ds;
protected final org.slf4j.Logger logger = LoggerFactory.getLogger(this.getClass());
public List<Person> getAllPerson(){
KieBase kieBase=ds.newKieBase();
List<Person> list=pm.getAllPerson();
for(Person temp:list) {
// System.out.print(temp.getName());
KieSession kieSession=kieBase.newKieSession();
kieSession.insert(temp);
int i=kieSession.fireAllRules();
System.out.println("符合条件的数据有>>>>>>>>>>>>"+i);
kieSession.dispose();
}
return list;
}
}
总结:
Drools配置使用非常简单,语法也谈不上复杂,但是胜在可以通过拼接字符串的方式从数据库动态生成规则。当用户在前端配置好了自己想要的规则以后,就可以动态入库,随时根据需求调整获得的数据。
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/153546.html