@Override public ChainAdapter createAdapter(AnnotationLayer aLayer) { ChainAdapter adapter = new ChainAdapter(featureSupportRegistry, eventPublisher, aLayer, schemaService.listAnnotationFeature(aLayer), layerBehaviorsRegistry.getLayerBehaviors(this, SpanLayerBehavior.class)); return adapter; }
AnnotationFS originNext = getNextLink(aOriginFs); AnnotationFS targetNext = getNextLink(aTargetFs); if (isLinkedListBehavior()) { throw new IllegalStateException("Cannot change direction of a link within a chain"); FeatureStructure originChain = getChainForLink(aJCas, aOriginFs); FeatureStructure targetChain = getChainForLink(aJCas, aTargetFs); AnnotationFS targetPrev = getPrevLink(targetChain, aTargetFs); if (isLinkedListBehavior()) { newChain(aJCas, originNext); setNextLink(targetPrev, null); setNextLink(aOriginFs, aTargetFs); links.addAll(collectLinks(originChain)); links.addAll(collectLinks(targetChain)); setNextLink(prev, link); setNextLink(links.get(links.size() - 1), null); setFirstLink(originChain, links.get(0)); if (isLinkedListBehavior()) {
public AnnotationFS handle(CreateSpanAnnotationRequest aRequest) throws AnnotationException { CreateSpanAnnotationRequest request = aRequest; for (SpanLayerBehavior behavior : behaviors) { request = behavior.onCreate(this, request); } return createChainElementAnnotation(request); }
private List<AnnotationFS> collectLinks(FeatureStructure aChain) { List<AnnotationFS> links = new ArrayList<>(); // Now we seek the link within the current chain AnnotationFS linkFs = (AnnotationFS) aChain.getFeatureValue(aChain.getType() .getFeatureByBaseName(getChainFirstFeatureName())); while (linkFs != null) { links.add(linkFs); linkFs = getNextLink(linkFs); } return links; }
@Override public void delete(SourceDocument aDocument, String aUsername, JCas aJCas, VID aVid) { if (aVid.getSubId() == VID.NONE) { deleteSpan(aJCas, aVid.getId()); } else { deleteArc(aJCas, aVid.getId()); } }
Type chainType = CasUtil.getType(aJcas.getCas(), typeAdapter.getChainTypeName()); Feature chainFirst = chainType.getFeatureByBaseName(typeAdapter.getChainFirstFeatureName()); .getFeatureByBaseName(typeAdapter.getLinkNextFeatureName()); AnnotationFS nextLinkFs = (AnnotationFS) linkFs.getFeatureValue(linkNext); linkFs.getEnd() - windowBeginOffset); VSpan span = new VSpan(typeAdapter.getLayer(), linkFs, bratTypeName, offsets, colorIndex, singletonMap("label", bratLabelText), singletonMap("label", bratHoverText)); String bratLabelText = null; if (typeAdapter.isLinkedListBehavior() && arcLabelFeature != null) { aResponse.add(new VArc(typeAdapter.getLayer(), new VID(prevLinkFs, 1, VID.NONE, VID.NONE), bratTypeName, prevLinkFs, linkFs, colorIndex, singletonMap("label", bratLabelText)));
@Test public void thatSpanStackingBehaviorOnCreateThrowsException() throws AnnotationException { TokenBuilder<Token, Sentence> builder = new TokenBuilder<>(Token.class, Sentence.class); builder.buildTokens(jcas, "This is a test ."); ChainAdapter sut = new ChainAdapter(featureSupportRegistry, null, corefLayer, asList(), behaviors); // First time should work sut.addSpan(document, username, jcas, 0, 1); // Second time not assertThatExceptionOfType(AnnotationException.class) .isThrownBy(() -> sut.addSpan(document, username, jcas, 0, 1)) .withMessageContaining("stacking is not enabled"); }
/** * Create a new chain head feature structure. Already adds the chain to the CAS. */ private FeatureStructure newChain(JCas aJCas, AnnotationFS aFirstLink) { Type chainType = CasUtil.getType(aJCas.getCas(), getChainTypeName()); FeatureStructure newChain = aJCas.getCas().createFS(chainType); newChain.setFeatureValue(chainType.getFeatureByBaseName(getChainFirstFeatureName()), aFirstLink); aJCas.addFsToIndexes(newChain); return newChain; }
private void createNewChainElement(AjaxRequestTarget aTarget, ChainAdapter aAdapter, JCas aJCas) throws AnnotationException { AnnotatorState state = getModelObject(); Selection selection = state.getSelection(); AnnotationFS annoFs = aAdapter.addSpan(state.getDocument(), state.getUser().getUsername(), aJCas, selection.getBegin(), selection.getEnd()); selection.selectSpan(new VID(annoFs), aJCas, annoFs.getBegin(), annoFs.getEnd()); }
/** * Get the first link of a chain from the chain head feature structure. */ private AnnotationFS getFirstLink(FeatureStructure aChain) { return (AnnotationFS) aChain.getFeatureValue(aChain.getType().getFeatureByBaseName( getChainFirstFeatureName())); }
/** * Create a new link annotation. Already adds the chain to the CAS. */ private AnnotationFS newLink(JCas aJCas, int aBegin, int aEnd) { Type linkType = CasUtil.getType(aJCas.getCas(), getAnnotationTypeName()); AnnotationFS newLink = aJCas.getCas().createAnnotation(linkType, aBegin, aEnd); aJCas.getCas().addFsToIndexes(newLink); return newLink; }
private void createNewChainLinkAnnotation(ChainAdapter aAdapter, JCas aJCas) { LOG.trace("createNewChainLinkAnnotation()"); AnnotatorState state = getModelObject(); Selection selection = state.getSelection(); AnnotationFS originFs = selectByAddr(aJCas, selection.getOrigin()); AnnotationFS targetFs = selectByAddr(aJCas, selection.getTarget()); // Creating a new chain link int addr = aAdapter.addArc(state.getDocument(), state.getUser().getUsername(), aJCas, originFs, targetFs); selection.selectArc(new VID(addr), originFs, targetFs); }
Type chainType = CasUtil.getType(aJcas.getCas(), typeAdapter.getChainTypeName()); Feature chainFirst = chainType.getFeatureByBaseName(typeAdapter.getChainFirstFeatureName()); .getFeatureByBaseName(typeAdapter.getLinkNextFeatureName()); AnnotationFS nextLinkFs = (AnnotationFS) linkFs.getFeatureValue(linkNext); linkFs.getEnd() - windowBeginOffset); VSpan span = new VSpan(typeAdapter.getLayer(), linkFs, bratTypeName, offsets, colorIndex, singletonMap("label", bratLabelText), singletonMap("label", bratHoverText)); String bratLabelText = null; if (typeAdapter.isLinkedListBehavior() && arcLabelFeature != null) { aResponse.add(new VArc(typeAdapter.getLayer(), new VID(prevLinkFs, 1, VID.NONE, VID.NONE), bratTypeName, prevLinkFs, linkFs, colorIndex, singletonMap("label", bratLabelText)));
@Test public void thatSpanAnchoringAndStackingBehaviorsWorkInConcert() throws AnnotationException { TokenBuilder<Token, Sentence> builder = new TokenBuilder<>(Token.class, Sentence.class); builder.buildTokens(jcas, "This is a test ."); ChainAdapter sut = new ChainAdapter(featureSupportRegistry, null, corefLayer, asList(), behaviors); // First time should work - we annotate the whole word "This" sut.addSpan(document, username, jcas, 0, 4); // Second time not - here we annotate "T" but it should be expanded to "This" assertThatExceptionOfType(AnnotationException.class) .isThrownBy(() -> sut.addSpan(document, username, jcas, 0, 1)) .withMessageContaining("stacking is not enabled"); } }
@Override public void delete(SourceDocument aDocument, String aUsername, JCas aJCas, VID aVid) { if (aVid.getSubId() == VID.NONE) { deleteSpan(aJCas, aVid.getId()); } else { deleteArc(aJCas, aVid.getId()); } }
/** * Create a new chain head feature structure. Already adds the chain to the CAS. */ private FeatureStructure newChain(JCas aJCas, AnnotationFS aFirstLink) { Type chainType = CasUtil.getType(aJCas.getCas(), getChainTypeName()); FeatureStructure newChain = aJCas.getCas().createFS(chainType); newChain.setFeatureValue(chainType.getFeatureByBaseName(getChainFirstFeatureName()), aFirstLink); aJCas.addFsToIndexes(newChain); return newChain; }
private List<AnnotationFS> collectLinks(FeatureStructure aChain) { List<AnnotationFS> links = new ArrayList<>(); // Now we seek the link within the current chain AnnotationFS linkFs = (AnnotationFS) aChain.getFeatureValue(aChain.getType() .getFeatureByBaseName(getChainFirstFeatureName())); while (linkFs != null) { links.add(linkFs); linkFs = getNextLink(linkFs); } return links; }
/** * Set the first link of a chain in the chain head feature structure. */ private void setFirstLink(FeatureStructure aChain, AnnotationFS aLink) { aChain.setFeatureValue(aChain.getType().getFeatureByBaseName(getChainFirstFeatureName()), aLink); }
/** * Create a new link annotation. Already adds the chain to the CAS. */ private AnnotationFS newLink(JCas aJCas, int aBegin, int aEnd) { Type linkType = CasUtil.getType(aJCas.getCas(), getAnnotationTypeName()); AnnotationFS newLink = aJCas.getCas().createAnnotation(linkType, aBegin, aEnd); aJCas.getCas().addFsToIndexes(newLink); return newLink; }
AnnotationFS originNext = getNextLink(aOriginFs); AnnotationFS targetNext = getNextLink(aTargetFs); if (isLinkedListBehavior()) { throw new IllegalStateException("Cannot change direction of a link within a chain"); FeatureStructure originChain = getChainForLink(aJCas, aOriginFs); FeatureStructure targetChain = getChainForLink(aJCas, aTargetFs); AnnotationFS targetPrev = getPrevLink(targetChain, aTargetFs); if (isLinkedListBehavior()) { newChain(aJCas, originNext); setNextLink(targetPrev, null); setNextLink(aOriginFs, aTargetFs); links.addAll(collectLinks(originChain)); links.addAll(collectLinks(targetChain)); setNextLink(prev, link); setNextLink(links.get(links.size() - 1), null); setFirstLink(originChain, links.get(0)); if (isLinkedListBehavior()) {