Skip to content

Commit

Permalink
Fix model multiplatform
Browse files Browse the repository at this point in the history
  • Loading branch information
JPercival committed Dec 31, 2024
1 parent dd5099d commit 9637574
Show file tree
Hide file tree
Showing 52 changed files with 399 additions and 455 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ kotlin {

jvmTest {
dependencies {
implementation(kotlin("test-junit"))
implementation(kotlin("test-junit5"))
}
}

Expand All @@ -95,6 +95,10 @@ kotlin {
}
}

tasks.withType<Test> {
useJUnitPlatform()
}

tasks.register<Jar>("dokkaHtmlJar") {
dependsOn(tasks.dokkaHtml)
from(tasks.dokkaHtml.flatMap { it.outputDirectory })
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package org.cqframework.fhir.npm;

import static kotlinx.io.CoreKt.buffered;
import static kotlinx.io.JvmCoreKt.asSource;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
Expand Down Expand Up @@ -52,16 +55,17 @@ public ModelInfo load(ModelIdentifier modelIdentifier) {
}
InputStream is = new ByteArrayInputStream(a.getData());
var reader = new XmlModelInfoReader();
return reader.read(is);

var source = buffered(asSource(is));
return reader.read(source);
}
}
}
} catch (IOException e) {
logger.logDebugMessage(
ILoggingService.LogCategory.PROGRESS,
String.format(
"Exceptions occurred attempting to load npm library for model %s",
modelIdentifier.toString()));
"Exceptions occurred attempting to load npm library for model %s", modelIdentifier));
}
}

