Java Code Examples for org.apache.uima.cas.ArrayFS#set()

The following examples show how to use org.apache.uima.cas.ArrayFS#set() . 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: SlotFeatureSupportTest.java    From webanno with Apache License 2.0 6 votes vote down vote up
@Test
public void testWrapUnwrap() throws Exception
{
    CAS cas = jcas.getCas();
    
    List<LinkWithRoleModel> links = new ArrayList<>();
    links.add(new LinkWithRoleModel("role", "label", 3));
            
    AnnotationFS targetFS = cas.createAnnotation(targetType, 0, cas.getDocumentText().length());
    
    ArrayFS array = cas.createArrayFS(1);
    FeatureStructure linkFS = cas.createFS(linkType);
    FSUtil.setFeature(linkFS, slotFeature.getLinkTypeRoleFeatureName(), "role");
    FSUtil.setFeature(linkFS, slotFeature.getLinkTypeTargetFeatureName(), targetFS);
    array.set(0, linkFS);
    
    assertThat(sut.wrapFeatureValue(slotFeature, cas, array)).isEqualTo(links);
    assertThat(sut.wrapFeatureValue(slotFeature, cas, null)).isEmpty();
    assertThatThrownBy(() -> sut.wrapFeatureValue(slotFeature, cas, new Object()))
            .isInstanceOf(IllegalArgumentException.class);
    
    assertThat(sut.unwrapFeatureValue(slotFeature, cas, links)).isSameAs(links);
    assertThat(sut.unwrapFeatureValue(slotFeature, cas, null)).isNull();
    assertThatThrownBy(() -> sut.unwrapFeatureValue(slotFeature, cas, new Object()))
            .isInstanceOf(IllegalArgumentException.class);
}
 
Example 2
Source File: EditViewPage.java    From uima-uimaj with Apache License 2.0 6 votes vote down vote up
@Override
public void run() {
  IStructuredSelection selection = (IStructuredSelection) viewer.getSelection();

  Object element = selection.getFirstElement();

  if (element instanceof FeatureValue) {
    FeatureValue featureValue = (FeatureValue) element;

    if (!featureValue.getFeature().getRange().isPrimitive()) {
      featureValue.getFeatureStructure().setFeatureValue(featureValue.getFeature(), null);

      document.update(featureValue.getFeatureStructure());
    }
  } else if (element instanceof ArrayValue) {
      ArrayValue arrayValue = (ArrayValue) element;

      ArrayFS array = (ArrayFS) arrayValue.getFeatureStructure();

      array.set(arrayValue.slot(), null);

      document.update(array);
  }
}
 
Example 3
Source File: SerDesTest4.java    From uima-uimaj with Apache License 2.0 6 votes vote down vote up
public void testDeltaWithRefsBelow() {
  lfs.clear();
  loadCas(lfs);
  setupCas2ForDeltaSerialization();

  FeatureStructure fs = createFS(cas, akof);
  if (includeUid) fs.setIntValue(akofUid, aint.getAndAdd(1));
  fs.setFeatureValue(akofFs, lfs2.get(0));
  ArrayFS<FeatureStructure> fsafs = createArrayFS(cas, 4);
  fsafs.set(1, lfs2.get(1));
  fsafs.set(2, lfs2.get(2));
  fsafs.set(3, lfs2.get(3));
  fs.setFeatureValue(akofAfs, fsafs);

  cas = cas1;
  verifyDelta(marker, "DeltaWithRefsBelow");
}
 
Example 4
Source File: SerDesTest6.java    From uima-uimaj with Apache License 2.0 6 votes vote down vote up
public void testDeltaWithRefsBelow() {
  lfs.clear();
  TTypeSystem m = getTT(EqTwoTypes);
  remoteCas = setupCas(m);
  loadCas(casSrc, mSrc);
  ReuseInfo ri[] = serializeDeserialize(casSrc, remoteCas, null, null);
  MarkerImpl marker = (MarkerImpl) remoteCas.createMarker();

  lfs = getIndexedFSs(remoteCas, m);
  FeatureStructure fs = remoteCas.createFS(m.getType(Akof1));
  maybeSetFeature(fs, m, lfs.get(0));
  ArrayFS fsafs = remoteCas.createArrayFS(4);
  fsafs.set(1, lfs.get(1));
  fsafs.set(2, lfs.get(2));
  fsafs.set(3, lfs.get(3));
  maybeSetFeatureKind(fs, m, "Afs", fsafs);

  verifyDelta(marker, ri);
}
 
