use of com.ibm.cohort.cql.evaluation.CqlEvaluationRequest in project quality-measure-and-cohort-service by Alvearie.
the class ConfigurableOutputColumnNameEncoder method create.
public static ConfigurableOutputColumnNameEncoder create(CqlEvaluationRequests evaluationRequests, EncodedParametersCache paramCache, String defaultNameDelimiter) {
Map<String, List<CqlEvaluationRequest>> requestsByContext = new HashMap<>();
// Gather requests by context
for (CqlEvaluationRequest request : evaluationRequests.getEvaluations()) {
String contextKey = request.getContextKey();
List<CqlEvaluationRequest> contextRequests = requestsByContext.computeIfAbsent(contextKey, x -> new ArrayList<>());
contextRequests.add(request);
}
Map<String, ContextColumnNameEncoder> contextKeyToContextNameEncoder = new HashMap<>();
for (String contextKey : requestsByContext.keySet()) {
ContextColumnNameEncoder nameEncoder = ContextColumnNameEncoder.create(requestsByContext.get(contextKey), paramCache, defaultNameDelimiter);
contextKeyToContextNameEncoder.put(contextKey, nameEncoder);
}
return new ConfigurableOutputColumnNameEncoder(contextKeyToContextNameEncoder);
}
use of com.ibm.cohort.cql.evaluation.CqlEvaluationRequest in project quality-measure-and-cohort-service by Alvearie.
the class ContextColumnNameEncoder method create.
public static ContextColumnNameEncoder create(List<CqlEvaluationRequest> contextRequests, EncodedParametersCache paramCache, String defaultColumnDelimiter) {
Map<Integer, Map<String, String>> requestToDefineToOutputColumn = new HashMap<>();
Map<String, Set<String>> outputColumnNamesByParameters = new HashMap<>();
for (CqlEvaluationRequest contextRequest : contextRequests) {
// The global parameters need to be present in the request at this point. They are applied automatically by
// the getFilteredRequestsByContext() method, so we are good, but FYI.
String parametersJson = paramCache.getKeyParametersColumnData(contextRequest);
Set<String> outputColumnNames = outputColumnNamesByParameters.computeIfAbsent(parametersJson, x -> new HashSet<>());
Map<String, String> defineToOutputNameMap = getDefineToOutputNameMap(contextRequest, defaultColumnDelimiter);
// Make sure output names per unique parameter combination are unique across requests
for (String value : defineToOutputNameMap.values()) {
if (outputColumnNames.contains(value)) {
throw new IllegalArgumentException("Duplicate outputColumn " + value + " defined for context " + contextRequest.getContextKey());
}
outputColumnNames.add(value);
}
Integer requestId = contextRequest.getId();
if (requestId == null) {
throw new IllegalArgumentException("Each CqlEvaluationRequest argument must have an requestId set before calling ContextColumnNameEncoder.create()");
}
requestToDefineToOutputColumn.put(requestId, defineToOutputNameMap);
}
Set<String> mergedOutputColumns = outputColumnNamesByParameters.values().stream().reduce(new HashSet<>(), (all, perParams) -> {
all.addAll(perParams);
return all;
});
return new ContextColumnNameEncoder(requestToDefineToOutputColumn, mergedOutputColumns);
}
use of com.ibm.cohort.cql.evaluation.CqlEvaluationRequest 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 com.ibm.cohort.cql.evaluation.CqlEvaluationRequest in project quality-measure-and-cohort-service by Alvearie.
the class SparkCqlEvaluatorTest method testGetFilteredRequestsFilterEvaluations.
@Test
public void testGetFilteredRequestsFilterEvaluations() {
CqlEvaluationRequests requests = new CqlEvaluationRequests();
CqlEvaluationRequest request = makeEvaluationRequest("context", "lib1", "1.0.0");
request.setExpressionsByNames(new HashSet<>(Arrays.asList("cohortOrig", "expr1")));
CqlEvaluationRequest request2 = makeEvaluationRequest("context", "lib2", "1.0.0");
request2.setExpressionsByNames(new HashSet<>(Arrays.asList("cohortOrig", "expr2")));
CqlEvaluationRequest request3 = makeEvaluationRequest("context", "lib3", "1.0.0");
request3.setExpressionsByNames(new HashSet<>(Arrays.asList("cohortOrig")));
List<CqlEvaluationRequest> evaluations = Arrays.asList(request, request2, request3);
requests.setEvaluations(evaluations);
Map<String, String> libs = new HashMap<String, String>() {
{
put("lib1", "1.0.0");
put("lib2", "1.0.0");
put("lib3", "1.0.0");
}
};
Set<String> expressions = new HashSet<>(Arrays.asList("expr1", "expr2"));
CqlEvaluationRequests actual = evaluator.getFilteredRequests(requests, libs, expressions);
assertEquals(3, actual.getEvaluations().size());
assertEquals(Collections.singleton("expr1"), actual.getEvaluations().get(0).getExpressionNames());
assertEquals(Collections.singleton("expr2"), actual.getEvaluations().get(1).getExpressionNames());
assertEquals(Collections.emptySet(), actual.getEvaluations().get(2).getExpressionNames());
}
use of com.ibm.cohort.cql.evaluation.CqlEvaluationRequest in project quality-measure-and-cohort-service by Alvearie.
the class SparkCqlEvaluatorTest method testGetFilteredRequestsNoEvaluationRequestsForLibraries.
@Test
public void testGetFilteredRequestsNoEvaluationRequestsForLibraries() {
CqlEvaluationRequests requests = new CqlEvaluationRequests();
CqlEvaluationRequest request = makeEvaluationRequest("context", "lib1", "1.0.0");
request.setExpressionsByNames(new HashSet<>(Collections.singletonList("cohort")));
CqlEvaluationRequest request2 = makeEvaluationRequest("context", "lib2", "1.0.0");
request2.setExpressionsByNames(new HashSet<>(Collections.singletonList("cohort")));
List<CqlEvaluationRequest> evaluations = Arrays.asList(request, request2);
requests.setEvaluations(evaluations);
Map<String, String> libs = new HashMap<String, String>() {
{
put("lib3", "1.0.0");
put("lib4", "1.0.0");
}
};
CqlEvaluationRequests actual = evaluator.getFilteredRequests(requests, libs, null);
assertTrue(actual.getEvaluations().isEmpty());
}
Aggregations