org.apache.uima.cas.ArrayFS Java Examples

The following examples show how to use org.apache.uima.cas.ArrayFS. 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: 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 #2
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 #3
Source File: DiffAdapter_ImplBase.java    From webanno with Apache License 2.0 6 votes vote down vote up
@Override
public List<? extends Position> generateSubPositions(int aCasId, AnnotationFS aFs,
        LinkCompareBehavior aLinkCompareBehavior)
{
    List<Position> subPositions = new ArrayList<>();
    
    for (LinkFeatureDecl decl : linkFeatures) {
        Feature linkFeature = aFs.getType().getFeatureByBaseName(decl.getName());
        ArrayFS array = (ArrayFS) aFs.getFeatureValue(linkFeature);
        if (array == null) {
            continue;
        }
        for (FeatureStructure linkFS : array.toArray()) {
            String role = linkFS.getStringValue(linkFS.getType().getFeatureByBaseName(
                    decl.getRoleFeature()));
            AnnotationFS target = (AnnotationFS) linkFS.getFeatureValue(linkFS.getType()
                    .getFeatureByBaseName(decl.getTargetFeature()));
            Position pos = getPosition(aCasId, aFs, decl.getName(), role, target.getBegin(),
                    target.getEnd(), aLinkCompareBehavior);
            subPositions.add(pos);
        }
    }
    
    return subPositions;
}
 
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: 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 #6
Source File: EditViewPage.java    From uima-uimaj with Apache License 2.0 6 votes vote down vote up
@Override
protected boolean updateSelection(IStructuredSelection selection) {

  boolean result = false;

  if (selection.size() == 1) {
    if (selection.getFirstElement() instanceof FeatureValue) {
      FeatureValue featureValue = (FeatureValue) selection.getFirstElement();

      result = !featureValue.getFeature().getRange().isPrimitive() &&
          featureValue.getFeatureStructure().getFeatureValue(featureValue.getFeature()) != null;
    }
    else if (selection.getFirstElement() instanceof ArrayValue) {
      ArrayValue arrayValue = (ArrayValue) selection.getFirstElement();

        if (arrayValue.getFeatureStructure() instanceof ArrayFS) {
          ArrayFS array = (ArrayFS) arrayValue.getFeatureStructure();

          result = array.get(arrayValue.slot()) != null;
        }
    }
  }

  return result;

}
 
Example #7
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 #8
Source File: ArrayValue.java    From uima-uimaj with Apache License 2.0 6 votes vote down vote up
@Override
public Object getAdapter(@SuppressWarnings("rawtypes") Class adapter) {

  if (FeatureStructure.class.equals(adapter)) {
    if (arrayFS instanceof ArrayFS) {
      ArrayFS array = (ArrayFS) arrayFS;

      return array.get(slot());
    }
  }

  if (AnnotationFS.class.equals(adapter)) {
    FeatureStructure fs = (FeatureStructure) getAdapter(FeatureStructure.class);

    if (fs instanceof AnnotationFS) {
      return fs;
    }
  }

  return null;
}
 
