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 2008/02/28 16:08:34 UTC

svn commit: r631990 - in /incubator/uima/uimaj/trunk/uimaj-core/src: main/java/org/apache/uima/util/ test/java/org/apache/uima/util/ test/resources/CasCreationUtilsTest/

Author: alally
Date: Thu Feb 28 07:08:32 2008
New Revision: 631990

URL: http://svn.apache.org/viewvc?rev=631990&view=rev
Log:
UIMA-826: https://issues.apache.org/jira/browse/UIMA-826
Fixed mergeTypeSystems to properly handle a type declared twice with
different supertypes.  Includes test case.

Added:
    incubator/uima/uimaj/trunk/uimaj-core/src/test/resources/CasCreationUtilsTest/SupertypeMergeTest1.xml
    incubator/uima/uimaj/trunk/uimaj-core/src/test/resources/CasCreationUtilsTest/SupertypeMergeTest2.xml
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?rev=631990&r1=631989&r2=631990&view=diff
==============================================================================
--- 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 Thu Feb 28 07:08:32 2008
@@ -1037,8 +1037,18 @@
     // also build a Map from Type names to Types
     Map typeNameMap = new HashMap();
 
-    // Now iterate through all type systems - if a new type is found, add it.
-    // If an existing type is found, merge features.
+    // Iterate through all type systems and add types to the merged TypeSystem.
+    // If a type is defined more than once, we need to check if the superType
+    // declarations are compatible (one inherits from another), and merge the
+    // features.
+    
+    // In order to propertly handle the supertype merging, we need to make sure
+    // that we process the supertype definitions before the subtypes. To do this,
+    // we build a linked list of type descriptions, and make multiple passes
+    // over this, adding types to the merged type system when their supertypes
+    // become defined.  We continue until the list is empty or we cannot make any
+    // progress.
+    LinkedList typeList = new LinkedList();
     Iterator it = aTypeSystems.iterator();
     while (it.hasNext()) {
       TypeSystemDescription ts = (TypeSystemDescription) it.next();
@@ -1049,63 +1059,86 @@
           throw new ResourceInitializationException(e);
         }
         TypeDescription[] types = ts.getTypes();
