You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@uima.apache.org by re...@apache.org on 2024/11/14 14:34:52 UTC

(uima-ruta) 01/01: Issue #177: Consider resource manager when loading type descriptions

This is an automated email from the ASF dual-hosted git repository.

rec pushed a commit to branch feature/177-Consider-resource-manager-when-loading-type-descriptions
in repository https://gitbox.apache.org/repos/asf/uima-ruta.git

commit 9f9ce2c4b54e365f80ff7d18accbe8d71b8759ca
Author: Richard Eckart de Castilho <re...@apache.org>
AuthorDate: Thu Nov 14 15:34:42 2024 +0100

    Issue #177: Consider resource manager when loading type descriptions
    
    - Enhance the RutaResourceLoader to consider an explicitly or implicitly provided resource manager for locating type system descriptions
---
 .../java/org/apache/uima/ruta/RutaEnvironment.java |  2 +-
 .../ruta/descriptor/RutaDescriptorBuilder.java     | 15 +++++---
 .../org/apache/uima/ruta/engine/RutaEngine.java    |  4 +--
 .../uima/ruta/resource/RutaResourceLoader.java     | 40 +++++++++++++++++++--
 .../uima/ruta/type/spi/RutaBasicTypeProvider.java  | 35 ------------------
 .../spi/RutaTypeSystemDescriptionProvider.java     | 41 +++-------------------
 .../services/org.apache.uima.spi.JCasClassProvider |  2 --
 ...g.apache.uima.spi.TypeSystemDescriptionProvider |  1 -
 ruta-parent/pom.xml                                | 16 ++-------
 9 files changed, 57 insertions(+), 99 deletions(-)

