1 /*
2 * The TM4J Software License
3 *
4 *
5 * Copyright (c) 2000, 2001, 2002 The TM4J Project. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 *
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the
17 * distribution.
18 *
19 * 3. The end-user documentation included with the redistribution,
20 * if any, must include the following acknowledgment:
21 * "This product includes software developed by
22 * The TM4J Project (http://sourceforge.net/projects/tm4j)
23 * Alternately, this acknowledgment may appear in the software itself,
24 * if and wherever such third-party acknowledgments normally appear.
25 *
26 * 4. The names "TM4J" and "The TM4J Project" must
27 * not be used to endorse or promote products derived from this
28 * software without prior written permission. For written
29 * permission, please contact kal@techquila.com.
30 *
31 * 5. Products derived from this software may not be called "TM4J",
32 * nor may "TM4J" appear in their name, without prior written
33 * permission of the TM4J Project.
34 *
35 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
36 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
37 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
38 * DISCLAIMED. IN NO EVENT SHALL THE TM4J PROJECT OR
39 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
42 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
43 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
44 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
45 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
46 * SUCH DAMAGE.
47 * ====================================================================
48 *
49 */
50
51 /*
52 * $Header: /cvsroot/tm4j/tm4j/examples/src/examples/indexes/IndexingExample.java,v 1.2 2003/01/09 20:07:13 kal_ahmed Exp $
53 */
54
55 package examples.indexes;
56
57 import org.tm4j.topicmap;
58 import org.tm4j.topicmap.index;
59 import org.tm4j.topicmap.index.basic;
60 import org.tm4j.net;
61 import org.tm4j.topicmap.utils.extractors.TopicNameExtractor;
62
63 import examples.ExampleBase;
64
65 import java.util.Collection;
66 import java.util.Iterator;
67
68 /**
69 * An example application showing how to use the indexing APIs of TM4J.
70 *
71 * <p>This application parses a topic map and prints out the topic, association
72 * and occurrence types found in the map and the number of instances of each.</p>
73 *
74 * <p>This application should be invoked as follows:</p>
75 *
76 * <pre>java examples.indexes.IndexingExample [<i>tm_file_name</i>]</pre>
77 *
78 * <p>Where <i>tm_file_name</i> is the file name of the topic map to be parsed. The
79 * topic map file must be in XTM syntax. If this parameter is not defined, <i>tm_file_name</i>
80 * defaults to <code>topicmaps/kings_and_queens.xtm</code> which should be a valid
81 * topic map file name if this application is run from the <code>examples</code> subdirectory of
82 * the TM4J installation.</p>
83 */
84 public class IndexingExample extends ExampleBase
85 {
86 private TopicMap m_topicMap;
87 private IndexManager m_ixMgr;
88 private TopicNameExtractor m_nameExtractor = new TopicNameExtractor();
89
90 public static void main(String[] args)
91 {
92 String tmSource = "topicmaps/kings_and_queens.xtm";
93 if (args.length > 0)
94 {
95 tmSource = args[0];
96 }
97
98 try
99 {
100 IndexingExample theApp = new IndexingExample(tmSource);
101 theApp.run();
102 }
103 catch(Exception ex)
104 {
105 System.out.println("Error running test: " + ex.toString());
106 ex.printStackTrace();
107 }
108 }
109
110 /**
111 * Constructor which initialises the application by parsing the
112 * topic map file specified by <code>tmSource</code> and getting
113 * the IndexManager for that topic map.
114 * @param tmSource the file name of the topic map to be parsed.
115 */
116 public IndexingExample(String tmSource)
117 {
118 // Load the topic map first
119 m_topicMap = readTopicMap(tmSource);
120
121 // Get the IndexManager which provides access to the indexes:
122 m_ixMgr = m_topicMap.getIndexManager();
123 }
124
125 public void run()
126 throws Exception
127 {
128 // Dump info:
129 dumpTopicTypes();
130 dumpAssociationTypes();
131 dumpOccurrenceTypes();
132 }
133
134 /**
135 * Prints out all topics which define the type of one or more other topics
136 * and the number of topics which are typed by each topic.
137 */
138 private void dumpTopicTypes()
139 throws IndexManagerException, IndexException
140 {
141 TopicTypesIndex tti = null;
142
143 try
144 {
145 tti = (TopicTypesIndex)m_ixMgr.getIndex(TopicTypesIndex.class);
146 }
147 catch(IndexManagerException ex)
148 {
149 System.out.println("Unable to get TopicTypesIndex: " + ex.getMessage());
150 throw ex;
151 }
152
153 if (tti == null) {
154 System.out.println("Back-end does not support the TopicTypesIndex.");
155 } else {
156
157 try
158 {
159 // Some index implementations are open immediately when they are created.
160 // Other index implementations require a call to open()
161 if (!tti.isOpen())
162 {
163 tti.open();
164 }
165
166 // Now use the index methods provided by the TopicTypesIndex interface
167 Collection topicTypes = tti.getTopicTypes();
168 System.out.println("\n" + topicTypes.size() + " topic types:");
169 Iterator it = topicTypes.iterator();
170 while (it.hasNext())
171 {
172 Topic topicType = (Topic)it.next();
173 String topicName = (String)m_nameExtractor.fn(topicType);
174 Collection instances = tti.getTopicsOfType(topicType);
175 System.out.println("Type " + topicName + " has " + instances.size() + " instances.");
176 }
177
178 }
179 catch(IndexException ex)
180 {
181 System.out.println("Error using index: " + ex.getMessage());
182 throw ex;
183 }
184 finally
185 {
186 // Final clean up to ensure that the index is properly closed.
187 if (tti.isOpen())
188 {
189 tti.close();
190 }
191 }
192 }
193 }
194
195 /**
196 * Prints out all association typing topics and the number of associations typed
197 * by each topic.
198 */
199 private void dumpAssociationTypes()
200 throws IndexException, IndexManagerException
201 {
202 AssociationTypesIndex ati = null;
203 try
204 {
205 ati = (AssociationTypesIndex)m_ixMgr.getIndex(AssociationTypesIndex.class);
206 }
207 catch(IndexManagerException ex)
208 {
209 System.out.println("Error getting the AssociationTypesIndex: " + ex.getMessage());
210 throw ex;
211 }
212
213 try
214 {
215 if (!ati.isOpen())
216 {
217 ati.open();
218 }
219 Collection assocTypes = ati.getAssociationTypes();
220 System.out.println("\n" + assocTypes.size() + " association types:");
221 Iterator it = assocTypes.iterator();
222 while (it.hasNext())
223 {
224 Topic assocType = (Topic)it.next();
225 Collection instances = ati.getAssociationsOfType(assocType);
226 System.out.println(" Association type: " + m_nameExtractor.fn(assocType) + " has " + instances.size() + " instances.");
227 }
228 }
229 catch(IndexException ex)
230 {
231 System.out.println("Error reading from AssociationTypesIndex: "+ ex.getMessage());
232 throw ex;
233 }
234 finally
235 {
236 if (ati.isOpen())
237 {
238 ati.close();
239 }
240 }
241 }
242
243 /**
244 * Prints out all occurrence typing topics and the number of occurrences typed
245 * by each topic.
246 */
247 private void dumpOccurrenceTypes()
248 throws Exception
249 {
250 OccurrenceTypesIndex oti = null;
251 try
252 {
253 oti = (OccurrenceTypesIndex)m_ixMgr.getIndex(OccurrenceTypesIndex.class);
254 }
255 catch(IndexManagerException ex)
256 {
257 System.out.println("Error getting the OccurrenceTypesIndex: " + ex.getMessage());
258 throw ex;
259 }
260
261 try
262 {
263 if (!oti.isOpen())
264 {
265 oti.open();
266 }
267 Collection occTypes = oti.getOccurrenceTypes();
268 System.out.println("\n" + occTypes.size() + " occurrence types:");
269 Iterator it = occTypes.iterator();
270 while (it.hasNext())
271 {
272 Topic occType = (Topic)it.next();
273 Collection instances = oti.getOccurrencesOfType(occType);
274 System.out.println(" Association type: " + m_nameExtractor.fn(occType) + " has " + instances.size() + " instances.");
275 }
276 }
277 catch(IndexException ex)
278 {
279 System.out.println("Error reading from AssociationTypesIndex: "+ ex.getMessage());
280 throw ex;
281 }
282 finally
283 {
284 if (oti.isOpen())
285 {
286 oti.close();
287 }
288 }
289 }
290 }
291
292 /*
293 * $Log: IndexingExample.java,v $
294 * Revision 1.2 2003/01/09 20:07:13 kal_ahmed
295 * Fixed typo causing compilation failure
296 *
297 * Revision 1.1 2003/01/07 17:25:25 kal_ahmed
298 * Added example of using the TM4J indexing APIs.
299 *
300 */