From 982eef9d226d039b22114fb467a4650a618aebe8 Mon Sep 17 00:00:00 2001 From: Dylan Hall Date: Mon, 30 Aug 2021 17:08:16 -0400 Subject: [PATCH] custom exporter concept --- build.gradle | 2 ++ lib/custom/.keep | 0 .../org/mitre/synthea/engine/Generator.java | 1 + .../org/mitre/synthea/export/Exporter.java | 36 +++++++++++++++++++ .../mitre/synthea/export/PatientExporter.java | 19 ++++++++++ .../export/PostCompletionExporter.java | 17 +++++++++ src/main/resources/synthea.properties | 3 ++ 7 files changed, 78 insertions(+) create mode 100644 lib/custom/.keep create mode 100644 src/main/java/org/mitre/synthea/export/PatientExporter.java create mode 100644 src/main/java/org/mitre/synthea/export/PostCompletionExporter.java diff --git a/build.gradle b/build.gradle index 0079a6ae0b..fd457d0fe5 100644 --- a/build.gradle +++ b/build.gradle @@ -113,6 +113,8 @@ dependencies { // JfreeChart for drawing physiology charts implementation 'org.jfree:jfreechart:1.5.3' + implementation fileTree(dir: 'lib/custom', include: '*.jar') + // Use JUnit test framework testImplementation('junit:junit') { version { diff --git a/lib/custom/.keep b/lib/custom/.keep new file mode 100644 index 0000000000..e69de29bb2 diff --git a/src/main/java/org/mitre/synthea/engine/Generator.java b/src/main/java/org/mitre/synthea/engine/Generator.java index 3a9af8f413..013385cf21 100644 --- a/src/main/java/org/mitre/synthea/engine/Generator.java +++ b/src/main/java/org/mitre/synthea/engine/Generator.java @@ -219,6 +219,7 @@ private void init() { if (Config.getAsBoolean("exporter.cdw.export")) { CDWExporter.getInstance().setKeyStart((stateIndex * 1_000_000) + 1); } + Exporter.loadCustomExporters(); this.populationRandom = new DefaultRandomNumberGenerator(options.seed); this.clinicianRandom = new DefaultRandomNumberGenerator(options.clinicianSeed); diff --git a/src/main/java/org/mitre/synthea/export/Exporter.java b/src/main/java/org/mitre/synthea/export/Exporter.java index ca3e9f8852..b79d0ac3ea 100644 --- a/src/main/java/org/mitre/synthea/export/Exporter.java +++ b/src/main/java/org/mitre/synthea/export/Exporter.java @@ -16,6 +16,7 @@ import java.util.Iterator; import java.util.LinkedList; import java.util.List; +import java.util.ServiceLoader; import java.util.concurrent.BlockingQueue; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.LinkedBlockingQueue; @@ -57,6 +58,28 @@ public enum SupportedFhirVersion { private static final int FILE_BUFFER_SIZE = 4 * 1024 * 1024; + private static List patientExporters; + private static List postCompletionExporters; + + public static void loadCustomExporters() { + if (Config.getAsBoolean("exporter.enable_custom_exporters", false)) { + patientExporters = new LinkedList<>(); + postCompletionExporters = new LinkedList<>(); + + ServiceLoader loader = ServiceLoader.load(PatientExporter.class); + for (PatientExporter instance : loader) { + System.out.println(instance.getClass().getCanonicalName()); + patientExporters.add(instance); + } + + ServiceLoader loader2 = ServiceLoader.load(PostCompletionExporter.class); + for (PostCompletionExporter instance : loader2) { + System.out.println(instance.getClass().getCanonicalName()); + postCompletionExporters.add(instance); + } + } + } + /** * Runtime configuration of the record exporter. */ @@ -318,6 +341,13 @@ private static boolean exportRecord(Person person, String fileTag, long stopTime String consolidatedNotes = ClinicalNoteExporter.export(person); writeNewFile(outFilePath, consolidatedNotes); } + + if (patientExporters != null && !patientExporters.isEmpty()) { + for (PatientExporter patientExporter : patientExporters) { + patientExporter.export(person, stopTime, options); + } + } + if (options.isQueueEnabled()) { try { switch (options.queuedFhirVersion()) { @@ -508,6 +538,12 @@ public static void runPostCompletionExports(Generator generator, ExporterRuntime e.printStackTrace(); } } + + if (postCompletionExporters != null && !postCompletionExporters.isEmpty()) { + for (PostCompletionExporter postCompletionExporter : postCompletionExporters) { + postCompletionExporter.export(generator, options); + } + } closeOpenFiles(); } diff --git a/src/main/java/org/mitre/synthea/export/PatientExporter.java b/src/main/java/org/mitre/synthea/export/PatientExporter.java new file mode 100644 index 0000000000..ae2f7bcf01 --- /dev/null +++ b/src/main/java/org/mitre/synthea/export/PatientExporter.java @@ -0,0 +1,19 @@ +package org.mitre.synthea.export; + +import org.mitre.synthea.export.Exporter.ExporterRuntimeOptions; +import org.mitre.synthea.world.agents.Person; + +/** + * + * + */ +public interface PatientExporter { + + /** + * + * @param person Patient to export + * @param stopTime Time at which the simulation stopped + * @param options Runtime exporter options + */ + void export(Person person, long stopTime, ExporterRuntimeOptions options); +} diff --git a/src/main/java/org/mitre/synthea/export/PostCompletionExporter.java b/src/main/java/org/mitre/synthea/export/PostCompletionExporter.java new file mode 100644 index 0000000000..192e711ccf --- /dev/null +++ b/src/main/java/org/mitre/synthea/export/PostCompletionExporter.java @@ -0,0 +1,17 @@ +package org.mitre.synthea.export; + +import org.mitre.synthea.engine.Generator; +import org.mitre.synthea.export.Exporter.ExporterRuntimeOptions; + +/** + * + * + */ +public interface PostCompletionExporter { + /** + * + * @param generator + * @param options + */ + void export(Generator generator, ExporterRuntimeOptions options); +} diff --git a/src/main/resources/synthea.properties b/src/main/resources/synthea.properties index 843845d50a..310ea91336 100644 --- a/src/main/resources/synthea.properties +++ b/src/main/resources/synthea.properties @@ -93,6 +93,9 @@ exporter.symptoms.csv.append_mode = false exporter.symptoms.csv.folder_per_run = false exporter.symptoms.text.export = false +# enable searching for custom exporter implementations +exporter.enable_custom_exporters = true + # the number of patients to generate, by default # this can be overridden by passing a different value to the Generator constructor generate.default_population = 1