直接进入正题

我们在使用开源规则引擎 Drools 的时候, 启动的时候可能会抛出如下异常:

Caused by: java.lang.ClassCastException: cn.com.chengzi.drools.domain.cofing.DroolsAutoConfiguration$1 cannot be cast to org.drools.compiler.kie.builder.impl.InternalKieModule
at org.drools.compiler.kie.builder.impl.KieServicesImpl.newKieContainer(KieServicesImpl.java:184)
at org.drools.compiler.kie.builder.impl.KieServicesImpl.newKieContainer(KieServicesImpl.java:172)
at cn.com.chengzi.drools.domain.cofing.DroolsAutoConfiguration.kieContainer(DroolsAutoConfiguration.java:57)
at cn.com.chengzi.drools.domain.cofing.DroolsAutoConfiguration$$EnhancerBySpringCGLIB$$f73b7244.CGLIB$kieContainer$2(<generated>)
at cn.com.chengzi.drools.domain.cofing.DroolsAutoConfiguration$$EnhancerBySpringCGLIB$$f73b7244$$FastClassBySpringCGLIB$$c4bed561.invoke(<generated>)
at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:244)
at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:363)
at cn.com.chengzi.drools.domain.cofing.DroolsAutoConfiguration$$EnhancerBySpringCGLIB$$f73b7244.kieContainer(<generated>)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154)
... 43 more
Caused by: java.lang.ClassCastException: cn.com.chengzi.drools.domain.cofing.DroolsAutoConfiguration$1 cannot be cast to org.drools.compiler.kie.builder.impl.InternalKieModule
at org.drools.compiler.kie.builder.impl.KieServicesImpl.newKieContainer(KieServicesImpl.java:184)
at org.drools.compiler.kie.builder.impl.KieServicesImpl.newKieContainer(KieServicesImpl.java:172)
at cn.com.chengzi.drools.domain.cofing.DroolsAutoConfiguration.kieContainer(DroolsAutoConfiguration.java:57)
at cn.com.chengzi.drools.domain.cofing.DroolsAutoConfiguration$$EnhancerBySpringCGLIB$$f73b7244.CGLIB$kieContainer$2(<generated>)
at cn.com.chengzi.drools.domain.cofing.DroolsAutoConfiguration$$EnhancerBySpringCGLIB$$f73b7244$$FastClassBySpringCGLIB$$c4bed561.invoke(<generated>)
at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:244)
at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:363)
at cn.com.chengzi.drools.domain.cofing.DroolsAutoConfiguration$$EnhancerBySpringCGLIB$$f73b7244.kieContainer(<generated>)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154)
... 43 more

查看发现 KieServicesImpl 类中的 newKieContainer 发现抛出了类型转换异常:

public KieContainer newKieContainer(String containerId, ReleaseId releaseId, ClassLoader classLoader) {
InternalKieModule kieModule = (InternalKieModule) getRepository().getKieModule(releaseId);
if (kieModule == null) {
throw new RuntimeException("Cannot find KieModule: " + releaseId);
}
if (classLoader == null) {
classLoader = kieModule.getModuleClassLoader();
}
KieProject kProject = new KieModuleKieProject( kieModule, classLoader );
if (classLoader != kProject.getClassLoader()) {
// if the new kproject has a different classloader than the original one it has to be initialized
kProject.init();
}
	if (containerId == null) {
KieContainerImpl newContainer = new KieContainerImpl( UUID.randomUUID().toString(), kProject, getRepository(), releaseId );
return newContainer;
}
if ( kContainers.get(containerId) == null ) {
KieContainerImpl newContainer = new KieContainerImpl( containerId, kProject, getRepository(), releaseId );
KieContainer check = kContainers.putIfAbsent(containerId, newContainer);
if (check == null) {
return newContainer;
} else {
newContainer.dispose();
throw new IllegalStateException("There's already another KieContainer created with the id "+containerId);
}
} else {
throw new IllegalStateException("There's already another KieContainer created with the id "+containerId);
}
}

