1   /*
2    *  Scratch.java
3    *
4    *  Copyright (c) 1998-2004, 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.82 2004/12/05 21:47:38 valyt Exp $
14   */
15  
16  
17  package gate.util;
18  
19  import java.awt.Color;
20  import java.io.*;
21  import java.net.URL;
22  import java.util.*;
23  import java.util.prefs.Preferences;
24  import java.util.zip.GZIPInputStream;
25  import java.util.zip.GZIPOutputStream;
26  
27  import javax.swing.UIManager;
28  
29  import gate.*;
30  import gate.creole.ANNIEConstants;
31  import gate.creole.Transducer;
32  import gate.creole.gazetteer.DefaultGazetteer;
33  import gate.creole.ir.*;
34  import gate.creole.tokeniser.DefaultTokeniser;
35  import gate.gui.MainFrame;
36  import gate.gui.docview.AnnotationSetsView;
37  import gate.persist.SerialDataStore;
38  
39  /** A scratch pad for experimenting.
40    */
41  public class Scratch
42  {
43    /** Debug flag */
44    private static final boolean DEBUG = false;
45  
46    public static void main(String args[]) throws Exception {
47      File file = new File("Z:/gate/bin");
48      System.out.println("Canonical path: " + file.getCanonicalPath());
49      System.out.println("URL: " + file.toURL());
50      
51      URL url = new URL("jar:file:/Z:/gate/bin/gate.jar!/gate/Gate.class");
52      System.out.println(url);
53      System.out.println("Path: " + url.getPath());
54      System.out.println("File: " + url.getFile());
55      System.out.println("Host: " + url.getHost());
56      System.out.println("Proto: " + url.getProtocol());
57      
58      url = Thread.currentThread().getContextClassLoader().
59        getResource("gate/Gate.class");
60      System.out.println(url);
61      System.out.println("Path: " + url.getPath());
62      System.out.println("File: " + url.getFile());
63      System.out.println("Host: " + url.getHost());
64      System.out.println("Proto: " + url.getProtocol());
65      
66      Map defaultsMap = UIManager.getLookAndFeelDefaults();
67      System.out.println(defaultsMap.keySet());
68  
69      
70      //test for a bug reported by Luc Plamondon
71      
72      Gate.init();
73      Document doc = Factory.newDocument("ala bala portocala");
74      AnnotationSet set = doc.getAnnotations();
75      Integer annId = 
76        set.add(new Long(3), new Long(5), "FooBar", Factory.newFeatureMap());
77      Annotation ann = set.get(annId);
78      //remove the annotation 
79      set.remove(ann);
80      
81      AnnotationSet resSet = set.get(new Long(0), new Long(10));
82      
83      //this set is empty so the bug was fixed.
84      System.out.println(resSet);
85      
86      System.out.println("==============================================");
87      
88      
89      Map listsMap = new HashMap();
90      listsMap.put("blah", new ArrayList());
91      List theList = (List)listsMap.get("blah");
92      System.out.println(theList);
93      theList.add("object");
94      theList = (List)listsMap.get("blah");
95      System.out.println(theList);
96      
97      
98      
99      File home = new File("z:/gate/plugins");
100     File tok = new File(home, "ANNIE/resources/tokeniser/Default.rul");
101     System.out.println(tok);
102     
103     Preferences prefRoot = Preferences.userNodeForPackage(AnnotationSetsView.class);
104     System.out.println(prefRoot.keys().length);
105     prefRoot.removeNode();
106     prefRoot = Preferences.userNodeForPackage(AnnotationSetsView.class);
107     System.out.println(prefRoot.keys().length);
108     Color col = new Color(100, 101, 102, 103);
109     int rgb = col.getRGB();
110     int alpha = col.getAlpha();
111     int rgba = rgb | (alpha << 24);
112     Color col1 = new Color(rgba, true);
113     System.out.println(col + " a: " + col.getAlpha());
114     System.out.println(col1+ " a: " + col1.getAlpha());
115     System.out.println(col.equals(col1));
116 //    Map defaultsMap = UIManager.getLookAndFeelDefaults();
117 //    System.out.println(defaultsMap.keySet());
118     
119     
120 //    double a = 16.99;
121 //    double b = 9.99;
122 //    double c = a - b;
123 //    System.out.println(c);
124 
125 //    Runtime.getRuntime().exec(new String[]{"cmd",
126 //                                           "C:\\Program Files\\GATE 2.2\\bin\\gate.bat"},
127 //                              null,
128 //                              new File("C:\\Program Files\\GATE 2.2\\bin"));
129 
130 //    Gate.init();
131 //    Document doc = Factory.newDocument("The quick brown fox jumped over the lazy dog");
132 //    AnnotationSet annSet1 = doc.getAnnotations("Set1");
133 //    annSet1.add(new Long(1), new Long(5), "Foo", Factory.newFeatureMap());
134 //
135 //    AnnotationSet annSet2 = doc.getAnnotations("Set2");
136 //    annSet2.add(new Long(1), new Long(5), "Bar", Factory.newFeatureMap());
137 //    annSet2.addAll(annSet1);
138 //
139 //    List annotations = new ArrayList(annSet2);
140 //    Collections.sort(annotations, new OffsetComparator());
141 //    Iterator annIter = annotations.iterator();
142 //    while(annIter.hasNext()){
143 //      Annotation ann =(Annotation)annIter.next();
144 //      System.out.print("Start node: ID = " + ann.getStartNode().getId());
145 //      System.out.println(" Offset = " + ann.getStartNode().getOffset());
146 //      System.out.print("End node: ID = " + ann.getEndNode().getId());
147 //      System.out.println(" Offset = " + ann.getEndNode().getOffset());
148 //
149 //    }
150 //    File tempFile = File.createTempFile("gaga", "");
151 //    tempFile.delete();
152 //    tempFile.mkdir();
153 //    tempFile.deleteOnExit();
154 //    File tempFile2 = File.createTempFile("fil", ".tmp", tempFile);
155 //    tempFile2.deleteOnExit();
156 //System.out.println(tempFile.getCanonicalPath());
157 //    Thread.sleep(100000);
158 //
159 //    Map charsets = java.nio.charset.Charset.availableCharsets();
160 //    Iterator namesIter = charsets.keySet().iterator();
161 //    while(namesIter.hasNext()){
162 //      String name = (String)namesIter.next();
163 //      System.out.println(name + " : " + charsets.get(name));
164 //    }
165 //    System.out.println(System.getProperty("file.encoding"));
166 //    System.out.println(java.nio.charset.Charset.forName(System.getProperty("file.encoding")).name());
167 //    System.out.println(new Character((char)0xa3));
168 //    Gate.init();
169 //
170 //    List classes = Tools.findSubclasses(gate.creole.ir.Search.class);
171 //    if(classes != null) for(int i = 0; i < classes.size(); i++){
172 //      Out.prln(classes.get(i).toString());
173 //    }
174 //    createIndex();
175 //    URL anURL = new URL("file:/z:/a/b/c/d.txt");
176 //    URL anotherURL = new URL("file:/z:/a/b/c/d.txt");
177 //    String relPath = gate.util.persistence.PersistenceManager.
178 //                     getRelativePath(anURL, anotherURL);
179 //    Out.prln("Context: " + anURL);
180 //    Out.prln("Target: " + anotherURL);
181 //    Out.prln("Relative path: " + relPath);
182 //    Out.prln("Result " + new URL(anURL, relPath));
183 //    javax.swing.text.FlowView fv;
184 //    javax.swing.UIManager.setLookAndFeel(javax.swing.UIManager.getSystemLookAndFeelClassName());
185 //    Map uidefaults  = (Map)javax.swing.UIManager.getDefaults();
186 //    List keys = new ArrayList(uidefaults.keySet());
187 //    Collections.sort(keys);
188 //    Iterator keyIter = keys.iterator();
189 //    while(keyIter.hasNext()){
190 //      Object key = keyIter.next();
191 //      System.out.println(key + " : " + uidefaults.get(key));
192 //    }
193 
194     // initialise the thing
195 //    Gate.setNetConnected(false);
196 //    Gate.setLocalWebServer(false);
197 //    Gate.init();
198 
199 //    Scratch oneOfMe = new Scratch();
200 //    try{
201 //      oneOfMe.runNerc();
202 //    } catch (Exception e) {
203 //      e.printStackTrace(Out.getPrintWriter());
204 //    }
205 
206 
207 //    CreoleRegister reg = Gate.getCreoleRegister();
208 //System.out.println("Instances for " + reg.getLrInstances("gate.creole.AnnotationSchema"));
209 //System.out.println("Instances for " + reg.getAllInstances ("gate.creole.AnnotationSchema"));
210 
211 //System.out.println("VRs for " + reg.getAnnotationVRs("Tree"));
212 //System.out.println("VRs for " + reg.getAnnotationVRs());
213 
214 //System.out.println(reg.getLargeVRsForResource("gate.corpora.DocumentImpl"));
215 
216   } // main
217 
218   /** Example of using an exit-time hook. */
219   public static void exitTimeHook() {
220     Runtime.getRuntime().addShutdownHook(new Thread() {
221       public void run() {
222         System.out.println("shutting down");
223         System.out.flush();
224 
225         // create a File to store the state in
226         File stateFile = new File("z:\\tmp", "GateGuiState.gzsr");
227 
228         // dump the state into the new File
229         try {
230           ObjectOutputStream oos = new ObjectOutputStream(
231             new GZIPOutputStream(new FileOutputStream(stateFile))
232           );
233           System.out.println("writing main frame");
234           System.out.flush();
235           oos.writeObject(Main.getMainFrame());
236           oos.close();
237         } catch(Exception e) {
238           System.out.println("Couldn't write to state file: " + e);
239         }
240 
241         System.out.println("done");
242         System.out.flush();
243       }
244     });
245   } // exitTimeHook()
246 
247   /**
248    * ***** <B>Failed</B> *****
249    * attempt to serialise whole gui state - various swing components
250    * don't like to be serialised :-(. might be worth trying again when
251    * jdk1.4 arrives.
252    */
253   public static void dumpGuiState() {
254     System.out.println("dumping gui state...");
255     System.out.flush();
256 
257     // create a File to store the state in
258     File stateFile = new File("z:\\tmp", "GateGuiState.gzsr");
259 
260     // dump the state into the new File
261     try {
262       ObjectOutputStream oos = new ObjectOutputStream(
263         new GZIPOutputStream(new FileOutputStream(stateFile))
264       );
265       MainFrame mf = Main.getMainFrame();
266 
267       // wait for 1 sec
268       long startTime = System.currentTimeMillis();
269       long timeNow = System.currentTimeMillis();
270       while(timeNow - startTime < 3000){
271         try {
272           Thread.sleep(150);
273           timeNow = System.currentTimeMillis();
274         } catch(InterruptedException ie) {}
275       }
276 
277       System.out.println("writing main frame");
278       System.out.flush();
279       oos.writeObject(mf);
280       oos.close();
281     } catch(Exception e) {
282       System.out.println("Couldn't write to state file: " + e);
283     }
284 
285     System.out.println("...done gui dump");
286     System.out.flush();
287   } // dumpGuiState
288 
289   /**
290    * Run NERC and print out the various stages (doesn't actually
291    * use Nerc but the individual bits), and serialise then deserialise
292    * the NERC system.
293    */
294   public void runNerc() throws Exception {
295     long startTime = System.currentTimeMillis();
296 
297     Out.prln("gate init");
298     Gate.setLocalWebServer(false);
299     Gate.setNetConnected(false);
300     Gate.init();
301 
302     Out.prln((System.currentTimeMillis() - startTime) / 1000.0 + " seconds");
303     Out.prln("creating resources");
304 
305     // a controller
306     Controller c1 = (Controller) Factory.createResource(
307       "gate.creole.SerialController",
308       Factory.newFeatureMap()
309     );
310     c1.setName("Scratch controller");
311 
312     //get a document
313     FeatureMap params = Factory.newFeatureMap();
314     params.put(Document.DOCUMENT_URL_PARAMETER_NAME, Gate.getUrl("tests/doc0.html"));
315     params.put(Document.DOCUMENT_MARKUP_AWARE_PARAMETER_NAME, "false");
316     Document doc = (Document)Factory.createResource("gate.corpora.DocumentImpl",
317                                                     params);
318 
319     //create a default tokeniser
320     params = Factory.newFeatureMap();
321     params.put(DefaultTokeniser.DEF_TOK_TOKRULES_URL_PARAMETER_NAME,
322       "gate:/creole/tokeniser/DefaultTokeniser.rules");
323     params.put(DefaultTokeniser.DEF_TOK_ENCODING_PARAMETER_NAME, "UTF-8");
324     params.put(DefaultTokeniser.DEF_TOK_DOCUMENT_PARAMETER_NAME, doc);
325     ProcessingResource tokeniser = (ProcessingResource) Factory.createResource(
326       "gate.creole.tokeniser.DefaultTokeniser", params
327     );
328 
329     //create a default gazetteer
330     params = Factory.newFeatureMap();
331     params.put(DefaultGazetteer.DEF_GAZ_DOCUMENT_PARAMETER_NAME, doc);
332     params.put(DefaultGazetteer.DEF_GAZ_LISTS_URL_PARAMETER_NAME,
333       "gate:/creole/gazeteer/default/lists.def");
334     ProcessingResource gaz = (ProcessingResource) Factory.createResource(
335       "gate.creole.gazetteer.DefaultGazetteer", params
336     );
337 
338     //create a default transducer
339     params = Factory.newFeatureMap();
340     params.put(Transducer.TRANSD_DOCUMENT_PARAMETER_NAME, doc);
341     //params.put("grammarURL", new File("z:\\tmp\\main.jape").toURL());
342     ProcessingResource trans = (ProcessingResource) Factory.createResource(
343       "gate.creole.Transducer", params
344     );
345 
346     // get the controller to encapsulate the tok and gaz
347     c1.getPRs().add(tokeniser);
348     c1.getPRs().add(gaz);
349     c1.getPRs().add(trans);
350 
351     Out.prln((System.currentTimeMillis() - startTime) / 1000.0 + " seconds");
352     Out.prln("dumping state");
353 
354     // create a File to store the state in
355     File stateFile = new File("z:\\tmp", "SerialisedGateState.gzsr");
356 
357     // dump the state into the new File
358     try {
359       ObjectOutputStream oos = new ObjectOutputStream(
360         new GZIPOutputStream(new FileOutputStream(stateFile))
361       );
362       oos.writeObject(new SessionState());
363       oos.close();
364     } catch(IOException e) {
365       throw new GateException("Couldn't write to state file: " + e);
366     }
367 
368     Out.prln(System.getProperty("user.home"));
369 
370     Out.prln((System.currentTimeMillis() - startTime) / 1000.0 + " seconds");
371     Out.prln("reinstating");
372 
373     try {
374       FileInputStream fis = new FileInputStream(stateFile);
375       GZIPInputStream zis = new GZIPInputStream(fis);
376       ObjectInputStream ois = new ObjectInputStream(zis);
377       SessionState state = (SessionState) ois.readObject();
378       ois.close();
379     } catch(IOException e) {
380       throw
381         new GateException("Couldn't read file "+stateFile+": "+e);
382     } catch(ClassNotFoundException ee) {
383       throw
384         new GateException("Couldn't find class: "+ee);
385     }
386 
387     Out.prln((System.currentTimeMillis() - startTime) / 1000.0 + " seconds");
388     Out.prln("done");
389   } // runNerc()
390 
391 
392   /** Inner class for holding CR and DSR for serialisation experiments */
393   class SessionState implements Serializable {
394     SessionState() {
395       cr = Gate.getCreoleRegister();
396       dsr = Gate.getDataStoreRegister();
397     }
398 
399     CreoleRegister cr;
400 
401     DataStoreRegister dsr;
402 
403     // other state from Gate? and elsewhere?
404   } // SessionState
405 
406   /** Generate a random integer for file naming. */
407   protected static int random() {
408     return randomiser.nextInt(9999);
409   } // random
410 
411   /**
412    * Generates an index for a corpus in a datastore on Valy's computer in order
413    * to have some test data.
414    */
415   public static void createIndex() throws Exception{
416     String dsURLString = "file:///d:/temp/ds";
417     String indexLocation = "d:/temp/ds.idx";
418 
419     Gate.init();
420 
421     //open the datastore
422     SerialDataStore sds = (SerialDataStore)Factory.openDataStore(
423                             "gate.persist.SerialDataStore", dsURLString);
424     sds.open();
425     List corporaIds = sds.getLrIds("gate.corpora.SerialCorpusImpl");
426     IndexedCorpus corpus = (IndexedCorpus)
427                            sds.getLr("gate.corpora.SerialCorpusImpl",
428 
429                                      corporaIds.get(0));
430     DefaultIndexDefinition did = new DefaultIndexDefinition();
431     did.setIrEngineClassName(gate.creole.ir.lucene.
432                              LuceneIREngine.class.getName());
433 
434     did.setIndexLocation(indexLocation);
435     did.addIndexField(new IndexField("body", new ContentPropertyReader(), false));
436 
437     corpus.setIndexDefinition(did);
438 
439     Out.prln("removing old index");
440     corpus.getIndexManager().deleteIndex();
441     Out.prln("building new index");
442     corpus.getIndexManager().createIndex();
443     Out.prln("optimising new index");
444     corpus.getIndexManager().optimizeIndex();
445     Out.prln("saving corpus");
446     sds.sync(corpus);
447     Out.prln("done!");
448   }
449 
450   /**
451    *
452    * @param file a TXT file containing the text
453    */
454   public static void tokeniseFile(File file) throws Exception{
455     //initialise GATE (only call it once!!)
456     Gate.init();
457     //create the document
458     Document doc = Factory.newDocument(file.toURL());
459     //create the tokeniser
460     DefaultTokeniser tokeniser = (DefaultTokeniser)Factory.createResource(
461       "gate.creole.tokeniser.DefaultTokeniser");
462 
463     //tokenise the document
464     tokeniser.setParameterValue(DefaultTokeniser.DEF_TOK_DOCUMENT_PARAMETER_NAME, doc);
465     tokeniser.execute();
466 
467     //extract data from document
468     //we need tokens and spaces
469     Set annotationTypes = new HashSet();
470     annotationTypes.add(ANNIEConstants.TOKEN_ANNOTATION_TYPE);
471     annotationTypes.add(ANNIEConstants.SPACE_TOKEN_ANNOTATION_TYPE);
472 
473     List tokenList = new ArrayList(doc.getAnnotations().get(annotationTypes));
474     Collections.sort(tokenList, new OffsetComparator());
475 
476     //iterate through the tokens
477     Iterator tokIter = tokenList.iterator();
478     while(tokIter.hasNext()){
479       Annotation anAnnotation = (Annotation)tokIter.next();
480       System.out.println("Annotation: (" +
481                         anAnnotation.getStartNode().getOffset().toString() +
482                         ", " + anAnnotation.getEndNode().getOffset().toString() +
483                         "[type: " + anAnnotation.getType() +
484                          ", features: " + anAnnotation.getFeatures().toString()+
485                          "]" );
486     }
487   }
488 
489 
490   public static class ContentPropertyReader implements PropertyReader{
491     public String getPropertyValue(gate.Document doc){
492       return doc.getContent().toString();
493     }
494   }
495 
496   /** Random number generator */
497   protected static Random randomiser = new Random();
498 
499 } // class Scratch
500 
501 
502