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

The following examples show how to use org.apache.uima.cas.CAS#removeFsFromIndexes() . 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: RemoteStringMatchingNerRecommender.java    From inception with Apache License 2.0 6 votes vote down vote up
public String predict(String aPredictionRequestJson) throws IOException, UIMAException,
    SAXException, RecommendationException
{
    PredictionRequest request = deserializePredictionRequest(aPredictionRequestJson);
    CAS cas = deserializeCas(request.getDocument().getXmi(), request.getTypeSystem());

    // Only work on real annotations, not on predictions
    Type predictedType = CasUtil.getType(cas, recommender.getLayer().getName());
    Feature feature = predictedType.getFeatureByBaseName(FEATURE_NAME_IS_PREDICTION);

    for (AnnotationFS fs : CasUtil.select(cas, predictedType)) {
        if (fs.getBooleanValue(feature)) {
            cas.removeFsFromIndexes(fs);
        }
    }

    recommendationEngine.predict(context, cas);

    return buildPredictionResponse(cas);
}
 
Example 2
Source File: SpanAdapter.java    From webanno with Apache License 2.0 6 votes vote down vote up
@Override
public void delete(SourceDocument aDocument, String aUsername, CAS aCas, VID aVid)
{
    AnnotationFS fs = selectByAddr(aCas, AnnotationFS.class, aVid.getId());
    aCas.removeFsFromIndexes(fs);

    // delete associated attachFeature
    if (getAttachTypeName() != null) {
        Type theType = CasUtil.getType(aCas, getAttachTypeName());
        Feature attachFeature = theType.getFeatureByBaseName(getAttachFeatureName());
        if (attachFeature != null) {
            CasUtil.selectCovered(aCas, theType, fs.getBegin(), fs.getEnd()).get(0)
                    .setFeatureValue(attachFeature, null);
        }
    }
    
    publishEvent(new SpanDeletedEvent(this, aDocument, aUsername, getLayer(), fs));
}
 
Example 3
Source File: DocumentMetadataLayerAdapter.java    From inception with Apache License 2.0 5 votes vote down vote up
@Override
public void delete(SourceDocument aDocument, String aUsername, CAS aCas, VID aVid)
{
    AnnotationBaseFS fs = (AnnotationBaseFS) selectFsByAddr(aCas, aVid.getId());
    aCas.removeFsFromIndexes(fs);

    publishEvent(new DocumentMetadataDeletedEvent(this, aDocument, aUsername, getLayer(), fs));
}
 
Example 4
Source File: RelationAdapter.java    From webanno with Apache License 2.0 5 votes vote down vote up
@Override
public void delete(SourceDocument aDocument, String aUsername, CAS aCas, VID aVid)
{
    AnnotationFS fs = selectByAddr(aCas, AnnotationFS.class, aVid.getId());
    aCas.removeFsFromIndexes(fs);
    publishEvent(new RelationDeletedEvent(this, aDocument, aUsername, getLayer(),
            fs, getTargetAnnotation(fs), getSourceAnnotation(fs)));
}
 
Example 5
Source File: WebAnnoCasUtil.java    From webanno with Apache License 2.0 5 votes vote down vote up
public static FeatureStructure createDocumentMetadata(CAS aCas)
{
    Type type = getType(aCas, DocumentMetaData.class);
    FeatureStructure dmd;
    if (aCas.getDocumentText() != null) {
        dmd = aCas.createAnnotation(type, 0, aCas.getDocumentText().length());
    }
    else {
        dmd = aCas.createAnnotation(type, 0, 0);
    }
    
    // If there is already a DocumentAnnotation copy it's information and delete it
    FeatureStructure da = aCas.getDocumentAnnotation();
    if (da != null) {
        FSUtil.setFeature(dmd, FEATURE_BASE_NAME_LANGUAGE,
                FSUtil.getFeature(da, FEATURE_BASE_NAME_LANGUAGE, String.class));
        FSUtil.setFeature(dmd, FEATURE_BASE_NAME_BEGIN,
                FSUtil.getFeature(da, FEATURE_BASE_NAME_BEGIN, Integer.class));
        FSUtil.setFeature(dmd, FEATURE_BASE_NAME_END,
                FSUtil.getFeature(da, FEATURE_BASE_NAME_END, Integer.class));
        aCas.removeFsFromIndexes(da);
    }
    else if (aCas.getDocumentText() != null) {
        FSUtil.setFeature(dmd, FEATURE_BASE_NAME_BEGIN, 0);
        FSUtil.setFeature(dmd, FEATURE_BASE_NAME_END, aCas.getDocumentText().length());
    }
    aCas.addFsToIndexes(dmd);
    return dmd;
}
 
