You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@uima.apache.org by al...@apache.org on 2006/11/17 18:10:46 UTC

svn commit: r476218 - in /incubator/uima/uimaj/trunk/uimaj-core/src: main/java/org/apache/uima/util/CasCreationUtils.java test/java/org/apache/uima/util/CasCreationUtilsTest.java

Author: alally
Date: Fri Nov 17 09:10:45 2006
New Revision: 476218

URL: http://svn.apache.org/viewvc?view=rev&rev=476218
Log:
JIRA issue UIMA-46: Duplicate feature name on supertype and subtype does not work if subtype definition comes first in descriptor.

Now the feature will be added to the supertype regardless of the definition order.

Modified:
    incubator/uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/util/CasCreationUtils.java
    incubator/uima/uimaj/trunk/uimaj-core/src/test/java/org/apache/uima/util/CasCreationUtilsTest.java

Modified: incubator/uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/util/CasCreationUtils.java
URL: http://svn.apache.org/viewvc/incubator/uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/util/CasCreationUtils.java?view=diff&rev=476218&r1=476217&r2=476218
==============================================================================
--- incubator/uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/util/CasCreationUtils.java (original)
+++ incubator/uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/util/CasCreationUtils.java Fri Nov 17 09:10:45 2006
@@ -916,6 +916,7 @@
           typeList.addAll(Arrays.asList(types));
           int numTypes = typeList.size();
           int lastNumTypes;
+          LinkedList typesInOrderOfCreation = new LinkedList();
           do
           {
             lastNumTypes = numTypes;
@@ -965,8 +966,9 @@
                   }                  
                   typeSystemMgr.addType(typeName, supertype);
                 }
-                //remove from list of type descriptions
+                //remove from list of type descriptions and add it to the typesInOrderOfCreation list for later processing
                 it.remove();
+                typesInOrderOfCreation.add(curTypeDesc);
               }
             }
             numTypes = typeList.size();
@@ -986,13 +988,18 @@
                 firstFailed.getSourceUrlString()});
           }
 
-          //now for each type, add its features
-          for (int i = 0; i < types.length; i++)
+          //now for each type, add its features.  We add features to supertypes before subtypes.  This is done so that
+          //if we have a duplicate feature name on both a supertype and a subtype, it is added to the supertype and then
+          //ignored when we get to the subtype.  Although this is a dubious type system, we support it for backwards
+          //compatibility (but we might want to think about generating a warning).
+          Iterator typeIter = typesInOrderOfCreation.iterator();
+          while (typeIter.hasNext())
           {
-            Type type = typeSystemMgr.getType(types[i].getName());
+            TypeDescription typeDesc = (TypeDescription)typeIter.next();
+            Type type = typeSystemMgr.getType(typeDesc.getName());
             //            assert type != null;
 
-            FeatureDescription[] features = types[i].getFeatures();
+            FeatureDescription[] features = typeDesc.getFeatures();
             if (features != null)
             {
               for (int j = 0; j < features.length; j++)
@@ -1004,7 +1011,7 @@
                 {
                   throw new ResourceInitializationException(
                     ResourceInitializationException.UNDEFINED_RANGE_TYPE,
-                    new Object[] { rangeTypeName, featName, types[i].getName(),
+                    new Object[] { rangeTypeName, featName, typeDesc.getName(),
                         features[j].getSourceUrlString()});
                 }
                 if (rangeType.isArray()) //TODO: also List?
@@ -1019,7 +1026,7 @@
                     {
                       throw new ResourceInitializationException(
                           ResourceInitializationException.UNDEFINED_RANGE_TYPE,
-                          new Object[]{elementTypeName, featName, types[i].getName(),
+                          new Object[]{elementTypeName, featName, typeDesc.getName(),
                               features[j].getSourceUrlString()});                        
                     }
                     rangeType = typeSystemMgr.getArrayType(elementType);

Modified: incubator/uima/uimaj/trunk/uimaj-core/src/test/java/org/apache/uima/util/CasCreationUtilsTest.java
URL: http://svn.apache.org/viewvc/incubator/uima/uimaj/trunk/uimaj-core/src/test/java/org/apache/uima/util/CasCreationUtilsTest.java?view=diff&rev=476218&r1=476217&r2=476218
==============================================================================
--- incubator/uima/uimaj/trunk/uimaj-core/src/test/java/org/apache/uima/util/CasCreationUtilsTest.java (original)
+++ incubator/uima/uimaj/trunk/uimaj-core/src/test/java/org/apache/uima/util/CasCreationUtilsTest.java Fri Nov 17 09:10:45 2006
@@ -29,8 +29,11 @@
 import org.apache.uima.analysis_engine.AnalysisEngineDescription;
 import org.apache.uima.cas.Feature;
 import org.apache.uima.cas.Type;
+import org.apache.uima.cas.admin.CASFactory;
+import org.apache.uima.cas.admin.CASMgr;
 import org.apache.uima.cas.text.AnnotationFS;
 import org.apache.uima.cas.text.TCAS;
+import org.apache.uima.resource.ResourceInitializationException;
 import org.apache.uima.resource.ResourceManager;
 import org.apache.uima.resource.metadata.FsIndexCollection;
 import org.apache.uima.resource.metadata.FsIndexDescription;
@@ -38,6 +41,7 @@
 import org.apache.uima.resource.metadata.TypePriorities;
 import org.apache.uima.resource.metadata.TypePriorityList;
 import org.apache.uima.resource.metadata.TypeSystemDescription;
+import org.apache.uima.resource.metadata.impl.TypeSystemDescription_impl;
 import org.apache.uima.test.junit_extension.JUnitExtension;
 
 /**
@@ -302,4 +306,34 @@
       JUnitExtension.handleException(e);
     }
   }
+  
+  public void testSetupTypeSystem() throws Exception
+  {
+    try
+    {
+      //test that duplicate feature names on supertype and subtype works
+      //regardless of the order in which the types appear in the TypeSystemDescription
+      TypeSystemDescription tsd1 = new TypeSystemDescription_impl();
+      TypeDescription supertype = tsd1.addType("test.Super", "", "uima.cas.TOP");
+      supertype.addFeature("testfeat", "", "uima.cas.Integer");
+      TypeDescription subtype = tsd1.addType("test.Sub", "", "test.Super");
+      subtype.addFeature("testfeat", "", "uima.cas.Integer");    
+      
+      CASMgr casMgr = CASFactory.createCAS();
+      CasCreationUtils.setupTypeSystem(casMgr, tsd1);
+      assertNotNull(casMgr.getTypeSystemMgr().getType("test.Super").getFeatureByBaseName("testfeat"));
+
+      TypeSystemDescription tsd2 = new TypeSystemDescription_impl();
+      tsd2.setTypes(new TypeDescription[]{subtype,supertype});
+      
+      casMgr = CASFactory.createCAS();
+      CasCreationUtils.setupTypeSystem(casMgr, tsd2);
+      assertNotNull(casMgr.getTypeSystemMgr().getType("test.Super").getFeatureByBaseName("testfeat"));
+
+    }
+    catch (ResourceInitializationException e)
+    {
+      JUnitExtension.handleException(e);
+    }
+  }  
 }