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.Brother;
027    import biz.hammurapi.rules.tutorial.conclusions.Child;
028    import biz.hammurapi.rules.tutorial.conclusions.Sibling;
029    import biz.hammurapi.rules.tutorial.conclusions.Sister;
030    
031    public class SiblingRules extends Rule {        
032            
033            public SiblingRules() {
034                    setMethodFactTypes(Sibling.class , new Class[] {Brother.class, Sister.class});
035                    setMethodFactTypes(new Class[] {Child.class, Child.class}, Sibling.class);
036            }       
037                    
038            /**
039             * Male sibling is brother, female sibling is sister.
040             * If A is a sibling of B then B is a sibling of A.
041             * @param sibling
042             * @return Brother or Sister.
043             */
044            public Sibling infer(Sibling sibling) { 
045                    post(new Sibling(sibling.getObject(), sibling.getSubject()));
046                    
047                    if (sibling.getSubject().isMale()) {
048                            if (!(sibling instanceof Brother)) {
049                                    return new  Brother(sibling.getSubject(), sibling.getObject());
050                            }                       
051                    } else {
052                            if (!(sibling instanceof Sister)) {
053                                    return new Sister(sibling.getSubject(), sibling.getObject());
054                            }                       
055                    }
056                    return null;
057            }
058    
059            /**
060             * If two children have common parent then they are siblings.
061             * @param child
062             * @param anotherChild
063             */
064            public void infer(Child child, Child anotherChild) {
065                    if (!child.getSubject().equals(anotherChild.getSubject()) 
066                                    && child.getObject().equals(anotherChild.getObject())) {
067                            post(new Sibling(child.getSubject(), anotherChild.getSubject()));
068                            post(new Sibling(anotherChild.getSubject(), child.getSubject()));
069                    }
070            }
071    }