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;
}
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;
}
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());
}
}
}
}
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);
}
}
}
}
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);
}
Aggregations