Skip to content

Commit

Permalink
Merge pull request #30 from MeasureAuthoringTool/MAT-4721_UpdateCqFra…
Browse files Browse the repository at this point in the history
…mework

Mat 4721 update cq framework
  • Loading branch information
gregory-akins authored Oct 4, 2022
2 parents 39aa64a + 16fabba commit a5177c6
Show file tree
Hide file tree
Showing 8 changed files with 102 additions and 62 deletions.
36 changes: 32 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<description>Translates CQL to ELM for MADiE</description>

<properties>
<cqframework.version>1.5.8</cqframework.version>
<cqframework.version>2.2.0</cqframework.version>
<java.version>16</java.version>
<mvn.checkstyle.file>madie-checkstyle.xml</mvn.checkstyle.file>
<mvn.checkstyle.version>3.1.2</mvn.checkstyle.version>
Expand All @@ -23,6 +23,26 @@
</properties>

<dependencies>
<dependency>
<groupId>org.fhir</groupId>
<artifactId>ucum</artifactId>
<version>1.0.3</version>
</dependency>
<!-- JAXB API only -->

<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>org.eclipse.persistence.moxy</artifactId>
<version>2.6.8</version>
</dependency>

<!-- END JAXB API only -->

<dependency>
<groupId>org.jvnet.jaxb2_commons</groupId>
<artifactId>jaxb2-basics-runtime</artifactId>
<version>0.13.1</version>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
Expand Down Expand Up @@ -58,6 +78,12 @@
<version>${cqframework.version}</version>
</dependency>

<dependency>
<groupId>info.cqframework</groupId>
<artifactId>model-jaxb</artifactId>
<version>${cqframework.version}</version>
</dependency>

<dependency>
<groupId>info.cqframework</groupId>
<artifactId>cql-to-elm</artifactId>
Expand All @@ -69,7 +95,11 @@
<artifactId>elm</artifactId>
<version>${cqframework.version}</version>
</dependency>

<dependency>
<groupId>info.cqframework</groupId>
<artifactId>elm-jaxb</artifactId>
<version>${cqframework.version}</version>
</dependency>
<dependency>
<groupId>info.cqframework</groupId>
<artifactId>quick</artifactId>
Expand Down Expand Up @@ -139,7 +169,6 @@
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.12.0</version>
</dependency>

<dependency>
Expand Down Expand Up @@ -189,7 +218,6 @@
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>

</dependencies>

<!-- Required to fetch artifacts from repositories other than maven central -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

