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;
024    
025    import java.io.File;
026    import java.rmi.RemoteException;
027    import java.util.ArrayList;
028    import java.util.Collections;
029    import java.util.Comparator;
030    import java.util.Iterator;
031    import java.util.List;
032    
033    import javax.rules.ConfigurationException;
034    import javax.rules.InvalidRuleSessionException;
035    import javax.rules.ObjectFilter;
036    import javax.rules.RuleExecutionSetNotFoundException;
037    import javax.rules.RuleRuntime;
038    import javax.rules.RuleServiceProvider;
039    import javax.rules.RuleServiceProviderManager;
040    import javax.rules.RuleSessionCreateException;
041    import javax.rules.RuleSessionTypeUnsupportedException;
042    import javax.rules.StatefulRuleSession;
043    
044    import biz.hammurapi.rules.Conclusion;
045    import biz.hammurapi.rules.tutorial.conclusions.Child;
046    import biz.hammurapi.rules.tutorial.conclusions.Spouse;
047    import biz.hammurapi.rules.tutorial.facts.Person;
048    
049    import biz.hammurapi.swing.Browser;
050    import biz.hammurapi.xml.dom.DOMUtils;
051    
052    public class Tutorial {
053    
054            /**
055             * Infers relationships.
056             * @param args
057             * @return
058             * @throws ClassNotFoundException
059             * @throws ConfigurationException
060             * @throws RuleSessionTypeUnsupportedException
061             * @throws RuleSessionCreateException
062             * @throws RuleExecutionSetNotFoundException
063             * @throws RemoteException
064             * @throws InvalidRuleSessionException
065             */
066            public static List infer(String ruleSetUrl, ObjectFilter filter) throws ClassNotFoundException, ConfigurationException, RuleSessionTypeUnsupportedException, RuleSessionCreateException, RuleExecutionSetNotFoundException, RemoteException, InvalidRuleSessionException {
067                    String ruleServiceProviderClassName = "biz.hammurapi.rules.jsr94.FileRuleServiceProvider";
068                    Class.forName(ruleServiceProviderClassName);
069                    RuleServiceProvider serviceProvider = RuleServiceProviderManager.getRuleServiceProvider(ruleServiceProviderClassName);
070                    RuleRuntime runtime = serviceProvider.getRuleRuntime();
071                    System.out.println("Loading rule set from "+ruleSetUrl);
072                    StatefulRuleSession session = (StatefulRuleSession) runtime.createRuleSession(ruleSetUrl, null, RuleRuntime.STATEFUL_SESSION_TYPE);
073                    
074                    Person kate = new Person("Kate", 58, false);
075                    Person victor = new Person("Victor", 63, true);
076                    session.addObject(new Spouse(kate, victor));
077    
078                    Person peter = new Person("Peter", 37, true);
079                    session.addObject(new Child(peter, kate));
080                    session.addObject(new Child(peter, victor));
081                    
082                    Person alison = new Person("Alison", 36, false);
083                    session.addObject(new Spouse(peter, alison));
084    
085                    Person lucy = new Person("Lucy", 17, false);
086                    session.addObject(new Child(lucy, alison));
087                    
088                    Person nancy = new Person("Nancy", 14, false);
089                    session.addObject(new Child(nancy, peter));
090                    
091                    Person dan = new Person("Dan", 7, true);
092                    session.addObject(new Child(dan, peter));
093                    session.addObject(new Child(dan, alison));
094                    
095                    Person audrey = new Person("Audrey", 4, false);
096                    session.addObject(new Child(audrey, peter));
097                    session.addObject(new Child(audrey, alison));           
098                    
099                    Person tanya = new Person("Tanya", 31, false);
100                    Person max = new Person("Max", 32, true);
101                    session.addObject(new Spouse(tanya, max));
102                    session.addObject(new Child(tanya, kate));
103                    session.addObject(new Child(tanya, victor));
104    
105                    Person vilma = new Person("Vilma", 14, false);
106                    session.addObject(new Child(vilma, tanya));
107                    
108                    Person george = new Person("George", 10, true);
109                    session.addObject(new Child(george, tanya));
110                    
111                    Person lisa = new Person("Lisa", 5, false);
112                    session.addObject(new Child(lisa, tanya));
113                    session.addObject(new Child(lisa, max));                
114                    
115                    System.out.println("Source facts: "+session.getObjects().size());
116                    
117                    session.executeRules();
118                    
119                    List objects = new ArrayList(filter==null ? session.getObjects() : session.getObjects(filter));
120                    
121                    System.out.println("Conclusions: "+objects.size());
122                    
123                    session.release();
124                    return objects;
125            }
126    
127            /**
128             * Main method for the tutorial.
129             * @param args The first argument is rule set URI.
130             */
131            public static void main(String[] args) throws Exception {
132                    System.out.println("Hammurapi rules tutorial");
133                    
134                    if (args.length!=1) {
135                            System.out.println("Usage: java <options> biz.hammurapi.rules.tutorial.Tutorial <rule set url>");
136                            System.exit(1);
137                    }
138                    
139                    // Exclude already known.
140                    ObjectFilter of = new ObjectFilter() {
141    
142                            public Object filter(Object arg) {
143                                    return arg instanceof Conclusion && ((Conclusion) arg).getDepth()==0 ? null : arg;
144                            }
145    
146                            public void reset() {
147                                    // TODO Auto-generated method stub
148                                    
149                            }
150                            
151                    };              
152                    
153                    List objects = infer(args[0], of);                              
154                    
155                    Collections.sort(
156                                    objects,
157                                    new Comparator() {
158    
159                                            public int compare(Object o1, Object o2) {
160                                                    return o1.toString().compareTo(o2.toString());
161                                            }
162                                            
163                                    });
164                    
165                    Iterator it=objects.iterator();
166                    for (int i=1; it.hasNext(); i++) {
167                            System.out.println(i + ": "+it.next());
168                    }
169                    
170                    DOMUtils.serialize(objects, "conclusions", new File("conclusions.xml"));
171    
172                    Browser.showAndExitOnClose(objects, "Family relationships");            
173            }
174    
175    }