[rules-users] Why isn't my rule firing??
Greg Barton
greg_barton at yahoo.com
Wed Jan 18 16:40:25 EST 2012
This is by design. Unless you modify an object and inform the inference engine of the change, rule conditions will not be re-evaluated. If you want the rule to re-fire you must tell the engine to reconsider the old NotificationEvents via update() or modify(). A rule like this would do it every time a new MyBean is detected:
rule "TouchAllNotificationEvents"
dialect "mvel"
when
MyBean()
$ne : NotificationEvent()
then
update($ne);
end
--- On Wed, 1/18/12, dunnlow <dunnlow at yahoo.com> wrote:
> From: dunnlow <dunnlow at yahoo.com>
> Subject: [rules-users] Why isn't my rule firing??
> To: rules-users at lists.jboss.org
> Date: Wednesday, January 18, 2012, 1:47 PM
> First, my intent:
>
> 1) insert a pojo into a stateful session
> 2) based upon that pojo's attributes, create and
> insert a new fact
> (different type)
> 3) retract the initial pojo, but keep the newly
> created fact in memory for
> some period of time
>
> After some helpful insight from the forum, I thought I
> could knock this out.
>
> However, I'm stuck and I've boiled down to what I imagine
> is a
> misunderstanding that I have. I have the code and
> rules below.
>
> With each fact that I insert, I expected
> 1) to see the number of facts grow (the list of
> facts printed line 16 does
> grow with each call). This works
> 2) to see the NotificationEvent added only once to
> working memory (the
> facts printed shows only one NotificationEvent and then a
> new MyBean for
> each one I insert). So, I think this works also.
> 3) BUT, each time I call fireAllRules(), I expect
> the "foundNE" rule to be
> triggered and to see "Found A notification event"
> printed. However, this is
> only printed once - after my first insert. Also, I see that
> 0 rules get
> fired (line 13) after the first MyBean insert.
>
> SO, if I see a NotificationEvent in the list of facts in
> line 17, why isn't
> my foundNE rule printing out the message each time I insert
> a new MyBean and
> call fireallrules??
>
> FYI, my future plan is to have a rule with low salience
> that retracts the
> MyBeans just inserted and have my foundNE rule retract any
> NotificationEvents that are at least 10 mins old. (so
> that the
> NotificationEvents will grow but not MyBeans) .
>
> Here is my test code (please excuse typos - there are none
> in my code):
>
> Thanks very much for any insight.
> -J
>
> ===test.java ===============
>
> 1 KnowledgeBase kbase = readKnowledgeBaseFromFile();
> 2 StatefulKnowledegeSession ksession =
> kbase.newStatefulKnowledgeSession():
> 3 KnowledgeRuntimeLogger logger =
> KnowledgeRuntimeLoggerFactory.newFileLogger(ksession,"testlog");
>
> 4 for (int x=0;x<10;x++){
> 5 myBean mb = new MyBean();
> 6 mb.setName("me");
> 7 mb.setHeadline("my_head");
> 8 mb.setTag("tagline");
> 9
> 10 List<Command> cmds = new
> ArrayList<Command>();
> 11 FactHandle fh = (FactHandle)
> ksession.insert(seb);
> 12 int rf = ksession.fireAllRules();
> 13 System.out.println(rf+"rules fired");
> 14
> 15 Collection<FactHandle> c =
> ksession.getFactHandles();
> 16 for (FactHandle f:c) {
> 17
> System.out.println(" now: "+f.toString());
> 18 }
> 19
> 20 Thread.sleep(5000);
> 21 }
>
> === test.drl ================
> import com.me.MyBean
> import java.util.Calendar
>
> declare NotificationEvent
> name: String
> headline: String
> tagline: String
> dts: Long
> end
>
> rule "foundNE"
> dialect "mvel"
> when
> $ne :
> NotificationEvent()
> then
>
> System.out.println("Found a Notification Event");
> end
>
> rule "NotTemplate_1"
> dialect "mvel"
> when
> $myb : MyBean(name
> matches "me", headline matches "my_head")
> not ($ne :
> NotificationEvent(name matches $myb.name,
> headline=$myb.headline)
> then
> NotificationEvent
> fact0 = new NotificationEvent();
>
> fact0.setName($myb.name);
>
> fact0.setHeadline($myb.headline);
> insert(fact0);
>
> System.out.println("Adding a notification event");
> end
>
> --
> View this message in context: http://drools.46999.n3.nabble.com/Why-isn-t-my-rule-firing-tp3670261p3670261.html
> Sent from the Drools: User forum mailing list archive at
> Nabble.com.
> _______________________________________________
> rules-users mailing list
> rules-users at lists.jboss.org
> https://lists.jboss.org/mailman/listinfo/rules-users
>
More information about the rules-users
mailing list