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

The following examples show how to use org.apache.uima.cas.CAS#createFS() . 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: DocumentMetadataLayerAdapter.java    From inception with Apache License 2.0 5 votes vote down vote up
/**
 * Add new document metadata annotation into the CAS and return the the id of the annotation.
 *
 * @param aDocument
 *            the document to which the CAS belongs
 * @param aUsername
 *            the user to which the CAS belongs
 * @param aCas
 *            the CAS.
 * @return the ID.
 * @throws AnnotationException
 *             if the annotation cannot be created/updated.
 */
public AnnotationBaseFS add(SourceDocument aDocument, String aUsername, CAS aCas)
    throws AnnotationException
{
    Type type = CasUtil.getType(aCas, getAnnotationTypeName());
    
    AnnotationBaseFS newAnnotation = aCas.createFS(type);
    aCas.addFsToIndexes(newAnnotation);
    
    publishEvent(new DocumentMetadataCreatedEvent(this, aDocument, aUsername, getLayer(),
            newAnnotation));
    
    return newAnnotation;
}
 
Example 2
Source File: TypeSystemReinitTest.java    From uima-uimaj with Apache License 2.0 5 votes vote down vote up
public void testReinitCASCompleteSerializer() throws Exception {
  try {
    AnalysisEngineDescription aed = UIMAFramework.getXMLParser().parseAnalysisEngineDescription(
            new XMLInputSource(JUnitExtension
                    .getFile("TextAnalysisEngineImplTest/TestPrimitiveTae1.xml")));
    TypeSystemDescription tsd = UIMAFramework.getXMLParser().parseTypeSystemDescription(
            new XMLInputSource(getClass().getResource("/org/apache/uima/examples/SourceDocumentInformation.xml")));

    List<MetaDataObject> l = new ArrayList<>();
    l.add(aed);
    l.add(tsd);
    CAS cas1 = CasCreationUtils.createCas(l);
    cas1.setDocumentText("foo");
    CASCompleteSerializer ser = Serialization.serializeCASComplete((CASMgr) cas1);

    CAS tcas2 = CasCreationUtils.createCas(new TypeSystemDescription_impl(), null, null);
    CASImpl cas2 = ((CASImpl) tcas2).getBaseCAS();
    tcas2.setDocumentText("bar");

    // reinit
    //  This uses cas2 which only has a base type system to start, 
    //    and loads it from a complete serialization which has other new types
    cas2.getBinaryCasSerDes().reinit(ser);
    CAS tcas3 = cas2.getCurrentView();

    assertTrue(tcas2 == tcas3);
    assertNotNull(cas1.getTypeSystem().getType("NamedEntity"));
    assertNotNull(tcas3.getTypeSystem().getType("NamedEntity"));

    FeatureStructure fs = tcas3.createFS(tcas3.getTypeSystem().getType("NamedEntity"));
    tcas3.getIndexRepository().addFS(fs);
  } catch (Exception e) {
    JUnitExtension.handleException(e);
  }
}
 
Example 3
Source File: FeatureStructureTest.java    From uima-uimaj with Apache License 2.0 5 votes vote down vote up
public void testErrorDerefDifferentCAS() {
  CAS cas2 = CASInitializer.initCas(new CASTestSetup(), null);
  Type tokenType1 = this.ts.getType(CASTestSetup.TOKEN_TYPE);
  Feature tokenTypeFeature = this.ts.getFeatureByFullName(CASTestSetup.TOKEN_TYPE + ":" + CASTestSetup.TOKEN_TYPE_FEAT);
  FeatureStructure fs1 = cas2.createFS(tokenType1);
  FeatureStructure fs = cas.createFS(tokenType1);
  boolean caught = false;
  try {
  	  fs.setFeatureValue(tokenTypeFeature, fs1);
  } catch (Exception e) {
    assertTrue( e instanceof CASRuntimeException);
    caught = true;
  }
  assertTrue(caught);
}
 
Example 4
Source File: SerDesTest4.java    From uima-uimaj with Apache License 2.0 5 votes vote down vote up
private FeatureStructure createFS(CAS cas, Type type) {
  if (isKeep) {
    LowLevelCAS llCas = cas.getLowLevelCAS();
    return llCas.ll_getFSForRef(llCas.ll_createFS(((TypeImpl)type).getCode()));
  }
  return cas.createFS(type);
}
 
