Search in sources :

Example 81 with IParser

use of org.hl7.fhir.r5.formats.IParser in project synthea by synthetichealth.

the class ValidationSupportR4 method loadFromDirectory.

/**
 * Loads the structure definitions from the given directory.
 * @param rootDir the directory to load structure definitions from
 * @return a list of structure definitions
 * @throws Throwable when there is an error reading the structure definitions.
 */
private void loadFromDirectory(String rootDir) throws Throwable {
    IParser jsonParser = FhirR4.getContext().newJsonParser();
    jsonParser.setParserErrorHandler(new StrictErrorHandler());
    URL profilesFolder = ClassLoader.getSystemClassLoader().getResource(rootDir);
    Path path = Paths.get(profilesFolder.toURI());
    Files.walk(path, Integer.MAX_VALUE).filter(Files::isReadable).filter(Files::isRegularFile).filter(p -> p.toString().endsWith(".json")).forEach(f -> {
        try {
            IBaseResource resource = jsonParser.parseResource(new FileReader(f.toFile()));
            handleResource(resource);
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        }
    });
}
Also used : Path(java.nio.file.Path) CodeSystem(org.hl7.fhir.r4.model.CodeSystem) ValueSetExpansionContainsComponent(org.hl7.fhir.r4.model.ValueSet.ValueSetExpansionContainsComponent) URL(java.net.URL) ValueSet(org.hl7.fhir.r4.model.ValueSet) ConceptValidationOptions(ca.uhn.fhir.context.support.ConceptValidationOptions) FhirContext(ca.uhn.fhir.context.FhirContext) ValidationSupportContext(ca.uhn.fhir.context.support.ValidationSupportContext) PrePopulatedValidationSupport(org.hl7.fhir.common.hapi.validation.support.PrePopulatedValidationSupport) IBaseResource(org.hl7.fhir.instance.model.api.IBaseResource) BundleEntryComponent(org.hl7.fhir.r4.model.Bundle.BundleEntryComponent) ValueSetExpansionComponent(org.hl7.fhir.r4.model.ValueSet.ValueSetExpansionComponent) Path(java.nio.file.Path) Nonnull(javax.annotation.Nonnull) IParser(ca.uhn.fhir.parser.IParser) ConceptReferenceComponent(org.hl7.fhir.r4.model.ValueSet.ConceptReferenceComponent) Files(java.nio.file.Files) StructureDefinition(org.hl7.fhir.r4.model.StructureDefinition) StrictErrorHandler(ca.uhn.fhir.parser.StrictErrorHandler) FileNotFoundException(java.io.FileNotFoundException) ConceptSetComponent(org.hl7.fhir.r4.model.ValueSet.ConceptSetComponent) Paths(java.nio.file.Paths) Bundle(org.hl7.fhir.r4.model.Bundle) ConceptDefinitionComponent(org.hl7.fhir.r4.model.CodeSystem.ConceptDefinitionComponent) FileReader(java.io.FileReader) ValueSetComposeComponent(org.hl7.fhir.r4.model.ValueSet.ValueSetComposeComponent) StrictErrorHandler(ca.uhn.fhir.parser.StrictErrorHandler) FileNotFoundException(java.io.FileNotFoundException) FileReader(java.io.FileReader) Files(java.nio.file.Files) IBaseResource(org.hl7.fhir.instance.model.api.IBaseResource) URL(java.net.URL) IParser(ca.uhn.fhir.parser.IParser)

Example 82 with IParser

use of org.hl7.fhir.r5.formats.IParser in project synthea by synthetichealth.

the class ValidationSupportSTU3 method loadFromDirectory.

/**
 * Loads the structure definitions from the given directory.
 * @param rootDir the directory to load structure definitions from
 * @return a list of structure definitions
 * @throws Throwable when there is an error reading the structure definitions.
 */
