Java Code Examples for org.apache.uima.cas.CAS#createView()

The following examples show how to use org.apache.uima.cas.CAS#createView() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CASArtifact.java    From biomedicus with Apache License 2.0 6 votes vote down vote up
CASArtifact(
    @Nullable LabelAdapters labelAdapters,
    CAS cas,
    String artifactID
) {
  this.labelAdapters = labelAdapters;
  this.cas = cas;

  TypeSystem typeSystem = cas.getTypeSystem();
  metadataType = typeSystem.getType("ArtifactMetadata");
  keyFeature = metadataType.getFeatureByBaseName("key");
  valueFeature = metadataType.getFeatureByBaseName("value");

  metadataCas = cas.createView("metadata");
  metadataCas.setDocumentText("");

  Type idType = typeSystem.getType("ArtifactID");
  Feature idFeat = idType.getFeatureByBaseName("artifactID");
  this.artifactID = artifactID;
  FeatureStructure documentIdFs = metadataCas.createFS(idType);
  documentIdFs.setStringValue(idFeat, artifactID);
  metadataCas.addFsToIndexes(documentIdFs);
  metadataIndex = metadataCas.getIndexRepository().getIndex("metadata", metadataType);

  casMetadata = new CASMetadata();
}
 
Example 2
Source File: CasUtil.java    From uima-uimafit with Apache License 2.0 6 votes vote down vote up
/**
 * Convenience method to get the specified view or create a new view if the requested view does
 * not exist.
 * 
 * @param cas
 *          a CAS
 * @param viewName
 *          the requested view.
 * @param create
 *          the view is created if it does not exist.
 * @return the requested view
 * @throws IllegalArgumentException
 *           if the view does not exist and is not to be created.
 */
public static CAS getView(CAS cas, String viewName, boolean create) {
  CAS view;
  try {
    view = cas.getView(viewName);
  } catch (CASRuntimeException e) {
    // View does not exist
    if (create) {
      view = cas.createView(viewName);
    } else {
      throw new IllegalArgumentException("No view with name [" + viewName + "]");
    }
  }

  return view;
}
 
Example 3
Source File: CasCopierTest.java    From uima-uimaj with Apache License 2.0 6 votes vote down vote up
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());
    // switch method of creating Annotation to create one with valid sofa ref https://issues.apache.org/jira/browse/UIMA-4099
//    int destFsAddr = lowLevelSrcCasView.ll_createFS(typeCode);
//    Annotation fs = (Annotation) lowLevelSrcCasView.ll_getFSForRef(destFsAddr);
    // the above creates an invalid Annotation, because it doesn't set the sofa ref for the view
    // replace with below that includes the proper sofa ref
    JCas srcJCas = srcCasView.getJCas();
    Annotation fs = new Annotation(srcJCas, 0, 4);
//    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<Annotation> annotIter = destCasView.<Annotation>getAnnotationIndex().iterator();
    annotIter.next(); // document annotation
    Annotation copiedFs = annotIter.next();
    assertEquals("This", copiedFs.getCoveredText());
  }
 
Example 4
Source File: JcasSofaTest.java    From uima-uimaj with Apache License 2.0 6 votes vote down vote up
public void testIndexTwice() throws Exception {
  try {
    CAS newCas = CasCreationUtils.createCas(new TypeSystemDescription_impl(), null, null);
    JCas newJCas = newCas.getJCas();
    CAS view = newCas.createView("DetaggedView");
    view.getJCas();

    Annotation annot = new Annotation(newJCas);
    annot.addToIndexes();
    
    Iterator<Annotation> annotIter = newJCas.getAnnotationIndex(Annotation.type).iterator();
    Annotation annot2 = annotIter.next();
    assertEquals(annot, annot2);
    assertEquals(annot2.getSofa(), annot2.getCASImpl().getSofa());
    
    annot2.addToIndexes();
  }
  catch (Exception e) {
    JUnitExtension.handleException(e);      
  }    
}
 
Example 5
Source File: CASArtifact.java    From biomedicus with Apache License 2.0 5 votes vote down vote up
CASArtifact(
    @Nullable LabelAdapters labelAdapters,
    Artifact artifact,
    CAS cas
) {
  this.labelAdapters = labelAdapters;
  this.cas = cas;

  TypeSystem typeSystem = cas.getTypeSystem();
  metadataType = typeSystem.getType("ArtifactMetadata");
  keyFeature = metadataType.getFeatureByBaseName("key");
  valueFeature = metadataType.getFeatureByBaseName("value");

  metadataCas = cas.createView("metadata");
  metadataCas.setDocumentText("");

  Type idType = typeSystem.getType("ArtifactID");
  Feature idFeat = idType.getFeatureByBaseName("artifactID");
  this.artifactID = artifact.getArtifactID();
  FeatureStructure documentIdFs = metadataCas.createFS(idType);
  documentIdFs.setStringValue(idFeat, artifactID);
  metadataCas.addFsToIndexes(documentIdFs);
  metadataIndex = metadataCas.getIndexRepository().getIndex("metadata", metadataType);

  casMetadata = new CASMetadata();
  casMetadata.putAll(artifact.getMetadata());

  copyDocuments(artifact);
}
 