Example 5
Source File: FSCollectionFactory.java    From uima-uimafit with Apache License 2.0 5 votes vote down vote up
public static <T extends FeatureStructure> T createStringList(CAS aCas, Collection<String> aValues) {
  if (aValues == null) {
    return null;
  }
  
  TypeSystem ts = aCas.getTypeSystem();

  Type emptyType = ts.getType(CAS.TYPE_NAME_EMPTY_STRING_LIST);

  if (aValues.size() == 0) {
    return aCas.createFS(emptyType);
  }
  
  Type nonEmptyType = ts.getType(CAS.TYPE_NAME_NON_EMPTY_STRING_LIST);
  Feature headFeature = nonEmptyType.getFeatureByBaseName(CAS.FEATURE_BASE_NAME_HEAD);
  Feature tailFeature = nonEmptyType.getFeatureByBaseName(CAS.FEATURE_BASE_NAME_TAIL);

  FeatureStructure head = aCas.createFS(nonEmptyType);
  FeatureStructure list = head;
  Iterator<String> i = aValues.iterator();
  while (i.hasNext()) {
    head.setStringValue(headFeature, i.next());
    if (i.hasNext()) {
      FeatureStructure tail = aCas.createFS(nonEmptyType);
      head.setFeatureValue(tailFeature, tail);
      head = tail;
    } else {
      head.setFeatureValue(tailFeature, aCas.createFS(emptyType));
    }
  }

  return (T) list;
}
 
Example 6
Source File: FSCollectionFactory.java    From uima-uimafit with Apache License 2.0 5 votes vote down vote up
public static <T extends FeatureStructure> T createIntegerList(CAS aCas, Collection<Integer> aValues) {
  if (aValues == null) {
    return null;
  }
  
  TypeSystem ts = aCas.getTypeSystem();

  Type emptyType = ts.getType(CAS.TYPE_NAME_EMPTY_INTEGER_LIST);

  if (aValues.size() == 0) {
    return aCas.createFS(emptyType);
  }
  
  Type nonEmptyType = ts.getType(CAS.TYPE_NAME_NON_EMPTY_INTEGER_LIST);
  Feature headFeature = nonEmptyType.getFeatureByBaseName(CAS.FEATURE_BASE_NAME_HEAD);
  Feature tailFeature = nonEmptyType.getFeatureByBaseName(CAS.FEATURE_BASE_NAME_TAIL);

  FeatureStructure head = aCas.createFS(nonEmptyType);
  FeatureStructure list = head;
  Iterator<Integer> i = aValues.iterator();
  while (i.hasNext()) {
    head.setIntValue(headFeature, i.next());
    if (i.hasNext()) {
      FeatureStructure tail = aCas.createFS(nonEmptyType);
      head.setFeatureValue(tailFeature, tail);
      head = tail;
    } else {
      head.setFeatureValue(tailFeature, aCas.createFS(emptyType));
    }
  }

  return (T) list;
}
 
Example 7
Source File: FSCollectionFactory.java    From uima-uimafit with Apache License 2.0 5 votes vote down vote up
public static <T extends FeatureStructure> T createIntegerList(CAS aCas, int... aValues) {
  if (aValues == null) {
    return null;
  }
  
  TypeSystem ts = aCas.getTypeSystem();

  Type emptyType = ts.getType(CAS.TYPE_NAME_EMPTY_INTEGER_LIST);

  if (aValues.length == 0) {
    return aCas.createFS(emptyType);
  }
  
  Type nonEmptyType = ts.getType(CAS.TYPE_NAME_NON_EMPTY_INTEGER_LIST);
  Feature headFeature = nonEmptyType.getFeatureByBaseName(CAS.FEATURE_BASE_NAME_HEAD);
  Feature tailFeature = nonEmptyType.getFeatureByBaseName(CAS.FEATURE_BASE_NAME_TAIL);

  FeatureStructure head = aCas.createFS(nonEmptyType);
  FeatureStructure list = head;
  int i = 0;
  while (i < aValues.length) {
    head.setIntValue(headFeature, aValues[i]);
    i++;
    if (i < aValues.length) {
      FeatureStructure tail = aCas.createFS(nonEmptyType);
      head.setFeatureValue(tailFeature, tail);
      head = tail;
    } else {
      head.setFeatureValue(tailFeature, aCas.createFS(emptyType));
    }
  }

  return (T) list;
}
 
