Search in sources :

Example 1 with CqlRule

use of org.hl7.davinci.endpoint.cql.CqlRule in project CRD by HL7-DaVinci.

the class CqlRule method getFhirVersionFromCqlFile.

private static String getFhirVersionFromCqlFile(byte[] cql) {
    String fhirVersion = "";
    UsingDef usingDef = new UsingDef();
    Pattern pattern = Pattern.compile("using (.*?) version '(.*?)'");
    try {
        Matcher matcher = pattern.matcher(new String(cql));
        while (fhirVersion.isEmpty()) {
            matcher.find();
            if (matcher.groupCount() != 2) {
                throw new RuntimeException("Encountered bad CQL file, could not detect library identifier.");
            }
            if (matcher.group(1).equalsIgnoreCase("FHIR")) {
                fhirVersion = matcher.group(2);
            }
        }
    } catch (Exception e) {
        logger.warn("exception in CqlRule::getFhirVersionFromCqlFile(): " + e.getMessage());
    }
    return fhirVersion;
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) UsingDef(org.hl7.elm.r1.UsingDef) IOException(java.io.IOException)

Example 2 with CqlRule

use of org.hl7.davinci.endpoint.cql.CqlRule in project CRD by HL7-DaVinci.

the class CqlExecutionContextBuilder method getExecutionContext.

