Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Basket {
- public enum Color { RED, GREEN };
- private Color color;
- private List<FruitBox> fruitBoxes;
- //Assume appropriate constructors, getters exist
- }
- public class FruitBox {
- public enum Fruit { STRAWBERRY, GRAPE };
- private Fruit fruit;
- //Assume appropriate constructors, getters exist
- }
- rule "Two or more of same fruit in any Basket"
- when
- //Bind the contents of each basket
- $Basket : Basket($FruitBoxes : fruitBoxes)
- //Compute A list of the unique fruits in current basket contents
- $fruits : Object() from accumulate( FruitBox($f : fruit) from $FruitBoxes, collectSet($f) )
- //Match and bind each unique fruit one by one
- $fruit : FruitBox.Fruit() from $fruits
- //Find the number of times the unique fruit occurs in this Basket
- $count : Number(intValue >= 2) from accumulate(
- $f : FruitBox(fruit == $fruit) from $FruitBoxes,
- count($f) )
- then
- System.out.println($Basket + " has " + $count + " of the fruit " + $fruit);
- end
- com.sample.DroolsTest$Basket@10cd6753 has 4 of the fruit STRAWBERRY
- com.sample.DroolsTest$Basket@10cd6753 has 3 of the fruit GRAPE
- com.sample.DroolsTest$Basket@2e060819 has 2 of the fruit GRAPE
- rule "Totals by fruit and by basket color"
- when
- //Calculate the cross product of all fruits and colors
- $fruit : FruitBox.Fruit()
- $color : Basket.Color()
- //Count the number of FruitBoxes of the chosen fruit in Baskets of the chosen color
- $count : Number() from accumulate(
- $fb : FruitBox(fruit == $fruit)
- and exists Basket(color == $color, fruitBoxes contains $fb),
- count($fb) )
- then
- System.out.println($count + " " + $fruit + " in " + $color + " baskets");
- end
- 5 STRAWBERRY in GREEN baskets
- 6 STRAWBERRY in RED baskets
- 6 GRAPE in GREEN baskets
- 4 GRAPE in RED baskets
Add Comment
Please, Sign In to add comment