org.opencds.cqf.cql.execution.CqlLibraryReader Java Examples

The following examples show how to use org.opencds.cqf.cql.execution.CqlLibraryReader. 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: ExpressionProcessor.java    From synthea with Apache License 2.0 6 votes vote down vote up
/**
 * ExpressionProcessor constructor.
 * @param expression Expression to evaluate for each future set of parameters.
 * @param paramTypeMap Map of parameter names to their corresponding CQL types.
 */
public ExpressionProcessor(String expression, Map<String,String> paramTypeMap) {
  this.cqlParamMap = HashBiMap.create();
  this.paramTypeMap = paramTypeMap;
  
  String cleanExpression = replaceParameters(expression);
  String wrappedExpression = convertParameterizedExpressionToCql(cleanExpression);

  // Compile our constructed CQL expression into elm once for execution
  this.elm = cqlToElm(wrappedExpression);
  try {
    this.library = CqlLibraryReader.read(new ByteArrayInputStream(
        elm.getBytes(StandardCharsets.UTF_8)));
  } catch (IOException | JAXBException ex) {
    throw new RuntimeException(ex);
  }
  this.context = new Context(library);
  this.expression = expression;
}
 
Example #2
Source File: TestFhirLibrary.java    From cql_engine with Apache License 2.0 6 votes vote down vote up
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 #3
Source File: TranslatorHelper.java    From cqf-ruler with Apache License 2.0 5 votes vote down vote up
public static Library readLibrary(InputStream xmlStream) {
    try {
        return CqlLibraryReader.read(xmlStream);
    } catch (IOException | JAXBException e) {
        throw new IllegalArgumentException("Error encountered while reading ELM xml: " + e.getMessage());
    }
}
 
Example #4
Source File: FhirExecutionTestBase.java    From cql_engine with Apache License 2.0 4 votes vote down vote up
@BeforeMethod
public void beforeEachTestMethod() throws JAXBException, IOException, UcumException {
    String fileName = this.getClass().getSimpleName();
    library = libraries.get(fileName);
    if (library == null) {
        ModelManager modelManager = new ModelManager();
        LibraryManager libraryManager = new LibraryManager(modelManager);
        UcumService ucumService = new UcumEssenceService(UcumEssenceService.class.getResourceAsStream("/ucum-essence.xml"));
        try {
            File cqlFile = new File(URLDecoder.decode(this.getClass().getResource(fileName + ".cql").getFile(), "UTF-8"));

            ArrayList<CqlTranslator.Options> options = new ArrayList<>();
            options.add(CqlTranslator.Options.EnableDateRangeOptimization);
            CqlTranslator translator = CqlTranslator.fromFile(cqlFile, modelManager, libraryManager, ucumService, options.toArray(new CqlTranslator.Options[options.size()]));

            if (translator.getErrors().size() > 0) {
                System.err.println("Translation failed due to errors:");
                ArrayList<String> errors = new ArrayList<>();
                for (CqlTranslatorException error : translator.getErrors()) {
                    TrackBack tb = error.getLocator();
                    String lines = tb == null ? "[n/a]" : String.format("[%d:%d, %d:%d]",
                            tb.getStartLine(), tb.getStartChar(), tb.getEndLine(), tb.getEndChar());
                    System.err.printf("%s %s%n", lines, error.getMessage());
                    errors.add(lines + error.getMessage());
                }
                throw new IllegalArgumentException(errors.toString());
            }

            assertThat(translator.getErrors().size(), is(0));

            xmlFile = new File(cqlFile.getParent(), fileName + ".xml");
            xmlFile.createNewFile();

            PrintWriter pw = new PrintWriter(xmlFile, "UTF-8");
            pw.println(translator.toXml());
            pw.println();
            pw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        library = CqlLibraryReader.read(xmlFile);
        libraries.put(fileName, library);
    }
}