Java Code Examples for org.apache.uima.cas.text.AnnotationFS#getFeatureValue()

The following examples show how to use org.apache.uima.cas.text.AnnotationFS#getFeatureValue() . 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: RelationAttachmentBehavior.java    From webanno with Apache License 2.0 6 votes vote down vote up
public static FeatureStructure[] resolve(RelationAdapter aAdapter, AnnotationFS aRelation)
{
    Type type = aRelation.getType();
    Feature targetFeature = type.getFeatureByBaseName(aAdapter.getTargetFeatureName());
    Feature sourceFeature = type.getFeatureByBaseName(aAdapter.getSourceFeatureName());
    
    FeatureStructure targetFs;
    FeatureStructure sourceFs;
    
    if (aAdapter.getAttachFeatureName() != null) {
        Type spanType = getType(aRelation.getCAS(), aAdapter.getAttachTypeName());
        Feature arcSpanFeature = spanType.getFeatureByBaseName(aAdapter.getAttachFeatureName());
        targetFs = aRelation.getFeatureValue(targetFeature).getFeatureValue(arcSpanFeature);
        sourceFs = aRelation.getFeatureValue(sourceFeature).getFeatureValue(arcSpanFeature);
    }
    else {
        targetFs = aRelation.getFeatureValue(targetFeature);
        sourceFs = aRelation.getFeatureValue(sourceFeature);
    }
    
    return new FeatureStructure[] { sourceFs, targetFs };
}
 
Example 2
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 3
Source File: RemoveDanglingRelationsRepair.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)
{
    Set<FeatureStructure> nonIndexed = getNonIndexedFSes(aCas);
    
    Set<FeatureStructure> toDelete = new LinkedHashSet<>();
    
    for (AnnotationFS fs : aCas.getAnnotationIndex()) {
        Type t = fs.getType();
        
        Feature sourceFeat = t.getFeatureByBaseName(WebAnnoConst.FEAT_REL_SOURCE);
        Feature targetFeat = t.getFeatureByBaseName(WebAnnoConst.FEAT_REL_TARGET);
        
        // Is this a relation?
        if (!(sourceFeat != null && targetFeat != null)) {
            continue;
        }
        
        FeatureStructure source = fs.getFeatureValue(sourceFeat);
        FeatureStructure target = fs.getFeatureValue(targetFeat);
        
        // Does it point to deleted spans?
        if (nonIndexed.contains(source) || nonIndexed.contains(target)) {
            toDelete.add(fs);
        }
    }

    // Delete those relations that pointed to deleted spans
    if (!toDelete.isEmpty()) {
        toDelete.forEach(aCas::removeFsFromIndexes);
        aMessages.add(new LogMessage(this, LogLevel.INFO, "Removed [%d] dangling relations.",
                nonIndexed.size()));
    }
}
 
Example 4
Source File: AutomationUtil.java    From webanno with Apache License 2.0 5 votes vote down vote up
private static boolean isSamAnno(Type aType, AnnotationFS aMFs, AnnotationFS aFs)
{
    for (Feature f : aType.getFeatures()) {
        // anywhere is ok
        if (f.getName().equals(CAS.FEATURE_FULL_NAME_BEGIN)) {
            continue;
        }
        // anywhere is ok
        if (f.getName().equals(CAS.FEATURE_FULL_NAME_END)) {
            continue;
        }
        if (!f.getRange().isPrimitive() && aMFs.getFeatureValue(f) instanceof SofaFS) {
            continue;
        }
        // do not attach relation on empty span annotations
        if (aMFs.getFeatureValueAsString(f) == null) {
            continue;
        }
        if (aFs.getFeatureValueAsString(f) == null) {
            continue;
        }
        if (!aMFs.getFeatureValueAsString(f).equals(aFs.getFeatureValueAsString(f))) {
            return false;
        }
    }
    return true;
}
 
Example 5
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 6
Source File: CurationEditorExtension.java    From inception with Apache License 2.0 4 votes vote down vote up
/**
 * Save annotation identified by aVID from user CAS to given curator's CAS
 */