Example #9
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 #10
Source File: CasComparer.java    From uima-uimaj with Apache License 2.0 5 votes vote down vote up
private ARRAY_TYPE getArrayType(CommonArrayFS c) {
  if (c instanceof ArrayFS) return ARRAY_TYPE.FS; 
  if (c instanceof StringArrayFS) return ARRAY_TYPE.STRING; 
  if (c instanceof BooleanArrayFS) return ARRAY_TYPE.BOOLEAN; 
  if (c instanceof ByteArrayFS) return ARRAY_TYPE.BYTE; 
  if (c instanceof ShortArrayFS) return ARRAY_TYPE.SHORT; 
  if (c instanceof IntArrayFS) return ARRAY_TYPE.INT; 
  if (c instanceof LongArrayFS) return ARRAY_TYPE.LONG; 
  if (c instanceof FloatArrayFS) return ARRAY_TYPE.FLOAT; 
  if (c instanceof DoubleArrayFS) return ARRAY_TYPE.DOUBLE;
  return 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: 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 #13
Source File: FeatureStructureTest.java    From uima-uimaj with Apache License 2.0 5 votes vote down vote up
public void testSetArrayValuedFeature() {
	FeatureStructure testFS = this.cas.createFS(this.arrayFsWithSubtypeType);
	assertTrue(testFS.getFeatureValue(this.arrayFsWithSubtypeTypeFeat) == null);
	ArrayFS arrayFS = this.cas.createArrayFS(1);
	testFS.setFeatureValue(this.arrayFsWithSubtypeTypeFeat, arrayFS);
	assertTrue(true);
	boolean caughtExc = false;
	try {
		testFS.setFeatureValue(this.arrayFsWithSubtypeTypeFeat, testFS);
	} catch (CASRuntimeException e) {
		caughtExc = true;
		assertTrue(e.getMessageKey().equals(CASRuntimeException.INAPPROP_RANGE));
	}
	assertTrue(caughtExc);
}
 
Example #14
Source File: DebugNameValuePair.java    From uima-uimaj with Apache License 2.0 5 votes vote down vote up
public String toString() {
  Object v = getValue();
  String className = v.getClass().getSimpleName();
  if (v instanceof StringArrayFS)
    v = className + "[" + ((StringArrayFS) v).size() + "]";
  else if (v instanceof FloatArrayFS)
    v = className + "[" + ((FloatArrayFS) v).size() + "]";
  else if (v instanceof IntArrayFS)
    v = className + "[" + ((IntArrayFS) v).size() + "]";
  else if (v instanceof ArrayFS)
    v = className + "[" + ((ArrayFS) v).size() + "]";
  return getName() + ": " + v;
}
 
Example #15
Source File: EditViewPage.java    From uima-uimaj with Apache License 2.0 5 votes vote down vote up
@Override
protected boolean updateSelection(IStructuredSelection selection) {

  boolean result = false;

  if (selection.size() == 1) {
    if (selection.getFirstElement() instanceof FeatureValue) {
      FeatureValue featureValue = (FeatureValue) selection.getFirstElement();

      result = !featureValue.getFeature().getRange().isPrimitive() &&
          featureValue.getFeatureStructure().getFeatureValue(featureValue.getFeature()) == null;
    }
    else if (selection.getFirstElement() instanceof ArrayValue) {
      ArrayValue value = (ArrayValue) selection.getFirstElement();

      if (value.getFeatureStructure() instanceof ArrayFS) {
        ArrayFS array = (ArrayFS) value.getFeatureStructure();

        if (array.get(value.slot()) == null) {
          result = true;
        }
      }
    }
  }

  return result;
}
 
Example #16
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 #17
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 #18
Source File: WebannoTsv3Reader.java    From webanno with Apache License 2.0 5 votes vote down vote up
/**
 * update a base annotation with slot annotations
 * 
 * @param linkFSesPerAnno
 *            contains list of slot annotations per a base annotation
 * @param aLinkeF
 *            The link slot annotation feature
 */
private void addSlotAnnotations(Map<AnnotationFS, List<FeatureStructure>> linkFSesPerAnno,
        Feature aLinkeF)
{
    for (AnnotationFS anno : linkFSesPerAnno.keySet()) {
        ArrayFS array = anno.getCAS().createArrayFS(linkFSesPerAnno.get(anno).size());
        array.copyFromArray(
                linkFSesPerAnno.get(anno)
                        .toArray(new FeatureStructure[linkFSesPerAnno.get(anno).size()]),
                0, 0, linkFSesPerAnno.get(anno).size());
        anno.setFeatureValue(aLinkeF, array);
        anno.getCAS().addFsToIndexes(anno);
    }
}
 
Example #19
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 #20
Source File: SlotFeatureSupport.java    From webanno with Apache License 2.0 5 votes vote down vote up
@Override
public ArrayList<LinkWithRoleModel> wrapFeatureValue(AnnotationFeature aFeature, CAS aCAS,
        Object aValue)
{
    if (aValue instanceof ArrayFS) {
        ArrayFS array = (ArrayFS) aValue;

        Type linkType = aCAS.getTypeSystem().getType(aFeature.getLinkTypeName());
        Feature roleFeat = linkType.getFeatureByBaseName(aFeature
                .getLinkTypeRoleFeatureName());
        Feature targetFeat = linkType.getFeatureByBaseName(aFeature
                .getLinkTypeTargetFeatureName());

        ArrayList<LinkWithRoleModel> links = new ArrayList<>();
        for (FeatureStructure link : array.toArray()) {
            LinkWithRoleModel m = new LinkWithRoleModel();
            m.role = link.getStringValue(roleFeat);
            m.targetAddr = WebAnnoCasUtil.getAddr(link.getFeatureValue(targetFeat));
            m.label = ((AnnotationFS) link.getFeatureValue(targetFeat))
                    .getCoveredText();
            links.add(m);
        }
        
        return links;
    }
    else if (aValue == null ) {
        return new ArrayList<>();
    }
    else {
        throw new IllegalArgumentException(
                "Unable to handle value [" + aValue + "] of type [" + aValue.getClass() + "]");
    }
}
 
Example #21
Source File: WebAnnoCasUtil.java    From webanno with Apache License 2.0 5 votes vote down vote up
public static void setLinkFeatureValue(FeatureStructure aFS, Feature aFeature,
        List<FeatureStructure> linkFSes)
{
    // Create a new array if size differs otherwise re-use existing one
    ArrayFS array = (ArrayFS) WebAnnoCasUtil.getFeatureFS(aFS, aFeature.getShortName());
    if (array == null || (array.size() != linkFSes.size())) {
        array = aFS.getCAS().createArrayFS(linkFSes.size());
    }

    // Fill in links
    array.copyFromArray(linkFSes.toArray(new FeatureStructure[linkFSes.size()]), 0, 0,
            linkFSes.size());

    aFS.setFeatureValue(aFeature, array);
}
 
Example #22
Source File: FSCollectionFactory.java    From uima-uimafit with Apache License 2.0 5 votes vote down vote up
/**
 * Fetch all annotations of the given type or its sub-types from the given FS array.
 * 
 * @param aArray
 *          the FS array
 * @param aType
 *          the CAS type.
 * @return a new collection of all feature structures of the given type.
 */
public static <T extends FeatureStructure> List<T> create(ArrayFS<T> aArray, Type aType) {
  TypeSystem ts = aArray.getCAS().getTypeSystem();
  List<FeatureStructure> data = new ArrayList<FeatureStructure>(aArray.size());
  for (int i = 0; i < aArray.size(); i++) {
    FeatureStructure value = aArray.get(i);
    if (value != null && (aType == null || ts.subsumes(aType, value.getType()))) {
      data.add(value);
    }
  }
  return (List<T>) asList(data.toArray(new FeatureStructure[data.size()]));
}
 
Example #23
Source File: AgreementUtils.java    From webanno with Apache License 2.0 5 votes vote down vote up
private static Object extractLinkFeatureValueForAgreement(FeatureStructure aFs, String aFeature,
        int aLinkIndex, LinkCompareBehavior aLCB)
{
    ArrayFS links = (ArrayFS) aFs.getFeatureValue(aFs.getType().getFeatureByBaseName(
            aFeature));
    FeatureStructure link = links.get(aLinkIndex);
    
    switch (aLCB) {
    case LINK_TARGET_AS_LABEL:
        // FIXME The target feature name should be obtained from the feature
        // definition!
        AnnotationFS target = (AnnotationFS) link.getFeatureValue(link.getType()
                .getFeatureByBaseName("target"));
        
        return target.getBegin() + "-" + target.getEnd() + " ["
                + target.getCoveredText() + "]";
    case LINK_ROLE_AS_LABEL:
        // FIXME The role feature name should be obtained from the feature
        // definition!
        String role = link.getStringValue(link.getType().getFeatureByBaseName(
                "role"));
        
        return role;
    default:
        throw new IllegalStateException("Unknown link target comparison mode ["
                + aLCB + "]");
    }        
}
 
Example #24
Source File: SubjectObjectFeatureSupport.java    From inception with Apache License 2.0 5 votes vote down vote up
@Override
public ArrayList<LinkWithRoleModel> wrapFeatureValue(AnnotationFeature aFeature, CAS aCAS,
        Object aValue)
{
    if (aValue instanceof ArrayFS) {
        ArrayFS array = (ArrayFS) aValue;

        Type linkType = aCAS.getTypeSystem().getType(aFeature.getLinkTypeName());
        Feature roleFeat = linkType.getFeatureByBaseName(aFeature.getLinkTypeRoleFeatureName());
        Feature targetFeat = linkType
                .getFeatureByBaseName(aFeature.getLinkTypeTargetFeatureName());

        ArrayList<LinkWithRoleModel> links = new ArrayList<>();
        for (FeatureStructure link : array.toArray()) {
            LinkWithRoleModel m = new LinkWithRoleModel();
            m.role = link.getStringValue(roleFeat);
            m.targetAddr = WebAnnoCasUtil.getAddr(link.getFeatureValue(targetFeat));
            m.label = ((AnnotationFS) link.getFeatureValue(targetFeat))
                .getCoveredText();
            links.add(m);
        }
        
        return links;
    }
    else if (aValue == null ) {
        return new ArrayList<>();
    }
    else {
        throw new IllegalArgumentException(
                "Unable to handle value [" + aValue + "] of type [" + aValue.getClass() + "]");
    }
}
 
Example #25
Source File: CASImpl.java    From uima-uimaj with Apache License 2.0 4 votes vote down vote up
private ArrayFS createArrayFS(TypeImpl type, int length) {
  checkArrayPreconditions(length);
  return new FSArray(type, this, length);         
}
 
Example #26
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 #27
Source File: SerDesTest4.java    From uima-uimaj with Apache License 2.0 4 votes vote down vote up
private ArrayFS<FeatureStructure> createArrayFS(CAS c, int length) {
  return (ArrayFS<FeatureStructure>) createArray(c, ((CASImpl)c).getTypeSystemImpl().fsArrayType, length);
}
 
Example #28
Source File: DebugFSLogicalStructure.java    From uima-uimaj with Apache License 2.0 4 votes vote down vote up
public static Object getDebugLogicalStructure_FeatureStructure(FeatureStructure fs) {

    if (fs instanceof StringArrayFS) {
      return ((StringArrayFS) fs).toArray();
    }
    if (fs instanceof FloatArrayFS) {
      return ((FloatArrayFS) fs).toArray();
    }
    if (fs instanceof IntArrayFS) {
      return ((IntArrayFS) fs).toArray();
    }
    if (fs instanceof ArrayFS) {
      return ((ArrayFS) fs).toArray();
    }
    CASImpl cas = (CASImpl) fs.getCAS();
    TypeSystem ts = cas.getTypeSystem();
    Type fsType = fs.getType();
    if (ts.subsumes(ts.getType("uima.cas.FloatList"), fsType)) {
      return (floatListToArray(fs));
    }
    if (ts.subsumes(ts.getType("uima.cas.IntegerList"), fsType)) {
      return (integerListToArray(fs));
    }
    if (ts.subsumes(ts.getType("uima.cas.StringList"), fsType)) {
      return (stringListToArray(fs));
    }
    if (ts.subsumes(ts.getType("uima.cas.FSList"), fsType)) {
      return (fsListToArray(fs));
    }

    DebugNameValuePair[] result;
    String typeName = fsType.getName();

    List<Feature> features = fsType.getFeatures();
    int nbrFeats = features.size();
    boolean isAnnotation = false;
    boolean isJCasClass = false;
    if (fs.getClass().getName().equals(typeName)) { // true for JCas cover classes
      isJCasClass = true;
    }

    if (ts.subsumes(cas.getAnnotationType(), fsType)) {
      isAnnotation = true;
    }

    result = new DebugNameValuePair[(isJCasClass ? 0 : 1) // slot for type name if not JCas
            + (isAnnotation ? 3 : nbrFeats) // annotations have 4 slot display
    ];
    int i = 0;
    if (!isJCasClass) {
      result[i++] = new DebugNameValuePair("CasType", typeName);
    }

    if (isAnnotation) {
      DebugNameValuePair[] featResults = new DebugNameValuePair[nbrFeats];
      fillFeatures(featResults, 0, fs, features);
      result[i++] = new DebugNameValuePair("Features", featResults);
      result[i++] = new DebugNameValuePair("Covered Text", ((AnnotationFS) fs).getCoveredText());
      result[i++] = new DebugNameValuePair("SubAnnotations", new UnexpandedFeatureStructures(
              (AnnotationFS) fs));
    } else {
      fillFeatures(result, isJCasClass ? 0 : 1, fs, features);
    }

    return result;
  }
 
Example #29
Source File: CASImpl.java    From uima-uimaj with Apache License 2.0 4 votes vote down vote up
@Override
public ArrayFS createArrayFS(int length) {
  return createArrayFS(getTypeSystemImpl().fsArrayType, length);
}
 
Example #30
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);

  }