public KieContainer newKieContainer(String containerId, ReleaseId releaseId, ClassLoader classLoader) {

InternalKieModule kieModule = (InternalKieModule) getRepository().getKieModule(releaseId);

if (kieModule == null) {

throw new RuntimeException("Cannot find KieModule: " + releaseId);

}

if (classLoader == null) {

classLoader = kieModule.getModuleClassLoader();

}

KieProject kProject = new KieModuleKieProject( kieModule, classLoader );

if (classLoader != kProject.getClassLoader()) {

// if the new kproject has a different classloader than the original one it has to be initialized

kProject.init();

}

	<span class="hljs-keyword">if</span> (containerId == <span class="hljs-literal">null</span>) {
KieContainerImpl newContainer = <span class="hljs-keyword">new</span> KieContainerImpl( UUID.randomUUID().toString(), kProject, getRepository(), releaseId );
<span class="hljs-keyword">return</span> newContainer;
}
<span class="hljs-keyword">if</span> ( kContainers.<span class="hljs-keyword">get</span>(containerId) == <span class="hljs-literal">null</span> ) {
KieContainerImpl newContainer = <span class="hljs-keyword">new</span> KieContainerImpl( containerId, kProject, getRepository(), releaseId );
KieContainer check = kContainers.putIfAbsent(containerId, newContainer);
<span class="hljs-keyword">if</span> (check == <span class="hljs-literal">null</span>) {
<span class="hljs-keyword">return</span> newContainer;
} <span class="hljs-keyword">else</span> {
newContainer.dispose();
<span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> IllegalStateException(<span class="hljs-string">"There's already another KieContainer created with the id "</span>+containerId);
}
} <span class="hljs-keyword">else</span> {
<span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> IllegalStateException(<span class="hljs-string">"There's already another KieContainer created with the id "</span>+containerId);
}
}

此为初始化 drl 文件时的异常, 说明我们的 drl 文件不规范, Drools 不能解析成功.

我仔细检查了一下:

rule "read-Reading-speed"
when
$rlt:ReadingLevelTest(totalWordsNum != null,totalSecondTime != null)
$rl:ReadingLevel(readingSpeed == null)
then
$rl.setReadingSpeed($rlt.getTotalWordsNum()/$rlt.getTotalSecondTime()/60);
update($rl)
System.out.println("测评结果读速: " + $rl.toString());
end
rule "read-Reading-speed"
when
$rlt:ReadingLevelTest(totalWordsNum != null,totalSecondTime != null)
$rl:ReadingLevel(readingSpeed == null)
then
$rl.setReadingSpeed($rlt.getTotalWordsNum()/$rlt.getTotalSecondTime()/60);
update($rl)
System.out.println("测评结果读速: " + $rl.toString());
end

发现其实是因为在 then 中的 update($rl) 后没有用分号结尾, 加上分号运行正常.

  • when 后面每行表达式后面是不需要添加分号结尾的
  • then 后面为 java 代码, 每行必须使用分号结尾, 如果我们忘记了添加分号,编译器也会报错题型的, 但是有一些特例, 比如 Drools 提供的方法 update(),insert 等等, 如果后面不加分号, 编译器是不会报错的, 但是运行的时候就会抛出解析失败!

标题:开源规则引擎 Drools 学习笔记 之 -- 1 cannot be cast to org.drools.compiler.kie.builder.impl.InternalKieModule

作者:chengzime

地址:https://www.chengzime.com.cn/articles/2019/09/11/1568195807017.html



原文地址:https://www.chengzime.com.cn/articles/2019/09/11/1568195807017.html

