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