/* * LICENSE * ------------ * This file is distributed under the provisions of the TM4J license. See * http://tm4j.org/LICENSE.TXT for the details of this license. */ package org.tm4j.panckoucke.examples; import org.tm4j.panckoucke.abstraction.AbstractionContext; import org.tm4j.panckoucke.context.ContextFactory; import org.tm4j.panckoucke.context.PanckouckeContext; import org.tm4j.panckoucke.impl.abstraction.AbstractionContextImpl; import org.tm4j.panckoucke.impl.abstraction.ix.TopicIndexAbstractor; import org.tm4j.panckoucke.impl.context.ContextFactoryImpl; import org.tm4j.panckoucke.model.AMArc; import org.tm4j.panckoucke.model.AMMember; import org.tm4j.panckoucke.model.AMNode; import org.tm4j.panckoucke.model.AModel; import org.tm4j.panckoucke.model.AbstractorInstantiationException; import org.tm4j.panckoucke.model.ModelProvider; import org.tm4j.panckoucke.model.UnsupportedAMMemberClassException; import org.tm4j.panckoucke.notification.Notification; import org.tm4j.panckoucke.notification.NotificationListener; import org.tm4j.panckoucke.notification.NotificationRegistry; import org.tm4j.panckoucke.notification.NotificationType; import org.tm4j.panckoucke.notification.StateChangedNotification; import org.tm4j.panckoucke.store.ProviderReference; import org.tm4j.panckoucke.store.StoreManager; import org.tm4j.panckoucke.store.StoreState; import org.tm4j.panckoucke.store.TopicMapReference; import java.util.Iterator; import java.util.Properties; /** * @author cf * Example, how to open a TopicMap with panckoucke */ public class ExampleOpenTopicMap implements NotificationListener { static boolean ok_to_exit = false; // String path_to_topicmap = " insert/path/to/topicmap"; protected final PanckouckeContext panckouckeContext; /** * - initializes panckoucke * - registering this instance as NotificationListener * - creating a provider * - start loading a topicmap */ public ExampleOpenTopicMap(String path_to_topicmap) throws Exception { // ---------------------------------------- // Initializes PanckouckeContext // ---------------------------------------- // instantiate a factory ContextFactory f = new ContextFactoryImpl(); // get the default context panckouckeContext = f.createContext(); // get the StoreManager StoreManager storeMgr = panckouckeContext.getStoreManager(); // ---------------------------------------- // Register for notifications // ---------------------------------------- NotificationRegistry nr = panckouckeContext.getNotificationRegistry(); nr.addListener(this, NotificationType.TM_STATE_CHANGED); // ---------------------------------------- // Create an in memory provider // ---------------------------------------- String providerFactory = "org.tm4j.topicmap.memory.TopicMapProviderFactoryImpl"; Properties props = new Properties(); ProviderReference provRef = storeMgr.createProvider(providerFactory, props); // ---------------------------------------- // Demand to open a topicmap // ---------------------------------------- TopicMapReference tmRef = storeMgr.loadTopicmap(path_to_topicmap, provRef); System.out.println("Current State of topicmap: " + tmRef.getStateProse() + "(" + tmRef.getState() + ")"); } /** * Panckoucke has something to tell */ public void panckouckeNotifies(Notification notification) { // since we are listening only to TM_STATE_CHANGED notifications // the following test is unnecessary. But for demonstration purposes... NotificationType type = notification.getType(); if (type == NotificationType.TM_STATE_CHANGED) { StateChangedNotification scn = (StateChangedNotification) notification.getRelatedObject(); StoreState newState = scn.getNewState(); System.out.println("Changed state"); System.out.println("from: " + scn.getOldState()); System.out.println("to: " + newState); System.out.println("for Topicmap: " + scn.getTopicmapRef()); if (newState.equals(StoreState.ACCESSIBLE)) { // quit demonstration getTopicsOfMap(scn.getTopicmapRef()); ok_to_exit = true; } else if (newState.equals(StoreState.CORRUPTED)) { // log error msg System.out.println("Cause: " + scn.getTopicmapRef().getStateProse()); // quit demonstration ok_to_exit = true; } } } /** * uses the {@link org.tm4j.panckoucke.impl.abstraction.ix.TopicIndexAbstractor} * to get a model that contains all topics of the given map. * @param tmr */ private void getTopicsOfMap(TopicMapReference tmr) { if (StoreState.ACCESSIBLE.equals(tmr.getState())) { ModelProvider mProvider = panckouckeContext.getModelProvider(); AbstractionContext ac = new AbstractionContextImpl(); String classname = TopicIndexAbstractor.class.getName(); AMMember nodeForMap = tmr.getAMNode(); try { AModel firstModel = mProvider.getModel(nodeForMap, classname, ac); render(firstModel); } catch (UnsupportedAMMemberClassException e) { e.printStackTrace(); } catch (AbstractorInstantiationException e) { e.printStackTrace(); } } } /** * Performs simple renderering. * lists all endnodes * of all outgoing arcs * of the centerNode * of the given model * @param args */ private void render(AModel model) { AMNode center = model.getCenterNode(); Iterator arcs = center.getArcs().iterator(); while (arcs.hasNext()) { AMArc arc = (AMArc) arcs.next(); Iterator endnodes = arc.getEndNodes().iterator(); while (endnodes.hasNext()) { AMNode node = (AMNode) endnodes.next(); System.out.println(node.getLabel()); } } } public static void main(String[] args) { try { // start the example if (args.length < 1) { System.out.println("Please supply the path to an xtm-file as the first paramter"); System.exit(1); } new ExampleOpenTopicMap(args[0]); // wait until loading is done while (!ok_to_exit) { try { Thread.sleep(200); } catch (InterruptedException e) { System.exit(1); } } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }