[rules-users] Caching results in temperory variables
Manav
manav7574 at yahoo.com
Fri Aug 13 12:36:33 EDT 2010
Your explanation is absolutely superb. I modified my drl as per your
suggestions and it's just been completely transformed. Just one question
regarding one suggestion below :
-- is this something like a for each loop in java . This is the first example i
have seen in drl
-- will this automatically iterate over as many languages as have been inserted
?
-- the inserts .. don't they have to be within a rule block ? or did you just
omit other things to keep it simple
insert( new Language( "Japanese" ) );
insert( new Language( "French" ) );
// more of the same
rule "PolyglottScore"
salience 100
when
Language( $name : name )
User ($id : id, language.keySet contains $name", $f_score : language[$name])
$urslt : UserResult (id == $id, langScore.keySet not contains $name )
then
modify( $usrlt ){
setLangScore($name, $_score)
}
end
________________________________
From: Wolfgang Laun <wolfgang.laun at gmail.com>
To: Manav <manav7574 at yahoo.com>; Rules Users List <rules-users at lists.jboss.org>
Sent: Fri, August 13, 2010 1:37:45 PM
Subject: Re: [rules-users] Caching results in temperory variables
See inline annotations.
On 13 August 2010 04:34, Manav <manav7574 at yahoo.com> wrote:
Thanks for your response Greg. As you said this is what i tried to fix the issue
>by creating a UserResult obj that has score as a property and inserting the
same
>in working memory. I update the results in subsequent rules in the "then"
>clause. There are some issues that i faced in this as well :-
>
>-- In my last rule if i want to do a check on score property of my result
>object in the "when" clause the score has not been updated .
>
>-- however if i do the same check in the "then" clause it seems to work
>properly.
>
>
>-- I tried to overcome by doing an update in one of my rules to check if that
>would work. That however went into an infinite loop. So i used the
>"lock-on-active true" property . However it seemed to work well for only the
>first user object and doesn't work for other user objects that are loaded
later.
>
>
>Below is the sample rule file . If the scenario is not clear pls do let me know
>and i will send across the different drls as attachments .
>
>
>
>salience 200
>when
> User ($id : id)
>then
> UserResult urslt = new UserResult ($id);
> insert ($id)
>
Here I'd expect
insert( urslt );
Also, to be on the safe side, I always add a pattern
not( UserResult( id == $id ) )
to avoid the creation of multiple UserResult's for the same User (if there's a
minimal chance it might be modified during the following fusillade).
end
>
>
rule "JapaneseScore"
salience 100
>
lock-on-active truewhen
> $usr : User (language.keySet contains "Japanese", $j_score :
>language["Japanese"])
>
I prefer to add a binding of User's id: User( $id == id )
$urslt : UserResult (id == $usr.id)
>
so I can simplify this. Add a constraint to avoid looping:
$urslt : USerResult( id == $id, langScore.keySet not contains "Japanese"
)
then
> $usrlt.setLangScore("Japanese", $j_score);
>
update( $usrlt );
end
>
rule "FrenchScore"
salience 50
>
Why not 100?
lock-on-active true
not necessary now
when
> User (language.keySet contains "French", $f_score : language["French"])
> $urslt : UserResult (id == $usr.id)
>
Same as before:
$urslt : UserResult (id == $id, langScore.keySet not contains "French" )
then
> $usrlt.setLangScore("French", $_score);
> update ($usrlt);
>end
>
>salience 25
>when
> UserResult (score > 4) // does not work
>then
> // if ($usr_rslt.getScore() > 4) --> works well
> // update the db
>end
>
>Regards,
>Manav
>
The repetition of rules JapanesScore and FrenchScore could be avoided by
inserting static objects
insert( new Language( "Japanese" ) );
insert( new Language( "French" ) );
// more of the same
rule "PolyglottScore"
salience 100
when
Language( $name : name )
User ($id : id, language.keySet contains $name", $f_score : language[$name])
$urslt : UserResult (id == $id, langScore.keySet not contains $name )
then
modify( $usrlt ){
setLangScore($name, $_score)
}
end
HTH
--W
>
>----- Original Message ----
>From: Greg Barton <greg_barton at yahoo.com>
>To: Manav <manav7574 at yahoo.com>; Rules Users List <rules-users at lists.jboss.org>
>Sent: Thu, August 12, 2010 9:29:28 PM
>Subject: Re: [rules-users] Caching results in temperory variables
>
>You must either update an existing working memory object or insert a new
working
>memory object with the results. That's how rules communicate with each other.
>
>GreG
>
>On Aug 12, 2010, at 10:31 AM, Manav <manav7574 at yahoo.com> wrote:
>
>Hi,
>
>I am using the version 5.x of drools and i have a scenario where i want to hold
>the result of
>
>a test in one rule to a temperory variable . This result will then be used in
>another rule that will be fired
>
>later in the sequence .
>
>For ex :
>
>salience 100
>when
> User (language.keySet contains "Japanese", $j_score : language["Japanese"])
>then
> // do something
>end
>
>salience 50
>when
> User (language.keySet contains "French", $f_score : language["French"])
>then
> // do something
>end
>
>salience 25
>when
> eval (($j_score + $f_score) > 4)
>then
> // update the same to db
>end
>
>Is this possible in Drools 5.x ?
>
>Regards,
>Manav
>
>
>
>
>
>_______________________________________________
>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/20100813/d4a346b5/attachment.html
More information about the rules-users
mailing list