public static Context getExecutionContext(CqlRule cqlRule, HashMap<String, Resource> cqlParams, String baseUrl) {
    ModelManager modelManager = new ModelManager();
    LibraryManager libraryManager = new LibraryManager(modelManager);
    libraryManager.getLibrarySourceLoader().clearProviders();
    Library library = null;
    LibraryLoader libraryLoader = null;
    if (cqlRule.isPrecompiled()) {
    // todo
    } else {
        libraryManager.getLibrarySourceLoader().registerProvider(cqlRule.getRawCqlLibrarySourceProvider(CQL_VERSION));
        libraryLoader = new LocalLibraryLoader(libraryManager);
        try {
            library = CqlExecution.translate(cqlRule.getRawMainCqlLibrary(CQL_VERSION), libraryManager, modelManager);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    FhirContext fhirContext = FhirContext.forR4();
    Context context = new Context(library);
    context.registerLibraryLoader(libraryLoader);
    context.setExpressionCaching(true);
    R4FhirModelResolver modelResolver = new R4FhirModelResolver();
    RestFhirRetrieveProvider retrieveProvider = new RestFhirRetrieveProvider(new SearchParameterResolver(fhirContext), fhirContext.newRestfulGenericClient("http://fhirtest.uhn.ca/baseR4"));
    CompositeDataProvider provider = new CompositeDataProvider(modelResolver, retrieveProvider);
    context.registerDataProvider("http://hl7.org/fhir", provider);
    for (Map.Entry<String, org.hl7.fhir.r4.model.Resource> entry : cqlParams.entrySet()) {
        context.setParameter(null, entry.getKey(), entry.getValue());
    }
    context.setParameter(null, "base_url", baseUrl);
    return context;
}
Also used : Context(org.opencds.cqf.cql.engine.execution.Context) FhirContext(ca.uhn.fhir.context.FhirContext) FhirContext(ca.uhn.fhir.context.FhirContext) Resource(org.hl7.fhir.r4.model.Resource) ModelManager(org.cqframework.cql.cql2elm.ModelManager) LibraryLoader(org.opencds.cqf.cql.engine.execution.LibraryLoader) LocalLibraryLoader(org.hl7.davinci.endpoint.cql.LocalLibraryLoader) R4FhirModelResolver(org.opencds.cqf.cql.engine.fhir.model.R4FhirModelResolver) SearchParameterResolver(org.opencds.cqf.cql.engine.fhir.searchparam.SearchParameterResolver) LocalLibraryLoader(org.hl7.davinci.endpoint.cql.LocalLibraryLoader) RestFhirRetrieveProvider(org.opencds.cqf.cql.engine.fhir.retrieve.RestFhirRetrieveProvider) LibraryManager(org.cqframework.cql.cql2elm.LibraryManager) CompositeDataProvider(org.opencds.cqf.cql.engine.data.CompositeDataProvider) Library(org.cqframework.cql.elm.execution.Library) HashMap(java.util.HashMap) Map(java.util.Map)

Example 3 with CqlRule

use of org.hl7.davinci.endpoint.cql.CqlRule in project CRD by HL7-DaVinci.

the class FhirBundleProcessor method buildExecutionContexts.

private void buildExecutionContexts(List<CoverageRequirementRuleCriteria> criteriaList, HashMap<String, Resource> cqlParams) {
    for (CoverageRequirementRuleCriteria criteria : criteriaList) {
        logger.info("FhirBundleProcessor::buildExecutionContexts() criteria: " + criteria.toString());
        List<RuleMapping> rules = fileStore.findRules(criteria);
        for (RuleMapping rule : rules) {
            CoverageRequirementRuleResult result = new CoverageRequirementRuleResult();
            result.setCriteria(criteria).setTopic(rule.getTopic());
            try {
                logger.info("FhirBundleProcessor::buildExecutionContexts() found rule topic: " + rule.getTopic());
                // get the CqlRule
                CqlRule cqlRule = fileStore.getCqlRule(rule.getTopic(), rule.getFhirVersion());
                result.setContext(CqlExecutionContextBuilder.getExecutionContext(cqlRule, cqlParams, baseUrl));
                results.add(result);
            } catch (Exception e) {
                logger.info("r4/FhirBundleProcessor::buildExecutionContexts: failed processing cql bundle: " + e.getMessage());
            }
        }
    }
}
Also used : CoverageRequirementRuleCriteria(org.hl7.davinci.endpoint.rules.CoverageRequirementRuleCriteria) CqlRule(org.hl7.davinci.endpoint.cql.CqlRule) CoverageRequirementRuleResult(org.hl7.davinci.endpoint.rules.CoverageRequirementRuleResult) RuleMapping(org.hl7.davinci.endpoint.database.RuleMapping) RequestIncompleteException(org.hl7.davinci.RequestIncompleteException)

Example 4 with CqlRule

use of org.hl7.davinci.endpoint.cql.CqlRule in project CRD by HL7-DaVinci.

the class CqlRule method build.

private void build(HashMap<String, byte[]> cqlFiles, HashMap<String, byte[]> jsonElmFiles, HashMap<String, byte[]> xmlElmFiles) {
    // build a list of all of the CQL Libraries
    List<CqlRule.CqlLibrary> cqlLibraries = new ArrayList<>();
    for (String fileName : cqlFiles.keySet()) {
        logger.debug("CqlRule: file: " + fileName);
        CqlRule.CqlLibrary cqlLibrary = new CqlRule.CqlLibrary();
        cqlLibrary.cql = cqlFiles.get(fileName);
        // only add those that are the right fhir version
        String fhirVersionFromCql = getFhirVersionFromCqlFile(cqlLibrary.cql);
        // last character of fhirVersion ("R4" => "4") and first character of FHIR version from file ("3.0.0" => "3")
        if (fhirVersion.substring(fhirVersion.length() - 1).equalsIgnoreCase(fhirVersionFromCql.substring(0, 1))) {
            String fileNameWithoutExtension = fileName.substring(0, fileName.length() - 4);
            String xmlElmName = fileNameWithoutExtension + ".xml";
            if (xmlElmFiles.containsKey(xmlElmName)) {
                cqlLibrary.xlmElm = true;
                cqlLibrary.elm = xmlElmFiles.get(xmlElmName);
            }
            String jsonElmName = fileNameWithoutExtension + ".json";
            if (jsonElmFiles.containsKey(jsonElmName)) {
                cqlLibrary.xlmElm = false;
                cqlLibrary.elm = jsonElmFiles.get(jsonElmName);
            }
            cqlLibraries.add(cqlLibrary);
        }
    }
    precompiled = false;
    for (CqlLibrary cqlLibrary : cqlLibraries) {
        if (precompiled) {
            if (cqlLibrary.elm == null) {
                throw new RuntimeException("Package indicated CQL was precompiled, but elm xml missing.");
            }
        // TODO: need to set rulePackage.elmLibraries and mainCqlLibraryId
        } else {
            InputStream cqlStream = new ByteArrayInputStream(cqlLibrary.cql);
            VersionedIdentifier id = getIdFromCqlFile(cqlLibrary.cql);
            String fhirVersionFromFile = getFhirVersionFromCqlFile(cqlLibrary.cql);
            logger.info("CqlRule::Constructor() add id: " + id.getId() + ", fhir version: " + fhirVersionFromFile);
            if (rawCqlLibraries.containsKey(fhirVersionFromFile)) {
                // logger.info("CqlRule::Constructor() add rawCqlLibraries add: " + id.getId());
                rawCqlLibraries.get(fhirVersionFromFile).put(id, cqlStream);
            } else {
                HashMap<VersionedIdentifier, InputStream> map = new HashMap<>();
                map.put(id, cqlStream);
                // logger.info("CqlRule::Constructor() add rawCqlLibraries new: " + id.getId());
                rawCqlLibraries.put(fhirVersionFromFile, map);
            }
            if (id.getId().equals(mainCqlLibraryName)) {
                // logger.info("CqlRule::Constructor() add mainCqlLibraryId: " + id.getId());
                mainCqlLibraryId.put(fhirVersionFromFile, id);
            }
        }
    }
}
Also used : VersionedIdentifier(org.hl7.elm.r1.VersionedIdentifier) ByteArrayInputStream(java.io.ByteArrayInputStream) HashMap(java.util.HashMap) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList)

Example 5 with CqlRule

use of org.hl7.davinci.endpoint.cql.CqlRule in project CRD by HL7-DaVinci.

the class GitHubFileStore method getCqlRule.

public CqlRule getCqlRule(String topic, String fhirVersion) {
    logger.info("GitHubFileStore::getCqlRule(): " + topic + "/" + fhirVersion);
    // load CQL files needed for the CRD Rule
    HashMap<String, byte[]> cqlFiles = new HashMap<>();
    String rulePath = config.getGitHubConfig().getRulePath();
    String examplesPath = config.getGitHubConfig().getExamplesPath();
    String mainCqlLibraryName = topic + "Rule";
    String mainCqlFile = findGitHubFile(topic, fhirVersion, mainCqlLibraryName, FileStore.CQL_EXTENSION);
    if (mainCqlFile == null) {
        logger.warn("GitHubFileStore::getCqlRule(): failed to find main CQL file");
    } else {
        String mainCqlFilePath = rulePath + topic + "/" + fhirVersion + "/files/" + mainCqlFile;
        try {
            InputStream inputStream = connection.getFile(mainCqlFilePath);
            if (inputStream == null) {
                // look for the main cql file in the examples path as well
                mainCqlFilePath = examplesPath + topic + "/" + fhirVersion + "/files/" + mainCqlFile;
                inputStream = connection.getFile(mainCqlFilePath);
            }
            cqlFiles.put(mainCqlFile, IOUtils.toByteArray(inputStream));
            logger.info("GitHubFileStore::getCqlRule(): added mainCqlFile: " + mainCqlFile);
        } catch (IOException e) {
            logger.warn("GitHubFileStore::getCqlRule(): failed to open main cql file: " + e.getMessage());
        }
    }
    String helperCqlFile = findGitHubFile(FileStore.SHARED_TOPIC, fhirVersion, FileStore.FHIR_HELPERS_FILENAME, FileStore.CQL_EXTENSION);
    if (helperCqlFile == null) {
        logger.warn("GitHubFileStore::getCqlRule(): failed to find FHIR helper CQL file");
    } else {
        String helperCqlFilePath = rulePath + "Shared/" + fhirVersion + "/files/" + helperCqlFile;
        try {
            InputStream inputStream = connection.getFile(helperCqlFilePath);
            if (inputStream == null) {
                // look for the helper cql file in the examples path as well
                helperCqlFilePath = examplesPath + "Shared/" + fhirVersion + "/files/" + helperCqlFile;
                inputStream = connection.getFile(helperCqlFilePath);
            }
            cqlFiles.put(helperCqlFile, IOUtils.toByteArray(inputStream));
            logger.info("GitHubFileStore::getCqlRule(): added helperCqlFile: " + helperCqlFile);
        } catch (IOException e) {
            logger.warn("GitHubFileStore::getCqlRule(): failed to open file FHIR helper cql file: " + e.getMessage());
        }
    }
    return new CqlRule(mainCqlLibraryName, cqlFiles, fhirVersion);
}
Also used : CqlRule(org.hl7.davinci.endpoint.cql.CqlRule) HashMap(java.util.HashMap) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException)

Aggregations

HashMap (java.util.HashMap)3 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 CqlRule (org.hl7.davinci.endpoint.cql.CqlRule)2 FhirContext (ca.uhn.fhir.context.FhirContext)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 FileInputStream (java.io.FileInputStream)1 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1 LibraryManager (org.cqframework.cql.cql2elm.LibraryManager)1 ModelManager (org.cqframework.cql.cql2elm.ModelManager)1 Library (org.cqframework.cql.elm.execution.Library)1 RequestIncompleteException (org.hl7.davinci.RequestIncompleteException)1 LocalLibraryLoader (org.hl7.davinci.endpoint.cql.LocalLibraryLoader)1 RuleMapping (org.hl7.davinci.endpoint.database.RuleMapping)1 CoverageRequirementRuleCriteria (org.hl7.davinci.endpoint.rules.CoverageRequirementRuleCriteria)1 CoverageRequirementRuleResult (org.hl7.davinci.endpoint.rules.CoverageRequirementRuleResult)1 UsingDef (org.hl7.elm.r1.UsingDef)1