Example 6
Source File: RemoveZeroSizeTokensAndSentencesRepair.java    From webanno with Apache License 2.0 5 votes vote down vote up
@Override
public void repair(Project aProject, CAS aCas, List<LogMessage> aMessages)
{
    for (AnnotationFS s : selectSentences(aCas)) {
        if (s.getBegin() >= s.getEnd()) {
            aCas.removeFsFromIndexes(s);
            aMessages.add(
                    new LogMessage(this, INFO, "Removed sentence with illegal span: %s", s));
        }
    }

    for (AnnotationFS t : WebAnnoCasUtil.selectTokens(aCas)) {
        if (t.getBegin() >= t.getEnd()) {
            AnnotationFS lemma = FSUtil.getFeature(t, "lemma", AnnotationFS.class);
            if (lemma != null) {
                aCas.removeFsFromIndexes(lemma);
                aMessages.add(new LogMessage(this, INFO,
                        "Removed lemma attached to token with illegal span: %s", t));
            }

            AnnotationFS pos = FSUtil.getFeature(t, "pos", AnnotationFS.class);
            if (pos != null) {
                aCas.removeFsFromIndexes(pos);
                aMessages.add(new LogMessage(this, INFO,
                        "Removed POS attached to token with illegal span: %s", t));
            }

            AnnotationFS stem = FSUtil.getFeature(t, "stem", AnnotationFS.class);
            if (stem != null) {
                aCas.removeFsFromIndexes(stem);
                aMessages.add(new LogMessage(this, INFO,
                        "Removed stem attached to token with illegal span: %s", t));
            }

            aCas.removeFsFromIndexes(t);
            aMessages.add(new LogMessage(this, INFO, "Removed token with illegal span: %s", t));
        }
    }
}
 
Example 7
Source File: AutomationUtil.java    From webanno with Apache License 2.0 5 votes vote down vote up
public static void clearAnnotations(CAS aCas, AnnotationFeature aFeature)
    throws IOException
{
    // Check if annotation layer is attached to another layer
    String attachTypeName = aFeature.getLayer().getAttachType() == null ? null : aFeature
            .getLayer().getAttachType().getName();
    Type attachType = null;
    Feature attachFeature = null;
    if (attachTypeName != null) {
        attachType = CasUtil.getType(aCas, attachTypeName);
        attachFeature = attachType
                .getFeatureByBaseName(aFeature.getLayer().getAttachFeature().getName());
    }
    
    List<AnnotationFS> annotationsToRemove = new ArrayList<>();
    Type type = CasUtil.getType(aCas, aFeature.getLayer().getName());
    annotationsToRemove.addAll(select(aCas, type));
    
    for (AnnotationFS annotation : annotationsToRemove) {
        if (attachFeature != null) {
            // Unattach the annotation to be removed
            for (AnnotationFS attach : selectCovered(attachType, annotation)) {
                FeatureStructure existing = attach.getFeatureValue(attachFeature);
                if (annotation.equals(existing)) {
                    attach.setFeatureValue(attachFeature, null);
                }
            }
        }
        aCas.removeFsFromIndexes(annotation);
    }
}
 
Example 8
Source File: NewPrimitiveTypesTest.java    From uima-uimaj with Apache License 2.0 5 votes vote down vote up
public void testClone() throws Exception {
  createExampleFS(cas);
  // get the example FS
  CAS englishView = cas.getView("EnglishDocument");
  FSIterator iter = englishView.getAnnotationIndex().iterator();
  // skip document annotation
  iter.moveToNext();
  // the exampleType fs
  AnnotationFS fs = (AnnotationFS) iter.get();

  // clone it
  AnnotationFS clone = (AnnotationFS) fs.clone();

  // substitute the clone for the original in the index,
  // and validate that it was correctly copied
  englishView.removeFsFromIndexes(fs);
  englishView.addFsToIndexes(clone);
  validateFSData(cas);

  // editing the original FS should not change the clone
  englishView.removeFsFromIndexes(fs);  // does nothing, is not in the index, the clone is
  fs.setStringValue(stringFeature, "foo");
  fs.setFloatValue(floatFeature, -1f);
  fs.setByteValue(byteFeature, (byte) -1);
  fs.setBooleanValue(booleanFeature, false);
  fs.setShortValue(shortFeature, (short) -1);
  fs.setLongValue(longFeature, -1);
  fs.setDoubleValue(doubleFeature, -1);
  fs.setBegin(clone.getBegin() + 1);  // to be sure that fs is beyond the original
  englishView.addFsToIndexes(fs);  // will add, is no longer "equal" to the clone
  validateFSData(cas);
}
 
