
import java.util.ArrayList;
import java.util.Iterator;

import org.tmapi.core.*;

/**
 * Created on 2004-11-28<br>
 * Last modified on 2005-02-17<br>
 * <br>
 * code examples being part of the<br>
 * Tutorial for using TMAPI 1.0<br>
 *
 * @author Oliver Leimig, oliver at delete.this.leimig.de
 * @author Robert Barta, rho@bond.edu.au
 * @version 1.0
 * 
 */
public class TMAPITutorial {

	/**
	 * Iterates over all TopicNames of the given Topic and prints them out
	 * @param t	any Topic
	 */
	public static void printTopicNames(Topic t){
        Iterator it = t.getTopicNames().iterator();
        System.out.print("Topic : ");
        while(it.hasNext()){
            TopicName tn= (TopicName)it.next();
            System.out.print(tn.getValue());

            Iterator itS = tn.getScope().iterator();
        	while ( itS.hasNext() ) {
        		Topic sc = (Topic)itS.next();
        		System.out.print("\n (scope: " + ((TopicName)sc.getTopicNames().iterator().next()).getValue() +") " );
        	}

        	if(it.hasNext())
                System.out.print(", ");
        }
        System.out.println("");
    } //endmethod printTopicNames


	/**
	 * Examples for using TMAPI, explanations see TMAPI Tutorial
	 */
	public static void main(String[] args) throws TMAPIException {

		
		//Preliminaries
		TopicMapSystemFactory tSystemFactory = TopicMapSystemFactory.newInstance();
		TopicMapSystem tSystem = tSystemFactory.newTopicMapSystem();
		TopicMap tMap = tSystem.createTopicMap( "http://topicmaps.bond.edu.au/example/" );

		//Adding Topics with Names
		Topic tAirportSydney = tMap.createTopic();
		tAirportSydney.createTopicName( "Airport Sydney, Australia", null );

		Topic tAirport = tMap.createTopic();
		tAirport.createTopicName( "Airport", null );

		tAirportSydney.addType( tAirport );

		Topic tGerman     = tMap.createTopic();
		tGerman.createTopicName( "german language", null );

		ArrayList alScope = new ArrayList();
		alScope.add( tGerman );

		tAirportSydney.createTopicName( "Flughafen Sydney, Australien", alScope );

		Locator locSydOcc = tMap.createLocator ( "http://www.sydney.com.au/airport-transfers.htm" );
		Occurrence ocSydAirportTransfers = tAirportSydney.createOccurrence( locSydOcc, null, null );

		Topic tHomepage = tMap.createTopic();
		tHomepage.createTopicName( "Homepage", null );

		Locator locSydHome  = tMap.createLocator ( "http://www.sydneyairport.com.au/SACL/default.htm" );
		Occurrence ocSydAirportHP = tAirportSydney.createOccurrence( locSydHome, tHomepage, null );

		Topic tAirportCode = tMap.createTopic();
		tAirportCode.createTopicName( "Airport Code", null );
		tAirportSydney.createOccurrence( "SYD", tAirportCode, null );


		
		//Adding Associations

		Association assoc = tMap.createAssociation();

		Topic tAssType = tMap.createTopic();
		tAssType.createTopicName( "has flight from to", null );

		assoc.setType ( tAssType );
		
		// -------------------
		Topic tAirportTokyo = tMap.createTopic();
		Topic tDestination = tMap.createTopic();
		tDestination.createTopicName("destination", null);
		Topic tOrigin= tMap.createTopic();
		tOrigin.createTopicName("origin", null);
		Topic tTue1015= tMap.createTopic();
		tTue1015.createTopicName("Tue, 10:15", null);
		Topic tTime= tMap.createTopic();
		tTime.createTopicName("time", null);
		// -------------------
				
		assoc.createAssociationRole( tAirportSydney, tOrigin );
		assoc.createAssociationRole( tAirportTokyo,  tDestination );
		assoc.createAssociationRole( tTue1015,       tTime );

		Topic tSummerTimetable = tMap.createTopic();
		tSummerTimetable.createTopicName( "summer timetable", null );

		assoc.addScopingTopic( tSummerTimetable );


		//Topic Identification

		Topic tNaritaHP = tMap.createTopic();
		tNaritaHP.createTopicName( "Narita Homepage", null );
		tNaritaHP.createOccurrence( "informative", null, null );

		Locator locNarita = tMap.createLocator ( "http://www.narita-airport.or.jp/airport_e/" ); 
		tNaritaHP.addSubjectLocator( locNarita );

			//already created above
			//Topic tAirportTokyo = tMap.createTopic();
		tAirportTokyo.createTopicName( "Narita", null );

		tAirportTokyo.addSubjectIdentifier( locNarita );
		
		
		//Reification

		Locator srcLocWP = tMap.createLocator (
		             tMap.getBaseLocator().getReference() + 
		             "#sydney-airport-homepage" );

		ocSydAirportHP.addSourceLocator ( srcLocWP );

		Topic tocSydAirportHP = tMap.createTopic();
					tocSydAirportHP.createTopicName("Sydney Airport homepage", null);
		tocSydAirportHP.addSubjectIdentifier ( srcLocWP );

		// -------------------
		Topic tFlightNumber = tMap.createTopic();
		tFlightNumber.createTopicName("flight number", null);
		// -------------------

		Topic tJL772 = tMap.createTopic();
		tJL772.createTopicName  ( "Japan Airlines Flight 772", null );
		tJL772.createOccurrence ( "JL772", tFlightNumber, null );

		Locator locAssoc = tMap.createLocator ( tMap.getBaseLocator().getReference() + "#sydney-tokyo-flight" );
		assoc.addSourceLocator      ( locAssoc );
		tJL772.addSubjectIdentifier ( locAssoc );

		
		//Browsing 
 		
		//print Topics including characteristics
		Iterator itT = tMap.getTopics().iterator();
		while (itT.hasNext()) {
			Topic t = (Topic)itT.next();
			printTopicNames( t );

			Iterator itSL = t.getSourceLocators().iterator();
			while ( itSL.hasNext() ) {
				System.out.println(" SourceLocator : " + ((Locator)itSL.next()).getReference() );				
			}
			
			Iterator itSIN = t.getSubjectIdentifiers().iterator();
			while ( itSIN.hasNext() ) {
				System.out.println(" Subject Indicator : " + ((Locator)itSIN.next()).getReference() );				
			}

			Iterator itSID = t.getSubjectLocators().iterator();
			while ( itSID.hasNext() ) {
				System.out.println(" Subject Identifier : " + ((Locator)itSID.next()).getReference() );				
			}

			Iterator itOL = t.getOccurrences().iterator();
			while ( itOL.hasNext() ) {
				Occurrence oc = (Occurrence)itOL.next();
				if (oc.getResource() != null) {
					if(oc.getType() != null) {
						System.out.println(" Occurrence : (" + ((TopicName)oc.getType().getTopicNames().iterator().next()).getValue() + ") " + oc.getResource().getReference() );				
					} else {
						System.out.println(" Occurrence : " + oc.getResource().getReference() );				
					}
				} else {
					System.out.println(" Occurrence : " + oc.getValue() );
				}
			}

		} //endwhile
		
		//print associations
		Iterator itA = tMap.getAssociations().iterator();
		while(itA.hasNext()){
        	Association a = (Association)itA.next();
        	if (a.getType() != null) {
        		System.out.println("\nAssociation : " + ((TopicName)a.getType().getTopicNames().iterator().next()).getValue() );
        	}

        	Iterator itS = a.getScope().iterator();
        	while ( itS.hasNext() ) {
        		Topic sc = (Topic)itS.next();
        		System.out.println(" Scope: " + ((TopicName)sc.getTopicNames().iterator().next()).getValue() );
        	}

        	Iterator itr = a.getAssociationRoles().iterator();
        	while(itr.hasNext()){
        			AssociationRole m = (AssociationRole)itr.next();
        			System.out.print("Association Members : ");
        			if (m.getType() != null) {
        				System.out.print( "("+((TopicName)m.getType().getTopicNames().iterator().next()).getValue() + ")\t");
        			}
        			if (m.getPlayer() != null) {
        				System.out.print( ((TopicName)m.getPlayer().getTopicNames().iterator().next()).getValue());
        			}
         			System.out.println();
        	}
        } //endwhile
		
		// print reified and reifier
       Iterator itR = tocSydAirportHP.getReified().iterator();
       while( itR.hasNext() ) {
                TopicMapObject o = (TopicMapObject)itR.next();
                System.out.println("\nreified TopicMapObject : "+((Locator)o.getSourceLocators().iterator().next()).getReference());
       }
        
       System.out.print("reifying ");
       printTopicNames(ocSydAirportHP.getReifier());
       System.out.println("reified size : "+tocSydAirportHP.getReified().size() + "\n");

        
       itR = tJL772.getReified().iterator();
       while( itR.hasNext() ) {
                TopicMapObject o = (TopicMapObject)itR.next();
                System.out.println("\nreified TopicMapObject : "+((Locator)o.getSourceLocators().iterator().next()).getReference());
       }
    
       System.out.print("reifying ");
       printTopicNames(assoc.getReifier());
       System.out.println("reified size : "+tJL772.getReified().size() + "\n");
		
	} // endmethod main
} // endclass TMAPITutorial