Example 6
Source File: XmlDetagger.java    From uima-uimaj with Apache License 2.0 5 votes vote down vote up
public void process(CAS aCAS) throws AnalysisEngineProcessException {
    // get handle to CAS view containing XML document
    CAS xmlCas = aCAS.getView("xmlDocument");
    InputStream xmlStream = xmlCas.getSofa().getSofaDataStream();

    // parse with detag handler
    DetagHandler handler = new DetagHandler();
    try {
      SAXParser parser = parserFactory.newSAXParser();
      parser.parse(xmlStream, handler);
    } catch (Exception e) {
      throw new AnalysisEngineProcessException(e);
    }

    // create the plain text view and set its document text
    CAS plainTextView = aCAS.createView("plainTextDocument");
    plainTextView.setDocumentText(handler.getDetaggedText());
    plainTextView.setDocumentLanguage(aCAS.getView("_InitialView").getDocumentLanguage());

    // Index the SourceDocumentInformation object, if there is one, in the new sofa.
    // This is needed by the SemanticSearchCasIndexer
    FeatureStructure sourceDocInfoFs = xmlCas.select(sourceDocInfoType).singleOrNull();
    if (null != sourceDocInfoFs) {
      plainTextView.addFsToIndexes(sourceDocInfoFs);
    }
//    Iterator iter = xmlCas.getAnnotationIndex(sourceDocInfoType).iterator();
//    if (iter.hasNext()) {
//      FeatureStructure sourceDocInfoFs = (FeatureStructure) iter.next();
//      plainTextView.getIndexRepository().addFS(sourceDocInfoFs);
//    }

  }
 
Example 7
Source File: IntVectorTest.java    From uima-uimaj with Apache License 2.0 5 votes vote down vote up
public void testPool() throws Exception {
  try {
    
    AnalysisEngineDescription aed = (AnalysisEngineDescription)UIMAFramework.getXMLParser().parse(
            new XMLInputSource(JUnitExtension
                    .getFile("TextAnalysisEngineImplTest/TestPrimitiveTae1.xml")));
    
    AnalysisEngine ae = UIMAFramework.produceAnalysisEngine(aed);

    // define a caspool of size 2
    CasManager cm = ((UimaContext_ImplBase)ae.getUimaContext()).getResourceManager().getCasManager();
    cm.defineCasPool("uniqueString", 2, null);
    
    CAS c1 = cm.getCas("uniqueString");
    CAS c2 = cm.getCas("uniqueString");
    c1.getJCas();
    
    CAS c1v2 = c1.createView("view2");
    CAS c2v2 = c2.createView("view3");
    c2v2.getJCas();
    
    TypeSystem ts = c1.getTypeSystem();
    
    Assert.assertTrue(ts == c2.getTypeSystem());
    Assert.assertTrue(ts == c1v2.getTypeSystem());
    Assert.assertTrue(ts == c2v2.getTypeSystem());

  } catch (Exception e) {
    JUnitExtension.handleException(e);
  }
}
 
Example 8
Source File: XCasToCasDataSaxHandlerTest.java    From uima-uimaj with Apache License 2.0 5 votes vote down vote up
public void testConversions() throws Exception {
  try {
    // complex CAS obtained by deserialization
    File typeSystemFile = JUnitExtension.getFile("ExampleCas/testTypeSystem.xml");
    TypeSystemDescription typeSystem = UIMAFramework.getXMLParser().parseTypeSystemDescription(
            new XMLInputSource(typeSystemFile));
    CAS cas = CasCreationUtils.createCas(typeSystem, new TypePriorities_impl(),
            new FsIndexDescription[0]);

    InputStream serCasStream = new FileInputStream(JUnitExtension.getFile("ExampleCas/cas.xml"));
    
    XCASDeserializer deser = new XCASDeserializer(cas.getTypeSystem());
    ContentHandler deserHandler = deser.getXCASHandler(cas);
    SAXParserFactory fact = SAXParserFactory.newInstance();
    SAXParser parser = fact.newSAXParser();
    XMLReader xmlReader = parser.getXMLReader();
    xmlReader.setContentHandler(deserHandler);
    xmlReader.parse(new InputSource(serCasStream));
    serCasStream.close();
    _testConversions(cas);

    // a CAS with multiple Sofas
    InputStream translatorAeStream = new FileInputStream(JUnitExtension
            .getFile("CpeSofaTest/TransAnnotator.xml"));
    AnalysisEngineDescription translatorAeDesc = UIMAFramework.getXMLParser()
            .parseAnalysisEngineDescription(new XMLInputSource(translatorAeStream, null));
    AnalysisEngine transAnnotator = UIMAFramework.produceAnalysisEngine(translatorAeDesc);
    CAS cas2 = transAnnotator.newCAS();
    CAS englishView = cas2.createView("EnglishDocument");
    englishView.setSofaDataString("this beer is good", "text/plain");
    transAnnotator.process(cas2);
    _testConversions(cas2);

  } catch (Exception e) {
    JUnitExtension.handleException(e);
  }
}
 
Example 9
Source File: CasPoolTest.java    From uima-uimaj with Apache License 2.0 5 votes vote down vote up
public void testPool() throws Exception {
  try {
         
    casManager.defineCasPool("uniqueString", 2, null);
    
    CAS c1 = casManager.getCas("uniqueString");
    CAS c2 = casManager.getCas("uniqueString");
    c1.getJCas();
    
    CAS c1v2 = c1.createView("view2");
    CAS c2v2 = c2.createView("view3");
    c2v2.getJCas();
    
    TypeSystem ts = c1.getTypeSystem();
    
    Assert.assertTrue(ts == c2.getTypeSystem());
    Assert.assertTrue(ts == c1v2.getTypeSystem());
    Assert.assertTrue(ts == c2v2.getTypeSystem());
    
    casManager.releaseCas(c1v2);
    casManager.releaseCas(c2);
    
    c1 = casManager.getCas("uniqueString");
    c1.createView("mappedName");
    RootUimaContext_impl rootContext = new RootUimaContext_impl();
    ChildUimaContext_impl context = new ChildUimaContext_impl(rootContext, "abc", Collections.singletonMap(CAS.NAME_DEFAULT_SOFA, "mappedName"));
    c1.setCurrentComponentInfo(context.getComponentInfo());
    casManager.releaseCas(c1);

  } catch (Exception e) {
    JUnitExtension.handleException(e);
  }
}
 
Example 10
Source File: XmlDetagger.java    From uima-uimaj with Apache License 2.0 5 votes vote down vote up
public void process(CAS aCAS) throws AnalysisEngineProcessException {
  // get handle to CAS view containing XML document
  CAS xmlCas = aCAS.getView("xmlDocument");
  InputStream xmlStream = xmlCas.getSofa().getSofaDataStream();

  // parse with detag handler
  DetagHandler handler = new DetagHandler();
  try {
    SAXParser parser = parserFactory.newSAXParser();
    parser.parse(xmlStream, handler);
  } catch (Exception e) {
    throw new AnalysisEngineProcessException(e);
  }

  // create the plain text view and set its document text
  CAS plainTextView = aCAS.createView("plainTextDocument");
  plainTextView.setDocumentText(handler.getDetaggedText());
  plainTextView.setDocumentLanguage(aCAS.getView("_InitialView").getDocumentLanguage());

  // Index the SourceDocumentInformation object, if there is one, in the new sofa.
  // This is needed by the SemanticSearchCasIndexer
  Iterator iter = xmlCas.getAnnotationIndex(sourceDocInfoType).iterator();
  if (iter.hasNext()) {
    FeatureStructure sourceDocInfoFs = (FeatureStructure) iter.next();
    plainTextView.getIndexRepository().addFS(sourceDocInfoFs);

  }

}
 
Example 11
Source File: XmiCasDeserializerTest.java    From uima-uimaj with Apache License 2.0 4 votes vote down vote up
public void testMultipleSofas() throws Exception {
    try {
      CAS cas = CasCreationUtils.createCas(typeSystem, new TypePriorities_impl(),
              new FsIndexDescription[0]);
      // set document text for the initial view
      cas.setDocumentText("This is a test");
      // create a new view and set its document text
      CAS cas2 = cas.createView("OtherSofa");
      cas2.setDocumentText("This is only a test");

      // Change this test to create an instance of TOP because you cannot add an annotation to other than 
      //   the view it is created in. https://issues.apache.org/jira/browse/UIMA-4099
      // create a TOP and add to index of both views
      Type topType = cas.getTypeSystem().getTopType();
      FeatureStructure aTOP = cas.createFS(topType);
      cas.getIndexRepository().addFS(aTOP);
      cas2.getIndexRepository().addFS(aTOP); 
      FSIterator<FeatureStructure> it = cas.getIndexRepository().getAllIndexedFS(topType);
      FSIterator<FeatureStructure> it2 = cas2.getIndexRepository().getAllIndexedFS(topType);
      it.next(); it.next();
      it2.next(); it2.next(); 
      assertFalse(it.hasNext());
      assertFalse(it2.hasNext());

      // serialize
      StringWriter sw = new StringWriter();
      XMLSerializer xmlSer = new XMLSerializer(sw, false);
      XmiCasSerializer xmiSer = new XmiCasSerializer(cas.getTypeSystem());
      xmiSer.serialize(cas, xmlSer.getContentHandler());
      String xml = sw.getBuffer().toString();

      // deserialize into another CAS (repeat twice to check it still works after reset)
      CAS newCas = CasCreationUtils.createCas(typeSystem, new TypePriorities_impl(),
              new FsIndexDescription[0]);
      for (int i = 0; i < 2; i++) {
        XmiCasDeserializer newDeser = new XmiCasDeserializer(newCas.getTypeSystem());
        ContentHandler newDeserHandler = newDeser.getXmiCasHandler(newCas);
        SAXParserFactory fact = SAXParserFactory.newInstance();
        SAXParser parser = fact.newSAXParser();
        XMLReader xmlReader = parser.getXMLReader();
        xmlReader.setContentHandler(newDeserHandler);
        xmlReader.parse(new InputSource(new StringReader(xml)));

        // check sofas
        assertEquals("This is a test", newCas.getDocumentText());
        CAS newCas2 = newCas.getView("OtherSofa");
        assertEquals("This is only a test", newCas2.getDocumentText());

        // check that annotation is still indexed in both views
        // check that annotation is still indexed in both views
        it = newCas.getIndexRepository().getAllIndexedFS(topType);
        it2 = newCas2.getIndexRepository().getAllIndexedFS(topType);
        it.next(); it.next();
        it2.next(); it2.next(); 
        assertFalse(it.hasNext());
//        assertFalse(it2.hasNext());        assertTrue(tIndex.size() == 2); // document annot and this one
//        assertTrue(t2Index.size() == 2); // ditto

        newCas.reset();
      }
    } catch (Exception e) {
      JUnitExtension.handleException(e);
    }
  }
 
Example 12
Source File: NewPrimitiveTypesTest.java    From uima-uimaj with Apache License 2.0 4 votes vote down vote up
private FeatureStructure createExampleFS(CAS parmCas) throws Exception {
  // Create a view
  CAS englishView = parmCas.createView("EnglishDocument");
  // Set the document text
  englishView.setDocumentText("this beer is good");

  // create an FS of exampleType and index it
  AnnotationFS fs = englishView.createAnnotation(exampleType, 1, 5);

  // create Array FSs
  StringArrayFS strArrayFS = parmCas.createStringArrayFS(5);
  strArrayFS.set(0, "zzzzzz");
  strArrayFS.set(1, "yyyyyy");
  strArrayFS.set(2, "xxxxxx");
  strArrayFS.set(3, "wwwwww");
  strArrayFS.set(4, "vvvvvv");

  IntArrayFS intArrayFS = parmCas.createIntArrayFS(5);
  intArrayFS.set(0, Integer.MAX_VALUE);
  intArrayFS.set(1, Integer.MAX_VALUE - 1);
  intArrayFS.set(2, 42);
  intArrayFS.set(3, Integer.MIN_VALUE + 1);
  intArrayFS.set(4, Integer.MIN_VALUE);

  FloatArrayFS floatArrayFS = parmCas.createFloatArrayFS(5);
  floatArrayFS.set(0, Float.MAX_VALUE);
  floatArrayFS.set(1, (float) (Float.MAX_VALUE / 1000.0));
  floatArrayFS.set(2, 42);
  floatArrayFS.set(3, (float) (Float.MIN_VALUE * 1000.0));
  floatArrayFS.set(4, Float.MIN_VALUE);

  ByteArrayFS byteArrayFS = parmCas.createByteArrayFS(5);
  byteArrayFS.set(0, (byte) 8);
  byteArrayFS.set(1, (byte) 16);
  byteArrayFS.set(2, (byte) 64);
  byteArrayFS.set(3, (byte) 128);
  byteArrayFS.set(4, (byte) 255);

  BooleanArrayFS boolArrayFS = parmCas.createBooleanArrayFS(20);
  boolean val = false;
  for (int i = 0; i < 20; i++) {
    boolArrayFS.set(i, val = !val);
  }

  ShortArrayFS shortArrayFS = parmCas.createShortArrayFS(5);
  shortArrayFS.set(0, Short.MAX_VALUE);
  shortArrayFS.set(1, (short) (Short.MAX_VALUE - 1));
  shortArrayFS.set(2, (short) (Short.MAX_VALUE - 2));
  shortArrayFS.set(3, (short) (Short.MAX_VALUE - 3));
  shortArrayFS.set(4, (short) (Short.MAX_VALUE - 4));

  LongArrayFS longArrayFS = parmCas.createLongArrayFS(5);
  longArrayFS.set(0, Long.MAX_VALUE);
  longArrayFS.set(1, Long.MAX_VALUE - 1);
  longArrayFS.set(2, Long.MAX_VALUE - 2);
  longArrayFS.set(3, Long.MAX_VALUE - 3);
  longArrayFS.set(4, Long.MAX_VALUE - 4);

  DoubleArrayFS doubleArrayFS = parmCas.createDoubleArrayFS(5);
  doubleArrayFS.set(0, Double.MAX_VALUE);
  doubleArrayFS.set(1, Double.MIN_VALUE);
  doubleArrayFS.set(2, Double.parseDouble("1.5555"));
  doubleArrayFS.set(3, Double.parseDouble("99.000000005"));
  doubleArrayFS.set(4, Double.parseDouble("4.44444444444444444"));

  // set features of fs
  fs.setStringValue(stringFeature, "aaaaaaa");
  fs.setFloatValue(floatFeature, (float) 99.99);

  fs.setFeatureValue(intArrayFeature, intArrayFS);
  fs.setFeatureValue(floatArrayFeature, floatArrayFS);
  fs.setFeatureValue(stringArrayFeature, strArrayFS);

  // fs.setByteValue(byteFeature, Byte.MAX_VALUE);
  fs.setByteValue(byteFeature, (byte) 'z');
  fs.setFeatureValue(byteArrayFeature, byteArrayFS);
  fs.setBooleanValue(booleanFeature, true);
  fs.setFeatureValue(booleanArrayFeature, boolArrayFS);
  fs.setShortValue(shortFeature, Short.MIN_VALUE);
  fs.setFeatureValue(shortArrayFeature, shortArrayFS);
  fs.setLongValue(longFeature, Long.MIN_VALUE);
  fs.setFeatureValue(longArrayFeature, longArrayFS);
  fs.setDoubleValue(doubleFeature, Double.MAX_VALUE);
  fs.setFeatureValue(doubleArrayFeature, doubleArrayFS);
  
  englishView.getIndexRepository().addFS(fs);
  return fs;
}
 
Example 13
Source File: XmiCasDeserializerTest.java    From uima-uimaj with Apache License 2.0 4 votes vote down vote up
public void testDeltaCasIndexExistingFsInNewView() throws Exception {
    CAS cas1 = CasCreationUtils.createCas(typeSystem, new TypePriorities_impl(),
            indexes);
    CAS cas2 = CasCreationUtils.createCas(typeSystem, new TypePriorities_impl(),
            indexes);
    cas1.setDocumentText("This is a test document in the initial view");
    Type referentType = cas1.getTypeSystem().getType("org.apache.uima.testTypeSystem.Referent");
    FeatureStructure fs1 = cas1.createFS(referentType);
    cas1.getIndexRepository().addFS(fs1);

    //serialize complete
    XmiSerializationSharedData sharedData = new XmiSerializationSharedData();
    String xml = serialize(cas1, sharedData);
//    System.out.println(xml);
    int maxOutgoingXmiId = sharedData.getMaxXmiId();

    //deserialize into cas2
    XmiSerializationSharedData sharedData2 = new XmiSerializationSharedData();
    this.deserialize(xml, cas2, sharedData2, true, -1);
    CasComparer.assertEquals(cas1, cas2);

    //create Marker, add/modify fs and serialize in delta xmi format.
    Marker marker = cas2.createMarker();

    //create View
    CAS view = cas2.createView("NewView");
    //add FS to index
    Type referentType2 = cas2.getTypeSystem().getType("org.apache.uima.testTypeSystem.Referent");
    Iterator<FeatureStructure> fsIter = cas2.getIndexRepository().getAllIndexedFS(referentType2);
    while (fsIter.hasNext()) {
      FeatureStructure fs = fsIter.next();
      view.getIndexRepository().addFS(fs);
    }
    AnnotationFS cas2newAnnot = view.createAnnotation(cas2.getAnnotationType(), 6, 8);
    view.getIndexRepository().addFS(cas2newAnnot);

    // serialize cas2 in delta format
    String deltaxml1 = serialize(cas2, sharedData2, marker);
//    System.out.println(deltaxml1);

    //deserialize delta xmi into cas1
    this.deserialize(deltaxml1, cas1, sharedData, true, maxOutgoingXmiId, AllowPreexistingFS.allow);

    //check that new View contains the FS
    CAS deserView = cas1.getView("NewView");
    Iterator<FeatureStructure> deserFsIter = deserView.getIndexRepository().getAllIndexedFS(referentType);
    assertTrue(deserFsIter.hasNext());
  }
 
Example 14
Source File: XmiCasDeserializerTest.java    From uima-uimaj with Apache License 2.0 4 votes vote down vote up
public void testDeltaCasIndexExistingFsInInitialView() throws Exception {
    CAS cas1 = CasCreationUtils.createCas(typeSystem, new TypePriorities_impl(),
            indexes);
    CAS cas2 = CasCreationUtils.createCas(typeSystem, new TypePriorities_impl(),
            indexes);
    // no sofa
//    cas1.setDocumentText("This is a test document in the initial view");
    Type referentType = cas1.getTypeSystem().getType("org.apache.uima.testTypeSystem.Referent");
    FeatureStructure fs1 = cas1.createFS(referentType);
    cas1.getIndexRepository().addFS(fs1);  // index in initial view

    //serialize complete
    XmiSerializationSharedData sharedData = new XmiSerializationSharedData();
    String xml = serialize(cas1, sharedData);
//    System.out.println(xml);
    int maxOutgoingXmiId = sharedData.getMaxXmiId();

    //deserialize into cas2
    XmiSerializationSharedData sharedData2 = new XmiSerializationSharedData();
    this.deserialize(xml, cas2, sharedData2, true, -1);
    CasComparer.assertEquals(cas1, cas2);

    //create Marker, add/modify fs and serialize in delta xmi format.
    Marker marker = cas2.createMarker();

    //create View
    CAS view = cas2.createView("NewView");
    //add FS to index
    Type referentType2 = cas2.getTypeSystem().getType("org.apache.uima.testTypeSystem.Referent");
    Iterator<FeatureStructure> fsIter = cas2.getIndexRepository().getAllIndexedFS(referentType2);
    while (fsIter.hasNext()) {
      FeatureStructure fs = fsIter.next();
      view.getIndexRepository().addFS(fs);
    }
    AnnotationFS cas2newAnnot = view.createAnnotation(cas2.getAnnotationType(), 6, 8);
    view.getIndexRepository().addFS(cas2newAnnot);

    // add fs to initial view index repo.
    
    fs1 = cas2.createFS(referentType);
    cas2.getIndexRepository().addFS(fs1);
    
    // serialize cas2 in delta format
    String deltaxml1 = serialize(cas2, sharedData2, marker);
//    System.out.println(deltaxml1);

    //deserialize delta xmi into cas1
    this.deserialize(deltaxml1, cas1, sharedData, true, maxOutgoingXmiId, AllowPreexistingFS.allow);

    //check that new View contains the FS
    CAS deserView = cas1.getView("NewView");
    Iterator<FeatureStructure> deserFsIter = deserView.getIndexRepository().getAllIndexedFS(referentType);
    assertTrue(deserFsIter.hasNext());
  }
 
Example 15
Source File: XmiCasDeserializerTest.java    From uima-uimaj with Apache License 2.0 4 votes vote down vote up
public void testDeltaCasIndexExistingFsInView() throws Exception {
    CAS cas1 = CasCreationUtils.createCas(typeSystem, new TypePriorities_impl(),
            indexes);
    CAS cas2 = CasCreationUtils.createCas(typeSystem, new TypePriorities_impl(),
            indexes);
    cas1.setDocumentText("This is a test document in the initial view");
    Type referentType = cas1.getTypeSystem().getType("org.apache.uima.testTypeSystem.Referent");
    FeatureStructure fs1 = cas1.createFS(referentType);
    cas1.getIndexRepository().addFS(fs1);

    //serialize complete
    XmiSerializationSharedData sharedData = new XmiSerializationSharedData();
    String xml = serialize(cas1, sharedData);
//    System.out.println(xml);
    int maxOutgoingXmiId = sharedData.getMaxXmiId();

    //deserialize into cas2
    XmiSerializationSharedData sharedData2 = new XmiSerializationSharedData();
    this.deserialize(xml, cas2, sharedData2, true, -1);
    CasComparer.assertEquals(cas1, cas2);

    //create Marker, add/modify fs and serialize in delta xmi format.
    Marker marker = cas2.createMarker();

    //create View
    CAS view = cas2.createView("NewView");
    //add FS to index
    Type referentType2 = cas2.getTypeSystem().getType("org.apache.uima.testTypeSystem.Referent");
    Iterator<FeatureStructure> fsIter = cas2.getIndexRepository().getAllIndexedFS(referentType2);
    while (fsIter.hasNext()) {
      FeatureStructure fs = fsIter.next();
      view.getIndexRepository().addFS(fs);
    }
    AnnotationFS cas2newAnnot = view.createAnnotation(cas2.getAnnotationType(), 6, 8);
    view.getIndexRepository().addFS(cas2newAnnot);

    // serialize cas2 in delta format
    String deltaxml1 = serialize(cas2, sharedData2, marker);
//    System.out.println(deltaxml1);

    //deserialize delta xmi into cas1
    this.deserialize(deltaxml1, cas1, sharedData, true, maxOutgoingXmiId, AllowPreexistingFS.allow);

    //check that new View contains the FS
    CAS deserView = cas1.getView("NewView");
    Iterator<FeatureStructure> deserFsIter = deserView.getIndexRepository().getAllIndexedFS(referentType);
    assertTrue(deserFsIter.hasNext());
  }
 
Example 16
Source File: CasCopierTest.java    From uima-uimaj with Apache License 2.0 4 votes vote down vote up
public void testCopyFs() 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 and the CasCopier instance
    CAS destCas = CasCreationUtils.createCas(typeSystem, new TypePriorities_impl(), indexes);
    CasCopier copier = new CasCopier(srcCas, destCas);

    // set sofa data in destination CAS (this is not copied automatically)
    destCas.setDocumentText(srcCas.getDocumentText());

    CasComparer cci = new CasComparer();
    // copy all entities
    Iterator<TOP> it = srcCas.getIndexRepository().getIndexedFSs(srcCas.getTypeSystem().getType("org.apache.uima.testTypeSystem.Entity")).iterator();
//    Iterator<TOP> it = srcCas.getIndexRepository().getAllIndexedFS(srcCas.getTypeSystem().getType("org.apache.uima.testTypeSystem.Entity"));
//    while(it.hasNext()) {
      TOP fs = it.next();
      TOP fsc = (TOP) copier.copyFs(fs);
//      destCas.addFsToIndexes(fsc);
      CasComparer.assertEquals(fs, fsc);
//    }
    // copy an Annotation
    Iterator<Annotation> annotIter = srcCas.<Annotation>getAnnotationIndex().iterator();
    TOP annot = annotIter.next();
    TOP copy = (TOP) copier.copyFs(annot);
    // verify copy
    CasComparer.assertEquals(annot, copy);

    // copy a Relation (which will have references)
    Iterator<TOP> relationIter = srcCas.getIndexRepository().<TOP>getIndex("testRelationIndex").iterator();
    TOP relFS = relationIter.next();
    TOP relCopy = (TOP) copier.copyFs(relFS);
    // verify copy
    CasComparer.assertEquals(relFS, relCopy);

    // test null array element
    ArrayFS arrFS = srcCas.createArrayFS(3);
    arrFS.set(0, annot);
    arrFS.set(1, null);
    arrFS.set(2, relFS);
    TOP copyArrFS = (TOP) copier.copyFs(arrFS);
    CasComparer.assertEquals((TOP)arrFS, copyArrFS);
    
    // test with using base cas
    
    destCas = CasCreationUtils.createCas(typeSystem, new TypePriorities_impl(), indexes);
    destCas.setDocumentText(srcCas.getDocumentText());
    copier = new CasCopier(((CASImpl)srcCas).getBaseCAS(), ((CASImpl)destCas).getBaseCAS());

    annotIter = srcCas.<Annotation>getAnnotationIndex().iterator();
    annot = annotIter.next();
    boolean wascaught = false;
    try {
      copy = copier.copyFs(annot);
    } catch (CASRuntimeException e) {
      wascaught = true;
    }
    assertFalse(wascaught);
    // verify copy
    CasComparer.assertEquals(annot, copy);
    
    // test copyFS with two CASs, different views, annotations
    // create a destination CAS and the CasCopier instance
    CAS destCas2 = CasCreationUtils.createCas(typeSystem, new TypePriorities_impl(), indexes);
    CAS destCas2v = destCas2.createView("secondView");
    CasCopier copier2 = new CasCopier(srcCas, destCas2v);
    
    // copy an Annotation
    annotIter = srcCas.<Annotation>getAnnotationIndex().iterator();
    annot = annotIter.next();
    copy = copier2.copyFs(annot);
    destCas2v.addFsToIndexes(copy);
    // verify copy
    CasComparer.assertEquals(annot, copy);

  }
 
Example 17
Source File: XCASDeserializerTest.java    From uima-uimaj with Apache License 2.0 4 votes vote down vote up
public void testMultipleSofas() throws Exception {
    /*************************************************
     * Make CAS with 2 sofas, initial and OtherSofa  *
     *                                               *
     * Add instance of TOP and index in both views   *
     *                                               *
     * Serialize to string "xml"                     *
     *                                               *
     * Deserialize from string                       *
     *************************************************/
    CAS cas = CasCreationUtils.createCas(typeSystem, new TypePriorities_impl(), indexes);
    // set document text for the initial view
    cas.setDocumentText("This is a test");
    // create a new view and set its document text
    CAS cas2 = cas.createView("OtherSofa");
    cas2.setDocumentText("This is only a test");

    // Change this test to create an instance of TOP because you cannot add an annotation to other than 
    //   the view it is created in. https://issues.apache.org/jira/browse/UIMA-4099
    // create a TOP and add to index of both views
    Type topType = cas.getTypeSystem().getTopType();
    FeatureStructure aTOP = cas.createFS(topType);
    cas.getIndexRepository().addFS(aTOP);
    cas2.getIndexRepository().addFS(aTOP); 
    FSIterator<FeatureStructure> it = cas.getIndexRepository().getAllIndexedFS(topType);
    FSIterator<FeatureStructure> it2 = cas2.getIndexRepository().getAllIndexedFS(topType);
    it.next(); it.next();
    it2.next(); it2.next(); 
    assertFalse(it.hasNext());
    assertFalse(it2.hasNext());
     
    // serialize
    StringWriter sw = new StringWriter();
    XMLSerializer xmlSer = new XMLSerializer(sw, false);
    XCASSerializer xcasSer = new XCASSerializer(cas.getTypeSystem());
    xcasSer.serialize(cas, xmlSer.getContentHandler(), true);
    String xml = sw.getBuffer().toString();

    // deserialize into another CAS (repeat twice to check it still works after reset)
    CAS newCas = CasCreationUtils.createCas(typeSystem, new TypePriorities_impl(), indexes);
    for (int i = 0; i < 2; i++) {
      XCASDeserializer newDeser = new XCASDeserializer(newCas.getTypeSystem());
      ContentHandler newDeserHandler = newDeser.getXCASHandler(newCas);
      SAXParserFactory fact = SAXParserFactory.newInstance();
      SAXParser parser = fact.newSAXParser();
      XMLReader xmlReader = parser.getXMLReader();
      xmlReader.setContentHandler(newDeserHandler);
      xmlReader.parse(new InputSource(new StringReader(xml)));

      // check sofas
      assertEquals("This is a test", newCas.getDocumentText());
      CAS newCas2 = newCas.getView("OtherSofa");
      assertEquals("This is only a test", newCas2.getDocumentText());

      // check that annotation is still indexed in both views
      it = newCas.getIndexRepository().getAllIndexedFS(topType);
      it2 = newCas2.getIndexRepository().getAllIndexedFS(topType);
      it.next(); it.next();
      it2.next(); it2.next(); 
      assertFalse(it.hasNext());
      assertFalse(it2.hasNext());
//      assertTrue(tIndex.size() == 2); // document annot and this one
//      assertTrue(t2Index.size() == 2); // ditto
      newCas.reset();  // testing if works after cas reset, go around loop 2nd time
    }
  }
 
Example 18
Source File: MultiViewAnnotator.java    From uima-uimaj with Apache License 2.0 4 votes vote down vote up
public void process(CAS aCas, ResultSpecification aResultSpec) throws AnnotatorProcessException {
  CAS engTcas, germTcas;

  engTcas = aCas;

  // Create the output German text Sofa and open CAS view
  germTcas = aCas.createView("GermanDocument");

  // Get some necessary Type System constants
  Type annot = engTcas.getAnnotationType();
  Type cross = engTcas.getTypeSystem().getType("sofa.test.CrossAnnotation");
  Feature other = cross.getFeatureByBaseName("otherAnnotation");

  // Get the English text
  String engText = engTcas.getDocumentText();

  // Setup for translated text
  int engEnd = 0;
  int germBegin = 0;
  int germEnd = 0;
  StringBuffer translation = new StringBuffer();

  // Parse the English text
  StringTokenizer st = new StringTokenizer(engText);
  while (st.hasMoreTokens()) {
    String thisTok = st.nextToken();
    int engBegin = engText.indexOf(thisTok, engEnd);
    engEnd = engBegin + thisTok.length();

    // Create token annotations on English text
    AnnotationFS engAnnot = engTcas.createAnnotation(annot, engBegin, engEnd);
    engTcas.getIndexRepository().addFS(engAnnot);

    // Simple word-by-word translation
    String germWord = Translate(thisTok);

    // Accumulate the translated text
    if (germBegin > 0) {
      translation.append(' ');
      germBegin += 1;
    }
    translation.append(germWord.toCharArray(), 0, germWord.length());

    // Create token annotations on German text
    germEnd = germBegin + germWord.length();
    AnnotationFS germAnnot = germTcas.createAnnotation(cross, germBegin, germEnd);
    germTcas.getIndexRepository().addFS(germAnnot);

    // add link to English text
    germAnnot.setFeatureValue(other, engAnnot);
    germBegin = germEnd;
  }

  // Finally, set the output tranlation Sofa data
  germTcas.setDocumentText(translation.toString());

}
 
Example 19
Source File: SofaExampleAnnotator.java    From uima-uimaj with Apache License 2.0 4 votes vote down vote up
public void process(CAS aCas) throws AnalysisEngineProcessException {
  CAS englishView, germanView;

  // get the CAS view for the English document
  englishView = aCas.getView("EnglishDocument");

  // Create the German text Sofa and open its view
  germanView = aCas.createView("GermanDocument");

  // Get some necessary Type System constants
  Type annot = englishView.getAnnotationType();
  Type cross = englishView.getTypeSystem().getType("sofa.test.CrossAnnotation");
  Feature other = cross.getFeatureByBaseName("otherAnnotation");

  // Get the English text
  String engText = englishView.getDocumentText();

  // Setup for translated text
  int engEnd = 0;
  int germBegin = 0;
  int germEnd = 0;
  StringBuffer translation = new StringBuffer();

  // Parse the English text
  StringTokenizer st = new StringTokenizer(engText);
  while (st.hasMoreTokens()) {
    String thisTok = st.nextToken();
    int engBegin = engText.indexOf(thisTok, engEnd);
    engEnd = engBegin + thisTok.length();

    // Create token annotations on English text
    AnnotationFS engAnnot = englishView.createAnnotation(annot, engBegin, engEnd);
    englishView.addFsToIndexes(engAnnot);

    // Simple word-by-word translation
    String germWord = translate(thisTok);

    // Accumulate the translated text
    if (germBegin > 0) {
      translation.append(' ');
      germBegin += 1;
    }
    translation.append(germWord);

    // Create token annotations on German text
    germEnd = germBegin + germWord.length();
    AnnotationFS germAnnot = germanView.createAnnotation(cross, germBegin, germEnd);
    germanView.addFsToIndexes(germAnnot);

    // add link to English text
    germAnnot.setFeatureValue(other, engAnnot);
    germBegin = germEnd;
  }

  // Finally, set the output tranlation Sofa data
  germanView.setDocumentText(translation.toString());

}