org.cqframework.cql.cql2elm.CqlTranslator Java Examples

The following examples show how to use org.cqframework.cql.cql2elm.CqlTranslator. 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: LibraryOperationsProvider.java    From cqf-ruler with Apache License 2.0 8 votes vote down vote up
@Operation(name = "$refresh-generated-content", type = Library.class)
public MethodOutcome refreshGeneratedContent(HttpServletRequest theRequest, RequestDetails theRequestDetails,
        @IdParam IdType theId) {
    Library theResource = this.libraryResourceProvider.getDao().read(theId);
    //this.formatCql(theResource);

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

    CqlTranslator translator = this.dataRequirementsProvider.getTranslator(theResource, libraryManager, modelManager);
    if (translator.getErrors().size() > 0) {
        throw new RuntimeException("Errors during library compilation.");
    }
    
    this.dataRequirementsProvider.ensureElm(theResource, translator);
    this.dataRequirementsProvider.ensureRelatedArtifacts(theResource, translator, this);
    this.dataRequirementsProvider.ensureDataRequirements(theResource, translator);

    Narrative n = this.narrativeProvider.getNarrative(this.libraryResourceProvider.getContext(), theResource);
    theResource.setText(n);

    return this.libraryResourceProvider.update(theRequest, theResource, theId,
            theRequestDetails.getConditionalUrl(RestOperationTypeEnum.UPDATE), theRequestDetails);
}
 
Example #2
Source File: LibraryBuilder.java    From clinical_quality_language with Apache License 2.0 6 votes vote down vote up
public void setTranslatorOptions(CqlTranslatorOptions options) {
    if (options == null) {
        throw new IllegalArgumentException("Options cannot be null");
    }

    this.options = options;
    if (options.getOptions().contains(CqlTranslator.Options.DisableListTraversal)) {
        this.listTraversal = false;
    }
    if (options.getOptions().contains(CqlTranslator.Options.DisableListDemotion)) {
        this.getConversionMap().disableListDemotion();
    }
    if (options.getOptions().contains(CqlTranslator.Options.DisableListPromotion)) {
        this.getConversionMap().disableListPromotion();
    }
    if (options.getOptions().contains(CqlTranslator.Options.EnableIntervalDemotion)) {
        this.getConversionMap().enableIntervalDemotion();
    }
    if (options.getOptions().contains(CqlTranslator.Options.EnableIntervalPromotion)) {
        this.getConversionMap().enableIntervalPromotion();
    }

    this.cqlToElmInfo.setTranslatorOptions(options.toString());
}
 
Example #3
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 #4
Source File: TranslatorHelper.java    From cqf-ruler with Apache License 2.0 6 votes vote down vote up
public static CqlTranslator getTranslator(InputStream cqlStream, LibraryManager libraryManager, ModelManager modelManager) {
    ArrayList<CqlTranslator.Options> options = new ArrayList<>();
    options.add(CqlTranslator.Options.EnableAnnotations);
    options.add(CqlTranslator.Options.EnableLocators);
    options.add(CqlTranslator.Options.DisableListDemotion);
    options.add(CqlTranslator.Options.DisableListPromotion);
    options.add(CqlTranslator.Options.DisableMethodInvocation);
    CqlTranslator translator;
    try {
        translator = CqlTranslator.fromStream(cqlStream, modelManager, libraryManager,
                options.toArray(new CqlTranslator.Options[options.size()]));
    } catch (IOException e) {
        throw new IllegalArgumentException(String.format("Errors occurred translating library: %s", e.getMessage()));
    }
    
    return translator;
}
 
Example #5
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 #6
Source File: DataRequirementsProvider.java    From cqf-ruler with Apache License 2.0 6 votes vote down vote up
public CqlTranslator getTranslator(org.hl7.fhir.dstu3.model.Library library, LibraryManager libraryManager, ModelManager modelManager) {
    Attachment cql = null;
    for (Attachment a : library.getContent()) {
        if (a.getContentType().equals("text/cql")) {
            cql = a;
            break;
        }
    }

    if (cql == null) {
        return null;
    }

    CqlTranslator translator = TranslatorHelper.getTranslator(
            new ByteArrayInputStream(Base64.getDecoder().decode(cql.getDataElement().getValueAsString())),
            libraryManager, modelManager);

    return translator;
}
 
