Creating a topic map with topics and associations

This section contains a little example of how to create a topic map including two topics and an association. This example also prints the content of the topic map. This example can be found in the file examples/example1.java

Example 1. Creating a Topic Map

	[1]  TopicMapSystemFactory   tSystemFactory  =   TopicMapSystemFactory.newInstance();
	[2]  TopicMapSystem tSystem 	=	tSystemFactory.newTopicMapSystem();
	[3]  TopicMap tm 		=	tSystem.createTopicMap("http://www.tmapi.org/examples/exampletm");
    

In line 1 and 2 a TopicMapSystemFactory and a TopicMapSystem are instantiated. For a deeper look on whats happening here, please go to the the section called “TopicMapSystem and TopicMapSystemFactory” In line 3 a topic map with the baseLocator "http://www.tmapi.org/examples/exampletm" is created. Now you can work with this org.tmapi.core.TopicMap instance.

	[4]  Topic t1	= tm.createTopic();
	[5]  t1.createTopicName("hello",null);
	[6]  Topic t2	= tm.createTopic();
	[7]  t2.createTopicName("world",null);	
    

In line 4 a topic is being created inside the topic map. Line 5 adds the TopicName "hello". This topic name has no scope, which is indicated by a given null parameter to the method createTopicName(String Value, Set scope). If you want to scope a topic name, you have to give a set of topics as the second parameter. Lines 6 and 7 create a second topic.

        [8]  Association a   = tm.createAssociation();
        [9]  a.createAssociationRole(t1, null);
        [10] a.createAssociationRole(t2, null);    
    

In line 8 an org.tmapi.core.Association is created. Line 9 and 10 are adding association roles to this association. The null as second parameter to the method createAssociationRole(Topic player, Topic type) indicates that the created association role is untyped.

    
        [11]  Iterator itt = tm.getTopics().iterator();
        [12]  while(itt.hasNext()){
        [13]    Topic t     = (Topic)itt.next();
        [14]    printTopicNames(t);
        [15]  }
    

In line 11 we get an Iterator over all topics in the topic map. In the lines 12-15 we iterate over all topics and use the method printTopicNames(Topic t) to print all topic names of one topic. The implementation of this method can be seen in the examples/example1.java file.

    
        [16]  Iterator ita = tm.getAssociations().iterator();
        [17]  while(ita.hasNext()){
        [18]      Association as   = (Association)ita.next();
        [19]      java.util.Iterator itr = as.getAssociationRoles().iterator();
        [20]      while(itr.hasNext()){
        [21]          AssociationRole m = (AssociationRole)itr.next();
        [22]          printTopicNames(m.getPlayer());                
        [23]      }
        [24]  }     
    

In line 16 we get an Iterator over all associations in the topic map. Beginning in the line 17 we iterate over all associations. In line 19 we get an iterator over all association roles of this association. Beginning in line 20 we iterate over all association roles and use the method printTopicNames(Topic t) to print all topic names of the role playing topic.

This small example showed how to create a topic map with 2 topics, named "hello" and "world", and an association between these topics.

Now a TMAPI implementation has to be chosen in order to run this example. Because of the Lookup Procedure for TopicMapSystemFactory Implementations, you just have to add the Jar of the TMAPI implementation to the Classpath.

Procedure 1. Compile and execute the example

  1. Compiling

    Go to the examples directory and enter the following command

    javac example1.java -classpath "..\tmapi-1_0.jar"

  2. Execution with tinyTIM

    java -classpath ".;..\tmapi-1_0.jar;{tinyTIM_Directory}\tinyTIM-1_0.jar" example1

    The Classpath contains the current directory "." where the example1.class is located. It also contains the "tmapi-1_0.jar" from the parent directory. The last jar in the Classpath is the jar of the TMAPI implementation, in this case it is the "tinyTIM-1_0.jar" from the {tinyTIM_Directory}.

  3. Execution with TM4J

    java -classpath ".;..\tmapi-1_0.jar;{TM4J}\tm4j-tmapi-x.x.x.jar;{TM4J}\tm4j-x.x.x.jar;{TM4J}\resolver.jar;{TM4J}\commons-logging.jar;{TM4J}\mango.jar" example1

    The Classpath contains the current directory "." where the example1.class is located. It also contains the "tmapi-1_0.jar" from the parent directory. The remaining jars in the Classpath are the jars required for the TMAPI implementation. In the binary distribution of TM4J, all of these jars can be found in the lib directory of the TM4J distribution. In the source distribution, the files tm4j-tmapi-x.x.x.jar and tm4j-x.x.x.jar can be found in the build/lib directory after you have built the distribution.

The output of this example:

		Topics:
		Topic : hello
		Topic : world
		AssociationRole Player:
		Topic : hello
		Topic : world