Java Code Examples for org.cqframework.cql.cql2elm.CqlTranslator#toXml()

The following examples show how to use org.cqframework.cql.cql2elm.CqlTranslator#toXml() . 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
/**
 * Evaluate the given expression, within the context of the given Person and timestamp.
 * The given expression will be wrapped in CQL and evaluated to produce, ideally, a Number.
 * Examples:
 *  - In: "10 + 3", Out: (Integer)13
 *  - In: "25 / 2", Out: (Double)12.5
 *  - In: "#{age} / 3", Person{age = 27}, Out: (Integer) 9
 * 
 * @param expression "CQL-lite" expression, with attribute references wrapped in "#{ attr }"
 * @param person Person to evaluate expression against.
 * @param time Timestamp
 * @return result of the expression
 */

private String cqlToElm(String cql) {
  CqlTranslator translator = CqlTranslator.fromText(cql, modelManager, libraryManager);
  
  if (translator.getErrors().size() > 0) {
    throw translator.getErrors().get(0);
  }

  String elm = translator.toXml();

  if (translator.getErrors().size() > 0) {
    throw translator.getErrors().get(0);
  }

  return elm;
}
 
Example 2
Source File: LibraryOperationsProvider.java    From cqf-ruler with Apache License 2.0 6 votes vote down vote up
@Operation(name = "$get-elm", idempotent = true, type = Library.class)
public Parameters getElm(@IdParam IdType theId, @OptionalParam(name="format") String format) {
    Library theResource = this.libraryResourceProvider.getDao().read(theId);
    // this.formatCql(theResource);

    ModelManager modelManager = this.getModelManager();
    LibraryManager libraryManager = this.getLibraryManager(modelManager);


    String elm = "";
    CqlTranslator translator = this.dataRequirementsProvider.getTranslator(theResource, libraryManager, modelManager);
    if (translator != null) {
        if (format.equals("json")) {
            elm = translator.toJson();
        }
        else {
            elm = translator.toXml();
        }
    }
    Parameters p = new Parameters();
    p.addParameter().setValue(new StringType(elm));
    return p;
}
 
Example 3
Source File: LibraryOperationsProvider.java    From cqf-ruler with Apache License 2.0 6 votes vote down vote up
@Operation(name = "$get-elm", idempotent = true, type = Library.class)
public Parameters getElm(@IdParam IdType theId, @OptionalParam(name="format") String format) {
    Library theResource = this.libraryResourceProvider.getDao().read(theId);
    // this.formatCql(theResource);

    ModelManager modelManager = this.getModelManager();
    LibraryManager libraryManager = this.getLibraryManager(modelManager);

    String elm = "";
    CqlTranslator translator = this.dataRequirementsProvider.getTranslator(theResource, libraryManager, modelManager);
    if (translator != null) {
        if (format.equals("json")) {
            elm = translator.toJson();
        }
        else {
            elm = translator.toXml();
        }
    }
    Parameters p = new Parameters();
    p.addParameter().setValue(new StringType(elm));
    return p;
}
 
Example 4
Source File: CqlTestSuite.java    From cql_engine with Apache License 2.0 5 votes vote down vote up
private Library translate(String file)  throws UcumException, JAXBException, IOException {
    ModelManager modelManager = new ModelManager();
    LibraryManager libraryManager = new LibraryManager(modelManager);
    UcumService ucumService = new UcumEssenceService(UcumEssenceService.class.getResourceAsStream("/ucum-essence.xml"));

    File cqlFile = new File(URLDecoder.decode(this.getClass().getResource(file).getFile(), "UTF-8"));

    CqlTranslator translator = CqlTranslator.fromFile(cqlFile, modelManager, libraryManager, ucumService);

    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));

    String xml = translator.toXml();

    return CqlLibraryReader.read(new StringReader(xml));
}
 
Example 5
Source File: DataRequirementsProvider.java    From cqf-ruler with Apache License 2.0 5 votes vote down vote up
public void ensureElm(org.hl7.fhir.dstu3.model.Library library, CqlTranslator translator) {

        library.getContent().removeIf(a -> a.getContentType().equals("application/elm+xml"));
        String xml = translator.toXml();
        Attachment elm = new Attachment();
        elm.setContentType("application/elm+xml");
        elm.setData(xml.getBytes());
        library.getContent().add(elm);
    }
 
Example 6
Source File: DataRequirementsProvider.java    From cqf-ruler with Apache License 2.0 5 votes vote down vote up
public void ensureElm(org.hl7.fhir.r4.model.Library library, CqlTranslator translator) {

        library.getContent().removeIf(a -> a.getContentType().equals("application/elm+xml"));
        String xml = translator.toXml();
        Attachment elm = new Attachment();
        elm.setContentType("application/elm+xml");
        elm.setData(xml.getBytes());
        library.getContent().add(elm);
    }
 
Example 7
Source File: CqlExecutionTestBase.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) {
        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);
            options.add(CqlTranslator.Options.EnableAnnotations);
            options.add(CqlTranslator.Options.EnableLocators);
            CqlTranslator translator = CqlTranslator.fromFile(cqlFile, getModelManager(), getLibraryManager(), 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();

            String xml = translator.toXml();

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

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