Search in sources :

Example 16 with CqlEvaluationRequest

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);
}
Also used : HashMap(java.util.HashMap) CqlEvaluationRequest(com.ibm.cohort.cql.evaluation.CqlEvaluationRequest) List(java.util.List) ArrayList(java.util.ArrayList)

Example 17 with CqlEvaluationRequest

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);
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) HashMap(java.util.HashMap) CqlEvaluationRequest(com.ibm.cohort.cql.evaluation.CqlEvaluationRequest) Map(java.util.Map) HashMap(java.util.HashMap)

Example 18 with CqlEvaluationRequest

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;
}
Also used : DataType(org.apache.spark.sql.types.DataType) ModelInfo(org.hl7.elm_modelinfo.r1.ModelInfo) CqlToElmTranslator(com.ibm.cohort.cql.translation.CqlToElmTranslator) HashMap(java.util.HashMap) Format(com.ibm.cohort.cql.library.Format) ExpressionDef(org.cqframework.cql.elm.execution.ExpressionDef) ClassInfo(org.hl7.elm_modelinfo.r1.ClassInfo) HashSet(java.util.HashSet) ContextDefinition(com.ibm.cohort.cql.spark.aggregation.ContextDefinition) Map(java.util.Map) CqlLibraryReader(org.opencds.cqf.cql.engine.execution.CqlLibraryReader) CqlEvaluationRequest(com.ibm.cohort.cql.evaluation.CqlEvaluationRequest) ModelUtils(com.ibm.cohort.cql.spark.optimizer.ModelUtils) DataTypes(org.apache.spark.sql.types.DataTypes) StructField(org.apache.spark.sql.types.StructField) StructType(org.apache.spark.sql.types.StructType) ModelManager(org.cqframework.cql.cql2elm.ModelManager) Collection(java.util.Collection) VersionedIdentifier(org.hl7.elm.r1.VersionedIdentifier) Set(java.util.Set) CqlLibrary(com.ibm.cohort.cql.library.CqlLibrary) CqlLibraryProvider(com.ibm.cohort.cql.library.CqlLibraryProvider) Tuple2(scala.Tuple2) Collectors(java.util.stream.Collectors) CqlLibraryDescriptor(com.ibm.cohort.cql.library.CqlLibraryDescriptor) List(java.util.List) CqlEvaluationRequests(com.ibm.cohort.cql.evaluation.CqlEvaluationRequests) Library(org.cqframework.cql.elm.execution.Library) ClassInfoElement(org.hl7.elm_modelinfo.r1.ClassInfoElement) QName(javax.xml.namespace.QName) ContextDefinitions(com.ibm.cohort.cql.spark.aggregation.ContextDefinitions) StructType(org.apache.spark.sql.types.StructType) ExpressionDef(org.cqframework.cql.elm.execution.ExpressionDef) QName(javax.xml.namespace.QName) CqlEvaluationRequest(com.ibm.cohort.cql.evaluation.CqlEvaluationRequest) CqlLibrary(com.ibm.cohort.cql.library.CqlLibrary) StructField(org.apache.spark.sql.types.StructField) Tuple2(scala.Tuple2) DataType(org.apache.spark.sql.types.DataType) CqlLibrary(com.ibm.cohort.cql.library.CqlLibrary) Library(org.cqframework.cql.elm.execution.Library) CqlLibraryDescriptor(com.ibm.cohort.cql.library.CqlLibraryDescriptor) HashSet(java.util.HashSet)

Example 19 with CqlEvaluationRequest

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());
}
Also used : HashMap(java.util.HashMap) CqlEvaluationRequest(com.ibm.cohort.cql.evaluation.CqlEvaluationRequest) CqlEvaluationRequests(com.ibm.cohort.cql.evaluation.CqlEvaluationRequests) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 20 with CqlEvaluationRequest

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());
}
Also used : HashMap(java.util.HashMap) CqlEvaluationRequest(com.ibm.cohort.cql.evaluation.CqlEvaluationRequest) CqlEvaluationRequests(com.ibm.cohort.cql.evaluation.CqlEvaluationRequests) Test(org.junit.Test)

Aggregations

CqlEvaluationRequest (com.ibm.cohort.cql.evaluation.CqlEvaluationRequest)30 Test (org.junit.Test)21 CqlLibraryDescriptor (com.ibm.cohort.cql.library.CqlLibraryDescriptor)19 HashMap (java.util.HashMap)16 CqlEvaluationRequests (com.ibm.cohort.cql.evaluation.CqlEvaluationRequests)15 CqlExpressionConfiguration (com.ibm.cohort.cql.evaluation.CqlExpressionConfiguration)15 Parameter (com.ibm.cohort.cql.evaluation.parameters.Parameter)6 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)5 DateParameter (com.ibm.cohort.cql.evaluation.parameters.DateParameter)5 DecimalParameter (com.ibm.cohort.cql.evaluation.parameters.DecimalParameter)5 IntegerParameter (com.ibm.cohort.cql.evaluation.parameters.IntegerParameter)5 IntervalParameter (com.ibm.cohort.cql.evaluation.parameters.IntervalParameter)5 StringParameter (com.ibm.cohort.cql.evaluation.parameters.StringParameter)5 Map (java.util.Map)5 File (java.io.File)4 List (java.util.List)4 Set (java.util.Set)4 CqlLibraryProvider (com.ibm.cohort.cql.library.CqlLibraryProvider)3 CqlToElmTranslator (com.ibm.cohort.cql.translation.CqlToElmTranslator)3 ArrayList (java.util.ArrayList)3