use of org.cqframework.cql.elm.requirements.ElmRequirement in project clinical_quality_language by cqframework.
the class DataRequirementsProcessor method gatherDataRequirements.
public Library gatherDataRequirements(LibraryManager libraryManager, CompiledLibrary translatedLibrary, CqlTranslatorOptions options, Set<String> expressions, boolean includeLogicDefinitions, boolean recursive) {
if (libraryManager == null) {
throw new IllegalArgumentException("libraryManager required");
}
if (translatedLibrary == null) {
throw new IllegalArgumentException("translatedLibrary required");
}
ElmRequirementsVisitor visitor = new ElmRequirementsVisitor();
ElmRequirementsContext context = new ElmRequirementsContext(libraryManager, options, visitor);
List<ExpressionDef> expressionDefs = null;
if (expressions == null) {
visitor.visitLibrary(translatedLibrary.getLibrary(), context);
if (translatedLibrary.getLibrary() != null && translatedLibrary.getLibrary().getStatements() != null) {
expressionDefs = translatedLibrary.getLibrary().getStatements().getDef();
} else {
expressionDefs = new ArrayList<ExpressionDef>();
}
} else {
if (expressionDefs == null) {
expressionDefs = new ArrayList<ExpressionDef>();
}
context.enterLibrary(translatedLibrary.getIdentifier());
try {
for (String expression : expressions) {
ExpressionDef ed = translatedLibrary.resolveExpressionRef(expression);
if (ed != null) {
expressionDefs.add(ed);
visitor.visitElement(ed, context);
} else {
// If the expression is the name of any functions, include those in the gather
// TODO: Provide a mechanism to specify a function def (need signature)
Iterable<FunctionDef> fds = translatedLibrary.resolveFunctionRef(expression);
for (FunctionDef fd : fds) {
expressionDefs.add(fd);
visitor.visitElement(fd, context);
}
}
}
} finally {
context.exitLibrary();
}
}
// In the non-recursive case
// Collect top-level dependencies that have the same library identifier as the primary library
// Collect data requirements reported or inferred on expressions in the library
// In the recursive case
// Collect all top-level dependencies
// Collect all reported or inferred data requirements
ElmRequirements requirements = new ElmRequirements(translatedLibrary.getIdentifier(), translatedLibrary.getLibrary());
if (recursive) {
// Collect all the dependencies
requirements.reportRequirement(context.getRequirements());
// Collect reported data requirements from each expression
for (ElmRequirements reportedRequirements : context.getReportedRequirements()) {
requirements.reportRequirement(reportedRequirements);
}
for (ElmRequirement inferredRequirement : context.getInferredRequirements()) {
requirements.reportRequirement(inferredRequirement);
}
} else {
for (ElmRequirement requirement : context.getRequirements().getRequirements()) {
if (requirement.getLibraryIdentifier().equals(translatedLibrary.getIdentifier()))
requirements.reportRequirement(requirement);
}
for (ExpressionDef ed : expressionDefs) {
// Just being defensive here, can happen when there are errors deserializing the measure
if (ed != null) {
// Collect both inferred and reported requirements here, since reported requirements will not include
// directly inferred requirements
ElmRequirements reportedRequirements = context.getReportedRequirements(ed);
requirements.reportRequirement(reportedRequirements);
ElmRequirement inferredRequirement = context.getInferredRequirements(ed);
requirements.reportRequirement(inferredRequirement);
}
}
}
// Collapse the requirements
if (options.getCollapseDataRequirements()) {
requirements = requirements.collapse();
}
return createLibrary(context, requirements, translatedLibrary.getIdentifier(), expressionDefs, includeLogicDefinitions);
}
use of org.cqframework.cql.elm.requirements.ElmRequirement in project clinical_quality_language by cqframework.
the class DataRequirementsProcessor method extractDataRequirements.
private List<DataRequirement> extractDataRequirements(ElmRequirementsContext context, ElmRequirements requirements) {
List<DataRequirement> result = new ArrayList<>();
Map<String, Retrieve> retrieveMap = new HashMap<String, Retrieve>();
for (ElmRequirement retrieve : requirements.getRetrieves()) {
if (retrieve.getElement().getLocalId() != null) {
retrieveMap.put(retrieve.getElement().getLocalId(), (Retrieve) retrieve.getElement());
}
}
for (ElmRequirement retrieve : requirements.getRetrieves()) {
if (((Retrieve) retrieve.getElement()).getDataType() != null) {
result.add(toDataRequirement(context, retrieve.getLibraryIdentifier(), (Retrieve) retrieve.getElement(), retrieveMap, retrieve instanceof ElmDataRequirement ? ((ElmDataRequirement) retrieve).getProperties() : null));
}
}
return result;
}
use of org.cqframework.cql.elm.requirements.ElmRequirement in project clinical_quality_language by cqframework.
the class DataRequirementsProcessor method extractLogicDefinitions.
private List<Extension> extractLogicDefinitions(ElmRequirementsContext context, ElmRequirements requirements) {
List<Extension> result = new ArrayList<Extension>();
int sequence = 0;
for (ElmRequirement req : requirements.getExpressionDefs()) {
ExpressionDef def = (ExpressionDef) req.getElement();
org.hl7.cql_annotations.r1.Annotation a = getAnnotation(def);
if (a != null) {
result.add(toLogicDefinition(req, def, toNarrativeText(a), sequence++));
}
}
return result;
}
use of org.cqframework.cql.elm.requirements.ElmRequirement in project clinical_quality_language by cqframework.
the class DataRequirementsProcessor method extractParameters.
private List<ParameterDefinition> extractParameters(ElmRequirementsContext context, ElmRequirements requirements, VersionedIdentifier libraryIdentifier, Iterable<ExpressionDef> expressionDefs) {
List<ParameterDefinition> result = new ArrayList<>();
// TODO: Support library qualified parameters
// Until then, name clashes should result in a warning
Map<String, ParameterDefinition> pds = new HashMap<String, ParameterDefinition>();
for (ElmRequirement def : requirements.getParameterDefs()) {
ParameterDefinition pd = toParameterDefinition(def.getLibraryIdentifier(), (ParameterDef) def.getElement());
if (pds.containsKey(pd.getName())) {
ParameterDefinition existingPd = pds.get(pd.getName());
if (!isEquivalentDefinition(existingPd, pd)) {
// Issue a warning that the parameter has a duplicate name but an incompatible type
validationMessages.add(new ValidationMessage(ValidationMessage.Source.Publisher, ValidationMessage.IssueType.NOTSUPPORTED, "CQL Library Packaging", String.format("Parameter declaration %s.%s is already defined in a different library with a different type. Parameter binding may result in errors during evaluation.", def.getLibraryIdentifier().getId(), pd.getName()), ValidationMessage.IssueSeverity.WARNING));
}
} else {
pds.put(pd.getName(), pd);
result.add(pd);
}
}
for (ExpressionDef def : expressionDefs) {
if (def != null && !(def instanceof FunctionDef) && (def.getAccessLevel() == null || def.getAccessLevel() == AccessModifier.PUBLIC)) {
result.add(toOutputParameterDefinition(libraryIdentifier, def));
}
}
return result;
}
Aggregations