[rules-users] finding matching entries in parallel arrays

Mark Proctor mproctor at codehaus.org
Tue Aug 7 15:51:30 EDT 2007


You could just call a function?
        $first : ArrayList( )
        $second : ArrayList( this != $first )
        Integer( $index : intValue ) from 
returnArrayOfMatchingIndexes($first, $second)

This still uses from, but the function call returns a collection, which 
from iterates over in turn.

Mark

Aaron Dixon wrote:
>
> I spoke too soon. The from will match items in the two Lists that are 
> not at the same index.
>
> As for asserting ArrayList( ) facts -- in my actual application I have 
> facts that have Collections attributes for which I need this behavior. 
> I just made the problem simpler for this post.
>
>
> On 8/7/07, *Aaron Dixon* <atdixon at gmail.com 
> <mailto:atdixon at gmail.com>> wrote:
>
>     The "from" conditional element solves part of my problem:
>
>     rule "Find matching entry in parallel arrays (alternate)"
>         when
>             $first : ArrayList( )
>             $second : ArrayList( this != $first )
>             $fi : Object() from $first
>             $si : Object() from $second
>             eval( $fi == $si )
>         then
>             System.out.println ( " Found match at index ? : " + $fi +
>     "!" );
>     end
>
>     I do want reference equality (==) in my eval. But I understand
>     that object equality would look just like:
>
>     eval( $fi .equals( $si ) )
>
>
>     Thanks!
>
>     Aaron
>
>
>
>     On 8/7/07, *Mike D* <mike.dalaker at kewill.com
>     <mailto:mike.dalaker at kewill.com>> wrote:
>
>
>         Mark,
>
>         Just to be sure I read this correctly:
>
>         eval( $fi == $si )
>
>         means it's checking objects, not values within the object?
>         If a compare of values is needed, another eval would have to
>         be written to
>         compare temp value.
>
>         Thanks,
>         Mike
>
>
>         Oh, and can we change the documenatation examples please?
>
>         Somthing like...
>
>         rule
>             when
>                 Team( $team : name == "ManU" )
>                 $person : Person( favouriteTeam == $team )
>             then
>                 System.out.println( $person.getName() + " likes
>         Manchester" );
>             end
>
>         rule
>             when
>                 Team( $team : name == "Chelsea" )
>                 $person : Person( favouriteTeam != $team )
>             then
>                 System.out.println( $person.getName() + " does not
>         like Chelsea at
>         all" );
>             end
>
>
>
>         Mark Proctor wrote:
>         >
>         > We don't really recommend you assert ArrayLists etc as facts,
>         as they
>         > have no contextual meaning
>         >
>         > $first : ArrayList( )
>         > $second : ArrayList( this != $first )
>         > $fi Object() from $first
>         > $si Object from $second
>         > eval( $fi == $si )
>         >
>         > 'from' allows iteration of lists, so you could potentially do
>         it this
>         > way, although it doesn't provide an index number for you....
>         You could
>         > possibly have a global that as part of a function in eval get
>         > incremented and that global is available in the consequence.
>         But you'll
>         > have to be very careful with concurrency.... Might be easier
>         to have a
>         > hashmap of indexes where the key is made up of $first and
>         $second. Btw
>         > == checks for same instances, not an equality check, is that
>         what you
>         > wanted?
>         >
>         > Mark
>         >
>         > Aaron Dixon wrote:
>         >> My problem boils down to finding matching entries in
>         parallel arrays.
>         >>
>         >> Here is a rule that succesfully does just that:
>         >>
>         >> rule "Find matching entry in parallel arrays"
>         >>     when
>         >>         $first : ArrayList( )
>         >>         $second : ArrayList( this != $first )
>         >>         $i : Integer( this < $first.size )
>         >>         eval( $first.get($i) == $second.get($i) )
>         >>     then
>         >>         System.out.println ( "Found match at index " + $i +
>         "!" );
>         >> end
>         >>
>         >>
>         >> To execute this rule, I must insert (assert) the two
>         ArrayList facts
>         >> as well as at least as many Integer facts as there are items
>         in the
>         >> ArrayLists.
>         >>
>         >> What I don't like about this rule is that
>         >>
>         >>     (1) I have to assert the Integers
>         >>     (2) I can't support arbitrarily-sized lists in my rules
>         (without
>         >> asserting that many Integer facts)
>         >>
>         >> So -- Is there a better way?
>         >>
>         >> What if a future version of Drools supported implicit Number
>         facts
>         >> that allowed for these kinds of indexing rules?
>         >>
>         >> Aaron
>         >>
>         >>
>         >>
>         >>
>         >>
>         ------------------------------------------------------------------------
>         >>
>         >> _______________________________________________
>         >> rules-users mailing list
>         >> rules-users at lists.jboss.org <mailto:rules-users at lists.jboss.org>
>         >> https://lists.jboss.org/mailman/listinfo/rules-users
>         <https://lists.jboss.org/mailman/listinfo/rules-users>
>         >>
>         >
>         >
>         > _______________________________________________
>         > rules-users mailing list
>         > rules-users at lists.jboss.org <mailto:rules-users at lists.jboss.org>
>         > https://lists.jboss.org/mailman/listinfo/rules-users
>         >
>         >
>
>         --
>         View this message in context:
>         http://www.nabble.com/finding-matching-entries-in-parallel-arrays-tf4225120.html#a12034578
>         <http://www.nabble.com/finding-matching-entries-in-parallel-arrays-tf4225120.html#a12034578>
>         Sent from the drools - user mailing list archive at Nabble.com
>         <http://Nabble.com>.
>
>         _______________________________________________
>         rules-users mailing list
>         rules-users at lists.jboss.org <mailto:rules-users at lists.jboss.org>
>         https://lists.jboss.org/mailman/listinfo/rules-users
>         <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/20070807/947165a4/attachment.html 


More information about the rules-users mailing list