[rules-users] RE: Duplicate check using dialect(firing multiple times)

Edson Tirelli tirelli at post.com
Thu Nov 15 10:38:25 EST 2007


    Basha,

    This is the correct behavior since both facts match both patterns (one
at a time). If you don't want them to fire 2 times, you need a way to tell
the engine there is an order on the facts. I.e., (A,B) is a valid tuple for
you, but (B,A) is not.
    One way of doing that is if the facts have some ID attribute that can be
ordered:

        $f1 : Message( $id1 : id, $m1 : message )
        $f2 : Message( id > $id1, $m2 : message == $m1, eval( $f1 != $f2 ) )

    If your facts have no attribute that allow a user to establish an order
between them, then the only way is to use java system id to do that:

        $f1 : Message( $m1 : message )
        $f2 : Message( $m2 : message == $m1, eval( System.identityHashCode(
$f1 ) > System.identityHashCode( $f2 ) ) )

    []s
    Edson

2007/11/15, Sikkandar Nawabjan <Sikkandar.Nawabjan at ust-global.com>:
>
> Edson,
>
> With this code it is firing. But it is firing 2 times(u also got same 2
> time output). why is that? how we can avoid?
>
> Thanks and Regss,
>
> Basha
>
>
>
>
>
>
>
>    Sorry, my mistake. "this" is a reserved word in java (duh!!), and so
> you need to use your own binding:
>
> m : Message( $message1 : message )
> mdup : Message($message2:message==$message1,eval(mdup != m) )
>
>    You can't use mvel dialect, because in MVEL the operators "==" and "!="
> will call the equals() method anyway.
>    Other than that, it is working just fine for me:
>
> rule "Find duplicates"
>         salience 10
>     when
>         $f1 : Message( $m1 : message )
>         $f2 : Message( $m2 : message == $m1, eval( $f1 != $f2 ) )
>     then
>         System.out.println( "FIRED DUPLICATE:" );
>         System.out.println( "      $f1 = "+$f1+" [ message="+$m1+" ]"  );
>         System.out.println( "      $f2 = "+$f2+" [ message="+$m2+" ]"  );
> end
>
> rule "Find differents"
>     when
>         $f1 : Message( $m1 : message )
>         $f2 : Message( $m2 : message != $m1 )
>     then
>         System.out.println( "FIRED DIFFERENT:" );
>         System.out.println( "      $f1 = "+$f1+" [ message="+$m1+" ]" );
>         System.out.println( "      $f2 = "+$f2+" [ message="+$m2+" ]"  );
> end
>
>     The code to insert facts is:
>
>             Message message1 = new Message();
>             message1.setMessage(  "Hello World" );
>             workingMemory.insert( message1 );
>             Message message2 = new Message();
>             message2.setMessage(  "Hello World" );
>             workingMemory.insert( message2 );
>             Message message3 = new Message();
>             message3.setMessage(  "Hello Bob" );
>             workingMemory.insert( message3 );
>             workingMemory.fireAllRules();
>
>     And the result is:
>
> FIRED DUPLICATE:
>       $f1 = com.sample.DroolsTest$Message at 1b06041 [ message=Hello World ]
>       $f2 = com.sample.DroolsTest$Message at 1a001ff [ message=Hello World ]
> FIRED DUPLICATE:
>       $f1 = com.sample.DroolsTest$Message at 1a001ff [ message=Hello World ]
>       $f2 = com.sample.DroolsTest$Message at 1b06041 [ message=Hello World ]
> FIRED DIFFERENT:
>       $f1 = com.sample.DroolsTest$Message at 1c6572b [ message=Hello Bob ]
>       $f2 = com.sample.DroolsTest$Message at 1b06041 [ message=Hello World ]
> FIRED DIFFERENT:
>       $f1 = com.sample.DroolsTest$Message at 1b06041 [ message=Hello World ]
>       $f2 = com.sample.DroolsTest$Message at 1c6572b [ message=Hello Bob ]
> FIRED DIFFERENT:
>       $f1 = com.sample.DroolsTest$Message at 1c6572b [ message=Hello Bob ]
>       $f2 = com.sample.DroolsTest$Message at 1a001ff [ message=Hello World ]
> FIRED DIFFERENT:
>       $f1 = com.sample.DroolsTest$Message at 1a001ff [ message=Hello World ]
>       $f2 = com.sample.DroolsTest$Message at 1c6572b [ message=Hello Bob ]
>
>     []s
>     Edson
>
>
>
> 2007/11/14, Sikkandar Nawabjan <Sikkandar.Nawabjan at ust-global.com>:
>
>         i just use this sort of rule
>
>         rule "Hello World"
>
>         dialect "mvel"
>
>         when
>
>         m : Message( $message1 : message )
>
>         mdup : Message($message2:message==$message1,eval(this!=m) )
>
>         then
>
>         System.out.println("Rule Fired1111"+m +"::"+mdup );
>
>         System.out.println ("Rule Fired"+$message1 +"::"+$message2 );
>
>         end
>
>
>
>         if i put rule parameter dialect "MVEL"  the error "this should be
> used in static context" is gone. But now the rule is firing whatever may be
> the data
>
>         i assert 2 objects with message Hello and Hello11111.
>
>         still the rule is firing(2 times).
>
>         Thanks and regs,
>
>         basha
>
>
>
>
>         Message: 1
>         Date: Wed, 14 Nov 2007 09:48:02 -0200
>         From: "Edson Tirelli" < tirelli at post.com>
>         Subject: Re: [rules-users] RE: how to find duplicate inlineeval
>         To: "Rules Users List" <rules-users at lists.jboss.org >
>         Message-ID:
>                 <
> e6dd5ba30711140348p6da77e64kdac90cd313b5aa80 at mail.gmail.com>
>         Content-Type: text/plain; charset="iso-8859-1"
>
>            Sorry, you lost me. What is the error message?
>            Can you send us a self contained test showing the problem you
> are having?
>
>            []s
>            Edson
>
>         2007/11/14, Sikkandar Nawabjan < Sikkandar.Nawabjan at ust-global.com<mailto:
> Sikkandar.Nawabjan at ust-global.com> >:
>         >
>         > Edson,
>         >
>         > As you said i used inline eval. But am getting erroe message
> like this
>         > can't be used in static context.am <http://context.am/> using
> statelesssession to assert my
>         > objects.
>         >
>         > i also put eval(this!=obj1) as follows.
>         >
>         >   $obj1:Object1($id:id,$name:name)
>         >   $obj2:Object1(id==$id,$name:name==$name, eval( this!=$obj1
>         > ))  eval($obj2!=  $obj1)
>         >
>         > Though my references are different the rule is not firing
>         >
>         >
>         > Thanks and Regs
>         >
>         > Basha
>         >
>         > From: rules-users-bounces at lists.jboss.org on behalf of Edson
> Tirelli
>         > Sent: Tue 11/13/2007 9:35 PM
>         > To: Rules Users List
>         > Subject: Re: [rules-users] RE: how to find duplicate
>         >
>         >
>         >
>         >
>         >    Sikkandar,
>         >
>         >    The only way to check for identity is to use an inline eval
> and use
>         > java code to check that:
>         >
>         > when
>         >         $obj1:Object1($id:id,$name:name)
>         >         $obj2:Object1(id==$id,$name:name==$name, eval(
> this!=$obj1 ))
>         > then
>         >
>         >    Another option is to configure your rulebase to not allow the
> same
>         > object to be matched by more than one pattern in your rules. To
> do that you
>         > can either set a system property:
>         >
>         > drools.removeIdentities = true
>         >
>         >     Or you can use drools API:
>         >
>         > RuleBaseConfiguration conf = new RuleBaseConfiguration();
>         > conf.setRemoveIdentities( true );
>         > RuleBase rulebase = RuleBaseFactory.newRuleBase( conf );
>         >
>         >     If you do that, your rule can be written as this:
>         >
>         > when
>         >         $obj1:Object1($id:id,$name:name)
>         >         $obj2:Object1(id==$id,$name:name==$name)
>         > then
>         >
>         >    Since the engine will never allow the same fact to
> simultaneously match
>         > both patterns.
>         >
>         >    []s
>         >    Edson
>
>
>
>         _______________________________________________
>         rules-users mailing list
>         rules-users at lists.jboss.org
>         https://lists.jboss.org/mailman/listinfo/rules-users
>
>
>
>
>
>
>
> --
>   Edson Tirelli
>   Software Engineer - JBoss Rules Core Developer
>   Office: +55 11 3529-6000
>   Mobile: +55 11 9287-5646
>   JBoss, a division of Red
>
>
> _______________________________________________
> rules-users mailing list
> rules-users at lists.jboss.org
> https://lists.jboss.org/mailman/listinfo/rules-users
>
>
>


-- 
  Edson Tirelli
  Software Engineer - JBoss Rules Core Developer
  Office: +55 11 3529-6000
  Mobile: +55 11 9287-5646
  JBoss, a division of Red Hat @ www.jboss.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.jboss.org/pipermail/rules-users/attachments/20071115/0a3a614f/attachment.html 


More information about the rules-users mailing list