Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Templates and Keep modules #1403

Merged
merged 2 commits into from
Dec 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
}
} 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();

Check warning on line 205 in src/main/java/App.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/App.java#L205

Added line #L205 was not covered by tests
} 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);

Check warning on line 210 in src/main/java/App.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/App.java#L208-L210

Added lines #L208 - L210 were not covered by tests
if (Files.exists(possibleLocation)) {
options.keepPatientsModulePath = possibleLocation;

Check warning on line 212 in src/main/java/App.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/App.java#L212

Added line #L212 was not covered by tests
} else {
throw new FileNotFoundException(String.format(

Check warning on line 214 in src/main/java/App.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/App.java#L214

Added line #L214 was not covered by tests
"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 @@
});
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) {

Check warning on line 662 in src/main/java/org/mitre/synthea/helpers/Utilities.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/mitre/synthea/helpers/Utilities.java#L661-L662

Added lines #L661 - L662 were not covered by tests
// in this case we need to initialize it first:
provider.newFileSystem(uri, Collections.emptyMap());
}

Check warning on line 665 in src/main/java/org/mitre/synthea/helpers/Utilities.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/mitre/synthea/helpers/Utilities.java#L664-L665

Added lines #L664 - L665 were not covered by tests
}
}

Check warning on line 667 in src/main/java/org/mitre/synthea/helpers/Utilities.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/mitre/synthea/helpers/Utilities.java#L667

Added line #L667 was not covered by tests
}
}
}
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