import corpora.DataLoader; import relations.EventExtraction; import utils.DBUtils; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.StandardCopyOption; import java.util.ArrayList; import java.util.Comparator; import java.util.List; import java.util.stream.Stream; /** Input/output driver only. All linguistic processing is in the upstream jar. */ public final class BioSemBatch { public static void main(String[] args) throws Exception { Path input = Path.of(args[0]); Path output = Path.of(args[2]); Files.createDirectories(output); DBUtils source = new DBUtils(); source.openDB(args[1]); // Invoke the original serializer, including its duplicate handling. Method write = EventExtraction.class.getDeclaredMethod("writeResult", String.class, String.class); write.setAccessible(true); EventExtraction extractor = null; List paths = new ArrayList<>(); try (Stream files = Files.list(input)) { files.filter(p -> p.toString().endsWith(".txt")).forEach(paths::add); } // Stable adapter filenames; opaque biomedical record IDs do not affect order. paths.sort(Comparator.comparing(p -> p.getFileName().toString().replace(".txt", ""))); try { for (Path path : paths) { String id = path.getFileName().toString().replace(".txt", ""); DBUtils document = null; Path result = output.resolve(id + ".a2"); try { String text = Files.readString(path, StandardCharsets.UTF_8); List proteins = Files.readAllLines(input.resolve(id + ".a1"), StandardCharsets.UTF_8); // Upstream programmatic loader preserves text and offsets, including empty text. document = new DataLoader().Txt2Db("document", text, proteins); if (extractor == null) { extractor = new EventExtraction(source, document); extractor.init(); } else { extractor.setDb(document); } extractor.extractEvents("document"); write.invoke(extractor, id, output.toString()); // Only a closed, complete upstream result is exposed to Python. Files.move(result, output.resolve(id + ".ready"), StandardCopyOption.REPLACE_EXISTING); } catch (Exception | StackOverflowError failure) { Throwable cause = failure instanceof InvocationTargetException ? ((InvocationTargetException) failure).getCause() : failure; System.err.println("BioSem record " + id + " failed; no annotations emitted: " + cause); cause.printStackTrace(System.err); Files.deleteIfExists(result); // Never reuse partially mutated extractor state after an exception. extractor = null; } finally { if (document != null) document.closeDB(); } } } finally { source.closeDB(); } } }