ctakes-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From tm...@apache.org
Subject svn commit: r1589499 - in /ctakes/trunk/ctakes-core/src/main/java/org/apache/ctakes/core: ae/CopyAnnotator.java resource/FileLocator.java util/ParamUtil.java
Date Wed, 23 Apr 2014 19:37:23 GMT
Author: tmill
Date: Wed Apr 23 19:37:22 2014
New Revision: 1589499

URL: http://svn.apache.org/r1589499
Log:
CTAKES-295: Update CopyAnnotator to use UimaFit-style configuration.

Modified:
    ctakes/trunk/ctakes-core/src/main/java/org/apache/ctakes/core/ae/CopyAnnotator.java
    ctakes/trunk/ctakes-core/src/main/java/org/apache/ctakes/core/resource/FileLocator.java
    ctakes/trunk/ctakes-core/src/main/java/org/apache/ctakes/core/util/ParamUtil.java

Modified: ctakes/trunk/ctakes-core/src/main/java/org/apache/ctakes/core/ae/CopyAnnotator.java
URL: http://svn.apache.org/viewvc/ctakes/trunk/ctakes-core/src/main/java/org/apache/ctakes/core/ae/CopyAnnotator.java?rev=1589499&r1=1589498&r2=1589499&view=diff
==============================================================================
--- ctakes/trunk/ctakes-core/src/main/java/org/apache/ctakes/core/ae/CopyAnnotator.java (original)
+++ ctakes/trunk/ctakes-core/src/main/java/org/apache/ctakes/core/ae/CopyAnnotator.java Wed
Apr 23 19:37:22 2014
@@ -20,22 +20,21 @@ package org.apache.ctakes.core.ae;
 
 import java.lang.reflect.Constructor;
 import java.lang.reflect.Method;
+import java.util.Collection;
 import java.util.HashMap;
 import java.util.Iterator;
 import java.util.Map;
 
-import org.apache.ctakes.core.util.JCasUtil;
 import org.apache.ctakes.core.util.ParamUtil;
 import org.apache.log4j.Logger;
 import org.apache.uima.UimaContext;
-import org.apache.uima.analysis_component.JCasAnnotator_ImplBase;
 import org.apache.uima.analysis_engine.AnalysisEngineProcessException;
-import org.apache.uima.cas.FSIterator;
 import org.apache.uima.jcas.JCas;
-import org.apache.uima.jcas.JFSIndexRepository;
 import org.apache.uima.jcas.cas.TOP;
-import org.apache.uima.jcas.tcas.Annotation;
 import org.apache.uima.resource.ResourceInitializationException;
