[rules-users] In drools what is the exact difference between Insert and Update ??

Wolfgang Laun wolfgang.laun at gmail.com
Tue Apr 3 02:27:34 EDT 2012


Here's how I'd do it. Note:
   * Don't look at the Customer until you really need to.
   * One salience level for the additional "background" rule.
   * Don't use halt() - fireAllRules() should do the trick.
   * Don't modify Account facts all over the place - this avoids loops
and, therefore, salience or lock-on-active.
   * Use auxiliary facts to avoid updates of primary facts.
   * Always add a test != null when checking a field that might be null.

class AccountException {
  Account account;
  int code;
}

rule "madatory-rule-2"
when
       $account: Account( account_no == null )
then
       insert( new AccountException( $account, 102 ) );
end

rule "madatory-rule-3"
when
       $account: Account( account_no != null && account_no.trim == "" )
then
       insert( new AccountException( $account, 103 ) );
end

And then there is:

rule "collect-account-exceptions"
salience -1000
when
       AccountException( $account: account )
       $elist: List() from collect( AccountException( account == $account ) )
       $customer: Customer( account == $account )
then
       modify( $customer ){
           setExceptions( $elist ),
           setCustomerValidationSuccess( false )
       }
       for( Object objec: $elist ) retract( $object );
end

-W


On 02/04/2012, Bhushan <bhushansonaje at gmail.com> wrote:
> Thanks *Davide and laune*  for your all suggestions.Davide u r right all
> those are best practices  suggested by client :) .Davide can we discuss
> below example .how to rewrite this example using best practices (which is
> mention by u and laune in your earlier posts)
>
>
> Consider Bank Class which contains Account details
>
> Acnt_holder_name
> account_no
> branch_name
>
>
> we have some PreDefined exceptions in DB with error code
>
> 101-"account holder name can not be blank"
> 102-"account_no can not be null"
> 103-"account_no can not be blank"
> 104-"Wrong branch"
>
>
>
> Rule "Base-Rule"
> Salience 200
> lock-on-active true
> when
>        $Customer : Bank($account:account)
> then
>  //Do nothing this is base rule for defining customer
> end
>
>
> rule "madatory-rule-1" extends "Base-Rule"
> Salience 190
> lock-on-active true
> when
>        Account(Acnt_holder_name.trim =="")  from $Customer
> then
>        $Customer.AddNewException(101)
>        update($Customer);
> end
>
>
> rule "madatory-rule-2" extends "Base-Rule"
> Salience 180
> lock-on-active true
> when
>        Account(account_no ==null)  from $Customer
> then
>        $Customer.AddNewException(102)
>        update($Customer);
> end
>
>
> rule "madatory-rule-3"  extends "Base-Rule"
> Salience 170
> lock-on-active true
> when
>        Account(account_no.trim=="")  from $Customer
> then
>        $Customer.AddNewException(103)
>        update($Customer);
>
> end
>
> rule "madatory-rule-4" extends "Base-Rule"
> Salience 160
> lock-on-active true
> when
>        Account(branch_name=="mumbai")  from $Customer
> then
>        $Customer.AddNewException(103)
>        update($Customer);
>
> end
>
>
> rule "madatory-rule-5"
> Salience 150
> lock-on-active true
> when
>        $Customer : Bank(exceptionList.size>0)
> then
>        $Customer.setCustomerValidationSuccess(false)
>        drools.halt();
>
> end
>
>
> Here I want to avoid salience and update (Like this I am also using lots of
> setter methods in my drools project.Now I  am trying to use all these setter
> methods without using UPDDATE.But in some cases rules are dependent on other
> rules so I dnt have any other choice left .In such cases I am using *update*
> keyword so other rules can use updated object fom working memory).
>
>
>
> --
> View this message in context:
> http://drools.46999.n3.nabble.com/In-drools-what-is-the-exact-difference-between-Insert-and-Update-tp3865471p3878911.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
>



More information about the rules-users mailing list