Expand Down
3 changes: 1 addition & 2 deletions Src/java/cql-to-elm-cli/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,9 @@ dependencies {
implementation(project(":model-xmlutil"))
implementation(project(":elm-xmlutil"))
implementation(project(":ucum"))
implementation("org.jetbrains.kotlinx:kotlinx-io-core-jvm:0.6.0")
implementation("net.sf.jopt-simple:jopt-simple:4.7")
implementation("org.slf4j:slf4j-simple:2.0.13")
implementation("org.glassfish.jaxb:jaxb-runtime:4.0.5")
implementation("org.eclipse.persistence:org.eclipse.persistence.moxy:4.0.2")
testImplementation(project(":model-xmlutil"))
testImplementation(project(":elm-xmlutil"))
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package org.cqframework.cql.cql2elm.cli;

import static java.nio.file.FileVisitResult.CONTINUE;
import static kotlinx.io.CoreKt.buffered;
import static kotlinx.io.JvmCoreKt.asSource;
import static org.cqframework.cql.cql2elm.CqlTranslator.fromFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.file.FileVisitResult;
Expand All @@ -27,8 +30,9 @@
public class Main {
public static ModelInfoProvider getModelInfoProvider(File modelInfoXML) {
try {
var source = buffered(asSource(new FileInputStream(modelInfoXML)));
final ModelInfo modelInfo =
ModelInfoReaderFactory.getReader("application/xml").read(modelInfoXML);
ModelInfoReaderFactory.INSTANCE.getReader("application/xml").read(source);
return (ModelIdentifier modelIdentifier) -> modelInfo;
} catch (IOException e) {
System.err.printf("Could not load model-info XML: %s%n", modelInfoXML);
Expand Down
2 changes: 2 additions & 0 deletions Src/java/cql-to-elm/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ dependencies {
api(project(":model"))
api(project(":elm"))

implementation("org.jetbrains.kotlinx:kotlinx-io-core-jvm:0.6.0")

// Temporary until we can get rid of the dependency on wrapping
// the CQL annotations in a JAXBElement for narrative generation
implementation("jakarta.xml.bind:jakarta.xml.bind-api:4.0.1")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ package org.cqframework.cql.cql2elm

import java.io.*
import java.nio.file.Path
import kotlinx.io.asSource
import kotlinx.io.buffered
import org.cqframework.cql.cql2elm.model.Version
import org.hl7.cql.model.ModelIdentifier
import org.hl7.cql.model.ModelInfoProvider
Expand Down Expand Up @@ -100,7 +102,8 @@ class DefaultModelInfoProvider() : ModelInfoProvider, PathAware {
}
try {
val inputStream: InputStream = FileInputStream(modelFile)
return ModelInfoReaderFactory.getReader("application/xml")?.read(inputStream)
return ModelInfoReaderFactory.getReader("application/xml")
?.read(inputStream.asSource().buffered())
} catch (e: IOException) {
throw IllegalArgumentException(
"Could not load definition for model info ${modelIdentifier.id}.",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ class ConversionMap {
private fun getAllConversions(fromType: DataType?): List<Conversion> {
val conversions: MutableList<Conversion> = ArrayList()
var currentType = fromType
while (currentType != null) {
while (currentType != null && currentType != DataType.ANY) {
conversions.addAll(getConversions(currentType))
currentType = currentType.baseType
}
Expand Down Expand Up @@ -339,7 +339,7 @@ class ConversionMap {

if (result == null) {
// NOTE: FHIRPath Implicit conversion from list to singleton
// If the from type is a list and the target type is a singleton (potentially with a
// If the fromType is a list and the target type is a singleton (potentially with a
// compatible conversion),
// Convert by invoking a singleton
result =
Expand All @@ -361,7 +361,7 @@ class ConversionMap {
(allowPromotionAndDemotion || isIntervalPromotionEnabled) ->
findIntervalPromotion(fromType, toType, operatorMap)

// If the from type is a choice, attempt to find a conversion from one of the
// If the fromType is a choice, attempt to find a conversion from one of the
// choice
// types
fromType is ChoiceType ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -600,7 +600,7 @@ class ModelImporter(val modelInfo: ModelInfo, val modelManager: ModelManager?) {
if (t is ProfileInfo) {
ProfileType(
qualifiedName,
resolveTypeNameOrSpecifier(t.baseType, t.baseTypeSpecifier)
resolveTypeNameOrSpecifier(t.baseType, t.baseTypeSpecifier) ?: DataType.ANY
)
} else {
// Added to support generic notation in ModelInfo file for class type names
Expand All @@ -615,6 +615,7 @@ class ModelImporter(val modelInfo: ModelInfo, val modelManager: ModelManager?) {
ClassType(
qualifiedName,
resolveTypeNameOrSpecifier(t.baseType, t.baseTypeSpecifier)
?: DataType.ANY
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,17 +71,20 @@ static void setup() {
@Test
void namespacePath() {
assertThat(
NamespaceManager.getPath(defaultNamespaceInfo.getUri(), "Main"), is("http://cql.hl7.org/public/Main"));
NamespaceManager.Companion.getPath(defaultNamespaceInfo.getUri(), "Main"),
is("http://cql.hl7.org/public/Main"));
}

@Test
void namespaceNamePart() {
assertThat(NamespaceManager.getNamePart("http://cql.hl7.org/public/Main"), is("Main"));
assertThat(NamespaceManager.Companion.getNamePart("http://cql.hl7.org/public/Main"), is("Main"));
}

@Test
void namespaceUriPart() {
assertThat(NamespaceManager.getUriPart("http://cql.hl7.org/public/Main"), is("http://cql.hl7.org/public"));
assertThat(
NamespaceManager.Companion.getUriPart("http://cql.hl7.org/public/Main"),
is("http://cql.hl7.org/public"));
}

/* Ensure base functionality with a defaulted namespace uri */
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package org.cqframework.cql.cql2elm;

import java.io.IOException;
import static kotlinx.io.CoreKt.buffered;
import static kotlinx.io.JvmCoreKt.asSource;

import org.hl7.cql.model.ModelIdentifier;
import org.hl7.cql.model.ModelInfoProvider;
import org.hl7.elm_modelinfo.r1.ModelInfo;
Expand All @@ -18,15 +20,8 @@ public TestFhirModelInfoProvider(Class clazz) {

public ModelInfo load(ModelIdentifier modelIdentifier) {
if (modelIdentifier.getId().equals("FHIR")) {
try {
return ModelInfoReaderFactory.getReader("application/xml")
.read(clazz.getResourceAsStream("fhir-modelinfo-1.8.xml"));
} catch (IOException e) {
e.printStackTrace();
// Do not throw, allow other providers to resolve
// throw new IllegalArgumentException(String.format("Unknown version %s of the QDM model.",
// localVersion));
}
var source = buffered(asSource(clazz.getResourceAsStream("fhir-modelinfo-1.8.xml")));
return ModelInfoReaderFactory.INSTANCE.getReader("application/xml").read(source);
}

return null;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package org.cqframework.cql.cql2elm;

import java.io.IOException;
import static kotlinx.io.CoreKt.buffered;
import static kotlinx.io.JvmCoreKt.asSource;

import org.hl7.cql.model.ModelIdentifier;
import org.hl7.cql.model.ModelInfoProvider;
import org.hl7.elm_modelinfo.r1.ModelInfo;
Expand All @@ -9,15 +11,8 @@
public class TestModelInfoProvider implements ModelInfoProvider {
public ModelInfo load(ModelIdentifier modelIdentifier) {
if (modelIdentifier.getId().equals("Test")) {
try {
return ModelInfoReaderFactory.getReader("application/xml")
.read(TestModelInfoProvider.class.getResourceAsStream("ModelTests/test-modelinfo.xml"));
} catch (IOException e) {
e.printStackTrace();
// Do not throw, allow other providers to resolve
// throw new IllegalArgumentException(String.format("Unknown version %s of the QDM model.",
// localVersion));
}
var stream = TestModelInfoProvider.class.getResourceAsStream("ModelTests/test-modelinfo.xml");
return ModelInfoReaderFactory.INSTANCE.getReader("application/xml").read(buffered(asSource(stream)));
}

return null;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package org.cqframework.cql.cql2elm.model;

import java.io.IOException;
import static kotlinx.io.CoreKt.buffered;
import static kotlinx.io.JvmCoreKt.asSource;

import java.io.InputStream;
import org.hl7.cql.model.ModelIdentifier;
import org.hl7.cql.model.ModelInfoProvider;
Expand All @@ -11,16 +13,9 @@ public class GentestModelInfoProvider implements ModelInfoProvider {
@Override
public ModelInfo load(ModelIdentifier modelIdentifier) {
if (modelIdentifier.getId().equals("GENTEST")) {
try {
InputStream is = GentestModelInfoProvider.class.getResourceAsStream(
"/org/cqframework/cql/cql2elm/ModelTests/test-modelinfowithgenerics-happy.xml");
return ModelInfoReaderFactory.getReader("application/xml").read(is);
} catch (IOException e) {
e.printStackTrace();
// Do not throw, allow other providers to resolve
// throw new IllegalArgumentException(String.format("Unknown version %s of the GENTEST model.",
// localVersion));
}
InputStream is = GentestModelInfoProvider.class.getResourceAsStream(
"/org/cqframework/cql/cql2elm/ModelTests/test-modelinfowithgenerics-happy.xml");
return ModelInfoReaderFactory.INSTANCE.getReader("application/xml").read(buffered(asSource(is)));
}

return null;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package org.cqframework.cql.cql2elm.model;

import java.io.IOException;
import static kotlinx.io.CoreKt.buffered;
import static kotlinx.io.JvmCoreKt.asSource;

import java.io.InputStream;
import org.hl7.cql.model.ModelIdentifier;
import org.hl7.cql.model.ModelInfoProvider;
Expand All @@ -11,16 +13,9 @@ public class GentestModelInfoProviderSad1 implements ModelInfoProvider {
@Override
public ModelInfo load(ModelIdentifier modelIdentifier) {
if (modelIdentifier.equals("GENTEST")) {
try {
InputStream is = GentestModelInfoProviderSad1.class.getResourceAsStream(
"/org/cqframework/cql/cql2elm/ModelTests/test-modelinfowithgenerics-sad1.xml");
return ModelInfoReaderFactory.getReader("application/xml").read(is);
} catch (IOException e) {
e.printStackTrace();
// Do not throw, allow other providers to resolve
// throw new IllegalArgumentException(String.format("Unknown version %s of the GENTEST model.",
// localVersion));
}
InputStream is = GentestModelInfoProviderSad1.class.getResourceAsStream(
"/org/cqframework/cql/cql2elm/ModelTests/test-modelinfowithgenerics-sad1.xml");
return ModelInfoReaderFactory.INSTANCE.getReader("application/xml").read(buffered(asSource(is)));
}

return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ void mixedMultipleSourceQuery() {
def,
hasTypeAndResult(
Query.class,
"list<tuple{P:tuple{id:System.Integer,name:System.String},S:tuple{id:System.Integer,name:System.String}}>"));
"list<tuple{S:tuple{id:System.Integer,name:System.String},P:tuple{id:System.Integer,name:System.String}}>"));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -714,8 +714,8 @@ public CompiledLibrary resolveLibrary(VersionedIdentifier libraryIdentifier, Str

public CompiledLibrary resolveLibraryFromIncludeDef(IncludeDef includeDef) {
VersionedIdentifier targetLibraryIdentifier = new VersionedIdentifier()
.withSystem(NamespaceManager.getUriPart(includeDef.getPath()))
.withId(NamespaceManager.getNamePart(includeDef.getPath()))
.withSystem(NamespaceManager.Companion.getUriPart(includeDef.getPath()))
.withId(NamespaceManager.Companion.getNamePart(includeDef.getPath()))
.withVersion(includeDef.getVersion());

return resolveLibrary(targetLibraryIdentifier);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -589,8 +589,8 @@ private org.hl7.fhir.r5.model.RelatedArtifact toRelatedArtifact(
}

private String getReferenceUrl(String path, String version) {
String uri = NamespaceManager.getUriPart(path);
String name = NamespaceManager.getNamePart(path);
String uri = NamespaceManager.Companion.getUriPart(path);
String name = NamespaceManager.Companion.getNamePart(path);

if (uri != null) {
// The translator has no way to correctly infer the namespace of the FHIRHelpers library, since it will
Expand Down
1 change: 1 addition & 0 deletions Src/java/elm-xmlutil/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ dependencies {
api(project(":model"))
api(project(":elm"))

implementation("org.jetbrains.kotlinx:kotlinx-io-core:0.6.0")
implementation("io.github.pdvrieze.xmlutil:core:0.90.3")
implementation("io.github.pdvrieze.xmlutil:serialization:0.90.3")
implementation("io.github.pdvrieze.xmlutil:serialization-jvm:0.90.3")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -303,8 +303,8 @@ private Library loadAndValidate(VersionedIdentifier libraryIdentifier) {
if (library.getIncludes() != null && library.getIncludes().getDef() != null) {
for (IncludeDef include : library.getIncludes().getDef()) {
this.loadAndValidate(new VersionedIdentifier()
.withSystem(NamespaceManager.getUriPart(include.getPath()))
.withId(NamespaceManager.getNamePart(include.getPath()))
.withSystem(NamespaceManager.Companion.getUriPart(include.getPath()))
.withId(NamespaceManager.Companion.getNamePart(include.getPath()))
.withVersion(include.getVersion()));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,8 @@ public static List<FunctionDef> getFunctionDefs(final String name, final Library

public static VersionedIdentifier toVersionedIdentifier(IncludeDef includeDef) {
return new VersionedIdentifier()
.withSystem(NamespaceManager.getUriPart(includeDef.getPath()))
.withId(NamespaceManager.getNamePart(includeDef.getPath()))
.withSystem(NamespaceManager.Companion.getUriPart(includeDef.getPath()))
.withId(NamespaceManager.Companion.getNamePart(includeDef.getPath()))
.withVersion(includeDef.getVersion());
}
}
1 change: 1 addition & 0 deletions Src/java/model-xmlutil/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ plugins {
dependencies {
api(project(":model"))

implementation("org.jetbrains.kotlinx:kotlinx-io-core:0.6.0")
implementation("io.github.pdvrieze.xmlutil:core:0.90.3")
implementation("io.github.pdvrieze.xmlutil:serialization:0.90.3")
implementation("io.github.pdvrieze.xmlutil:serialization-jvm:0.90.3")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,7 @@ import org.hl7.elm_modelinfo.r1.serializing.ModelInfoReader
import org.hl7.elm_modelinfo.r1.serializing.ModelInfoReaderProvider

class ModelInfoReaderProvider : ModelInfoReaderProvider {
override fun create(contentType: String): ModelInfoReader? {
var contentType: String? = contentType
if (contentType == null) {
contentType = "application/xml"
}
override fun create(contentType: String): ModelInfoReader {
return when (contentType) {
"application/xml" -> XmlModelInfoReader()
else ->
Expand Down
Loading

0 comments on commit 9637574

Please sign in to comment.