+import org.uimafit.component.JCasAnnotator_ImplBase;
+import org.uimafit.descriptor.ConfigurationParameter;
+import org.uimafit.util.JCasUtil;
 
 
 /**
@@ -46,7 +45,31 @@ import org.apache.uima.resource.Resource
  * 
  */
 public class CopyAnnotator extends JCasAnnotator_ImplBase {
-	private int iv_srcType;
+  public static final String PARAM_SOURCE_CLASS = "srcObjClass";
+  @ConfigurationParameter(
+      name = PARAM_SOURCE_CLASS,
+      mandatory = true,
+      description = "Name of source class"
+      )
+  private String srcClassName;
+  
+  public static final String PARAM_DEST_CLASS = "destObjClass";
+  @ConfigurationParameter(
+      name = PARAM_DEST_CLASS,
+      mandatory = true,
+      description = "Name of destination class"
+      )
+  private String destClassName;
+  
+  public static final String PARAM_METHOD_MAP = "dataBindMap";
+  @ConfigurationParameter(
+      name = PARAM_METHOD_MAP,
+      mandatory = true,
+      description = "Mapping between source methods and destination methods in a bar (\"|\")
separated format"
+      )
+  private String[] methodMapArray;
+  
+	private Class<? extends TOP> srcClass;
 
 	// LOG4J logger based on class name
 	private Logger logger = Logger.getLogger(getClass().getName());
@@ -59,30 +82,28 @@ public class CopyAnnotator extends JCasA
 	// val = destination setter method (java.lang.reflect.Method)
 	private Map<Method, Method> iv_getSetMap;
 
-	public void initialize(UimaContext annotCtx)
+  @SuppressWarnings("unchecked")
+  @Override
+  public void initialize(UimaContext annotCtx)
 			throws ResourceInitializationException {
-		super.initialize(annotCtx);
+    super.initialize(annotCtx);
 
-		try {
-			String className;
-			className = (String) annotCtx
-					.getConfigParameterValue("srcObjClass");
-			Class<?> srcClass = Class.forName(className);
-			iv_srcType = JCasUtil.getType(className);
-
-			className = (String) annotCtx
-					.getConfigParameterValue("destObjClass");
-			Class<?> destClass = Class.forName(className);
+    
+    try {
+			srcClass = (Class<? extends TOP>) Class.forName(srcClassName);
+		  if(!TOP.class.isAssignableFrom(srcClass)) throw new ResourceInitializationException();
+		  
+			Class<?> destClass = Class.forName(destClassName);
 			Class<?>[] constrArgs = { JCas.class };
 			iv_destContr = destClass.getConstructor(constrArgs);
 
 			Map<String, String> m = ParamUtil.getStringParameterValuesMap(
-					"dataBindMap", annotCtx, "|");
-			iv_getSetMap = new HashMap<Method, Method>();
+					methodMapArray, "|");
+			iv_getSetMap = new HashMap<>();
 			Iterator<String> getterItr = m.keySet().iterator();
 			while (getterItr.hasNext()) {
-				String getterMethName = (String) getterItr.next();
-				String setterMethName = (String) m.get(getterMethName);
+				String getterMethName = getterItr.next();
+				String setterMethName = m.get(getterMethName);
 
 				Method getterMeth = srcClass.getMethod(getterMethName,
 						(Class[]) null);
@@ -94,21 +115,22 @@ public class CopyAnnotator extends JCasA
 
 				iv_getSetMap.put(getterMeth, setterMeth);
 			}
-		} catch (Exception e) {
+		} catch (ClassNotFoundException e) {
 			throw new ResourceInitializationException(e);
-		}
+		} catch (NoSuchMethodException e) {
+      throw new ResourceInitializationException(e);
+    } catch (SecurityException e) {
+      throw new ResourceInitializationException(e);
+    }
 	}
 
