///*
//@license.text@
// */
//package com.hammurapi.reasoning.tutorial;
//
//import java.io.File;
//import java.util.ArrayList;
//import java.util.Collection;
//import java.util.Iterator;
//import java.util.logging.Handler;
//import java.util.logging.LogRecord;
//import java.util.logging.Logger;
//
//import biz.hammurapi.config.DomConfigFactory;
//import biz.hammurapi.rules.FactSource;
//import biz.hammurapi.rules.PojoCollectionManager;
//import biz.hammurapi.rules.backwardreasoning.BackwardReasoningRulesContainer;
//import biz.hammurapi.rules.tutorial.objectmodel.Child;
//import biz.hammurapi.rules.tutorial.objectmodel.GrandMother;
//import biz.hammurapi.rules.tutorial.objectmodel.Spouse;
//import biz.hammurapi.rules.tutorial.objectmodel.Person;
//import biz.hammurapi.swing.Browser;
//import biz.hammurapi.util.ThreadPool;
//
//public class BackwardReasoningTutorial {
//
//	/**
//	 * Main method for the tutorial.
//	 * @param args The first argument is rule set URI.
//	 */
//	public static void main(String[] args) throws Exception {
//		
//		// For debugging.
//		Logger containerLogger = Logger.getLogger("biz.hammurapi.rules");
////		containerLogger.setLevel(Level.FINEST);
//		containerLogger.addHandler(new Handler() {
//
//			public void close() throws SecurityException {
//				
//			}
//
//			public void flush() {
//				
//			}
//
//			public void publish(LogRecord record) {
//				System.out.println(record.getMessage());
//			}
//			
//		});
//		
//		// Source facts
//		final Collection sourceFacts = new ArrayList();
//		
//		Person kate = new Person("Kate", 58, false);
//		Person victor = new Person("Victor", 63, true);
//		sourceFacts.add(new Spouse(kate, victor));
//
//		Person peter = new Person("Peter", 37, true);
//		sourceFacts.add(new Child(peter, kate));
//		sourceFacts.add(new Child(peter, victor));
//		
//		Person alison = new Person("Alison", 36, false);
//		sourceFacts.add(new Spouse(peter, alison));
//
//		Person lucy = new Person("Lucy", 17, false);
//		sourceFacts.add(new Child(lucy, alison));
//		
//		Person nancy = new Person("Nancy", 14, false);
//		sourceFacts.add(new Child(nancy, peter));
//		
//		Person dan = new Person("Dan", 7, true);
//		sourceFacts.add(new Child(dan, peter));
//		sourceFacts.add(new Child(dan, alison));
//		
//		Person audrey = new Person("Audrey", 4, false);
//		sourceFacts.add(new Child(audrey, peter));
//		sourceFacts.add(new Child(audrey, alison));		
//		
//		Person tanya = new Person("Tanya", 31, false);
//		Person max = new Person("Max", 32, true);
//		sourceFacts.add(new Spouse(tanya, max));
//		sourceFacts.add(new Child(tanya, kate));
//		sourceFacts.add(new Child(tanya, victor));
//
//		Person vilma = new Person("Vilma", 14, false);
//		sourceFacts.add(new Child(vilma, tanya));
//		
//		Person george = new Person("George", 10, true);
//		sourceFacts.add(new Child(george, tanya));
//		
//		Person lisa = new Person("Lisa", 5, false);
//		sourceFacts.add(new Child(lisa, tanya));
//		sourceFacts.add(new Child(lisa, max));
//		
//		// Configuring rules container
//		DomConfigFactory factory = new DomConfigFactory();
//		BackwardReasoningRulesContainer container = (BackwardReasoningRulesContainer) factory.create(new File("familyties-backwardreasoning.xml"), null);
//
//		PojoCollectionManager collectionManager = new PojoCollectionManager();
//		collectionManager.start();
//		
//		container.setCollectionManager(collectionManager);
//		
//		ThreadPool worker = new ThreadPool();
//		worker.setNumberOfThreads(10);
//		worker.start();
//		container.setWorker(worker);		
//		container.setMaxWorkerJobs(10);
//		
//		// Simple implementation of fact source
//		FactSource factSource = new FactSource() {
//
//			public Iterator getFacts(Class factType) {
//				Collection ret = new ArrayList();
//				Iterator it = sourceFacts.iterator();
//				while (it.hasNext()) {
//					Object next = it.next();
//					if (factType.isInstance(next)) {
//						ret.add(next);
//					}
//				}
//				return ret.iterator();
//			}
//
//			public Class[] getFactTypes() {
//				return new Class[] {Child.class, Spouse.class};
//			}
//			
//		};
//		
//		container.addFactSource(factSource);
//		
//		container.start();
//		
//		// Fact types available
//		Class[] factTypes = container.getFactTypes();
//		System.out.println("Fact types");
//		for (int i=0; i<factTypes.length; ++i) {
//			System.out.println("\t"+factTypes[i].getName());
//		}
//		
//		// Querying for different fact types
//		
//		Class queryType = GrandMother.class;
//		System.out.println("Querying for "+queryType);
//		Iterator fit = container.getFacts(queryType);
//		Collection objects = new ArrayList();
//		while (fit.hasNext()) {
//			Object next = fit.next();
//			if (next!=null) {
//				System.out.println("\t"+next);
//				objects.add(next);
//			}
//		}
//		
//		Browser.showAndExitOnClose(objects, queryType.getName());		
//		
//		// Done with reasoning, stopping.
//		container.join();		
//		container.stop();
//		worker.stop();
//		collectionManager.stop();
//		
////		List objects = infer(args[0], of);				
////		
////		Collections.sort(
////				objects,
////				new Comparator() {
////
////					public int compare(Object o1, Object o2) {
////						return o1.toString().compareTo(o2.toString());
////					}
////					
////				});
////		
////		Iterator it=objects.iterator();
////		for (int i=1; it.hasNext(); i++) {
////			System.out.println(i + ": "+it.next());
////		}
////		
////		DOMUtils.serialize(objects, "conclusions", new File("conclusions.xml"));
////
////		Browser.showAndExitOnClose(objects, "Family relationships");		
//	}
//
//}
