Skip to content

Commit

Permalink
Merge pull request #1403 from synthetichealth/templates
Browse files Browse the repository at this point in the history
Templates and Keep modules
  • Loading branch information
eedrummer committed Dec 19, 2023
2 parents 85673df + 649eb82 commit 96f4e79
Show file tree
Hide file tree
Showing 15 changed files with 1,199 additions and 28 deletions.
19 changes: 16 additions & 3 deletions src/main/java/App.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URI;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.LinkedList;
Expand Down Expand Up @@ -195,12 +199,21 @@ public static void main(String[] args) throws Exception {
}
} else if (currArg.equals("-k")) {
String value = argsQ.poll();
// first check if it's an absolute path, or path relative to .
File keepPatientsModule = new File(value);
if (keepPatientsModule.exists()) {
options.keepPatientsModulePath = keepPatientsModule;
options.keepPatientsModulePath = keepPatientsModule.toPath();
} else {
throw new FileNotFoundException(String.format(
"Specified keep-patients file (%s) does not exist", value));
// look inside the src/main/resources/keep_modules folder
URI keepModulesURI = App.class.getClassLoader().getResource("keep_modules").toURI();
Utilities.enableReadingURIFromJar(keepModulesURI);
Path possibleLocation = Paths.get(keepModulesURI).resolve(value);
if (Files.exists(possibleLocation)) {
options.keepPatientsModulePath = possibleLocation;
} else {
throw new FileNotFoundException(String.format(
"Specified keep-patients file (%s) does not exist", value));
}
}
} else if (currArg.equals("-fm")) {
String value = argsQ.poll();
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/org/mitre/synthea/engine/Generator.java
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ public static class GeneratorOptions {
* value of -1 will evolve the population to the current system time. */
public int daysToTravelForward = -1;
/** Path to a module defining which patients should be kept and exported. */
public File keepPatientsModulePath;
public Path keepPatientsModulePath;
}

/**
Expand Down Expand Up @@ -278,8 +278,8 @@ private void init() {

if (options.keepPatientsModulePath != null) {
try {
Path path = options.keepPatientsModulePath.toPath().toAbsolutePath();
this.keepPatientsModule = Module.loadFile(path, false, null, true);
this.keepPatientsModule =
Module.loadFile(options.keepPatientsModulePath, false, null, true);
} catch (Exception e) {
throw new ExceptionInInitializerError(e);
}
Expand Down
21 changes: 1 addition & 20 deletions src/main/java/org/mitre/synthea/engine/Module.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,9 @@
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;
import java.nio.file.FileSystemNotFoundException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.spi.FileSystemProvider;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
Expand Down Expand Up @@ -102,7 +100,7 @@ private static Map<String, ModuleSupplier> loadModules() {
*/
public static Path getModulesPath() throws URISyntaxException, IOException {
URI modulesURI = Module.class.getClassLoader().getResource("modules").toURI();
fixPathFromJar(modulesURI);
Utilities.enableReadingURIFromJar(modulesURI);
return Paths.get(modulesURI);
}

Expand Down Expand Up @@ -166,23 +164,6 @@ public static void addModules(File dir) {
submoduleCount);
}

private static void fixPathFromJar(URI uri) throws IOException {
// this function is a hack to enable reading modules from within a JAR file
// see https://stackoverflow.com/a/48298758
if ("jar".equals(uri.getScheme())) {
for (FileSystemProvider provider: FileSystemProvider.installedProviders()) {
if (provider.getScheme().equalsIgnoreCase("jar")) {
try {
provider.getFileSystem(uri);
} catch (FileSystemNotFoundException e) {
// in this case we need to initialize it first:
provider.newFileSystem(uri, Collections.emptyMap());
}
}
}
}
}

/**
* Create a relative path from a folder to a file, removing the file extension and normalizing
* path segment separators.
Expand Down
29 changes: 29 additions & 0 deletions src/main/java/org/mitre/synthea/helpers/Utilities.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,20 @@
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URI;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.nio.file.FileSystemNotFoundException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.spi.FileSystemProvider;
import java.time.Instant;
import java.time.LocalDate;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Random;
Expand Down Expand Up @@ -638,4 +642,29 @@ public static Map cleanMap(Map input) {
});
return input;
}

/**
* Enable reading the given URI from within a JAR file.
* For example, for command-line args which may refer to internal or external paths.
* Note that it's not always possible to know when a user-provided path
* is within a JAR file, so this method should be called if it is possible the
* path refers to an internal location.
* @param uri URI to be accessed
*/
public static void enableReadingURIFromJar(URI uri) throws IOException {
// this function is a hack to enable reading modules from within a JAR file
// see https://stackoverflow.com/a/48298758
if ("jar".equals(uri.getScheme())) {
for (FileSystemProvider provider: FileSystemProvider.installedProviders()) {
if (provider.getScheme().equalsIgnoreCase("jar")) {
try {
provider.getFileSystem(uri);
} catch (FileSystemNotFoundException e) {
// in this case we need to initialize it first:
provider.newFileSystem(uri, Collections.emptyMap());
}
}
}
}
}
}
42 changes: 42 additions & 0 deletions src/main/resources/keep_modules/keep_diabetes.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"name": "Generated Keep Module",
"states": {
"Initial": {
"type": "Initial",
"name": "Initial",
"conditional_transition": [
{
"transition": "Keep",
"condition": {
"condition_type": "And",
"conditions": [
{
"condition_type": "Active Condition",
"codes": [
{
"system": "SNOMED-CT",
"code": "44054006",
"display": "Diabetes"
}
]
}
]
}
},
{
"transition": "Terminal"
}
]
},
"Terminal": {
"type": "Terminal",
"name": "Terminal"
},
"Keep": {
"type": "Terminal",
"name": "Keep"
}
},
"gmf_version": 2
}

28 changes: 28 additions & 0 deletions src/main/resources/keep_modules/keep_hospice.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"name": "keep_hospice",
"states": {
"Initial": {
"type": "Initial",
"conditional_transition": [
{
"transition": "Keep",
"condition": {
"condition_type": "Attribute",
"attribute": "hospice",
"operator": "is not nil"
}
},
{
"transition": "Terminal"
}
]
},
"Terminal": {
"type": "Terminal"
},
"Keep": {
"type": "Terminal"
}
},
"gmf_version": 2
}
Loading

0 comments on commit 96f4e79

Please sign in to comment.