Search in sources :

Example 11 with ValidationEngine

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

the class LoadIgTests method testPackage.

@Test
public void testPackage() {
    String id = "hl7.fhir.r4.core";
    String version = "4.0.1";
    int DO_TIMES = 3;
    try {
        final String fhirSpecVersion = "4.0";
        final String definitions = VersionUtilities.packageForVersion(fhirSpecVersion) + "#" + VersionUtilities.getCurrentVersion(fhirSpecVersion);
        ValidationEngine hl7Validator = new ValidationEngine.ValidationEngineBuilder().fromSource(definitions);
        hl7Validator.setDoNative(false);
        hl7Validator.setAnyExtensionsAllowed(true);
        hl7Validator.prepare();
        IgLoader igLoader = new IgLoader(hl7Validator.getPcm(), hl7Validator.getContext(), hl7Validator.getVersion(), true);
        for (int i = 0; i < DO_TIMES; i++) {
            System.gc();
            System.out.print("loading: allocated memory " + getUsedMemoryAsMbs() + " MB, ");
            System.out.print("free memory " + getFreeMemoryAsMbs() + " MB, ");
            System.out.println("max memory " + getTotalMemoryAsMbs() + " MB");
            // The method under test:
            igLoader.loadIg(hl7Validator.getIgs(), hl7Validator.getBinaries(), id + (version != null ? "#" + version : ""), true);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
// loadResourceByVersion
}
Also used : IgLoader(org.hl7.fhir.validation.IgLoader) ValidationEngine(org.hl7.fhir.validation.ValidationEngine) Test(org.junit.Test)

Example 12 with ValidationEngine

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

the class ValidationService method compile.

public void compile(CliContext cliContext, ValidationEngine validator) throws Exception {
    if (cliContext.getSources().size() > 0)
        throw new Exception("Cannot specify sources when compling transform (found " + cliContext.getSources() + ")");
    if (cliContext.getMap() == null)
        throw new Exception("Must provide a map when compiling a transform");
    if (cliContext.getOutput() == null)
        throw new Exception("Must provide an output name when compiling 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());
        StructureMap map = validator.compile(cliContext.getMap());
        if (map == null)
            throw new Exception("Unable to locate map " + cliContext.getMap());
        validator.handleOutput(map, cliContext.getOutput(), validator.getVersion());
        System.out.println(" ...success");
    } catch (Exception e) {
        System.out.println(" ...Failure: " + e.getMessage());
        e.printStackTrace();
    }
}
Also used : IOException(java.io.IOException) FHIRException(org.hl7.fhir.exceptions.FHIRException)

Example 13 with ValidationEngine

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

the class ValidationService method validateSources.

public void validateSources(CliContext cliContext, ValidationEngine validator) throws Exception {
    long start = System.currentTimeMillis();
    List<ValidationRecord> records = new ArrayList<>();
    Resource r = validator.validate(cliContext.getSources(), cliContext.getProfiles(), records);
    MemoryMXBean mbean = ManagementFactory.getMemoryMXBean();
    System.out.println("Done. " + validator.getContext().clock().report() + ". Memory = " + Utilities.describeSize(mbean.getHeapMemoryUsage().getUsed() + mbean.getNonHeapMemoryUsage().getUsed()));
    System.out.println();
    PrintStream dst = null;
    if (cliContext.getOutput() == null) {
        dst = System.out;
    } else {
        dst = new PrintStream(new FileOutputStream(cliContext.getOutput()));
    }
    ValidationOutputRenderer renderer = makeValidationOutputRenderer(cliContext);
    renderer.setOutput(dst);
    renderer.setCrumbTrails(validator.isCrumbTrails());
    int ec = 0;
    if (r instanceof Bundle) {
        if (renderer.handlesBundleDirectly()) {
            renderer.render((Bundle) r);
        } else {
            renderer.start(((Bundle) r).getEntry().size() > 1);
            for (Bundle.BundleEntryComponent e : ((Bundle) r).getEntry()) {
                OperationOutcome op = (OperationOutcome) e.getResource();
                ec = ec + countErrors(op);
                renderer.render(op);
            }
            renderer.finish();
        }
    } else if (r == null) {
        ec = ec + 1;
        System.out.println("No output from validation - nothing to validate");
    } else {
        renderer.start(false);
        OperationOutcome op = (OperationOutcome) r;
        ec = countErrors(op);
        renderer.render((OperationOutcome) r);
        renderer.finish();
    }
    if (cliContext.getOutput() != null) {
        dst.close();
    }
    if (cliContext.getHtmlOutput() != null) {
        String html = new HTMLOutputGenerator(records).generate(System.currentTimeMillis() - start);
        TextFile.stringToFile(html, cliContext.getHtmlOutput());
        System.out.println("HTML Summary in " + cliContext.getHtmlOutput());
    }
    System.exit(ec > 0 ? 1 : 0);
}
Also used : PrintStream(java.io.PrintStream) ValidationOutputRenderer(org.hl7.fhir.validation.cli.renderers.ValidationOutputRenderer) ArrayList(java.util.ArrayList) MemoryMXBean(java.lang.management.MemoryMXBean) FileOutputStream(java.io.FileOutputStream) ValidationRecord(org.hl7.fhir.validation.ValidationRecord)

Example 14 with ValidationEngine

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

the class ValidationService method initializeValidator.

public String initializeValidator(CliContext cliContext, String definitions, TimeTracker tt, String sessionId) throws Exception {
    tt.milestone();
    if (!sessionCache.sessionExists(sessionId)) {
        if (sessionId != null) {
            System.out.println("No such cached session exists for session id " + sessionId + ", re-instantiating validator.");
        }
        System.out.print("  Load FHIR v" + cliContext.getSv() + " from " + definitions);
        ValidationEngine validator = new ValidationEngine.ValidationEngineBuilder().withVersion(cliContext.getSv()).withTimeTracker(tt).withUserAgent("fhir/validator").fromSource(definitions);
        sessionId = sessionCache.cacheSession(validator);
        FhirPublication ver = FhirPublication.fromCode(cliContext.getSv());
        IgLoader igLoader = new IgLoader(validator.getPcm(), validator.getContext(), validator.getVersion(), validator.isDebug());
        System.out.println(" - " + validator.getContext().countAllCaches() + " resources (" + tt.milestone() + ")");
        igLoader.loadIg(validator.getIgs(), validator.getBinaries(), "hl7.terminology", false);
        System.out.print("  Terminology server " + cliContext.getTxServer());
        String txver = validator.setTerminologyServer(cliContext.getTxServer(), cliContext.getTxLog(), ver);
        System.out.println(" - Version " + txver + " (" + tt.milestone() + ")");
        validator.setDebug(cliContext.isDoDebug());
        validator.getContext().setLogger(new SystemOutLoggingService(cliContext.isDoDebug()));
        for (String src : cliContext.getIgs()) {
            igLoader.loadIg(validator.getIgs(), validator.getBinaries(), src, cliContext.isRecursive());
        }
        System.out.print("  Get set... ");
        validator.setQuestionnaireMode(cliContext.getQuestionnaireMode());
        validator.setLevel(cliContext.getLevel());
        validator.setDoNative(cliContext.isDoNative());
        validator.setHintAboutNonMustSupport(cliContext.isHintAboutNonMustSupport());
        for (String s : cliContext.getExtensions()) {
            if ("*".equals(s)) {
                validator.setAnyExtensionsAllowed(true);
            } else {
                validator.getExtensionDomains().add(s);
            }
        }
        validator.setLanguage(cliContext.getLang());
        validator.setLocale(cliContext.getLocale());
        validator.setSnomedExtension(cliContext.getSnomedCTCode());
        validator.setAssumeValidRestReferences(cliContext.isAssumeValidRestReferences());
        validator.setShowMessagesFromReferences(cliContext.isShowMessagesFromReferences());
        validator.setNoExtensibleBindingMessages(cliContext.isNoExtensibleBindingMessages());
        validator.setNoUnicodeBiDiControlChars(cliContext.isNoUnicodeBiDiControlChars());
        validator.setNoInvariantChecks(cliContext.isNoInvariants());
        validator.setWantInvariantInMessage(cliContext.isWantInvariantsInMessages());
        validator.setSecurityChecks(cliContext.isSecurityChecks());
        validator.setCrumbTrails(cliContext.isCrumbTrails());
        validator.setShowTimes(cliContext.isShowTimes());
        validator.setAllowExampleUrls(cliContext.isAllowExampleUrls());
        StandAloneValidatorFetcher fetcher = new StandAloneValidatorFetcher(validator.getPcm(), validator.getContext(), validator);
        validator.setFetcher(fetcher);
        validator.getContext().setLocator(fetcher);
        validator.getBundleValidationRules().addAll(cliContext.getBundleValidationRules());
        TerminologyCache.setNoCaching(cliContext.isNoInternalCaching());
        // generate any missing snapshots
        validator.prepare();
        System.out.println(" go (" + tt.milestone() + ")");
    } else {
        System.out.println("Cached session exists for session id " + sessionId + ", returning stored validator session id.");
    }
    return sessionId;
}
Also used : IgLoader(org.hl7.fhir.validation.IgLoader) FhirPublication(org.hl7.fhir.utilities.FhirPublication) SystemOutLoggingService(org.hl7.fhir.r5.context.SystemOutLoggingService) ValidationEngine(org.hl7.fhir.validation.ValidationEngine)

Example 15 with ValidationEngine

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

the class Common method getValidationEngine.

public static ValidationEngine getValidationEngine(String version, String txServer, String definitions, String txLog, TimeTracker tt) throws Exception {
    System.out.println("Loading (v = " + version + ", tx server -> " + txServer + ")");
    ValidationEngine ve = new ValidationEngine.ValidationEngineBuilder().withVersion(version).withTimeTracker(tt).withUserAgent("fhir/validator").fromSource(definitions);
    ve.connectToTSServer(txServer, txLog, FhirPublication.fromCode(version));
    return ve;
}
Also used : ValidationEngine(org.hl7.fhir.validation.ValidationEngine)

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