Example 8
Source File: FSCollectionFactory.java    From uima-uimafit with Apache License 2.0 5 votes vote down vote up
public static <T extends FeatureStructure> T createFloatList(CAS aCas, Collection<Float> aValues) {
  if (aValues == null) {
    return null;
  }
  
  TypeSystem ts = aCas.getTypeSystem();

  Type emptyType = ts.getType(CAS.TYPE_NAME_EMPTY_FLOAT_LIST);

  if (aValues.size() == 0) {
    return aCas.createFS(emptyType);
  }
  
  Type nonEmptyType = ts.getType(CAS.TYPE_NAME_NON_EMPTY_FLOAT_LIST);
  Feature headFeature = nonEmptyType.getFeatureByBaseName(CAS.FEATURE_BASE_NAME_HEAD);
  Feature tailFeature = nonEmptyType.getFeatureByBaseName(CAS.FEATURE_BASE_NAME_TAIL);

  FeatureStructure head = aCas.createFS(nonEmptyType);
  FeatureStructure list = head;
  Iterator<Float> i = aValues.iterator();
  while (i.hasNext()) {
    head.setFloatValue(headFeature, i.next());
    if (i.hasNext()) {
      FeatureStructure tail = aCas.createFS(nonEmptyType);
      head.setFeatureValue(tailFeature, tail);
      head = tail;
    } else {
      head.setFeatureValue(tailFeature, aCas.createFS(emptyType));
    }
  }

  return (T) list;
}
 
Example 9
Source File: FSCollectionFactory.java    From uima-uimafit with Apache License 2.0 5 votes vote down vote up
public static <T extends FeatureStructure> T createFloatList(CAS aCas, float... aValues) {
  if (aValues == null) {
    return null;
  }
  
  TypeSystem ts = aCas.getTypeSystem();

  Type emptyType = ts.getType(CAS.TYPE_NAME_EMPTY_FLOAT_LIST);

  if (aValues.length == 0) {
    return aCas.createFS(emptyType);
  }
  
  Type nonEmptyType = ts.getType(CAS.TYPE_NAME_NON_EMPTY_FLOAT_LIST);
  Feature headFeature = nonEmptyType.getFeatureByBaseName(CAS.FEATURE_BASE_NAME_HEAD);
  Feature tailFeature = nonEmptyType.getFeatureByBaseName(CAS.FEATURE_BASE_NAME_TAIL);

  FeatureStructure head = aCas.createFS(nonEmptyType);
  FeatureStructure list = head;
  int i = 0;
  while (i < aValues.length) {
    head.setFloatValue(headFeature, aValues[i]);
    i++;
    if (i < aValues.length) {
      FeatureStructure tail = aCas.createFS(nonEmptyType);
      head.setFeatureValue(tailFeature, tail);
      head = tail;
    } else {
      head.setFeatureValue(tailFeature, aCas.createFS(emptyType));
    }
  }

  return (T) list;
}
 
Example 10
Source File: FSCollectionFactory.java    From uima-uimafit with Apache License 2.0 5 votes vote down vote up
public static <T extends TOP> FSList<T> createFSList(CAS aCas, Collection<T> aValues) {
  if (aValues == null) {
    return null;
  }
  
  TypeSystem ts = aCas.getTypeSystem();

  if (aValues.size() == 0) {
    return aCas.emptyFSList();
  }
  
  Type nonEmptyType = ts.getType(CAS.TYPE_NAME_NON_EMPTY_FS_LIST);
  Feature headFeature = nonEmptyType.getFeatureByBaseName(CAS.FEATURE_BASE_NAME_HEAD);
  Feature tailFeature = nonEmptyType.getFeatureByBaseName(CAS.FEATURE_BASE_NAME_TAIL);

  FeatureStructure head = aCas.createFS(nonEmptyType);
  FeatureStructure list = head;
  Iterator<? extends FeatureStructure> i = aValues.iterator();
  while (i.hasNext()) {
    head.setFeatureValue(headFeature, i.next());
    if (i.hasNext()) {
      FeatureStructure tail = aCas.createFS(nonEmptyType);
      head.setFeatureValue(tailFeature, tail);
      head = tail;
    } else {
      head.setFeatureValue(tailFeature, aCas.emptyFSList());
    }
  }

  return (FSList<T>) list;
}
 
Example 11
Source File: JCasTest.java    From uima-uimaj with Apache License 2.0 5 votes vote down vote up
public void testFSListNotPromoted() throws Exception {
	try {
		CAS localCas = jcas.getCas();
		TypeSystem ts = localCas.getTypeSystem();
		Type fsl = ts.getType("uima.cas.NonEmptyFSList");
		FeatureStructure fs = localCas.createFS(fsl);
		assertTrue(fs instanceof NonEmptyFSList);
	} catch (Exception e) {
		JUnitExtension.handleException(e);
	}
}
 