Example #7
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 #8
Source File: DataRequirementsProvider.java    From cqf-ruler with Apache License 2.0 6 votes vote down vote up
public CqlTranslator getTranslator(org.hl7.fhir.r4.model.Library library, LibraryManager libraryManager, ModelManager modelManager) {
    Attachment cql = null;
    for (Attachment a : library.getContent()) {
        if (a.getContentType().equals("text/cql")) {
            cql = a;
            break;
        }
    }

    if (cql == null) {
        return null;
    }

    return TranslatorHelper.getTranslator(
            new ByteArrayInputStream(Base64.getDecoder().decode(cql.getDataElement().getValueAsString())),
            libraryManager, modelManager);
}
 
Example #9
Source File: CqlListOperatorsTest.java    From clinical_quality_language with Apache License 2.0 5 votes vote down vote up
@BeforeTest
public void setup() throws IOException {
    ModelManager modelManager = new ModelManager();
    CqlTranslator translator = CqlTranslator.fromStream(CqlListOperatorsTest.class.getResourceAsStream("../OperatorTests/CqlListOperators.cql"), modelManager, new LibraryManager(modelManager));
    assertThat(translator.getErrors().size(), is(0));
    Library library = translator.toELM();
    defs = new HashMap<>();
    if (library.getStatements() != null) {
        for (ExpressionDef def : library.getStatements().getDef()) {
            defs.put(def.getName(), def);
        }
    }
}
 
Example #10
Source File: QueryTest.java    From clinical_quality_language with Apache License 2.0 5 votes vote down vote up
@BeforeTest
public void setup() throws IOException {
    ModelManager modelManager = new ModelManager();
    CqlTranslator translator = CqlTranslator.fromStream(QueryTest.class.getResourceAsStream("../OperatorTests/Query.cql"), modelManager, new LibraryManager(modelManager));
    assertThat(translator.getErrors().size(), is(0));
    Library library = translator.toELM();
    defs = new HashMap<>();
    for (ExpressionDef def: library.getStatements().getDef()) {
        defs.put(def.getName(), def);
    }
}
 
Example #11
Source File: BaseTest.java    From clinical_quality_language with Apache License 2.0 5 votes vote down vote up
@Test
public void testRetrieveWithConcept() throws IOException {
    CqlTranslator translator = TestUtils.runSemanticTest("fhir/r4/TestRetrieveWithConcept.cql", 0);
    TranslatedLibrary library = translator.getTranslatedLibrary();
    ExpressionDef expressionDef = library.resolveExpressionRef("Test Tobacco Smoking Status");

    assertThat(expressionDef.getExpression(), instanceOf(Retrieve.class));
    Retrieve retrieve = (Retrieve)expressionDef.getExpression();
    assertThat(retrieve.getCodes(), instanceOf(ToList.class));
    ToList toList = (ToList)retrieve.getCodes();
    assertThat(toList.getOperand(), instanceOf(CodeRef.class));
}
 
Example #12
Source File: CqlIntervalOperatorsTest.java    From clinical_quality_language with Apache License 2.0 5 votes vote down vote up
@BeforeTest
public void setup() throws IOException {
    ModelManager modelManager = new ModelManager();
    CqlTranslator translator = CqlTranslator.fromStream(CqlIntervalOperatorsTest.class.getResourceAsStream("../OperatorTests/CqlIntervalOperators.cql"), modelManager, new LibraryManager(modelManager));
    assertThat(translator.getErrors().size(), is(0));
    Library library = translator.toELM();
    defs = new HashMap<>();
    if (library.getStatements() != null) {
        for (ExpressionDef def : library.getStatements().getDef()) {
            defs.put(def.getName(), def);
        }
    }
}
 
Example #13
Source File: TimeOperatorsTest.java    From clinical_quality_language with Apache License 2.0 5 votes vote down vote up
@BeforeTest
public void setup() throws IOException {
    ModelManager modelManager = new ModelManager();
    CqlTranslator translator = CqlTranslator.fromStream(TimeOperatorsTest.class.getResourceAsStream("../OperatorTests/TimeOperators.cql"), modelManager, new LibraryManager(modelManager));
    assertThat(translator.getErrors().size(), is(0));
    Library library = translator.toELM();
    defs = new HashMap<>();
    for (ExpressionDef def: library.getStatements().getDef()) {
        defs.put(def.getName(), def);
    }
}
 