private void saveAnnotation(String aAction, AnnotationActionHandler aPanel,
        AnnotatorState aState, AjaxRequestTarget aTarget, CAS aTargetCas, VID aVID)
    throws IOException, AnnotationException
{
    AnnotationLayer layer = annotationService.getLayer(aVID.getLayerId());
    
    // get user CAS and annotation (to be merged into curator's)
    SourceDocument doc = aState.getDocument();
    String srcUser = ((CurationVID) aVID).getUsername();
    
    if (!documentService.existsAnnotationDocument(doc, srcUser)) {
        log.error(
              String.format("Source CAS of %s for curation not found", srcUser));
        return;
    }
    
    CAS srcCas = documentService.readAnnotationCas(doc, srcUser);
    AnnotationFS sourceAnnotation = selectAnnotationByAddr(srcCas, aVID.getId());
    
    // merge into curator's CAS depending on annotation type (span or arc)
    CasMerge casMerge = new CasMerge(annotationService);
    CasMergeOperationResult mergeResult;
    if (ACTION_SELECT_SPAN.equals(aAction.toString())) {
        mergeResult = casMerge.mergeSpanAnnotation(doc, srcUser, layer, aTargetCas,
                sourceAnnotation, layer.isAllowStacking());
        // open created/updates FS in annotation detail editorpanel
        aState.getSelection().selectSpan(new VID(mergeResult.getResultFSAddress()), aTargetCas,
                sourceAnnotation.getBegin(), sourceAnnotation.getEnd());
        
    }
    else if (ACTION_SELECT_ARC.equals(aAction.toString())) {
        // this is a slot arc
        if (aVID.isSlotSet()) {
            TypeAdapter adapter = annotationService.getAdapter(layer);
            AnnotationFeature feature = adapter.listFeatures().stream().sequential()
                    .skip(aVID.getAttribute()).findFirst().get();

            mergeResult = casMerge.mergeSlotFeature(doc, srcUser, layer, aTargetCas,
                    sourceAnnotation, feature.getName(), aVID.getSlot());
            // open created/updates FS in annotation detail editorpanel
            aState.getSelection().selectSpan(new VID(mergeResult.getResultFSAddress()),
                    aTargetCas, sourceAnnotation.getBegin(), sourceAnnotation.getEnd());
        }
        // normal relation annotation arc is clicked
        else {
            mergeResult = casMerge.mergeRelationAnnotation(doc, srcUser, layer, aTargetCas,
                    sourceAnnotation, layer.isAllowStacking());
            // open created/updates FS in annotation detail editorpanel 
            AnnotationFS mergedAnno = selectAnnotationByAddr(aTargetCas,
                    mergeResult.getResultFSAddress());
            Type depType = mergedAnno.getType();
            Feature originFeat = depType.getFeatureByBaseName(WebAnnoConst.FEAT_REL_SOURCE);
            Feature targetFeat = depType.getFeatureByBaseName(WebAnnoConst.FEAT_REL_TARGET);
            AnnotationFS originFS = (AnnotationFS) mergedAnno.getFeatureValue(originFeat);
            AnnotationFS targetFS = (AnnotationFS) mergedAnno.getFeatureValue(targetFeat);
            aState.getSelection().selectArc(new VID(mergeResult.getResultFSAddress()), originFS,
                    targetFS);
        }
    }
    
    aPanel.actionSelect(aTarget);
    aPanel.actionCreateOrUpdate(aTarget, aTargetCas);  //should also update timestamps
}
 
Example 7
Source File: WebannoTsv3Writer.java    From webanno with Apache License 2.0 4 votes vote down vote up
private void setChainAnnotation(JCas aJCas)
{
    for (String l : chainLayers) {
        if (l.equals(Token.class.getName())) {
            continue;
        }

        Map<AnnotationUnit, List<List<String>>> annotationsPertype = null;
        Type type = getType(aJCas.getCas(), l + CHAIN);
        Feature chainFirst = type.getFeatureByBaseName(FIRST);
        int chainNo = 1;
        for (FeatureStructure chainFs : selectFS(aJCas.getCas(), type)) {
            AnnotationFS linkFs = (AnnotationFS) chainFs.getFeatureValue(chainFirst);
            AnnotationUnit unit = getUnit(linkFs.getBegin(), linkFs.getEnd(),
                    linkFs.getCoveredText());
            Type lType = linkFs.getType();

            // this is the layer with annotations
            l = lType.getName();
            if (annotationsPerPostion.get(l) == null) {
                annotationsPertype = new HashMap<>();

            }
            else {
                annotationsPertype = annotationsPerPostion.get(l);
            }
            Feature linkNext = linkFs.getType().getFeatureByBaseName(NEXT);
            int linkNo = 1;
            while (linkFs != null) {
                AnnotationFS nextLinkFs = (AnnotationFS) linkFs.getFeatureValue(linkNext);
                if (nextLinkFs != null) {
                    addChinFeatureAnno(annotationsPertype, lType, linkFs, unit, linkNo,
                            chainNo);
                }
                else {
                    addChinFeatureAnno(annotationsPertype, lType, linkFs, unit, linkNo,
                            chainNo);
                }
                linkFs = nextLinkFs;
                linkNo++;
                if (nextLinkFs != null) {
                    unit = getUnit(linkFs.getBegin(), linkFs.getEnd(), linkFs.getCoveredText());
                }
            }
            if (annotationsPertype.keySet().size() > 0) {
                annotationsPerPostion.put(l, annotationsPertype);
            }
            chainNo++;
        }
    }
}
 