-	public void process(JCas jcas) throws AnalysisEngineProcessException {
+	@Override
+  public void process(JCas jcas) throws AnalysisEngineProcessException {
 		logger.info("process(JCas)");
 
 		// iterate over source objects in JCas
-		JFSIndexRepository indexes = jcas.getJFSIndexRepository();
-		FSIterator<Annotation> srcObjItr = indexes.getAnnotationIndex(
-				iv_srcType).iterator();
-		while (srcObjItr.hasNext()) {
-			TOP srcObj = (TOP) srcObjItr.next();
-
+		Collection<? extends TOP> srcObjs = JCasUtil.select(jcas, srcClass);
+		for(TOP srcObj : srcObjs){
 			Object[] constrArgVals = { jcas };
 			try {
 				// create new destination object
@@ -117,8 +139,8 @@ public class CopyAnnotator extends JCasA
 				// copy data from source to destination
 				Iterator<Method> getterItr = iv_getSetMap.keySet().iterator();
 				while (getterItr.hasNext()) {
-					Method getterMeth = (Method) getterItr.next();
-					Method setterMeth = (Method) iv_getSetMap.get(getterMeth);
+					Method getterMeth = getterItr.next();
+					Method setterMeth = iv_getSetMap.get(getterMeth);
 
 					Object val = getterMeth.invoke(srcObj, (Object[]) null);
 					Object[] setterArgs = { val };

Modified: ctakes/trunk/ctakes-core/src/main/java/org/apache/ctakes/core/resource/FileLocator.java
URL: http://svn.apache.org/viewvc/ctakes/trunk/ctakes-core/src/main/java/org/apache/ctakes/core/resource/FileLocator.java?rev=1589499&r1=1589498&r2=1589499&view=diff
==============================================================================
--- ctakes/trunk/ctakes-core/src/main/java/org/apache/ctakes/core/resource/FileLocator.java
(original)
+++ ctakes/trunk/ctakes-core/src/main/java/org/apache/ctakes/core/resource/FileLocator.java
Wed Apr 23 19:37:22 2014
@@ -53,7 +53,7 @@ public class FileLocator
     }
 	
     /**
-     * @deprecated  As of release 3.1, replaced by {@link #getAsStream()}
+     * @Deprecated  As of release 3.1, replaced by {@link #getAsStream()}
      */    
     public static File locateFile(String location)
             throws FileNotFoundException

Modified: ctakes/trunk/ctakes-core/src/main/java/org/apache/ctakes/core/util/ParamUtil.java
URL: http://svn.apache.org/viewvc/ctakes/trunk/ctakes-core/src/main/java/org/apache/ctakes/core/util/ParamUtil.java?rev=1589499&r1=1589498&r2=1589499&view=diff
==============================================================================
--- ctakes/trunk/ctakes-core/src/main/java/org/apache/ctakes/core/util/ParamUtil.java (original)
+++ ctakes/trunk/ctakes-core/src/main/java/org/apache/ctakes/core/util/ParamUtil.java Wed
Apr 23 19:37:22 2014
@@ -23,10 +23,8 @@ import java.util.HashSet;
 import java.util.Map;
 import java.util.Set;
 
-
-import org.apache.uima.UimaContext;
+import org.apache.uima.UimaContext;
 import org.apache.uima.analysis_engine.annotator.AnnotatorContextException;
-import org.apache.uima.resource.ResourceAccessException;
 
 public class ParamUtil {
 
@@ -38,9 +36,8 @@ public class ParamUtil {
 	 *         optional or not set.
 	 * @throws AnnotatorContextException
 	 */
-	public static Set<String> getStringParameterValuesSet(String parameterName, UimaContext
annotatorContext)
-			throws ResourceAccessException {
-		Set<String> returnValues = new HashSet<String>();
+	public static Set<String> getStringParameterValuesSet(String parameterName, UimaContext
annotatorContext) {
+		Set<String> returnValues = new HashSet<>();
 		String[] strings = (String[]) annotatorContext.getConfigParameterValue(parameterName);
 		if (strings == null)
 			return returnValues;
@@ -52,18 +49,21 @@ public class ParamUtil {
 	}
 
 	public static Map<String, String> getStringParameterValuesMap(String parameterName,
-			UimaContext annotatorContext, String keyValueDelimiter) throws ResourceAccessException
{
+			UimaContext annotatorContext, String keyValueDelimiter) {
 		String[] paramValues = (String[]) annotatorContext.getConfigParameterValue(parameterName);
-		Map<String, String> map = new HashMap<String, String>();
-		for (int i = 0; i < paramValues.length; i++) {
-			int delimiterIndex = paramValues[i].lastIndexOf(keyValueDelimiter);
-			if (delimiterIndex == -1)
-				continue;
-			String key = paramValues[i].substring(0, delimiterIndex);
-			String value = paramValues[i].substring(delimiterIndex + 1);
-			map.put(key, value);
-		}
-		return map;
+		return getStringParameterValuesMap(paramValues, keyValueDelimiter);
+	}
+	
+	public static Map<String, String> getStringParameterValuesMap(String[] paramValues,
String keyValueDelimiter){
+    Map<String, String> map = new HashMap<>();
+    for (int i = 0; i < paramValues.length; i++) {
+      int delimiterIndex = paramValues[i].lastIndexOf(keyValueDelimiter);
+      if (delimiterIndex == -1)
+        continue;
+      String key = paramValues[i].substring(0, delimiterIndex);
+      String value = paramValues[i].substring(delimiterIndex + 1);
+      map.put(key, value);
+    }
+    return map;
 	}
-
 }



Mime
View raw message