Search in sources :

Example 1 with UcumEssenceService

use of org.fhir.ucum.UcumEssenceService in project org.hl7.fhir.core by hapifhir.

the class ValidationEngine method loadCoreDefinitions.

/**
 * @param src
 * @param recursive
 * @param terminologyCachePath
 * @param userAgent
 * @param tt
 * @param loggingService
 * @throws FHIRException
 * @throws IOException
 *
 * @see IgLoader#loadIgSource(String, boolean, boolean) loadIgSource for detailed description of the src parameter
 */
private void loadCoreDefinitions(String src, boolean recursive, String terminologyCachePath, String userAgent, TimeTracker tt, IWorkerContext.ILoggingService loggingService) throws FHIRException, IOException {
    NpmPackage npm = getPcm().loadPackage(src, null);
    if (npm != null) {
        version = npm.fhirVersion();
        SimpleWorkerContext.SimpleWorkerContextBuilder contextBuilder = new SimpleWorkerContext.SimpleWorkerContextBuilder().withLoggingService(loggingService);
        if (terminologyCachePath != null)
            contextBuilder = contextBuilder.withTerminologyCachePath(terminologyCachePath);
        if (userAgent != null) {
            contextBuilder.withUserAgent(userAgent);
        }
        context = contextBuilder.fromPackage(npm, ValidatorUtils.loaderForVersion(version));
    } else {
        Map<String, byte[]> source = igLoader.loadIgSource(src, recursive, true);
        if (version == null) {
            version = getVersionFromPack(source);
        }
        SimpleWorkerContext.SimpleWorkerContextBuilder contextBuilder = new SimpleWorkerContext.SimpleWorkerContextBuilder();
        if (terminologyCachePath != null)
            contextBuilder = contextBuilder.withTerminologyCachePath(terminologyCachePath);
        if (userAgent != null) {
            contextBuilder.withUserAgent(userAgent);
        }
        context = contextBuilder.fromDefinitions(source, ValidatorUtils.loaderForVersion(version), new PackageVersion(src, new Date()));
        ValidatorUtils.grabNatives(getBinaries(), source, "http://hl7.org/fhir");
    }
    // on https://chat.fhir.org/#narrow/stream/179167-hapi
    try {
        ClassLoader classLoader = ValidationEngine.class.getClassLoader();
        InputStream ue = classLoader.getResourceAsStream("ucum-essence.xml");
        context.setUcumService(new UcumEssenceService(ue));
    } catch (Exception e) {
        throw new FHIRException("Error loading UCUM from embedded ucum-essence.xml: " + e.getMessage(), e);
    }
    initContext(tt);
}
Also used : SimpleWorkerContext(org.hl7.fhir.r5.context.SimpleWorkerContext) PackageVersion(org.hl7.fhir.r5.context.IWorkerContext.PackageVersion) FHIRException(org.hl7.fhir.exceptions.FHIRException) URISyntaxException(java.net.URISyntaxException) SAXException(org.xml.sax.SAXException) DefinitionException(org.hl7.fhir.exceptions.DefinitionException) FHIRException(org.hl7.fhir.exceptions.FHIRException) UcumEssenceService(org.fhir.ucum.UcumEssenceService) NpmPackage(org.hl7.fhir.utilities.npm.NpmPackage)

Example 2 with UcumEssenceService

use of org.fhir.ucum.UcumEssenceService in project org.hl7.fhir.core by hapifhir.

the class TestingUtilities method getWorkerContext.