Example 5
Source File: ArrayFSTest.java    From uima-uimaj with Apache License 2.0 6 votes vote down vote up
public void testArraysOfArrays() {
  Type annotationType = this.ts.getType(CAS.TYPE_NAME_ANNOTATION);
  AnnotationFS annot = this.cas.createAnnotation(annotationType, 0, 5);
  IntArrayFS intArray = this.cas.createIntArrayFS(3);
  intArray.set(0, 1);
  intArray.set(1, 2);
  intArray.set(2, -10);
  ArrayFS subArray1 = this.cas.createArrayFS(1);
  ArrayFS subArray2 = this.cas.createArrayFS(2);
  subArray1.set(0, subArray2);
  subArray2.set(1, annot);
  ArrayFS superArray = this.cas.createArrayFS(3);
  superArray.set(0, subArray1);
  superArray.set(1, subArray2);
  superArray.set(2, intArray);
  assertTrue(superArray.get(0).equals(subArray1));
  assertTrue(superArray.get(1).equals(subArray2));
  assertTrue(superArray.get(2).equals(intArray));
  assertTrue(((ArrayFS) superArray.get(0)).get(0).equals(subArray2));
  assertTrue(((ArrayFS) superArray.get(1)).get(0) == null);
  assertTrue(((ArrayFS) superArray.get(1)).get(1).equals(annot));
  assertTrue(((IntArrayFS) superArray.get(2)).get(0) == 1);
  assertTrue(((IntArrayFS) superArray.get(2)).get(1) == 2);
  assertTrue(((IntArrayFS) superArray.get(2)).get(2) == -10);
}
 
Example 6
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 7
Source File: FsCopiers.java    From biomedicus with Apache License 2.0 5 votes vote down vote up
/**
 * Copies one array of fs references to another.
 *
 * @param from the array to copy from
 * @param to the array to copy to
 */
private void copyFsArray(FeatureStructure from, FeatureStructure to) {
  ArrayFS sourceFses = (ArrayFS) from;
  ArrayFS targetFses = (ArrayFS) to;
  for (int index = 0; index < sourceFses.size(); index++) {
    FeatureStructure arrayMember = sourceFses.get(index);
    FeatureStructure toFs = featureStructureEncounteredCallback.apply(arrayMember);
    targetFses.set(index, toFs);
  }
}
 
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> ArrayFS<T> fillArrayFS(ArrayFS<T> aArrayFs,
        Iterable<? extends T> aCollection) {
  int i = 0;
  for (T fs : aCollection) {
    aArrayFs.set(i, fs);
    i++;
  }
  return aArrayFs;
}
 
Example 9
Source File: CasUtilTest.java    From uima-uimafit with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings({ "rawtypes", "unchecked" })
  @Test
  public void testSelectOnArrays() throws Exception {
    String text = "Rot wood cheeses dew?";
    tokenBuilder.buildTokens(jCas, text);

    CAS cas = jCas.getCas();

    Collection<FeatureStructure> allFS = selectFS(cas, getType(cas, TOP.class.getName()));
    ArrayFS allFSArray = cas.createArrayFS(allFS.size());
    int i = 0;
    for (FeatureStructure fs : allFS) {
      allFSArray.set(i, fs);
      i++;
    }

    // Print what is expected
//    for (FeatureStructure fs : allFS) {
//      System.out.println("Type: " + fs.getType().getName() + "]");
//    }
//    System.out
//            .println("Tokens: [" + toText(select(cas, getType(cas, Token.class.getName()))) + "]");

    // Document Annotation, one sentence and 4 tokens.
    assertEquals(6, allFS.size());

    assertEquals(toText(select(cas, getType(cas, Token.class.getName()))),
            toText(select(allFSArray, getType(cas, Token.class.getName()))));

    assertEquals(toText((Iterable) selectFS(cas, getType(cas, Token.class.getName()))),
            toText((Iterable) selectFS(allFSArray, getType(cas, Token.class.getName()))));
  }
 
