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/01/29 17:07:35 UTC

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

Author: alally
Date: Tue Jan 29 08:07:31 2008
New Revision: 616422

URL: http://svn.apache.org/viewvc?rev=616422&view=rev
Log:
UIMA-729: fixed CasCopier to support annotations with null 
Sofa references (which happens when they are created using the
LowLevelCAS APIs).

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

Modified: incubator/uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/util/CasCopier.java
URL: http://svn.apache.org/viewvc/incubator/uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/util/CasCopier.java?rev=616422&r1=616421&r2=616422&view=diff
==============================================================================
--- incubator/uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/util/CasCopier.java (original)
+++ incubator/uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/util/CasCopier.java Tue Jan 29 08:07:31 2008
@@ -56,6 +56,7 @@
   private CAS mSrcCas;
   private CAS mDestCas;
   private LowLevelCAS mLowLevelDestCas;
+  private Feature mDestSofaFeature;
 
   private Map mFsMap = new HashMap();
 
@@ -73,6 +74,7 @@
     mSrcCas = aSrcCas;
     mDestCas = aDestCas;
     mLowLevelDestCas = aDestCas.getLowLevelCAS();
+    mDestSofaFeature = aDestCas.getTypeSystem().getFeatureByFullName(CAS.FEATURE_FULL_NAME_SOFA);    
   }
   
   /**
@@ -138,6 +140,16 @@
         FeatureStructure fs = (FeatureStructure) iter.next();
         if (!indexedFs.contains(fs)) {
           FeatureStructure copyOfFs = copyFs(fs);
+          //check for annotations with null Sofa reference - this can happen
+          //if the annotations were created with the Low Level CAS API.  If the
+          //Sofa reference isn't set, attempting to add the FS to the indexes
+          //will fail.
+          if (fs instanceof AnnotationFS) {
+              FeatureStructure sofa =((AnnotationFS)copyOfFs).getFeatureValue(mDestSofaFeature);
+              if (sofa == null) {
+                copyOfFs.setFeatureValue(mDestSofaFeature, targetView.getSofa());               
+              }
+          }
           // also don't index the DocumentAnnotation (it's indexed by default)
           if (!isDocumentAnnotation(copyOfFs)) {
             targetView.addFsToIndexes(copyOfFs);

Modified: incubator/uima/uimaj/trunk/uimaj-core/src/test/java/org/apache/uima/util/CasCopierTest.java
URL: http://svn.apache.org/viewvc/incubator/uima/uimaj/trunk/uimaj-core/src/test/java/org/apache/uima/util/CasCopierTest.java?rev=616422&r1=616421&r2=616422&view=diff
==============================================================================
--- incubator/uima/uimaj/trunk/uimaj-core/src/test/java/org/apache/uima/util/CasCopierTest.java (original)
+++ incubator/uima/uimaj/trunk/uimaj-core/src/test/java/org/apache/uima/util/CasCopierTest.java Tue Jan 29 08:07:31 2008
@@ -31,7 +31,9 @@
 import org.apache.uima.cas.CAS;
 import org.apache.uima.cas.FeatureStructure;
 import org.apache.uima.cas.impl.CASImpl;
+import org.apache.uima.cas.impl.LowLevelCAS;
 import org.apache.uima.cas.impl.XCASDeserializer;
+import org.apache.uima.cas.text.AnnotationFS;
 import org.apache.uima.cas_data.impl.CasComparer;
 import org.apache.uima.resource.metadata.FsIndexDescription;
 import org.apache.uima.resource.metadata.TypeDescription;
@@ -61,7 +63,8 @@
   public void testCopyCas() 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/multiSofaCas.xml"));
+    InputStream serCasStream = new FileInputStream(JUnitExtension
+            .getFile("ExampleCas/multiSofaCas.xml"));
     XCASDeserializer.deserialize(serCasStream, srcCas);
     serCasStream.close();
 
@@ -70,30 +73,31 @@
 
     // do the copy
     CasCopier.copyCas(srcCas, destCas, true);
-    //XCASSerializer.serialize(destCas, System.out);
+    // XCASSerializer.serialize(destCas, System.out);
 
     // verify copy
-    CasComparer.assertEquals(srcCas, destCas);    
-    
-    //try with type systems are not identical (dest. a superset of src.)
+    CasComparer.assertEquals(srcCas, destCas);
+
+    // try with type systems are not identical (dest. a superset of src.)
     TypeSystemDescription additionalTypes = new TypeSystemDescription_impl();
-    TypeDescription fooType = additionalTypes.addType("test.Foo", "Test Type", "uima.tcas.Annotation");
-    fooType.addFeature("bar", "Test Feature", "uima.cas.String");    
+    TypeDescription fooType = additionalTypes.addType("test.Foo", "Test Type",
+            "uima.tcas.Annotation");
+    fooType.addFeature("bar", "Test Feature", "uima.cas.String");
     ArrayList destTypeSystems = new ArrayList();
     destTypeSystems.add(additionalTypes);
     destTypeSystems.add(typeSystem);
     CAS destCas2 = CasCreationUtils.createCas(destTypeSystems);
     CasCopier.copyCas(srcCas, destCas2, true);
     CasComparer.assertEquals(srcCas, destCas);
-    
-    //try with base CAS rather than initial view
-    CAS srcCasBase = ((CASImpl)srcCas).getBaseCAS();
+
+    // try with base CAS rather than initial view
+    CAS srcCasBase = ((CASImpl) srcCas).getBaseCAS();
     destCas.reset();
-    CAS destCasBase = ((CASImpl)destCas).getBaseCAS();
+    CAS destCasBase = ((CASImpl) destCas).getBaseCAS();
     CasCopier.copyCas(srcCasBase, destCasBase, true);
-    CasComparer.assertEquals(srcCasBase, destCasBase);        
+    CasComparer.assertEquals(srcCasBase, destCasBase);
   }
-  
+
   public void testCopyCasView() throws Exception {
     // create a source CAS by deserializing from XCAS
     CAS srcCas = CasCreationUtils.createCas(typeSystem, new TypePriorities_impl(), indexes);
@@ -139,13 +143,35 @@
     FeatureStructure relCopy = copier.copyFs(relFS);
     // verify copy
     CasComparer.assertEquals(relFS, relCopy);
-    
-    //test null array element
+
+    // test null array element
     ArrayFS arrFS = srcCas.createArrayFS(3);
     arrFS.set(0, annot);
     arrFS.set(1, null);
     arrFS.set(2, relFS);
     FeatureStructure copyArrFS = copier.copyFs(arrFS);
-    CasComparer.assertEquals(arrFS, copyArrFS);     
+    CasComparer.assertEquals(arrFS, copyArrFS);
+  }
+
+  public void testAnnotationWithNullSofaRef() throws Exception {
+    CAS srcCas = CasCreationUtils.createCas(typeSystem, new TypePriorities_impl(), indexes);
+    CAS srcCasView = srcCas.createView("TestView");
+    srcCasView.setDocumentText("This is a test");
+    CAS destCas = CasCreationUtils.createCas(typeSystem, new TypePriorities_impl(), indexes);
+    LowLevelCAS lowLevelSrcCasView = srcCasView.getLowLevelCAS();
+    int typeCode = lowLevelSrcCasView.ll_getTypeSystem().ll_getCodeForType(
+            srcCas.getAnnotationType());
+    int destFsAddr = lowLevelSrcCasView.ll_createFS(typeCode);
+    AnnotationFS fs = (AnnotationFS) lowLevelSrcCasView.ll_getFSForRef(destFsAddr);
+    fs.setIntValue(srcCas.getBeginFeature(), 0);
+    fs.setIntValue(srcCas.getEndFeature(), 4);
+    assertEquals("This", fs.getCoveredText());
+    srcCasView.addFsToIndexes(fs);
+    CasCopier.copyCas(srcCas, destCas, true);
+    CAS destCasView = destCas.getView("TestView");
+    Iterator annotIter = destCasView.getAnnotationIndex().iterator();
+    annotIter.next(); // document annotation
+    AnnotationFS copiedFs = (AnnotationFS) annotIter.next();
+    assertEquals("This", copiedFs.getCoveredText());
   }
 }