/** Class creates the interceptors used in header processing */
@Configuration
@Slf4j
public class InterceptorConfig implements WebMvcConfigurer {

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@

@RestController
@RequestMapping(path = "/mat/translator")
@Tag(name = "Transfer-Translator", description = "API for translating MAT transferred measure CQL to ELM.")
@Tag(
name = "Transfer-Translator",
description = "API for translating MAT transferred measure CQL to ELM.")
@Slf4j
@RequiredArgsConstructor
public class MatMeasureController {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package gov.cms.mat.cql_elm_translation.cql_translator;

import org.cqframework.cql.cql2elm.CqlCompilerException;
import org.cqframework.cql.cql2elm.CqlTranslator;
import org.cqframework.cql.cql2elm.CqlTranslatorException;
import org.cqframework.cql.cql2elm.CqlTranslatorOptions;

import org.cqframework.cql.cql2elm.LibraryBuilder;
import org.cqframework.cql.cql2elm.LibraryManager;
import org.cqframework.cql.cql2elm.ModelManager;
Expand All @@ -22,44 +24,48 @@ public enum ModelType {
QICore;
}

private static final MultivaluedMap<String, CqlTranslator.Options> PARAMS_TO_OPTIONS_MAP =
private static final MultivaluedMap<String, CqlTranslatorOptions.Options> PARAMS_TO_OPTIONS_MAP =
new MultivaluedHashMap<>() {
{
putSingle("date-range-optimization", CqlTranslator.Options.EnableDateRangeOptimization);
putSingle("annotations", CqlTranslator.Options.EnableAnnotations);
putSingle("locators", CqlTranslator.Options.EnableLocators);
putSingle("result-types", CqlTranslator.Options.EnableResultTypes);
putSingle("detailed-errors", CqlTranslator.Options.EnableDetailedErrors);
putSingle("disable-list-traversal", CqlTranslator.Options.DisableListTraversal);
putSingle("disable-list-demotion", CqlTranslator.Options.DisableListDemotion);
putSingle("disable-list-promotion", CqlTranslator.Options.DisableListPromotion);
putSingle("enable-interval-demotion", CqlTranslator.Options.EnableIntervalDemotion);
putSingle("enable-interval-promotion", CqlTranslator.Options.EnableIntervalPromotion);
putSingle("disable-method-invocation", CqlTranslator.Options.DisableMethodInvocation);
putSingle("require-from-keyword", CqlTranslator.Options.RequireFromKeyword);
putSingle(
"date-range-optimization", CqlTranslatorOptions.Options.EnableDateRangeOptimization);
putSingle("annotations", CqlTranslatorOptions.Options.EnableAnnotations);
putSingle("locators", CqlTranslatorOptions.Options.EnableLocators);
putSingle("result-types", CqlTranslatorOptions.Options.EnableResultTypes);
putSingle("detailed-errors", CqlTranslatorOptions.Options.EnableDetailedErrors);
putSingle("disable-list-traversal", CqlTranslatorOptions.Options.DisableListTraversal);
putSingle("disable-list-demotion", CqlTranslatorOptions.Options.DisableListDemotion);
putSingle("disable-list-promotion", CqlTranslatorOptions.Options.DisableListPromotion);
putSingle(
"enable-interval-demotion", CqlTranslatorOptions.Options.EnableIntervalDemotion);
putSingle(
"enable-interval-promotion", CqlTranslatorOptions.Options.EnableIntervalPromotion);
putSingle(
"disable-method-invocation", CqlTranslatorOptions.Options.DisableMethodInvocation);
putSingle("require-from-keyword", CqlTranslatorOptions.Options.RequireFromKeyword);

// Todo Do we even use these consolidated options ?
put(
"strict",
Arrays.asList(
CqlTranslator.Options.DisableListTraversal,
CqlTranslator.Options.DisableListDemotion,
CqlTranslator.Options.DisableListPromotion,
CqlTranslator.Options.DisableMethodInvocation));
CqlTranslatorOptions.Options.DisableListTraversal,
CqlTranslatorOptions.Options.DisableListDemotion,
CqlTranslatorOptions.Options.DisableListPromotion,
CqlTranslatorOptions.Options.DisableMethodInvocation));
put(
"debug",
Arrays.asList(
CqlTranslator.Options.EnableAnnotations,
CqlTranslator.Options.EnableLocators,
CqlTranslator.Options.EnableResultTypes));
CqlTranslatorOptions.Options.EnableAnnotations,
CqlTranslatorOptions.Options.EnableLocators,
CqlTranslatorOptions.Options.EnableResultTypes));
put(
"mat",
Arrays.asList(
CqlTranslator.Options.EnableAnnotations,
CqlTranslator.Options.EnableLocators,
CqlTranslator.Options.DisableListDemotion,
CqlTranslator.Options.DisableListPromotion,
CqlTranslator.Options.DisableMethodInvocation));
CqlTranslatorOptions.Options.EnableAnnotations,
CqlTranslatorOptions.Options.EnableLocators,
CqlTranslatorOptions.Options.DisableListDemotion,
CqlTranslatorOptions.Options.DisableListPromotion,
CqlTranslatorOptions.Options.DisableMethodInvocation));
}
};