Example 10
Source File: ArrayFSTest.java    From uima-uimaj with Apache License 2.0 5 votes vote down vote up
public void testToArray() {
  // From CAS array to Java array.
  FeatureStructure fs1 = this.cas.createFS(this.ts.getType(CAS.TYPE_NAME_ANNOTATION));
  FeatureStructure fs2 = this.cas.createFS(this.ts.getType(CAS.TYPE_NAME_TOP));
  FeatureStructure fs3 = this.cas.createFS(this.ts.getType(CASTestSetup.TOKEN_TYPE));
  ArrayFS array = this.cas.createArrayFS(3);
  FeatureStructure[] fsArray = array.toArray();
  for (int i = 0; i < 3; i++) {
    assertTrue(fsArray[i] == null);
  }
  array.set(0, fs1);
  array.set(1, fs2);
  array.set(2, fs3);
  fsArray = array.toArray();
  assertTrue(fsArray.length == 3);
  assertTrue(fsArray[0].equals(fs1));
  assertTrue(fsArray[1].equals(fs2));
  assertTrue(fsArray[2].equals(fs3));

  // From Java array to CAS array.
  array = this.cas.createArrayFS(3);
  assertTrue(array.get(0) == null);
  assertTrue(array.get(1) == null);
  assertTrue(array.get(2) == null);
  for (int i = 0; i < 3; i++) {
    array.set(i, fsArray[i]);
  }
  assertTrue(array.get(0).equals(fs1));
  assertTrue(array.get(1).equals(fs2));
  assertTrue(array.get(2).equals(fs3));
  array.set(0, null);
  assertTrue(array.get(0) == null);
}
 
Example 11
Source File: ArrayFSTest.java    From uima-uimaj with Apache License 2.0 5 votes vote down vote up
public void testCopyToArray() {
  FeatureStructure fs1 = this.cas.createFS(this.ts.getType(CAS.TYPE_NAME_ANNOTATION));
  FeatureStructure fs2 = this.cas.createFS(this.ts.getType(CAS.TYPE_NAME_TOP));
  FeatureStructure fs3 = this.cas.createFS(this.ts.getType(CASTestSetup.TOKEN_TYPE));
  ArrayFS array = this.cas.createArrayFS(4);
  array.set(0, fs1);
  array.set(1, fs2);
  array.set(2, fs3);
  // We now have an FS array with the last element being null
  final int destinationOffset = 2;
  final int destiniationSize = 10;
  FeatureStructure[] fsArray = new FeatureStructure[destiniationSize];
  String[] stringArray = new String[destiniationSize];
  // Copy to array, skipping first element
  // This must not throw an NPE, see UIMA-726
  array.copyToArray(1, fsArray, destinationOffset, array.size() - 1);
  array.copyToArray(1, stringArray, destinationOffset, array.size() - 1);
  assertTrue(fs2.equals(fsArray[destinationOffset]));
  assertTrue(fs3.equals(fsArray[destinationOffset+1]));
  assertNotNull(stringArray[destinationOffset]);
  assertNotNull(stringArray[destinationOffset+1]);
  for (int i = 0; i < destinationOffset; i++) {
    assertNull(fsArray[i]);
    assertNull(stringArray[i]);
  }
  for (int i = (destinationOffset + 2); i < destiniationSize; i++) {
    assertNull(fsArray[i]);
    assertNull(stringArray[i]);
  }
}
 
Example 12
Source File: XmiCasDeserializerTest.java    From uima-uimaj with Apache License 2.0 4 votes vote down vote up
public void testOutOfTypeSystemArrayElement() throws Exception {
  //add to type system an annotation type that has an FSArray feature
  TypeDescription testAnnotTypeDesc = typeSystem.addType("org.apache.uima.testTypeSystem.TestAnnotation", "", "uima.tcas.Annotation");
  testAnnotTypeDesc.addFeature("arrayFeat", "", "uima.cas.FSArray");
  //populate a CAS with such an array
  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);
  ArrayFS arrayFs = cas.createArrayFS(2);
  arrayFs.set(0, orgAnnot1);
  arrayFs.set(1, orgAnnot2);
  Feature arrayFeat = testAnnotType.getFeatureByBaseName("arrayFeat");
  testAnnot.setFeatureValue(arrayFeat, arrayFs);
  
  //serialize to XMI
  String xmiStr = serialize(cas, null);
  
  //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("arrayFeat", "", "uima.cas.FSArray");
  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 arrayFeat2 = testAnnotType2.getFeatureByBaseName("arrayFeat");
  FeatureStructure arrayFs2 = testAnnot2.getFeatureValue(arrayFeat2);
  List ootsElems = sharedData.getOutOfTypeSystemElements();
  assertEquals(2, ootsElems.size());
  List ootsArrayElems = sharedData.getOutOfTypeSystemArrayElements((FSArray) arrayFs2);
  assertEquals(2, ootsArrayElems.size());
  for (int i = 0; i < 2; i++) {
    OotsElementData oed = (OotsElementData)ootsElems.get(i);
    XmiArrayElement arel = (XmiArrayElement)ootsArrayElems.get(i);
    assertEquals(oed.xmiId, arel.xmiId);      
  }
  
  //reserialize along with out of type system data
  String xmiStr2 = serialize(partialTsCas, sharedData);
  
  //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 13
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);

  }