Guest User

Untitled

a guest
Jul 22nd, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.90 KB | None | 0 0
  1. public class Basket {
  2. public enum Color { RED, GREEN };
  3.  
  4. private Color color;
  5. private List<FruitBox> fruitBoxes;
  6.  
  7. //Assume appropriate constructors, getters exist
  8. }
  9.  
  10. public class FruitBox {
  11. public enum Fruit { STRAWBERRY, GRAPE };
  12.  
  13. private Fruit fruit;
  14.  
  15. //Assume appropriate constructors, getters exist
  16. }
  17.  
  18. rule "Two or more of same fruit in any Basket"
  19. when
  20. //Bind the contents of each basket
  21. $Basket : Basket($FruitBoxes : fruitBoxes)
  22.  
  23. //Compute A list of the unique fruits in current basket contents
  24. $fruits : Object() from accumulate( FruitBox($f : fruit) from $FruitBoxes, collectSet($f) )
  25.  
  26. //Match and bind each unique fruit one by one
  27. $fruit : FruitBox.Fruit() from $fruits
  28.  
  29. //Find the number of times the unique fruit occurs in this Basket
  30. $count : Number(intValue >= 2) from accumulate(
  31. $f : FruitBox(fruit == $fruit) from $FruitBoxes,
  32. count($f) )
  33. then
  34. System.out.println($Basket + " has " + $count + " of the fruit " + $fruit);
  35. end
  36.  
  37. com.sample.DroolsTest$Basket@10cd6753 has 4 of the fruit STRAWBERRY
  38. com.sample.DroolsTest$Basket@10cd6753 has 3 of the fruit GRAPE
  39. com.sample.DroolsTest$Basket@2e060819 has 2 of the fruit GRAPE
  40.  
  41. rule "Totals by fruit and by basket color"
  42. when
  43. //Calculate the cross product of all fruits and colors
  44. $fruit : FruitBox.Fruit()
  45. $color : Basket.Color()
  46.  
  47. //Count the number of FruitBoxes of the chosen fruit in Baskets of the chosen color
  48. $count : Number() from accumulate(
  49. $fb : FruitBox(fruit == $fruit)
  50. and exists Basket(color == $color, fruitBoxes contains $fb),
  51. count($fb) )
  52. then
  53. System.out.println($count + " " + $fruit + " in " + $color + " baskets");
  54. end
  55.  
  56. 5 STRAWBERRY in GREEN baskets
  57. 6 STRAWBERRY in RED baskets
  58. 6 GRAPE in GREEN baskets
  59. 4 GRAPE in RED baskets
Add Comment
Please, Sign In to add comment