You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@uima.apache.org by sc...@apache.org on 2015/01/21 23:46:52 UTC

svn commit: r1653684 - in /uima/uimaj/trunk/uimaj-core/src: main/java/org/apache/uima/cas/impl/TypeSystemImpl.java main/java/org/apache/uima/util/CasCopier.java test/java/org/apache/uima/util/CasCopierTest.java

Author: schor
Date: Wed Jan 21 22:46:51 2015
New Revision: 1653684

URL: http://svn.apache.org/r1653684
Log:
[UIMA-4198] cas copier cleanup, slight speed improvement, better test

Modified:
    uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/cas/impl/TypeSystemImpl.java
    uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/util/CasCopier.java
    uima/uimaj/trunk/uimaj-core/src/test/java/org/apache/uima/util/CasCopierTest.java

Modified: uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/cas/impl/TypeSystemImpl.java
URL: http://svn.apache.org/viewvc/uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/cas/impl/TypeSystemImpl.java?rev=1653684&r1=1653683&r2=1653684&view=diff
==============================================================================
--- uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/cas/impl/TypeSystemImpl.java (original)
+++ uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/cas/impl/TypeSystemImpl.java Wed Jan 21 22:46:51 2015
@@ -1669,4 +1669,33 @@ public class TypeSystemImpl implements T
     return m;
   }
   
+  /**
+   * @param otherTs type system to compare to this one
+   * @return true if one or more identically named features have differently named ranges
+   */
+  public boolean isRangeCheckNeeded(TypeSystemImpl otherTs) {
+    if (this == otherTs) {
+      return false;
+    }
+    final List<Feature> smallerList;
+    if (features.size() > otherTs.features.size()) {
+      smallerList = otherTs.features;
+      otherTs = this; 
+    } else {
+      smallerList = features;
+    }
+    
+    for (int i = smallerList.size() - 1; i > 0; i--) { // position 0 is null
+      final Feature f1 = smallerList.get(i);
+      final Feature f2 = otherTs.getFeatureByFullName(f1.getName());
+      if (f2 == null) {
+        continue;
+      }
+      if (!f1.getRange().getName().equals(f2.getRange().getName())) {
+        return true;
+      }
+    }
+    return false;
+  }
+  
 }

