Search in sources :

Example 26 with CqlLibraryDescriptor

use of com.ibm.cohort.cql.library.CqlLibraryDescriptor 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 27 with CqlLibraryDescriptor

use of com.ibm.cohort.cql.library.CqlLibraryDescriptor in project quality-measure-and-cohort-service by Alvearie.

the class ConfigurableOutputColumnNameEncoderTest method testNamesForMultipleContexts.

@Test
public void testNamesForMultipleContexts() {
    CqlLibraryDescriptor libraryDescriptor1 = new CqlLibraryDescriptor();
    libraryDescriptor1.setLibraryId("lib1");
    CqlEvaluationRequest request = new CqlEvaluationRequest();
    request.setDescriptor(libraryDescriptor1);
    request.setId(1);
    CqlExpressionConfiguration expressionConfiguration1 = new CqlExpressionConfiguration();
    expressionConfiguration1.setName("abcd");
    expressionConfiguration1.setOutputColumn("A1");
    CqlExpressionConfiguration expressionConfiguration2 = new CqlExpressionConfiguration();
    expressionConfiguration2.setName("efgh");
    expressionConfiguration2.setOutputColumn("A2");
    request.setExpressions(new HashSet<>(Arrays.asList(expressionConfiguration1, expressionConfiguration2)));
    CqlLibraryDescriptor libraryDescriptor2 = new CqlLibraryDescriptor();
    libraryDescriptor2.setLibraryId("lib1");
    CqlEvaluationRequest request2 = new CqlEvaluationRequest();
    request2.setDescriptor(libraryDescriptor2);
    request2.setId(2);
    CqlExpressionConfiguration expressionConfiguration3 = new CqlExpressionConfiguration();
    expressionConfiguration3.setName("abcd");
    expressionConfiguration3.setOutputColumn("A3");
    CqlExpressionConfiguration expressionConfiguration4 = new CqlExpressionConfiguration();
    expressionConfiguration4.setName("efgh");
    expressionConfiguration4.setOutputColumn("A4");
    request2.setExpressions(new HashSet<>(Arrays.asList(expressionConfiguration3, expressionConfiguration4)));
    request.setContextKey("context1");
    request2.setContextKey("context2");
    CqlEvaluationRequests evaluationRequests = new CqlEvaluationRequests();
    evaluationRequests.setEvaluations(Arrays.asList(request, request2));
    ConfigurableOutputColumnNameEncoder nameEncoder = ConfigurableOutputColumnNameEncoder.create(evaluationRequests, "|");
    assertEquals("A1", nameEncoder.getColumnName(request, "abcd"));
    assertEquals("A2", nameEncoder.getColumnName(request, "efgh"));
    assertEquals("A3", nameEncoder.getColumnName(request2, "abcd"));
    assertEquals("A4", nameEncoder.getColumnName(request2, "efgh"));
}
Also used : CqlEvaluationRequest(com.ibm.cohort.cql.evaluation.CqlEvaluationRequest) CqlExpressionConfiguration(com.ibm.cohort.cql.evaluation.CqlExpressionConfiguration) CqlEvaluationRequests(com.ibm.cohort.cql.evaluation.CqlEvaluationRequests) CqlLibraryDescriptor(com.ibm.cohort.cql.library.CqlLibraryDescriptor) Test(org.junit.Test)

Example 28 with CqlLibraryDescriptor

use of com.ibm.cohort.cql.library.CqlLibraryDescriptor in project quality-measure-and-cohort-service by Alvearie.

the class ConfigurableOutputColumnNameEncoderTest method testOutputColumnsRepeatedAcrossContexts.

