Search in sources :

Example 1 with ValidationEngine

use of org.hl7.fhir.validation.ValidationEngine in project Gravity-SDOH-Exchange-RI by FHIR.

the class ConvertService method initValidationEngine.

@EventListener(ApplicationReadyEvent.class)
protected void initValidationEngine() throws IOException, URISyntaxException {
    String definitions = VersionUtilities.packageForVersion(PACKAGE_VERSION) + "#" + VersionUtilities.getCurrentVersion(PACKAGE_VERSION);
    this.validationEngine = new ValidationEngine(definitions, FhirPublication.R4, PACKAGE_VERSION, new TimeTracker());
    // Loading structure definitions from official package and uploading custom definitions if needed from resources
    this.validationEngine.loadPackage(SDOH_CLINICAL_CARE_PACKAGE, SDOH_CLINICAL_CARE_VERSION);
    // Loading  custom structure definitions, copying all resources from jar to local folder to be able for HAPI to
    // upload them. Just passing resources folder is not working for "in jar" files.
    loadStructureDefinitions();
}
Also used : TimeTracker(org.hl7.fhir.utilities.TimeTracker) ValidationEngine(org.hl7.fhir.validation.ValidationEngine) EventListener(org.springframework.context.event.EventListener)

Example 2 with ValidationEngine

use of org.hl7.fhir.validation.ValidationEngine in project org.hl7.fhir.core by hapifhir.

the class ValidationService method validateSources.

public ValidationResponse validateSources(ValidationRequest request) throws Exception {
    if (request.getCliContext().getSv() == null) {
        String sv = determineVersion(request.getCliContext(), request.sessionId);
        request.getCliContext().setSv(sv);
    }
    String definitions = VersionUtilities.packageForVersion(request.getCliContext().getSv()) + "#" + VersionUtilities.getCurrentVersion(request.getCliContext().getSv());
    String sessionId = initializeValidator(request.getCliContext(), definitions, new TimeTracker(), request.sessionId);
    ValidationEngine validator = sessionCache.fetchSessionValidatorEngine(sessionId);
    if (request.getCliContext().getProfiles().size() > 0) {
        System.out.println("  .. validate " + request.listSourceFiles() + " against " + request.getCliContext().getProfiles().toString());
    } else {
        System.out.println("  .. validate " + request.listSourceFiles());
    }
    ValidationResponse response = new ValidationResponse().setSessionId(sessionId);
    for (FileInfo fp : request.getFilesToValidate()) {
        List<ValidationMessage> messages = new ArrayList<>();
        validator.validate(fp.getFileContent().getBytes(), Manager.FhirFormat.getFhirFormat(fp.getFileType()), request.getCliContext().getProfiles(), messages);
        ValidationOutcome outcome = new ValidationOutcome().setFileInfo(fp);
        messages.forEach(outcome::addMessage);
        response.addOutcome(outcome);
    }
    System.out.println("  Max Memory: " + Runtime.getRuntime().maxMemory());
    return response;
}
Also used : ValidationMessage(org.hl7.fhir.utilities.validation.ValidationMessage) TimeTracker(org.hl7.fhir.utilities.TimeTracker) ArrayList(java.util.ArrayList) ValidationEngine(org.hl7.fhir.validation.ValidationEngine)

Example 3 with ValidationEngine

use of org.hl7.fhir.validation.ValidationEngine in project org.hl7.fhir.core by hapifhir.

the class ValidationService method transform.

public void transform(CliContext cliContext, ValidationEngine validator) throws Exception {
    if (cliContext.getSources().size() > 1)
        throw new Exception("Can only have one source when doing a transform (found " + cliContext.getSources() + ")");
    if (cliContext.getTxServer() == null)
        throw new Exception("Must provide a terminology server when doing a transform");
    if (cliContext.getMap() == null)
        throw new Exception("Must provide a map when doing a transform");
    try {
        List<StructureDefinition> structures = validator.getContext().allStructures();
        for (StructureDefinition sd : structures) {
            if (!sd.hasSnapshot()) {
                if (sd.getKind() != null && sd.getKind() == StructureDefinitionKind.LOGICAL) {
                    validator.getContext().generateSnapshot(sd, true);
                } else {
                    validator.getContext().generateSnapshot(sd, false);
                }
            }
        }
        validator.setMapLog(cliContext.getMapLog());
        org.hl7.fhir.r5.elementmodel.Element r = validator.transform(cliContext.getSources().get(0), cliContext.getMap());
        System.out.println(" ...success");
        if (cliContext.getOutput() != null) {
            FileOutputStream s = new FileOutputStream(cliContext.getOutput());
            if (cliContext.getOutput() != null && cliContext.getOutput().endsWith(".json"))
                new org.hl7.fhir.r5.elementmodel.JsonParser(validator.getContext()).compose(r, s, IParser.OutputStyle.PRETTY, null);
            else
                new org.hl7.fhir.r5.elementmodel.XmlParser(validator.getContext()).compose(r, s, IParser.OutputStyle.PRETTY, null);
            s.close();
        }
    } catch (Exception e) {
        System.out.println(" ...Failure: " + e.getMessage());
        e.printStackTrace();
    }
}
Also used : XmlParser(org.hl7.fhir.r5.formats.XmlParser) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) FHIRException(org.hl7.fhir.exceptions.FHIRException) JsonParser(org.hl7.fhir.r5.formats.JsonParser)

