You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@uima.apache.org by pk...@apache.org on 2016/10/25 11:43:31 UTC

svn commit: r1766517 - in /uima/ruta/trunk: ruta-core/src/main/java/org/apache/uima/ruta/engine/ ruta-core/src/test/java/org/apache/uima/ruta/engine/ ruta-docbook/src/docbook/

Author: pkluegl
Date: Tue Oct 25 11:43:31 2016
New Revision: 1766517

URL: http://svn.apache.org/viewvc?rev=1766517&view=rev
Log:
UIMA-5159 + UIMA-5144
- boolean parameter modifyDataPath, default false
- fixed dataPath string enumeration
- reuse extension class loader
- added tests

Added:
    uima/ruta/trunk/ruta-core/src/test/java/org/apache/uima/ruta/engine/HandleDataPathTest.java   (with props)
    uima/ruta/trunk/ruta-core/src/test/java/org/apache/uima/ruta/engine/UimaClassLoaderTest.java   (with props)
Modified:
    uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/engine/RutaEngine.java
    uima/ruta/trunk/ruta-docbook/src/docbook/tools.ruta.overview.xml

Modified: uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/engine/RutaEngine.java
URL: http://svn.apache.org/viewvc/uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/engine/RutaEngine.java?rev=1766517&r1=1766516&r2=1766517&view=diff
==============================================================================
--- uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/engine/RutaEngine.java (original)
+++ uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/engine/RutaEngine.java Tue Oct 25 11:43:31 2016
@@ -414,6 +414,16 @@ public class RutaEngine extends JCasAnno
       "uima.tcas.Annotation" })
   private String[] reindexOnly;
 
+  
+  /**
+   * Option to extend the datapath by the descriptorPaths
+   */
+  public static final String PARAM_MODIFY_DATAPATH = "modifyDataPath";
+
+  @ConfigurationParameter(name = PARAM_MODIFY_DATAPATH, mandatory = false, defaultValue = "false")
+  private boolean modifyDataPath;
+
+  
   private UimaContext context;
 
   private RutaModule script;
@@ -452,10 +462,11 @@ public class RutaEngine extends JCasAnno
     // reinitialize analysis engines if this one is configured
     analysisEnginesAlreadyInitialized = false;
 
+    initializeResourceManager();
     handleDataPath();
-
-    scriptRutaResourceLoader = new RutaResourceLoader(scriptPaths);
-    descriptorRutaResourceLoader = new RutaResourceLoader(descriptorPaths);
+    
+    scriptRutaResourceLoader = new RutaResourceLoader(scriptPaths, resourceManager.getExtensionClassLoader());
+    descriptorRutaResourceLoader = new RutaResourceLoader(descriptorPaths, resourceManager.getExtensionClassLoader());
 
     if (!factory.isInitialized()) {
       initializeExtensionWithClassPath();
@@ -516,6 +527,19 @@ public class RutaEngine extends JCasAnno
     }
   }
 
+  private void initializeResourceManager() {
+    if (context instanceof UimaContextAdmin) {
+      UimaContextAdmin uca = (UimaContextAdmin) context;
+      ResourceManager rm = uca.getResourceManager();
+      if (rm != null) {
+        resourceManager = rm;
+      }
+    }
+    if (resourceManager == null) {
+      resourceManager = UIMAFramework.newDefaultResourceManager();
+    }
+  }
+  
   private void handleDataPath() throws ResourceInitializationException {
     String dataPath = context.getDataPath();
     String[] singleDataPaths = dataPath.split(File.pathSeparator);
@@ -530,17 +554,11 @@ public class RutaEngine extends JCasAnno
       descriptorPaths = ArrayUtils.addAll(descriptorPaths, singleDataPaths);
       resourcePaths = ArrayUtils.addAll(resourcePaths, singleDataPaths);
     }
