Search in sources :

Example 11 with Context

use of org.opencds.cqf.cql.engine.execution.Context in project quality-measure-and-cohort-service by Alvearie.

the class CqlContextFactory method createContext.

/**
 * Initialize a CQL context from the values associated with the provided
 * CQL Context Key. This encapsulates the set of initializations that are
 * static from run to run.
 *
 * @param contextKey container for stable context settings
 * @return initialized CQL Context object
 * @throws CqlLibraryDeserializationException if the specified library cannot be loaded
 */
protected Context createContext(ContextCacheKey contextKey) throws CqlLibraryDeserializationException {
    LibraryLoader libraryLoader = new ProviderBasedLibraryLoader(contextKey.libraryProvider);
    VersionedIdentifier vid = new VersionedIdentifier().withId(contextKey.topLevelLibraryIdentifier.getId()).withVersion(contextKey.topLevelLibraryIdentifier.getVersion());
    Library entryPoint = libraryLoader.load(vid);
    Context cqlContext;
    if (contextKey.evaluationDateTime != null) {
        cqlContext = new Context(entryPoint, contextKey.evaluationDateTime, new CqlSystemDataProvider());
    } else {
        cqlContext = new Context(entryPoint, new CqlSystemDataProvider());
    }
    cqlContext.registerExternalFunctionProvider(vid, this.externalFunctionProvider);
    registerExternalIncludes(cqlContext, cqlContext.getCurrentLibrary());
    cqlContext.registerLibraryLoader(libraryLoader);
    cqlContext.registerTerminologyProvider(contextKey.terminologyProvider);
    if (contextKey.parameters != null) {
        Library library = cqlContext.getCurrentLibrary();
        for (Map.Entry<String, Parameter> entry : contextKey.parameters.entrySet()) {
            Object cqlValue = entry.getValue().toCqlType();
            cqlContext.setParameter(library.getLocalId(), entry.getKey(), cqlValue);
        }
        if (library.getIncludes() != null && library.getIncludes().getDef() != null) {
            for (IncludeDef def : library.getIncludes().getDef()) {
                String name = def.getLocalIdentifier();
                for (Map.Entry<String, Parameter> parameterValue : contextKey.parameters.entrySet()) {
                    Object cqlValue = parameterValue.getValue().toCqlType();
                    cqlContext.setParameter(name, parameterValue.getKey(), cqlValue);
                }
            }
        }
    }
    return cqlContext;
}
Also used : Context(org.opencds.cqf.cql.engine.execution.Context) VersionedIdentifier(org.cqframework.cql.elm.execution.VersionedIdentifier) CqlVersionedIdentifier(com.ibm.cohort.cql.library.CqlVersionedIdentifier) ProviderBasedLibraryLoader(com.ibm.cohort.cql.library.ProviderBasedLibraryLoader) IncludeDef(org.cqframework.cql.elm.execution.IncludeDef) Parameter(com.ibm.cohort.cql.evaluation.parameters.Parameter) Library(org.cqframework.cql.elm.execution.Library) LibraryLoader(org.opencds.cqf.cql.engine.execution.LibraryLoader) ProviderBasedLibraryLoader(com.ibm.cohort.cql.library.ProviderBasedLibraryLoader) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) Map(java.util.Map) DebugMap(org.opencds.cqf.cql.engine.debug.DebugMap) CqlSystemDataProvider(com.ibm.cohort.cql.data.CqlSystemDataProvider)

Example 12 with Context

use of org.opencds.cqf.cql.engine.execution.Context in project quality-measure-and-cohort-service by Alvearie.

the class CqlContextFactoryTest method testCreateContextSuccess.