diff --git a/ruta-core/src/main/java/org/apache/uima/ruta/RutaEnvironment.java b/ruta-core/src/main/java/org/apache/uima/ruta/RutaEnvironment.java
index ae77be9a..d9b514e0 100644
--- a/ruta-core/src/main/java/org/apache/uima/ruta/RutaEnvironment.java
+++ b/ruta-core/src/main/java/org/apache/uima/ruta/RutaEnvironment.java
@@ -717,7 +717,7 @@ public class RutaEnvironment {
     RutaTable result = tables.get(table);
     if (result == null) {
       if (table.endsWith("csv") || table.endsWith("txt") || table.endsWith("tsv")) {
-        ResourceLoader resourceLoader = new RutaResourceLoader(getResourcePaths(),
+        var resourceLoader = new RutaResourceLoader(getResourceManager(), getResourcePaths(),
                 getResourceManager().getExtensionClassLoader());
         Resource resource = resourceLoader.getResource(table);
         if (resource.exists()) {
diff --git a/ruta-core/src/main/java/org/apache/uima/ruta/descriptor/RutaDescriptorBuilder.java b/ruta-core/src/main/java/org/apache/uima/ruta/descriptor/RutaDescriptorBuilder.java
index 320a32e8..1322b9c7 100644
--- a/ruta-core/src/main/java/org/apache/uima/ruta/descriptor/RutaDescriptorBuilder.java
+++ b/ruta-core/src/main/java/org/apache/uima/ruta/descriptor/RutaDescriptorBuilder.java
@@ -84,12 +84,13 @@ public class RutaDescriptorBuilder {
           throws InvalidXMLException, ResourceInitializationException, IOException,
           URISyntaxException {
 
-    TypeSystemDescription typeSystemDescription = uimaFactory.createTypeSystemDescription();
+    var typeSystemDescription = uimaFactory.createTypeSystemDescription();
 
     ResourceManager rm = UIMAFramework.newDefaultResourceManager();
     if (options.getClassLoader() != null) {
       rm = new ResourceManager_impl(options.getClassLoader());
     }
+
     if (enginePaths != null) {
       String dataPath = "";
       for (String string : enginePaths) {
@@ -97,12 +98,15 @@ public class RutaDescriptorBuilder {
       }
       rm.setDataPath(dataPath);
     }
-    Map<String, String> typeNameMap = new HashMap<>();
-    TypeSystemDescription initialTypeSystem = UIMAFramework.getXMLParser()
+
+    var typeNameMap = new HashMap<String, String>();
+    var initialTypeSystem = UIMAFramework.getXMLParser()
             .parseTypeSystemDescription(new XMLInputSource(defaultTypeSystem));
+
     CAS cas = CasCreationUtils.createCas(initialTypeSystem, null, new FsIndexDescription[0]);
     fillTypeNameMap(typeNameMap, cas.getTypeSystem());
     cas.release();
+
     List<TypeSystemDescription> toInclude = new ArrayList<>();
     List<Import> importList = new ArrayList<>();
     Import_impl import_impl = new Import_impl();
@@ -132,16 +136,18 @@ public class RutaDescriptorBuilder {
     }
     addImportIfValid(importList, import_impl);
 
-    RutaResourceLoader descriptorRutaResourceLoader = new RutaResourceLoader(enginePaths,
+    var descriptorRutaResourceLoader = new RutaResourceLoader(rm, enginePaths,
             options.getClassLoader());
 
     for (String eachName : desc.getImportedTypeSystems()) {
       Resource resource = descriptorRutaResourceLoader.getResourceWithDotNotation(eachName, ".xml");
+
       URL url = null;
       boolean include = false;
       if (resource != null) {
         url = resource.getURL();
       }
+
       if (url == null) {
         url = checkImportExistence(eachName, ".xml", options.getClassLoader());
         include = true;
@@ -150,6 +156,7 @@ public class RutaDescriptorBuilder {
                   "Build process can't find " + eachName + " in " + desc.getScriptName());
         }
       }
+
       TypeSystemDescription each = getTypeSystemDescriptor(url, rm);
       if (each != null) {
         fillTypeNameMap(typeNameMap, each);
diff --git a/ruta-core/src/main/java/org/apache/uima/ruta/engine/RutaEngine.java b/ruta-core/src/main/java/org/apache/uima/ruta/engine/RutaEngine.java
index 9ea17fd2..1c86fbe7 100644
--- a/ruta-core/src/main/java/org/apache/uima/ruta/engine/RutaEngine.java
+++ b/ruta-core/src/main/java/org/apache/uima/ruta/engine/RutaEngine.java
@@ -653,9 +653,9 @@ public class RutaEngine extends JCasAnnotator_ImplBase {
     initializeResourceManager();
     handleDataPath();
 
-    scriptRutaResourceLoader = new RutaResourceLoader(scriptPaths,
+    scriptRutaResourceLoader = new RutaResourceLoader(resourceManager, scriptPaths,
             resourceManager.getExtensionClassLoader());
-    descriptorRutaResourceLoader = new RutaResourceLoader(descriptorPaths,
+    descriptorRutaResourceLoader = new RutaResourceLoader(resourceManager, descriptorPaths,
             resourceManager.getExtensionClassLoader());
 
     if (!externalFactory.isInitialized()) {
diff --git a/ruta-core/src/main/java/org/apache/uima/ruta/resource/RutaResourceLoader.java b/ruta-core/src/main/java/org/apache/uima/ruta/resource/RutaResourceLoader.java
index 71356ff7..d844e4b2 100644
--- a/ruta-core/src/main/java/org/apache/uima/ruta/resource/RutaResourceLoader.java
+++ b/ruta-core/src/main/java/org/apache/uima/ruta/resource/RutaResourceLoader.java
@@ -19,9 +19,15 @@
 
 package org.apache.uima.ruta.resource;
 
+import org.apache.uima.UIMAFramework;
+import org.apache.uima.UimaContextAdmin;
+import org.apache.uima.UimaContextHolder;
+import org.apache.uima.resource.ResourceManager;
+import org.apache.uima.util.InvalidXMLException;
 import org.springframework.core.io.DefaultResourceLoader;
 import org.springframework.core.io.Resource;
 import org.springframework.core.io.ResourceLoader;
+import org.springframework.core.io.UrlResource;
 import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
 
 /**
@@ -32,6 +38,8 @@ import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
  */
 public class RutaResourceLoader implements ResourceLoader {
 
+  private final ResourceManager resMgr;
+
   private final ResourceLoader wrapped;
 
   private final ResourceLoader fallback;
@@ -46,7 +54,8 @@ public class RutaResourceLoader implements ResourceLoader {
    * @param classLoader
    *          optional classloader for fallback resource loader
    */
-  public RutaResourceLoader(String[] paths, ClassLoader classLoader) {
+  public RutaResourceLoader(ResourceManager resMgr, String[] paths, ClassLoader classLoader) {
+    this.resMgr = resMgr;
     this.wrapped = new PathMatchingResourcePatternResolver(new ResourcePathResourceLoader(
             paths));
     if(classLoader == null) {
@@ -58,12 +67,16 @@ public class RutaResourceLoader implements ResourceLoader {
     this.fallback2 = new DefaultResourceLoader();
   }
 
+  public RutaResourceLoader(String[] paths, ClassLoader classLoader) {
+    this(null, paths, classLoader);
+  }
+
   /**
    * @param paths
    *          paths to search in priority.
    */
   public RutaResourceLoader(String[] paths) {
-    this(paths, null);
+    this(null, paths, null);
   }
 
 
@@ -84,7 +97,28 @@ public class RutaResourceLoader implements ResourceLoader {
 
   public Resource getResourceWithDotNotation(String location, String extension) {
     String path = location.replaceAll("[.]", "/") + extension;
-    return getResource(path);
+    var resource = getResource(path);
+
+    var rm = resMgr;
+    if (rm == null && UimaContextHolder.getContext() instanceof UimaContextAdmin ctxAdm) {
+      rm = ctxAdm.getResourceManager();
+    }
+
+    if (rm != null && !resource.exists()) {
+      try {
+        var descImport = UIMAFramework.getResourceSpecifierFactory().createImport();
+        descImport.setName(location);
+        var url = descImport.findAbsoluteUrl(rm);
+        if (url != null) {
+          return new UrlResource(url);
+        }
+      } catch (InvalidXMLException e) {
+        // Unable to resolve the resource through the resource manager - let's fall back to the
+        // non-existing resource from earlier
+      }
+    }
+
+    return resource;
   }
 
   @Override
diff --git a/ruta-core/src/main/java/org/apache/uima/ruta/type/spi/RutaBasicTypeProvider.java b/ruta-core/src/main/java/org/apache/uima/ruta/type/spi/RutaBasicTypeProvider.java
deleted file mode 100644
index f1417ccd..00000000
--- a/ruta-core/src/main/java/org/apache/uima/ruta/type/spi/RutaBasicTypeProvider.java
+++ /dev/null
@@ -1,35 +0,0 @@
-/*
- * 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.type.spi;
-
-import static java.util.Arrays.asList;
-
-import java.util.List;
-
-import org.apache.uima.jcas.cas.TOP;
-import org.apache.uima.ruta.type.RutaBasic;
-import org.apache.uima.spi.JCasClassProvider;
-
-public class RutaBasicTypeProvider implements JCasClassProvider {
-
-  @Override
-  public List<Class<? extends TOP>> listJCasClasses() {
-    return asList(RutaBasic.class);
-  }
-}
diff --git a/ruta-core/src/main/java/org/apache/uima/ruta/type/spi/RutaTypeSystemDescriptionProvider.java b/ruta-core/src/main/java/org/apache/uima/ruta/type/spi/RutaTypeSystemDescriptionProvider.java
index 8739b736..377a04a4 100644
--- a/ruta-core/src/main/java/org/apache/uima/ruta/type/spi/RutaTypeSystemDescriptionProvider.java
+++ b/ruta-core/src/main/java/org/apache/uima/ruta/type/spi/RutaTypeSystemDescriptionProvider.java
@@ -18,24 +18,12 @@
  */
 package org.apache.uima.ruta.type.spi;
 
-import static org.apache.uima.util.TypeSystemUtil.loadTypeSystemDescriptionsFromClasspath;
+import org.apache.uima.spi.TypeSystemProvider_ImplBase;
 
-import java.util.ArrayList;
-import java.util.List;
+public class RutaTypeSystemDescriptionProvider extends TypeSystemProvider_ImplBase {
 
-import org.apache.uima.jcas.cas.TOP;
-import org.apache.uima.resource.metadata.TypeDescription;
-import org.apache.uima.resource.metadata.TypeSystemDescription;
-import org.apache.uima.spi.JCasClassProvider;
-import org.apache.uima.spi.TypeSystemDescriptionProvider;
-
-public class RutaTypeSystemDescriptionProvider
-        implements TypeSystemDescriptionProvider, JCasClassProvider {
-
-  @Override
-  public List<TypeSystemDescription> listTypeSystemDescriptions() {
-
-    return loadTypeSystemDescriptionsFromClasspath(getClass(), //
+  public RutaTypeSystemDescriptionProvider() {
+    setTypeSystemLocations( //
             "/org/apache/uima/ruta/engine/BasicTypeSystem.xml", //
             "/org/apache/uima/ruta/engine/DefaultSeederTypeSystem.xml", //
             "/org/apache/uima/ruta/engine/HtmlTypeSystem.xml", //
@@ -44,25 +32,4 @@ public class RutaTypeSystemDescriptionProvider
             "/org/apache/uima/ruta/engine/RutaInternalTypeSystem.xml", //
             "/org/apache/uima/ruta/engine/SourceDocumentInformation.xml");
   }
-
-  @SuppressWarnings("unchecked")
-@Override
-  public List<Class<? extends TOP>> listJCasClasses() {
-
-    List<Class<? extends TOP>> classes = new ArrayList<>();
-    ClassLoader cl = getClass().getClassLoader();
-
-    List<TypeSystemDescription> typeSystemDescriptions = listTypeSystemDescriptions();
-    for (TypeSystemDescription tsd : typeSystemDescriptions) {
-      for (TypeDescription td : tsd.getTypes()) {
-        try {
-          classes.add((Class<? extends TOP>) cl.loadClass(td.getName()));
-        } catch (ClassNotFoundException e) {
-          // This is acceptable - there may not be a JCas class
-        }
-      }
-    }
-
-    return classes;
-  }
 }
diff --git a/ruta-core/src/main/resources/META-INF/services/org.apache.uima.spi.JCasClassProvider b/ruta-core/src/main/resources/META-INF/services/org.apache.uima.spi.JCasClassProvider
deleted file mode 100644
index 940694a3..00000000
--- a/ruta-core/src/main/resources/META-INF/services/org.apache.uima.spi.JCasClassProvider
+++ /dev/null
@@ -1,2 +0,0 @@
-org.apache.uima.ruta.type.spi.RutaBasicTypeProvider
-org.apache.uima.ruta.type.spi.RutaTypeSystemDescriptionProvider
diff --git a/ruta-core/src/main/resources/META-INF/services/org.apache.uima.spi.TypeSystemDescriptionProvider b/ruta-core/src/main/resources/META-INF/services/org.apache.uima.spi.TypeSystemDescriptionProvider
deleted file mode 100644
index 9bfd15ab..00000000
--- a/ruta-core/src/main/resources/META-INF/services/org.apache.uima.spi.TypeSystemDescriptionProvider
+++ /dev/null
@@ -1 +0,0 @@
-org.apache.uima.ruta.type.spi.RutaTypeSystemDescriptionProvider
diff --git a/ruta-parent/pom.xml b/ruta-parent/pom.xml
index 403b0225..4b697e16 100644
--- a/ruta-parent/pom.xml
+++ b/ruta-parent/pom.xml
@@ -52,6 +52,7 @@
       - The Eclipse Plugin modules use version ranges for their dependencies. These could resolve to
       - SNAPSHOT versions if we have a SNAPSHOT repo declaration here. Thus, this repo should only
       - be enabled when really needed.
+      -->
     <repository>
       <id>apache.snapshots</id>
       <name>Apache Snapshot Repository</name>
@@ -63,7 +64,6 @@
         <enabled>true</enabled>
       </snapshots>
     </repository>
-      -->
 
     <repository>
       <id>${eclipseP2RepoId}</id>
@@ -120,8 +120,7 @@
     <tycho-version>4.0.4</tycho-version>
 
 
-    <uima-version>3.5.0</uima-version>
-    <uimafit-version>3.5.0</uimafit-version>
+    <uima-version>3.6.0-SNAPSHOT</uima-version>
     <spring-version>5.3.30</spring-version>
     <slf4j-version>1.7.36</slf4j-version>
     <maven.version>3.8.1</maven.version>
@@ -211,17 +210,6 @@
         <type>pom</type>
         <scope>import</scope>
       </dependency>
-
-      <dependency>
-        <groupId>org.apache.uima</groupId>
-        <artifactId>uimafit-core</artifactId>
-        <version>${uimafit-version}</version>
-      </dependency>
-      <dependency>
-        <groupId>org.apache.uima</groupId>
-        <artifactId>uimafit-junit</artifactId>
-        <version>${uimafit-version}</version>
-      </dependency>
       
       <dependency>
         <groupId>org.antlr</groupId>