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
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@lists.jboss.org
>> https://lists.jboss.org/mailman/listinfo/rules-users
>>
>
>
> _______________________________________________
> rules-users mailing list
> rules-users@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
Sent from the drools - user mailing list archive at Nabble.com.
_______________________________________________
rules-users mailing list
rules-users@lists.jboss.org
https://lists.jboss.org/mailman/listinfo/rules-users