From a3857fcdfda2542a927f20b1490268fa4570a128 Mon Sep 17 00:00:00 2001 From: Joseph Kotanchik Date: Thu, 5 Oct 2023 12:56:43 -0400 Subject: [PATCH] Extract Direct Reference Code info for data criteria while parsing cql. --- .../utils/cql/CQLTools.java | 17 ++-- .../utils/cql/parsing/Cql2ElmListener.java | 84 ++++++------------- .../utils/cql/parsing/model/CQLGraph.java | 3 +- 3 files changed, 31 insertions(+), 73 deletions(-) diff --git a/src/main/java/gov/cms/mat/cql_elm_translation/utils/cql/CQLTools.java b/src/main/java/gov/cms/mat/cql_elm_translation/utils/cql/CQLTools.java index d0589bec..47ca7d03 100644 --- a/src/main/java/gov/cms/mat/cql_elm_translation/utils/cql/CQLTools.java +++ b/src/main/java/gov/cms/mat/cql_elm_translation/utils/cql/CQLTools.java @@ -109,7 +109,7 @@ public CQLTools( /** * The CQL Filter Entry Point. * - *

This function will find all of the used CQL expressions, create a valueset - datatype map + *

This function will find all the used CQL expressions, create a valueset - datatype map * and code - datatype map, and find return types for each expression. * * @throws IOException @@ -142,6 +142,7 @@ public void generate() throws IOException { new HashMap<>(listener.getValueSetDataTypeMap()); Map>> codeMap = new HashMap<>(listener.getCodeDataTypeMap()); Map valueSetOids = new HashMap<>(listener.getValueSetOids()); + Map drcs = new HashMap<>(listener.getDrcs()); collectUsedExpressions( graph, @@ -154,10 +155,10 @@ public void generate() throws IOException { functionsSet); collectValueSetCodeDataType(valuesetMap, codeMap); collectReturnTypeMap(); - collectDataCriteria(valueSetOids); + collectDataCriteria(valueSetOids, drcs); } - private void collectDataCriteria(Map valueSetOids) { + private void collectDataCriteria(Map valueSetOids, Map drcs) { valuesetDataTypeMap .keySet() .forEach( @@ -174,15 +175,7 @@ private void collectDataCriteria(Map valueSetOids) { code -> dataCriteria .getDataCriteriaWithCodes() - .put( - CQLCode.builder() - .codeName(code) - // TODO lookup code & code system details - .codeOID("shrug") - .codeSystemName("shrug") - .codeSystemOID("shrug") - .build(), - codeDataTypeMap.get(code))); + .put(drcs.get(code), codeDataTypeMap.get(code))); } private void collectUsedExpressions( diff --git a/src/main/java/gov/cms/mat/cql_elm_translation/utils/cql/parsing/Cql2ElmListener.java b/src/main/java/gov/cms/mat/cql_elm_translation/utils/cql/parsing/Cql2ElmListener.java index 37d21d7b..40aaac30 100644 --- a/src/main/java/gov/cms/mat/cql_elm_translation/utils/cql/parsing/Cql2ElmListener.java +++ b/src/main/java/gov/cms/mat/cql_elm_translation/utils/cql/parsing/Cql2ElmListener.java @@ -6,7 +6,9 @@ import java.nio.charset.StandardCharsets; import java.util.*; +import gov.cms.mat.cql_elm_translation.utils.cql.parsing.model.CQLCode; import gov.cms.mat.cql_elm_translation.utils.cql.parsing.model.CQLGraph; +import lombok.Getter; import org.antlr.v4.runtime.CharStreams; import org.antlr.v4.runtime.CommonTokenStream; import org.antlr.v4.runtime.misc.NotNull; @@ -45,7 +47,7 @@ public class Cql2ElmListener extends cqlBaseListener { * The identifier of the current library, relative to the library that brought us here. Will be in * the form of libraryName|alias */ - private String libraryIdentifier; + private final String libraryIdentifier; /** * The include def object which we are current parsing, relative to the library that brought us @@ -54,7 +56,7 @@ public class Cql2ElmListener extends cqlBaseListener { IncludeDef libraryAccessor = null; /** The current library object from the parser */ - private CompiledLibrary library; + private final CompiledLibrary library; /** The map of the other libraries in the current library */ Map translatedLibraryMap; @@ -62,20 +64,21 @@ public class Cql2ElmListener extends cqlBaseListener { /** The current context, aka which expression are we currently in. */ private String currentContext; - private Set libraries = new HashSet<>(); - private Set valuesets = new HashSet<>(); - private Set codes = new HashSet<>(); - private Set codesystems = new HashSet<>(); - private Set parameters = new HashSet<>(); - private Set definitions = new HashSet<>(); - private Set functions = new HashSet<>(); - private HashMap valueSetOids = new HashMap<>(); - private Map>> valueSetDataTypeMap = new HashMap<>(); - private Map>> codeDataTypeMap = new HashMap<>(); + @Getter private final Set libraries = new HashSet<>(); + @Getter private final Set valuesets = new HashSet<>(); + @Getter private final Set codes = new HashSet<>(); + @Getter private final Set codesystems = new HashSet<>(); + @Getter private final Set parameters = new HashSet<>(); + @Getter private final Set definitions = new HashSet<>(); + @Getter private final Set functions = new HashSet<>(); + @Getter private final HashMap valueSetOids = new HashMap<>(); + @Getter private final HashMap drcs = new HashMap<>(); + @Getter private final Map>> valueSetDataTypeMap = new HashMap<>(); + @Getter private final Map>> codeDataTypeMap = new HashMap<>(); - private Stack namespace = new Stack<>(); + private final Stack namespace = new Stack<>(); - private CQLGraph graph; + @Getter private final CQLGraph graph; public Cql2ElmListener( CQLGraph graph, @@ -130,7 +133,7 @@ public void enterQualifiedIdentifierExpression(QualifiedIdentifierExpressionCont String qualifier = ""; if (shouldResolve(identifier)) { - // a qualified identifier can take on the form (qualifier) '.')* identifier. If there is only + // a qualified identifier can take on the form (qualifier '.')* identifier. If there is only // one qualifier, // then that could be a library. Resolve the qualifier to check if it's a library. if (CollectionUtils.isNotEmpty(ctx.qualifierExpression()) @@ -269,6 +272,7 @@ public void enterRetrieve(@NotNull cqlParser.RetrieveContext ctx) { current.get(formattedIdentifier).add(dataType); valueSetOids.putIfAbsent( formattedIdentifier, ((ValueSetDef) element).getId().substring("urn:oid:".length())); + } else if (element instanceof CodeDef) { Map> current = codeDataTypeMap.get(currentContext); if (current == null) { @@ -283,6 +287,12 @@ public void enterRetrieve(@NotNull cqlParser.RetrieveContext ctx) { } current.get(formattedIdentifier).add(dataType); + drcs.putIfAbsent(formattedIdentifier, + CQLCode.builder() + .id(((CodeDef)element).getId()) + .codeName(formattedIdentifier) + .codeSystemName(((CodeDef)element).getCodeSystem().getName()) + .build()); } } @@ -508,48 +518,4 @@ private void parseChildLibraries(IncludeDef def) throws IOException { codeDataTypeMap.putAll(listener.getCodeDataTypeMap()); valueSetOids.putAll(listener.getValueSetOids()); } - - public Set getLibraries() { - return libraries; - } - - public Set getValuesets() { - return valuesets; - } - - public Set getCodes() { - return codes; - } - - public Set getCodesystems() { - return codesystems; - } - - public Set getParameters() { - return parameters; - } - - public Set getDefinitions() { - return definitions; - } - - public Set getFunctions() { - return functions; - } - - public Map>> getValueSetDataTypeMap() { - return valueSetDataTypeMap; - } - - public Map>> getCodeDataTypeMap() { - return codeDataTypeMap; - } - - public Map getValueSetOids() { - return valueSetOids; - } - - public CQLGraph getGraph() { - return graph; - } } diff --git a/src/main/java/gov/cms/mat/cql_elm_translation/utils/cql/parsing/model/CQLGraph.java b/src/main/java/gov/cms/mat/cql_elm_translation/utils/cql/parsing/model/CQLGraph.java index 525e7245..82be9fb5 100644 --- a/src/main/java/gov/cms/mat/cql_elm_translation/utils/cql/parsing/model/CQLGraph.java +++ b/src/main/java/gov/cms/mat/cql_elm_translation/utils/cql/parsing/model/CQLGraph.java @@ -74,9 +74,8 @@ public boolean isPath(String source, String destination) { public String toString() { StringBuilder builder = new StringBuilder(); for (String node : graph.keySet()) { - builder.append(node + " ---> " + graph.get(node) + "\n"); + builder.append(node).append(" ---> ").append(graph.get(node)).append("\n"); } - return builder.toString(); }