Example 8
Source File: WebannoTsv3Writer.java    From webanno with Apache License 2.0 4 votes vote down vote up
private void setRelationAnnotation(JCas aJCas)
{
    for (String l : relationLayers) {
        if (l.equals(Token.class.getName())) {
            continue;
        }
        Map<AnnotationUnit, List<List<String>>> annotationsPertype;
        if (annotationsPerPostion.get(l) == null) {
            annotationsPertype = new HashMap<>();

        }
        else {
            annotationsPertype = annotationsPerPostion.get(l);
        }
        Type type = getType(aJCas.getCas(), l);
        Feature dependentFeature = null;
        Feature governorFeature = null;

        List<Feature> features = type.getFeatures();
        Collections.sort(features, (a, b) -> 
                StringUtils.compare(a.getShortName(), b.getShortName()));
        for (Feature feature : features) {
            if (feature.getShortName().equals(DEPENDENT)) {

                // check if the dependent is
                dependentFeature = feature;
            }
            if (feature.getShortName().equals(GOVERNOR)) {
                governorFeature = feature;
            }
        }
        for (AnnotationFS fs : CasUtil.select(aJCas.getCas(), type)) {
            AnnotationFS depFs = (AnnotationFS) fs.getFeatureValue(dependentFeature);
            AnnotationFS govFs = (AnnotationFS) fs.getFeatureValue(governorFeature);

            Type govType = govFs.getType();

            AnnotationUnit govUnit = getFirstUnit(
                    getUnit(govFs.getBegin(), govFs.getEnd(), govFs.getCoveredText()));
            if (ambigUnits.get(govType.getName()).get(govUnit) == null) {
                govUnit = getUnit(govFs.getBegin(), govFs.getEnd(), govFs.getCoveredText());
            }

            AnnotationUnit depUnit = getFirstUnit(
                    getUnit(depFs.getBegin(), depFs.getEnd(), depFs.getCoveredText()));
            if (ambigUnits.get(govType.getName()).get(depUnit) == null) {
                depUnit = getUnit(depFs.getBegin(), depFs.getEnd(), depFs.getCoveredText());
            }
            // Since de.tudarmstadt.ukp.dkpro.core.api.syntax.type.dependency.Dependency is over
            // Over POS anno which itself attached to Token, we need the POS type here

            if (type.getName().equals(Dependency.class.getName())) {
                govType = aJCas.getCas().getTypeSystem().getType(POS.class.getName());
            }

            int govRef = 0;
            int depRef = 0;

            // For that unit test case only, where annotations are on Tokens.
            // The WebAnno world do not ever process Token as an annotation
            if (!govType.getName().equals(Token.class.getName())
                    && ambigUnits.get(govType.getName()).get(govUnit).equals(true)) {
                govRef = annotaionRefPerType.get(govType).get(govFs);
            }

            if (!govType.getName().equals(Token.class.getName())
                    && ambigUnits.get(govType.getName()).get(depUnit).equals(true)) {
                depRef = annotaionRefPerType.get(govType).get(depFs);
            }

            setRelationAnnoPerFeature(annotationsPertype, type, fs, depUnit, govUnit, govRef,
                    depRef, govType);

        }
        if (annotationsPertype.keySet().size() > 0) {
            annotationsPerPostion.put(l, annotationsPertype);
        }
    }
}
 