开源规则引擎 Drools 学习笔记 之 -- 1 cannot be cast to org.drools.compiler.kie.builder.impl.InternalKieModule的更多相关文章

  1. Drools学习笔记-01-在eclipse indgo集成Drools5.5

    1.1.条件 Drools它是一个基于Java开源规则引擎.因此,使用Drools以及前需要安装在开发机器JDK周边环境,Drools5.5需要JDK版本号的1.5或者更多. 1.2.开发环境搭建 大 ...

  2. Drools学习笔记

    Drools是一款基于Java的开源规则引擎 实现了将业务决策从应用程序中分离出来. 优点: 1.简化系统架构,优化应用 2.提高系统的可维护性和维护成本 3.方便系统的整合 4.减少编写“硬代码”业 ...

  3. Android开源项目SlidingMenu本学习笔记(两)

    我们已经出台SlidingMenu使用:Android开源项目SlidingMenu本学习笔记(一个),接下来再深入学习下.依据滑出项的Menu切换到相应的页面 文件夹结构: watermark/2/ ...

  4. 开源规则引擎 drools

    java语言开发的开源业务规则引擎 DROOLS(JBOSS RULES )具有一个易于访问企业策略.易于调整以及易于管理的开源业务规则引擎,符合业内标准,速度快.效率高.业务分析师或审核人员可以利用 ...

  5. Drools学习笔记1—规则文件

    Facts(即普通的POJO) 指普通业务对象插入到Workingmemory后的对象规则可以对fact对象进行任意的对象操作是规则与应用系统交换的桥梁返回FactHandler对象,是插入到Work ...

  6. 开源项目SuperSocket的学习笔记

    近几日想在一个项目中引进一个Socket Server,用来接收客户端发送的命令消息并根据具体的业务逻辑对消息进行处理,然后转发给其它在线的客户端.因为以前在博客园关注过江大渔开源的SuperSock ...

  7. 【Asphyre引擎】学习笔记(二)

    转一篇火人论坛那边的一份学习文档,我简单排一下版,希望对入门者有帮助. 感谢China Yang,这份文档也帮助我快速入了门. 和我一起学 Asphyre Sphinx Framework v1.0. ...

  8. 开源流媒体服务器SRS学习笔记(1) - 安装、推流、拉流

    SRS(Simple RTMP Server)  是国人写的一款非常优秀的开源流媒体服务器软件,可用于直播/录播/视频客服等多种场景,其定位是运营级的互联网直播服务器集群. 一.安装 官网提供了3种安 ...

  9. Drools学习笔记4—Consequence/RHS

    Right Hand Side,当LHS所有条件满足才会执行 可以使用LHS部分定义的绑定变量.全局变量.或者直接编写JAVA代码. 提供宏函数操作working memory fact对象,如ins ...

随机推荐

  1. linux echo命令颜色显示

    echo命令颜色显示: echo:      -n:  不换行.      -e:让转移符生效. \t(tab) \n (换行) 实例: $ echo -e "\033[34mabcd\03 ...

  2. c# MVC5(一) 初步认识以及新建mvc

    一:MVC5初始 1:广义MVC(Model--View-Controller): V是界面 : M是数据和逻辑 : C是控制,把M和V链接起来: 是程序设计模式,一种设计理念,可以有效的分离界面和业 ...

  3. 让Linux中的Nginx支持中文文件名

    原文:https://blog.csdn.net/soeben/article/details/79525964 首先你的服务器需要安装了UTF-8字符集在命令行里输入env|grep LANG如果显 ...

  4. 数据库中的gt,ge,lt,le的区别

    eq相等   ne.neq不相等,   gt大于, lt小于 gte.ge大于等于   lte.le 小于等于   not非   mod求模   is [not] div by是否能被某数整除   i ...

  5. robotframework-selenium2library各个版本

    https://github.com/robotframework/Selenium2Library/downloads

  6. 网络编程 TCP协议:三次握手,四次回收,反馈机制 socket套接字通信 粘包问题与解决方法

    TCP协议:传输协议,基于端口工作 三次握手,四次挥手 TCP协议建立双向通道. 三次握手, 建连接: 1:客户端向服务端发送建立连接的请求 2:服务端返回收到请求的信息给客户端,并且发送往客户端建立 ...

  7. 搭建代理服务器时的笔记,request使用笔记

    request 请求笔记: 1.opation中使用form字段传参 对应 content-type': 'application/x-www-form-urlencoded',如果想要content ...

  8. hdu6222——佩尔方程&&大数__int128

    题意 给定一个整数 $N$($1 \leq N \leq 10^{30}$),求最小的整数 $t$,要求 $t \geq N$,使得边长为 $t-1, t, t+1$ 的三角形面积为整数. 分析 根据 ...

  9. Scrapy笔记(1)- 入门篇

    Scrapy笔记01- 入门篇 Scrapy是一个为了爬取网站数据,提取结构性数据而编写的应用框架.可以应用在包括数据挖掘, 信息处理或存储历史数据等一系列的程序中.其最初是为了页面抓取(更确切来说, ...

  10. git crate&query&delete tag(九)

    root@vmuer-VirtualBox:/opt/myProject# git log --pretty=oneline0169b7a1c4bccb47e76711f353fd8d3864bde9 ...