Example 9
Source File: ChainAdapter.java    From webanno with Apache License 2.0 4 votes vote down vote up
public int addArc(SourceDocument aDocument, String aUsername, CAS aCas,
        AnnotationFS aOriginFs, AnnotationFS aTargetFs)
{
    // Determine if the links are adjacent. If so, just update the arc label
    AnnotationFS originNext = getNextLink(aOriginFs);
    AnnotationFS targetNext = getNextLink(aTargetFs);

    // adjacent - origin links to target
    if (WebAnnoCasUtil.isSame(originNext, aTargetFs)) {
    }
    // adjacent - target links to origin
    else if (WebAnnoCasUtil.isSame(targetNext, aOriginFs)) {
        if (isLinkedListBehavior()) {
            throw new IllegalStateException("Cannot change direction of a link within a chain");
        }
        else {
            // in set mode there are no arc labels anyway
        }
    }
    // if origin and target are not adjacent
    else {
        FeatureStructure originChain = getChainForLink(aCas, aOriginFs);
        FeatureStructure targetChain = getChainForLink(aCas, aTargetFs);

        AnnotationFS targetPrev = getPrevLink(targetChain, aTargetFs);

        if (!WebAnnoCasUtil.isSame(originChain, targetChain)) {
            if (isLinkedListBehavior()) {
                // if the two links are in different chains then split the chains up at the
                // origin point and target point and create a new link between origin and target
                // the tail of the origin chain becomes a new chain

                // if originFs has a next, then split of the origin chain up
                // the rest becomes its own chain
                if (originNext != null) {
                    newChain(aCas, originNext);
                    // we set originNext below
                    // we set the arc label below
                }

                // if targetFs has a prev, then split it off
                if (targetPrev != null) {
                    setNextLink(targetPrev, null);
                }
                // if it has no prev then we fully append the target chain to the origin chain
                // and we can remove the target chain head
                else {
                    aCas.removeFsFromIndexes(targetChain);
                }

                // connect the rest of the target chain to the origin chain
                setNextLink(aOriginFs, aTargetFs);
            }
            else {
                // collect all the links
                List<AnnotationFS> links = new ArrayList<>();
                links.addAll(collectLinks(originChain));
                links.addAll(collectLinks(targetChain));

                // sort them ascending by begin and descending by end (default UIMA order)
                links.sort(new AnnotationComparator());

                // thread them
                AnnotationFS prev = null;
                for (AnnotationFS link : links) {
                    if (prev != null) {
                        // Set next link
                        setNextLink(prev, link);
                        // // Clear arc label - it makes no sense in this mode
                        // setLabel(prev, aFeature, null);
                    }
                    prev = link;
                }

                // make sure the last link terminates the chain
                setNextLink(links.get(links.size() - 1), null);

                // the chain head needs to point to the first link
                setFirstLink(originChain, links.get(0));

                // we don't need the second chain head anymore
                aCas.removeFsFromIndexes(targetChain);
            }
        }
        else {
            // if the two links are in the same chain, we just ignore the action
            if (isLinkedListBehavior()) {
                throw new IllegalStateException(
                        "Cannot connect two spans that are already part of the same chain");
            }
        }
    }
    
    publishEvent(new ChainLinkCreatedEvent(this, aDocument, aUsername, getLayer(), aOriginFs));

    // We do not actually create a new FS for the arc. Features are set on the originFS.
    return WebAnnoCasUtil.getAddr(aOriginFs);
}
 