Example 9
Source File: RelationRenderer.java    From webanno with Apache License 2.0 4 votes vote down vote up
@Override
public void render(final CAS aCas, List<AnnotationFeature> aFeatures,
        VDocument aResponse, int aWindowBegin, int aWindowEnd)
{
    RelationAdapter typeAdapter = getTypeAdapter();
    Type type;
    Type spanType;
    try {
        type = getType(aCas, typeAdapter.getAnnotationTypeName());
        spanType = getType(aCas, typeAdapter.getAttachTypeName());
    }
    catch (IllegalArgumentException e) {
        // If the types are not defined, then we do not need to try and render them because the
        // CAS does not contain any instances of them
        return;
    }
    
    List<AnnotationFeature> visibleFeatures = aFeatures.stream()
            .filter(f -> f.isVisible() && f.isEnabled()).collect(Collectors.toList());
    
    Feature dependentFeature = type.getFeatureByBaseName(typeAdapter.getTargetFeatureName());
    Feature governorFeature = type.getFeatureByBaseName(typeAdapter.getSourceFeatureName());

    Feature arcSpanFeature = spanType.getFeatureByBaseName(typeAdapter.getAttachFeatureName());

    FeatureStructure dependentFs;
    FeatureStructure governorFs;

    Map<Integer, Set<Integer>> relationLinks = getRelationLinks(aCas, aWindowBegin, aWindowEnd,
            type, dependentFeature, governorFeature, arcSpanFeature);

    // if this is a governor for more than one dependent, avoid duplicate yield
    List<Integer> yieldDeps = new ArrayList<>();

    // Index mapping annotations to the corresponding rendered arcs
    Map<AnnotationFS, VArc> annoToArcIdx = new HashMap<>();
    
    for (AnnotationFS fs : selectCovered(aCas, type, aWindowBegin, aWindowEnd)) {
        if (typeAdapter.getAttachFeatureName() != null) {
            dependentFs = fs.getFeatureValue(dependentFeature).getFeatureValue(arcSpanFeature);
            governorFs = fs.getFeatureValue(governorFeature).getFeatureValue(arcSpanFeature);
        }
        else {
            dependentFs = fs.getFeatureValue(dependentFeature);
            governorFs = fs.getFeatureValue(governorFeature);
        }

        String bratTypeName = typeAdapter.getEncodedTypeName();
        Map<String, String> features = renderLabelFeatureValues(typeAdapter, fs,
                visibleFeatures);
        
        if (dependentFs == null || governorFs == null) {
            StringBuilder message = new StringBuilder();
            
            message.append("Relation [" + typeAdapter.getLayer().getName() + "] with id ["
                    + getAddr(fs) + "] has loose ends - cannot render.");
            if (typeAdapter.getAttachFeatureName() != null) {
                message.append("\nRelation [" + typeAdapter.getLayer().getName()
                        + "] attached to feature [" + typeAdapter.getAttachFeatureName() + "].");
            }
            message.append("\nDependent: " + dependentFs);
            message.append("\nGovernor: " + governorFs);
            
            RequestCycle requestCycle = RequestCycle.get();
            IPageRequestHandler handler = PageRequestHandlerTracker
                    .getLastHandler(requestCycle);
            Page page = (Page) handler.getPage();
            page.warn(message.toString());
            
            continue;
        }

        VArc arc = new VArc(typeAdapter.getLayer(), fs, bratTypeName, governorFs,
                dependentFs, features);
        arc.addLazyDetails(getLazyDetails(typeAdapter, fs, aFeatures));
        annoToArcIdx.put(fs, arc);

        aResponse.add(arc);

        // Render errors if required features are missing
        renderRequiredFeatureErrors(visibleFeatures, fs, aResponse);
        
        if (relationLinks.keySet().contains(getAddr(governorFs))
                && !yieldDeps.contains(getAddr(governorFs))) {
            yieldDeps.add(getAddr(governorFs));

            // sort the annotations (begin, end)
            List<Integer> sortedDepFs = new ArrayList<>(relationLinks.get(getAddr(governorFs)));
            sortedDepFs
                    .sort(comparingInt(arg0 -> selectAnnotationByAddr(aCas, arg0).getBegin()));

            String cm = getYieldMessage(aCas, sortedDepFs);
            aResponse.add(new VComment(governorFs, VCommentType.YIELD, cm));
        }
    }

    
    for (RelationLayerBehavior behavior : behaviors) {
        behavior.onRender(typeAdapter, aResponse, annoToArcIdx);
    }
}
 