@Test
public void testOutputColumnsRepeatedAcrossContexts() {
    CqlLibraryDescriptor libraryDescriptor1 = new CqlLibraryDescriptor();
    libraryDescriptor1.setLibraryId("lib1");
    CqlEvaluationRequest request = new CqlEvaluationRequest();
    request.setDescriptor(libraryDescriptor1);
    request.setId(1);
    CqlExpressionConfiguration expressionConfiguration1 = new CqlExpressionConfiguration();
    expressionConfiguration1.setName("abcd");
    expressionConfiguration1.setOutputColumn("A1");
    request.setExpressions(new HashSet<>(Collections.singletonList(expressionConfiguration1)));
    CqlLibraryDescriptor libraryDescriptor2 = new CqlLibraryDescriptor();
    libraryDescriptor2.setLibraryId("lib2");
    CqlEvaluationRequest request2 = new CqlEvaluationRequest();
    request2.setDescriptor(libraryDescriptor2);
    request2.setId(2);
    CqlExpressionConfiguration expressionConfiguration2 = new CqlExpressionConfiguration();
    expressionConfiguration2.setName("abcd");
    expressionConfiguration2.setOutputColumn("A1");
    request2.setExpressions(new HashSet<>(Collections.singletonList(expressionConfiguration2)));
    request.setContextKey("context1");
    request2.setContextKey("context2");
    CqlEvaluationRequests evaluationRequests = new CqlEvaluationRequests();
    evaluationRequests.setEvaluations(Arrays.asList(request, request2));
    ConfigurableOutputColumnNameEncoder nameEncoder = ConfigurableOutputColumnNameEncoder.create(evaluationRequests, "|");
    assertEquals("A1", nameEncoder.getColumnName(request, "abcd"));
    assertEquals("A1", nameEncoder.getColumnName(request2, "abcd"));
}
Also used : CqlEvaluationRequest(com.ibm.cohort.cql.evaluation.CqlEvaluationRequest) CqlExpressionConfiguration(com.ibm.cohort.cql.evaluation.CqlExpressionConfiguration) CqlEvaluationRequests(com.ibm.cohort.cql.evaluation.CqlEvaluationRequests) CqlLibraryDescriptor(com.ibm.cohort.cql.library.CqlLibraryDescriptor) Test(org.junit.Test)

Example 29 with CqlLibraryDescriptor

use of com.ibm.cohort.cql.library.CqlLibraryDescriptor in project quality-measure-and-cohort-service by Alvearie.

the class ContextColumnNameEncoderTest method testMultipleRequestsSameDefineSameDefaultOutputColumn.

@Test
public void testMultipleRequestsSameDefineSameDefaultOutputColumn() {
    String commonLibrary = "lib1";
    String commonDefine = "abcd";
    CqlLibraryDescriptor libraryDescriptor1 = new CqlLibraryDescriptor();
    libraryDescriptor1.setLibraryId(commonLibrary);
    CqlEvaluationRequest request = new CqlEvaluationRequest();
    request.setDescriptor(libraryDescriptor1);
    request.setId(1);
    CqlExpressionConfiguration expressionConfiguration1 = new CqlExpressionConfiguration();
    expressionConfiguration1.setName(commonDefine);
    request.setExpressions(new HashSet<>(Collections.singletonList(expressionConfiguration1)));
    CqlLibraryDescriptor libraryDescriptor2 = new CqlLibraryDescriptor();
    libraryDescriptor2.setLibraryId(commonLibrary);
    CqlEvaluationRequest request2 = new CqlEvaluationRequest();
    request2.setDescriptor(libraryDescriptor2);
    request2.setId(2);
    CqlExpressionConfiguration expressionConfiguration2 = new CqlExpressionConfiguration();
    expressionConfiguration2.setName(commonDefine);
    request2.setExpressions(new HashSet<>(Collections.singletonList(expressionConfiguration2)));
    IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, () -> ContextColumnNameEncoder.create(Arrays.asList(request, request2), "|"));
    assertTrue(ex.getMessage().contains("Duplicate outputColumn lib1|abcd defined"));
}
Also used : CqlEvaluationRequest(com.ibm.cohort.cql.evaluation.CqlEvaluationRequest) CqlExpressionConfiguration(com.ibm.cohort.cql.evaluation.CqlExpressionConfiguration) CqlLibraryDescriptor(com.ibm.cohort.cql.library.CqlLibraryDescriptor) Test(org.junit.Test)

