[rules-users] How to determine which rules fired under what conditions, for testing

Stephen Masters stephen.masters at me.com
Wed Mar 27 05:24:39 EDT 2013


Good point Wolfgang - I linked to the wrong listener for tracking rule activations!

As Wolfgang alludes, I use object filters all the time for unit testing. It lets me create assertions about the objects that are in working memory. I find it very valuable when my rules are inserting new facts, rather than just modifying the fact I inserted.

btw - If your toString() method doesn't give you quite enough details about a fact, then the objectDetails(Object) method in the following will use reflection (via commons BeanUtils) to iterate through all the properties of an object and build a big string from them. Which is a good reason to ensure that all your classes have decent toString() methods.
https://github.com/gratiartis/sctrcd-fx-web/blob/master/src/main/java/uk/co/scattercode/drools/util/DroolsUtil.java

Steve


On 27 Mar 2013, at 04:49, Wolfgang Laun <wolfgang.laun at gmail.com> wrote:

> Stephen has pointed you to a WorkingMemoryEventListener, and next to
> it there is also a TrackingAgendaEventListener. Callback methods
> beforeActivationFired and afterActivationFired bracket the execution
> of a rule's consequence, which, in combination with the WM events,
> will show you what a rule changes in WM.
> 
> As to the facts participating in the firing of a rule: the Activation
> object of before/afterActivationFired lets you retrieve these Objects.
> (The list of FactHandle's may contain FactHandle objects that do not
> necessarily relate to Facts inserted into Working Memory.)
> 
> If your Fact objects provide good toString() methods, putting this all
> together is a fairly simple task. Make sure to pick up Stephen's ideas
> about filtering - this may even be useful for unit testing.
> 
> -W
> 
> 
> On 26/03/2013, Magnarelli, James <James_Magnarelli at intuit.com> wrote:
>> Just to clarify, for anyone else who might offer advice, my original
>> desire was to have some kind of information about what rule fired, and
>> what object it fired for.
>> 
>> 
>> -- James
>> 
>> 
>> 
>> 
>> 
>> 
>> On 3/26/13 1:15 PM, "Grant Rettke" <grettke at acm.org> wrote:
>> 
>>> You might think differently about unit testing, set up your tests so
>>> you *know* the data is there, and add or retract facts, and verify
>>> their presence as a matter of success of failure.
>>> 
>>> There are also more advanced approaches that others will surely share
>>> soon.
>>> 
>>> On Tue, Mar 26, 2013 at 3:11 PM, Magnarelli, James
>>> <James_Magnarelli at intuit.com> wrote:
>>>> Though all of those are worthy pursuits, I want to do it in the sense of
>>>> unit testing to make sure that I have defined the rule correctly.
>>>> 
>>>> Thanks for your rapid response, by the way. I really appreciate it.
>>>> 
>>>> -- James
>>>> 
>>>> 
>>>> 
>>>> 
>>>> 
>>>> 
>>>> On 3/26/13 1:08 PM, "Grant Rettke" <grettke at acm.org> wrote:
>>>> 
>>>>> Do you want to do that in the sense of unit testing to make sure you
>>>>> have defined the rule correctly?
>>>>> 
>>>>> Or, do you want to understand more about the dynamic behavior as your
>>>>> system executes for profiling? Or learning more about your model?
>>>>> 
>>>>> On Tue, Mar 26, 2013 at 3:05 PM, jmagnare <james_magnarelli at intuit.com>
>>>>> wrote:
>>>>>> I'm fairly new to Drools, but a fair amount of digging and searching
>>>>>> hasn't
>>>>>> brought me closer to an answer on this:
>>>>>> 
>>>>>> Is there a way for me to determine, at runtime, which rules were fired
>>>>>> and
>>>>>> with what inputs?
>>>>>> 
>>>>>> I am looking to use this my JUnit tests, to make sure that the rules I
>>>>>> want
>>>>>> to be fired are firing when I expect them to.
>>>>>> 
>>>>>> 
>>>>>> 
>>>>>> --
>>>>>> View this message in context:
>>>>>> http://drools.46999.n3.nabble.com/How-to-determine-which-rules-fired-un
>>>>>> de
>>>>>> r-what-conditions-for-testing-tp4023063.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
>>>>> 
>>>>> 
>>>>> 
>>>>> --
>>>>> Grant Rettke | ACM, AMA, COG, IEEE
>>>>> grettke at acm.org | http://www.wisdomandwonder.com/
>>>>> Wisdom begins in wonder.
>>>>> ((λ (x) (x x)) (λ (x) (x x)))
>>>>> 
>>>>> _______________________________________________
>>>>> rules-users mailing list
>>>>> rules-users at lists.jboss.org
>>>>> https://lists.jboss.org/mailman/listinfo/rules-users
>>>> 
>>>> 
>>>> _______________________________________________
>>>> rules-users mailing list
>>>> rules-users at lists.jboss.org
>>>> https://lists.jboss.org/mailman/listinfo/rules-users
>>> 
>>> 
>>> 
>>> --
>>> Grant Rettke | ACM, AMA, COG, IEEE
>>> grettke at acm.org | http://www.wisdomandwonder.com/
>>> Wisdom begins in wonder.
>>> ((λ (x) (x x)) (λ (x) (x x)))
>>> 
>>> _______________________________________________
>>> rules-users mailing list
>>> rules-users at lists.jboss.org
>>> https://lists.jboss.org/mailman/listinfo/rules-users
>> 
>> 
>> _______________________________________________
>> rules-users mailing list
>> rules-users at lists.jboss.org
>> https://lists.jboss.org/mailman/listinfo/rules-users
>> 
> 
> _______________________________________________
> rules-users mailing list
> rules-users at lists.jboss.org
> https://lists.jboss.org/mailman/listinfo/rules-users

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.jboss.org/pipermail/rules-users/attachments/20130327/68fa80e6/attachment.html 


More information about the rules-users mailing list