Example 12
Source File: WebAnnoTsv3WriterTestBase.java    From webanno with Apache License 2.0 5 votes vote down vote up
private static void makeChainHead(Type aType, AnnotationFS first)
{
    CAS cas = first.getCAS();
    FeatureStructure h = cas.createFS(aType);
    FSUtil.setFeature(h, "first", first);
    cas.addFsToIndexes(h);
}
 
Example 13
Source File: SubjectObjectFeatureSupportTest.java    From inception with Apache License 2.0 5 votes vote down vote up
@Test
public void testWrapUnwrap() throws Exception
{
    CAS cas = JCasFactory.createJCasFromPath("src/test/resources/desc/type/webannoTestTypes.xml")
            .getCas();
    
    SubjectObjectFeatureSupport sut = new SubjectObjectFeatureSupport();
    
    AnnotationFeature feat1 = new AnnotationFeature("slot", "webanno.custom.SimpleSpan");
    feat1.setLinkTypeName("webanno.custom.LinkType");
    feat1.setLinkMode(LinkMode.WITH_ROLE);
    feat1.setLinkTypeRoleFeatureName("role");
    feat1.setLinkTypeTargetFeatureName("target");
    feat1.setMode(MultiValueMode.ARRAY);
    
    List<LinkWithRoleModel> links = new ArrayList<>();
    links.add(new LinkWithRoleModel("role", "label", 3));
    
    cas.setDocumentText("label");
    Type targetType = cas.getTypeSystem().getType(feat1.getType());
    Type linkType = cas.getTypeSystem().getType(feat1.getLinkTypeName());
    
    AnnotationFS targetFS = cas.createAnnotation(targetType, 0, cas.getDocumentText().length());
    
    ArrayFS array = cas.createArrayFS(1);
    FeatureStructure linkFS = cas.createFS(linkType);
    FSUtil.setFeature(linkFS, feat1.getLinkTypeRoleFeatureName(), "role");
    FSUtil.setFeature(linkFS, feat1.getLinkTypeTargetFeatureName(), targetFS);
    array.set(0, linkFS);
    
    assertThat(sut.wrapFeatureValue(feat1, cas, array)).isEqualTo(links);
    assertThat(sut.wrapFeatureValue(feat1, cas, null)).isEmpty();
    assertThatThrownBy(() -> sut.wrapFeatureValue(feat1, cas, new Object()))
            .isInstanceOf(IllegalArgumentException.class);
    
    assertThat(sut.unwrapFeatureValue(feat1, cas, links)).isSameAs(links);
    assertThat(sut.unwrapFeatureValue(feat1, cas, null)).isNull();
    assertThatThrownBy(() -> sut.unwrapFeatureValue(feat1, cas, new Object()))
            .isInstanceOf(IllegalArgumentException.class);
}
 
Example 14
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 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: 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 17
Source File: CasIOUtilsAlwaysHoldOnTest.java    From uima-uimaj with Apache License 2.0 4 votes vote down vote up
@Test
public void thatDocumentAnnotationIsNotResurrected() throws Exception {
  // Must set this to true, otherwise the test will not fail. Setting it to true will cause 
  // FSes which are not in any index to still be serialized out. When reading this data back,
  // UIMA will find the non-indexed DocumentAnnotation and add it back without checking whether
  // is was actually indexed or not.
  System.setProperty(CASImpl.ALWAYS_HOLD_ONTO_FSS, "true");
    
  String customDocAnnoTypeName = "org.apache.uima.testing.CustomDocumentAnnotation";
    
  TypeSystemDescription tsd = UIMAFramework.getResourceSpecifierFactory().createTypeSystemDescription();
  tsd.addType(customDocAnnoTypeName, "", CAS.TYPE_NAME_DOCUMENT_ANNOTATION);
  
  CAS cas = CasCreationUtils.createCas(tsd, null, null);
  
  // Initialize the default document annotation
  // ... then immediately remove it from the indexes.
  FeatureStructure da = cas.getDocumentAnnotation();

  assertThat(cas.select(cas.getTypeSystem().getType(CAS.TYPE_NAME_DOCUMENT_ANNOTATION)).asList())
      .extracting(fs -> fs.getType().getName())
      .containsExactly(CAS.TYPE_NAME_DOCUMENT_ANNOTATION);
  
  cas.removeFsFromIndexes(da);
  
  // Now add a new document annotation of our custom type
  FeatureStructure cda = cas.createFS(cas.getTypeSystem().getType(customDocAnnoTypeName));
  cas.addFsToIndexes(cda);

  assertThat(cas.select(cas.getTypeSystem().getType(CAS.TYPE_NAME_DOCUMENT_ANNOTATION)).asList())
      .extracting(fs -> fs.getType().getName())
      .containsExactly(customDocAnnoTypeName);
  
  // Serialize to a buffer
  ByteArrayOutputStream bos = new ByteArrayOutputStream();
  CasIOUtils.save(cas, bos, SerialFormat.SERIALIZED_TSI);
  
  // Deserialize from the buffer
  ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
  CasIOUtils.load(bis, cas);
  
  assertThat(cas.select(cas.getTypeSystem().getType(CAS.TYPE_NAME_DOCUMENT_ANNOTATION)).asList())
      .extracting(fs -> fs.getType().getName())
      .containsExactly(customDocAnnoTypeName);
}
 
