Skip to content

Commit

Permalink
Convert cql-to-elm preprocessor Java code to Kotlin (#1450)
Browse files Browse the repository at this point in the history
* Rename .java to .kt

* Convert cql-to-elm preprocessor Java code to Kotlin

* Use builtin methods for size and length checks

* Use builtin trimStart method

* Use more builtin methods and vals

* Cleanup

* Cleanup

* Couple of tweaks

* More clean-up

---------

Co-authored-by: JP <[email protected]>
  • Loading branch information
antvaset and JPercival authored Nov 26, 2024
1 parent faf20f1 commit b80b613
Show file tree
Hide file tree
Showing 29 changed files with 1,467 additions and 2,078 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.cqframework.cql.cql2elm;

import static java.util.Objects.requireNonNull;

import java.math.BigDecimal;
import java.util.*;
import java.util.List;
Expand All @@ -20,8 +22,12 @@
import org.hl7.cql.model.*;
import org.hl7.elm.r1.*;
import org.hl7.elm_modelinfo.r1.ModelInfo;
import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Cql2ElmVisitor extends CqlPreprocessorElmCommonVisitor {
private static final Logger log = LoggerFactory.getLogger(Cql2ElmVisitor.class);
private final SystemMethodResolver systemMethodResolver;

private final Set<String> definedExpressionDefinitions = new HashSet<>();
Expand All @@ -37,7 +43,7 @@ public class Cql2ElmVisitor extends CqlPreprocessorElmCommonVisitor {

public Cql2ElmVisitor(LibraryBuilder libraryBuilder, TokenStream tokenStream, LibraryInfo libraryInfo) {
super(libraryBuilder, tokenStream);
this.libraryInfo = Objects.requireNonNull(libraryInfo, "libraryInfo required");
this.setLibraryInfo(requireNonNull(libraryInfo, "libraryInfo required"));
this.systemMethodResolver = new SystemMethodResolver(this, libraryBuilder);
}

Expand Down Expand Up @@ -127,21 +133,13 @@ public UsingDef visitUsingDefinition(cqlParser.UsingDefinitionContext ctx) {
return usingDef;
}

public Model getModel() {
return getModel((String) null);
}

public Model getModel(String modelName) {
return getModel(null, modelName, null, null);
}

@NotNull
public Model getModel(NamespaceInfo modelNamespace, String modelName, String version, String localIdentifier) {
if (modelName == null) {
var defaultUsing = libraryInfo.getDefaultUsingDefinition();
modelName = defaultUsing.getName();
version = defaultUsing.getVersion();
}

requireNonNull(modelName, "modelName");
var modelIdentifier = new ModelIdentifier().withId(modelName).withVersion(version);
if (modelNamespace != null) {
modelIdentifier.setSystem(modelNamespace.getUri());
Expand Down Expand Up @@ -408,8 +406,8 @@ public ConceptDef visitConceptDefinition(cqlParser.ConceptDefinitionContext ctx)
@Override
public NamedTypeSpecifier visitNamedTypeSpecifier(cqlParser.NamedTypeSpecifierContext ctx) {
List<String> qualifiers = parseQualifiers(ctx);
String modelIdentifier = getModelIdentifier(qualifiers);
String identifier = getTypeIdentifier(qualifiers, parseString(ctx.referentialOrTypeNameIdentifier()));
String modelIdentifier = Companion.getModelIdentifier(qualifiers);
String identifier = Companion.getTypeIdentifier(qualifiers, parseString(ctx.referentialOrTypeNameIdentifier()));

final ResultWithPossibleError<NamedTypeSpecifier> retrievedResult =
libraryBuilder.getNamedTypeSpecifierResult(String.format("%s:%s", modelIdentifier, identifier));
Expand Down Expand Up @@ -459,7 +457,7 @@ public Object visitContextDefinition(cqlParser.ContextDefinitionContext ctx) {
if (libraryBuilder.hasUsings()) {
ModelInfo modelInfo = modelIdentifier == null
? libraryBuilder
.getModel(libraryInfo.getDefaultModelName())
.getModel(getLibraryInfo().getDefaultModelName())
.getModelInfo()
: libraryBuilder.getModel(modelIdentifier).getModelInfo();
// String contextTypeName = modelContext.getName();
Expand Down Expand Up @@ -2812,8 +2810,8 @@ public Object visitSetAggregateExpressionTerm(cqlParser.SetAggregateExpressionTe
public Expression visitRetrieve(cqlParser.RetrieveContext ctx) {
libraryBuilder.checkLiteralContext();
List<String> qualifiers = parseQualifiers(ctx.namedTypeSpecifier());
String model = getModelIdentifier(qualifiers);
String label = getTypeIdentifier(
String model = Companion.getModelIdentifier(qualifiers);
String label = Companion.getTypeIdentifier(
qualifiers, parseString(ctx.namedTypeSpecifier().referentialOrTypeNameIdentifier()));
DataType dataType = libraryBuilder.resolveTypeName(model, label);
if (dataType == null) {
Expand Down Expand Up @@ -2910,7 +2908,7 @@ public Expression visitRetrieve(cqlParser.RetrieveContext ctx) {

// Only expand a choice-valued code path if no comparator is specified
// Otherwise, a code comparator will always choose a specific representation
boolean hasFHIRHelpers = libraryInfo.resolveLibraryName("FHIRHelpers") != null;
boolean hasFHIRHelpers = getLibraryInfo().resolveLibraryName("FHIRHelpers") != null;
if (property != null && property.getResultType() instanceof ChoiceType && codeComparator == null) {
for (DataType propertyType : ((ChoiceType) property.getResultType()).getTypes()) {
if (hasFHIRHelpers
Expand Down Expand Up @@ -4043,7 +4041,7 @@ private Expression resolveIdentifier(String identifier) {
// and parameters
Expression result = libraryBuilder.resolveIdentifier(identifier, false);
if (result == null) {
ExpressionDefinitionInfo expressionInfo = libraryInfo.resolveExpressionReference(identifier);
ExpressionDefinitionInfo expressionInfo = getLibraryInfo().resolveExpressionReference(identifier);
if (expressionInfo != null) {
String saveContext = saveCurrentContext(expressionInfo.getContext());
try {
Expand All @@ -4069,7 +4067,7 @@ private Expression resolveIdentifier(String identifier) {
}
}

ParameterDefinitionInfo parameterInfo = libraryInfo.resolveParameterReference(identifier);
ParameterDefinitionInfo parameterInfo = getLibraryInfo().resolveParameterReference(identifier);
if (parameterInfo != null) {
visitParameterDefinition(parameterInfo.getDefinition());
}
Expand Down Expand Up @@ -4140,8 +4138,10 @@ public Expression resolveFunction(

// Find all functionDefinitionInfo instances with the given name
// register each functionDefinitionInfo
if (libraryName == null || libraryName.equals("") || libraryName.equals(this.libraryInfo.getLibraryName())) {
Iterable<FunctionDefinitionInfo> fdis = libraryInfo.resolveFunctionReference(functionName);
if (libraryName == null
|| libraryName.equals("")
|| libraryName.equals(this.getLibraryInfo().getLibraryName())) {
Iterable<FunctionDefinitionInfo> fdis = getLibraryInfo().resolveFunctionReference(functionName);
if (fdis != null) {
for (FunctionDefinitionInfo fdi : fdis) {
String saveContext = saveCurrentContext(fdi.getContext());
Expand Down Expand Up @@ -4353,7 +4353,6 @@ private FunctionHeader getFunctionHeader(Operator op) {
String.format("Could not resolve function header for operator %s", op.getName()));
}
FunctionHeader result = getFunctionHeaderByDef(fd);
// FunctionHeader result = functionHeadersByDef.get(fd);
if (result == null) {
throw new IllegalArgumentException(
String.format("Could not resolve function header for operator %s", op.getName()));
Expand Down Expand Up @@ -4449,7 +4448,7 @@ public FunctionDef compileFunctionDefinition(cqlParser.FunctionDefinitionContext
try {
libraryBuilder.popIdentifier();
} catch (Exception e) {
e.printStackTrace();
log.info("Error popping identifier: {}", e.getMessage());
}
}
// Intentionally do _not_ pop the function name, it needs to remain in global scope!
Expand Down Expand Up @@ -4504,25 +4503,23 @@ private TrackBack getTrackBack(ParseTree tree) {
}

private TrackBack getTrackBack(TerminalNode node) {
TrackBack tb = new TrackBack(
return new TrackBack(
libraryBuilder.getLibraryIdentifier(),
node.getSymbol().getLine(),
node.getSymbol().getCharPositionInLine() + 1, // 1-based instead of 0-based
node.getSymbol().getLine(),
node.getSymbol().getCharPositionInLine()
+ node.getSymbol().getText().length());
return tb;
}

private TrackBack getTrackBack(ParserRuleContext ctx) {
TrackBack tb = new TrackBack(
return new TrackBack(
libraryBuilder.getLibraryIdentifier(),
ctx.getStart().getLine(),
ctx.getStart().getCharPositionInLine() + 1, // 1-based instead of 0-based
ctx.getStop().getLine(),
ctx.getStop().getCharPositionInLine() + ctx.getStop().getText().length() // 1-based instead of 0-based
);
return tb;
}

private void decorate(Element element, TrackBack tb) {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package org.cqframework.cql.cql2elm.preprocessor

import org.antlr.v4.runtime.misc.Interval
import org.antlr.v4.runtime.tree.ParseTree

open class BaseInfo(open val definition: ParseTree?) {
var header: String? = null
var headerInterval: Interval? = null
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package org.cqframework.cql.cql2elm.preprocessor

import org.cqframework.cql.gen.cqlParser.CodeDefinitionContext

/** Created by Bryn on 5/22/2016. */
class CodeDefinitionInfo(val name: String, override val definition: CodeDefinitionContext) :
BaseInfo(definition)

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package org.cqframework.cql.cql2elm.preprocessor

import org.cqframework.cql.gen.cqlParser.CodesystemDefinitionContext

class CodesystemDefinitionInfo(
val name: String,
override val definition: CodesystemDefinitionContext
) : BaseInfo(definition)

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package org.cqframework.cql.cql2elm.preprocessor

import org.cqframework.cql.gen.cqlParser.ConceptDefinitionContext

/** Created by Bryn on 5/22/2016. */
class ConceptDefinitionInfo(val name: String, override val definition: ConceptDefinitionContext) :
BaseInfo(definition)

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package org.cqframework.cql.cql2elm.preprocessor

import org.cqframework.cql.gen.cqlParser.ContextDefinitionContext

class ContextDefinitionInfo(override val definition: ContextDefinitionContext) :
BaseInfo(definition) {
var context: String? = null
}
Loading

0 comments on commit b80b613

Please sign in to comment.