@Test
public void testCreateContextSuccess() {
    boolean expectedDebug = true;
    ZonedDateTime expectedEvaluationDateTime = ZonedDateTime.of(LocalDateTime.of(2001, 10, 2, 11, 12, 13), ZoneId.of("America/New_York"));
    PriorityCqlLibraryProvider libraryProvider = new PriorityCqlLibraryProvider(new ClasspathCqlLibraryProvider("cql", ClasspathCqlLibraryProvider.FHIR_HELPERS_CLASSPATH));
    CqlToElmTranslator translator = new CqlToElmTranslator();
    TranslatingCqlLibraryProvider translatingProvider = new TranslatingCqlLibraryProvider(libraryProvider, translator);
    CqlVersionedIdentifier topLevelLibraryIdentifier = new CqlVersionedIdentifier("MyCQL", "1.0.0");
    CqlTerminologyProvider terminologyProvider = new UnsupportedTerminologyProvider();
    CqlDataProvider dataProvider = mock(CqlDataProvider.class);
    Pair<String, String> contextData = Pair.of("Patient", "123");
    Map<String, Parameter> expectedParams = new HashMap<>();
    expectedParams.put("P1", new StringParameter("MyString"));
    expectedParams.put("P2", new IntegerParameter(10));
    CqlContextFactory cqlContextFactory = spy(CqlContextFactory.class);
    Context context = cqlContextFactory.createContext(translatingProvider, topLevelLibraryIdentifier, terminologyProvider, dataProvider, expectedEvaluationDateTime, contextData, expectedParams, expectedDebug ? CqlDebug.DEBUG : CqlDebug.NONE);
    assertEquals(expectedDebug, context.getDebugMap().getIsLoggingEnabled());
    assertEquals(expectedEvaluationDateTime.toInstant(), context.getEvaluationDateTime().getDateTime().toZonedDateTime().toInstant());
    context.enterContext(contextData.getKey());
    assertEquals(contextData.getValue(), context.getCurrentContextValue());
    assertEquals(topLevelLibraryIdentifier.getId(), context.getCurrentLibrary().getIdentifier().getId());
    assertEquals(topLevelLibraryIdentifier.getVersion(), context.getCurrentLibrary().getIdentifier().getVersion());
    for (Map.Entry<String, Parameter> entry : expectedParams.entrySet()) {
        Object actualValue = context.resolveParameterRef(null, entry.getKey());
        assertEquals(entry.getValue().toCqlType(), actualValue);
    }
    // Once more just to check that caching is working. Using a different data provider because that is
    // how we will actually use it at runtime.
    CqlDataProvider dataProvider2 = mock(CqlDataProvider.class);
    cqlContextFactory.createContext(translatingProvider, topLevelLibraryIdentifier, terminologyProvider, dataProvider2, expectedEvaluationDateTime, contextData, expectedParams, expectedDebug ? CqlDebug.DEBUG : CqlDebug.NONE);
    verify(cqlContextFactory, times(1)).createContext(any(ContextCacheKey.class));
}
Also used : Context(org.opencds.cqf.cql.engine.execution.Context) UnsupportedTerminologyProvider(com.ibm.cohort.cql.terminology.UnsupportedTerminologyProvider) IntegerParameter(com.ibm.cohort.cql.evaluation.parameters.IntegerParameter) StringParameter(com.ibm.cohort.cql.evaluation.parameters.StringParameter) TranslatingCqlLibraryProvider(com.ibm.cohort.cql.translation.TranslatingCqlLibraryProvider) HashMap(java.util.HashMap) CqlToElmTranslator(com.ibm.cohort.cql.translation.CqlToElmTranslator) PriorityCqlLibraryProvider(com.ibm.cohort.cql.library.PriorityCqlLibraryProvider) ZonedDateTime(java.time.ZonedDateTime) IntegerParameter(com.ibm.cohort.cql.evaluation.parameters.IntegerParameter) StringParameter(com.ibm.cohort.cql.evaluation.parameters.StringParameter) Parameter(com.ibm.cohort.cql.evaluation.parameters.Parameter) ClasspathCqlLibraryProvider(com.ibm.cohort.cql.library.ClasspathCqlLibraryProvider) CqlTerminologyProvider(com.ibm.cohort.cql.terminology.CqlTerminologyProvider) ContextCacheKey(com.ibm.cohort.cql.evaluation.CqlContextFactory.ContextCacheKey) CqlDataProvider(com.ibm.cohort.cql.data.CqlDataProvider) HashMap(java.util.HashMap) Map(java.util.Map) CqlVersionedIdentifier(com.ibm.cohort.cql.library.CqlVersionedIdentifier) Test(org.junit.Test)

Example 13 with Context

use of org.opencds.cqf.cql.engine.execution.Context in project quality-measure-and-cohort-service by Alvearie.

the class ShortCircuitEvaluatorTest method testShortCircuitAnd.

@Test
public void testShortCircuitAnd() {
    Context context = new Context(library);
    verifyAnd(context, "TrueAndTrue", true, false);
    verifyAnd(context, "TrueAndFalse", false, false);
    verifyAnd(context, "TrueAndNull", null, false);
    verifyAnd(context, "FalseAndTrue", false, true);
    verifyAnd(context, "FalseAndFalse", false, true);
    verifyAnd(context, "FalseAndNull", false, true);
    verifyAnd(context, "NullAndTrue", null, false);
    verifyAnd(context, "NullAndFalse", false, false);
    verifyAnd(context, "NullAndNull", null, false);
}
Also used : Context(org.opencds.cqf.cql.engine.execution.Context) Test(org.junit.Test)

Aggregations

Context (org.opencds.cqf.cql.engine.execution.Context)13 VersionedIdentifier (org.cqframework.cql.elm.execution.VersionedIdentifier)6 LibraryLoader (org.opencds.cqf.cql.engine.execution.LibraryLoader)6 FhirContext (ca.uhn.fhir.context.FhirContext)4 TerminologyProvider (org.opencds.cqf.cql.engine.terminology.TerminologyProvider)4 Map (java.util.Map)3 Test (org.junit.Test)3 DataProvider (org.opencds.cqf.cql.engine.data.DataProvider)3 SystemRequestDetails (ca.uhn.fhir.jpa.partition.SystemRequestDetails)2 RequestDetails (ca.uhn.fhir.rest.api.server.RequestDetails)2 BaseServerResponseException (ca.uhn.fhir.rest.server.exceptions.BaseServerResponseException)2 InvalidRequestException (ca.uhn.fhir.rest.server.exceptions.InvalidRequestException)2 JsonParser (com.google.gson.JsonParser)2 CqlDataProvider (com.ibm.cohort.cql.data.CqlDataProvider)2 Parameter (com.ibm.cohort.cql.evaluation.parameters.Parameter)2 CqlVersionedIdentifier (com.ibm.cohort.cql.library.CqlVersionedIdentifier)2 CqlToElmTranslator (com.ibm.cohort.cql.translation.CqlToElmTranslator)2 CDMContext (com.ibm.cohort.engine.cqfruler.CDMContext)2 IOException (java.io.IOException)2 HashMap (java.util.HashMap)2