Example 18
Source File: XmiCasDeserializerTest.java    From uima-uimaj with Apache License 2.0 4 votes vote down vote up
public void testOutOfTypeSystemListElement() throws Exception {
    //add to type system an annotation type that has an FSList feature
    TypeDescription testAnnotTypeDesc = typeSystem.addType("org.apache.uima.testTypeSystem.TestAnnotation", "", "uima.tcas.Annotation");
    testAnnotTypeDesc.addFeature("listFeat", "", "uima.cas.FSList");
    
    //populate a CAS with such an list
    CAS cas = CasCreationUtils.createCas(typeSystem, null, null);
    Type testAnnotType = cas.getTypeSystem().getType("org.apache.uima.testTypeSystem.TestAnnotation");
    Type orgType       = cas.getTypeSystem().getType("org.apache.uima.testTypeSystem.Organization");
    
    AnnotationFS orgAnnot1 = cas.createAnnotation(orgType, 0, 10);
                             cas.addFsToIndexes(orgAnnot1);
    AnnotationFS orgAnnot2 = cas.createAnnotation(orgType, 10, 20);
                             cas.addFsToIndexes(orgAnnot2);
    AnnotationFS testAnnot = cas.createAnnotation(testAnnotType, 0, 20);
                             cas.addFsToIndexes(testAnnot);
                             
    Type nonEmptyFsListType = cas.getTypeSystem().getType(CAS.TYPE_NAME_NON_EMPTY_FS_LIST);
    Type emptyFsListType    = cas.getTypeSystem().getType(CAS.TYPE_NAME_EMPTY_FS_LIST);
    Feature headFeat = nonEmptyFsListType.getFeatureByBaseName("head");
    Feature tailFeat = nonEmptyFsListType.getFeatureByBaseName("tail");
    
    FeatureStructure emptyNode  = cas.createFS(emptyFsListType);
    
    FeatureStructure secondNode = cas.createFS(nonEmptyFsListType);
    secondNode.setFeatureValue(headFeat, orgAnnot2);
    secondNode.setFeatureValue(tailFeat, emptyNode);
    
    FeatureStructure firstNode = cas.createFS(nonEmptyFsListType);
    firstNode.setFeatureValue(headFeat, orgAnnot1);
    firstNode.setFeatureValue(tailFeat, secondNode);
    
    Feature listFeat = testAnnotType.getFeatureByBaseName("listFeat");
    testAnnot.setFeatureValue(listFeat, firstNode);
    
    //serialize to XMI
    String xmiStr = serialize(cas, null);
//    System.out.println(xmiStr);
    
    //deserialize into a CAS that's missing the Organization type
    File partialTypeSystemFile = JUnitExtension.getFile("ExampleCas/partialTestTypeSystem.xml");
    TypeSystemDescription partialTypeSystem = UIMAFramework.getXMLParser().parseTypeSystemDescription(
            new XMLInputSource(partialTypeSystemFile));
    testAnnotTypeDesc = partialTypeSystem.addType("org.apache.uima.testTypeSystem.TestAnnotation", "", "uima.tcas.Annotation");
    testAnnotTypeDesc.addFeature("listFeat", "", "uima.cas.FSList");
    CAS partialTsCas = CasCreationUtils.createCas(partialTypeSystem, null, null);
    XmiSerializationSharedData sharedData = new XmiSerializationSharedData();
    deserialize(xmiStr, partialTsCas, sharedData, true, -1);
    
    //check out of type system data
    Type testAnnotType2 = partialTsCas.getTypeSystem().getType("org.apache.uima.testTypeSystem.TestAnnotation");
    FeatureStructure testAnnot2 = partialTsCas.getAnnotationIndex(testAnnotType2).iterator().get(); 
    Feature listFeat2 = testAnnotType2.getFeatureByBaseName("listFeat");
    FeatureStructure listFs = testAnnot2.getFeatureValue(listFeat2);
    List ootsElems = sharedData.getOutOfTypeSystemElements();
    assertEquals(2, ootsElems.size());
    
    OotsElementData oed = sharedData.getOutOfTypeSystemFeatures((TOP) listFs);
    XmlAttribute attr = oed.attributes.get(0);
    assertNotNull(attr);
    assertEquals(CAS.FEATURE_BASE_NAME_HEAD, attr.name);
    assertEquals(attr.value, ((OotsElementData)ootsElems.get(0)).xmiId);
    
    //reserialize along with out of type system data
    String xmiStr2 = serialize(partialTsCas, sharedData);
//    System.out.println(xmiStr2);
    
    //deserialize into a new CAS and compare
    CAS cas2 = CasCreationUtils.createCas(typeSystem, null, null);
    deserialize(xmiStr2, cas2, null, false, -1);
    
    CasComparer.assertEquals(cas, cas2);    
  }
 
