Skip to content

Commit

Permalink
Partial conversion of the model package to Kotlin (#1455)
Browse files Browse the repository at this point in the history
* WIP

* WIP

* WIP

* merge nondeterminism fixes

* WIP

* Further work on kotlin conversion of model

* Signature converted

* More Signature updates

* Update gradle version, fix deprecations, change XJC task configuration, add foojay resolver to buildSrc

* further updates to signature

* Move FhirLibrarySourceLoader to correct project

* Kotlin for LibraryRef, InstantiationResult

* FunctionHeader

* Fix usage of spread operator

* Version

* PropertyResolution, OperatorResolution, Operator

* Add Kotlin toolchain back

* Small code reduction (#1456)

Small suggestions

* Fixes for Sonar

* QueryContext

* Suppress warnings on refactored code

* Fixes for Sonar warnings

* Make version pattern a constant

---------

Co-authored-by: Anton Vasetenkov <[email protected]>
  • Loading branch information
JPercival and antvaset authored Nov 27, 2024
1 parent b80b613 commit e9b4887
Show file tree
Hide file tree
Showing 48 changed files with 3,722 additions and 4,071 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ kotlin {

java {
toolchain {
languageVersion.set(JavaLanguageVersion.of(17))
languageVersion = JavaLanguageVersion.of(17)
}
}

Expand All @@ -30,7 +30,6 @@ repositories {
}
}


spotless {
java {
targetExclude("**/generated/**")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1905,6 +1905,8 @@ public Expression visitBooleanExpression(cqlParser.BooleanExpressionContext ctx)
public Object visitTimingExpression(cqlParser.TimingExpressionContext ctx) {
Expression left = parseExpression(ctx.expression(0));
Expression right = parseExpression(ctx.expression(1));
requireNonNull(left, "left expression of timing operator can not be null");
requireNonNull(right, "right expression of timing operator can not be null");
TimingOperatorContext timingOperatorContext = new TimingOperatorContext(left, right);
timingOperators.push(timingOperatorContext);
try {
Expand Down Expand Up @@ -3582,7 +3584,6 @@ private Expression getConversionReference(Expression reference) {
if (o != null
&& o.getLibraryName() != null
&& o.getLibraryName().equals(functionRef.getLibraryName())
&& o.getName() != null
&& o.getName().equals(functionRef.getName())) {
return functionRef.getOperand().get(0);
}
Expand Down Expand Up @@ -4172,7 +4173,7 @@ public Expression resolveFunction(
.getId()))) {
Operator op = invocation.getResolution().getOperator();
FunctionHeader fh = getFunctionHeader(op);
if (!fh.getIsCompiled()) {
if (!fh.isCompiled()) {
cqlParser.FunctionDefinitionContext ctx = getFunctionDefinitionContext(fh);
String saveContext = saveCurrentContext(fh.getFunctionDef().getContext());
Stack<Chunk> saveChunks = chunks;
Expand Down Expand Up @@ -4440,7 +4441,7 @@ public FunctionDef compileFunctionDefinition(cqlParser.FunctionDefinitionContext
}

fun.setContext(getCurrentContext());
fh.setIsCompiled();
fh.setCompiled(true);

return fun;
} finally {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1280,12 +1280,7 @@ public CallContext buildCallContext(
}

return new CallContext(
libraryName,
operatorName,
allowPromotionAndDemotion,
allowFluent,
mustResolve,
dataTypes.toArray(new DataType[dataTypes.size()]));
libraryName, operatorName, allowPromotionAndDemotion, allowFluent, mustResolve, dataTypes);
}

public Invocation resolveInvocation(
Expand Down Expand Up @@ -1384,7 +1379,7 @@ public Operator resolveFunctionDefinition(FunctionDef fd) {
false,
fd.isFluent() != null && fd.isFluent(),
false,
dataTypes.toArray(new DataType[dataTypes.size()]));
dataTypes);
// Resolve exact, no conversion map
OperatorResolution resolution = compiledLibrary.resolveCall(callContext, null);
if (resolution != null) {
Expand All @@ -1395,7 +1390,7 @@ public Operator resolveFunctionDefinition(FunctionDef fd) {

public OperatorResolution resolveCall(CallContext callContext) {
OperatorResolution result = null;
if (callContext.getLibraryName() == null || callContext.getLibraryName().equals("")) {
if (callContext.getLibraryName() == null || callContext.getLibraryName().isEmpty()) {
result = compiledLibrary.resolveCall(callContext, conversionMap);
if (result == null) {
result = getSystemLibrary().resolveCall(callContext, conversionMap);
Expand Down Expand Up @@ -2279,7 +2274,7 @@ public PropertyResolution resolveProperty(DataType sourceType, String identifier
"Inconsistent target maps %s and %s for choice type %s",
resultTargetMaps.get(resolution.getType()),
resolution.getTargetMap(),
resolution.getType().toString()));
resolution.getType()));
}
} else {
resultTargetMaps.put(resolution.getType(), resolution.getTargetMap());
Expand All @@ -2293,14 +2288,6 @@ public PropertyResolution resolveProperty(DataType sourceType, String identifier
"Inconsistent property resolution for choice type %s (was %s, is %s)",
choice.toString(), name, resolution.getName()));
}

if (name == null) {
name = resolution.getName();
} else if (!name.equals(resolution.getName())) {
throw new IllegalArgumentException(String.format(
"Inconsistent property resolution for choice type %s (was %s, is %s)",
choice.toString(), name, resolution.getName()));
}
}
}

Expand All @@ -2310,9 +2297,7 @@ public PropertyResolution resolveProperty(DataType sourceType, String identifier
}

if (resultTypes.size() == 1) {
for (DataType resultType : resultTypes) {
return new PropertyResolution(resultType, name, resultTargetMaps);
}
return new PropertyResolution(resultTypes.iterator().next(), name, resultTargetMaps);
}
} else if (currentType instanceof ListType && listTraversal) {
// NOTE: FHIRPath path traversal support
Expand Down Expand Up @@ -2495,10 +2480,7 @@ public Expression resolveIdentifier(String identifier, boolean mustResolve) {

if (element instanceof IncludeDef) {
checkLiteralContext();
LibraryRef libraryRef = new LibraryRef();
libraryRef.setLocalId(of.nextId());
libraryRef.setLibraryName(((IncludeDef) element).getLocalIdentifier());
return libraryRef;
return new LibraryRef(of.nextId(), ((IncludeDef) element).getLocalIdentifier());
}
}

Expand Down Expand Up @@ -3171,7 +3153,7 @@ private DataType getExpressionDefResultType(ExpressionDef expressionDef) {
if (inUnfilteredContext()) {
// If we are in the source clause of a query, indicate that the source references patient context
if (inQueryContext() && getScope().getQueries().peek().inSourceClause()) {
getScope().getQueries().peek().referenceSpecificContext();
getScope().getQueries().peek().setReferencesSpecificContextValue(true);
}

DataType resultType = expressionDef.getResultType();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ private void checkArgumentCount(cqlParser.ParamListContext ctx, String functionN

private AliasedQuerySource enterQueryContext(Expression target) {
QueryContext queryContext = new QueryContext();
queryContext.setIsImplicit(
true); // Set to an implicit context to allow for implicit resolution of property names
queryContext.setImplicit(true);
// Set to an implicit context to allow for implicit resolution of property names
List<AliasedQuerySource> sources = new ArrayList<>();
AliasedQuerySource source =
of.createAliasedQuerySource().withExpression(target).withAlias("$this");
Expand Down

This file was deleted.

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

import org.hl7.cql.model.DataType

class CallContext(
val libraryName: String?,
val operatorName: String,
val allowPromotionAndDemotion: Boolean,
val allowFluent: Boolean,
val mustResolve: Boolean,
operandTypes: List<DataType>
) {
constructor(
libraryName: String?,
operatorName: String,
allowPromotionAndDemotion: Boolean,
allowFluent: Boolean,
mustResolve: Boolean,
vararg operandTypes: DataType
) : this(
libraryName,
operatorName,
allowPromotionAndDemotion,
allowFluent,
mustResolve,
operandTypes.toList()
)

val signature: Signature

init {
require(operatorName.isNotEmpty()) { "operatorName is empty" }
signature = Signature(operandTypes)
}
}

This file was deleted.

Loading

0 comments on commit e9b4887

Please sign in to comment.