Using statics and setting them in a condition would probably not be a great idea.
Also, its generally frowned on to set a value of anything in a consequence (of course, calling a method to do a calculation to use in a predicate is fine) at the moment. Perhaps a better more "rule" way of doing this is to have the calculation as a rule on its own - it can then assert the result of the calculation as its own fact into the working memory. Finally, the rules that depend on the result of this calculation will not be considered until the calculation is done (this is assuming you want the calculation result to be constant for the firing of the rules of course). Drools 3 also has a "binding" where you can bind a field on a fact and use it in the consequence etc.. (more info on this one day soon !). That would solve this problem partly (however, if you are trying to avoid repeating the calculation for the same fact in each rule, thats different). On 2/1/06, Ephemeris Lappis <[EMAIL PROTECTED]> wrote: > > I have few experiences with Drools, but I don't think that "application > data" and static variables are the same. > > Application data, if I'm not mistaken, are placed in the working memory by > the application client code. Thus, they are not a private state variable > of > the rule context, but a public exchange entity that rules can use out of > their declared parameters. As the name says, they are the "application" > responsibility, not the rule context one ! The working memory can be > easily > instantiated by the client code according to concurrent call, it seems > easy > to control threading issues... > > The static variable declared in a DRL function block doesn't seem to be > public, since there is no evident (for me) way to get it from outside the > rule context. What I don't know, is how Drools generates Java classes from > the DRL files, and how resulting objects are created and/or shared by > concurrent callers... > > Once again, some clarification about the better way to keep memory between > rules, without any necessary external declarations, such as "application > data", is welcome... > > -- > Ephemeris Lappis > > > > -----Message d'origine----- > > De: news [mailto:[EMAIL PROTECTED] De la part de Peter Van Weert > > Envoyé: mardi 31 janvier 2006 18:38 > > À: [email protected] > > Objet: Re: [drools-user] Drools: Sharing a value between the rule's > > "condition" and "consequence" ? > > > > I agree, this is a brittle and counter-intuitive technique. Also, > > looking at the original problem statement: > > > > > Please note I know about "application data", but i'd rather not use > > > it, because my sqrt is just a temporary value... > > > > Does storing the value in a static variable solves this? I don't know > > the exact semantics of Drools, but my guess is that application data is > > essentially the same as a static variable? > > > > Carriing over computed results from conditions/guards to the > > consequences/body/actions is a problem other rule engines also > > encounter, and I don't think it can be solved nicely (i.e. without > > resorting to global state) without a (small) language extension... > > > > Yours, > > Peter > > > > > > Smith, Philip wrote: > > > Rule firing is single threaded so that shouldn't be a problem. > However, > > > the same static variable could be written to by the evaluation of many > > > conditions and as such it could be overwritten before the consequence > is > > > actually fired. Your rules could be written such this could not happen > > > but this remains a brittle technique. > > > > > > Philip Smith > > > [EMAIL PROTECTED] > > > (650) 628-7941 (x87941) > > > > > > -----Original Message----- > > > From: sol myr [mailto:[EMAIL PROTECTED] > > > Sent: Tuesday, January 31, 2006 8:48 AM > > > To: [email protected] > > > Subject: Re: [drools-user] Drools: Sharing a value between the rule's > > > "condition" and "consequence" ? > > > > > > Thanks very much for this reply :) > > > However, I was wondering: will it work in a multi-threaded > environment > > > ? > > > I have a Servlet, where each HTTP request is handled by a separate > > > thread, as usual. > > > All those threads use the rule engine, simultaniously... if it's a > > > static variable, won't they override each other's data ? > > > > > > Thanks again > > > Sol > > > > > > > > > > > > Ricardo Rojas Aguirre <[EMAIL PROTECTED]> wrote: > > > Hi, I solved the same problem with a local variable defined in the > > > function > > > tag... > > > > > > To solve your proble try somthing like this... > > > > > > > > > public static double sqrtResult; > > > > > > > > > > > > > > > > > > < parameter identifier="num" > > > > < class > Integer < / class > > > > < / parameter > > > > > > > < java:condition > > > > (sqrtResult=Math.sqrt(num.intValue())) > 1000 > > > < / java:condition > > > > > > > < java:consequence > > > > System.out.println( sqrtResult ); > > > < / java:consequence > > > > > > > > > > > > > Cheers > > > Rik > > > > > > > > > ----- Original Message ----- > > > From: "sol myr" > > > To: > > > Sent: Tuesday, January 31, 2006 11:04 AM > > > Subject: [drools-user] Drools: Sharing a value between the rule's > > > "condition" and "consequence" ? > > > > > > > > > > > >>Hi, > > >>I'm new to Drools. Could anyone please help with the following: > > >> > > >>Can you share data between a rule's "condition" and "consequence" ? > > >>For example: > > >>- My "condition" calculates some square root (Math.sqrt) > > >>- The "consequence" needs to print out the same square root > > >> > > >>It's not efficient to call Math.sqrt twice... : > > >> > > >>< parameter identifier="num" > > > >>< class > Integer < / class > > > >>< / parameter > > > >>< java:condition > > > >>Math.sqrt(num.intValue()) > 1000 > > >>< / java:condition > > > >>< java:consequence > > > >>System.out.println( Math.sqrt(num.intValue()) > > >>< / java:consequence > > > >> > > >>Is there a way to store the result of "Math.sqrt" in some temporary > > >>variable ( so as to avoid the duplicate calculation ) ? > > >>Please note I know about "application data", but i'd rather not use > > > > > > it, > > > > > >>because my sqrt is just a temporary value... > > >> > > >>Thanks :) > > >> > > >> > > >>--------------------------------- > > >> > > >>What are the most popular cars? Find out at Yahoo! Autos > > > > > > > > > > > > > > > > > > > > > > > > --------------------------------- > > > Yahoo! Autos. Looking for a sweet ride? Get pricing, reviews, & more > on > > > new and used cars. > > > > > >