Example 19
Source File: CasIOUtilsTest.java    From uima-uimaj with Apache License 2.0 4 votes vote down vote up
public void testDocumentAnnotationIsNotResurrected() throws Exception {
  String refererAnnoTypeName = "org.apache.uima.testing.Referer";
  String customDocAnnoTypeName = "org.apache.uima.testing.CustomDocumentAnnotation";
    
  TypeSystemDescription tsd = UIMAFramework.getResourceSpecifierFactory().createTypeSystemDescription();
  tsd.addType(customDocAnnoTypeName, "", CAS.TYPE_NAME_DOCUMENT_ANNOTATION);
  TypeDescription refererType = tsd.addType(refererAnnoTypeName, "", CAS.TYPE_NAME_TOP);
  refererType.addFeature("ref", "", CAS.TYPE_NAME_DOCUMENT_ANNOTATION);
  
  CAS cas = CasCreationUtils.createCas(tsd, null, null);
  
  // Initialize the default document annotation
  // ... then immediately remove it from the indexes.
  FeatureStructure da = cas.getDocumentAnnotation();

  assertThat(cas.select(cas.getTypeSystem().getType(CAS.TYPE_NAME_DOCUMENT_ANNOTATION)).asList())
      .extracting(fs -> fs.getType().getName())
      .containsExactly(CAS.TYPE_NAME_DOCUMENT_ANNOTATION);
  
  // Add a feature structure that references the original document annotation before we remove
  // it from the indexes
  FeatureStructure referer = cas.createFS(cas.getTypeSystem().getType(refererAnnoTypeName));
  referer.setFeatureValue(referer.getType().getFeatureByBaseName("ref"), da);
  cas.addFsToIndexes(referer);
  
  cas.removeFsFromIndexes(da);
  
  // Now add a new document annotation of our custom type
  FeatureStructure cda = cas.createFS(cas.getTypeSystem().getType(customDocAnnoTypeName));
  cas.addFsToIndexes(cda);

  assertThat(cas.select(cas.getTypeSystem().getType(CAS.TYPE_NAME_DOCUMENT_ANNOTATION)).asList())
      .extracting(fs -> fs.getType().getName())
      .containsExactly(customDocAnnoTypeName);
  
  // Serialize to a buffer
  ByteArrayOutputStream bos = new ByteArrayOutputStream();
  CasIOUtils.save(cas, bos, SerialFormat.SERIALIZED_TSI);
  
  // Deserialize from the buffer
  ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
  CasIOUtils.load(bis, cas);
  
  assertThat(cas.select(cas.getTypeSystem().getType(CAS.TYPE_NAME_DOCUMENT_ANNOTATION)).asList())
      .extracting(fs -> fs.getType().getName())
      .containsExactly(customDocAnnoTypeName);
}
 
Example 20
Source File: TestAnnotator.java    From uima-uimaj with Apache License 2.0 4 votes vote down vote up
public void process(CAS cas) {
  FeatureStructure fs = cas.createFS(cas.getTypeSystem().getType(CAS.TYPE_NAME_ANNOTATION_BASE));
  cas.addFsToIndexes(fs);
  fs = cas.createFS(cas.getTypeSystem().getType("OtherAnnotation"));
  cas.addFsToIndexes(fs);
}