repeatthink123yuchao commented on issue #5731:
URL:
https://github.com/apache/incubator-kie-drools/issues/5731#issuecomment-1959250101
> That's cool because this is probably one of the biggest rules based
installation of which I have ever heard. Regarding your question I have a few
suggestions:
>
> 1. Are you using the executable model, i.e. rewriting the rules in pure
Java at compile time? If not I strongly suggest to do so: it will make the
compilation time a little longer but will pay back with a much faster startup
at runtime.
> 2. Do all those rules actually belong to the knowledge base? Isn't it
possible to shard them, based on the business logic of your application, and
then split them in multiple KieBases, each with a more confined and better
defined business scope?
> 3. If you have so many rules I bet that they are automatically generated
in some way? If so can you please provide an example of those rules? Maybe we
could find some common pattern allowing us to rewrite those rules in a more
compact way and reduce their cardinality.
Thank you very much for your reply. The details of the situation are as
follows:
1 The current application only has 1 InternalKnowledgeBase
2. The system starts up and loads the drl file in the database. The database
is read and the row records are written to the disk drl file. The contents of
the file are as follows
directory /tmp/2024022219/rule_603_1_0_0_0.drl
package rule_603_1_0_0;
import java.util.*;
import static xxx.RuleUtils.*;
rule "rule_RD00000214"
no-loop true
lock-on-active true
salience 11
agenda-group "rule_603_1_0_0"
when
$map : Map(
RuleUtils.ruleEquals(rule_package_name,"rule_603_1_0_0"),equalsString(isExistListInRedis($map,"BLACK","[Org_no=documentNumber,P_subscribeLabel=subscribeLabel]&[License=documentNumber,P_subscribeLabel=subscribeLabel]"),"Y")
)
then
RuleUtils.assembleHitRuleCodes($map,"RD00000214");
RuleUtils.put($map,"RSE00000083_RGC00007454_setresult","pass");
RuleUtils.put($map,"M_ID_matched_flag","Y");
end
3 The project startup code is like this
/**
* Initialize rule component group
*/
@PostConstruct
public void concurrencyLoadInit() {
String bn = "concurrency:policy rule initialization";
Boolean result = Boolean.TRUE;
StopWatch stopWatchForAll = StopWatch.createStarted();
//group counter
Integer groupNumberCounter = 0;
//package counter
Integer packageCounter = 0;
//rule counter
try {
logger.info("{}:阶段:begin", bn);
RuleKieBaseTool tempMap = new RuleKieBaseTool();
StopWatch stopWatch = StopWatch.createStarted();
//2.读取rule表配置信息 策略发布状态成功 + 规则组件状态上线成功 +规则已发布
List<LprRulTabDTO> lprRulTabDTOList =
rulTabMapper.selectByStatus(RuleEnum.RULE_RELEASE_SUCCESS.getCode());
log.info("{}:阶段:策略规则初始化获取规则数据库配置完成 行记录数:{} 耗时:{}", bn,
lprRulTabDTOList.size(), stopWatch.getTime());
stopWatch.reset();
stopWatch.start();
//Map<package,List<rule>>
Map<String, List<String>> ruleListMap =
decodeRuleSetByTab(lprRulTabDTOList);
packageCounter = ruleListMap.size();
log.info("{}:阶段:规则按包分组 耗时:{}", bn, stopWatch.getTime());
stopWatch.reset();
stopWatch.start();
//to disk
String droolsDirectory =
kbsLoadPackageRuleManager.toDisk(ruleListMap);
log.info("{}:阶段:写入drl文件 目录:{} 耗时:{}", bn, droolsDirectory,
stopWatch.getTime());
List<File> drlFileList =
FileUtil.listFilesBySuffix(droolsDirectory, FileSuffixEnum.DRL);
if (CollectionUtils.isEmpty(drlFileList)) {
return;
}
log.info("{}:阶段:读取drl文件 目录:{} drl文件数:{} 耗时:{}", bn,
droolsDirectory, drlFileList.size(), stopWatch.getTime());
stopWatch.reset();
stopWatch.start();
List<FileSystemResource> fileSystemResourceList =
drlFileList.stream().map(a -> new
FileSystemResource(a)).collect(Collectors.toList());
KnowledgeBuilder knowledgeBuilder0010 =
KnowledgeBuilderFactory.newKnowledgeBuilder();
CompositeKnowledgeBuilder compositeKnowledgeBuilder0010 =
knowledgeBuilder0010.batch();
compositeKnowledgeBuilder0010.type(ResourceType.DRL);
fileSystemResourceList.stream().forEach(a -> {
compositeKnowledgeBuilder0010.add(a);
});
log.info("{}:阶段:CompositeKnowledgeBuilder.add 耗时:{}", bn,
stopWatch.getTime());
stopWatch.reset();
stopWatch.start();
log.info("{}:阶段:CompositeKnowledgeBuilder.build begin", bn);
compositeKnowledgeBuilder0010.build();
log.info("{}:阶段:CompositeKnowledgeBuilder.build 耗时:{}", bn,
stopWatch.getTime());
KnowledgeBuilderErrors knowledgeBuilderErrors =
knowledgeBuilder0010.getErrors();
if (CollectionUtils.isNotEmpty(knowledgeBuilderErrors)) {
knowledgeBuilderErrors.iterator().forEachRemaining((x) -> {
String errorMessage = String.format("line [ %s ] - %s",
JSON.toJSONString(x.getLines()), x.getMessage());
log.error("{}:阶段:CompositeKnowledgeBuilder.build 编译报错
耗时:{}", bn, errorMessage);
});
AssertUtil.isTrue(Boolean.FALSE, BizErrorCode.START_ERROR,
"drl compile error");
return;
}
stopWatch.reset();
stopWatch.start();
tempMap.getKIEBASE().addPackages(knowledgeBuilder0010.getKnowledgePackages());
log.info("{}:阶段:kbs.addPackages 耗时:{}", bn, stopWatch.getTime());
//4.替换map
ruleKieBaseTool = tempMap;
} catch (Throwable e) {
result = Boolean.FALSE;
log.error("{}:阶段:策略规则加载结束 系统异常:异常文本:{} 异常类型:{} 异常栈:", bn,
e.getMessage(), e.getClass().getName(), e);
throw e;
} finally {
log.info("{}:阶段:策略规则加载结束 result:{} 耗时:{} 组数:{} 每组包数:{} 包数:{} ",
bn, result, stopWatchForAll.getTime(), groupNumberCounter,
PACKAGE_SIZE_ONE_GROUP, packageCounter);
//async access kbs
//kbsLoadPackageRuleManager.asyncAccessKbs(ruleKieBaseTool.getKIEBASE());
}
}
//end+++++++++++++++++++++++++
4 I do not understand "rewriting the rules in pure Java at compile
time",Can you give me a demo? for /tmp/2024022219/rule_603_1_0_0_0.drl file
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]