Example 4 with ValidationEngine

use of org.hl7.fhir.validation.ValidationEngine in project org.hl7.fhir.core by hapifhir.

the class ValidationService method generateSpreadsheet.

public void generateSpreadsheet(CliContext cliContext, ValidationEngine validator) throws Exception {
    CanonicalResource cr = validator.loadCanonicalResource(cliContext.getSources().get(0), cliContext.getSv());
    boolean ok = true;
    if (cr instanceof StructureDefinition) {
        new StructureDefinitionSpreadsheetGenerator(validator.getContext(), false, false).renderStructureDefinition((StructureDefinition) cr).finish(new FileOutputStream(cliContext.getOutput()));
    } else if (cr instanceof CodeSystem) {
        new CodeSystemSpreadsheetGenerator(validator.getContext()).renderCodeSystem((CodeSystem) cr).finish(new FileOutputStream(cliContext.getOutput()));
    } else if (cr instanceof ValueSet) {
        new ValueSetSpreadsheetGenerator(validator.getContext()).renderValueSet((ValueSet) cr).finish(new FileOutputStream(cliContext.getOutput()));
    } else if (cr instanceof ConceptMap) {
        new ConceptMapSpreadsheetGenerator(validator.getContext()).renderConceptMap((ConceptMap) cr).finish(new FileOutputStream(cliContext.getOutput()));
    } else {
        ok = false;
        System.out.println(" ...Unable to generate spreadsheet for " + cliContext.getSources().get(0) + ": no way to generate a spreadsheet for a " + cr.fhirType());
    }
    if (ok) {
        System.out.println(" ...generated spreadsheet successfully");
    }
}
Also used : StructureDefinitionSpreadsheetGenerator(org.hl7.fhir.r5.renderers.spreadsheets.StructureDefinitionSpreadsheetGenerator) FileOutputStream(java.io.FileOutputStream) CodeSystemSpreadsheetGenerator(org.hl7.fhir.r5.renderers.spreadsheets.CodeSystemSpreadsheetGenerator) ValueSetSpreadsheetGenerator(org.hl7.fhir.r5.renderers.spreadsheets.ValueSetSpreadsheetGenerator) ConceptMapSpreadsheetGenerator(org.hl7.fhir.r5.renderers.spreadsheets.ConceptMapSpreadsheetGenerator)

Example 5 with ValidationEngine

use of org.hl7.fhir.validation.ValidationEngine in project org.hl7.fhir.core by hapifhir.

the class ComparisonService method compareStructureDefinitions.

public static void compareStructureDefinitions(String dest, ValidationEngine validator, String left, String right, StructureDefinition resLeft, StructureDefinition resRight) throws IOException, FHIRException, EOperationOutcome {
    System.out.println("Comparing StructureDefinitions " + left + " to " + right);
    ComparisonSession session = new ComparisonSession(validator.getContext(), validator.getContext(), "Comparing Profiles", null);
    session.compare(resLeft, resRight);
    System.out.println("Generating output to " + dest + "...");
    Utilities.createDirectory(dest);
    ComparisonRenderer cr = new ComparisonRenderer(validator.getContext(), validator.getContext(), dest, session);
    cr.getTemplates().put("CodeSystem", new String(validator.getContext().getBinaries().get("template-comparison-CodeSystem.html")));
    cr.getTemplates().put("ValueSet", new String(validator.getContext().getBinaries().get("template-comparison-ValueSet.html")));
    cr.getTemplates().put("Profile", new String(validator.getContext().getBinaries().get("template-comparison-Profile.html")));
    cr.getTemplates().put("Index", new String(validator.getContext().getBinaries().get("template-comparison-index.html")));
    File htmlFile = cr.render(left, right);
    Desktop.getDesktop().browse(htmlFile.toURI());
    System.out.println("Done");
}
Also used : ComparisonSession(org.hl7.fhir.r5.comparison.ComparisonSession) ComparisonRenderer(org.hl7.fhir.r5.comparison.ComparisonRenderer) File(java.io.File)

Aggregations

ValidationEngine (org.hl7.fhir.validation.ValidationEngine)18 Test (org.junit.jupiter.api.Test)10 OperationOutcome (org.hl7.fhir.r5.model.OperationOutcome)7 CacheVerificationLogger (org.hl7.fhir.utilities.tests.CacheVerificationLogger)7 OperationOutcomeIssueComponent (org.hl7.fhir.r5.model.OperationOutcome.OperationOutcomeIssueComponent)6 IgLoader (org.hl7.fhir.validation.IgLoader)4 FileOutputStream (java.io.FileOutputStream)3 ArrayList (java.util.ArrayList)3 DisplayName (org.junit.jupiter.api.DisplayName)3 IOException (java.io.IOException)2 FHIRException (org.hl7.fhir.exceptions.FHIRException)2 StructureDefinition (org.hl7.fhir.r5.model.StructureDefinition)2 TimeTracker (org.hl7.fhir.utilities.TimeTracker)2 Test (org.junit.Test)2 File (java.io.File)1 PrintStream (java.io.PrintStream)1 MemoryMXBean (java.lang.management.MemoryMXBean)1 ComparisonRenderer (org.hl7.fhir.r5.comparison.ComparisonRenderer)1 ComparisonSession (org.hl7.fhir.r5.comparison.ComparisonSession)1 SystemOutLoggingService (org.hl7.fhir.r5.context.SystemOutLoggingService)1