-    if (context instanceof UimaContextAdmin) {
-      UimaContextAdmin uca = (UimaContextAdmin) context;
-      ResourceManager rm = uca.getResourceManager();
-      if (rm != null) {
-        resourceManager = rm;
+    
+    if (modifyDataPath && clonedDescriptorPath != null) {
+      if(!dataPath.endsWith(File.pathSeparator)) {
+        dataPath += File.pathSeparator;
       }
-    }
-    if (resourceManager == null) {
-      resourceManager = UIMAFramework.newDefaultResourceManager();
-    }
-    if (clonedDescriptorPath != null) {
       for (String path : clonedDescriptorPath) {
         dataPath += path + File.pathSeparator;
       }

Added: uima/ruta/trunk/ruta-core/src/test/java/org/apache/uima/ruta/engine/HandleDataPathTest.java
URL: http://svn.apache.org/viewvc/uima/ruta/trunk/ruta-core/src/test/java/org/apache/uima/ruta/engine/HandleDataPathTest.java?rev=1766517&view=auto
==============================================================================
--- uima/ruta/trunk/ruta-core/src/test/java/org/apache/uima/ruta/engine/HandleDataPathTest.java (added)
+++ uima/ruta/trunk/ruta-core/src/test/java/org/apache/uima/ruta/engine/HandleDataPathTest.java Tue Oct 25 11:43:31 2016
@@ -0,0 +1,79 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ * 
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.uima.ruta.engine;
+
+import java.io.File;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.net.URLClassLoader;
+import java.util.regex.Pattern;
+
+import org.apache.uima.analysis_engine.AnalysisEngine;
+import org.apache.uima.fit.factory.AnalysisEngineFactory;
+import org.apache.uima.fit.internal.ResourceManagerFactory;
+import org.apache.uima.fit.internal.ResourceManagerFactory.ResourceManagerCreator;
+import org.apache.uima.resource.ResourceInitializationException;
+import org.apache.uima.resource.ResourceManager;
+import org.apache.uima.resource.impl.ResourceManager_impl;
+import org.junit.Assert;
+import org.junit.Test;
+
+public class HandleDataPathTest {
+
+  @Test
+  public void testDefault() throws Exception {
+    assertDataPathEntries(false, new String[] { "datapath" });
+  }
+
+  @Test
+  public void testModify() throws Exception {
+    assertDataPathEntries(true, new String[] { "datapath", "desc1", "desc2" });
+  }
+
+  private void assertDataPathEntries(boolean modify, String[] expected) throws ResourceInitializationException {
+    ResourceManagerFactory.setResourceManagerCreator(new ResourceManagerCreator() {
+
+      @Override
+      public ResourceManager newResourceManager() throws ResourceInitializationException {
+
+        URL[] urls = new URL[1];
+        urls[0] = HandleDataPathTest.class.getResource("/org/apache/uima/ruta/CustomViewTest.ruta");
+        ResourceManager resourceManager = new ResourceManager_impl(new URLClassLoader(urls));
+        try {
+          resourceManager.setDataPath("datapath");
+        } catch (MalformedURLException e) {
+          throw new ResourceInitializationException(e);
+        }
+        return resourceManager;
+      }
+    });
+    AnalysisEngine ruta = AnalysisEngineFactory.createEngine(RutaEngine.class,
+            RutaEngine.PARAM_MODIFY_DATAPATH, modify, RutaEngine.PARAM_DESCRIPTOR_PATHS,
+            new String[] { "desc1", "desc2" });
+    String dataPath = ruta.getResourceManager().getDataPath();
+    String[] paths = dataPath.split(Pattern.quote(File.pathSeparator));
+    Assert.assertEquals(expected.length, paths.length);
+    for (int i = 0; i < expected.length; i++) {
+      String e = expected[i];
+      String dp = paths[i];
+      Assert.assertEquals(e, dp);
+    }
+  }
+
+}

Propchange: uima/ruta/trunk/ruta-core/src/test/java/org/apache/uima/ruta/engine/HandleDataPathTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: uima/ruta/trunk/ruta-core/src/test/java/org/apache/uima/ruta/engine/UimaClassLoaderTest.java
URL: http://svn.apache.org/viewvc/uima/ruta/trunk/ruta-core/src/test/java/org/apache/uima/ruta/engine/UimaClassLoaderTest.java?rev=1766517&view=auto
==============================================================================
--- uima/ruta/trunk/ruta-core/src/test/java/org/apache/uima/ruta/engine/UimaClassLoaderTest.java (added)
+++ uima/ruta/trunk/ruta-core/src/test/java/org/apache/uima/ruta/engine/UimaClassLoaderTest.java Tue Oct 25 11:43:31 2016
@@ -0,0 +1,74 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ * 
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.uima.ruta.engine;
+
+import java.io.File;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.util.Collection;
+
+import org.apache.uima.UIMAFramework;
+import org.apache.uima.analysis_engine.AnalysisEngine;
+import org.apache.uima.fit.factory.AnalysisEngineFactory;
+import org.apache.uima.fit.internal.ResourceManagerFactory;
+import org.apache.uima.fit.internal.ResourceManagerFactory.ResourceManagerCreator;
+import org.apache.uima.fit.util.JCasUtil;
+import org.apache.uima.jcas.JCas;
+import org.apache.uima.resource.ResourceInitializationException;
+import org.apache.uima.resource.ResourceManager;
+import org.apache.uima.ruta.type.FalsePositive;
+import org.apache.uima.ruta.type.TruePositive;
+import org.junit.Assert;
+import org.junit.Test;
+
+public class UimaClassLoaderTest {
+
+  @Test
+  public void test() throws Exception {
+    URL url = UimaClassLoaderTest.class
+            .getResource("/org/apache/uima/ruta/engine/UimafitTest.ruta");
+    final File cpDir = new File(url.toURI()).getParentFile();
+
+    ResourceManagerFactory.setResourceManagerCreator(new ResourceManagerCreator() {
+
+      @Override
+      public ResourceManager newResourceManager() throws ResourceInitializationException {
+        ResourceManager resourceManager = null;
+        try {
+          resourceManager = UIMAFramework.newDefaultResourceManager();
+          resourceManager.setExtensionClassPath(this.getClass().getClassLoader(), cpDir.getAbsolutePath(), true);
+          resourceManager.setDataPath("datapath");
+        } catch (MalformedURLException e) {
+          throw new ResourceInitializationException(e);
+        }
+        return resourceManager;
+      }
+    });
+
+    AnalysisEngine ae = AnalysisEngineFactory.createEngine(RutaEngine.class,
+            RutaEngine.PARAM_MAIN_SCRIPT, "UimafitTest");
+    JCas jcas = ae.newJCas();
+    jcas.setDocumentText("This is a test.");
+    new TruePositive(jcas, 0, 4).addToIndexes();
+    ae.process(jcas);
+    Collection<FalsePositive> select = JCasUtil.select(jcas, FalsePositive.class);
+    Assert.assertTrue(!select.isEmpty());
+  }
+
+}

Propchange: uima/ruta/trunk/ruta-core/src/test/java/org/apache/uima/ruta/engine/UimaClassLoaderTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: uima/ruta/trunk/ruta-docbook/src/docbook/tools.ruta.overview.xml
URL: http://svn.apache.org/viewvc/uima/ruta/trunk/ruta-docbook/src/docbook/tools.ruta.overview.xml?rev=1766517&r1=1766516&r2=1766517&view=diff
==============================================================================
--- uima/ruta/trunk/ruta-docbook/src/docbook/tools.ruta.overview.xml (original)
+++ uima/ruta/trunk/ruta-docbook/src/docbook/tools.ruta.overview.xml Tue Oct 25 11:43:31 2016
@@ -805,6 +805,14 @@ Document{-> EXEC(MyAnalysisEngine, {MyTy
                 </row>
                 <row>
                   <entry>
+                    <link linkend='ugr.tools.ruta.ae.basic.parameter.modifyDataPath'>modifyDataPath</link>
+                  </entry>
+                  <entry>Option to extend the datapath by the descriptorPaths
+                  </entry>
+                  <entry>Single Boolean</entry>
+                </row>
+                <row>
+                  <entry>
                     <link linkend='ugr.tools.ruta.ae.basic.parameter.strictImports'>strictImports</link>
                   </entry>
                   <entry>Option to restrict short type names resolution to those in the declared typesystems.
@@ -1038,6 +1046,13 @@ Document{-> EXEC(MyAnalysisEngine, {MyTy
             Default value is uima.tcas.Annotation
           </para>
         </section>
+        <section id="ugr.tools.ruta.ae.basic.parameter.modifyDataPath">
+          <title>modifyDataPath</title>
+          <para>
+            This parameter specifies whether the datapath of the ResourceManager is extended by the values of the configuration parameter <code>descriptorPaths</code>.
+            The default value is set to false.
+          </para>
+        </section>
         <section id="ugr.tools.ruta.ae.basic.parameter.strictImports">
           <title>strictImports</title>
           <para>