BioSemEventAnnotator extends JCasAnnotator_ImplBase { @Override public void process(JCas aJCas) throws AnalysisEngineProcessException { String text = aJCas.getDocumentText(); FSIterator<Annotation> headerIt = aJCas.getAnnotationIndex(Header.type).iterator(); String docId; if (headerIt.hasNext()) { Header header = (Header) headerIt.next(); docId = header.getDocId(); } else { docId = "<unknown ID>"; } DBUtils docDb = null; try { log.debug("Processing document {}", docId); Map<String, Gene> proteins = enumerateProteins(aJCas); if (proteins.isEmpty()) { log.debug( "Skipping event extraction for this document because no proteins have been found that could be involved in an event."); return; } List<String> proteinLines = getProteinLines(proteins, docId); synchronized (lock) { try { docDb = loader.Txt2Db(docId, text, proteinLines); } catch (NullPointerException e) { log.debug( "Could not create text database for document {} due to NullPointerException during creation. Trying to close the DB and open it again after a short delay", docId); Thread.sleep(10000); try { docDb = loader.Txt2Db(docId + "-secondtry", text, proteinLines); } catch (Exception e2) { log.error("Repeatedly failed to create text database for document " + docId + ". This document will be skipped. Exception was: ", e2); throw e2; } } } if (null == xtr) { xtr = new EventExtraction(trainedDb, docDb); } else { xtr.setDb(docDb); } xtr.Test(); Set<Word> triggers = xtr.getExtractedTriggers(); Set<PData> events = xtr.getExtractedEvents(); log.debug("Got {} triggers from BioSem.", triggers.size()); log.debug("Got {} events from BioSem.", events.size()); addEventsToIndexes(events, proteins, triggers, aJCas); } catch (BioSemException e) { log.debug("BioSemException occurred: ", e); } catch (Exception e) { log.error("Error occurred in document " + docId + ":", e); throw new AnalysisEngineProcessException(e); } finally { try { if (docDb != null) { synchronized (lock) { docDb.closeDB(); } } } catch (Exception e) { log.warn( "Exception while shutting down document database for document {}. Since events have already been extracted, this is a minor error taken for itself. However it could lead to subsequent errors in the HSQL database system which could be critical.", docId); } } }  @Override void initialize(UimaContext aContext); @Override void process(JCas aJCas); @Override void destroy(); final static String RESOURCE_TRAINED_DB; }
@Test public void testProcess() throws Exception { JCas jCas = JCasFactory.createJCas("de.julielab.jcore.types.jcore-all-types"); CollectionReader bioNlpSTReader = CollectionReaderFactory.createReader(BioEventReader.class, BioEventReader.DIRECTORY_PARAM, "src/test/resources/st09-traindoc/", BioEventReader.BIOEVENT_SERVICE_MODE_PARAM, false); ExternalResourceDescription dbResourceDescription = ExternalResourceFactory.createExternalResourceDescription( DBUtilsProviderImpl.class, "file:src/test/resources/de/julielab/jcore/ae/biosemannotator.test.properties"); AnalysisEngine engine = AnalysisEngineFactory.createEngine(BioSemEventAnnotator.class, BioSemEventAnnotator.RESOURCE_TRAINED_DB, dbResourceDescription); AnalysisEngine bioNlpSTWriter = AnalysisEngineFactory.createEngine(BioEventConsumer.class, BioEventConsumer.DIRECTORY_PARAM, "src/test/resources/test-predict-out", BioEventConsumer.BIOEVENT_SERVICE_MODE_PARAM, false); File testOutputFile = new File("src/test/resources/test-predict-out/1313226.a2"); if (testOutputFile.exists()) testOutputFile.delete(); assertTrue("Test document was not found by the BioNLP ST reader.", bioNlpSTReader.hasNext()); bioNlpSTReader.getNext(jCas.getCas()); engine.process(jCas); bioNlpSTWriter.process(jCas); List<String> expectedLines = IOUtils .readLines(new FileInputStream("src/test/resources/st09-predicted/1313226.a2")); List<String> actualLines = IOUtils .readLines(new FileInputStream("src/test/resources/test-predict-out/1313226.a2")); Collections.sort(expectedLines); Collections.sort(actualLines); assertEquals(expectedLines, actualLines); }
@Test public void testStackoverflow() throws Exception { JCas jCas = JCasFactory.createJCas("de.julielab.jcore.types.jcore-all-types"); XmiCasDeserializer.deserialize(new FileInputStream("src/test/resources/test-xmi/4401723.xmi"), jCas.getCas()); ExternalResourceDescription dbResourceDescription = ExternalResourceFactory.createExternalResourceDescription( DBUtilsProviderImpl.class, "file:src/test/resources/de/julielab/jcore/ae/biosemannotator.test.properties"); AnalysisEngine engine = AnalysisEngineFactory.createEngine(BioSemEventAnnotator.class, BioSemEventAnnotator.RESOURCE_TRAINED_DB, dbResourceDescription); engine.process(jCas); }