Modified: uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/util/CasCopier.java
URL: http://svn.apache.org/viewvc/uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/util/CasCopier.java?rev=1653684&r1=1653683&r2=1653684&view=diff
==============================================================================
--- uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/util/CasCopier.java (original)
+++ uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/util/CasCopier.java Wed Jan 21 22:46:51 2015
@@ -78,6 +78,8 @@ public class CasCopier {
   private final TypeSystemImpl srcTsi;
   private final TypeSystemImpl tgtTsi;
   
+  private final boolean needRuntimeRangeCheck;
+  
   private final TypeImpl srcStringType;
   private final int srcStringTypeCode;
   
@@ -163,6 +165,8 @@ public class CasCopier {
     srcTsi = originalSrcCasImpl.getTypeSystemImpl();
     tgtTsi = originalTgtCasImpl.getTypeSystemImpl();
 
+    needRuntimeRangeCheck = srcTsi.isRangeCheckNeeded(tgtTsi);
+    
     srcStringType = srcTsi.stringType;
     srcStringTypeCode = srcStringType.getCode();
     
@@ -172,6 +176,8 @@ public class CasCopier {
     this.lenient = lenient;
   }
   
+
+  
   /**
    * Does a complete deep copy of one CAS into another CAS.  The contents of each view
    * in the source CAS will be copied to the same-named view in the destination CAS.  If
@@ -358,7 +364,7 @@ public class CasCopier {
     
     srcViewName = srcCasViewImpl.getViewName();
     tgtViewName = tgtCasViewImpl.getViewName();
-    isChangeViewName = srcViewName != tgtViewName;
+    isChangeViewName = !srcViewName.equals(tgtViewName);
 
     if ((aSrcCasView == srcCasViewImpl.getBaseCAS()) || (aTgtCasView == tgtCasViewImpl.getBaseCAS())) {
       throw new UIMARuntimeException(UIMARuntimeException.UNSUPPORTED_CAS_COPY_TO_OR_FROM_BASE_CAS, null);
@@ -655,15 +661,15 @@ public class CasCopier {
             throw new UIMARuntimeException(UIMARuntimeException.FEATURE_NOT_FOUND_DURING_CAS_COPY,
                 new Object[] { srcFeat.getName() });
           }
-        }
+        }        
       }
       final int srcFeatCode = ((FeatureImpl)srcFeat).getCode();
       final int tgtFeatCode = ((FeatureImpl)tgtFeat).getCode();
       
-      TypeImpl srcRangeType = (TypeImpl) srcFeat.getRange();      
-      TypeImpl tgtRangeType = (TypeImpl) tgtFeat.getRange(); 
-
-      verifyRangeType(srcFeat, srcRangeType, tgtRangeType);
+      TypeImpl srcRangeType = (TypeImpl) srcFeat.getRange();
+      if (needRuntimeRangeCheck) {
+        verifyRangeType(srcFeat, srcRangeType, (TypeImpl) tgtFeat.getRange());
+      }
 
       // copy primitive values      
       if (srcTsi.ll_subsumes(srcStringTypeCode, srcRangeType.getCode())) {

Modified: uima/uimaj/trunk/uimaj-core/src/test/java/org/apache/uima/util/CasCopierTest.java
URL: http://svn.apache.org/viewvc/uima/uimaj/trunk/uimaj-core/src/test/java/org/apache/uima/util/CasCopierTest.java?rev=1653684&r1=1653683&r2=1653684&view=diff
==============================================================================
--- uima/uimaj/trunk/uimaj-core/src/test/java/org/apache/uima/util/CasCopierTest.java (original)
+++ uima/uimaj/trunk/uimaj-core/src/test/java/org/apache/uima/util/CasCopierTest.java Wed Jan 21 22:46:51 2015
@@ -155,8 +155,30 @@ public class CasCopierTest extends TestC
     destTypeSystems.add(typeSystem);
     CAS destCas2 = CasCreationUtils.createCas(destTypeSystems);
     CasCopier.copyCas(srcCas, destCas2, true);
-    CasComparer.assertEquals(srcCas, destCas);
-
+    CasComparer.assertEquals(srcCas, destCas2);
+    
+    // try with src type system having extra type (no instances) with
+    //   incompatible ranges
+    additionalTypes = new TypeSystemDescription_impl();
+    fooType = additionalTypes.addType("test.Foo", "Test Type",
+            "uima.tcas.Annotation");
+    fooType.addFeature("bar", "Test Feature", "uima.cas.Float");
+    
+    ArrayList<TypeSystemDescription> srcTypeSystems = new ArrayList<TypeSystemDescription>();
+    srcTypeSystems.add(additionalTypes);
+    srcTypeSystems.add(typeSystem);
+    
+    srcCas = CasCreationUtils.createCas(srcTypeSystems);
+    serCasStream = new FileInputStream(JUnitExtension
+            .getFile("ExampleCas/multiSofaCas.xml"));
+    XCASDeserializer.deserialize(serCasStream, srcCas);
+    serCasStream.close();
+    
+    destCas2.reset();
+    
+    CasCopier.copyCas(srcCas, destCas2, true);
+    CasComparer.assertEquals(srcCas, destCas2);
+    
     // try with base CAS rather than initial view
     CAS srcCasBase = ((CASImpl) srcCas).getBaseCAS();
     destCas.reset();
@@ -177,12 +199,18 @@ public class CasCopierTest extends TestC
 
     CasCopier copier;
     // do the copy
-//    for (int i = 0; i < 1200; i++) {  // uncomment for perf test.  was more than 2x faster than version 2.6.0
+    long shortest = Long.MAX_VALUE;
+    int i = 0;
+//    for (; i < 6000; i++) {  // uncomment for perf test.  was more than 2x faster than version 2.6.0
       destCas.reset();
       long startTime = System.nanoTime();
       copier = new CasCopier(srcCas, destCas);
       copier.copyCasView(srcCas, true);
-//      System.out.format("CasCopier speed for 400KB xcas is %,d microseconds%n", (System.nanoTime() - startTime)/ 1000 );
+      long time = (System.nanoTime() - startTime)/ 1000;
+      if (time < shortest) {
+        shortest = time;
+        System.out.format("CasCopier speed for 400KB xcas is %,d microseconds on iteration %,d%n", shortest, i);
+      }
 //    }
     
     // verify copy