我在尝试将 CEP 示例从 Drools 5X 转换为 6X 时遇到了一个奇怪的错误: - 准确地说是 Drool 6.1.0.Final。
我对这个小项目的灵感来源可以在以下链接中找到:=> PlugTree。
我得到的错误表明 Drools无法创建字段提取器- 当他们忘记在其域 POJO 中创建 setter/getter 时会出现错误。
严重:无法构建 KieBaseModel:规则 无法为类“com.sample.Sale”找不到“amount”字段/方法“amount”创建字段提取器:[Rule name='StoreOne - Has Passed it's Sales Record'] java .lang.RuntimeException:找不到类“com.sample.Sale”的字段/方法“金额”
我已经将问题追溯到规则文件中的“声明”语句(我将在下面列出完整列表):
declare Sale
@role(event)
end
仅使用它会导致错误(顺便说一句,在 V5 中不会发生),但是使用下一个“更改的”声明语句不会导致错误。它只是什么都不做......
declare Sale
@role(event)
article : String
amount : long
quantity : int
end
它所做的 - 什么都不是。它编译,运行,但事实只是没有被插入(或识别)。
这是我的 Java 测试工具:
package com.sample.cep;
import org.kie.api.KieBaseConfiguration;
import org.kie.api.KieServices;
import org.kie.api.conf.EventProcessingOption;
import org.kie.api.event.rule.DebugAgendaEventListener;
import org.kie.api.event.rule.DebugRuleRuntimeEventListener;
import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSession;
import org.kie.api.runtime.conf.ClockTypeOption;
import org.kie.api.runtime.rule.EntryPoint;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class CEPExample {
public static void main(String[] args) {
try {
// load up the knowledge base & get the kSession
KieServices ks = KieServices.Factory.get();
KieContainer kContainer = ks.getKieClasspathContainer();
KieSession kSession = kContainer.newKieSession("ksession-rules");
// CEP - get the KIE related configuration container and set the EventProcessing (from default cloud) to Stream
KieBaseConfiguration config = ks.newKieBaseConfiguration();
config.setOption( EventProcessingOption.STREAM );
// Listeners
kSession.addEventListener( new DebugAgendaEventListener() );
kSession.addEventListener( new DebugRuleRuntimeEventListener() );
// To setup a file based audit logger, uncomment the next line
// KieRuntimeLogger loggerKie = ks.getLoggers().newFileLogger( kSession, "./logger" );
// KieRuntimeLogger consoleLogger = ks.getLoggers().newConsoleLogger(kSession);
Logger logger = LoggerFactory.getLogger(CEPExample.class);
logger.info("\n*********************************>>>> Drools CEP Example \n");
// Each Event is Inserted into WorkingMemory through an *EntryPoint*
EntryPoint entryPointStoreOne = kSession.getEntryPoint( "StoreOne" );
EntryPoint entryPointStoreTwo = kSession.getEntryPoint( "StoreTwo" );
// Insert EventData into WM for StoreOne
entryPointStoreOne.insert(new Sale("meat", 40, 5) );
entryPointStoreOne.insert(new Sale("bananna", 5, 10) );
entryPointStoreOne.insert(new Sale("pear", 5, 10) );
entryPointStoreOne.insert(new Sale("yogurt", 5, 50) );
entryPointStoreOne.insert(new Sale("led TV", 10000, 1) );
// Insert EventData into WM for StoreTwo
entryPointStoreTwo.insert(new Sale("meat", 40, 5) );
entryPointStoreTwo.insert(new Sale("bananna", 5, 10) );
entryPointStoreTwo.insert(new Sale("pear", 5, 10) );
entryPointStoreTwo.insert(new Sale("yogurt", 5, 50) );
// Fire all Rules
kSession.fireAllRules();
// Close Logger
//logger.close();
// Close the session
kSession.destroy();
System.out.println("*** DONE *** ");
} catch (Throwable t) {
t.printStackTrace();
}
} // End Method - MAIN
// // Helper Class to INSERTEVENT
// private static void insertEvent(EntryPoint entryPoint, Sale sale, String article, long amount, int quantity) {
//
// sale.setArticle(article);
// sale.setAmount(amount);
// sale.setQuantity(quantity);
// entryPoint.insert(sale);
//
// } // End Class insertEvent
}// End Class CEPExample
这是我的规则文件:
//created on: Nov 28, 2014
package com.sample
import com.sample.Sale;
// Declarations
declare Sale
@role(event)
//article : String
//amount : long
//quantity : int
end
rule "StoreOne - Has Passed it's Sales Record"
when
Number( $totalSalesAmount : intValue, intValue > 1000 )
from accumulate ( Sale($amount : amount, $quantity : quantity)
from entry-point "StoreOne", sum( $amount*$quantity ))
then
System.out.println("StoreOne - Has passed its Sales Record!");
end
rule "StoreTwo - has Passed its Sales Record"
when
Number( $totalSalesAmount : intValue, intValue > 1000 )
from accumulate ( Sale($amount : amount, $quantity : quantity) from entry-point "StoreTwo", sum( $amount * $quantity ))
then
System.out.println("StoreTwo - Has passed its Sales Record!");
end
还有我的 kmodule.xml 文件:
<?xml version="1.0" encoding="UTF-8"?>
<kmodule xmlns="http://jboss.org/kie/6.0.0/kmodule">
<kbase name="rules" packages="rules">
<ksession name="ksession-rules"/>
</kbase>
</kmodule>
我确实认为这也是相关的 - 使用侦听器的咨询输出(用于更改的声明语句(列出属性):
Nov 29, 2014 4:31:10 PM org.drools.compiler.kie.builder.impl.ClasspathKieProject notifyKieModuleFound
INFO: Found kmodule: file:/C:/Users/versaggi/workspace-spring-framework/CEPProject/target/classes/META-INF/kmodule.xml
Nov 29, 2014 4:31:10 PM org.drools.compiler.kie.builder.impl.KieRepositoryImpl addKieModule
INFO: KieModule was added:FileKieModule[ ReleaseId=com.versaggi:CEPProject:0.0.1-SNAPSHOTfile=C:\Users\versaggi\workspace-spring-framework\CEPProject\target\classes]
Nov 29, 2014 4:31:14 PM com.sample.cep.CEPExample main
INFO:
*********************************>>>> Drools CEP Example
==>[ObjectInsertedEventImpl: getFactHandle()=[fact 0:1:24780333:24780333:1:StoreOne:NON_TRAIT:com.sample.cep.Sale@17a1e2d], getObject()=com.sample.cep.Sale@17a1e2d, getKnowledgeRuntime()=org.drools.core.impl.StatefulKnowledgeSessionImpl@527389, getPropagationContext()=PhreakPropagationContext [entryPoint=EntryPoint::StoreOne, factHandle=[fact 0:1:24780333:24780333:1:StoreOne:NON_TRAIT:com.sample.cep.Sale@17a1e2d], leftTuple=null, originOffset=-1, propagationNumber=2, rule=null, type=0]]
==>[ObjectInsertedEventImpl: getFactHandle()=[fact 0:2:20723018:20723018:2:StoreOne:NON_TRAIT:com.sample.cep.Sale@13c354a], getObject()=com.sample.cep.Sale@13c354a, getKnowledgeRuntime()=org.drools.core.impl.StatefulKnowledgeSessionImpl@527389, getPropagationContext()=PhreakPropagationContext [entryPoint=EntryPoint::StoreOne, factHandle=[fact 0:2:20723018:20723018:2:StoreOne:NON_TRAIT:com.sample.cep.Sale@13c354a], leftTuple=null, originOffset=-1, propagationNumber=3, rule=null, type=0]]
==>[ObjectInsertedEventImpl: getFactHandle()=[fact 0:3:16489063:16489063:3:StoreOne:NON_TRAIT:com.sample.cep.Sale@fb9a67], getObject()=com.sample.cep.Sale@fb9a67, getKnowledgeRuntime()=org.drools.core.impl.StatefulKnowledgeSessionImpl@527389, getPropagationContext()=PhreakPropagationContext [entryPoint=EntryPoint::StoreOne, factHandle=[fact 0:3:16489063:16489063:3:StoreOne:NON_TRAIT:com.sample.cep.Sale@fb9a67], leftTuple=null, originOffset=-1, propagationNumber=4, rule=null, type=0]]
==>[ObjectInsertedEventImpl: getFactHandle()=[fact 0:4:33509294:33509294:4:StoreOne:NON_TRAIT:com.sample.cep.Sale@1ff4fae], getObject()=com.sample.cep.Sale@1ff4fae, getKnowledgeRuntime()=org.drools.core.impl.StatefulKnowledgeSessionImpl@527389, getPropagationContext()=PhreakPropagationContext [entryPoint=EntryPoint::StoreOne, factHandle=[fact 0:4:33509294:33509294:4:StoreOne:NON_TRAIT:com.sample.cep.Sale@1ff4fae], leftTuple=null, originOffset=-1, propagationNumber=5, rule=null, type=0]]
==>[ObjectInsertedEventImpl: getFactHandle()=[fact 0:5:27946348:27946348:5:StoreOne:NON_TRAIT:com.sample.cep.Sale@1aa6d6c], getObject()=com.sample.cep.Sale@1aa6d6c, getKnowledgeRuntime()=org.drools.core.impl.StatefulKnowledgeSessionImpl@527389, getPropagationContext()=PhreakPropagationContext [entryPoint=EntryPoint::StoreOne, factHandle=[fact 0:5:27946348:27946348:5:StoreOne:NON_TRAIT:com.sample.cep.Sale@1aa6d6c], leftTuple=null, originOffset=-1, propagationNumber=6, rule=null, type=0]]
==>[ObjectInsertedEventImpl: getFactHandle()=[fact 0:6:26643062:26643062:6:StoreTwo:NON_TRAIT:com.sample.cep.Sale@1968a76], getObject()=com.sample.cep.Sale@1968a76, getKnowledgeRuntime()=org.drools.core.impl.StatefulKnowledgeSessionImpl@527389, getPropagationContext()=PhreakPropagationContext [entryPoint=EntryPoint::StoreTwo, factHandle=[fact 0:6:26643062:26643062:6:StoreTwo:NON_TRAIT:com.sample.cep.Sale@1968a76], leftTuple=null, originOffset=-1, propagationNumber=7, rule=null, type=0]]
==>[ObjectInsertedEventImpl: getFactHandle()=[fact 0:7:26870209:26870209:7:StoreTwo:NON_TRAIT:com.sample.cep.Sale@19a01c1], getObject()=com.sample.cep.Sale@19a01c1, getKnowledgeRuntime()=org.drools.core.impl.StatefulKnowledgeSessionImpl@527389, getPropagationContext()=PhreakPropagationContext [entryPoint=EntryPoint::StoreTwo, factHandle=[fact 0:7:26870209:26870209:7:StoreTwo:NON_TRAIT:com.sample.cep.Sale@19a01c1], leftTuple=null, originOffset=-1, propagationNumber=8, rule=null, type=0]]
==>[ObjectInsertedEventImpl: getFactHandle()=[fact 0:8:1161635:1161635:8:StoreTwo:NON_TRAIT:com.sample.cep.Sale@11b9a3], getObject()=com.sample.cep.Sale@11b9a3, getKnowledgeRuntime()=org.drools.core.impl.StatefulKnowledgeSessionImpl@527389, getPropagationContext()=PhreakPropagationContext [entryPoint=EntryPoint::StoreTwo, factHandle=[fact 0:8:1161635:1161635:8:StoreTwo:NON_TRAIT:com.sample.cep.Sale@11b9a3], leftTuple=null, originOffset=-1, propagationNumber=9, rule=null, type=0]]
==>[ObjectInsertedEventImpl: getFactHandle()=[fact 0:9:2257152:2257152:9:StoreTwo:NON_TRAIT:com.sample.cep.Sale@227100], getObject()=com.sample.cep.Sale@227100, getKnowledgeRuntime()=org.drools.core.impl.StatefulKnowledgeSessionImpl@527389, getPropagationContext()=PhreakPropagationContext [entryPoint=EntryPoint::StoreTwo, factHandle=[fact 0:9:2257152:2257152:9:StoreTwo:NON_TRAIT:com.sample.cep.Sale@227100], leftTuple=null, originOffset=-1, propagationNumber=10, rule=null, type=0]]
*** DONE ***
由于 V5 中的 CEP 示例很少在网络上流传,而在 V6 KIE 中则更少,我会很感激有关此错误的起源以及如何纠正它的任何想法。