Example 10
Source File: ChainAdapter.java    From webanno with Apache License 2.0 4 votes vote down vote up
/**
 * Get the link following the current link.
 */
private AnnotationFS getNextLink(AnnotationFS aLink)
{
    return (AnnotationFS) aLink.getFeatureValue(aLink.getType().getFeatureByBaseName(
            getLinkNextFeatureName()));
}
 
Example 11
Source File: RelationAdapter.java    From webanno with Apache License 2.0 4 votes vote down vote up
private AnnotationFS getSourceAnnotation(AnnotationFS aTargetFs)
{
    Feature sourceFeature = aTargetFs.getType().getFeatureByBaseName(sourceFeatureName);
    AnnotationFS sourceToken = (AnnotationFS) aTargetFs.getFeatureValue(sourceFeature);
    return sourceToken;
}
 
Example 12
Source File: RelationAdapter.java    From webanno with Apache License 2.0 4 votes vote down vote up
private AnnotationFS getTargetAnnotation(AnnotationFS aTargetFs)
{
    Feature targetFeature = aTargetFs.getType().getFeatureByBaseName(targetFeatureName);
    AnnotationFS targetToken = (AnnotationFS) aTargetFs.getFeatureValue(targetFeature);
    return targetToken;
}
 
Example 13
Source File: RelationOverlapBehavior.java    From webanno with Apache License 2.0 4 votes vote down vote up
private Set<AnnotationFS> stackingRelations(Collection<AnnotationFS> aRelations,
        Feature sourceFeature, Feature targetFeature)
{
    // Here, we must find all stacked relations because they are not permitted.
    // We go through all relations based on the their offsets. Stacked relations must have
    // the same offsets (at least if we consider relations as having a direction, i.e. 
    // that a relation A->B does not count as stacked on a relation B->A). But since there 
    // can be multiple relations going out from the same sourceFS, we need to consider all 
    // of them for potential stacking.
    Set<AnnotationFS> stacking = new HashSet<>();
    List<AnnotationFS> candidates = new ArrayList<>();
    for (AnnotationFS fs : aRelations) {
        AnnotationFS sourceFs = (AnnotationFS) fs.getFeatureValue(sourceFeature);
        AnnotationFS targetFs = (AnnotationFS) fs.getFeatureValue(targetFeature);
        
        // If there are no stacking candidates at the current position yet, collect the
        // first
        if (candidates.isEmpty()) {
            candidates.add(fs);
        }
        // If the current FS is at a different position from the current candidates, clear 
        // the candidates list and add the current one as the first new candidate
        else if (
                candidates.get(0).getBegin() != fs.getBegin() || 
                candidates.get(0).getEnd() != fs.getEnd()
        ) {
            candidates.clear();
            candidates.add(fs);
        }
        // If there are already stacking candidates, check if the current FS is stacking on 
        // any of them. If yes, generate an error message
        else {
            for (AnnotationFS cand : candidates) {
                FeatureStructure candidateOriginFS = cand.getFeatureValue(sourceFeature);
                FeatureStructure candidateTargetFS = cand.getFeatureValue(targetFeature);

                if (stacking(candidateOriginFS, candidateTargetFS, sourceFs, targetFs)) {
                    stacking.add(fs);
                    stacking.add(cand);
                }
            }
        }
    }
    
    return stacking;
}
 
