ctakes-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From seanfi...@apache.org
Subject svn commit: r1779398 - /ctakes/trunk/ctakes-core/src/main/java/org/apache/ctakes/core/pipeline/PipelineBuilder.java
Date Wed, 18 Jan 2017 23:06:14 GMT
Author: seanfinan
Date: Wed Jan 18 23:06:14 2017
New Revision: 1779398

URL: http://svn.apache.org/viewvc?rev=1779398&view=rev
Log:
Write XMIs in tree, write using addLast

Modified:
    ctakes/trunk/ctakes-core/src/main/java/org/apache/ctakes/core/pipeline/PipelineBuilder.java

Modified: ctakes/trunk/ctakes-core/src/main/java/org/apache/ctakes/core/pipeline/PipelineBuilder.java
URL: http://svn.apache.org/viewvc/ctakes/trunk/ctakes-core/src/main/java/org/apache/ctakes/core/pipeline/PipelineBuilder.java?rev=1779398&r1=1779397&r2=1779398&view=diff
==============================================================================
--- ctakes/trunk/ctakes-core/src/main/java/org/apache/ctakes/core/pipeline/PipelineBuilder.java
(original)
+++ ctakes/trunk/ctakes-core/src/main/java/org/apache/ctakes/core/pipeline/PipelineBuilder.java
Wed Jan 18 23:06:14 2017
@@ -1,15 +1,18 @@
 package org.apache.ctakes.core.pipeline;
 
 
-import org.apache.ctakes.core.cc.XmiWriterCasConsumerCtakes;
+import org.apache.ctakes.core.cc.FileTreeXmiWriter;
+import org.apache.ctakes.core.config.ConfigParameterConstants;
 import org.apache.ctakes.core.cr.FileTreeReader;
 import org.apache.ctakes.core.util.PropertyAeFactory;
 import org.apache.log4j.Logger;
 import org.apache.uima.UIMAException;
 import org.apache.uima.analysis_component.AnalysisComponent;
+import org.apache.uima.analysis_engine.AnalysisEngine;
 import org.apache.uima.analysis_engine.AnalysisEngineDescription;
 import org.apache.uima.collection.CollectionReader;
 import org.apache.uima.fit.factory.AggregateBuilder;
+import org.apache.uima.fit.factory.AnalysisEngineFactory;
 import org.apache.uima.fit.factory.CollectionReaderFactory;
 import org.apache.uima.fit.factory.JCasFactory;
 import org.apache.uima.fit.pipeline.SimplePipeline;