Example #14
Source File: BaseTest.java    From clinical_quality_language with Apache License 2.0 5 votes vote down vote up
@Test
public void testRetrieveWithConcept() throws IOException {
    CqlTranslator translator = TestUtils.runSemanticTest("fhir/stu3/TestRetrieveWithConcept.cql", 0);
    TranslatedLibrary library = translator.getTranslatedLibrary();
    ExpressionDef expressionDef = library.resolveExpressionRef("Test Tobacco Smoking Status");

    assertThat(expressionDef.getExpression(), instanceOf(Retrieve.class));
    Retrieve retrieve = (Retrieve)expressionDef.getExpression();
    assertThat(retrieve.getCodes(), instanceOf(ToList.class));
    ToList toList = (ToList)retrieve.getCodes();
    assertThat(toList.getOperand(), instanceOf(CodeRef.class));
}
 
Example #15
Source File: ArithmeticOperatorsTest.java    From clinical_quality_language with Apache License 2.0 5 votes vote down vote up
@BeforeTest
public void setup() throws IOException {
    ModelManager modelManager = new ModelManager();
    CqlTranslator translator = CqlTranslator.fromStream(ArithmeticOperatorsTest.class.getResourceAsStream("../OperatorTests/ArithmeticOperators.cql"), modelManager, new LibraryManager(modelManager));
    assertThat(translator.getErrors().size(), is(0));
    Library library = translator.toELM();
    defs = new HashMap<>();
    for (ExpressionDef def: library.getStatements().getDef()) {
        defs.put(def.getName(), def);
    }
}
 
Example #16
Source File: ListOperatorsTest.java    From clinical_quality_language with Apache License 2.0 5 votes vote down vote up
@BeforeTest
public void setup() throws IOException {
    ModelManager modelManager = new ModelManager();
    CqlTranslator translator = CqlTranslator.fromStream(ListOperatorsTest.class.getResourceAsStream("../OperatorTests/ListOperators.cql"), modelManager, new LibraryManager(modelManager));
    assertThat(translator.getErrors().size(), is(0));
    Library library = translator.toELM();
    defs = new HashMap<>();
    for (ExpressionDef def: library.getStatements().getDef()) {
        defs.put(def.getName(), def);
    }
}
 
Example #17
Source File: BaseTest.java    From clinical_quality_language with Apache License 2.0 5 votes vote down vote up
@Test
public void testRetrieveWithConcept() throws IOException {
    CqlTranslator translator = TestUtils.runSemanticTest("fhir/r401/TestRetrieveWithConcept.cql", 0);
    TranslatedLibrary library = translator.getTranslatedLibrary();
    ExpressionDef expressionDef = library.resolveExpressionRef("Test Tobacco Smoking Status");

    assertThat(expressionDef.getExpression(), instanceOf(Retrieve.class));
    Retrieve retrieve = (Retrieve)expressionDef.getExpression();
    assertThat(retrieve.getCodes(), instanceOf(ToList.class));
    ToList toList = (ToList)retrieve.getCodes();
    assertThat(toList.getOperand(), instanceOf(CodeRef.class));
}
 
Example #18
Source File: BaseTest.java    From clinical_quality_language with Apache License 2.0 5 votes vote down vote up
@Test
public void testFHIRNamespaces() throws IOException {
    CqlTranslator translator = TestUtils.runSemanticTest(new NamespaceInfo("Public", "http://cql.hl7.org/public"), "fhir/r401/TestFHIRNamespaces.cql", 0);
    TranslatedLibrary library = translator.getTranslatedLibrary();
    IncludeDef includeDef = library.resolveIncludeRef("FHIRHelpers");
    assertThat(includeDef, notNullValue());
    assertThat(includeDef.getPath(), is("http://hl7.org/fhir/FHIRHelpers"));
    assertThat(includeDef.getVersion(), is("4.0.1"));
}
 
Example #19
Source File: BaseTest.java    From clinical_quality_language with Apache License 2.0 5 votes vote down vote up
@Test
public void testFHIRWithoutNamespaces() throws IOException {
    CqlTranslator translator = TestUtils.runSemanticTest("fhir/r401/TestFHIRNamespaces.cql", 0);
    TranslatedLibrary library = translator.getTranslatedLibrary();
    IncludeDef includeDef = library.resolveIncludeRef("FHIRHelpers");
    assertThat(includeDef, notNullValue());
    assertThat(includeDef.getPath(), is("FHIRHelpers"));
    assertThat(includeDef.getVersion(), is("4.0.1"));
}
 
