I'm experiencing an odd error while trying to convert a a CEP example from Drools 5X to 6X: - Drool 6.1.0.Final to be precise.
The source of my inspiration for this little project can be found at the following link:=> PlugTree.
The error I get indicated that Drools is unable to create a Field Extractor - an error one gets when they forget to create setters/getters in their Domain POJO's.
SEVERE: Unable to build KieBaseModel:rules Unable to create Field Extractor for 'amount'Field/method 'amount' not found for class 'com.sample.Sale' : [Rule name='StoreOne - Has Passed it's Sales Record'] java.lang.RuntimeException: Field/method 'amount' not found for class 'com.sample.Sale'
I've traced the problem to a 'declare' statement in the Rules File (I'll list the full listing further down):
declare Sale
@role(event)
end
Using just this causes the error (which does NOT happen in V5 btw), however using the next "altered" declaration statement does not cause an error. It just does nothing ...
declare Sale
@role(event)
article : String
amount : long
quantity : int
end
What it does do - is nothing. It compiles, runs but the facts just don't get inserted (or recognized).
Here is my Java Test Harness:
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
And here is my Rules File:
//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
And my kmodule.xml file:
<?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>
I do think this is also relevant - the Consult output using a listener (for the altered declaration statement (listing the attributes):
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 ***
Since there are very few examples of CEP in V5 floating around the web, and even less in V6 KIE, I'd appreciate any thoughts on the origins of this error and how to correct it.
A
declarestatement of the DRL language can be used for two rather different purposes:@role) to a POJOThe distinction is derived from the presence of one or more fields in the
declarestatement.When using the second form, Java code cannot create objects of the declared class in the usual way. You'll have to use the roundabout technique of locating the class from the package using (KieBase.getFactType()), create an instance (FactType.newInstance()) and add attribute values using the generic setters (FactType.set(), setFromMap()).
It's quite possible that something goes wrong if you have a POJO Sale and a declared class Sale at the same time. I haven't tried a build using kmodule.xml (I never do), but calling the KieBuilder from Java and checking for errors produces (Sale fields not commented):
BTW, this example isn't using any event processing feature and the issue isn't related to cEP at all.
Later
The only difference with the insertion of a "standard fact" and an "event fact" is that the Engine may have to add the timestamp field (unless your meta data says it's already there as an attribute).
Technically, for the way you code an insert, it depends on the way the fact type is defined: either by a Java class (with the "event" flavour being added by the declare according to item 1) or by a full-blown declare, as described previously. There is no further difference w.r.t. insertion between plain fact and event fact.
Also, there's no difference in accessing (to retrieve attributes for pattern evaluation), except that events have the additional timestamp attribute (unless mapped to an explicit attribute) which is implicitly retrieved when temporal operators are applied.
There are differences in processing, most notably temporal operators being applicable to events themselves (as opposed to attributes). Also, events may be eligible for automatic retraction, and they are available for selection in windows.