001    /*
002    @license.text@
003     */
004    package com.hammurapi.reasoning.tutorial.rules;
005    
006    import com.hammurapi.reasoning.spi.Condition;
007    import com.hammurapi.reasoning.spi.Infer;
008    import com.hammurapi.reasoning.spi.InferenceContext;
009    import com.hammurapi.reasoning.tutorial.objectmodel.Aunt;
010    import com.hammurapi.reasoning.tutorial.objectmodel.Brother;
011    import com.hammurapi.reasoning.tutorial.objectmodel.Child;
012    import com.hammurapi.reasoning.tutorial.objectmodel.Cousin;
013    import com.hammurapi.reasoning.tutorial.objectmodel.Daughter;
014    import com.hammurapi.reasoning.tutorial.objectmodel.Husband;
015    import com.hammurapi.reasoning.tutorial.objectmodel.Nephew;
016    import com.hammurapi.reasoning.tutorial.objectmodel.Niece;
017    import com.hammurapi.reasoning.tutorial.objectmodel.Parent;
018    import com.hammurapi.reasoning.tutorial.objectmodel.Sibling;
019    import com.hammurapi.reasoning.tutorial.objectmodel.Sister;
020    import com.hammurapi.reasoning.tutorial.objectmodel.Son;
021    import com.hammurapi.reasoning.tutorial.objectmodel.Uncle;
022    import com.hammurapi.reasoning.tutorial.objectmodel.Wife;
023    
024    /**
025     * Infers aunt, uncle, cousin, niece, nephew
026     * @author Pavel Vlasov
027     * @revision $Revision$
028     */
029    public class SecondaryRules {
030            
031            /**
032             * Infers cousin as a child or parent's sibling.
033             */
034            @Infer(Cousin.class)
035            public void infer(Child child1, Child child2, Sibling sibling) {
036                    if (child1.getObject().equals(sibling.getSubject()) && child2.getObject().equals(sibling.getObject())) {
037                            InferenceContext.INSTANCE.post(new Cousin(child1.getSubject(), child2.getSubject()));
038                    }
039            }
040            
041            /**
042             * If A is a cousin of B then B is a cousin of A
043             * @param cousin
044             * @return
045             */
046            @Infer
047            public Cousin infer(Cousin cousin) {
048                    return new Cousin(cousin.getObject(), cousin.getSubject());
049            }
050            
051            /**
052             * Another way to infer cousin to demonstrate mutiple derivations
053             * @param aunt
054             * @param child
055             */
056            @Infer
057            @Condition("arg0.getSubject().equals(arg1.getSubject())")
058            public Cousin infer(Aunt aunt, Parent parent) {
059                    return new Cousin(aunt.getObject(), parent.getObject());
060            }
061            
062            /**
063             * Another way to infer cousin to demonstrate mutiple derivations
064             * @param uncle
065             * @param child
066             */
067            @Infer
068            public Cousin infer(Uncle uncle, Parent parent) {
069                    if (uncle.getSubject().equals(parent.getSubject())) {
070                            return new Cousin(uncle.getObject(), parent.getObject());
071                    }
072                    return null;
073            }
074            
075            /**
076             * Sister of parent is aunt.
077             * @param sister
078             * @param parent
079             */
080            @Infer
081            public Aunt infer(Sister sister, Parent parent) {
082                    return sister.getObject().equals(parent.getSubject()) ? new Aunt(sister.getSubject(), parent.getObject()) : null;
083            }
084            
085            /**
086             * Wife of uncle is aunt.
087             * @param sister
088             * @param parent
089             */
090            @Infer
091            public Aunt infer(Wife wife, Uncle uncle) {
092                    return wife.getObject().equals(uncle.getSubject()) ? new Aunt(wife.getSubject(), uncle.getObject()) : null;
093            }
094            
095            /**
096             * Brother of parent is uncle.
097             * @param sister
098             * @param parent
099             */
100            @Infer
101            public Uncle infer(Brother brother, Parent parent) {
102                    return brother.getObject().equals(parent.getSubject()) ? new Uncle(brother.getSubject(), parent.getObject()) : null;
103            }
104            
105            /**
106             * Husband of aunt is uncle.
107             * @param sister
108             * @param parent
109             */
110            @Infer
111            public Uncle infer(Husband husband, Aunt aunt) {
112                    return husband.getObject().equals(aunt.getSubject()) ? new Uncle(husband.getSubject(), aunt.getObject()) : null;
113            }
114            
115            /**
116             * Daughter of sibling is niece.
117             */
118            @Infer
119            public Niece infer(Daughter daughter, Sibling sibling) {
120                    if (daughter.getObject().equals(sibling.getSubject())) {
121                            return new Niece(daughter.getSubject(), sibling.getObject());
122                    }
123                    return null;
124            }
125            
126            // TODO Implement: Spouse's niece is my niece.
127            
128            /**
129             * Son of sibling is nephew.
130             */
131            @Infer
132            public Nephew infer(Son son, Sibling sibling) {
133                    if (son.getObject().equals(sibling.getSubject())) {
134                            return new Nephew(son.getSubject(), sibling.getObject());
135                    }
136                    return null;
137            }
138            
139            // TODO Implement: Spouse's nephew is my nephew.
140                    
141    }