[rules-users] match longest matching Long in WM

Wolfgang Laun wolfgang.laun at gmail.com
Thu Jul 21 13:04:08 EDT 2011


This is a very nice problem!

I assume that you have those MyBean facts with one String attribute code
inserted and that one String fact is being kicked in. Then the following
rule will find the longest match.

rule matchLongest
when
    $string: String()
    $myBean: MyBean() from accumulate(
       $mb: MyBean( $code: code, eval( $string.startsWith( $mb.getCode() ) )
),
        init( MyBean bestBean = null; int bestLength = 0; )
        action( if( $code.length() > bestLength ){
                    bestBean = $mb;
                    bestLength = $code.length();
                } )
        result( bestBean ) )
then
    System.out.println( "best fit " + $string + " is " + $myBean.getCode()
);
    retract( $string );
end

Cheers
-W



On 21 July 2011 13:59, esstrata <esstrata at yahoo.es> wrote:

> Hi,
>
>  I hope you can help me out here with a way to implement this rule:
>
> 1) I have String WM
> 2) I have beans in WM with a property called 'code'
> public class MyBean {
>  private String code;
>  ...
>
> 3) I want a rule of the type "Process MyBean with the longest-matching,
> not-null code"
>
> Let explain myself a little further with an example.
>
> I have the following MyBean objects in WM with the given code properties:
> MyBean [code = 44]
> MyBean [code = 443]
> MyBean [code = 4432]
> MyBean [code = 36]
> MyBean [code = 7]
>
> What I want to get is a rule where the following samples would assign to a
> variable the given results shown:
> 4411111 gives MyBean [code = 44]
> 4433333 gives MyBean [code = 443]
> 22222 gives null
>
> I think the rule could start like:
> rule "Process MyBean with the longest-matching, not-null code"
>        dialect "mvel"
>        when
>                $s : String()
>                $m : MyBean($s matches /the longest not-null possible
> MyBean.code/)
>        then
>  ......
>
> Thank you
>
> --
> View this message in context:
> http://drools.46999.n3.nabble.com/match-longest-matching-Long-in-WM-tp3188343p3188343.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
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.jboss.org/pipermail/rules-users/attachments/20110721/9a775ff6/attachment.html 


More information about the rules-users mailing list