use of org.apache.uima.cas.FeatureStructure in project webanno by webanno.
the class CasDoctorUtils method getNonIndexedFSes.
public static Set<FeatureStructure> getNonIndexedFSes(CAS aCas) {
TypeSystem ts = aCas.getTypeSystem();
Set<FeatureStructure> allIndexedFS = collectIndexed(aCas);
Set<FeatureStructure> allReachableFS = collectReachable(aCas);
// Remove all that are indexed
allReachableFS.removeAll(allIndexedFS);
// Remove all that are not annotations
allReachableFS.removeIf(fs -> !ts.subsumes(aCas.getAnnotationType(), fs.getType()));
// All that is left are non-index annotations
return allReachableFS;
}
use of org.apache.uima.cas.FeatureStructure in project webanno by webanno.
the class RelationRenderer method render.
@Override
public void render(final JCas aJcas, List<AnnotationFeature> aFeatures, VDocument aResponse, AnnotatorState aBratAnnotatorModel) {
List<AnnotationFeature> visibleFeatures = aFeatures.stream().filter(f -> f.isVisible() && f.isEnabled()).collect(Collectors.toList());
ArcAdapter typeAdapter = getTypeAdapter();
Type type = getType(aJcas.getCas(), typeAdapter.getAnnotationTypeName());
int windowBegin = aBratAnnotatorModel.getWindowBeginOffset();
int windowEnd = aBratAnnotatorModel.getWindowEndOffset();
Feature dependentFeature = type.getFeatureByBaseName(typeAdapter.getTargetFeatureName());
Feature governorFeature = type.getFeatureByBaseName(typeAdapter.getSourceFeatureName());
Type spanType = getType(aJcas.getCas(), typeAdapter.getAttachTypeName());
Feature arcSpanFeature = spanType.getFeatureByBaseName(typeAdapter.getAttachFeatureName());
FeatureStructure dependentFs;
FeatureStructure governorFs;
Map<Integer, Set<Integer>> relationLinks = getRelationLinks(aJcas, windowBegin, windowEnd, type, dependentFeature, governorFeature, arcSpanFeature);
// if this is a governor for more than one dependent, avoid duplicate yield
List<Integer> yieldDeps = new ArrayList<>();
for (AnnotationFS fs : selectCovered(aJcas.getCas(), type, windowBegin, windowEnd)) {
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 = TypeUtil.getUiTypeName(typeAdapter);
Map<String, String> features = getFeatures(typeAdapter, fs, visibleFeatures);
if (dependentFs == null || governorFs == null) {
RequestCycle requestCycle = RequestCycle.get();
IPageRequestHandler handler = PageRequestHandlerTracker.getLastHandler(requestCycle);
Page page = (Page) handler.getPage();
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);
page.warn(message.toString());
continue;
}
aResponse.add(new VArc(typeAdapter.getLayer(), fs, bratTypeName, governorFs, dependentFs, features));
// 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 -> selectByAddr(aJcas, arg0).getBegin()));
String cm = getYieldMessage(aJcas, sortedDepFs);
aResponse.add(new VComment(governorFs, VCommentType.YIELD, cm));
}
}
}
use of org.apache.uima.cas.FeatureStructure in project webanno by webanno.
the class SlotFeatureSupport method getFeatureValue.
@Override
public <T> T getFeatureValue(AnnotationFeature aFeature, FeatureStructure aFS) {
// Get type and features - we need them later in the loop
Feature linkFeature = aFS.getType().getFeatureByBaseName(aFeature.getName());
Type linkType = aFS.getCAS().getTypeSystem().getType(aFeature.getLinkTypeName());
Feature roleFeat = linkType.getFeatureByBaseName(aFeature.getLinkTypeRoleFeatureName());
Feature targetFeat = linkType.getFeatureByBaseName(aFeature.getLinkTypeTargetFeatureName());
List<LinkWithRoleModel> links = new ArrayList<>();
ArrayFS array = (ArrayFS) aFS.getFeatureValue(linkFeature);
if (array != null) {
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 (T) links;
}
use of org.apache.uima.cas.FeatureStructure in project webanno by webanno.
the class ArcAdapter method delete.
@Override
public void delete(AnnotatorState aState, JCas aJCas, VID aVid) {
FeatureStructure fs = selectByAddr(aJCas, FeatureStructure.class, aVid.getId());
aJCas.removeFsFromIndexes(fs);
}
use of org.apache.uima.cas.FeatureStructure in project webanno by webanno.
the class ChainAdapter method addArc.
public int addArc(JCas aJCas, 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 (linkedListBehavior) {
throw new IllegalStateException("Cannot change direction of a link within a chain");
// BratAjaxCasUtil.setFeature(aTargetFs, aFeature, aValue);
} else {
// in set mode there are no arc labels anyway
}
} else // if origin and target are not adjacent
{
FeatureStructure originChain = getChainForLink(aJCas, aOriginFs);
FeatureStructure targetChain = getChainForLink(aJCas, aTargetFs);
AnnotationFS targetPrev = getPrevLink(targetChain, aTargetFs);
if (!WebAnnoCasUtil.isSame(originChain, targetChain)) {
if (linkedListBehavior) {
// the rest becomes its own chain
if (originNext != null) {
newChain(aJCas, 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
{
aJCas.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
aJCas.removeFsFromIndexes(targetChain);
}
} else {
// if the two links are in the same chain, we just ignore the action
if (linkedListBehavior) {
throw new IllegalStateException("Cannot connect two spans that are already part of the same chain");
}
}
}
// We do not actually create a new FS for the arc. Features are set on the originFS.
return WebAnnoCasUtil.getAddr(aOriginFs);
}
Aggregations