-        if (types != null) {
-          for (int i = 0; i < types.length; i++) {
-            String typeName = types[i].getName();
-            TypeDescription existingType = (TypeDescription) typeNameMap.get(typeName);
-            if (existingType == null) {
-              // create new type
-              existingType = result.addType(types[i].getName(), types[i].getDescription(), types[i]
-                  .getSupertypeName());
-              existingType.setAllowedValues(types[i].getAllowedValues());
-              existingType.setSourceUrl(types[i].getSourceUrl());
-              typeNameMap.put(types[i].getName(), existingType);
-              FeatureDescription[] features = types[i].getFeatures();
-              if (features != null) {
-                mergeFeatures(existingType, types[i].getFeatures());
-              }
-            } else {
-              // type already existed - check that supertypes are compatible
-              String supertypeName = types[i].getSupertypeName();
-              String existingSupertypeName = existingType.getSupertypeName();
-              if (!existingSupertypeName.equals(supertypeName)) {
-                // supertypes are not identical - check if one subsumes the other
-                if (subsumes(existingSupertypeName, supertypeName, typeNameMap)) {
-                  // existing supertype subsumes newly specified supertype -
-                  // reset supertype to the new, more specific type
-                  existingType.setSupertypeName(supertypeName);
-                  // report that a merge occurred
-                  reportMerge(aOutputMergedTypes, types[i], existingType);
-                } else if (subsumes(supertypeName, existingSupertypeName, typeNameMap)) {
-                  // newly specified supertype subsumes old type, this is OK and we don't
-                  // need to do anything except report this
-                  reportMerge(aOutputMergedTypes, types[i], existingType);
-                } else {
-                  // error
-                  throw new ResourceInitializationException(
-                      ResourceInitializationException.INCOMPATIBLE_SUPERTYPES, new Object[] {
-                          typeName, supertypeName, existingSupertypeName,
-                          types[i].getSourceUrlString() });
-                }
+        typeList.addAll(Arrays.asList(types));
+      }
+    }
+    int lastNumTypes;
+    do {
+      lastNumTypes = typeList.size();
+      Iterator typeIter = typeList.iterator();
+      while (typeIter.hasNext()) {
+        TypeDescription type = (TypeDescription)typeIter.next();
+        String supertypeName = type.getSupertypeName();
+        if (supertypeName.startsWith("uima.") || typeNameMap.containsKey(supertypeName)) {
+          //supertype is defined, ok to proceed
+          //check if type is already defined 
+          addTypeToMergedTypeSystem(aOutputMergedTypes, result, typeNameMap, type);
+          typeIter.remove();
+        }
+      }
+    } while (typeList.size() > 0 && typeList.size() != lastNumTypes);
+      
+    //At this point, if the typeList is not empty, then we either have a type with an undefined supertype, or a cycle.
+    //We go ahead and merge the type definitions anway - these problems will be caught at CAS creation time. Undefined supertypes 
+    //may be OK at this stage - this type system will have to be further merged before it can be used.
+    Iterator typeIter = typeList.iterator();
+    while (typeIter.hasNext()) {
+      TypeDescription type = (TypeDescription)typeIter.next();
+      addTypeToMergedTypeSystem(aOutputMergedTypes, result, typeNameMap, type);
+    }    
+    return result;
+  }
 
-              }
-              // merge features
-              int prevNumFeatures = existingType.getFeatures().length;
-              FeatureDescription[] features = types[i].getFeatures();
-              if (features != null) {
-                mergeFeatures(existingType, types[i].getFeatures());
-                // if feature-merged occurred, the number of features on the type will have
-                // changed. Report this by adding to the aOutputMergedTypeNames collection.
-                if (existingType.getFeatures().length != prevNumFeatures) {
-                  reportMerge(aOutputMergedTypes, types[i], existingType);
-                }
-              }
-            }
-          }
+  private static void addTypeToMergedTypeSystem(Map aOutputMergedTypes, TypeSystemDescription result, Map typeNameMap, TypeDescription type) throws ResourceInitializationException {
+    String typeName = type.getName();
+    String supertypeName = type.getSupertypeName();
+    TypeDescription existingType = (TypeDescription) typeNameMap.get(typeName);
+    if (existingType == null) {
+      // create new type
+      existingType = result.addType(typeName, type.getDescription(), supertypeName);
+      existingType.setAllowedValues(type.getAllowedValues());
+      existingType.setSourceUrl(type.getSourceUrl());
+      typeNameMap.put(type.getName(), existingType);
+      FeatureDescription[] features = type.getFeatures();
+      if (features != null) {
+        mergeFeatures(existingType, type.getFeatures());
+      }
+    } else {
+      // type already existed - check that supertypes are compatible
+      String existingSupertypeName = existingType.getSupertypeName();
+      if (!existingSupertypeName.equals(supertypeName)) {
+        // supertypes are not identical - check if one subsumes the other
+        if (subsumes(existingSupertypeName, supertypeName, typeNameMap)) {
+          // existing supertype subsumes newly specified supertype -
+          // reset supertype to the new, more specific type
+          existingType.setSupertypeName(supertypeName);
+          // report that a merge occurred
+          reportMerge(aOutputMergedTypes, type, existingType);
+        } else if (subsumes(supertypeName, existingSupertypeName, typeNameMap)) {
+          // newly specified supertype subsumes old type, this is OK and we don't
+          // need to do anything except report this
+          reportMerge(aOutputMergedTypes, type, existingType);
+        } else {
+          // error
+          throw new ResourceInitializationException(
+              ResourceInitializationException.INCOMPATIBLE_SUPERTYPES, new Object[] {
+                  typeName, supertypeName, existingSupertypeName,
+                  type.getSourceUrlString() });
+        }
+      }
+      // merge features
+      int prevNumFeatures = existingType.getFeatures().length;
+      FeatureDescription[] features = type.getFeatures();
+      if (features != null) {
+        mergeFeatures(existingType, type.getFeatures());
+        // if feature-merged occurred, the number of features on the type will have
+        // changed. Report this by adding to the aOutputMergedTypeNames collection.
+        if (existingType.getFeatures().length != prevNumFeatures) {
+          reportMerge(aOutputMergedTypes, type, existingType);
         }
       }
+      
     }
-    return result;
   }
 
   /**

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?rev=631990&r1=631989&r2=631990&view=diff
==============================================================================
--- 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 Thu Feb 28 07:08:32 2008
@@ -168,6 +168,37 @@
       JUnitExtension.handleException(e);
     }
   }
+  
+  public void testMergeTypeSystemsWithDifferentSupertypes() throws Exception {
+    try {
+      TypeSystemDescription ts1desc = UIMAFramework.getXMLParser().parseTypeSystemDescription(
+              new XMLInputSource(JUnitExtension.getFile("CasCreationUtilsTest/SupertypeMergeTest1.xml")));
+      assertEquals("uima.tcas.Annotation", ts1desc.getType("Sub").getSupertypeName());
+      TypeSystemDescription ts2desc = UIMAFramework.getXMLParser().parseTypeSystemDescription(
+              new XMLInputSource(JUnitExtension.getFile("CasCreationUtilsTest/SupertypeMergeTest2.xml")));
+      assertEquals("Super", ts2desc.getType("Sub").getSupertypeName());
+
+      ArrayList tsList = new ArrayList();
+      tsList.add(ts1desc);
+      tsList.add(ts2desc);
+      TypeSystemDescription merged = CasCreationUtils.mergeTypeSystems(tsList, UIMAFramework
+              .newDefaultResourceManager());
+      assertEquals("Super", merged.getType("Sub").getSupertypeName());
+
+      // try merging in the other order - bug UIMA-826 was an order depenency in the behavior of
+      // this kind of merging
+      tsList = new ArrayList();
+      tsList.add(ts2desc);
+      tsList.add(ts1desc);
+      merged = CasCreationUtils.mergeTypeSystems(tsList, UIMAFramework
+              .newDefaultResourceManager());
+      assertEquals("Super", merged.getType("Sub").getSupertypeName());
+
+    } catch (Exception e) {
+      JUnitExtension.handleException(e);
+    }
+  }
+  
 
   public void testAggregateWithImports() throws Exception {
     try {

Added: incubator/uima/uimaj/trunk/uimaj-core/src/test/resources/CasCreationUtilsTest/SupertypeMergeTest1.xml
URL: http://svn.apache.org/viewvc/incubator/uima/uimaj/trunk/uimaj-core/src/test/resources/CasCreationUtilsTest/SupertypeMergeTest1.xml?rev=631990&view=auto
==============================================================================
--- incubator/uima/uimaj/trunk/uimaj-core/src/test/resources/CasCreationUtilsTest/SupertypeMergeTest1.xml (added)
+++ incubator/uima/uimaj/trunk/uimaj-core/src/test/resources/CasCreationUtilsTest/SupertypeMergeTest1.xml Thu Feb 28 07:08:32 2008
@@ -0,0 +1,31 @@
+<typeSystemDescription xmlns="http://uima.apache.org/resourceSpecifier">
+	
+        <!--
+	 ***************************************************************
+	 * 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.
+	 ***************************************************************
+   -->
+
+<types>
+<typeDescription>
+<name>Sub</name>
+<description>Example type.</description>
+<supertypeName>uima.tcas.Annotation</supertypeName>
+</typeDescription>
+</types>
+</typeSystemDescription>

Added: incubator/uima/uimaj/trunk/uimaj-core/src/test/resources/CasCreationUtilsTest/SupertypeMergeTest2.xml
URL: http://svn.apache.org/viewvc/incubator/uima/uimaj/trunk/uimaj-core/src/test/resources/CasCreationUtilsTest/SupertypeMergeTest2.xml?rev=631990&view=auto
==============================================================================
--- incubator/uima/uimaj/trunk/uimaj-core/src/test/resources/CasCreationUtilsTest/SupertypeMergeTest2.xml (added)
+++ incubator/uima/uimaj/trunk/uimaj-core/src/test/resources/CasCreationUtilsTest/SupertypeMergeTest2.xml Thu Feb 28 07:08:32 2008
@@ -0,0 +1,36 @@
+<typeSystemDescription xmlns="http://uima.apache.org/resourceSpecifier">
+	
+        <!--
+	 ***************************************************************
+	 * 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.
+	 ***************************************************************
+   -->
+
+<types>
+<typeDescription>
+<name>Sub</name>
+<description>Example type.</description>
+<supertypeName>Super</supertypeName>
+</typeDescription>
+<typeDescription>
+<name>Super</name>
+<description>Example type.</description>
+<supertypeName>uima.tcas.Annotation</supertypeName>
+</typeDescription>
+</types>
+</typeSystemDescription>