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.BadFact;
027    import biz.hammurapi.rules.tutorial.conclusions.CloseRelative;
028    import biz.hammurapi.rules.tutorial.conclusions.Parent;
029    import biz.hammurapi.rules.tutorial.conclusions.Relative;
030    import biz.hammurapi.rules.tutorial.conclusions.SuspectParentRelationship;
031    
032    public class Validator extends Rule {
033            
034    
035            public Validator() {
036                    super("validate", "remove", "accept");
037                    setMethodFactTypes(Relative.class , BadFact.class);
038                    setMethodFactTypes(Parent.class , SuspectParentRelationship.class);
039            }
040            
041            /**
042             * A person cannot be a relative of self.
043             * @param relative
044             */
045            public void validate(Relative relative) {
046                    if (relative.getSubject().equals(relative.getObject())) {
047                            post(new BadFact(relative, "A person cannot be a relative of self"));
048                    }
049            }
050            
051            /**
052             * Close relatives can have only relationship of one type with each other.
053             * Son is not supposed to marry his mother and sister shall not marry her brother.
054             * Conclusion which was inferred later that the other is negated. Relative's compareTo() is
055             * used for comparison.
056             * @param r1
057             * @param r2
058             */
059            public void validate(CloseRelative r1, CloseRelative r2) {
060                    if (!r1.equals(r2)
061                                    && ! (r1.supercedes(r2) || r2.supercedes(r1))
062                                    && r1.getSubject().equals(r2.getSubject())
063                                    && r1.getObject().equals(r2.getObject())
064                            ) {
065                            CloseRelative toBeRemoved = r1.compareTo(r2) < 0 ? r2 : r1;
066                            post(new BadFact(toBeRemoved, "Close relatives cannot have more than one type of relationship. Relationships '"+r1+"', '"+r2+"'. Relationship '"+toBeRemoved+"' will be removed"));
067                    }
068            }
069            
070            
071            private int legalAge;
072            
073            /**
074             * Minimal age when people are allowed to marry.
075             * @param legalAge Legal age in years
076             */
077            public void setLegalAge(int legalAge) {
078                    this.legalAge = legalAge;
079            }
080            
081            /**
082             * Validates that difference between parent and child age is not less than legal age.
083             * @param mother
084             */
085            public void validate(Parent parent) {
086                    if (parent.getSubject().getAge()-parent.getObject().getAge()<legalAge) {
087                            post(new SuspectParentRelationship(parent, parent.getSubject() + " was under legal age when "+parent.getObject()+" was born. Please verify input data."));
088                    }
089            }
090    
091    
092    }