use of org.cqframework.cql.elm.execution.Library in project quality-measure-and-cohort-service by Alvearie.
the class R4TranslatingLibraryLoader method load.
@Override
public Library load(VersionedIdentifier libraryIdentifier) {
Library elmLibrary = null;
org.hl7.fhir.r4.model.Library fhirLibrary = resolver.resolveByName(libraryIdentifier.getId(), libraryIdentifier.getVersion());
if (fhirLibrary == null) {
throw new IllegalArgumentException(String.format("Library %s-%s not found", libraryIdentifier.getId(), libraryIdentifier.getVersion()));
}
Map<String, Attachment> mimeTypeIndex = new HashMap<>();
for (Attachment attachment : fhirLibrary.getContent()) {
if (attachment.hasContentType()) {
mimeTypeIndex.put(attachment.getContentType(), attachment);
} else {
throw new IllegalArgumentException(String.format("Library %s-%s contains an attachment with no content type", libraryIdentifier.getId(), libraryIdentifier.getVersion()));
}
}
Attachment attachment = mimeTypeIndex.get("application/elm+xml");
if (attachment != null) {
try (InputStream is = getAttachmentDataAsStream(attachment)) {
elmLibrary = CqlLibraryReader.read(is);
} catch (Exception ex) {
throw new IllegalArgumentException(String.format("Library %s-%s elm attachment failed to deserialize", libraryIdentifier.getId(), libraryIdentifier.getVersion()), ex);
}
}
if (elmLibrary == null) {
attachment = mimeTypeIndex.get("text/cql");
if (attachment == null) {
throw new IllegalArgumentException(String.format("Library %s-%s must contain either a application/elm+xml or text/cql attachment", libraryIdentifier.getId(), libraryIdentifier.getVersion()));
} else {
String content = getAttachmentDataAsString(attachment);
try {
elmLibrary = OptimizedCqlLibraryReader.read(translateLibrary(content, libraryIdentifier));
} catch (Exception ex) {
throw new IllegalArgumentException(String.format("Library %s-%s cql attachment failed to deserialize", libraryIdentifier.getId(), libraryIdentifier.getVersion()), ex);
}
}
}
return elmLibrary;
}
use of org.cqframework.cql.elm.execution.Library in project quality-measure-and-cohort-service by Alvearie.
the class SparkSchemaCreator method calculateSchemaForContext.
public StructType calculateSchemaForContext(String contextName) throws Exception {
List<CqlEvaluationRequest> filteredRequests = requests.getEvaluationsForContext(contextName);
StructType resultsSchema = new StructType();
Set<Tuple2<String, String>> usingInfo = new HashSet<>();
for (CqlEvaluationRequest filteredRequest : filteredRequests) {
CqlLibraryDescriptor descriptor = filteredRequest.getDescriptor();
String libraryId = descriptor.getLibraryId();
for (String expression : filteredRequest.getExpressionNames()) {
CqlLibrary providedLibrary = libraryProvider.getLibrary(new CqlLibraryDescriptor().setLibraryId(libraryId).setVersion(descriptor.getVersion()).setFormat(Format.ELM));
if (providedLibrary == null) {
throw new IllegalArgumentException("Library not found: " + descriptor.getLibraryId() + "-" + descriptor.getVersion());
}
Library library = CqlLibraryReader.read(providedLibrary.getContentAsStream());
// Track the set of non-system using statements across libraries.
// Information is used later to access ModelInfos when searching
// for context key column type information.
usingInfo.addAll(library.getUsings().getDef().stream().filter(x -> !x.getLocalIdentifier().equals("System")).map(x -> new Tuple2<>(x.getLocalIdentifier(), x.getVersion())).collect(Collectors.toList()));
List<ExpressionDef> expressionDefs = library.getStatements().getDef().stream().filter(x -> x.getName().equals(expression)).collect(Collectors.toList());
if (expressionDefs.isEmpty()) {
throw new IllegalArgumentException("Expression " + expression + " is configured in the CQL jobs file, but not found in " + descriptor.getLibraryId() + "-" + descriptor.getVersion());
} else if (expressionDefs.size() > 1) {
throw new IllegalArgumentException("Expression " + expression + " was defined multiple times in library: " + descriptor.getLibraryId() + "-" + descriptor.getVersion());
}
QName resultTypeName = expressionDefs.get(0).getExpression().getResultTypeName();
if (resultTypeName == null) {
throw new IllegalArgumentException("Expression " + expression + " has a null result type: " + descriptor.getLibraryId() + "-" + descriptor.getVersion());
}
// The column name encoder already performed duplicate checking. We just need to make sure
// we add each uniquely named column to the output one time.
String columnName = sparkOutputColumnEncoder.getColumnName(filteredRequest, expression);
if (resultsSchema.getFieldIndex(columnName).isEmpty()) {
resultsSchema = resultsSchema.add(columnName, QNameToDataTypeConverter.getFieldType(resultTypeName), true);
}
}
}
if (resultsSchema.fields().length > 0) {
Tuple2<String, DataType> keyInformation = getDataTypeForContextKey(contextName, usingInfo);
StructType fullSchema = new StructType().add(keyInformation._1(), keyInformation._2(), false).add(getParametersColumnName(), DataTypes.StringType, false);
for (StructField field : resultsSchema.fields()) {
fullSchema = fullSchema.add(field);
}
resultsSchema = fullSchema;
}
return resultsSchema;
}
use of org.cqframework.cql.elm.execution.Library in project quality-measure-and-cohort-service by Alvearie.
the class CqlContextFactory method registerExternalIncludes.
private void registerExternalIncludes(Context context, Library currentLibrary) {
Library.Includes includes = currentLibrary.getIncludes();
if (includes != null) {
for (IncludeDef include : includes.getDef()) {
VersionedIdentifier vid = new VersionedIdentifier().withId(include.getLocalIdentifier()).withVersion(include.getVersion());
context.registerExternalFunctionProvider(vid, this.externalFunctionProvider);
}
}
}
use of org.cqframework.cql.elm.execution.Library in project quality-measure-and-cohort-service by Alvearie.
the class CqlContextFactory method createContext.
/**
* Initialize a CQL context from the values associated with the provided
* CQL Context Key. This encapsulates the set of initializations that are
* static from run to run.
*
* @param contextKey container for stable context settings
* @return initialized CQL Context object
* @throws CqlLibraryDeserializationException if the specified library cannot be loaded
*/
protected Context createContext(ContextCacheKey contextKey) throws CqlLibraryDeserializationException {
LibraryLoader libraryLoader = new ProviderBasedLibraryLoader(contextKey.libraryProvider);
VersionedIdentifier vid = new VersionedIdentifier().withId(contextKey.topLevelLibraryIdentifier.getId()).withVersion(contextKey.topLevelLibraryIdentifier.getVersion());
Library entryPoint = libraryLoader.load(vid);
Context cqlContext;
if (contextKey.evaluationDateTime != null) {
cqlContext = new Context(entryPoint, contextKey.evaluationDateTime, new CqlSystemDataProvider());
} else {
cqlContext = new Context(entryPoint, new CqlSystemDataProvider());
}
cqlContext.registerExternalFunctionProvider(vid, this.externalFunctionProvider);
registerExternalIncludes(cqlContext, cqlContext.getCurrentLibrary());
cqlContext.registerLibraryLoader(libraryLoader);
cqlContext.registerTerminologyProvider(contextKey.terminologyProvider);
if (contextKey.parameters != null) {
Library library = cqlContext.getCurrentLibrary();
for (Map.Entry<String, Parameter> entry : contextKey.parameters.entrySet()) {
Object cqlValue = entry.getValue().toCqlType();
cqlContext.setParameter(library.getLocalId(), entry.getKey(), cqlValue);
}
if (library.getIncludes() != null && library.getIncludes().getDef() != null) {
for (IncludeDef def : library.getIncludes().getDef()) {
String name = def.getLocalIdentifier();
for (Map.Entry<String, Parameter> parameterValue : contextKey.parameters.entrySet()) {
Object cqlValue = parameterValue.getValue().toCqlType();
cqlContext.setParameter(name, parameterValue.getKey(), cqlValue);
}
}
}
}
return cqlContext;
}
use of org.cqframework.cql.elm.execution.Library in project quality-measure-and-cohort-service by Alvearie.
the class OptimizedCqlLibraryReaderTest method testOverrideAndOrEvaluators.
@Test
public void testOverrideAndOrEvaluators() throws JAXBException {
InputStream is = this.getClass().getClassLoader().getResourceAsStream("cql/short-circuit-and-or.xml");
String xml = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8)).lines().collect(Collectors.joining(System.lineSeparator()));
Library library = OptimizedCqlLibraryReader.read(xml);
List<ExpressionDef> defs = library.getStatements().getDef();
Optional<ExpressionDef> orEvaluator = defs.stream().filter(def -> def.getName().equals("TrueOrTrue")).findFirst();
assertTrue(orEvaluator.isPresent());
assertThat(orEvaluator.get().getExpression(), instanceOf(ShortOrEvaluator.class));
Optional<ExpressionDef> andEvaluator = defs.stream().filter(def -> def.getName().equals("TrueAndTrue")).findFirst();
assertTrue(andEvaluator.isPresent());
assertThat(andEvaluator.get().getExpression(), instanceOf(ShortAndEvaluator.class));
}
Aggregations