Example 30 with CqlLibraryDescriptor

use of com.ibm.cohort.cql.library.CqlLibraryDescriptor in project quality-measure-and-cohort-service by Alvearie.

the class ContextColumnNameEncoderTest method testDefinesWithOutputColumns.

@Test
public void testDefinesWithOutputColumns() {
    CqlLibraryDescriptor libraryDescriptor = new CqlLibraryDescriptor();
    libraryDescriptor.setLibraryId("lib1");
    CqlEvaluationRequest request = new CqlEvaluationRequest();
    CqlExpressionConfiguration expressionConfiguration1 = new CqlExpressionConfiguration();
    expressionConfiguration1.setName("abcd");
    expressionConfiguration1.setOutputColumn("A1");
    CqlExpressionConfiguration expressionConfiguration2 = new CqlExpressionConfiguration();
    expressionConfiguration2.setName("efgh");
    expressionConfiguration2.setOutputColumn("A2");
    request.setExpressions(new HashSet<>(Arrays.asList(expressionConfiguration1, expressionConfiguration2)));
    request.setDescriptor(libraryDescriptor);
    request.setId(1);
    Map<String, String> defineToOutputNameMap = ContextColumnNameEncoder.getDefineToOutputNameMap(request, "|");
    Map<String, String> expectedResult = new HashMap<String, String>() {

        {
            put("abcd", "A1");
            put("efgh", "A2");
        }
    };
    TestCase.assertEquals(expectedResult, defineToOutputNameMap);
}
Also used : HashMap(java.util.HashMap) CqlEvaluationRequest(com.ibm.cohort.cql.evaluation.CqlEvaluationRequest) CqlExpressionConfiguration(com.ibm.cohort.cql.evaluation.CqlExpressionConfiguration) CqlLibraryDescriptor(com.ibm.cohort.cql.library.CqlLibraryDescriptor) Test(org.junit.Test)

Aggregations

CqlLibraryDescriptor (com.ibm.cohort.cql.library.CqlLibraryDescriptor)50 Test (org.junit.Test)34 CqlEvaluationRequest (com.ibm.cohort.cql.evaluation.CqlEvaluationRequest)19 CqlEvaluationRequests (com.ibm.cohort.cql.evaluation.CqlEvaluationRequests)18 CqlLibrary (com.ibm.cohort.cql.library.CqlLibrary)17 HashMap (java.util.HashMap)17 CqlExpressionConfiguration (com.ibm.cohort.cql.evaluation.CqlExpressionConfiguration)14 CqlLibraryProvider (com.ibm.cohort.cql.library.CqlLibraryProvider)14 ContextDefinitions (com.ibm.cohort.cql.spark.aggregation.ContextDefinitions)10 ClasspathCqlLibraryProvider (com.ibm.cohort.cql.library.ClasspathCqlLibraryProvider)9 CqlToElmTranslator (com.ibm.cohort.cql.translation.CqlToElmTranslator)8 File (java.io.File)8 IntegerParameter (com.ibm.cohort.cql.evaluation.parameters.IntegerParameter)7 Parameter (com.ibm.cohort.cql.evaluation.parameters.Parameter)7 DecimalParameter (com.ibm.cohort.cql.evaluation.parameters.DecimalParameter)5 StringParameter (com.ibm.cohort.cql.evaluation.parameters.StringParameter)5 Format (com.ibm.cohort.cql.library.Format)5 TranslatingCqlLibraryProvider (com.ibm.cohort.cql.translation.TranslatingCqlLibraryProvider)5 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)4 CqlDataProvider (com.ibm.cohort.cql.data.CqlDataProvider)4