|
Scratch |
|
1 /* 2 * Scratch.java 3 * 4 * Copyright (c) 1998-2001, The University of Sheffield. 5 * 6 * This file is part of GATE (see http://gate.ac.uk/), and is free 7 * software, licenced under the GNU Library General Public License, 8 * Version 2, June 1991 (in the distribution as file licence.html, 9 * and also available at http://gate.ac.uk/gate/licence.html). 10 * 11 * Hamish Cunningham, 22/03/00 12 * 13 * $Id: Scratch.java,v 1.48 2001/11/09 16:14:55 valyt Exp $ 14 */ 15 16 17 package gate.util; 18 19 import java.util.*; 20 import java.net.*; 21 import java.io.*; 22 import java.util.zip.*; 23 24 import gate.*; 25 import gate.creole.*; 26 import gate.gui.*; 27 28 import org.xml.sax.*; 29 import javax.xml.parsers.*; 30 import org.w3c.www.mime.*; 31 32 /** A scratch pad for experimenting. 33 */ 34 public class Scratch 35 { 36 /** Debug flag */ 37 private static final boolean DEBUG = false; 38 39 public static void main(String args[]) throws Exception { 40 41 42 // javax.swing.UIManager.setLookAndFeel(javax.swing.UIManager.getSystemLookAndFeelClassName()); 43 // Map uidefaults = (Map)javax.swing.UIManager.getDefaults(); 44 // List keys = new ArrayList(uidefaults.keySet()); 45 // Collections.sort(keys); 46 // Iterator keyIter = keys.iterator(); 47 // while(keyIter.hasNext()){ 48 // Object key = keyIter.next(); 49 // System.out.println(key + " : " + uidefaults.get(key)); 50 // } 51 52 // initialise the thing 53 // Gate.setNetConnected(false); 54 // Gate.setLocalWebServer(false); 55 // Gate.init(); 56 57 // Scratch oneOfMe = new Scratch(); 58 // try{ 59 // oneOfMe.runNerc(); 60 // } catch (Exception e) { 61 // e.printStackTrace(Out.getPrintWriter()); 62 // } 63 64 65 // CreoleRegister reg = Gate.getCreoleRegister(); 66 //System.out.println("Instances for " + reg.getLrInstances("gate.creole.AnnotationSchema")); 67 //System.out.println("Instances for " + reg.getAllInstances ("gate.creole.AnnotationSchema")); 68 69 //System.out.println("VRs for " + reg.getAnnotationVRs("Tree")); 70 //System.out.println("VRs for " + reg.getAnnotationVRs()); 71 72 //System.out.println(reg.getLargeVRsForResource("gate.corpora.DocumentImpl")); 73 74 } // main 75 76 /** Example of using an exit-time hook. */ 77 public static void exitTimeHook() { 78 Runtime.getRuntime().addShutdownHook(new Thread() { 79 public void run() { 80 System.out.println("shutting down"); 81 System.out.flush(); 82 83 // create a File to store the state in 84 File stateFile = new File("z:\\tmp", "GateGuiState.gzsr"); 85 86 // dump the state into the new File 87 try { 88 ObjectOutputStream oos = new ObjectOutputStream( 89 new GZIPOutputStream(new FileOutputStream(stateFile)) 90 ); 91 System.out.println("writing main frame"); 92 System.out.flush(); 93 oos.writeObject(Main.getMainFrame()); 94 oos.close(); 95 } catch(Exception e) { 96 System.out.println("Couldn't write to state file: " + e); 97 } 98 99 System.out.println("done"); 100 System.out.flush(); 101 } 102 }); 103 } // exitTimeHook() 104 105 /** 106 * ***** <B>Failed</B> ***** 107 * attempt to serialise whole gui state - various swing components 108 * don't like to be serialised :-(. might be worth trying again when 109 * jdk1.4 arrives. 110 */ 111 public static void dumpGuiState() { 112 System.out.println("dumping gui state..."); 113 System.out.flush(); 114 115 // create a File to store the state in 116 File stateFile = new File("z:\\tmp", "GateGuiState.gzsr"); 117 118 // dump the state into the new File 119 try { 120 ObjectOutputStream oos = new ObjectOutputStream( 121 new GZIPOutputStream(new FileOutputStream(stateFile)) 122 ); 123 MainFrame mf = Main.getMainFrame(); 124 125 // wait for 1 sec 126 long startTime = System.currentTimeMillis(); 127 long timeNow = System.currentTimeMillis(); 128 while(timeNow - startTime < 3000){ 129 try { 130 Thread.sleep(150); 131 timeNow = System.currentTimeMillis(); 132 } catch(InterruptedException ie) {} 133 } 134 135 System.out.println("writing main frame"); 136 System.out.flush(); 137 oos.writeObject(mf); 138 oos.close(); 139 } catch(Exception e) { 140 System.out.println("Couldn't write to state file: " + e); 141 } 142 143 System.out.println("...done gui dump"); 144 System.out.flush(); 145 } // dumpGuiState 146 147 /** 148 * Run NERC and print out the various stages (doesn't actually 149 * use Nerc but the individual bits), and serialise then deserialise 150 * the NERC system. 151 */ 152 public void runNerc() throws Exception { 153 long startTime = System.currentTimeMillis(); 154 155 Out.prln("gate init"); 156 Gate.setLocalWebServer(false); 157 Gate.setNetConnected(false); 158 Gate.init(); 159 160 Out.prln((System.currentTimeMillis() - startTime) / 1000.0 + " seconds"); 161 Out.prln("creating resources"); 162 163 // a controller 164 Controller c1 = (Controller) Factory.createResource( 165 "gate.creole.SerialController", 166 Factory.newFeatureMap() 167 ); 168 c1.setName("Scratch controller"); 169 170 //get a document 171 FeatureMap params = Factory.newFeatureMap(); 172 params.put("sourceUrl", Gate.getUrl("tests/doc0.html")); 173 // params.put("sourceUrl", new File("z:\\tmp\\zxc.txt").toURL()); 174 params.put("markupAware", "false"); 175 Document doc = (Document)Factory.createResource("gate.corpora.DocumentImpl", 176 params); 177 178 //create a default tokeniser 179 params = Factory.newFeatureMap(); 180 params.put("rulesURL", "gate:/creole/tokeniser/DefaultTokeniser.rules"); 181 params.put("encoding", "UTF-8"); 182 params.put("document", doc); 183 ProcessingResource tokeniser = (ProcessingResource) Factory.createResource( 184 "gate.creole.tokeniser.DefaultTokeniser", params 185 ); 186 187 //create a default gazetteer 188 params = Factory.newFeatureMap(); 189 params.put("document", doc); 190 params.put("listsURL", "gate:/creole/gazeteer/default/lists.def"); 191 ProcessingResource gaz = (ProcessingResource) Factory.createResource( 192 "gate.creole.gazetteer.DefaultGazetteer", params 193 ); 194 195 //create a default transducer 196 params = Factory.newFeatureMap(); 197 params.put("document", doc); 198 //params.put("grammarURL", new File("z:\\tmp\\main.jape").toURL()); 199 ProcessingResource trans = (ProcessingResource) Factory.createResource( 200 "gate.creole.Transducer", params 201 ); 202 203 // get the controller to encapsulate the tok and gaz 204 c1.getPRs().add(tokeniser); 205 c1.getPRs().add(gaz); 206 c1.getPRs().add(trans); 207 208 Out.prln((System.currentTimeMillis() - startTime) / 1000.0 + " seconds"); 209 Out.prln("dumping state"); 210 211 // create a File to store the state in 212 File stateFile = new File("z:\\tmp", "SerialisedGateState.gzsr"); 213 214 // dump the state into the new File 215 try { 216 ObjectOutputStream oos = new ObjectOutputStream( 217 new GZIPOutputStream(new FileOutputStream(stateFile)) 218 ); 219 oos.writeObject(new SessionState()); 220 oos.close(); 221 } catch(IOException e) { 222 throw new GateException("Couldn't write to state file: " + e); 223 } 224 225 Out.prln(System.getProperty("user.home")); 226 227 Out.prln((System.currentTimeMillis() - startTime) / 1000.0 + " seconds"); 228 Out.prln("reinstating"); 229 230 try { 231 FileInputStream fis = new FileInputStream(stateFile); 232 GZIPInputStream zis = new GZIPInputStream(fis); 233 ObjectInputStream ois = new ObjectInputStream(zis); 234 SessionState state = (SessionState) ois.readObject(); 235 ois.close(); 236 } catch(IOException e) { 237 throw 238 new GateException("Couldn't read file "+stateFile+": "+e); 239 } catch(ClassNotFoundException ee) { 240 throw 241 new GateException("Couldn't find class: "+ee); 242 } 243 244 Out.prln((System.currentTimeMillis() - startTime) / 1000.0 + " seconds"); 245 Out.prln("done"); 246 } // runNerc() 247 248 249 /** Inner class for holding CR and DSR for serialisation experiments */ 250 class SessionState implements Serializable { 251 SessionState() { 252 cr = Gate.getCreoleRegister(); 253 dsr = Gate.getDataStoreRegister(); 254 } 255 256 CreoleRegister cr; 257 258 DataStoreRegister dsr; 259 260 // other state from Gate? and elsewhere? 261 } // SessionState 262 263 /** Generate a random integer for file naming. */ 264 protected static int random() { 265 return randomiser.nextInt(9999); 266 } // random 267 268 /** Random number generator */ 269 protected static Random randomiser = new Random(); 270 271 } // class Scratch 272 273
|
Scratch |
|