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 2013/12/20 20:20:52 UTC

svn commit: r1552786 - in /uima/uimaj/trunk/uimaj-core/src: main/java/org/apache/uima/util/CasCopier.java test/java/org/apache/uima/util/CasCopierTest.java test/java/org/apache/uima/util/CasWrapperForTstng.java

Author: schor
Date: Fri Dec 20 19:20:52 2013
New Revision: 1552786

URL: http://svn.apache.org/r1552786
Log:
[UIMA-3513] fix logic for seeing if a view belongs to a CAS, to work better with wrapped CASes.  Add test case.  Fix some javadoc comments

Added:
    uima/uimaj/trunk/uimaj-core/src/test/java/org/apache/uima/util/CasWrapperForTstng.java
Modified:
    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/util/CasCopier.java
URL: http://svn.apache.org/viewvc/uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/util/CasCopier.java?rev=1552786&r1=1552785&r2=1552786&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 Fri Dec 20 19:20:52 2013
@@ -159,7 +159,7 @@ public class CasCopier {
    * @param aSrcCas
    *          the CAS to copy from
    * @param aDestCas
-   *          the CAS to copy to
+   *          the CAS to copy to; must be a completely different CAS than the source (that is, not an alternative "view" of the source)
    * @param aCopySofa
    *          if true, the sofa data and mimeType of each view will be copied. If false they will not.
    * @param lenient
@@ -270,7 +270,9 @@ public class CasCopier {
    * of cross-view references will have the same view name as in the source.
    * 
    * @param aSrcCasView
-   *          the CAS to copy from. This must be a view of the srcCas set in the constructor
+   *          the CAS view to copy from. This must be a view of the srcCas set in the constructor
+   * @param aTgtCasView 
+   *          the CAS view to copy to. This must be a view of the tgtCas set in the constructor
    * @param aCopySofa
    *          if true, the sofa data and mimeType will be copied. If false they will not.  
    *          If true and the sofa data is already set in the target, will throw CASRuntimeException        
@@ -387,7 +389,7 @@ public class CasCopier {
    * If the FS has been copied previously (using this CasCopier instance) the 
    * same identical copy will be returned rather than making another copy.
    * 
-   * @param aFS
+   * @param aFS the Feature Structure to copy
    * @return a deep copy of the Feature Structure - any referred to FSs will also be copied.
    */
   
@@ -676,7 +678,7 @@ public class CasCopier {
    */
   private static CAS getOrCreateView(CAS aCas, String aViewName) {
     //TODO: there should be some way to do this without the try...catch
-    try {
+    try { // throws if view doesn't exist
       return aCas.getView(aViewName); 
     }
     catch(CASRuntimeException e) {
@@ -705,6 +707,16 @@ public class CasCopier {
     if (null == c1 || null == c2) {
       return false;
     }
+    
+    // if the cas's are equal, then both views are in the same CAS, of course
+    // This test isn't logically needed, but it allows this case to pass if 
+    // incorrect wrappers are supplied, where the wrapper fails to 
+    // notice that a getView returns the same original CAS, (which the wrapper would
+    // need to replace with the wrapped version)
+    if (c1.equals(c2)) {
+      return true;
+    }
+    
     String v1 = c1.getViewName();
     String v2 = c2.getViewName();
     
@@ -714,11 +726,19 @@ public class CasCopier {
       // -- likely is a base CAS
       if (v2 == null) {
         // two base CASes case
-        return v1 == v2;
+        return c1.equals(c2);  // // defaults to object ==, but wrappers might implement something else
+      }
+      // we don't have a way to get to the base cas directly in the CAS Api
+      try { // throws if view doesn't exist
+        return c1.getView(v2).equals(c2);  // defaults to object ==, but wrappers might implement something else
+      } catch (CASRuntimeException e) {
+        return false;
       }
-      return c1.getView(v2) == c2;
     }
-    
-    return c2.getView(v1) == c1;
+    try { // throws if view doesn't exist
+      return c2.getView(v1).equals(c1); // defaults to object ==, but wrappers might implement something else
+    } catch (CASRuntimeException e) {
+      return false;
+    }
   }
 }

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=1552786&r1=1552785&r2=1552786&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 Fri Dec 20 19:20:52 2013
@@ -45,9 +45,7 @@ import org.apache.uima.resource.metadata
 import org.apache.uima.resource.metadata.impl.TypeSystemDescription_impl;
 import org.apache.uima.test.junit_extension.JUnitExtension;
 
-/**
- * 
- */
+
 public class CasCopierTest extends TestCase {
   private TypeSystemDescription typeSystem;
 
@@ -195,6 +193,41 @@ public class CasCopierTest extends TestC
     (new CasComparerViewChange(srcCas, destCas.getView("aNewView"))).assertEqualViews();
     
   }
+  
+  public void testCopyCasViewsWithWrapper() throws Exception {
+    // create a source CAS by deserializing from XCAS
+    CAS srcCas = CasCreationUtils.createCas(typeSystem, new TypePriorities_impl(), indexes);
+    InputStream serCasStream = new FileInputStream(JUnitExtension.getFile("ExampleCas/cas.xml"));
+    XCASDeserializer.deserialize(serCasStream, srcCas);
+    serCasStream.close();
+
+    // create a destination CAS
+    CAS tgtCas = CasCreationUtils.createCas(typeSystem, new TypePriorities_impl(), indexes);
+
+    // make wrappers
+    CAS wrappedSrcCas = new CasWrapperForTstng(srcCas);
+    CAS wrappedTgtCas = new CasWrapperForTstng(tgtCas);
+    
+    // do the copy
+    CasCopier copier = new CasCopier(wrappedSrcCas, wrappedTgtCas);
+    copier.copyCasView(wrappedSrcCas, true);
+
+    // verify copy
+    CasComparer.assertEquals(wrappedSrcCas, wrappedTgtCas);
+    
+    // do the copy to a different view
+    // create a destination CAS
+    tgtCas = CasCreationUtils.createCas(typeSystem, new TypePriorities_impl(), indexes);
+
+    wrappedTgtCas = new CasWrapperForTstng(tgtCas);
+    // do the copy
+    copier = new CasCopier(wrappedSrcCas, wrappedTgtCas);
+    copier.copyCasView(wrappedSrcCas, "aNewView", true);
+
+    // verify copy
+    (new CasComparerViewChange(wrappedSrcCas, wrappedTgtCas.getView("aNewView"))).assertEqualViews();
+    
+  }
 
   public void testCopyFs() throws Exception {
     // create a source CAS by deserializing from XCAS

Added: uima/uimaj/trunk/uimaj-core/src/test/java/org/apache/uima/util/CasWrapperForTstng.java
URL: http://svn.apache.org/viewvc/uima/uimaj/trunk/uimaj-core/src/test/java/org/apache/uima/util/CasWrapperForTstng.java?rev=1552786&view=auto
==============================================================================
--- uima/uimaj/trunk/uimaj-core/src/test/java/org/apache/uima/util/CasWrapperForTstng.java (added)
+++ uima/uimaj/trunk/uimaj-core/src/test/java/org/apache/uima/util/CasWrapperForTstng.java Fri Dec 20 19:20:52 2013
@@ -0,0 +1,281 @@
+package org.apache.uima.util;
+
+import java.io.InputStream;
+import java.util.Iterator;
+import java.util.ListIterator;
+
+import org.apache.uima.cas.ArrayFS;
+import org.apache.uima.cas.BooleanArrayFS;
+import org.apache.uima.cas.ByteArrayFS;
+import org.apache.uima.cas.CAS;
+import org.apache.uima.cas.CASException;
+import org.apache.uima.cas.CASRuntimeException;
+import org.apache.uima.cas.ComponentInfo;
+import org.apache.uima.cas.ConstraintFactory;
+import org.apache.uima.cas.DoubleArrayFS;
+import org.apache.uima.cas.FSIndexRepository;
+import org.apache.uima.cas.FSIterator;
+import org.apache.uima.cas.FSMatchConstraint;
+import org.apache.uima.cas.Feature;
+import org.apache.uima.cas.FeaturePath;
+import org.apache.uima.cas.FeatureStructure;
+import org.apache.uima.cas.FeatureValuePath;
+import org.apache.uima.cas.FloatArrayFS;
+import org.apache.uima.cas.IntArrayFS;
+import org.apache.uima.cas.LongArrayFS;
+import org.apache.uima.cas.Marker;
+import org.apache.uima.cas.ShortArrayFS;
+import org.apache.uima.cas.SofaFS;
+import org.apache.uima.cas.SofaID;
+import org.apache.uima.cas.StringArrayFS;
+import org.apache.uima.cas.Type;
+import org.apache.uima.cas.TypeSystem;
+import org.apache.uima.cas.admin.CASAdminException;
+import org.apache.uima.cas.impl.LowLevelCAS;
+import org.apache.uima.cas.text.AnnotationFS;
+import org.apache.uima.cas.text.AnnotationIndex;
+import org.apache.uima.jcas.JCas;
+
+public class CasWrapperForTstng implements CAS {
+
+  private CAS originalCAS;
+  
+  public CasWrapperForTstng(CAS original) {
+    originalCAS = original;
+  }
+
+  public void addFsToIndexes(FeatureStructure fs) {
+    originalCAS.addFsToIndexes(fs);
+  }
+
+  public AnnotationFS createAnnotation(Type type, int begin, int end) {
+    return originalCAS.createAnnotation(type, begin, end);
+  }
+
+  public ArrayFS createArrayFS(int length) throws CASRuntimeException {
+    return originalCAS.createArrayFS(length);
+  }
+
+  public BooleanArrayFS createBooleanArrayFS(int length) throws CASRuntimeException {
+    return originalCAS.createBooleanArrayFS(length);
+  }
+
+  public ByteArrayFS createByteArrayFS(int length) throws CASRuntimeException {
+    return originalCAS.createByteArrayFS(length);
+  }
+
+  public DoubleArrayFS createDoubleArrayFS(int length) throws CASRuntimeException {
+    return originalCAS.createDoubleArrayFS(length);
+  }
+
+  public FeaturePath createFeaturePath() {
+    return originalCAS.createFeaturePath();
+  }
+
+  public FeatureValuePath createFeatureValuePath(String featureValuePath) throws CASRuntimeException {
+    return originalCAS.createFeatureValuePath(featureValuePath);
+  }
+
+  public FSIterator createFilteredIterator(FSIterator it, FSMatchConstraint cons) {
+    return originalCAS.createFilteredIterator(it, cons);
+  }
+
+  public FloatArrayFS createFloatArrayFS(int length) throws CASRuntimeException {
+    return originalCAS.createFloatArrayFS(length);
+  }
+
+  public FeatureStructure createFS(Type type) throws CASRuntimeException {
+    return originalCAS.createFS(type);
+  }
+
+  public IntArrayFS createIntArrayFS(int length) throws CASRuntimeException {
+    return originalCAS.createIntArrayFS(length);
+  }
+
+  public LongArrayFS createLongArrayFS(int length) throws CASRuntimeException {
+    return originalCAS.createLongArrayFS(length);
+  }
+
+  public Marker createMarker() {
+    return originalCAS.createMarker();
+  }
+  
+  public ShortArrayFS createShortArrayFS(int length) throws CASRuntimeException {
+    return originalCAS.createShortArrayFS(length);
+  }
+
+  public SofaFS createSofa(SofaID sofaID, String mimeType) {
+    return originalCAS.createSofa(sofaID, mimeType);
+  }
+
+  public StringArrayFS createStringArrayFS(int length) throws CASRuntimeException {
+    return originalCAS.createStringArrayFS(length);
+  }
+
+  public CAS createView(String localViewName) {
+    return originalCAS.createView(localViewName);
+  }
+
+  public ListIterator fs2listIterator(FSIterator it) {
+    return originalCAS.fs2listIterator(it);
+  }
+
+  public AnnotationIndex getAnnotationIndex() {
+    return originalCAS.getAnnotationIndex();
+  }
+
+  public AnnotationIndex getAnnotationIndex(Type type) throws CASRuntimeException {
+    return originalCAS.getAnnotationIndex(type);
+  }
+
+  public Type getAnnotationType() {
+    return originalCAS.getAnnotationType();
+  }
+
+  public Feature getBeginFeature() {
+    return originalCAS.getBeginFeature();
+  }
+
+  public ConstraintFactory getConstraintFactory() {
+    return originalCAS.getConstraintFactory();
+  }
+
+  public CAS getCurrentView() {
+    return originalCAS.getCurrentView();
+  }
+
+  public AnnotationFS getDocumentAnnotation() {
+    return originalCAS.getDocumentAnnotation();
+  }
+
+  public String getDocumentLanguage() {
+    return originalCAS.getDocumentLanguage();
+  }
+
+  public String getDocumentText() {
+    return originalCAS.getDocumentText();
+  }
+
+  public Feature getEndFeature() {
+    return originalCAS.getEndFeature();
+  }
+
+  public FSIndexRepository getIndexRepository() {
+    return originalCAS.getIndexRepository();
+  }
+
+  public JCas getJCas() throws CASException {
+    return originalCAS.getJCas();
+  }
+
+  public JCas getJCas(SofaFS aSofa) throws CASException {
+    return originalCAS.getJCas(aSofa);
+  }
+
+  public JCas getJCas(SofaID aSofaID) throws CASException {
+    return originalCAS.getJCas(aSofaID);
+  }
+
+  public LowLevelCAS getLowLevelCAS() {
+    return originalCAS.getLowLevelCAS();
+  }
+
+  public SofaFS getSofa() {
+    return originalCAS.getSofa();
+  }
+
+  public SofaFS getSofa(SofaID sofaID) {
+    return originalCAS.getSofa(sofaID);
+  }
+
+  public FeatureStructure getSofaDataArray() {
+    return originalCAS.getSofaDataArray();
+  }
+
+  public InputStream getSofaDataStream() {
+    return originalCAS.getSofaDataStream();
+  }
+
+  public String getSofaDataString() {
+    return originalCAS.getSofaDataString();
+  }
+
+  public String getSofaDataURI() {
+    return originalCAS.getSofaDataURI();
+  }
+
+  public FSIterator getSofaIterator() {
+    return originalCAS.getSofaIterator();
+  }
+
+  public String getSofaMimeType() {
+    return originalCAS.getSofaMimeType();
+  }
+
+  public TypeSystem getTypeSystem() throws CASRuntimeException {
+    return originalCAS.getTypeSystem();
+  }
+
+  public CAS getView(SofaFS aSofa) {
+    return originalCAS.getView(aSofa);
+  }
+
+  // without the check if the view is the same as the originalCAS, the getView
+  // would not return the wrapped version.
+  public CAS getView(String localViewName) {
+    CAS view = originalCAS.getView(localViewName);
+    return (view.equals(originalCAS)) ? this : view;
+  }
+
+  public Iterator getViewIterator() {
+    return originalCAS.getViewIterator();
+  }
+
+  public Iterator getViewIterator(String localViewNamePrefix) {
+    return originalCAS.getViewIterator();
+  }
+
+  public String getViewName() {
+    return originalCAS.getViewName();
+  }
+
+  public void release() {
+    originalCAS.release();
+  }
+
+  public void removeFsFromIndexes(FeatureStructure fs) {
+    originalCAS.removeFsFromIndexes(fs);
+  }
+
+  public void reset() throws CASAdminException {
+    originalCAS.reset();
+  }
+
+  public void setCurrentComponentInfo(ComponentInfo info) {
+    originalCAS.setCurrentComponentInfo(info);
+  }
+
+  public void setDocumentLanguage(String languageCode) throws CASRuntimeException {
+    originalCAS.setDocumentLanguage(languageCode);
+  }
+
+  public void setDocumentText(String text) throws CASRuntimeException {
+    originalCAS.setDocumentText(text);
+  }
+
+  public void setSofaDataArray(FeatureStructure array, String mime) throws CASRuntimeException {
+    originalCAS.setSofaDataArray(array, mime);
+  }
+
+  public void setSofaDataString(String text, String mimetype) throws CASRuntimeException {
+    originalCAS.setSofaDataString(text, mimetype);
+  }
+
+  public void setSofaDataURI(String uri, String mime) throws CASRuntimeException {
+    originalCAS.setSofaDataURI(uri, mime);
+  }
+
+  public int size() {
+    return originalCAS.size();
+  }
+}