Java Code Examples for org.opencds.cqf.cql.execution.Context#registerDataProvider()
The following examples show how to use
org.opencds.cqf.cql.execution.Context#registerDataProvider() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TestFhirLibrary.java From cql_engine with Apache License 2.0 | 6 votes |
public void TestCMS9v4_CQM() throws IOException, JAXBException { File xmlFile = new File(URLDecoder.decode(TestFhirLibrary.class.getResource("CMS9v4_CQM.xml").getFile(), "UTF-8")); Library library = CqlLibraryReader.read(xmlFile); Context context = new Context(library); FhirContext fhirContext = FhirContext.forDstu3(); Dstu3FhirModelResolver modelResolver = new Dstu3FhirModelResolver(); RestFhirRetrieveProvider retrieveProvider = new RestFhirRetrieveProvider(new SearchParameterResolver(fhirContext), fhirContext.newRestfulGenericClient("http://fhirtest.uhn.ca/baseDstu3")); CompositeDataProvider provider = new CompositeDataProvider(modelResolver, retrieveProvider); //BaseFhirDataProvider provider = new FhirDataProviderStu3().setEndpoint("http://fhirtest.uhn.ca/baseDstu3"); //BaseFhirDataProvider provider = new FhirDataProviderStu3().setEndpoint("http://fhir3.healthintersections.com.au/open/"); //BaseFhirDataProvider provider = new FhirDataProviderStu3().setEndpoint("http://wildfhir.aegis.net/fhir"); context.registerDataProvider("http://hl7.org/fhir", provider); Object result = context.resolveExpressionRef("Breastfeeding Intention Assessment").evaluate(context); assertThat(result, instanceOf(Iterable.class)); for (Object element : (Iterable)result) { assertThat(element, instanceOf(RiskAssessment.class)); } }
Example 2
Source File: TestFhirDataProviderDstu3.java From cql_engine with Apache License 2.0 | 5 votes |
public void testChoiceTypes() { Context context = new Context(library); context.registerDataProvider("http://hl7.org/fhir", dstu3Provider); Object result = context.resolveExpressionRef("testChoiceTypes").getExpression().evaluate(context); Assert.assertTrue(result != null); }
Example 3
Source File: CqlExecutionProvider.java From cqf-ruler with Apache License 2.0 | 5 votes |
private Context setupContext(DomainResource instance, String patientId, LibraryLoader libraryLoader, org.cqframework.cql.elm.execution.Library library) { // Provide the instance as the value of the '%context' parameter, as well as the // value of a parameter named the same as the resource // This enables expressions to access the resource by root, as well as through // the %context attribute Context context = new Context(library); context.setParameter(null, instance.fhirType(), instance); context.setParameter(null, "%context", instance); context.setExpressionCaching(true); context.registerLibraryLoader(libraryLoader); context.setContextValue("Patient", patientId); context.registerDataProvider("http://hl7.org/fhir", this.providerFactory.createDataProvider("FHIR", "4.0.0")); return context; }
Example 4
Source File: CqlExecutionProvider.java From cqf-ruler with Apache License 2.0 | 5 votes |
public Object evaluateInContext(DomainResource instance, String cql, String patientId) { Iterable<Reference> libraries = getLibraryReferences(instance); // Provide the instance as the value of the '%context' parameter, as well as the // value of a parameter named the same as the resource // This enables expressions to access the resource by root, as well as through // the %context attribute String source = String.format( "library LocalLibrary using FHIR version '3.0.0' include FHIRHelpers version '3.0.0' called FHIRHelpers %s parameter %s %s parameter \"%%context\" %s define Expression: %s", buildIncludes(libraries), instance.fhirType(), instance.fhirType(), instance.fhirType(), cql); // String source = String.format("library LocalLibrary using FHIR version '1.8' // include FHIRHelpers version '1.8' called FHIRHelpers %s parameter %s %s // parameter \"%%context\" %s define Expression: %s", // buildIncludes(libraries), instance.fhirType(), instance.fhirType(), // instance.fhirType(), cql); LibraryLoader libraryLoader = LibraryHelper.createLibraryLoader(this.libraryResolutionProvider); org.cqframework.cql.elm.execution.Library library = TranslatorHelper.translateLibrary(source, libraryLoader.getLibraryManager(), libraryLoader.getModelManager()); Context context = new Context(library); context.setParameter(null, instance.fhirType(), instance); context.setParameter(null, "%context", instance); context.setExpressionCaching(true); context.registerLibraryLoader(libraryLoader); context.setContextValue("Patient", patientId); context.registerDataProvider("http://hl7.org/fhir", this.providerFactory.createDataProvider("FHIR", "3.0.0")); return context.resolveExpressionRef("Expression").evaluate(context); }
Example 5
Source File: TestFhirDataProviderDstu2.java From cql_engine with Apache License 2.0 | 5 votes |
@BeforeMethod public void before() { context = new Context(library); context.registerDataProvider("http://hl7.org/fhir", dstu2Provider); //BaseFhirDataProvider provider = new FhirDataProviderDstu2().setEndpoint("http://fhirtest.uhn.ca/baseDstu2"); // FhirDataProviderDstu2 primitiveProvider = new FhirDataProviderDstu2().withEndpoint("http://fhirtest.uhn.ca/baseDstu2").withPackageName("ca.uhn.fhir.model.primitive"); // context.registerDataProvider("http://hl7.org/fhir", primitiveProvider); // FhirDataProviderDstu2 compositeProvider = new FhirDataProviderDstu2().withEndpoint("http://fhirtest.uhn.ca/baseDstu2").withPackageName("ca.uhn.fhir.model.dstu2.composite"); // context.registerDataProvider("http://hl7.org/fhir", compositeProvider); }
Example 6
Source File: TestFhirDataProviderDstu3.java From cql_engine with Apache License 2.0 | 5 votes |
@Test public void testFhirObjectEquivalent() { Context context = new Context(library); context.registerDataProvider("http://hl7.org/fhir", dstu3Provider); Object result = context.resolveExpressionRef("testFhirObjectEquivalent").getExpression().evaluate(context); Assert.assertTrue((Boolean) result); }
Example 7
Source File: TestFhirDataProviderDstu3.java From cql_engine with Apache License 2.0 | 5 votes |
@Test public void testFhirObjectEqual() { Context context = new Context(library); context.registerDataProvider("http://hl7.org/fhir", dstu3Provider); Object result = context.resolveExpressionRef("testFhirObjectEqual").getExpression().evaluate(context); Assert.assertTrue((Boolean) result); }
Example 8
Source File: TestFhirDataProviderDstu3.java From cql_engine with Apache License 2.0 | 5 votes |
public void testDateType() { Context context = new Context(library); context.registerDataProvider("http://hl7.org/fhir", dstu3Provider); context.setContextValue("Patient", "Patient-12214"); Object result = context.resolveExpressionRef("testDateType").getExpression().evaluate(context); Assert.assertTrue(result != null); }
Example 9
Source File: TestFhirExecution.java From cql_engine with Apache License 2.0 | 5 votes |
public void testMonthFrom() { Context context = new Context(library); context.registerDataProvider("http://hl7.org/fhir", dstu3Provider); context.setParameter(null, "MAXYEAR", 2014); Object result = context.resolveExpressionRef("testMonthFrom").getExpression().evaluate(context); Assert.assertTrue(result != null); }
Example 10
Source File: TestFhirPath.java From cql_engine with Apache License 2.0 | 5 votes |
public void testFhirHelpersDstu2() throws UcumException { String cql = getStringFromResourceStream("Dstu2/TestFHIRHelpersDstu2.cql"); Library library = translate(cql); Context context = new Context(library); context.registerLibraryLoader(getLibraryLoader()); Dstu2FhirModelResolver modelResolver = new Dstu2FhirModelResolver(); RestFhirRetrieveProvider retrieveProvider = new RestFhirRetrieveProvider( new SearchParameterResolver(fhirContext), FhirContext.forDstu2().newRestfulGenericClient("")); CompositeDataProvider provider = new CompositeDataProvider(modelResolver, retrieveProvider); //BaseFhirDataProvider provider = new FhirDataProviderDstu2(); context.registerDataProvider("http://hl7.org/fhir", provider); Object result = context.resolveExpressionRef("TestPeriodToInterval").getExpression().evaluate(context); // TODO - millis shouldn't be populated - issue with DateTime.fromJavaDate(Date date) // Assert.assertEquals(((DateTime)((Interval) result).getStart()).getPartial(), new Partial(DateTime.getFields(7), new int[] {2017, 5, 6, 18, 8, 0, 0})); // Assert.assertEquals(((DateTime)((Interval) result).getEnd()).getPartial(), new Partial(DateTime.getFields(7), new int[] {2017, 5, 6, 19, 8, 0, 0})); result = context.resolveExpressionRef("TestToQuantity").getExpression().evaluate(context); result = context.resolveExpressionRef("TestRangeToInterval").getExpression().evaluate(context); result = context.resolveExpressionRef("TestToCode").getExpression().evaluate(context); result = context.resolveExpressionRef("TestToConcept").getExpression().evaluate(context); result = context.resolveExpressionRef("TestToString").getExpression().evaluate(context); result = context.resolveExpressionRef("TestRequestStatusToString").getExpression().evaluate(context); result = context.resolveExpressionRef("TestToDateTime").getExpression().evaluate(context); result = context.resolveExpressionRef("TestToTime").getExpression().evaluate(context); result = context.resolveExpressionRef("TestToInteger").getExpression().evaluate(context); result = context.resolveExpressionRef("TestToDecimal").getExpression().evaluate(context); result = context.resolveExpressionRef("TestToBoolean").getExpression().evaluate(context); }
Example 11
Source File: TestFhirPath.java From cql_engine with Apache License 2.0 | 5 votes |
@Test public void testFhirHelpersStu3() throws UcumException { String cql = getStringFromResourceStream("stu3/TestFHIRHelpers.cql"); Library library = translate(cql); Context context = new Context(library); context.registerLibraryLoader(getLibraryLoader()); Dstu3FhirModelResolver modelResolver = new Dstu3FhirModelResolver(); FhirContext fhirContext = FhirContext.forDstu3(); RestFhirRetrieveProvider retrieveProvider = new RestFhirRetrieveProvider(new SearchParameterResolver(fhirContext), fhirContext.newRestfulGenericClient("http://fhirtest.uhn.ca/baseDstu3")); CompositeDataProvider provider = new CompositeDataProvider(modelResolver, retrieveProvider); // BaseFhirDataProvider provider = new // FhirDataProviderStu3().setEndpoint("http://fhirtest.uhn.ca/baseDstu3"); context.registerDataProvider("http://hl7.org/fhir", provider); Object result = context.resolveExpressionRef("TestPeriodToInterval").getExpression().evaluate(context); // TODO - fix // Assert.assertEquals(((DateTime)((Interval) result).getStart()).getPartial(), // new Partial(DateTime.getFields(6), new int[] {2017, 5, 6, 18, 8, 0})); // Assert.assertEquals(((DateTime)((Interval) result).getEnd()).getPartial(), // new Partial(DateTime.getFields(6), new int[] {2017, 5, 6, 19, 8, 0})); result = context.resolveExpressionRef("TestToQuantity").getExpression().evaluate(context); // TODO: ModelInfo bug. Not aware of SimpleQuantity result = context.resolveExpressionRef("TestRangeToInterval").getExpression().evaluate(context); result = context.resolveExpressionRef("TestToCode").getExpression().evaluate(context); result = context.resolveExpressionRef("TestToConcept").getExpression().evaluate(context); result = context.resolveExpressionRef("TestToString").getExpression().evaluate(context); result = context.resolveExpressionRef("TestRequestStatusToString").getExpression().evaluate(context); result = context.resolveExpressionRef("TestToDateTime").getExpression().evaluate(context); result = context.resolveExpressionRef("TestToTime").getExpression().evaluate(context); result = context.resolveExpressionRef("TestToInteger").getExpression().evaluate(context); result = context.resolveExpressionRef("TestToDecimal").getExpression().evaluate(context); result = context.resolveExpressionRef("TestToBoolean").getExpression().evaluate(context); }
Example 12
Source File: TestFhirExecution.java From cql_engine with Apache License 2.0 | 4 votes |
public void testIdResolution() { Context context = new Context(library); context.registerDataProvider("http://hl7.org/fhir", dstu3Provider); Object result = context.resolveExpressionRef("Resource Id").getExpression().evaluate(context); Assert.assertTrue(result != null); }
Example 13
Source File: TestFhirPath.java From cql_engine with Apache License 2.0 | 4 votes |
private void runStu3Test(org.hl7.fhirpath.tests.Test test) throws UcumException { String resourceFilePath = "stu3/input/" + test.getInputfile(); org.hl7.fhir.dstu3.model.Resource resource = loadResourceFile(resourceFilePath); String cql = String.format( "library TestFHIRPath using FHIR version '3.0.0' include FHIRHelpers version '3.0.0' called FHIRHelpers parameter %s %s define Test: %s", resource.fhirType(), resource.fhirType(), test.getExpression().getValue()); Library library = null; // If the test expression is invalid, expect an error during translation and // fail if we don't get one boolean isInvalid = test.getExpression().isInvalid() != null && test.getExpression().isInvalid(); if (isInvalid) { boolean testPassed = false; try { library = translate(cql); } catch (Exception e) { testPassed = true; } if (!testPassed) { throw new RuntimeException(String.format("Expected exception not thrown for test %s.", test.getName())); } } else { library = translate(cql); Context context = new Context(library); context.registerLibraryLoader(getLibraryLoader()); context.registerDataProvider("http://hl7.org/fhir", provider); context.setParameter(null, resource.fhirType(), resource); Object result = context.resolveExpressionRef("Test").evaluate(context); Iterable<Object> actualResults; if (result instanceof Iterable) { actualResults = (Iterable<Object>) result; } else { List results = new ArrayList<>(); results.add(result); actualResults = results; } Iterable<Object> expectedResults = loadExpectedResults(test); Iterator<Object> actualResultsIterator = actualResults.iterator(); for (Object expectedResult : expectedResults) { if (actualResultsIterator.hasNext()) { Object actualResult = actualResultsIterator.next(); Boolean comparison = compareResults(expectedResult, actualResult); if (comparison == null || !comparison) { throw new RuntimeException("Actual result is not equal to expected result."); } } else { throw new RuntimeException("Actual result is not equal to expected result."); } } } }
Example 14
Source File: TestFhirExecution.java From cql_engine with Apache License 2.0 | 4 votes |
public void testMultisourceQueryCreatingDatePeriod() { Context context = new Context(library); context.registerDataProvider("http://hl7.org/fhir", dstu3Provider); Object result = context.resolveExpressionRef("Immunizations in range").getExpression().evaluate(context); Assert.assertTrue(result != null); }
Example 15
Source File: TestFhirExecution.java From cql_engine with Apache License 2.0 | 4 votes |
public void testCoalesce() { Context context = new Context(library); context.registerDataProvider("http://hl7.org/fhir", dstu3Provider); Object result = context.resolveExpressionRef("testCoalesce").getExpression().evaluate(context); Assert.assertTrue((Integer)((List) result).get(0) == 72); }
Example 16
Source File: TestFhirDataProviderDstu3.java From cql_engine with Apache License 2.0 | 4 votes |
public void testContained() { String patient = "{ \n" + " \"resourceType\":\"Patient\",\n" + " \"id\":\"81ee6581-02b9-44de-b026-7401bf36643a\",\n" + " \"meta\":{ \n" + " \"profile\":[ \n" + " \"http://hl7.org/fhir/profiles/Patient\"\n" + " ]\n" + " },\n" + " \"birthDate\":\"2012-01-01\"\n" + " }"; fhirClient.update().resource(patient).withId("81ee6581-02b9-44de-b026-7401bf36643a").execute(); String condition = "{ \n" + " \"resourceType\":\"Condition\",\n" + " \"id\":\"77d90968-1965-4574-aa34-19d7d1483d8a\",\n" + " \"contained\":[ \n" + " { \n" + " \"resourceType\":\"Provenance\",\n" + " \"id\":\"c76ceb3b-ff93-4d4a-ae1f-83b78ce39228\",\n" + " \"target\":[ \n" + " { \n" + " \"reference\":\"Condition/77d90968-1965-4574-aa34-19d7d1483d8a\"\n" + " }\n" + " ],\n" + " \"entity\":[ \n" + " { \n" + " \"role\":\"source\",\n" + " \"whatReference\":{ \n" + " \"reference\":\"Claim/920013f1-da9b-42ec-89ec-b50069a7aa5c\"\n" + " }\n" + " }\n" + " ]\n" + " }\n" + " ],\n" + " \"clinicalStatus\":\"active\",\n" + " \"verificationStatus\":\"confirmed\",\n" + " \"code\":{ \n" + " \"coding\":[ \n" + " { \n" + " \"system\":\"ICD-10-CM\",\n" + " \"code\":\"Z00.00\",\n" + " \"display\":\"HEDIS2019_Ambulatory_Visits_ValueSets\"\n" + " }\n" + " ]\n" + " },\n" + " \"subject\":{ \n" + " \"reference\":\"Patient/81ee6581-02b9-44de-b026-7401bf36643a\"\n" + " },\n" + " \"onsetDateTime\":\"2018-01-01\", \n" + " \"evidence\":[ \n" + " { \n" + " \"detail\":{ \n" + " \"reference\": \"#c76ceb3b-ff93-4d4a-ae1f-83b78ce39228\" \n" + " } \n" + " } \n" + " ] \n" + " }"; fhirClient.update().resource(condition).withId("77d90968-1965-4574-aa34-19d7d1483d8a").execute(); Context context = new Context(library); dstu3RetrieveProvider.setTerminologyProvider(new Dstu3FhirTerminologyProvider().setEndpoint("http://measure.eval.kanvix.com/cqf-ruler/baseDstu3", false)); //dstu3Provider.setTerminologyProvider(new FhirTerminologyProvider().setEndpoint("http://measure.eval.kanvix.com/cqf-ruler/baseDstu3", false)); context.registerDataProvider("http://hl7.org/fhir", dstu3Provider); context.enterContext("Patient"); context.setContextValue("Patient", "81ee6581-02b9-44de-b026-7401bf36643a"); Object result = context.resolveExpressionRef("GetProvenance").getExpression().evaluate(context); Assert.assertTrue(result instanceof List && ((List) result).size() == 1); }
Example 17
Source File: MeasureEvaluationSeed.java From cqf-ruler with Apache License 2.0 | 4 votes |
public void setup( Measure measure, String periodStart, String periodEnd, String productLine, String source, String user, String pass) { this.measure = measure; LibraryHelper.loadLibraries(measure, this.libraryLoader, this.libraryResourceProvider); // resolve primary library Library library = LibraryHelper.resolvePrimaryLibrary(measure, libraryLoader, this.libraryResourceProvider); // resolve execution context context = new Context(library); context.registerLibraryLoader(libraryLoader); List<Triple<String,String,String>> usingDefs = UsingHelper.getUsingUrlAndVersion(library.getUsings()); if (usingDefs.size() > 1) { throw new IllegalArgumentException("Evaluation of Measure using multiple Models is not supported at this time."); } // If there are no Usings, there is probably not any place the Terminology // actually used so I think the assumption that at least one provider exists is ok. TerminologyProvider terminologyProvider = null; if (usingDefs.size() > 0) { // Creates a terminology provider based on the first using statement. This assumes the terminology // server matches the FHIR version of the CQL. terminologyProvider = this.providerFactory.createTerminologyProvider( usingDefs.get(0).getLeft(), usingDefs.get(0).getMiddle(), source, user, pass); context.registerTerminologyProvider(terminologyProvider); } for (Triple<String,String,String> def : usingDefs) { this.dataProvider = this.providerFactory.createDataProvider(def.getLeft(), def.getMiddle(), terminologyProvider); context.registerDataProvider( def.getRight(), dataProvider); } // resolve the measurement period measurementPeriod = new Interval(DateHelper.resolveRequestDate(periodStart, true), true, DateHelper.resolveRequestDate(periodEnd, false), true); context.setParameter(null, "Measurement Period", new Interval(DateTime.fromJavaDate((Date) measurementPeriod.getStart()), true, DateTime.fromJavaDate((Date) measurementPeriod.getEnd()), true)); if (productLine != null) { context.setParameter(null, "Product Line", productLine); } context.setExpressionCaching(true); }
Example 18
Source File: MeasureEvaluationSeed.java From cqf-ruler with Apache License 2.0 | 4 votes |
public void setup( Measure measure, String periodStart, String periodEnd, String productLine, String source, String user, String pass) { this.measure = measure; LibraryHelper.loadLibraries(measure, this.libraryLoader, this.libraryResourceProvider); // resolve primary library Library library = LibraryHelper.resolvePrimaryLibrary(measure, libraryLoader, this.libraryResourceProvider); // resolve execution context context = new Context(library); context.registerLibraryLoader(libraryLoader); List<Triple<String,String,String>> usingDefs = UsingHelper.getUsingUrlAndVersion(library.getUsings()); if (usingDefs.size() > 1) { throw new IllegalArgumentException("Evaluation of Measure using multiple Models is not supported at this time."); } // If there are no Usings, there is probably not any place the Terminology // actually used so I think the assumption that at least one provider exists is ok. TerminologyProvider terminologyProvider = null; if (usingDefs.size() > 0) { // Creates a terminology provider based on the first using statement. This assumes the terminology // server matches the FHIR version of the CQL. terminologyProvider = this.providerFactory.createTerminologyProvider( usingDefs.get(0).getLeft(), usingDefs.get(0).getMiddle(), source, user, pass); context.registerTerminologyProvider(terminologyProvider); } for (Triple<String,String,String> def : usingDefs) { this.dataProvider = this.providerFactory.createDataProvider(def.getLeft(), def.getMiddle(), terminologyProvider); context.registerDataProvider( def.getRight(), dataProvider); } // resolve the measurement period measurementPeriod = new Interval(DateHelper.resolveRequestDate(periodStart, true), true, DateHelper.resolveRequestDate(periodEnd, false), true); context.setParameter(null, "Measurement Period", new Interval(DateTime.fromJavaDate((Date) measurementPeriod.getStart()), true, DateTime.fromJavaDate((Date) measurementPeriod.getEnd()), true)); if (productLine != null) { context.setParameter(null, "Product Line", productLine); } context.setExpressionCaching(true); }
Example 19
Source File: TestFhirPath.java From cql_engine with Apache License 2.0 | 4 votes |
private void runR4Test(org.hl7.fhirpath.tests.Test test) throws UcumException { String resourceFilePath = "r4/input/" + test.getInputfile(); org.hl7.fhir.r4.model.Resource resource = loadResourceFileR4(resourceFilePath); String cql = String.format( "library TestFHIRPath using FHIR version '4.0.0' include FHIRHelpers version '4.0.0' called FHIRHelpers parameter %s %s define Test: %s", resource.fhirType(), resource.fhirType(), test.getExpression().getValue()); Library library = null; // If the test expression is invalid, expect an error during translation and // fail if we don't get one boolean isInvalid = test.getExpression().isInvalid() != null && test.getExpression().isInvalid(); if (isInvalid) { boolean testPassed = false; try { library = translate(cql); } catch (Exception e) { testPassed = true; } if (!testPassed) { throw new RuntimeException(String.format("Expected exception not thrown for test %s.", test.getName())); } } else { library = translate(cql); Context context = new Context(library); context.registerLibraryLoader(getLibraryLoader()); context.registerDataProvider("http://hl7.org/fhir", providerR4); context.setParameter(null, resource.fhirType(), resource); Object result = context.resolveExpressionRef("Test").evaluate(context); Iterable<Object> actualResults; if (result instanceof Iterable) { actualResults = (Iterable<Object>) result; } else { List results = new ArrayList<>(); results.add(result); actualResults = results; } Iterable<Object> expectedResults = loadExpectedResults(test); Iterator<Object> actualResultsIterator = actualResults.iterator(); for (Object expectedResult : expectedResults) { if (actualResultsIterator.hasNext()) { Object actualResult = actualResultsIterator.next(); Boolean comparison = compareResults(expectedResult, actualResult); if (comparison == null || !comparison) { throw new RuntimeException("Actual result is not equal to expected result."); } } else { throw new RuntimeException("Actual result is not equal to expected result."); } } } }