| 1 | /* |
| 2 | @license.text@ |
| 3 | */ |
| 4 | package com.hammurapi.eventbus.tests.familyties.rules; |
| 5 | |
| 6 | import com.hammurapi.eventbus.Handler; |
| 7 | import com.hammurapi.eventbus.tests.familyties.FamilyTiesEventDispatchContext; |
| 8 | import com.hammurapi.eventbus.tests.familyties.model.Aunt; |
| 9 | import com.hammurapi.eventbus.tests.familyties.model.Brother; |
| 10 | import com.hammurapi.eventbus.tests.familyties.model.Child; |
| 11 | import com.hammurapi.eventbus.tests.familyties.model.Cousin; |
| 12 | import com.hammurapi.eventbus.tests.familyties.model.Daughter; |
| 13 | import com.hammurapi.eventbus.tests.familyties.model.Husband; |
| 14 | import com.hammurapi.eventbus.tests.familyties.model.Nephew; |
| 15 | import com.hammurapi.eventbus.tests.familyties.model.Niece; |
| 16 | import com.hammurapi.eventbus.tests.familyties.model.Parent; |
| 17 | import com.hammurapi.eventbus.tests.familyties.model.Sibling; |
| 18 | import com.hammurapi.eventbus.tests.familyties.model.Sister; |
| 19 | import com.hammurapi.eventbus.tests.familyties.model.Son; |
| 20 | import com.hammurapi.eventbus.tests.familyties.model.Uncle; |
| 21 | import com.hammurapi.eventbus.tests.familyties.model.Wife; |
| 22 | |
| 23 | /** |
| 24 | * Infers aunt, uncle, cousin, niece, nephew |
| 25 | * @author Pavel Vlasov |
| 26 | * @revision $Revision$ |
| 27 | */ |
| 28 | public class SecondaryRules extends FamilyTiesRules { |
| 29 | |
| 30 | /** |
| 31 | * Infers cousin as a child or parent's sibling. |
| 32 | */ |
| 33 | @Handler(posts=Cousin.class) |
| 34 | public void infer(FamilyTiesEventDispatchContext dispatchContext, Child child1, Child child2, Sibling sibling) { |
| 35 | if (child1.getObject().equals(sibling.getSubject()) && child2.getObject().equals(sibling.getObject())) { |
| 36 | dispatchContext.post(new Cousin(child1.getSubject(), child2.getSubject())); |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * If A is a cousin of B then B is a cousin of A |
| 42 | * @param cousin |
| 43 | * @return |
| 44 | */ |
| 45 | @Handler |
| 46 | public Cousin infer(Cousin cousin) { |
| 47 | return new Cousin(cousin.getObject(), cousin.getSubject()); |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Another way to infer cousin to demonstrate mutiple derivations |
| 52 | * @param aunt |
| 53 | * @param child |
| 54 | */ |
| 55 | @Handler("aunt.getSubject().equals(parent.getSubject())") |
| 56 | // @Handler("args[0].getSubject().equals(args[1].getSubject())") |
| 57 | public Cousin infer(Aunt aunt, Parent parent) { |
| 58 | return new Cousin(aunt.getObject(), parent.getObject()); |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Another way to infer cousin to demonstrate mutiple derivations |
| 63 | * @param uncle |
| 64 | * @param child |
| 65 | */ |
| 66 | @Handler |
| 67 | public Cousin infer(Uncle uncle, Parent parent) { |
| 68 | if (uncle.getSubject().equals(parent.getSubject())) { |
| 69 | return new Cousin(uncle.getObject(), parent.getObject()); |
| 70 | } |
| 71 | return null; |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Sister of parent is aunt. |
| 76 | * @param sister |
| 77 | * @param parent |
| 78 | */ |
| 79 | @Handler |
| 80 | public Aunt infer(Sister sister, Parent parent) { |
| 81 | return sister.getObject().equals(parent.getSubject()) ? new Aunt(sister.getSubject(), parent.getObject()) : null; |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Wife of uncle is aunt. |
| 86 | * @param sister |
| 87 | * @param parent |
| 88 | */ |
| 89 | @Handler |
| 90 | public Aunt infer(Wife wife, Uncle uncle) { |
| 91 | return wife.getObject().equals(uncle.getSubject()) ? new Aunt(wife.getSubject(), uncle.getObject()) : null; |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Brother of parent is uncle. |
| 96 | * @param sister |
| 97 | * @param parent |
| 98 | */ |
| 99 | @Handler |
| 100 | public Uncle infer(Brother brother, Parent parent) { |
| 101 | return brother.getObject().equals(parent.getSubject()) ? new Uncle(brother.getSubject(), parent.getObject()) : null; |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Husband of aunt is uncle. |
| 106 | * @param sister |
| 107 | * @param parent |
| 108 | */ |
| 109 | @Handler |
| 110 | public Uncle infer(Husband husband, Aunt aunt) { |
| 111 | return husband.getObject().equals(aunt.getSubject()) ? new Uncle(husband.getSubject(), aunt.getObject()) : null; |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Daughter of sibling is niece. |
| 116 | */ |
| 117 | @Handler("java(daughter, sibling)://daughter.getObject().equals(sibling.getSubject())") |
| 118 | public Niece infer(Daughter daughter, Sibling sibling) { |
| 119 | return new Niece(daughter.getSubject(), sibling.getObject()); |
| 120 | } |
| 121 | |
| 122 | // TODO Implement: Spouse's niece is my niece. |
| 123 | |
| 124 | /** |
| 125 | * Son of sibling is nephew. |
| 126 | */ |
| 127 | @Handler("java(son,sibling)://son.getObject().equals(sibling.getSubject())") |
| 128 | public Nephew infer(Son son, Sibling sibling) { |
| 129 | return new Nephew(son.getSubject(), sibling.getObject()); |
| 130 | } |
| 131 | |
| 132 | // TODO Implement: Spouse's nephew is my nephew. |
| 133 | |
| 134 | } |