[rules-users] finding matching entries in parallel arrays
Mark Proctor
mproctor at codehaus.org
Tue Aug 7 08:03:52 EDT 2007
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
> 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/097d7e44/attachment.html
More information about the rules-users
mailing list