我在使用Spring和Drools时遇到了问题。我的主要问题是在单元测试中不会发生错误。遵循异常
java.lang.RuntimeException: Unexpected global [premioService]
当我尝试在KieSession中设置全局变量时,会发生此错误 看到方法StatefulKnowledgeSessionImpl #setGlobal(String,Object)似乎我应该设置 在创建newInstance之前的全局变量。遵循StatefulKnowledgeSessionImpl#setGlobal代码:
public void setGlobal(final String identifier,
final Object value) {
// Cannot set null values
if ( value == null ) {
return;
}
try {
this.kBase.readLock();
startOperation();
// Make sure the global has been declared in the RuleBase
final Map globalDefintions = this.kBase.getGlobals();
final Class type = (Class) globalDefintions.get( identifier );
if ( (type == null) ) {
throw new RuntimeException( "Unexpected global [" + identifier + "]" );
} else if ( !type.isInstance( value ) ) {
throw new RuntimeException( "Illegal class for global. " + "Expected [" + type.getName() + "], " + "found [" + value.getClass().getName() + "]." );
} else {
this.globalResolver.setGlobal( identifier,
value );
}
} finally {
endOperation();
this.kBase.readUnlock();
}
}
按照我的代码:
@Inject
protected PremioVisaoService premioVisaoService;
protected final KieSession createSession() {
return this.kieBase.newKieSession();
}
protected final int process() {
final KieSession kieSession = this.createSession();
Object rulesFired = 0;
try {
//here occurs the error
kieSession.execute(CommandFactory.newSetGlobal(PREMIO_SERVICE_GLOBAL_ID, premioVisaoService));
} catch(Exception e) {
e.printStackTrace();
}
}
package br.com.company.brms.model.rules;
import br.com.company.brms.model.*;
import br.com.company.brms.model.premios.*;
import br.com.company.brms.model.tarifas.*;
import function br.com.company.brms.helpers.DomainUtils.getPais;
import function br.com.company.brms.helpers.DomainUtils.getUF;
import function br.com.company.brms.helpers.PremioFactory.novoPremioVisaoPercursoPadrao;
import function br.com.company.brms.helpers.CalculoTarifaHelper.calculaTaxaBasica;
global br.com.company.brms.services.PremioVisaoService premioService;
rule "Rule Example"
ruleflow-group "calculo"
salience -1
when
$averbacao : Averbacao( indicadorAvaria == Constantes.STATUS_SIM )
$taxa : TarifaPercursoVigencia( tipoTarifa == TipoTarifa.AVARIA)
then
PremioVisaoPercursoPadrao premio = novoPremioVisaoPercursoPadrao($taxa, $averbacao);
premio.setValor( calculaTaxaBasica($taxa, $averbacao) );
//insert ( premio );
premioService.inserirPremioCalculado( premio );
//System.out.println( $averbacao + " calculada com o premio: " + premio );
end
跟随堆栈跟踪:
java.lang.RuntimeException: Unexpected global [premioService]
at org.drools.core.impl.StatefulKnowledgeSessionImpl.setGlobal(StatefulKnowledgeSessionImpl.java:1124)
at org.drools.core.command.runtime.SetGlobalCommand.execute(SetGlobalCommand.java:65)
at org.drools.core.impl.StatefulKnowledgeSessionImpl.execute(StatefulKnowledgeSessionImpl.java:665)
at org.drools.core.impl.StatefulKnowledgeSessionImpl.execute(StatefulKnowledgeSessionImpl.java:648)
at br.com.company.brms.services.impl.BilhetagemBpmnRunnerServiceImpl.processar(BilhetagemBpmnRunnerServiceImpl.java:87)
at br.com.company.brms.services.impl.BilhetagemBpmnRunnerServiceImpl.processarRegrasMercado(BilhetagemBpmnRunnerServiceImpl.java:52)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:96)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:260)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:94)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
at $Proxy68.processarRegrasMercado(Unknown Source)
at br.com.company.brms.services.impl.BilhetagemProcessManagerServiceImpl.processar(BilhetagemProcessManagerServiceImpl.java:111)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:96)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:260)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:94)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
at $Proxy69.processar(Unknown Source)
at br.com.company.brms.spi.impl.BilhetagemServiceAsyncImpl.processarPorCliente(BilhetagemServiceAsyncImpl.java:88)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
at org.springframework.aop.interceptor.AsyncExecutionInterceptor$1.call(AsyncExecutionInterceptor.java:95)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
at java.util.concurrent.FutureTask.run(FutureTask.java:138)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:662)
事先提前
答案 0 :(得分:2)
我的问题是我在不同的罐子里寻找DRL文件。我还不知道我将如何以正确的方式做到这一点。但问题肯定已经解决了。
答案 1 :(得分:0)
我终于找到了根本原因是什么
如果您有一条规则,并且想为该规则setGlobal
,则必须在该规则(.drl文件)中定义了全局变量,例如;
global net.mikeski.ProviderImpl provider
然后,kSession.setGlobal("provider", myProviderImpl);
将起作用。
我通过查看setGlobal
中的Drools StatefulKnowledgeSessionImpl.java
方法发现了这一点:
...
final Class type = (Class) globalDefintions.get( identifier );
if ( (type == null) ) {
throw new RuntimeException( "Unexpected global [" + identifier + "]" );
} else if ( !type.isInstance( value ) ) {
throw new RuntimeException( "Illegal class for global. " + "Expected [" + type.getName() + "], " + "found [" + value.getClass().getName() + "]." );
} else {
this.globalResolver.setGlobal( identifier,
value );
}
...