The rule below assumes that markets is List<Market> in Town and ids is
List<Integer> in a Market.
It does not rely on Market or even Location objects being facts, and
there's no problem if a location id is re-used in another town.
rule findThree
when
$t: Town( $name: name, $markets: markets )
$s: HashSet( $size: size ==3 )
from accumulate( $m: Market() from $markets,
init( HashSet set = new HashSet(); )
action( set.addAll( $m.getIds() ); )
result( set ) )
then
System.out.println( $name + " is a three-location town " + $s );
end
2011/11/14 Michael Anstis <michael.anstis(a)gmail.com>
Something like this?
when
$t : Town( )
$s : SuperMarket( town == $t, $lid : locationId )
$ls : HashSet( size == 3) from ( Location( locationId = $lid ) )
I suspect Location would have to override equals and hashCode correctly.
Once you have some DRL that works I'd suggest moving to Guvnor.
On 12 November 2011 23:15, LCode <j_whittlesea(a)hotmail.com> wrote:
> I am trying to write a rule via the guided rule editor and I am really
> struggling.
>
> The following is a simplified explanation of my scenario:
>
> I have a model where there are lots of 'Town' objects, each town has any
> number of 'Supermarket' objects and each supermarket has a list of
> integers
> 'locationID'.
>
> Something like this....
>
> <Town>
> <Supermarket name = 'asda'>
> <LocationID>15</LocationID>
> <LocationID>18</LocationID>
> </Supermarket>
> <Supermarket name = 'tesco'>
> <LocationID>13</LocationID>
> </Supermarket>
> <Supermarket name = 'lidl'>
> <LocationID>13</LocationID>
> <LocationID>15</LocationID>
> <LocationID>18</LocationID>
> </Supermarket>
> </Town>
>
> (apologies to the Sainsburys shoppers!!)
>
> I need to write a rule that fires when there are precisely 3 unique
> location
> ID's in the whole town.
>
> So in the example above the complete list of locationIDs would be 13, 13,
> 15, 15, 18, 18
> With the following unique numbers 13, 15, 18
> therefore the rule would fire as there are 3 unique numbers.
>
> I hope that makes some sense.
>
> I understand that I should collect the locationIDs into a java.util.Set in
> order to count unique entries and I can collect together all supermarkets
> into an arraylist. However I cannot for the life of me seem to collect
> LocationID across all supermarkets.
>
> Thank you so much in anticipation, I realise this was rather long-winded!
>
> --
> View this message in context:
>
http://drools.46999.n3.nabble.com/Collecting-properties-in-a-rule-tp35035...
> Sent from the Drools: User forum mailing list archive at
Nabble.com.
> _______________________________________________
> rules-users mailing list
> rules-users(a)lists.jboss.org
>
https://lists.jboss.org/mailman/listinfo/rules-users
>
_______________________________________________
rules-users mailing list
rules-users(a)lists.jboss.org
https://lists.jboss.org/mailman/listinfo/rules-users