/*
	WordNet Access Library
	Copyright (C) 2006 Pavel Simakov
	http://www.softwaresecretweapons.com

	This library is free software; you can redistribute it and/or
	modify it under the terms of the GNU Lesser General Public
	License as published by the Free Software Foundation; either
	version 2.1 of the License, or (at your option) any later version.

	This library is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
	Lesser General Public License for more details.

	You should have received a copy of the GNU Lesser General Public
	License along with this library; if not, write to the Free Software
	Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
*/

package com.oy.shared.wn.samples;

import java.sql.SQLException;

import com.oy.shared.lw.LinguinePerfWatch;
import com.oy.shared.lw.misc.ITrace;
import com.oy.shared.wn.IWNDatabase;
import com.oy.shared.wn.IWNSynset;
import com.oy.shared.wn.IWNWord;
import com.oy.shared.wn.core.WNDatabase;

public class ConsoleTest {
	
	private static final String CONN_STRING = "jdbc:mysql://localhost:3306/wn_20?user=admin";
	
	public static void main(String [] args){
				
		LinguinePerfWatch watch = new LinguinePerfWatch();
	
		try {
			
			// load mySQL JDBC driver; must be on the classpath		
			final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
			try {
				Class.forName(JDBC_DRIVER);
			}catch(Exception e){
				throw new SQLException("Error loading database driver.");
			}
			
			// enable monitoring
			watch.getPerfAgentContext().setSnapshotFileName("c:/wn.perf.html");
			watch.getPerfAgentContext().setCollectDelayMillis(1000);
			watch.getPerfAgentContext().setSnapshotDelayMillis(1000);
			watch.start();
			try {
			
				// start WordNet engine
				WNDatabase db = new WNDatabase(
					watch.getPerfAgent(), 
					new ITrace.MockTraceOutImpl(ConsoleTest.class, ITrace.INFO), 
					CONN_STRING
				);
				db.open(); 
				try {
					doWork(db);
				} finally {
					db.close();
				}
			
				
				
			} finally {
				watch.stop();
			}
			
			
		} catch(Exception e){
			e.printStackTrace();
		}
	}
	
	public static void doWork(IWNDatabase db) throws SQLException {			
		try {			
			// get all root (no parent) words
			list(db.getRoots());
		
			// try words
			list(db.getSynsetByWord("I"));
			list(db.getSynsetByWord("love"));					
			list(db.getSynsetByWord("software"));
			list(db.getSynsetByWord("engineering"));
			
			// try part of
			list(db.getSynsetByWord("I")[0].getPartOf());
			list(db.getSynsetByWord("love")[0].getPartOf());
			list(db.getSynsetByWord("software")[0].getPartOf());
			list(db.getSynsetByWord("engineering")[0].getPartOf());				
		
		} catch(Exception e){
			e.printStackTrace();
		}	
	}
	
	public static void list(IWNSynset [] set) throws SQLException {
		for (int i=0; i < set.length; i++){
			System.out.println("SS [" + i + "]{");
			list(set[i].getWords());
			System.out.println("}");
		}
	}
	
	public static void list(IWNWord [] set) throws SQLException {
		for (int i=0; i < set.length; i++){
			System.out.println("  W[" + i + "]" + set[i].getWord());
		}
	}
	
}
