Hi,
I'm using sliding windows (hence stream mode).
Here's my rule:
declare Sensor
@role (event)
end
rule 'r'
when
Number( dv:doubleValue > 1 ) from accumulate(
Sensor( $v : value ) over window:length( 2 ),
average( $v ) )
then
System.out.println(dv);
end
Here's my class:
++++++++++++++++++++++++++++++++++++++++++++++
public class CEPTest {
public static void main(String[] args) throws Exception {
KnowledgeBuilder kbuilder =
KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(ResourceFactory.newClassPathResource("cep.drl"),
ResourceType.DRL);
if (kbuilder.getErrors().size() > 0) {
throw new IllegalArgumentException("Could not parse knowledge.");
}
KnowledgeBaseConfiguration kbaseConfig =
KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
kbaseConfig.setOption(EventProcessingOption.STREAM);
KnowledgeBase kbase =
KnowledgeBaseFactory.newKnowledgeBase(kbaseConfig);
kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());
KnowledgeBase kbase2 = (KnowledgeBase)
DroolsStreamUtils.streamIn(DroolsStreamUtils.streamOut(kbase), null);
final StatefulKnowledgeSession ksession =
kbase2.newStatefulKnowledgeSession();
System.out.println("T1");
ksession.insert(new Sensor(1.0));
ksession.fireAllRules();
System.out.println("T2");
ksession.insert(new Sensor(2.0));
ksession.fireAllRules();
System.out.println("T3");
ksession.insert(new Sensor(3.0));
ksession.fireAllRules();
ksession.dispose();
}
}
++++++++++++++++++++++++++++++++++++++++++++++
The output is (as expected):
+++++++++++++++
T1
T2
1.5
T3
2.5
+++++++++++++++
I'm wondering why I have to use DroolsStreamUtils to get a knowledge
base? When I use kbase (which didn't went through DroolsStreamUtils)
instead of kbase2 to create a new session, the output will be:
+++++++++++++++
T1
T2
2.0
T3
3.0
+++++++++++++++
Thanks,
Jarek