@@ -39,6 +42,10 @@ final public class PipelineBuilder {
    private final List<AnalysisEngineDescription> _descList;
    private final List<String> _aeEndNameList;
    private final List<AnalysisEngineDescription> _descEndList;
+   // Allow the pipeline to be changed even after it has been built once.
+   private AnalysisEngine _analysisEngine;
+   private boolean _pipelineChanged;
+
 
    public PipelineBuilder() {
       _aeNameList = new ArrayList<>();
@@ -55,6 +62,7 @@ final public class PipelineBuilder {
     */
    public PipelineBuilder set( final Object... parameters ) {
       PropertyAeFactory.getInstance().addParameters( parameters );
+      _pipelineChanged = true;
       return this;
    }
 
@@ -66,12 +74,27 @@ final public class PipelineBuilder {
     */
    public PipelineBuilder reader( final CollectionReader reader ) {
       _reader = reader;
+      _pipelineChanged = true;
+      return this;
+   }
+
+   /**
+    * Use of this method is not order-specific
+    *
+    * @param readerClass Collection Reader class to place at the beginning of the pipeline
+    * @param parameters reader parameter name value pairs.  May be empty.
+    * @return this PipelineBuilder
+    */
+   public PipelineBuilder reader( final Class<? extends CollectionReader> readerClass,
final Object... parameters )
+         throws UIMAException {
+      reader( CollectionReaderFactory.createReader( readerClass, parameters ) );
+      _pipelineChanged = true;
       return this;
    }
 
    /**
     * Adds a Collection reader to the beginning of the pipeline that will read files in a
directory tree.
-    * Relies upon {@link FileTreeReader#PARAM_INPUTDIR} having been specified
+    * Relies upon {@link org.apache.ctakes.core.config.ConfigParameterConstants#PARAM_INPUTDIR}
having been specified
     * Use of this method is not order-specific.
     *
     * @return this PipelineBuilder
@@ -79,6 +102,7 @@ final public class PipelineBuilder {
     */
    public PipelineBuilder readFiles() throws UIMAException {
       _reader = CollectionReaderFactory.createReader( FileTreeReader.class );
+      _pipelineChanged = true;
       return this;
    }
 
@@ -92,6 +116,7 @@ final public class PipelineBuilder {
     */
    public PipelineBuilder readFiles( final String inputDirectory ) throws UIMAException {
       _reader = FileTreeReader.createReader( inputDirectory );
+      _pipelineChanged = true;
       return this;
    }
 
@@ -115,6 +140,7 @@ final public class PipelineBuilder {
                                final Object... parameters ) throws ResourceInitializationException
{
       _aeNameList.add( component.getName() );
       _descList.add( PropertyAeFactory.getInstance().createDescription( component, parameters
) );
+      _pipelineChanged = true;
       return this;
    }
 
@@ -131,6 +157,7 @@ final public class PipelineBuilder {
                                      final Object... parameters ) throws ResourceInitializationException
{
       _aeNameList.add( component.getName() );
       _descList.add( PropertyAeFactory.getInstance().createLoggedDescription( component,
parameters ) );
+      _pipelineChanged = true;
       return this;
    }
 
@@ -143,6 +170,7 @@ final public class PipelineBuilder {
    public PipelineBuilder addDescription( final AnalysisEngineDescription description ) {
       _aeNameList.add( description.getAnnotatorImplementationName() );
       _descList.add( description );
+      _pipelineChanged = true;
       return this;
    }
 
@@ -158,6 +186,7 @@ final public class PipelineBuilder {
                                    final Object... parameters ) throws ResourceInitializationException
{
       _aeEndNameList.add( component.getName() );
       _descEndList.add( PropertyAeFactory.getInstance().createDescription( component, parameters
) );
+      _pipelineChanged = true;
       return this;
    }
 
@@ -196,19 +225,19 @@ final public class PipelineBuilder {
    }
 
    /**
-    * Adds ae that writes an xmi file.
-    * Relies upon {@link XmiWriterCasConsumerCtakes#PARAM_OUTPUTDIR} having been specified
+    * Adds ae that writes an xmi file at the end of the pipeline.
+    * Relies upon {@link ConfigParameterConstants#PARAM_OUTPUTDIR} having been specified
     * Use of this method is order-specific.
     *
     * @return this PipelineBuilder
     * @throws ResourceInitializationException if the Xmi writer engine cannot be created
     */
    public PipelineBuilder writeXMIs() throws ResourceInitializationException {
-      return add( XmiWriterCasConsumerCtakes.class );
+      return addLast( FileTreeXmiWriter.class );
    }
 
    /**
-    * Adds ae that writes an xmi file.
+    * Adds ae that writes an xmi file at the end of the pipeline.
     * Use of this method is order-specific.
     *
     * @param outputDirectory directory in which xmi files should be written
@@ -216,12 +245,33 @@ final public class PipelineBuilder {
     * @throws ResourceInitializationException if the Xmi writer engine cannot be created
     */
    public PipelineBuilder writeXMIs( final String outputDirectory ) throws ResourceInitializationException
{
-      return add( XmiWriterCasConsumerCtakes.class, XmiWriterCasConsumerCtakes.PARAM_OUTPUTDIR,
outputDirectory );
+      return addLast( FileTreeXmiWriter.class, ConfigParameterConstants.PARAM_OUTPUTDIR,
outputDirectory );
+   }
+
+   /**
+    * Initialize a pipeline that can be used repeatedly using {@link #run} and {@link #run(String)}.
+    * A pipeline can be extended between builds, but the full pipeline will be rebuilt on
each call.
+    * Use of this method is order-specific.
+    * @return this PipelineBuilder
+    * @throws IOException   if the pipeline could not be run
+    * @throws UIMAException if the pipeline could not be run
+    */
+   public PipelineBuilder build() throws IOException, UIMAException {
+      if ( _analysisEngine == null || _pipelineChanged ) {
+         final AggregateBuilder builder = new AggregateBuilder();
+         _descList.forEach( builder::add );
+         _descEndList.forEach( builder::add );
+         final AnalysisEngineDescription description = builder.createAggregateDescription();
+         _analysisEngine = AnalysisEngineFactory.createEngine( description );
+      }
+      _pipelineChanged = false;
+      return this;
    }
 
    /**
     * Run the pipeline using some specified collection reader.
     * Use of this method is order-specific.
+    * This method will call {@link #build()} if the pipeline has not already been initialized.
     *
     * @return this PipelineBuilder
     * @throws IOException   if the pipeline could not be run
@@ -232,17 +282,15 @@ final public class PipelineBuilder {
          LOGGER.error( "No Collection Reader specified." );
          return this;
       }
-      final AggregateBuilder builder = new AggregateBuilder();
-      _descList.forEach( builder::add );
-      _descEndList.forEach( builder::add );
-      final AnalysisEngineDescription desc = builder.createAggregateDescription();
-      SimplePipeline.runPipeline( _reader, desc );
+      build();
+      SimplePipeline.runPipeline( _reader, _analysisEngine );
       return this;
    }
 
    /**
     * Run the pipeline on the given text.
     * Use of this method is order-specific.
+    * This method will call {@link #build()} if the pipeline has not already been initialized.
     *
     * @param text text upon which to run this pipeline
     * @return this PipelineBuilder
@@ -256,11 +304,8 @@ final public class PipelineBuilder {
       }
       final JCas jcas = JCasFactory.createJCas();
       jcas.setDocumentText( text );
-      final AggregateBuilder builder = new AggregateBuilder();
-      _descList.forEach( builder::add );
-      _descEndList.forEach( builder::add );
-      final AnalysisEngineDescription desc = builder.createAggregateDescription();
-      SimplePipeline.runPipeline( jcas, desc );
+      build();
+      SimplePipeline.runPipeline( jcas, _analysisEngine );
       return this;
    }
 



Mime
View raw message