private void loadFromDirectory(String rootDir) throws Throwable {
    IParser jsonParser = FhirStu3.getContext().newJsonParser();
    jsonParser.setParserErrorHandler(new StrictErrorHandler());
    URL profilesFolder = ClassLoader.getSystemClassLoader().getResource(rootDir);
    Path path = Paths.get(profilesFolder.toURI());
    Files.walk(path, Integer.MAX_VALUE).filter(Files::isReadable).filter(Files::isRegularFile).filter(p -> p.toString().endsWith(".json")).forEach(f -> {
        try {
            IBaseResource resource = jsonParser.parseResource(new FileReader(f.toFile()));
            if (resource instanceof CodeSystem) {
                this.addCodeSystem(resource);
            } else if (resource instanceof ValueSet) {
            // The PrePopulatedValidationSupport base class only supports
            // R4 ValueSets so we don't validate these here
            // ValueSet vs = (ValueSet) resource;
            // this.addValueSet(vs);
            } else if (resource instanceof StructureDefinition) {
                this.addStructureDefinition(resource);
            }
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        }
    });
}
Also used : Path(java.nio.file.Path) Files(java.nio.file.Files) URL(java.net.URL) StrictErrorHandler(ca.uhn.fhir.parser.StrictErrorHandler) CodeSystem(org.hl7.fhir.dstu3.model.CodeSystem) FileNotFoundException(java.io.FileNotFoundException) FhirContext(ca.uhn.fhir.context.FhirContext) Paths(java.nio.file.Paths) PrePopulatedValidationSupport(org.hl7.fhir.common.hapi.validation.support.PrePopulatedValidationSupport) IBaseResource(org.hl7.fhir.instance.model.api.IBaseResource) StructureDefinition(org.hl7.fhir.dstu3.model.StructureDefinition) FileReader(java.io.FileReader) Path(java.nio.file.Path) IParser(ca.uhn.fhir.parser.IParser) ValueSet(org.hl7.fhir.dstu3.model.ValueSet) FileNotFoundException(java.io.FileNotFoundException) CodeSystem(org.hl7.fhir.dstu3.model.CodeSystem) URL(java.net.URL) StructureDefinition(org.hl7.fhir.dstu3.model.StructureDefinition) StrictErrorHandler(ca.uhn.fhir.parser.StrictErrorHandler) FileReader(java.io.FileReader) Files(java.nio.file.Files) IBaseResource(org.hl7.fhir.instance.model.api.IBaseResource) ValueSet(org.hl7.fhir.dstu3.model.ValueSet) IParser(ca.uhn.fhir.parser.IParser)

Example 83 with IParser

use of org.hl7.fhir.r5.formats.IParser in project synthea by synthetichealth.

the class FhirGroupExporterR4 method exportAndSave.

/**
 * Export the current patient list as a FHIR Group resource and save it as a JSON file.
 * @param stop The stop time.
 */