public static IWorkerContext getWorkerContext(String version) {
    FilesystemPackageCacheManager pcm;
    try {
        pcm = new FilesystemPackageCacheManager(true, ToolsVersion.TOOLS_VERSION);
        IWorkerContext fcontext = getWorkerContext(pcm.loadPackage(VersionUtilities.packageForVersion(version), version));
        fcontext.setUcumService(new UcumEssenceService(TestingUtilities.loadTestResourceStream("ucum", "ucum-essence.xml")));
        fcontext.setExpansionProfile(new Parameters());
        if (!fcontext.hasPackage("hl7.terminology", null)) {
            NpmPackage utg = new FilesystemPackageCacheManager(true, ToolsVersion.TOOLS_VERSION).loadPackage("hl7.terminology");
            System.out.println("Loading THO: " + utg.name() + "#" + utg.version());
            fcontext.loadFromPackage(utg, new TestPackageLoader(new String[] { "CodeSystem", "ValueSet" }));
        }
        return fcontext;
    } catch (Exception e) {
        e.printStackTrace();
        throw new Error(e);
    }
}
Also used : UcumEssenceService(org.fhir.ucum.UcumEssenceService) FilesystemPackageCacheManager(org.hl7.fhir.utilities.npm.FilesystemPackageCacheManager) IWorkerContext(org.hl7.fhir.r5.context.IWorkerContext) Parameters(org.hl7.fhir.r5.model.Parameters) NpmPackage(org.hl7.fhir.utilities.npm.NpmPackage) JsonSyntaxException(com.google.gson.JsonSyntaxException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException)

Example 3 with UcumEssenceService

use of org.fhir.ucum.UcumEssenceService in project org.hl7.fhir.core by hapifhir.

the class UtilitiesXTests method context.

public static IWorkerContext context(String version) {
    if (fcontexts == null) {
        fcontexts = new HashMap<>();
    }
    if (!fcontexts.containsKey(version)) {
        FilesystemPackageCacheManager pcm;
        try {
            pcm = new FilesystemPackageCacheManager(true, ToolsVersion.TOOLS_VERSION);
            IWorkerContext fcontext = TestingUtilities.getWorkerContext(pcm.loadPackage(VersionUtilities.packageForVersion(version), version), loaderForVersion(version));
            fcontext.setUcumService(new UcumEssenceService(UtilitiesXTests.loadTestResourceStream("ucum", "ucum-essence.xml")));
            fcontext.setExpansionProfile(new Parameters());
            fcontexts.put(version, fcontext);
        } catch (Exception e) {
            throw new Error(e);
        }
    }
    return fcontexts.get(version);
}
Also used : UcumEssenceService(org.fhir.ucum.UcumEssenceService) FilesystemPackageCacheManager(org.hl7.fhir.utilities.npm.FilesystemPackageCacheManager) IWorkerContext(org.hl7.fhir.r5.context.IWorkerContext) Parameters(org.hl7.fhir.r5.model.Parameters) JsonSyntaxException(com.google.gson.JsonSyntaxException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException)

Example 4 with UcumEssenceService

use of org.fhir.ucum.UcumEssenceService in project org.hl7.fhir.core by hapifhir.

the class TestingUtilities method context.

public static IWorkerContext context(String version) {
    if ("4.5.0".equals(version)) {
        // temporary work around
        version = "4.4.0";
    }
    String v = VersionUtilities.getMajMin(version);
    if (fcontexts == null) {
        fcontexts = new HashMap<>();
    }
    if (!fcontexts.containsKey(v)) {
        FilesystemPackageCacheManager pcm;
        try {
            pcm = new FilesystemPackageCacheManager(true, ToolsVersion.TOOLS_VERSION);
            IWorkerContext fcontext = SimpleWorkerContext.fromPackage(pcm.loadPackage(VersionUtilities.packageForVersion(version), version));
            fcontext.setUcumService(new UcumEssenceService(TestingUtilities.loadTestResourceStream("ucum", "ucum-essence.xml")));
            fcontext.setExpansionProfile(new Parameters());
            // ((SimpleWorkerContext) fcontext).connectToTSServer(new TerminologyClientR5("http://tx.fhir.org/r4"), null);
            fcontexts.put(v, fcontext);
        } catch (Exception e) {
            e.printStackTrace();
            throw new Error(e);
        }
    }
    return fcontexts.get(v);
}
Also used : UcumEssenceService(org.fhir.ucum.UcumEssenceService) FilesystemPackageCacheManager(org.hl7.fhir.utilities.npm.FilesystemPackageCacheManager) IWorkerContext(org.hl7.fhir.r4b.context.IWorkerContext) Parameters(org.hl7.fhir.r4b.model.Parameters) JsonSyntaxException(com.google.gson.JsonSyntaxException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException)

