ctakes-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From seanfi...@apache.org
Subject svn commit: r1783029 - in /ctakes/trunk/ctakes-lvg/src/main/java/org/apache/ctakes/lvg/ae: LvgSingleton.java ThreadSafeLvg.java
Date Tue, 14 Feb 2017 21:15:14 GMT
Author: seanfinan
Date: Tue Feb 14 21:15:14 2017
New Revision: 1783029

URL: http://svn.apache.org/viewvc?rev=1783029&view=rev
Log:
Attempt at thread safe lvg

Added:
    ctakes/trunk/ctakes-lvg/src/main/java/org/apache/ctakes/lvg/ae/LvgSingleton.java
    ctakes/trunk/ctakes-lvg/src/main/java/org/apache/ctakes/lvg/ae/ThreadSafeLvg.java

Added: ctakes/trunk/ctakes-lvg/src/main/java/org/apache/ctakes/lvg/ae/LvgSingleton.java
URL: http://svn.apache.org/viewvc/ctakes/trunk/ctakes-lvg/src/main/java/org/apache/ctakes/lvg/ae/LvgSingleton.java?rev=1783029&view=auto
==============================================================================
--- ctakes/trunk/ctakes-lvg/src/main/java/org/apache/ctakes/lvg/ae/LvgSingleton.java (added)
+++ ctakes/trunk/ctakes-lvg/src/main/java/org/apache/ctakes/lvg/ae/LvgSingleton.java Tue Feb
14 21:15:14 2017
@@ -0,0 +1,89 @@
+package org.apache.ctakes.lvg.ae;
+
+
+import org.apache.commons.io.FileUtils;
+import org.apache.ctakes.lvg.resource.LvgCmdApiResourceImpl;
+import org.apache.log4j.Logger;
+import org.apache.uima.UimaContext;
+import org.apache.uima.analysis_engine.AnalysisEngineProcessException;
+import org.apache.uima.fit.factory.ExternalResourceFactory;
+import org.apache.uima.jcas.JCas;
+import org.apache.uima.resource.ExternalResourceDescription;
+import org.apache.uima.resource.ResourceInitializationException;
+
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URL;
+
+/**
+ * Holds a single instance of the LvgAnnotator.
+ * Use this singleton from an annotator instead of the LvgAnnotator directly to help prevent
thread safety issues.
+ *
+ * @author SPF , chip-nlp
+ * @version %I%
+ * @since 2/14/2017
+ */
+enum LvgSingleton {
+   INSTANCE;
+
+   static public LvgSingleton getInstance() {
+      return INSTANCE;
+   }
+
+   static public final String PROPERTIES_PATH = "org/apache/ctakes/lvg/data/config/lvg.properties";
+   static public final String PROP_FILE_PATH = "/tmp/lvg.properties";
+
+   private final Logger LOGGER = Logger.getLogger( "LvgSingleton" );
+   private final Object LOCK = new Object();
+
+   private final LvgAnnotator _lvgAnnotator;
+
+   private ExternalResourceDescription _lvgCmdApi;
+   private boolean _initialized;
+
+
+   LvgSingleton() {
+      _lvgAnnotator = new LvgAnnotator();
+   }
+
+   /**
+    * Calls initialize on the single LVG instance if and only if it has not already been
initialized
+    */
+   public void initialize( final UimaContext context ) throws ResourceInitializationException
{
+      synchronized ( LOCK ) {
+         if ( !_initialized ) {
+            _initialized = true;
+            _lvgAnnotator.initialize( context );
+         }
+      }
+   }
+
+   public void process( final JCas jCas ) throws AnalysisEngineProcessException {
+      synchronized ( LOCK ) {
+         _lvgAnnotator.process( jCas );
+      }
+   }
+
+
+   public ExternalResourceDescription getDefaultLvgCmdApi() throws IOException {
+      synchronized ( LOCK ) {
+         if ( _lvgCmdApi != null ) {
+            return _lvgCmdApi;
+         }
+         final File lvgFile = new File( PROP_FILE_PATH );
+         try ( InputStream propFileStream = LvgSingleton.class.getClassLoader()
+               .getResourceAsStream( PROPERTIES_PATH ) ) {
+            FileUtils.copyInputStreamToFile( propFileStream, lvgFile );
+         } catch ( IOException ioE ) {
+            LOGGER.error( "Error copying temporary InputStream " + PROPERTIES_PATH + " to
" + PROP_FILE_PATH );
+            throw ioE;
+         }
+         final URL propFileUrl = lvgFile.toURI().toURL();
+         _lvgCmdApi
+               = ExternalResourceFactory.createExternalResourceDescription( LvgCmdApiResourceImpl.class,
propFileUrl );
+         return _lvgCmdApi;
+      }
+   }
+
+}

Added: ctakes/trunk/ctakes-lvg/src/main/java/org/apache/ctakes/lvg/ae/ThreadSafeLvg.java
URL: http://svn.apache.org/viewvc/ctakes/trunk/ctakes-lvg/src/main/java/org/apache/ctakes/lvg/ae/ThreadSafeLvg.java?rev=1783029&view=auto
==============================================================================
--- ctakes/trunk/ctakes-lvg/src/main/java/org/apache/ctakes/lvg/ae/ThreadSafeLvg.java (added)
+++ ctakes/trunk/ctakes-lvg/src/main/java/org/apache/ctakes/lvg/ae/ThreadSafeLvg.java Tue
Feb 14 21:15:14 2017
@@ -0,0 +1,68 @@
+package org.apache.ctakes.lvg.ae;
+
+import org.apache.ctakes.core.pipeline.PipeBitInfo;
+import org.apache.log4j.Logger;
+import org.apache.uima.UimaContext;
+import org.apache.uima.analysis_engine.AnalysisEngineDescription;
+import org.apache.uima.analysis_engine.AnalysisEngineProcessException;
+import org.apache.uima.fit.component.JCasAnnotator_ImplBase;
+import org.apache.uima.fit.factory.AnalysisEngineFactory;
+import org.apache.uima.jcas.JCas;
+import org.apache.uima.resource.ResourceInitializationException;
+
+import java.io.IOException;
+
+
+/**
+ * Utilizes a singleton to access a single instance of the LvgAnnotator.  This should help
prevent thread safety issues.
+ *
+ * @author SPF , chip-nlp
+ * @version %I%
+ * @since 2/14/2017
+ */
+@PipeBitInfo(
+      name = "Thread-Safe LVG",
+      description = "Annotates Lexical Variants for terms with attempted thread safety.",
+      input = "WordTokens.",
+      output = "Lexical Variants stored in existing WordTokens."
+)
+final public class ThreadSafeLvg extends JCasAnnotator_ImplBase {
+
+   static private final Logger LOGGER = Logger.getLogger( "ThreadSafeLvg" );
+
+
+   /**
+    * Calls initialize on the LvgSingleton.
+    * {@inheritDoc}
+    */
+   @Override
+   public void initialize( final UimaContext context ) throws ResourceInitializationException
{
+      LvgSingleton.getInstance().initialize( context );
+   }
+
+   /**
+    * Calls initialize on the LvgSingleton.
+    * {@inheritDoc}
+    */
+   @Override
+   public void process( final JCas jCas ) throws AnalysisEngineProcessException {
+      LvgSingleton.getInstance().process( jCas );
+   }
+
+
+   /**
+    * Necessary if the lvg.properties need to be copied into a temporary file.  Otherwise
all defaults are fine.
+    *
+    * @return description
+    * @throws ResourceInitializationException if the lvg annotator could not be initialized
+    * @throws IOException                     if there was a problem creating the default
LvgCmdApi resource
+    */
+   @SuppressWarnings( "resource" )
+   public static AnalysisEngineDescription createAnnotatorDescription() throws ResourceInitializationException,
+                                                                               IOException
{
+      return AnalysisEngineFactory.createEngineDescription( ThreadSafeLvg.class,
+            LvgAnnotator.PARAM_LVGCMDAPI_RESRC_KEY,
+            LvgSingleton.getInstance().getDefaultLvgCmdApi() );
+   }
+
+}



Mime
View raw message