1   /*
2    *  Copyright (c) 1998-2001, The University of Sheffield.
3    *
4    *  This file is part of GATE (see http://gate.ac.uk/), and is free
5    *  software, licenced under the GNU Library General Public License,
6    *  Version 2, June 1991 (in the distribution as file licence.html,
7    *  and also available at http://gate.ac.uk/gate/licence.html).
8    *
9    *  Valentin Tablan, 01 Feb 2000
10   *
11   *  $Id: Transducer.java,v 1.18 2002/03/06 17:15:40 kalina Exp $
12   */
13  
14  package gate.creole;
15  
16  import gate.creole.*;
17  import gate.*;
18  import gate.util.*;
19  import gate.jape.*;
20  
21  import java.net.*;
22  import gate.event.*;
23  import java.util.*;
24  import java.io.*;
25  
26  /**
27   * A cascaded multi-phase transducer using the Jape language which is a
28   * variant of the CPSL language.
29   */
30  public class Transducer extends AbstractLanguageAnalyser {
31  
32    public static final String
33      TRANSD_DOCUMENT_PARAMETER_NAME = "document";
34  
35    public static final String
36      TRANSD_INPUT_AS_PARAMETER_NAME = "inputASName";
37  
38    public static final String
39      TRANSD_OUTPUT_AS_PARAMETER_NAME = "outputASName";
40  
41    public static final String
42      TRANSD_ENCODING_PARAMETER_NAME = "encoding";
43  
44    public static final String
45      TRANSD_GRAMMAR_URL_PARAMETER_NAME = "grammarURL";
46  
47    /**
48     * Default constructor. Does nothing apart from calling the default
49     * constructor from the super class. The actual object initialisation is done
50     * via the {@link #init} method.
51     */
52    public Transducer() {
53    }
54  
55    /*
56    private void writeObject(ObjectOutputStream oos) throws IOException {
57      Out.prln("writing transducer");
58      oos.defaultWriteObject();
59      Out.prln("finished writing transducer");
60    } // writeObject
61    */
62  
63    /**
64     * This method is the one responsible for initialising the transducer. It
65     * assumes that all the needed parameters have been already set using the
66     * appropiate setXXX() methods.
67     *@return a reference to <b>this</b>
68     */
69    public Resource init() throws ResourceInstantiationException {
70      if(grammarURL != null && encoding != null){
71        try{
72          fireProgressChanged(0);
73          batch = new Batch(grammarURL, encoding, new InternalStatusListener());
74          fireProcessFinished();
75        }catch(JapeException je){
76          throw new ResourceInstantiationException(je);
77        }
78      } else
79        throw new ResourceInstantiationException (
80          "Both the URL (was " + grammarURL + ") and the encoding (was " +
81          encoding + ") are needed to create a JapeTransducer!"
82        );
83  
84        batch.addProgressListener(new IntervalProgressListener(0, 100));
85  
86      return this;
87    }
88  
89    /**
90     * Implementation of the run() method from {@link java.lang.Runnable}.
91     * This method is responsible for doing all the processing of the input
92     * document.
93     */
94    public void execute() throws ExecutionException{
95      interrupted = false;
96      if(document == null) throw new ExecutionException("No document provided!");
97      if(inputASName != null && inputASName.equals("")) inputASName = null;
98      if(outputASName != null && outputASName.equals("")) outputASName = null;
99      try{
100       batch.transduce(document,
101                       inputASName == null ?
102                         document.getAnnotations() :
103                         document.getAnnotations(inputASName),
104                       outputASName == null ?
105                         document.getAnnotations() :
106                         document.getAnnotations(outputASName));
107     }catch(JapeException je){
108       throw new ExecutionException(je);
109     }
110   }
111 
112 
113   /**
114    * Notifies all the PRs in this controller that they should stop their
115    * execution as soon as possible.
116    */
117   public synchronized void interrupt(){
118     interrupted = true;
119     batch.interrupt();
120   }
121   /**
122    * Sets the grammar to be used for building this transducer.
123    * @param newGrammarURL an URL to a file containing a Jape grammar.
124    */
125   public void setGrammarURL(java.net.URL newGrammarURL) {
126     grammarURL = newGrammarURL;
127   }
128 
129   /**
130    * Gets the URL to the grammar used to build this transducer.
131    * @return a {@link java.net.URL} pointing to the grammar file.
132    */
133   public java.net.URL getGrammarURL() {
134     return grammarURL;
135   }
136 
137   /**
138    *
139    * Sets the encoding to be used for reding the input file(s) forming the Jape
140    * grammar. Note that if the input grammar is a multi-file one than the same
141    * encoding will be used for reding all the files. Multi file grammars with
142    * different encoding across the composing files are not supported!
143    * @param newEncoding a {link String} representing the encoding.
144    */
145   public void setEncoding(String newEncoding) {
146     encoding = newEncoding;
147   }
148 
149   /**
150    * Gets the encoding used for reding the grammar file(s).
151    */
152   public String getEncoding() {
153     return encoding;
154   }
155 
156   /**
157    * Sets the {@link gate.AnnotationSet} to be used as input for the transducer.
158    * @param newInputAS a {@link gate.AnnotationSet}
159    */
160   public void setInputASName(String newInputASName) {
161     inputASName = newInputASName;
162   }
163 
164   /**
165    * Gets the {@link gate.AnnotationSet} used as input by this transducer.
166    * @return a {@link gate.AnnotationSet}
167    */
168   public String getInputASName() {
169     return inputASName;
170   }
171 
172   /**
173    * Sets the {@link gate.AnnotationSet} to be used as output by the transducer.
174    * @param newOutputAS a {@link gate.AnnotationSet}
175    */
176   public void setOutputASName(String newOutputASName) {
177     outputASName = newOutputASName;
178   }
179 
180   /**
181    * Gets the {@link gate.AnnotationSet} used as output by this transducer.
182    * @return a {@link gate.AnnotationSet}
183    */
184   public String getOutputASName() {
185     return outputASName;
186   }
187 
188   /**
189    * The URL to the jape file used as grammar by this transducer.
190    */
191   private java.net.URL grammarURL;
192 
193 
194   /**
195    * The actual JapeTransducer used for processing the document(s).
196    */
197   private Batch batch;
198 
199   /**
200    * The encoding used for reding the grammar file(s).
201    */
202   private String encoding;
203 
204   /**
205    * The {@link gate.AnnotationSet} used as input for the transducer.
206    */
207   private String inputASName;
208 
209   /**
210    * The {@link gate.AnnotationSet} used as output by the transducer.
211    */
212   private String outputASName;
213 }