Skip to content

Commit

Permalink
Extract Direct Reference Code info for data criteria while parsing cql.
Browse files Browse the repository at this point in the history
  • Loading branch information
jkotanchik-SB committed Oct 5, 2023
1 parent dd5a249 commit a3857fc
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 73 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public CQLTools(
/**
* The CQL Filter Entry Point.
*
* <p>This function will find all of the used CQL expressions, create a valueset - datatype map
* <p>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
Expand Down Expand Up @@ -142,6 +142,7 @@ public void generate() throws IOException {
new HashMap<>(listener.getValueSetDataTypeMap());
Map<String, Map<String, Set<String>>> codeMap = new HashMap<>(listener.getCodeDataTypeMap());
Map<String, String> valueSetOids = new HashMap<>(listener.getValueSetOids());
Map<String, CQLCode> drcs = new HashMap<>(listener.getDrcs());

collectUsedExpressions(
graph,
Expand All @@ -154,10 +155,10 @@ public void generate() throws IOException {
functionsSet);
collectValueSetCodeDataType(valuesetMap, codeMap);
collectReturnTypeMap();
collectDataCriteria(valueSetOids);
collectDataCriteria(valueSetOids, drcs);
}

private void collectDataCriteria(Map<String, String> valueSetOids) {
private void collectDataCriteria(Map<String, String> valueSetOids, Map<String, CQLCode> drcs) {
valuesetDataTypeMap
.keySet()
.forEach(
Expand All @@ -174,15 +175,7 @@ private void collectDataCriteria(Map<String, String> 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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -54,28 +56,29 @@ 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<String, CompiledLibrary> translatedLibraryMap;

/** The current context, aka which expression are we currently in. */
private String currentContext;

private Set<String> libraries = new HashSet<>();
private Set<String> valuesets = new HashSet<>();
private Set<String> codes = new HashSet<>();
private Set<String> codesystems = new HashSet<>();
private Set<String> parameters = new HashSet<>();
private Set<String> definitions = new HashSet<>();
private Set<String> functions = new HashSet<>();
private HashMap<String, String> valueSetOids = new HashMap<>();
private Map<String, Map<String, Set<String>>> valueSetDataTypeMap = new HashMap<>();
private Map<String, Map<String, Set<String>>> codeDataTypeMap = new HashMap<>();
@Getter private final Set<String> libraries = new HashSet<>();
@Getter private final Set<String> valuesets = new HashSet<>();
@Getter private final Set<String> codes = new HashSet<>();
@Getter private final Set<String> codesystems = new HashSet<>();
@Getter private final Set<String> parameters = new HashSet<>();
@Getter private final Set<String> definitions = new HashSet<>();
@Getter private final Set<String> functions = new HashSet<>();
@Getter private final HashMap<String, String> valueSetOids = new HashMap<>();
@Getter private final HashMap<String, CQLCode> drcs = new HashMap<>();
@Getter private final Map<String, Map<String, Set<String>>> valueSetDataTypeMap = new HashMap<>();
@Getter private final Map<String, Map<String, Set<String>>> codeDataTypeMap = new HashMap<>();

private Stack<String> namespace = new Stack<>();
private final Stack<String> namespace = new Stack<>();

private CQLGraph graph;
@Getter private final CQLGraph graph;

public Cql2ElmListener(
CQLGraph graph,
Expand Down Expand Up @@ -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())
Expand Down Expand Up @@ -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<String, Set<String>> current = codeDataTypeMap.get(currentContext);
if (current == null) {
Expand All @@ -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());
}
}

Expand Down Expand Up @@ -508,48 +518,4 @@ private void parseChildLibraries(IncludeDef def) throws IOException {
codeDataTypeMap.putAll(listener.getCodeDataTypeMap());
valueSetOids.putAll(listener.getValueSetOids());
}

public Set<String> getLibraries() {
return libraries;
}

public Set<String> getValuesets() {
return valuesets;
}

public Set<String> getCodes() {
return codes;
}

public Set<String> getCodesystems() {
return codesystems;
}

public Set<String> getParameters() {
return parameters;
}

public Set<String> getDefinitions() {
return definitions;
}

public Set<String> getFunctions() {
return functions;
}

public Map<String, Map<String, Set<String>>> getValueSetDataTypeMap() {
return valueSetDataTypeMap;
}

public Map<String, Map<String, Set<String>>> getCodeDataTypeMap() {
return codeDataTypeMap;
}

public Map<String, String> getValueSetOids() {
return valueSetOids;
}

public CQLGraph getGraph() {
return graph;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down

0 comments on commit a3857fc

Please sign in to comment.