Example 5 with UcumEssenceService

use of org.fhir.ucum.UcumEssenceService in project CRD by HL7-DaVinci.

the class CqlExecution method translateToElm.

public static String translateToElm(String cql, LibrarySourceProvider librarySourceProvider) throws Exception {
    ModelManager modelManager = new ModelManager();
    LibraryManager libraryManager = new LibraryManager(modelManager);
    libraryManager.getLibrarySourceLoader().registerProvider(new FhirLibrarySourceProvider());
    if (librarySourceProvider != null) {
        libraryManager.getLibrarySourceLoader().registerProvider(librarySourceProvider);
    }
    ArrayList<CqlTranslator.Options> options = new ArrayList<>();
    options.add(CqlTranslator.Options.EnableDateRangeOptimization);
    UcumService ucumService = new UcumEssenceService(UcumEssenceService.class.getResourceAsStream("/ucum-essence.xml"));
    CqlTranslator translator = CqlTranslator.fromText(cql, modelManager, libraryManager, ucumService, options.toArray(new CqlTranslator.Options[options.size()]));
    libraryManager.getLibrarySourceLoader().clearProviders();
    if (translator.getErrors().size() > 0) {
        ArrayList<String> errors = new ArrayList<>();
        for (CqlTranslatorException error : translator.getErrors()) {
            TrackBack tb = error.getLocator();
            String lines = tb == null ? "[n/a]" : String.format("[%d:%d, %d:%d]", tb.getStartLine(), tb.getStartChar(), tb.getEndLine(), tb.getEndChar());
            errors.add(lines + error.getMessage());
        }
        throw new IllegalArgumentException(errors.toString());
    }
    return translator.toJson();
}
Also used : CqlTranslator(org.cqframework.cql.cql2elm.CqlTranslator) ArrayList(java.util.ArrayList) ModelManager(org.cqframework.cql.cql2elm.ModelManager) CqlTranslatorException(org.cqframework.cql.cql2elm.CqlTranslatorException) UcumEssenceService(org.fhir.ucum.UcumEssenceService) UcumService(org.fhir.ucum.UcumService) LibraryManager(org.cqframework.cql.cql2elm.LibraryManager) TrackBack(org.cqframework.cql.elm.tracking.TrackBack) FhirLibrarySourceProvider(org.cqframework.cql.cql2elm.FhirLibrarySourceProvider)

Aggregations

UcumEssenceService (org.fhir.ucum.UcumEssenceService)10 IOException (java.io.IOException)6 JsonSyntaxException (com.google.gson.JsonSyntaxException)4 FileNotFoundException (java.io.FileNotFoundException)4 FilesystemPackageCacheManager (org.hl7.fhir.utilities.npm.FilesystemPackageCacheManager)4 UcumService (org.fhir.ucum.UcumService)3 ArrayList (java.util.ArrayList)2 CqlTranslator (org.cqframework.cql.cql2elm.CqlTranslator)2 CqlTranslatorException (org.cqframework.cql.cql2elm.CqlTranslatorException)2 TrackBack (org.cqframework.cql.elm.tracking.TrackBack)2 IWorkerContext (org.hl7.fhir.r5.context.IWorkerContext)2 Parameters (org.hl7.fhir.r5.model.Parameters)2 NpmPackage (org.hl7.fhir.utilities.npm.NpmPackage)2 FileInputStream (java.io.FileInputStream)1 FileOutputStream (java.io.FileOutputStream)1 StringReader (java.io.StringReader)1 URISyntaxException (java.net.URISyntaxException)1 JAXBException (javax.xml.bind.JAXBException)1 FhirLibrarySourceProvider (org.cqframework.cql.cql2elm.FhirLibrarySourceProvider)1 LibraryManager (org.cqframework.cql.cql2elm.LibraryManager)1