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.util.ArrayList; 027 import java.util.Collection; 028 import java.util.Iterator; 029 import java.util.logging.Handler; 030 import java.util.logging.LogRecord; 031 import java.util.logging.Logger; 032 033 import biz.hammurapi.config.DomConfigFactory; 034 import biz.hammurapi.rules.FactSource; 035 import biz.hammurapi.rules.PojoCollectionManager; 036 import biz.hammurapi.rules.backwardreasoning.BackwardReasoningRulesContainer; 037 import biz.hammurapi.rules.tutorial.conclusions.Child; 038 import biz.hammurapi.rules.tutorial.conclusions.GrandMother; 039 import biz.hammurapi.rules.tutorial.conclusions.Spouse; 040 import biz.hammurapi.rules.tutorial.facts.Person; 041 import biz.hammurapi.swing.Browser; 042 import biz.hammurapi.util.ThreadPool; 043 044 public class BackwardReasoningTutorial { 045 046 /** 047 * Main method for the tutorial. 048 * @param args The first argument is rule set URI. 049 */ 050 public static void main(String[] args) throws Exception { 051 052 // For debugging. 053 Logger containerLogger = Logger.getLogger("biz.hammurapi.rules"); 054 // containerLogger.setLevel(Level.FINEST); 055 containerLogger.addHandler(new Handler() { 056 057 public void close() throws SecurityException { 058 059 } 060 061 public void flush() { 062 063 } 064 065 public void publish(LogRecord record) { 066 System.out.println(record.getMessage()); 067 } 068 069 }); 070 071 // Source facts 072 final Collection sourceFacts = new ArrayList(); 073 074 Person kate = new Person("Kate", 58, false); 075 Person victor = new Person("Victor", 63, true); 076 sourceFacts.add(new Spouse(kate, victor)); 077 078 Person peter = new Person("Peter", 37, true); 079 sourceFacts.add(new Child(peter, kate)); 080 sourceFacts.add(new Child(peter, victor)); 081 082 Person alison = new Person("Alison", 36, false); 083 sourceFacts.add(new Spouse(peter, alison)); 084 085 Person lucy = new Person("Lucy", 17, false); 086 sourceFacts.add(new Child(lucy, alison)); 087 088 Person nancy = new Person("Nancy", 14, false); 089 sourceFacts.add(new Child(nancy, peter)); 090 091 Person dan = new Person("Dan", 7, true); 092 sourceFacts.add(new Child(dan, peter)); 093 sourceFacts.add(new Child(dan, alison)); 094 095 Person audrey = new Person("Audrey", 4, false); 096 sourceFacts.add(new Child(audrey, peter)); 097 sourceFacts.add(new Child(audrey, alison)); 098 099 Person tanya = new Person("Tanya", 31, false); 100 Person max = new Person("Max", 32, true); 101 sourceFacts.add(new Spouse(tanya, max)); 102 sourceFacts.add(new Child(tanya, kate)); 103 sourceFacts.add(new Child(tanya, victor)); 104 105 Person vilma = new Person("Vilma", 14, false); 106 sourceFacts.add(new Child(vilma, tanya)); 107 108 Person george = new Person("George", 10, true); 109 sourceFacts.add(new Child(george, tanya)); 110 111 Person lisa = new Person("Lisa", 5, false); 112 sourceFacts.add(new Child(lisa, tanya)); 113 sourceFacts.add(new Child(lisa, max)); 114 115 // Configuring rules container 116 DomConfigFactory factory = new DomConfigFactory(); 117 BackwardReasoningRulesContainer container = (BackwardReasoningRulesContainer) factory.create(new File("familyties-backwardreasoning.xml"), null); 118 119 PojoCollectionManager collectionManager = new PojoCollectionManager(); 120 collectionManager.start(); 121 122 container.setCollectionManager(collectionManager); 123 124 ThreadPool worker = new ThreadPool(); 125 worker.setNumberOfThreads(10); 126 worker.start(); 127 container.setWorker(worker); 128 container.setMaxWorkerJobs(10); 129 130 // Simple implementation of fact source 131 FactSource factSource = new FactSource() { 132 133 public Iterator getFacts(Class factType) { 134 Collection ret = new ArrayList(); 135 Iterator it = sourceFacts.iterator(); 136 while (it.hasNext()) { 137 Object next = it.next(); 138 if (factType.isInstance(next)) { 139 ret.add(next); 140 } 141 } 142 return ret.iterator(); 143 } 144 145 public Class[] getFactTypes() { 146 return new Class[] {Child.class, Spouse.class}; 147 } 148 149 }; 150 151 container.addFactSource(factSource); 152 153 container.start(); 154 155 // Fact types available 156 Class[] factTypes = container.getFactTypes(); 157 System.out.println("Fact types"); 158 for (int i=0; i<factTypes.length; ++i) { 159 System.out.println("\t"+factTypes[i].getName()); 160 } 161 162 // Querying for different fact types 163 164 Class queryType = GrandMother.class; 165 System.out.println("Querying for "+queryType); 166 Iterator fit = container.getFacts(queryType); 167 Collection objects = new ArrayList(); 168 while (fit.hasNext()) { 169 Object next = fit.next(); 170 if (next!=null) { 171 System.out.println("\t"+next); 172 objects.add(next); 173 } 174 } 175 176 Browser.showAndExitOnClose(objects, queryType.getName()); 177 178 // Done with reasoning, stopping. 179 container.join(); 180 container.stop(); 181 worker.stop(); 182 collectionManager.stop(); 183 184 // List objects = infer(args[0], of); 185 // 186 // Collections.sort( 187 // objects, 188 // new Comparator() { 189 // 190 // public int compare(Object o1, Object o2) { 191 // return o1.toString().compareTo(o2.toString()); 192 // } 193 // 194 // }); 195 // 196 // Iterator it=objects.iterator(); 197 // for (int i=1; it.hasNext(); i++) { 198 // System.out.println(i + ": "+it.next()); 199 // } 200 // 201 // DOMUtils.serialize(objects, "conclusions", new File("conclusions.xml")); 202 // 203 // Browser.showAndExitOnClose(objects, "Family relationships"); 204 } 205 206 }