public static void exportAndSave(RandomNumberGenerator rand, long stop) {
    if (Config.getAsBoolean("exporter.groups.fhir.export")) {
        Group group = export(rand, stop);
        // get output folder
        List<String> folders = new ArrayList<>();
        folders.add("fhir");
        String baseDirectory = Config.get("exporter.baseDirectory");
        File f = Paths.get(baseDirectory, folders.toArray(new String[0])).toFile();
        f.mkdirs();
        Path outFilePath = null;
        String groupJson = null;
        if (Config.getAsBoolean("exporter.fhir.bulk_data")) {
            IParser parser = FhirR4.getContext().newJsonParser().setPrettyPrint(false);
            groupJson = parser.encodeResourceToString(group);
            String filename = group.getResourceType().toString() + ".ndjson";
            outFilePath = f.toPath().resolve(filename);
        } else {
            IParser parser = FhirR4.getContext().newJsonParser().setPrettyPrint(true);
            groupJson = parser.encodeResourceToString(group);
            outFilePath = f.toPath().resolve("groupInformation" + stop + ".json");
        }
        try {
            Files.write(outFilePath, Collections.singleton(groupJson), StandardOpenOption.CREATE_NEW);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
Also used : Path(java.nio.file.Path) Group(org.hl7.fhir.r4.model.Group) ArrayList(java.util.ArrayList) IOException(java.io.IOException) File(java.io.File) IParser(ca.uhn.fhir.parser.IParser)

Example 84 with IParser

use of org.hl7.fhir.r5.formats.IParser in project clinical_quality_language by cqframework.

the class DataRequirementsProcessorTest method TestDataRequirementsProcessor.

@Test
public void TestDataRequirementsProcessor() {
    CqlTranslatorOptions cqlTranslatorOptions = new CqlTranslatorOptions();
    cqlTranslatorOptions.getFormats().add(CqlTranslator.Format.JSON);
    cqlTranslatorOptions.getOptions().add(CqlTranslatorOptions.Options.EnableAnnotations);
    try {
        /*
                OpioidCDSCommon.cql
                DataRequirements.cql
                AdultOutpatientEncountersFHIR4.xml
                AdvancedIllnessandFrailtyExclusionECQMFHIR4.xml
                BCSComponent.xml
                CCSComponent.xml
                FHIRHelpers.xml
                HBPComponent.xml
                HospiceFHIR4.xml
                MATGlobalCommonFunctionsFHIR4.xml
                PVSComponent.xml
                SupplementalDataElementsFHIR4.xml
                TSCComponent.xml
                BCSComponent-v0-0-001-FHIR-4-0-1.xml
                CCSComponent-v0-0-001-FHIR-4-0-1.xml
                HBPComponent-v0-0-001-FHIR-4-0-1.xml
                PVSComponent-v0-0-001-FHIR-4-0-1.xml
                TSCComponent-v0-0-001-FHIR-4-0-1.xml
                PreventiveCareandWellness-v0-0-001-FHIR-4-0-1.xml
             */
        // "OpioidCDS/cql/OpioidCDSCommon.cql", cqlTranslatorOptions);
        CqlTranslator translator = createTranslator("CompositeMeasures/cql/EXM124-9.0.000.cql", cqlTranslatorOptions);
        translator.toELM();
        assertTrue(translator.getErrors().isEmpty());
        libraryManager.cacheLibrary(translator.getTranslatedLibrary());
        DataRequirementsProcessor dqReqTrans = new DataRequirementsProcessor();
        org.hl7.fhir.r5.model.Library moduleDefinitionLibrary = dqReqTrans.gatherDataRequirements(libraryManager, translator.getTranslatedLibrary(), cqlTranslatorOptions, null, false);
        assertTrue(moduleDefinitionLibrary.getType().getCode("http://terminology.hl7.org/CodeSystem/library-type").equalsIgnoreCase("module-definition"));
        FhirContext context = getFhirContext();
        IParser parser = context.newJsonParser();
        String moduleDefString = parser.setPrettyPrint(true).encodeResourceToString(moduleDefinitionLibrary);
        logger.debug(moduleDefString);
    } catch (IOException ioException) {
        ioException.printStackTrace();
    }
}
Also used : FhirContext(ca.uhn.fhir.context.FhirContext) org.hl7.fhir.r5.model(org.hl7.fhir.r5.model) IOException(java.io.IOException) IParser(ca.uhn.fhir.parser.IParser) Test(org.testng.annotations.Test)

Example 85 with IParser

use of org.hl7.fhir.r5.formats.IParser in project clinical_quality_language by cqframework.

the class DataRequirementsProcessorTest method assertEqualToExpectedModuleDefinitionLibrary.

private void assertEqualToExpectedModuleDefinitionLibrary(org.hl7.fhir.r5.model.Library actualModuleDefinitionLibrary, String pathToExpectedModuleDefinitionLibrary) {
    FhirContext context = getFhirContext();
    IParser parser = context.newJsonParser();
    org.hl7.fhir.r5.model.Library expectedModuleDefinitionLibrary = (org.hl7.fhir.r5.model.Library) parser.parseResource(DataRequirementsProcessorTest.class.getResourceAsStream(pathToExpectedModuleDefinitionLibrary));
    assertNotNull(expectedModuleDefinitionLibrary);
    actualModuleDefinitionLibrary.setDate(null);
    expectedModuleDefinitionLibrary.setDate(null);
    assertTrue(actualModuleDefinitionLibrary.equalsDeep(expectedModuleDefinitionLibrary));
}
Also used : FhirContext(ca.uhn.fhir.context.FhirContext) org.hl7.fhir.r5.model(org.hl7.fhir.r5.model) IParser(ca.uhn.fhir.parser.IParser)

Aggregations

IParser (ca.uhn.fhir.parser.IParser)89 FhirContext (ca.uhn.fhir.context.FhirContext)43 IOException (java.io.IOException)35 ByteArrayOutputStream (java.io.ByteArrayOutputStream)30 Test (org.junit.Test)24 InputStream (java.io.InputStream)22 IParser (org.hl7.fhir.r5.formats.IParser)19 JsonParser (org.hl7.fhir.r5.formats.JsonParser)18 Test (org.junit.jupiter.api.Test)18 FHIRException (org.hl7.fhir.exceptions.FHIRException)17 ByteArrayInputStream (java.io.ByteArrayInputStream)16 File (java.io.File)16 FileInputStream (java.io.FileInputStream)16 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)15 Bundle (org.hl7.fhir.r4.model.Bundle)14 FileOutputStream (java.io.FileOutputStream)12 Bundle (org.hl7.fhir.dstu3.model.Bundle)12 IBaseResource (org.hl7.fhir.instance.model.api.IBaseResource)12 XmlParser (org.hl7.fhir.r5.formats.XmlParser)12 ArrayList (java.util.ArrayList)11