Search in sources :

Example 1 with ChainLinkCreatedEvent

use of de.tudarmstadt.ukp.clarin.webanno.api.annotation.event.ChainLinkCreatedEvent in project webanno by webanno.

the class ChainAdapter method addArc.

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)) {
    } else // adjacent - target links to origin
    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
        }
    } else // if origin and target are not adjacent
    {
        FeatureStructure originChain = getChainForLink(aCas, aOriginFs);
        FeatureStructure targetChain = getChainForLink(aCas, aTargetFs);
        AnnotationFS targetPrev = getPrevLink(targetChain, aTargetFs);
        if (!WebAnnoCasUtil.isSame(originChain, targetChain)) {
            if (isLinkedListBehavior()) {
                // 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);
                } else // if it has no prev then we fully append the target chain to the origin chain
                // and we can remove the target chain head
                {
                    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);
}
Also used : FeatureStructure(org.apache.uima.cas.FeatureStructure) AnnotationFS(org.apache.uima.cas.text.AnnotationFS) AnnotationComparator(de.tudarmstadt.ukp.clarin.webanno.api.annotation.util.AnnotationComparator) ChainLinkCreatedEvent(de.tudarmstadt.ukp.clarin.webanno.api.annotation.event.ChainLinkCreatedEvent) ArrayList(java.util.ArrayList) Collections.emptyList(java.util.Collections.emptyList) List(java.util.List)

Aggregations

ChainLinkCreatedEvent (de.tudarmstadt.ukp.clarin.webanno.api.annotation.event.ChainLinkCreatedEvent)1 AnnotationComparator (de.tudarmstadt.ukp.clarin.webanno.api.annotation.util.AnnotationComparator)1 ArrayList (java.util.ArrayList)1 Collections.emptyList (java.util.Collections.emptyList)1 List (java.util.List)1 FeatureStructure (org.apache.uima.cas.FeatureStructure)1 AnnotationFS (org.apache.uima.cas.text.AnnotationFS)1