/** Create a new chain head feature structure. Already adds the chain to the CAS. */ private FeatureStructure newChain(JCas aJCas, AnnotationFS aFirstLink) { Type chainType = getAnnotationType(aJCas.getCas()); FeatureStructure newChain = aJCas.getCas().createFS(chainType); newChain.setFeatureValue(chainType.getFeatureByBaseName(chainFirstFeatureName), aFirstLink); aJCas.addFsToIndexes(newChain); return newChain; }
@Override public boolean match(FeatureStructure ref, FeatureStructure cand) { if (!subtypeMatch) { return ref.getType().equals(cand.getType()); } else { TypeSystem ts = ref.getCAS().getTypeSystem(); return ts.subsumes(ref.getType(), cand.getType()); } }
private List<AnnotationFS> collectLinks(FeatureStructure aChain) { List<AnnotationFS> links = new ArrayList<AnnotationFS>(); // Now we seek the link within the current chain AnnotationFS linkFs = (AnnotationFS) aChain.getFeatureValue(aChain.getType().getFeatureByBaseName(chainFirstFeatureName)); while (linkFs != null) { links.add(linkFs); linkFs = getNextLink(linkFs); } return links; }
/** 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(chainFirstFeatureName)); }
/** 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(chainFirstFeatureName), aLink); }
/** * Add annotations from the CAS, which is controlled by the window size, to the brat response * {@link GetDocumentResponse} * * @param aJcas The JCAS object containing annotations * @param aResponse A brat response containing annotations in brat protocol * @param aBratAnnotatorModel Data model for brat annotations * @param aColoringStrategy the coloring strategy to render this layer (ignored) */ @Override public void render( JCas aJcas, List<AnnotationFeature> aFeatures, GetDocumentResponse aResponse, BratAnnotatorModel aBratAnnotatorModel, ColoringStrategy aColoringStrategy) { // Get begin and end offsets of window content int windowBegin = BratAjaxCasUtil.selectByAddr( aJcas, Sentence.class, aBratAnnotatorModel.getSentenceAddress()) .getBegin(); int windowEnd = BratAjaxCasUtil.selectByAddr( aJcas, Sentence.class, BratAjaxCasUtil.getLastSentenceAddressInDisplayWindow( aJcas, aBratAnnotatorModel.getSentenceAddress(), aBratAnnotatorModel.getPreferences().getWindowSize())) .getEnd(); // Find the features for the arc and span labels - it is possible that we do not find a // feature for arc/span labels because they may have been disabled. AnnotationFeature spanLabelFeature = null; AnnotationFeature arcLabelFeature = null; for (AnnotationFeature f : aFeatures) { if (WebAnnoConst.COREFERENCE_TYPE_FEATURE.equals(f.getName())) { spanLabelFeature = f; } if (WebAnnoConst.COREFERENCE_RELATION_FEATURE.equals(f.getName())) { arcLabelFeature = f; } } // At this point arc and span feature labels must have been found! If not, the later code // will crash. Type chainType = getAnnotationType(aJcas.getCas()); Feature chainFirst = chainType.getFeatureByBaseName(chainFirstFeatureName); int colorIndex = 0; // Iterate over the chains for (FeatureStructure chainFs : selectFS(aJcas.getCas(), chainType)) { AnnotationFS linkFs = (AnnotationFS) chainFs.getFeatureValue(chainFirst); AnnotationFS prevLinkFs = null; // Every chain is supposed to have a different color String color = ColoringStrategy.PALETTE_NORMAL_FILTERED[ colorIndex % ColoringStrategy.PALETTE_NORMAL_FILTERED.length]; // The color index is updated even for chains that have no visible links in the current // window because we would like the chain color to be independent of visibility. In // particular the color of a chain should not change when switching pages/scrolling. colorIndex++; // Iterate over the links of the chain while (linkFs != null) { Feature linkNext = linkFs.getType().getFeatureByBaseName(linkNextFeatureName); AnnotationFS nextLinkFs = (AnnotationFS) linkFs.getFeatureValue(linkNext); // Is link after window? If yes, we can skip the rest of the chain if (linkFs.getBegin() >= windowEnd) { break; // Go to next chain } // Is link before window? We only need links that being within the window and that // end within the window if (!(linkFs.getBegin() >= windowBegin) && (linkFs.getEnd() <= windowEnd)) { // prevLinkFs remains null until we enter the window linkFs = nextLinkFs; continue; // Go to next link } String bratTypeName = TypeUtil.getBratTypeName(this); // Render span { String bratLabelText = TypeUtil.getBratLabelText( this, linkFs, (spanLabelFeature != null) ? asList(spanLabelFeature) : Collections.EMPTY_LIST); Offsets offsets = new Offsets(linkFs.getBegin() - windowBegin, linkFs.getEnd() - windowBegin); aResponse.addEntity( new Entity( BratAjaxCasUtil.getAddr(linkFs), bratTypeName, offsets, bratLabelText, color)); } // Render arc (we do this on prevLinkFs because then we easily know that the current // and last link are within the window ;) if (prevLinkFs != null) { String bratLabelText = null; if (linkedListBehavior && arcLabelFeature != null) { // Render arc label bratLabelText = TypeUtil.getBratLabelText(this, prevLinkFs, asList(arcLabelFeature)); } else { // Render only chain type bratLabelText = TypeUtil.getBratLabelText(this, prevLinkFs, Collections.EMPTY_LIST); } List<Argument> argumentList = asList( new Argument("Arg1", BratAjaxCasUtil.getAddr(prevLinkFs)), new Argument("Arg2", BratAjaxCasUtil.getAddr(linkFs))); aResponse.addRelation( new Relation( BratAjaxCasUtil.getAddr(prevLinkFs), bratTypeName, argumentList, bratLabelText, color)); } // if (BratAjaxCasUtil.isSame(linkFs, nextLinkFs)) { // log.error("Loop in CAS detected, aborting rendering of chains"); // break; // } prevLinkFs = linkFs; linkFs = nextLinkFs; } } }
/** * Called when the processing of a Document is completed. <br> * The process status can be looked at and corresponding actions taken. * * @param aCas CAS corresponding to the completed processing * @param aStatus EntityProcessStatus that holds the status of all the events for aEntity */ public void entityProcessComplete(CAS aCas, EntityProcessStatus aStatus) { if (aStatus != null) { if (aStatus.isException()) { System.err.println("Error on process CAS call to remote service:"); List exceptions = aStatus.getExceptions(); for (int i = 0; i < exceptions.size(); i++) { ((Throwable) exceptions.get(i)).printStackTrace(); } if (!ignoreErrors) { System.err.println("Terminating Client..."); stop(); } } if (logCas) { String ip = "no IP"; List eList = aStatus.getProcessTrace().getEventsByComponentName("UimaEE", false); for (int e = 0; e < eList.size(); e++) { ProcessTraceEvent event = (ProcessTraceEvent) eList.get(e); if (event.getDescription().equals("Service IP")) { ip = event.getResultMessage(); } } String casId = ((UimaASProcessStatus) aStatus).getCasReferenceId(); if (casId != null) { long current = System.nanoTime() / 1000000 - mStartTime; if (casMap.containsKey(casId)) { Object value = casMap.get(casId); if (value != null && value instanceof Long) { long start = ((Long) value).longValue(); System.out.println(ip + "\t" + start + "\t" + (current - start)); } } } } else { System.out.print("."); if (0 == (entityCount + 1) % 50) { System.out.print((entityCount + 1) + " processed\n"); } } } // if output dir specified, dump CAS to XMI if (outputDir != null) { // try to retrieve the filename of the input file from the CAS File outFile = null; Type srcDocInfoType = aCas.getTypeSystem().getType("org.apache.uima.examples.SourceDocumentInformation"); if (srcDocInfoType != null) { FSIterator it = aCas.getIndexRepository().getAllIndexedFS(srcDocInfoType); if (it.hasNext()) { FeatureStructure srcDocInfoFs = it.get(); Feature uriFeat = srcDocInfoType.getFeatureByBaseName("uri"); Feature offsetInSourceFeat = srcDocInfoType.getFeatureByBaseName("offsetInSource"); String uri = srcDocInfoFs.getStringValue(uriFeat); int offsetInSource = srcDocInfoFs.getIntValue(offsetInSourceFeat); File inFile; try { inFile = new File(new URL(uri).getPath()); String outFileName = inFile.getName(); if (offsetInSource > 0) { outFileName += ("_" + offsetInSource); } outFileName += ".xmi"; outFile = new File((String) outputDir, outFileName); } catch (MalformedURLException e1) { // invalid URI, use default processing below } } } if (outFile == null) { outFile = new File((String) outputDir, "doc" + entityCount); } try { FileOutputStream outStream = new FileOutputStream(outFile); try { XmiCasSerializer.serialize(aCas, outStream); } finally { outStream.close(); } } catch (Exception e) { System.err.println("Could not save CAS to XMI file"); e.printStackTrace(); } } // update stats entityCount++; String docText = aCas.getDocumentText(); if (docText != null) { size += docText.length(); } // Called just before sendCas with next CAS from collection reader }