| Transducer |

    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.17 2001/10/29 13:37:30 valyt 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    /**
    33     * Default constructor. Does nothing apart from calling the default
    34     * constructor from the super class. The actual object initialisation is done
    35     * via the {@link #init} method.
    36     */
    37    public Transducer() {
    38    }
    39
    40    /*
    41    private void writeObject(ObjectOutputStream oos) throws IOException {
    42      Out.prln("writing transducer");
    43      oos.defaultWriteObject();
    44      Out.prln("finished writing transducer");
    45    } // writeObject
    46    */
    47
    48    /**
    49     * This method is the one responsible for initialising the transducer. It
    50     * assumes that all the needed parameters have been already set using the
    51     * appropiate setXXX() methods.
    52     *@return a reference to <b>this</b>
    53     */
    54    public Resource init() throws ResourceInstantiationException {
    55      if(grammarURL != null && encoding != null){
    56        try{
    57          fireProgressChanged(0);
    58          batch = new Batch(grammarURL, encoding, new InternalStatusListener());
    59          fireProcessFinished();
    60        }catch(JapeException je){
    61          throw new ResourceInstantiationException(je);
    62        }
    63      } else
    64        throw new ResourceInstantiationException (
    65          "Both the URL (was " + grammarURL + ") and the encoding (was " +
    66          encoding + ") are needed to create a JapeTransducer!"
    67        );
    68
    69        batch.addProgressListener(new IntervalProgressListener(0, 100));
    70
    71      return this;
    72    }
    73
    74    /**
    75     * Implementation of the run() method from {@link java.lang.Runnable}.
    76     * This method is responsible for doing all the processing of the input
    77     * document.
    78     */
    79    public void execute() throws ExecutionException{
    80      interrupted = false;
    81      if(document == null) throw new ExecutionException("No document provided!");
    82      if(inputASName != null && inputASName.equals("")) inputASName = null;
    83      if(outputASName != null && outputASName.equals("")) outputASName = null;
    84      try{
    85        batch.transduce(document,
    86                        inputASName == null ?
    87                          document.getAnnotations() :
    88                          document.getAnnotations(inputASName),
    89                        outputASName == null ?
    90                          document.getAnnotations() :
    91                          document.getAnnotations(outputASName));
    92      }catch(JapeException je){
    93        throw new ExecutionException(je);
    94      }
    95    }
    96
    97
    98    /**
    99     * Notifies all the PRs in this controller that they should stop their
    100    * execution as soon as possible.
    101    */
    102   public synchronized void interrupt(){
    103     interrupted = true;
    104     batch.interrupt();
    105   }
    106   /**
    107    * Sets the grammar to be used for building this transducer.
    108    * @param newGrammarURL an URL to a file containing a Jape grammar.
    109    */
    110   public void setGrammarURL(java.net.URL newGrammarURL) {
    111     grammarURL = newGrammarURL;
    112   }
    113
    114   /**
    115    * Gets the URL to the grammar used to build this transducer.
    116    * @return a {@link java.net.URL} pointing to the grammar file.
    117    */
    118   public java.net.URL getGrammarURL() {
    119     return grammarURL;
    120   }
    121
    122   /**
    123    *
    124    * Sets the encoding to be used for reding the input file(s) forming the Jape
    125    * grammar. Note that if the input grammar is a multi-file one than the same
    126    * encoding will be used for reding all the files. Multi file grammars with
    127    * different encoding across the composing files are not supported!
    128    * @param newEncoding a {link String} representing the encoding.
    129    */
    130   public void setEncoding(String newEncoding) {
    131     encoding = newEncoding;
    132   }
    133
    134   /**
    135    * Gets the encoding used for reding the grammar file(s).
    136    */
    137   public String getEncoding() {
    138     return encoding;
    139   }
    140
    141   /**
    142    * Sets the {@link gate.AnnotationSet} to be used as input for the transducer.
    143    * @param newInputAS a {@link gate.AnnotationSet}
    144    */
    145   public void setInputASName(String newInputASName) {
    146     inputASName = newInputASName;
    147   }
    148
    149   /**
    150    * Gets the {@link gate.AnnotationSet} used as input by this transducer.
    151    * @return a {@link gate.AnnotationSet}
    152    */
    153   public String getInputASName() {
    154     return inputASName;
    155   }
    156
    157   /**
    158    * Sets the {@link gate.AnnotationSet} to be used as output by the transducer.
    159    * @param newOutputAS a {@link gate.AnnotationSet}
    160    */
    161   public void setOutputASName(String newOutputASName) {
    162     outputASName = newOutputASName;
    163   }
    164
    165   /**
    166    * Gets the {@link gate.AnnotationSet} used as output by this transducer.
    167    * @return a {@link gate.AnnotationSet}
    168    */
    169   public String getOutputASName() {
    170     return outputASName;
    171   }
    172
    173   /**
    174    * The URL to the jape file used as grammar by this transducer.
    175    */
    176   private java.net.URL grammarURL;
    177
    178
    179   /**
    180    * The actual JapeTransducer used for processing the document(s).
    181    */
    182   private Batch batch;
    183
    184   /**
    185    * The encoding used for reding the grammar file(s).
    186    */
    187   private String encoding;
    188
    189   /**
    190    * The {@link gate.AnnotationSet} used as input for the transducer.
    191    */
    192   private String inputASName;
    193
    194   /**
    195    * The {@link gate.AnnotationSet} used as output by the transducer.
    196    */
    197   private String outputASName;
    198 }

| Transducer |