Rule evaluation in Stream Mode (CEP)

238 views
Skip to first unread message

ianEve

unread,
Jul 19, 2016, 9:12:16 AM7/19/16
to Drools Usage
Hello Drools Community,

I'm just trying my first steps with Complex Event Processing (CEP) with Drools Fusion. As a first step I want to implement "stream" as event processing option. For a fist experience, I implemented the rule taken form example 9.4 of Drools 6.4 documentation:

rule "Sound the alarm"
when
    $h
: Heartbeat( ) from entry-point "MonitoringStream"
   
not( Heartbeat( this != $h, this after[0s,10s] $h ) from entry-point "MonitoringStream" )
then
   
System.out.println("Sound the alarm");
end

Within a plain java main-method, I call the fireAllRules-method and wait for 12 seconds. Because of not inserting any Heartbeat-object, after 10 seconds there should be the output "sound the alarm".  But running this small application, leads to nothing: there's no output, no exception, ... Can anybody tell me what I am doing wrong?

This is my implementation of my main-method

public static void main(String[] arguments) {

       
KieServices ks = KieServices.Factory.get();
       
KieBaseConfiguration config = ks.newKieBaseConfiguration();
        config
.setOption(EventProcessingOption.STREAM);

       
KieContainer kContainer = ks.getKieClasspathContainer();
       
KieBase kBase = kContainer.getKieBase("rules");

       
KieSession kSession = kBase.newKieSession();

        kSession
.fireAllRules();
       
long startTime = System.currentTimeMillis();                
       
try {
           
Thread.sleep(12000);
       
} catch (InterruptedException e) {
            e
.printStackTrace();
       
}
       
long endTime = System.currentTimeMillis();

       
System.out.println("Thread slept for: " + (endTime - startTime) / 1000 + " seconds");
   
}



Regards, ian

Mauricio Salatino

unread,
Jul 19, 2016, 9:16:08 AM7/19/16
to drools...@googlegroups.com
Hi yes,
Your rule is probably activated, but you need to call fireAllRules() again.. or use fireUntilHalt :)


--
You received this message because you are subscribed to the Google Groups "Drools Usage" group.
To unsubscribe from this group and stop receiving emails from it, send an email to drools-usage...@googlegroups.com.
To post to this group, send email to drools...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/drools-usage/66d236c3-9682-40f2-b36c-ef1d184cf166%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
 - MyJourney @ http://salaboy.com
 - Co-Founder @ http://www.jugargentina.org
 - Co-Founder @ http://www.jbug.com.ar
 
 - Salatino "Salaboy" Mauricio -

ianEve

unread,
Jul 19, 2016, 9:33:36 AM7/19/16
to Drools Usage
Hi, thanks for your answer - but yout advice didn't work either :(

Mauricio Salatino

unread,
Jul 19, 2016, 9:39:14 AM7/19/16
to drools...@googlegroups.com
Your rule is not being activated because you didn't insert any Heartbeat Fact 



For more options, visit https://groups.google.com/d/optout.

ianEve

unread,
Jul 19, 2016, 10:02:57 AM7/19/16
to Drools Usage
Of course Id did not insert any heartbeat-Fact... because the rule should be executed if no heartbeat fact is inserted

Michael Anstis

unread,
Jul 19, 2016, 10:06:21 AM7/19/16
to Drools Usage
Your rule says $h: Heartbeat( ) from entry-point "MonitoringStream"

Which means you need a Heartbeat for the rule to activate.

Salaboy Mauricio Salatino

unread,
Jul 19, 2016, 10:07:20 AM7/19/16
to drools...@googlegroups.com
That's not what you rule is stating. 
You rule says: 
If there is a heartbeat and
Not a heartbeat after 10 secs

So there must be at least one heartbeat

Sent from my iPhone

ianEve

unread,
Jul 19, 2016, 10:12:11 AM7/19/16
to Drools Usage
You're all right - Sorry!

