[rules-users] Why isn't my rule firing??

dunnlow dunnlow at yahoo.com
Wed Jan 18 14:47:47 EST 2012


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.



More information about the rules-users mailing list