[rules-users] execute particular rules programmatically and dynamically

Michael Anstis michael.anstis at gmail.com
Thu Apr 14 13:56:49 EDT 2011


I believe Wolfgang's (and my) fear is that you are re-inventing the wheel.

Writing your own AgendaFilter is in essence achieving the same function as
having an additional Fact in your rule that prevents the rule's activation
appearing on the agenda in the first place.

Using an AgendaFilter will allow you to prevent a rule that has been matched
and placed on the agenda from executing.

Not that it won't work; it's just that if smells bad.

Just trying to be helpful :)

Mike


On 14 April 2011 18:51, Benson Fung <benson.redhat at gmail.com> wrote:

> Hi Bob,
>
> Let me try to implement the AgendaFIlter :
>
> import java.util.List;
>
> import org.drools.runtime.rule.Activation;
> import org.drools.runtime.rule.AgendaFilter;
>
> public class RuleNameContainsAgendaFilter implements AgendaFilter {
>
>
>        private List<String> rules;
>
>        public RuleNameContainsAgendaFilter(List<String> rules) {
>                this.rules = rules;
>        }
>
>        public boolean accept(Activation activation) {
>                // TODO Auto-generated method stub
>                String activationRuleName = activation.getRule().getName();
>                if(rules.contains(activationRuleName)) {
>                        return true;
>                } else {
>                        return false;
>                }
>        }
>
> }
>
> I think the above the AgendaFilter can resolve the particular rule
> execution value, i.e.  Just pass all the specific rule into a List
> object and create the above AgendaFilter, then those particular rule
> can be executed.
>
>
> Please comment.
>
>
> Benson
>
> On Thu, Apr 14, 2011 at 5:38 PM, Robert Christenson <rac at akc.org> wrote:
> > We've come across something similar in our project just recently as well.
> >
> > We have a requirement to have some rules activate based on a tab-off
> action but also at a higher level (such as validating an entire event entry
> in the GUI). I also need these rules to activate if initiated from an
> external webservice (no GUI at all).
> >
> > What we've decided to utilize is a meta identifier in certain rules
> called FieldsAffected which may contain a delimited list of field
> identifiers.
> >
> > An AgendaFilter implementation is created for GUI side requests and
> passed to fireAllRules. The filter implementation compares any/all field
> identifiers passed within the request and keeps only those activations which
> contain the field identifier in it's metadata field.
> >
> > Our RHS creates a validation msg which contains the field identifiers so
> that the calling GUI can display the proper msgs to the field.
> >
> > This allows us to support multiple call scenarios without duplicating the
> logic in multiple rules just based on context info.
> >
> > Hope this helps,
> >
> > Bob Christenson
> >
> >
> >> ------------------------------
> >>
> >> Message: 2
> >> Date: Thu, 14 Apr 2011 15:23:44 +0800
> >> From: Benson Fung <benson.redhat at gmail.com>
> >> Subject: Re: [rules-users] execute particular rules programmatically
> >>    and    dynamically
> >> To: Rules Users List <rules-users at lists.jboss.org>
> >> Message-ID: <BANLkTi=OGO=rWFEnYBHM144GVSt+P_KVtQ at mail.gmail.com>
> >> Content-Type: text/plain; charset=ISO-8859-1
> >>
> >> Thanks Michael, let me think about your solution seriously afterwards.
> >>  Actually, I am using GWT.
> >> I would like to say that sometimes customer's requirement is picky and
> >> unexpectable.  They really want to have close coupling between UI and
> >> Rules.  haha.....  :)    Do you think it is helpless as a
> >> consultant???   :~(
> >>
> >> If anyone has another idea of this scenario, you are welcome to post
> >> your idea out there.
> >>
> >>
> >> Thank you very much
> >>
> >>
> >>
> >> 2011/4/14 Michael Anstis <michael.anstis at gmail.com>:
> >>> In my example Rule 1 was shared between "screen1.panel1.editbox1" and
> >>> "screen1.panel1.editbox2":-
> >>>
> >>> rule "Rule1"
> >>> ? salience 1
> >>> ? dialect "mvel"
> >>> ??? when
> >>> ????? ApplicationContext( context in ("screen1.panel1.editbox1",
> >>> "screen1.panel1.editbox2") )
> >>> ? ? ? ad : ApplicationData( age == "" || ( ?== null ))
> >>> ? ? then
> >>> ? ??? ad.setReturnMsg( "\n age should not be null or empty" );
> >>> end
> >>>
> >>> This is an approach and may not be the best available; I was trying to
> >>> demonstrate how your problem can be solved without having to worry
> about
> >>> explicitly executing individual rules.For example, depending on what UI
> >>> technology you are using (Swing, JSF) you could subclass the UI
> components
> >>> and use these as facts - but such close coupling between UI and Rules
> may be
> >>> undesirable.
> >>>
> >>> With kind regards,
> >>>
> >>> Mike
> >>>
> >>> On 14 April 2011 03:52, Benson Fung <benson.redhat at gmail.com> wrote:
> >>>>
> >>>> Good, Michael.
> >>>>
> >>>> 'context' is used to distinguish which part of the UI which will be
> >>>> validated, right? ?The customer will ask if they have 10000 rules in
> >>>> the rulebase. ?And some of them are redundant, so they want to make
> >>>> some of the rules share with several part of UI, e.g. ?editbox 6,
> >>>> editbox 7 and editbox 8 these 3 boxes' value range is within 0 and
> >>>> 600. ?Therefore, Rule3 can be shared for these 3 editbox validation,
> >>>> right? ?However, for the context variable approach, it seems Rule3
> >>>> cannot be shared for another editbox with same value range validation.
> >>>> ?So these could be a key for the BRMS/Drools.
> >>>>
> >>>>
> >>>> Benson
> >>>>
> >>>> 2011/4/14 Michael Anstis <michael.anstis at gmail.com>:
> >>>>> Sure, whenever you copy values from your UI to your model for
> validation
> >>>>> you
> >>>>> also enter a fact representing the "context" of the values.
> >>>>>
> >>>>> Using your example you have two edit boxes on one screen and your
> rule
> >>>>> simply checks for the value of a single edit box; in this case the
> >>>>> context
> >>>>> differentiates the two.
> >>>>>
> >>>>> Walking your example:
> >>>>>
> >>>>> (1) When editbox 1 looses focus you copy the value from the dropdown
> and
> >>>>> edit box 1 into your model, plus you enter a "context" fact stating
> that
> >>>>> these values relate to that part of the UI (say
> >>>>> "screen1.panel1.editbox1").
> >>>>> You then insert these facts into WM and call fireAllRules. The rules
> >>>>> check
> >>>>> the "context" and only execute if the values are for the (rule)
> >>>>> applicable
> >>>>> context. (2) Editbox 2 works in a similar way, but has a different
> >>>>> context
> >>>>> ((say "screen1.panel1.editbox2").
> >>>>>
> >>>>> Have a look at Plugtree - I believe they're quite well customed to
> >>>>> writing
> >>>>> UIs backed with rules; salaboy or esteban (or IRC #drools) might be
> able
> >>>>> to
> >>>>> offer more practical advice.
> >>>>>
> >>>>> With kind regards,
> >>>>>
> >>>>> Mike
> >>>>>
> >>>>>
> >>>>>
> >>>>> On 13 April 2011 17:48, Benson Fung <benson.redhat at gmail.com> wrote:
> >>>>>>
> >>>>>> Hi Michael,
> >>>>>>
> >>>>>> Can you elaborate more for the uses of the ApplicationContext? ?I
> >>>>>> can't follow its uses.
> >>>>>>
> >>>>>>
> >>>>>> Thanks
> >>>>>> Benson
> >>>>>>
> >>>>>> 2011/4/14 Michael Anstis <michael.anstis at gmail.com>:
> >>>>>>> Here's a quick (and probably sub-optimal way) ;)
> >>>>>>>
> >>>>>>> When you copy values from the UI to Facts for validation you also
> >>>>>>> include
> >>>>>>> the context of the validation.
> >>>>>>>
> >>>>>>> I've also removed the inline evals you were using.
> >>>>>>>
> >>>>>>> rule "Rule1"
> >>>>>>> ? salience 1
> >>>>>>> ? dialect "mvel"
> >>>>>>> ??? when
> >>>>>>> ????? ApplicationContext( context == "*" )
> >>>>>>> ? ? ? ad : ApplicationData( age == "" || ( ?== null ))
> >>>>>>> ? ? then
> >>>>>>> ? ??? ad.setReturnMsg( "\n age should not be null or empty" );
> >>>>>>> end
> >>>>>>>
> >>>>>>> rule "Rule2"
> >>>>>>> ? dialect "mvel"
> >>>>>>> ??? when
> >>>>>>> ? ??? ApplicationContext( context == "screen1.panel1.ed" )
> >>>>>>> ? ? ? ad : ApplicationData( $age : age != null , age != "" , age <
> 0
> >>>>>>> ||
> >>>>>>>>
> >>>>>>> 100, minIssrdAge == "Years" )
> >>>>>>> ? ? then
> >>>>>>> ? ??? ad.setReturnMsg( "\nage is out of the range(i.e. ?< 0 and >
> >>>>>>> 100)"
> >>>>>>> );
> >>>>>>> end
> >>>>>>>
> >>>>>>> rule "Rule3"
> >>>>>>> ?? dialect "mvel"
> >>>>>>> ? ?? when
> >>>>>>> ? ? ?? ad : ApplicationData( $age : age != null , age != "" , age
> <0
> >>>>>>> ||
> >>>>>>>>
> >>>>>>> 600, minIssrdAge == "Years" )
> >>>>>>> ? ?? then
> >>>>>>> ? ? ?? ad.setReturnMsg( "\nage is out of the range(i.e. ?< 0 and >
> >>>>>>> 600)"
> >>>>>>> );
> >>>>>>> end
> >>>>>>>
> >>>>>>> On 13 April 2011 17:19, Benson Fung <benson.redhat at gmail.com>
> wrote:
> >>>>>>>>
> >>>>>>>> Hi,
> >>>>>>>>
> >>>>>>>> Here is the scenario :
> >>>>>>>>
> >>>>>>>> If there are 2 edit boxes and 2 dropdown list at the frontend
> like.
> >>>>>>>>
> >>>>>>>>
> >>>>>>>> dropdown(minIssrdAge1) ? editbox(age1)
> >>>>>>>> dropdown(minIssrdAge2) ? editbox(age2)
> >>>>>>>>
> >>>>>>>> everytime when I lost focus the editbox(age1 or age2), ?the
> >>>>>>>> editbox(age1 or age2) value will be validated against the
> following
> >>>>>>>> rules.
> >>>>>>>> i.e. ?minIssrdAge1 and age1 will be validated together if lost
> focus
> >>>>>>>> the editbox age1.
> >>>>>>>> ? ? ? minIssrdAge2 and age2 will be validated together if lost
> focus
> >>>>>>>> the editbox age2
> >>>>>>>>
> >>>>>>>> Rule1 is mandatory because both editbox are required field.
> >>>>>>>> However, editbox(age1) is only valid within the 0 and 100. ?and
> >>>>>>>> editbox(age2) is only valid within 0 and 600.
> >>>>>>>>
> >>>>>>>> In other words, editbox(age1) have to be validated against Rule1 +
> >>>>>>>> Rule2. ?However, editbox(age2) have to validated against Rule1 +
> >>>>>>>> Rule3.
> >>>>>>>>
> >>>>>>>> My question, how to design the rule attribute or at the java
> program
> >>>>>>>> side so that different editbox can be validated against different
> >>>>>>>> rule.
> >>>>>>>>
> >>>>>>>> Please help. ?I can't find any solution by now.
> >>>>>>>>
> >>>>>>>> rule "Rule1"
> >>>>>>>> ? ? ? ?salience 1
> >>>>>>>> ? ? ? ?dialect "mvel"
> >>>>>>>> ? ? ? ?when
> >>>>>>>> ? ? ? ? ? ? ? ?ad : ApplicationData( age == "" || ( ?== null ))
> >>>>>>>> ? ? ? ?then
> >>>>>>>> ? ? ? ? ? ? ? ?ad.setReturnMsg( "\n age should not be null or
> empty"
> >>>>>>>> );
> >>>>>>>> end
> >>>>>>>>
> >>>>>>>>
> >>>>>>>> rule "Rule2"
> >>>>>>>> ? ? ? ?dialect "mvel"
> >>>>>>>> ? ? ? ?when
> >>>>>>>> ? ? ? ? ? ? ? ?ad : ApplicationData( $age : age != null , age !=
> ""
> >>>>>>>> ,
> >>>>>>>> minIssrdAge
> >>>>>>>> == "Years" )
> >>>>>>>> ? ? ? ? ? ? ? ?eval(Integer.parseInt($age) < 0) or
> >>>>>>>> eval(Integer.parseInt($age) > 100)
> >>>>>>>> ? ? ? ?then
> >>>>>>>> ? ? ? ? ? ? ? ?ad.setReturnMsg( "\nage is out of the range(i.e. ?<
> 0
> >>>>>>>> and >
> >>>>>>>> 100)" );
> >>>>>>>> end
> >>>>>>>>
> >>>>>>>> rule "Rule3"
> >>>>>>>> ? ? ? ?dialect "mvel"
> >>>>>>>> ? ? ? ?when
> >>>>>>>> ? ? ? ? ? ? ? ?ad : ApplicationData( $age : age != null , age !=
> ""
> >>>>>>>> ,
> >>>>>>>> minIssrdAge
> >>>>>>>> == "Years" )
> >>>>>>>> ? ? ? ? ? ? ? ?eval(Integer.parseInt($age) < 0) or
> >>>>>>>> eval(Integer.parseInt($age) > 600)
> >>>>>>>> ? ? ? ?then
> >>>>>>>> ? ? ? ? ? ? ? ?ad.setReturnMsg( "\nage is out of the range(i.e. ?<
> 0
> >>>>>>>> and >
> >>>>>>>> 600)" );
> >>>>>>>> end
> >>>>>>>> _______________________________________________
> >>>>>>>> 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
> >>>>>
> >>>>>
> >>>>> _______________________________________________
> >>>>> 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
> >>>
> >>>
> >>
> >
> > _______________________________________________
> > 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/20110414/4351f6de/attachment.html 


More information about the rules-users mailing list