001    /*
002     * hammurapi-rules-tutorial @mesopotamia.version@
003     * Hammurapi rules tutorial. 
004     * Copyright (C) 2006  Hammurapi Group
005     *
006     * This program is free software; you can redistribute it and/or
007     * modify it under the terms of the GNU Lesser General Public
008     * License as published by the Free Software Foundation; either
009     * version 2 of the License, or (at your option) any later version.
010     *
011     * This program is distributed in the hope that it will be useful,
012     * but WITHOUT ANY WARRANTY; without even the implied warranty of
013     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014     * Lesser General Public License for more details.
015     *
016     * You should have received a copy of the GNU Lesser General Public
017     * License along with this library; if not, write to the Free Software
018     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
019     *
020     * URL: http://http://www.hammurapi.biz
021     * e-Mail: support@hammurapi.biz
022     */
023    package biz.hammurapi.rules.tutorial.rules;
024    
025    import biz.hammurapi.rules.Rule;
026    import biz.hammurapi.rules.tutorial.conclusions.Aunt;
027    import biz.hammurapi.rules.tutorial.conclusions.Brother;
028    import biz.hammurapi.rules.tutorial.conclusions.Child;
029    import biz.hammurapi.rules.tutorial.conclusions.Cousin;
030    import biz.hammurapi.rules.tutorial.conclusions.Daughter;
031    import biz.hammurapi.rules.tutorial.conclusions.Husband;
032    import biz.hammurapi.rules.tutorial.conclusions.Nephew;
033    import biz.hammurapi.rules.tutorial.conclusions.Niece;
034    import biz.hammurapi.rules.tutorial.conclusions.Parent;
035    import biz.hammurapi.rules.tutorial.conclusions.Sibling;
036    import biz.hammurapi.rules.tutorial.conclusions.Sister;
037    import biz.hammurapi.rules.tutorial.conclusions.Son;
038    import biz.hammurapi.rules.tutorial.conclusions.Uncle;
039    import biz.hammurapi.rules.tutorial.conclusions.Wife;
040    
041    /**
042     * Infers aunt, uncle, cousin, niece, nephew
043     * @author Pavel Vlasov
044     * @revision $Revision$
045     */
046    public class SecondaryRules extends Rule {
047            
048            {
049                    setMethodFactTypes(new Class[] {Child.class, Child.class, Sibling.class} , Cousin.class);
050            }
051    
052            /**
053             * Infers cousin as a child or parent's sibling.
054             */
055            public void infer(Child child1, Child child2, Sibling sibling) {
056                    if (child1.getObject().equals(sibling.getSubject()) && child2.getObject().equals(sibling.getObject())) {
057                            post(new Cousin(child1.getSubject(), child2.getSubject()));
058                    }
059            }
060            
061            /**
062             * If A is a cousin of B then B is a cousin of A
063             * @param cousin
064             * @return
065             */
066            public Cousin infer(Cousin cousin) {
067                    return new Cousin(cousin.getObject(), cousin.getSubject());
068            }
069            
070            {
071                    setMethodFactTypes(new Class[] {Aunt.class, Parent.class} , Cousin.class);
072            }
073    
074            /**
075             * Another way to infer cousin to demonstrate mutiple derivations
076             * @param aunt
077             * @param child
078             */
079            public void infer(Aunt aunt, Parent parent) {
080                    if (aunt.getSubject().equals(parent.getSubject())) {
081                            post(new Cousin(aunt.getObject(), parent.getObject()));
082                    }
083            }
084            
085            {
086                    setMethodFactTypes(new Class[] {Uncle.class, Parent.class} , Cousin.class);
087            }
088    
089            /**
090             * Another way to infer cousin to demonstrate mutiple derivations
091             * @param uncle
092             * @param child
093             */
094            public void infer(Uncle uncle, Parent parent) {
095                    if (uncle.getSubject().equals(parent.getSubject())) {
096                            post(new Cousin(uncle.getObject(), parent.getObject()));
097                    }
098            }
099            
100            /**
101             * Sister of parent is aunt.
102             * @param sister
103             * @param parent
104             */
105            public Aunt infer(Sister sister, Parent parent) {
106                    return sister.getObject().equals(parent.getSubject()) ? new Aunt(sister.getSubject(), parent.getObject()) : null;
107            }
108            
109            /**
110             * Wife of uncle is aunt.
111             * @param sister
112             * @param parent
113             */
114            public Aunt infer(Wife wife, Uncle uncle) {
115                    return wife.getObject().equals(uncle.getSubject()) ? new Aunt(wife.getSubject(), uncle.getObject()) : null;
116            }
117            
118            /**
119             * Brother of parent is uncle.
120             * @param sister
121             * @param parent
122             */
123            public Uncle infer(Brother brother, Parent parent) {
124                    return brother.getObject().equals(parent.getSubject()) ? new Uncle(brother.getSubject(), parent.getObject()) : null;
125            }
126            
127            /**
128             * Husband of aunt is uncle.
129             * @param sister
130             * @param parent
131             */
132            public Uncle infer(Husband husband, Aunt aunt) {
133                    return husband.getObject().equals(aunt.getSubject()) ? new Uncle(husband.getSubject(), aunt.getObject()) : null;
134            }
135            
136            {
137                    setMethodFactTypes(new Class[] {Daughter.class, Sibling.class} , Niece.class);
138            }
139    
140            /**
141             * Daughter of sibling is niece.
142             */
143            public void infer(Daughter daughter, Sibling sibling) {
144                    if (daughter.getObject().equals(sibling.getSubject())) {
145                            post(new Niece(daughter.getSubject(), sibling.getObject()));
146                    }
147            }
148            
149            // TODO Implement: Spouse's niece is my niece.
150            
151            {
152                    setMethodFactTypes(new Class[] {Son.class, Sibling.class} , Nephew.class);
153            }
154    
155            /**
156             * Son of sibling is nephew.
157             */
158            public void infer(Son son, Sibling sibling) {
159                    if (son.getObject().equals(sibling.getSubject())) {
160                            post(new Nephew(son.getSubject(), sibling.getObject()));
161                    }
162            }
163            
164            // TODO Implement: Spouse's nephew is my nephew.
165                    
166    }