欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Drools动态更新规则

程序员文章站 2022-05-28 11:17:59
...

Drools简介

简单说明一下规则动态插拔使用到的Drools相关类。

类名 使用说明
KnowledgeBuilder 在业务代码中收集已编写的规则,并对规则文件进行编译,生成编译好的KnowledgePackage 集合,提供给其他API使用。通过其提供的hasErrors()方法获得编译过程中是否有错,getErrors()方法打印错误信息。支持drl文件、dslr文件和xls文件等。
KnowledgePackage 存放编译好的规则对象
KnowledgeBase 收集应用当中知识(knowledge)定义的知识库对象(KnowledgePackage)在一个 KnowledgeBase当中可以包含普通的规则(rule)、规则流(rule flow)、函数定义(function)、用户自定义对象(type model)等,并创建session对象(StatefulKnowledgeSession和StatelessKnowledgeSession)

动态加载规则

1. 项目依赖

		<dependency>
			<groupId>org.drools</groupId>
			<artifactId>drools-compiler</artifactId>
			<version>7.37.0.Final</version>
		</dependency>
		<dependency>
			<groupId>org.drools</groupId>
			<artifactId>drools-core</artifactId>
			<version>7.37.0.Final</version>
		</dependency>
		<dependency>
			<groupId>org.drools</groupId>
			<artifactId>drools-templates</artifactId>
			<version>7.37.0.Final</version>
		</dependency>

2. 实现

(1) 新增规则

首先,新增规则,通过KnowledgeBuilder收集规则,将规则文件进行编译;然后通过KnowledgeBuilder.getKnowledgePackages()方法获取KnowledgePackage;最后将KnowledgePackage对象存入KnowledgeBase,完成对规则库的动态插入规则。

    /**
     * @Description: 对knowledgeBase操作,规则库新增规则操作
     * @Param: InternalKnowledgeBase knowledgeBase 规则库
     * @Param: String ruleContent 规则内容
     * @return:
     */
    private void addKnowledgePackages(InternalKnowledgeBase knowledgeBase, String ruleContent) {
        try {
            KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
            Resource resource = ResourceFactory.newReaderResource(new StringReader(ruleContent));
            kbuilder.add(resource, ResourceType.DRL);
            if (kbuilder.hasErrors()) {
                log.info("ADD RULE FILE ERROR: " + kbuilder.getErrors().toString());
            }
            knowledgeBase.addPackages(kbuilder.getKnowledgePackages());
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
    }

(2) 删除规则

使用getRule()、removeRule()方法实现规则删除。

    /**
     * @Description: 删除规则
     * @Param: ruleName 规则名称
     * @Param: knowledgeBase 规则库
     * @Param: packageName 包命名
     * @return: 
     */
    private void delRule(String ruleName, InternalKnowledgeBase knowledgeBase, String packageName) {
        Rule rule = knowledgeBase.getRule(packageName, ruleName);
        if (rule != null) {
            knowledgeBase.removeRule(packageName, ruleName);
            log.info("Del RuleName : {}", ruleName);
        }
    }

(3) 更新规则

更新规则过程就是重复上述删除+新增的过程,不再赘述。

最后,贴一下规则示例,仅供参考:

package rule.noticepkg
import java.util.*;
import com.indocloud.indo.oss.fm.engine.dto.AlarmDTO;
import org.drools.core.impl.InternalKnowledgeBase;
import org.drools.core.impl.KnowledgeBaseFactory;
import org.kie.internal.builder.KnowledgeBuilder;
import org.kie.internal.builder.KnowledgeBuilderError;
import org.kie.internal.builder.KnowledgeBuilderErrors;
import org.kie.internal.builder.KnowledgeBuilderFactory;
import org.kie.internal.io.ResourceTypeImpl;
import org.kie.internal.io.ResourceFactory;
import org.kie.internal.logger.KnowledgeRuntimeLoggerFactory;
import org.kie.internal.runtime.StatefulKnowledgeSession;
import function com.indocloud.indo.oss.fm.engine.util.AlarmExecuteActionUtils.executeAction;

rule "c38f77a9a883424993c59c48b338dbf0"
  lock-on-active true
  salience 2
	when
		$alarm:AlarmDTO(originType == 'alarm' && originSeverity == '4')
	then
	   HashMap<String,Object> actionMap = new HashMap<String,Object>();
	   actionMap.put("frnType","2");
       executeAction(actionMap,$alarm,"7","c38f77a9a883424993c59c48b338dbf0");
end
相关标签: Drools