Expand Down Expand Up @@ -91,7 +97,7 @@ public CqlTranslator buildTranslator(
try {
UcumService ucumService = null;
LibraryBuilder.SignatureLevel signatureLevel = LibraryBuilder.SignatureLevel.None;
List<CqlTranslator.Options> optionsList = new ArrayList<>();
List<CqlTranslatorOptions.Options> optionsList = new ArrayList<>();

for (String key : params.keySet()) {
if (PARAMS_TO_OPTIONS_MAP.containsKey(key) && Boolean.parseBoolean(params.getFirst(key))) {
Expand All @@ -103,7 +109,8 @@ public CqlTranslator buildTranslator(
}
}

CqlTranslator.Options[] options = optionsList.toArray(new CqlTranslator.Options[0]);
CqlTranslatorOptions.Options[] options =
optionsList.toArray(new CqlTranslatorOptions.Options[0]);

libraryManager.getLibrarySourceLoader().registerProvider(new MadieLibrarySourceProvider());

Expand All @@ -112,7 +119,7 @@ public CqlTranslator buildTranslator(
modelManager,
libraryManager,
ucumService,
CqlTranslatorException.ErrorSeverity.Error,
CqlCompilerException.ErrorSeverity.Error,
signatureLevel,
options);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.cqframework.cql.cql2elm.CqlTranslator;
import org.cqframework.cql.cql2elm.CqlTranslatorException;
import org.cqframework.cql.cql2elm.CqlCompilerException;
import org.springframework.stereotype.Service;
import java.util.List;

Expand All @@ -36,7 +36,7 @@ public CqlConversionPayload processCqlDataWithErrors(RequestData requestData) {
// Gets the translator results
CqlTranslator cqlTranslator = processCqlData(requestData);

List<CqlTranslatorException> cqlTranslatorExceptions =
List<CqlCompilerException> cqlTranslatorExceptions =
processErrors(
requestData.getCqlData(), requestData.isShowWarnings(), cqlTranslator.getExceptions());

Expand All @@ -61,18 +61,18 @@ public CqlTranslator processCqlData(RequestData requestData) {
.buildTranslator(requestData.getCqlDataInputStream(), requestData.createMap());
}

private List<CqlTranslatorException> processErrors(
String cqlData, boolean showWarnings, List<CqlTranslatorException> cqlTranslatorExceptions) {
private List<CqlCompilerException> processErrors(
String cqlData, boolean showWarnings, List<CqlCompilerException> cqlTranslatorExceptions) {
logErrors(cqlTranslatorExceptions);
return new CqlTranslatorExceptionFilter(cqlData, showWarnings, cqlTranslatorExceptions)
.filter();
}

private void logErrors(List<CqlTranslatorException> exceptions) {
private void logErrors(List<CqlCompilerException> exceptions) {
exceptions.forEach(e -> log.debug(formatMessage(e)));
}

private String formatMessage(CqlTranslatorException e) {
private String formatMessage(CqlCompilerException e) {
return String.format(
LOG_MESSAGE_TEMPLATE,
e.getSeverity() != null ? e.getSeverity().name() : null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.cqframework.cql.cql2elm.CqlTranslatorException;
import org.cqframework.cql.cql2elm.CqlCompilerException;
import org.hl7.elm.r1.VersionedIdentifier;

import java.util.Collections;
Expand All @@ -15,10 +15,10 @@
public class CqlTranslatorExceptionFilter implements CqlLibraryFinder {
@Getter private final String cqlData;
private final boolean showWarnings;
private final List<CqlTranslatorException> cqlTranslatorExceptions;
private final List<CqlCompilerException> cqlTranslatorExceptions;

public CqlTranslatorExceptionFilter(
String cqlData, boolean showWarnings, List<CqlTranslatorException> cqlTranslatorExceptions) {
String cqlData, boolean showWarnings, List<CqlCompilerException> cqlTranslatorExceptions) {
this.cqlData = cqlData;
this.showWarnings = showWarnings;
this.cqlTranslatorExceptions = cqlTranslatorExceptions;
Expand All @@ -29,12 +29,12 @@ public CqlTranslatorExceptionFilter(
* Then the cqlTranslatorExceptions are filtered out if they are not pointed to the
* parent library. ( reason: unknown )
* */
public List<CqlTranslatorException> filter() {
public List<CqlCompilerException> filter() {
if (CollectionUtils.isEmpty(cqlTranslatorExceptions)) {
log.debug("No CQL Errors found");
return Collections.emptyList();
} else {
List<CqlTranslatorException> filteredCqlTranslatorExceptions = filterOutWarnings();
List<CqlCompilerException> filteredCqlTranslatorExceptions = filterOutWarnings();

if (filteredCqlTranslatorExceptions.isEmpty()) {
return Collections.emptyList();
Expand All @@ -44,27 +44,27 @@ public List<CqlTranslatorException> filter() {
}
}

private List<CqlTranslatorException> filterOutWarnings() {
private List<CqlCompilerException> filterOutWarnings() {
if (showWarnings) {
return cqlTranslatorExceptions;
} else {
return cqlTranslatorExceptions.stream().filter(this::isError).collect(Collectors.toList());
}
}

private boolean isError(CqlTranslatorException cqlTranslatorException) {
private boolean isError(CqlCompilerException cqlTranslatorException) {
return cqlTranslatorException != null
&& cqlTranslatorException.getSeverity() != null
&& cqlTranslatorException.getSeverity() == CqlTranslatorException.ErrorSeverity.Error;
&& cqlTranslatorException.getSeverity() == CqlCompilerException.ErrorSeverity.Error;
}

/*
* Few exceptions from CqlTranslator doesn't have a library.version, so they are removed.
* This filter also removes if there are any errors caught by translator in any of the
* included libraries
* */
private List<CqlTranslatorException> filterByLibrary(
List<CqlTranslatorException> filteredCqlTranslatorExceptions) {
private List<CqlCompilerException> filterByLibrary(
List<CqlCompilerException> filteredCqlTranslatorExceptions) {
var libraryProperties = parseLibrary();

return filteredCqlTranslatorExceptions.stream()
Expand All @@ -73,7 +73,7 @@ private List<CqlTranslatorException> filterByLibrary(
}

private boolean filterOutInclude(
CqlTranslatorException cqlTranslatorException, LibraryProperties libraryProperties) {
CqlCompilerException cqlTranslatorException, LibraryProperties libraryProperties) {
if (cqlTranslatorException.getLocator() == null
|| cqlTranslatorException.getLocator().getLibrary() == null) {
return false;
Expand All @@ -85,6 +85,10 @@ private boolean filterOutInclude(
}

private boolean isPointingToSameLibrary(LibraryProperties p, VersionedIdentifier v) {
return p.getName().equals(v.getId()) && p.getVersion().equals(v.getVersion());
log.debug(v.toString());
String id = v.getId();
String version = v.getVersion();

return p.getName().equals(id) && p.getVersion().equals(version);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.cqframework.cql.cql2elm.CqlTranslatorException;
import org.cqframework.cql.cql2elm.CqlCompilerException;
import org.cqframework.cql.elm.tracking.TrackBack;
import java.util.List;
import java.util.stream.Collectors;
Expand All @@ -16,14 +16,14 @@ public class CqlExceptionErrorProcessor {

private static final ObjectMapper mapper = new ObjectMapper();

private final List<CqlTranslatorException> cqlErrors;
private final List<CqlCompilerException> cqlErrors;
private final String json;

/**
* Transforms CqlTranslatorException to MatCqlConversionException and prepend with
* "errorExceptions" object to the translator.json
*/
public CqlExceptionErrorProcessor(List<CqlTranslatorException> cqlErrors, String json) {
public CqlExceptionErrorProcessor(List<CqlCompilerException> cqlErrors, String json) {
this.cqlErrors = cqlErrors;
this.json = json;
}
Expand Down Expand Up @@ -55,7 +55,7 @@ private List<MatCqlConversionException> buildMatErrors() {
return cqlErrors.stream().map(this::createDto).collect(Collectors.toList());
}

private MatCqlConversionException createDto(CqlTranslatorException cqlException) {
private MatCqlConversionException createDto(CqlCompilerException cqlException) {
MatCqlConversionException matCqlConversionException = buildMatError(cqlException);

if (cqlException.getLocator() == null) {
Expand All @@ -67,7 +67,7 @@ private MatCqlConversionException createDto(CqlTranslatorException cqlException)
return matCqlConversionException;
}

private MatCqlConversionException buildMatError(CqlTranslatorException cqlTranslatorException) {
private MatCqlConversionException buildMatError(CqlCompilerException cqlTranslatorException) {
MatCqlConversionException matCqlConversionException = new MatCqlConversionException();
matCqlConversionException.setErrorSeverity(cqlTranslatorException.getSeverity().name());
log.debug("buildMatError" + cqlTranslatorException.getMessage());
Expand Down
Loading

0 comments on commit a5177c6

Please sign in to comment.