use of org.hl7.elm.r1.ExpressionDef in project cqf-ruler by DBCG.
the class LibraryEvaluationProvider method getLocations.
// TODO: Merge this into the evaluator
@SuppressWarnings("unused")
private Map<String, List<Integer>> getLocations(org.hl7.elm.r1.Library library) {
Map<String, List<Integer>> locations = new HashMap<>();
if (library.getStatements() == null)
return locations;
for (org.hl7.elm.r1.ExpressionDef def : library.getStatements().getDef()) {
int startLine = def.getTrackbacks().isEmpty() ? 0 : def.getTrackbacks().get(0).getStartLine();
int startChar = def.getTrackbacks().isEmpty() ? 0 : def.getTrackbacks().get(0).getStartChar();
List<Integer> loc = Arrays.asList(startLine, startChar);
locations.put(def.getName(), loc);
}
return locations;
}
use of org.hl7.elm.r1.ExpressionDef 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.hl7.elm.r1.ExpressionDef in project quality-measure-and-cohort-service by Alvearie.
the class DataTypeRequirementsProcessor method getDataRequirements.
public DataTypeRequirements getDataRequirements(CqlLibraryProvider sourceProvider, CqlLibraryDescriptor libraryDescriptor, Set<String> expressions) {
AnyColumnVisitor visitor = new AnyColumnVisitor(sourceProvider);
AnyColumnContext context = new AnyColumnContext();
Library elmLibrary = getElmLibrary(sourceProvider, libraryDescriptor);
// Build a list of all the expressions in the Library that we care about
List<ExpressionDef> expressionDefs = elmLibrary.getStatements().getDef();
if (expressions != null) {
expressionDefs = expressionDefs.stream().filter(def -> expressions.contains(def.getName())).collect(Collectors.toList());
if (expressionDefs.size() != expressions.size()) {
throw new IllegalArgumentException(String.format("One or more requested expressions %s not found in library %s", expressions, libraryDescriptor.toString()));
}
}
visitor.enterLibrary(elmLibrary.getIdentifier());
try {
for (UsingDef usingDef : elmLibrary.getUsings().getDef()) {
visitor.visitElement(usingDef, context);
}
for (ExpressionDef expressionDef : expressionDefs) {
visitor.visitElement(expressionDef, context);
}
} finally {
visitor.exitLibrary();
}
addToChildTypes(context.getModels(), context.getPathsByQName());
addToChildTypes(context.getModels(), context.getMatchers());
Map<String, Set<String>> pathsByDataType = mapToLocalPart(context.getPathsByQName());
Map<String, Set<StringMatcher>> matchersByDataType = mapToLocalPart(context.getMatchers());
return new DataTypeRequirements(context.getModels(), pathsByDataType, matchersByDataType);
}
use of org.hl7.elm.r1.ExpressionDef in project quality-measure-and-cohort-service by Alvearie.
the class GraphWalkingElmVisitor method visitRef.
protected R visitRef(ExpressionRef ref, C context, R defaultResult) {
R result = defaultResult;
if (!visited.contains(ref)) {
visited.add(ref);
Library library = prepareLibraryVisit(getCurrentLibraryIdentifier(), ref.getLibraryName(), context);
try {
// The TranslatedLibrary class that the CqlTranslator produces has this already indexed. If we want to
// use any ELM without relying on the translator, we need to do the lookup ourselves. It is worth
// considering whether or not we build our own indexing helper instead.
Optional<ExpressionDef> optionalEd = library.getStatements().getDef().stream().filter(def -> def.getName().equals(ref.getName())).findAny();
if (optionalEd.isPresent()) {
result = visitElement(optionalEd.get(), context);
} else {
throw new IllegalArgumentException("Could not find definition for reference " + ref.getName());
}
} finally {
unprepareLibraryVisit(ref.getLibraryName());
}
}
return result;
}
Aggregations