Example 14
Source File: RelationCrossSentenceBehavior.java    From webanno with Apache License 2.0 4 votes vote down vote up
@Override
public List<Pair<LogMessage, AnnotationFS>> onValidate(TypeAdapter aAdapter, CAS aCas)
{
    // If crossing sentence boundaries is permitted, then there is nothing to validate here
    if (aAdapter.getLayer().isCrossSentence()) {
        return emptyList();
    }
    
    RelationAdapter adapter = (RelationAdapter) aAdapter;
    Type type = getType(aCas, aAdapter.getAnnotationTypeName());
    Feature targetFeature = type.getFeatureByBaseName(adapter.getTargetFeatureName());
    Feature sourceFeature = type.getFeatureByBaseName(adapter.getSourceFeatureName());
    
    // If there are no annotations on this layer, nothing to do
    Collection<AnnotationFS> annotations = select(aCas, type);
    if (annotations.isEmpty()) {
        return emptyList();
    }

    // Prepare feedback messsage list
    List<Pair<LogMessage, AnnotationFS>> messages = new ArrayList<>();

    // Build indexes to allow quickly looking up the sentence by its begin/end offsets. Since
    // The indexes are navigable, we can also find the sentences starting/ending closes to a
    // particular offset, even if it is not the start/end offset of a sentence.
    NavigableMap<Integer, AnnotationFS> sentBeginIdx = new TreeMap<>();
    NavigableMap<Integer, AnnotationFS> sentEndIdx = new TreeMap<>();
    for (AnnotationFS sent : select(aCas, getType(aCas, Sentence.class))) {
        sentBeginIdx.put(sent.getBegin(), sent);
        sentEndIdx.put(sent.getEnd(), sent);
    }
    
    for (AnnotationFS fs : annotations) {
        AnnotationFS sourceFs = (AnnotationFS) fs.getFeatureValue(sourceFeature);
        AnnotationFS targetFs = (AnnotationFS) fs.getFeatureValue(targetFeature);

        Entry<Integer, AnnotationFS> s1 = sentBeginIdx.floorEntry(sourceFs.getBegin());
        Entry<Integer, AnnotationFS> s2 = sentEndIdx.ceilingEntry(targetFs.getEnd());
        
        if (s1 == null || s2 == null) {
            messages.add(Pair.of(LogMessage.error(this,
                    "Unable to determine any sentences overlapping with [%d-%d]",
                    sourceFs.getBegin(), targetFs.getEnd()), fs));
            continue;
        }
        
        if (!WebAnnoCasUtil.isSame(s1.getValue(), s2.getValue())) {
            messages.add(Pair.of(
                    LogMessage.error(this, "Crossing sentence boundaries is not permitted."),
                    fs));
        }
    }

    return messages;
    
}
 
Example 15
Source File: AnnotationDetailEditorPanel.java    From webanno with Apache License 2.0 4 votes vote down vote up
private static Set<AnnotationFS> getAttachedRels(AnnotationSchemaService aAS, AnnotationFS aFs,
        AnnotationLayer aLayer)
{
    CAS cas = aFs.getCAS();
    Set<AnnotationFS> toBeDeleted = new HashSet<>();
    for (AnnotationLayer relationLayer : aAS.listAttachedRelationLayers(aLayer)) {
        RelationAdapter relationAdapter = (RelationAdapter) aAS.getAdapter(relationLayer);
        Type relationType = CasUtil.getType(cas, relationLayer.getName());
        Feature sourceFeature = relationType.getFeatureByBaseName(relationAdapter
            .getSourceFeatureName());
        Feature targetFeature = relationType.getFeatureByBaseName(relationAdapter
            .getTargetFeatureName());

        // This code is already prepared for the day that relations can go between
        // different layers and may have different attach features for the source and
        // target layers.
        Feature relationSourceAttachFeature = null;
        Feature relationTargetAttachFeature = null;
        if (relationAdapter.getAttachFeatureName() != null) {
            relationSourceAttachFeature = sourceFeature.getRange().getFeatureByBaseName(
                relationAdapter.getAttachFeatureName());
            relationTargetAttachFeature = targetFeature.getRange().getFeatureByBaseName(
                relationAdapter.getAttachFeatureName());
        }

        for (AnnotationFS relationFS : CasUtil.select(cas, relationType)) {
            // Here we get the annotations that the relation is pointing to in the UI
            FeatureStructure sourceFS;
            if (relationSourceAttachFeature != null) {
                sourceFS = relationFS.getFeatureValue(sourceFeature).getFeatureValue(
                    relationSourceAttachFeature);
            }
            else {
                sourceFS = relationFS.getFeatureValue(sourceFeature);
            }

            FeatureStructure targetFS;
            if (relationTargetAttachFeature != null) {
                targetFS = relationFS.getFeatureValue(targetFeature).getFeatureValue(
                    relationTargetAttachFeature);
            }
            else {
                targetFS = relationFS.getFeatureValue(targetFeature);
            }

            if (isSame(sourceFS, aFs) || isSame(targetFS, aFs)) {
                toBeDeleted.add(relationFS);
                LOG.debug("Deleted relation [" + getAddr(relationFS) + "] from layer ["
                    + relationLayer.getName() + "]");
            }
        }
    }

    return toBeDeleted;
}