But adding a Headtbeat - still nothing happens :(

Salaboy Mauricio Salatino

unread,
Jul 19, 2016, 10:13:22 AM7/19/16
to drools...@googlegroups.com
Share your class and rules again

Sent from my iPhone

ianEve

unread,
Jul 19, 2016, 10:18:26 AM7/19/16
to Drools Usage
Rule:


rule "Sound the alarm"
when
    $h
: Heartbeat( ) from entry-point "MonitoringStream"
   
not( Heartbeat( this != $h, this after[0s,10s] $h ) from entry-point "MonitoringStream" )
then
   
System.out.println("Sound the alarm");
end


Java-Main:
public static void main(String[] arguments) {

       
KieServices ks = KieServices.Factory.get();
       
KieBaseConfiguration config = ks.newKieBaseConfiguration();
        config
.setOption(EventProcessingOption.STREAM);

       
KieContainer kContainer = ks.getKieClasspathContainer();
       
KieBase kBase = kContainer.getKieBase("rules");

       
KieSession kSession = kBase.newKieSession();

        kSession
.insert(new Heartbeat());
        kSession
.fireUntilHalt();

Mauricio Salatino

unread,
Jul 19, 2016, 10:23:28 AM7/19/16
to drools...@googlegroups.com
Your rule is expecting the hearbeat in a specific Entry-point
you need to do something like 
kSession.getEntryPoint("MonitoringStream").insert(new Heartbeat());

for that to work


For more options, visit https://groups.google.com/d/optout.

ianEve

unread,
Jul 19, 2016, 10:39:51 AM7/19/16
to Drools Usage
Thx,

now something happens: but the result is not the desired result. I get the following exception:

Exception in thread "main" java.lang.ClassCastException: org.drools.core.common.DefaultFactHandle cannot be cast to org.drools.core.common.EventFactHandle

Any idea how to solve?

Mauricio Salatino

unread,
Jul 19, 2016, 10:58:54 AM7/19/16
to drools...@googlegroups.com
Can you share the whole stacktrace? 
Usually when you deal with events (in this case Heartbeat) you need to mark the class as event by using the declare + type keywords.
Check in the drools doc on how to do that for your Heartbeat class. 



For more options, visit https://groups.google.com/d/optout.

ianEve

unread,
Jul 19, 2016, 12:12:27 PM7/19/16
to Drools Usage
This is my Stacktrace:


Exception in thread "main" java.lang.ClassCastException: org.drools.core.common.DefaultFactHandle cannot be cast to org.drools.core.common.EventFactHandle
    at org.drools.core.rule.VariableRestriction$LeftEndRightStartContextEntry.getTimestampFromTuple(VariableRestriction.java:589)
    at org.drools.core.rule.VariableRestriction$TimestampedContextEntry.updateFromTuple(VariableRestriction.java:512)
    at org.drools.core.common.DoubleBetaConstraints.updateFromTuple(DoubleBetaConstraints.java:80)
    at org.drools.core.phreak.PhreakNotNode.doLeftInserts(PhreakNotNode.java:109)
    at org.drools.core.phreak.PhreakNotNode.doNode(PhreakNotNode.java:86)
    at org.drools.core.phreak.RuleNetworkEvaluator.switchOnDoBetaNode(RuleNetworkEvaluator.java:524)
    at org.drools.core.phreak.RuleNetworkEvaluator.evalBetaNode(RuleNetworkEvaluator.java:505)
    at org.drools.core.phreak.RuleNetworkEvaluator.evalNode(RuleNetworkEvaluator.java:341)
    at org.drools.core.phreak.RuleNetworkEvaluator.innerEval(RuleNetworkEvaluator.java:301)
    at org.drools.core.phreak.RuleNetworkEvaluator.outerEval(RuleNetworkEvaluator.java:136)
    at org.drools.core.phreak.RuleNetworkEvaluator.evaluateNetwork(RuleNetworkEvaluator.java:94)
    at org.drools.core.phreak.RuleExecutor.reEvaluateNetwork(RuleExecutor.java:194)
    at org.drools.core.phreak.RuleExecutor.evaluateNetworkAndFire(RuleExecutor.java:73)
    at org.drools.core.common.DefaultAgenda.fireNextItem(DefaultAgenda.java:1007)
    at org.drools.core.common.DefaultAgenda.fireLoop(DefaultAgenda.java:1350)
    at org.drools.core.common.DefaultAgenda.fireUntilHalt(DefaultAgenda.java:1269)
    at org.drools.core.impl.StatefulKnowledgeSessionImpl.fireUntilHalt(StatefulKnowledgeSessionImpl.java:1345)
    at org.drools.core.impl.StatefulKnowledgeSessionImpl.fireUntilHalt(StatefulKnowledgeSessionImpl.java:1324)
    at de.myProject.cep.run.CepHeartbeatExample.main(CepHeartbeatExample.java:25)

Mauricio Salatino

unread,
Jul 19, 2016, 12:14:25 PM7/19/16
to drools...@googlegroups.com
Did you tried marking Hearbeat as event? 



For more options, visit https://groups.google.com/d/optout.

Mauricio Salatino

unread,
Jul 19, 2016, 12:15:43 PM7/19/16
to drools...@googlegroups.com

ianEve

unread,
Jul 19, 2016, 12:30:23 PM7/19/16
to Drools Usage
Not yet... Gonna continue the next days...

thx a lot for your help :)

Salaboy Mauricio Salatino

unread,
Jul 19, 2016, 3:07:26 PM7/19/16
to drools...@googlegroups.com
Check also that you are creating the config but you are not using it as argument when you create the kiebase

Sent from my iPhone

ianEve

unread,
Jul 22, 2016, 6:36:43 AM7/22/16
to Drools Usage
All right, everything works now :)

Thanks a lot, Salaboy
Reply all
Reply to author
Forward
0 new messages