Java Code Examples for org.opencds.cqf.cql.execution.Context#registerLibraryLoader()
The following examples show how to use
org.opencds.cqf.cql.execution.Context#registerLibraryLoader() .
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: 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 2
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 3
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 4
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 5
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 6
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."); } } } }
Example 7
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 8
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); }