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

pkluegl pushed a commit to branch 
bugfix/UIMA-6393-Circular-imports-break-resource-manager-cache_pk
in repository https://gitbox.apache.org/repos/asf/uima-uimaj.git


The following commit(s) were added to 
refs/heads/bugfix/UIMA-6393-Circular-imports-break-resource-manager-cache_pk by 
this push:
     new 55d48a8  [UIMA-6393]: Circular imports break resource manager cache PK
55d48a8 is described below

commit 55d48a8d23fefdb8449db8d1e37b9b53731ba8bb
Author: Peter Klügl <pklu...@apache.org>
AuthorDate: Sun Nov 7 22:51:18 2021 +0100

    [UIMA-6393]: Circular imports break resource manager cache PK
    
    - fixed and refactored algorithm
---
 .../metadata/impl/TypeSystemDescription_impl.java  | 43 ++++++++++------------
 .../impl/TypeSystemDescription_implTest.java       |  5 ++-
 2 files changed, 23 insertions(+), 25 deletions(-)

diff --git 
a/uimaj-core/src/main/java/org/apache/uima/resource/metadata/impl/TypeSystemDescription_impl.java
 
b/uimaj-core/src/main/java/org/apache/uima/resource/metadata/impl/TypeSystemDescription_impl.java
index 32e8a06..f27b26b 100644
--- 
a/uimaj-core/src/main/java/org/apache/uima/resource/metadata/impl/TypeSystemDescription_impl.java
+++ 
b/uimaj-core/src/main/java/org/apache/uima/resource/metadata/impl/TypeSystemDescription_impl.java
@@ -211,6 +211,7 @@ public class TypeSystemDescription_impl extends 
MetaDataObject_impl
   public synchronized void resolveImports(Collection<String> 
aAlreadyImportedTypeSystemURLs,
           ResourceManager aResourceManager) throws InvalidXMLException {
 
+    // TODO what to do with aAlreadyImportedTypeSystemURLs???
     List<TypeDescription> result = new ArrayList<>();
 
     if (getImports() == null || getImports().length == 0) {
@@ -219,59 +220,55 @@ public class TypeSystemDescription_impl extends 
MetaDataObject_impl
 
     Deque<String> typeSystemStack = new LinkedList<>();
     typeSystemStack.push(getSourceUrlString());
-    aAlreadyImportedTypeSystemURLs.add(getSourceUrlString());
-
-    resolveImports(result, typeSystemStack, aAlreadyImportedTypeSystemURLs, 
aResourceManager);
+    resolveImports(result, typeSystemStack, aResourceManager);
     typeSystemStack.pop();
 
     setTypes(result.toArray(new TypeDescription_impl[0]));
     setImports(Import.EMPTY_IMPORTS);
   }
 
-  private boolean resolveImports(List<TypeDescription> result, Deque<String> 
stack,
-          Collection<String> visited, ResourceManager aResourceManager) throws 
InvalidXMLException {
+  private boolean resolveImports(List<TypeDescription> result, Deque<String> 
stack, ResourceManager aResourceManager) throws InvalidXMLException {
+
+    Import[] imports = getImports();
+
+    if (imports.length == 0) {
+      result.addAll(Arrays.asList(getTypes()));
+      return true;
+    }
 
     List<Import> unresolvedImports = new ArrayList<>();
     List<TypeDescription> resolvedTypeDescriptions = new ArrayList<>();
     resolvedTypeDescriptions.addAll(Arrays.asList(getTypes()));
 
-    boolean completelyResolved = true;
-
-    Import[] imports = getImports();
     for (Import tsImport : imports) {
 
       URL url = tsImport.findAbsoluteUrl(aResourceManager);
       String urlString = url.toString();
 
-      if (visited.contains(urlString)) {
-        // cycle detected? -> completelyResolved = false, not able to 
completely resolve imports
-        // right now
-        completelyResolved &= !stack.contains(urlString);
+      if (stack.contains(urlString)) {
+        unresolvedImports.add(tsImport);
         continue;
       }
-      stack.push(urlString);
-      visited.add(urlString);
-
+      
       TypeSystemDescription_impl importedDescription = 
getTypeSystemDescription(url,
               aResourceManager);
+      stack.push(urlString);
       boolean importCompletelyResolved = importedDescription
-              .resolveImports(resolvedTypeDescriptions, stack, visited, 
aResourceManager);
+              .resolveImports(resolvedTypeDescriptions, stack, 
aResourceManager);
       if (!importCompletelyResolved) {
         // TODO: update importUrlsCache? here or at all?
         unresolvedImports.add(tsImport);
       }
-      completelyResolved &= importCompletelyResolved;
       stack.pop();
     }
 
-    if (imports.length != unresolvedImports.size()) {
-      // update own resolved status
-      setTypes(resolvedTypeDescriptions.toArray(new TypeDescription_impl[0]));
-      setImports(unresolvedImports.toArray(new Import[0]));
-    }
+    // update own resolved status
+    setTypes(resolvedTypeDescriptions.toArray(new TypeDescription_impl[0]));
+    setImports(unresolvedImports.toArray(new Import[0]));
+
     result.addAll(resolvedTypeDescriptions);
 
-    return completelyResolved;
+    return unresolvedImports.isEmpty();
   }
 
   private TypeSystemDescription_impl getTypeSystemDescription(URL url,
diff --git 
a/uimaj-core/src/test/java/org/apache/uima/resource/metadata/impl/TypeSystemDescription_implTest.java
 
b/uimaj-core/src/test/java/org/apache/uima/resource/metadata/impl/TypeSystemDescription_implTest.java
index 6968183..3187c68 100644
--- 
a/uimaj-core/src/test/java/org/apache/uima/resource/metadata/impl/TypeSystemDescription_implTest.java
+++ 
b/uimaj-core/src/test/java/org/apache/uima/resource/metadata/impl/TypeSystemDescription_implTest.java
@@ -51,6 +51,7 @@ import org.apache.uima.UIMAFramework;
 import org.apache.uima.cas.CAS;
 import org.apache.uima.resource.ResourceInitializationException;
 import org.apache.uima.resource.ResourceManager;
+import org.apache.uima.resource.impl.ResourceManager_impl;
 import org.apache.uima.resource.metadata.FeatureDescription;
 import org.apache.uima.resource.metadata.Import;
 import org.apache.uima.resource.metadata.TypeDescription;
@@ -330,7 +331,7 @@ public class TypeSystemDescription_implTest {
               .containsAll(e.getValue());
     }
   }
-
+  
   @Test
   public void thatCircularImportsDoNotCrash() throws Exception {
     File descriptor = 
getFile("TypeSystemDescriptionImplTest/Loop-with-2-nodes-1.xml");
@@ -376,7 +377,7 @@ public class TypeSystemDescription_implTest {
     TypeSystemDescription cachedCircular3Tsd = (TypeSystemDescription) cache
             .get(circular3.toURI().toURL().toString());
     assertThat(ts.getTypes()).hasSize(3);
-    assertThat(cachedCircular2Tsd.getTypes()).hasSize(1);
+    assertThat(cachedCircular2Tsd.getTypes()).hasSize(2);
     assertThat(cachedCircular3Tsd.getTypes()).hasSize(1);
   }
 

Reply via email to