Example #20
Source File: EscapeSequenceWithBacktickTests.java    From clinical_quality_language with Apache License 2.0 5 votes vote down vote up
@BeforeTest
public void setup() throws IOException {
    ModelManager modelManager = new ModelManager();
    LibraryManager libraryManager = new LibraryManager(modelManager);
    CqlTranslator translator = CqlTranslator.fromStream(org.cqframework.cql.cql2elm.EscapeSequenceTests.class.getResourceAsStream("EscapeSequenceWithBacktickTests.cql"), modelManager, libraryManager);
    assertThat(translator.getErrors().size(), is(0));
    Library library = translator.toELM();
    defs = new HashMap<>();
    for (ExpressionDef def: library.getStatements().getDef()) {
        defs.put(def.getName(), def);
    }
}
 
Example #21
Source File: DateTimeOperatorsTest.java    From clinical_quality_language with Apache License 2.0 5 votes vote down vote up
@BeforeTest
public void setup() throws IOException {
    ModelManager modelManager = new ModelManager();
    CqlTranslator translator = CqlTranslator.fromStream(DateTimeOperatorsTest.class.getResourceAsStream("../OperatorTests/DateTimeOperators.cql"), modelManager, new LibraryManager(modelManager));
    assertThat(translator.getErrors().size(), is(0));
    Library library = translator.toELM();
    defs = new HashMap<>();
    for (ExpressionDef def: library.getStatements().getDef()) {
        defs.put(def.getName(), def);
    }
}
 
Example #22
Source File: StringOperatorsTest.java    From clinical_quality_language with Apache License 2.0 5 votes vote down vote up
@BeforeTest
public void setup() throws IOException {
    ModelManager modelManager = new ModelManager();
    CqlTranslator translator = CqlTranslator.fromStream(StringOperatorsTest.class.getResourceAsStream("../OperatorTests/StringOperators.cql"), modelManager, new LibraryManager(modelManager));
    assertThat(translator.getErrors().size(), is(0));
    Library library = translator.toELM();
    defs = new HashMap<>();
    for (ExpressionDef def: library.getStatements().getDef()) {
        defs.put(def.getName(), def);
    }
}
 
Example #23
Source File: AgeOperatorsTest.java    From clinical_quality_language with Apache License 2.0 5 votes vote down vote up
@BeforeTest
public void setup() throws IOException {
    ModelManager modelManager = new ModelManager();
    CqlTranslator translator = CqlTranslator.fromStream(AgeOperatorsTest.class.getResourceAsStream("../OperatorTests/AgeOperators.cql"), modelManager, new LibraryManager(modelManager));
    assertThat(translator.getErrors().size(), is(0));
    Library library = translator.toELM();
    defs = new HashMap<>();
    if (library.getStatements() != null) {
        for (ExpressionDef def : library.getStatements().getDef()) {
            defs.put(def.getName(), def);
        }
    }
}
 
Example #24
Source File: AggregateOperatorsTest.java    From clinical_quality_language with Apache License 2.0 5 votes vote down vote up
@BeforeTest
public void setup() throws IOException {
    ModelManager modelManager = new ModelManager();
    CqlTranslator translator = CqlTranslator.fromStream(AggregateOperatorsTest.class.getResourceAsStream("../OperatorTests/AggregateOperators.cql"), modelManager, new LibraryManager(modelManager));
    assertThat(translator.getErrors().size(), is(0));
    Library library = translator.toELM();
    defs = new HashMap<>();
    if (library.getStatements() != null) {
        for (ExpressionDef def : library.getStatements().getDef()) {
            defs.put(def.getName(), def);
        }
    }
}
 
