2

I need my WorkingMemory to be aware of facts which are created in the DRL as follows:

rule "Your First Rule"
when
    $testRule : TestRule(count >= 100)
then 
    System.out.println("100 PACKETS");
end

Is there a way of getting the WorkingMemory to become aware of the TestRule fact and then be able to be updated? My objective is to get the rule to know when 100 packets have been received.

cwrwatson
  • 61
  • 1
  • 6

1 Answers1

2

usually I create something as part of a rule and use it as a flag. If you don't need to create anything, then you can create a flag, for example

rule "Your First Rule"
when
    $testRule : TestRule(count >= 100)
    not         Flag(id="packets")
then 
    System.out.println("100 PACKETS");
    insert( new Flag("packets") )
    update( $testRule )
end

I don't think this code is correct, but I think you can get the idea... And you'll need to create a class called Flag. The update is needed to tell drools that is has to update the rete tree.

Augusto
  • 28,839
  • 5
  • 58
  • 88
  • 1
    This doesn't make the WorkingMemory know about any facts and I would need to insert the 'Flag' class too which still requires me to somehow make the DRL insert facts – cwrwatson Feb 15 '11 at 16:30
  • I don't understand your point, insert() tells the WM about the fact. And you don't need to insert the Flag class into the working memory from java, you just need to import it in the DRL, and the rule will insert the fact. – Augusto Feb 15 '11 at 16:50
  • Then perhaps I have some bad code somewhere. At the moment my WorkingMemory isn't picking up any facts from the DRL... – cwrwatson Feb 15 '11 at 16:56
  • I added an extra bit (the `update( $testrule)`) that might help. It's very odd that you can insert a fact from the rules. In the app I'm working we insert only one object from java, and then we have about 60 rules that explode that object into lots of facts. – Augusto Feb 15 '11 at 17:09
  • I believe it is something to do with where I create the Packages, RuleBase and WorkingMemory – cwrwatson Feb 15 '11 at 17:26