Example 10
Source File: ChainAdapter.java    From webanno with Apache License 2.0 4 votes vote down vote up
private void deleteSpan(SourceDocument aDocument, String aUsername, CAS aCas, int aAddress)
{
    Type chainType = CasUtil.getType(aCas, getChainTypeName());

    AnnotationFS linkToDelete = WebAnnoCasUtil.selectByAddr(aCas, AnnotationFS.class,
            aAddress);

    // case 1 "removing first link": we keep the existing chain head and just remove the
    // first element
    //
    // case 2 "removing middle link": the new chain consists of the rest, the old chain head
    // remains
    //
    // case 3 "removing the last link": the old chain head remains and the last element of the
    // chain is removed.

    // To know which case we have, we first need to find the chain containing the element to
    // be deleted.
    FeatureStructure oldChainFs = null;
    AnnotationFS prevLinkFs = null;
    chainLoop: for (FeatureStructure chainFs : selectFS(aCas, chainType)) {
        AnnotationFS linkFs = getFirstLink(chainFs);
        prevLinkFs = null; // Reset when entering new chain!

        // Now we seek the link within the current chain
        while (linkFs != null) {
            if (WebAnnoCasUtil.isSame(linkFs, linkToDelete)) {
                oldChainFs = chainFs;
                break chainLoop;
            }
            prevLinkFs = linkFs;
            linkFs = getNextLink(linkFs);
        }
    }

    // Did we find the chain?!
    if (oldChainFs == null) {
        throw new IllegalArgumentException("Chain link with address [" + aAddress
                + "] not found in any chain!");
    }

    AnnotationFS followingLinkToDelete = getNextLink(linkToDelete);

    if (prevLinkFs == null) {
        // case 1: first element removed
        setFirstLink(oldChainFs, followingLinkToDelete);
        aCas.removeFsFromIndexes(linkToDelete);

        // removed last element form chain?
        if (followingLinkToDelete == null) {
            aCas.removeFsFromIndexes(oldChainFs);
        }
    }
    else if (followingLinkToDelete == null) {
        // case 3: removing the last link (but not leaving the chain empty)
        setNextLink(prevLinkFs, null);
        aCas.removeFsFromIndexes(linkToDelete);
    }
    else if (prevLinkFs != null && followingLinkToDelete != null) {
        // case 2: removing a middle link

        // Set up new chain for rest
        newChain(aCas, followingLinkToDelete);

        // Cut off from old chain
        setNextLink(prevLinkFs, null);
        
        // Delete middle link
        aCas.removeFsFromIndexes(linkToDelete);
    }
    else {
        throw new IllegalStateException(
                "Unexpected situation while removing link. Please contact developers.");
    }
    
    publishEvent(
            new ChainSpanDeletedEvent(this, aDocument, aUsername, getLayer(), linkToDelete));
}
 
Example 11
Source File: AnnotationDetailEditorPanel.java    From webanno with Apache License 2.0 4 votes vote down vote up
@Override
public void actionReverse(AjaxRequestTarget aTarget)
    throws IOException, AnnotationException
{
    aTarget.addChildren(getPage(), IFeedback.class);
    
    CAS cas = getEditorCas();

    AnnotatorState state = getModelObject();

    AnnotationFS idFs = selectAnnotationByAddr(cas,
            state.getSelection().getAnnotation().getId());

    cas.removeFsFromIndexes(idFs);

    AnnotationFS originFs = selectAnnotationByAddr(cas, state.getSelection().getOrigin());
    AnnotationFS targetFs = selectAnnotationByAddr(cas, state.getSelection().getTarget());

    List<FeatureState> featureStates = getModelObject().getFeatureStates();

    TypeAdapter adapter = annotationService.getAdapter(state.getSelectedAnnotationLayer());
    if (adapter instanceof RelationAdapter) {
        // If no features, still create arc #256
        AnnotationFS arc = ((RelationAdapter) adapter).add(state.getDocument(),
                state.getUser().getUsername(), targetFs, originFs, cas);
        state.getSelection().setAnnotation(new VID(getAddr(arc)));
        
        for (FeatureState featureState : featureStates) {
            adapter.setFeatureValue(state.getDocument(), state.getUser().getUsername(), cas,
                    getAddr(arc), featureState.feature, featureState.value);
        }
    }
    else {
        error("chains cannot be reversed");
        return;
    }

    // persist changes
    editorPage.writeEditorCas(cas);
    int sentenceNumber = getSentenceNumber(cas, originFs.getBegin());
    state.setFocusUnitIndex(sentenceNumber);
    state.getDocument().setSentenceAccessed(sentenceNumber);

    autoScroll(cas);

    state.rememberFeatures();

    // in case the user re-reverse it
    state.getSelection().reverseArc();

    onChange(aTarget);
}
 
Example 12
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 13
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 14
Source File: FeatureStructureImplC.java    From uima-uimaj with Apache License 2.0 2 votes vote down vote up
/**
 * remove this FS from indexes in a specific view, perhaps different from the view where this was created.
 * @param cas the Cas
 */
public void removeFromIndexes(CAS cas) {
  cas.removeFsFromIndexes(this);
}