Example #25
Source File: LibraryLoader.java    From cqf-ruler with Apache License 2.0 5 votes vote down vote up
private Library loadLibrary(VersionedIdentifier libraryIdentifier) {
    org.hl7.elm.r1.VersionedIdentifier identifier = new org.hl7.elm.r1.VersionedIdentifier()
            .withId(libraryIdentifier.getId()).withSystem(libraryIdentifier.getSystem())
            .withVersion(libraryIdentifier.getVersion());

    ArrayList<CqlTranslatorException> errors = new ArrayList<>();
    org.hl7.elm.r1.Library translatedLibrary = libraryManager.resolveLibrary(identifier, ErrorSeverity.Error,
            SignatureLevel.All,
            new CqlTranslator.Options[] { CqlTranslator.Options.EnableAnnotations,
                    CqlTranslator.Options.EnableLocators, CqlTranslator.Options.DisableListDemotion,
                    CqlTranslator.Options.DisableListPromotion, CqlTranslator.Options.DisableMethodInvocation },
            errors).getLibrary();

    if (CqlTranslatorException.HasErrors(errors)) {
        throw new IllegalArgumentException(errorsToString(errors));
    }
    try {
        CqlTranslator translator = getTranslator("", libraryManager, modelManager);

        if (translator.getErrors().size() > 0) {
            throw new IllegalArgumentException(errorsToString(translator.getErrors()));
        }

        return readLibrary(new ByteArrayInputStream(
                translator.convertToXml(translatedLibrary).getBytes(StandardCharsets.UTF_8)));
    } catch (JAXBException e) {
        throw new IllegalArgumentException(String.format("Errors occurred translating library %s%s.",
                identifier.getId(), identifier.getVersion() != null ? ("-" + identifier.getVersion()) : ""));
    }
}
 
Example #26
Source File: SortingTest.java    From clinical_quality_language with Apache License 2.0 5 votes vote down vote up
@BeforeTest
public void setup() throws IOException {
    ModelManager modelManager = new ModelManager();
    CqlTranslator translator = CqlTranslator.fromStream(QueryTest.class.getResourceAsStream("../OperatorTests/Sorting.cql"), modelManager, new LibraryManager(modelManager));
    
    // The alias test creates an error
    assertThat(translator.getErrors().size(), is(1));

    Library library = translator.toELM();
    defs = new HashMap<>();
    for (ExpressionDef def: library.getStatements().getDef()) {
        defs.put(def.getName(), def);
    }
}
 
Example #27
Source File: TypeOperatorsTest.java    From clinical_quality_language with Apache License 2.0 5 votes vote down vote up
@BeforeTest
public void setup() throws IOException {
    ModelManager modelManager = new ModelManager();
    CqlTranslator translator = CqlTranslator.fromStream(TypeOperatorsTest.class.getResourceAsStream("../OperatorTests/TypeOperators.cql"), modelManager, new LibraryManager(modelManager));
    assertThat(translator.getErrors().size(), is(0));
    Library library = translator.toELM();
    defs = new HashMap<>();
    for (ExpressionDef def: library.getStatements().getDef()) {
        defs.put(def.getName(), def);
    }
}
 
Example #28
Source File: DataRequirementsProvider.java    From cqf-ruler with Apache License 2.0 5 votes vote down vote up
public String getValueSetId(String valueSetName, CqlTranslator translator) {
    org.hl7.elm.r1.Library.ValueSets valueSets = translator.toELM().getValueSets();
    if (valueSets != null) {
        for (org.hl7.elm.r1.ValueSetDef def : valueSets.getDef()) {
            if (def.getName().equals(valueSetName)) {
                return def.getId();
            }
        }
    }

    return valueSetName;
}
 
Example #29
Source File: DataRequirementsProvider.java    From cqf-ruler with Apache License 2.0 5 votes vote down vote up
public void ensureDataRequirements(org.hl7.fhir.r4.model.Library library, CqlTranslator translator) {
    library.getDataRequirement().clear();

    List<DataRequirement> reqs = new ArrayList<>();

    for (org.hl7.elm.r1.Retrieve retrieve : translator.toRetrieves()) {
        DataRequirement dataReq = new DataRequirement();
        dataReq.setType(retrieve.getDataType().getLocalPart());
        if (retrieve.getCodeProperty() != null) {
            DataRequirement.DataRequirementCodeFilterComponent codeFilter = new DataRequirement.DataRequirementCodeFilterComponent();
            codeFilter.setPath(retrieve.getCodeProperty());
            if (retrieve.getCodes() instanceof ValueSetRef) {
                codeFilter.setValueSet(getValueSetId(((ValueSetRef) retrieve.getCodes()).getName(), translator));
            }
            dataReq.setCodeFilter(Collections.singletonList(codeFilter));
        }
        // TODO - Date filters - we want to populate this with a $data-requirements
        // request as there isn't a good way through elm analysis
        reqs.add(dataReq);
    }


    // 
    // org.hl7.elm.r1.Library elm = translator.toELM();
    // Codes codes = elm.getCodes();
    // for (CodeDef cd : codes.getDef()) {
    //     cd.
    // }

    library.setDataRequirement(reqs);     
}
 
Example #30
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);
    }