use of de.tudarmstadt.ukp.clarin.webanno.api.annotation.adapter.ArcAdapter in project webanno by webanno.
the class AnnotationDetailEditorPanel method actionReverse.
@Override
public void actionReverse(AjaxRequestTarget aTarget) throws IOException, AnnotationException {
aTarget.addChildren(getPage(), IFeedback.class);
JCas jCas = getEditorCas();
AnnotatorState state = getModelObject();
AnnotationFS idFs = selectByAddr(jCas, state.getSelection().getAnnotation().getId());
jCas.removeFsFromIndexes(idFs);
AnnotationFS originFs = selectByAddr(jCas, state.getSelection().getOrigin());
AnnotationFS targetFs = selectByAddr(jCas, state.getSelection().getTarget());
List<FeatureState> featureStates = getModelObject().getFeatureStates();
TypeAdapter adapter = annotationService.getAdapter(state.getSelectedAnnotationLayer());
if (adapter instanceof ArcAdapter) {
// If no features, still create arc #256
AnnotationFS arc = ((ArcAdapter) adapter).add(targetFs, originFs, jCas, state.getWindowBeginOffset(), state.getWindowEndOffset());
state.getSelection().setAnnotation(new VID(getAddr(arc)));
for (FeatureState featureState : featureStates) {
adapter.setFeatureValue(state, jCas, getAddr(arc), featureState.feature, featureState.value);
}
} else {
error("chains cannot be reversed");
return;
}
// persist changes
writeEditorCas(jCas);
int sentenceNumber = getSentenceNumber(jCas, originFs.getBegin());
state.setFocusUnitIndex(sentenceNumber);
state.getDocument().setSentenceAccessed(sentenceNumber);
if (state.getPreferences().isScrollPage()) {
autoScroll(jCas, false);
}
info("The arc has been reversed");
state.rememberFeatures();
// in case the user re-reverse it
state.getSelection().reverseArc();
onChange(aTarget);
}
use of de.tudarmstadt.ukp.clarin.webanno.api.annotation.adapter.ArcAdapter in project webanno by webanno.
the class AnnotationDetailEditorPanel method getAttachedRels.
public Set<AnnotationFS> getAttachedRels(AnnotationFS aFs, AnnotationLayer aLayer) {
CAS cas = aFs.getCAS();
Set<AnnotationFS> toBeDeleted = new HashSet<>();
for (AnnotationLayer relationLayer : annotationService.listAttachedRelationLayers(aLayer)) {
ArcAdapter relationAdapter = (ArcAdapter) annotationService.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;
}
use of de.tudarmstadt.ukp.clarin.webanno.api.annotation.adapter.ArcAdapter 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 de.tudarmstadt.ukp.clarin.webanno.api.annotation.adapter.ArcAdapter in project webanno by webanno.
the class AutomationUtil method repeateRelationAnnotation.
public static void repeateRelationAnnotation(AnnotatorState aState, DocumentService aDocumentService, CorrectionDocumentService aCorrectionDocumentService, AnnotationSchemaService aAnnotationService, AnnotationFS fs, AnnotationFeature aFeature, String aValue) throws UIMAException, ClassNotFoundException, IOException, AnnotationException {
for (SourceDocument d : aDocumentService.listSourceDocuments(aState.getProject())) {
loadDocument(d, aAnnotationService, aDocumentService, aCorrectionDocumentService, aState.getUser());
JCas jCas = aCorrectionDocumentService.readCorrectionCas(d);
ArcAdapter adapter = (ArcAdapter) aAnnotationService.getAdapter(aFeature.getLayer());
String sourceFName = adapter.getSourceFeatureName();
String targetFName = adapter.getTargetFeatureName();
Type type = getType(jCas.getCas(), aFeature.getLayer().getName());
Type spanType = getType(jCas.getCas(), adapter.getAttachTypeName());
Feature arcSpanFeature = spanType.getFeatureByBaseName(adapter.getAttachFeatureName());
Feature dependentFeature = type.getFeatureByBaseName(targetFName);
Feature governorFeature = type.getFeatureByBaseName(sourceFName);
AnnotationFS dependentFs = null;
AnnotationFS governorFs = null;
if (adapter.getAttachFeatureName() != null) {
dependentFs = (AnnotationFS) fs.getFeatureValue(dependentFeature).getFeatureValue(arcSpanFeature);
governorFs = (AnnotationFS) fs.getFeatureValue(governorFeature).getFeatureValue(arcSpanFeature);
} else {
dependentFs = (AnnotationFS) fs.getFeatureValue(dependentFeature);
governorFs = (AnnotationFS) fs.getFeatureValue(governorFeature);
}
if (adapter.isCrossMultipleSentence()) {
List<AnnotationFS> mSpanAnnos = new ArrayList<>(getAllAnnoFss(jCas, governorFs.getType()));
repeatRelation(aState, 0, jCas.getDocumentText().length() - 1, aFeature, aValue, jCas, adapter, dependentFs, governorFs, mSpanAnnos);
} else {
for (Sentence sent : select(jCas, Sentence.class)) {
List<AnnotationFS> spanAnnos = selectCovered(governorFs.getType(), sent);
repeatRelation(aState, sent.getBegin(), sent.getEnd(), aFeature, aValue, jCas, adapter, dependentFs, governorFs, spanAnnos);
}
}
aCorrectionDocumentService.writeCorrectionCas(jCas, d);
}
}
use of de.tudarmstadt.ukp.clarin.webanno.api.annotation.adapter.ArcAdapter in project webanno by webanno.
the class RelationRenderer method getRelationLinks.
/**
* Get relation links to display in relation yield
*/
private Map<Integer, Set<Integer>> getRelationLinks(JCas aJcas, int aWindowBegin, int aWindowEnd, Type type, Feature dependentFeature, Feature governorFeature, Feature arcSpanFeature) {
ArcAdapter typeAdapter = getTypeAdapter();
FeatureStructure dependentFs;
FeatureStructure governorFs;
Map<Integer, Set<Integer>> relations = new ConcurrentHashMap<>();
for (AnnotationFS fs : selectCovered(aJcas.getCas(), 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);
}
if (dependentFs == null || governorFs == null) {
log.warn("Relation [" + typeAdapter.getLayer().getName() + "] with id [" + getAddr(fs) + "] has loose ends - cannot render.");
continue;
}
Set<Integer> links = relations.get(getAddr(governorFs));
if (links == null) {
links = new ConcurrentSkipListSet<>();
}
links.add(getAddr(dependentFs));
relations.put(getAddr(governorFs), links);
}
// Update other subsequent links
for (int i = 0; i < relations.keySet().size(); i++) {
for (Integer fs : relations.keySet()) {
updateLinks(relations, fs);
}
}
// to start displaying the text from the governor, include it
for (Integer fs : relations.keySet()) {
relations.get(fs).add(fs);
}
return relations;
}
Aggregations