Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Translator supports namespaces #1496

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,24 @@
import org.hl7.fhir.utilities.TextFile;
import org.hl7.fhir.utilities.Utilities;
import org.hl7.fhir.utilities.VersionUtilities;
import org.slf4j.LoggerFactory;

public class IGContext {
private static class DefaultLogger implements ILoggingService {
private final org.slf4j.Logger log = LoggerFactory.getLogger(IGContext.class);

public void logMessage(String s) {
log.warn(s);
}

public void logDebugMessage(LogCategory logCategory, String s) {
log.debug("{}: {}", logCategory, s);
}

public boolean isDebugLogging() {
return log.isDebugEnabled();
}
}

private ILoggingService logger;

Expand Down Expand Up @@ -69,6 +85,10 @@ public IGContext(ILoggingService logger) {
this.logger = logger;
}

public IGContext() {
this.logger = new DefaultLogger();
}

public void initializeFromIg(String rootDir, String igPath, String fhirVersion) {
this.rootDir = rootDir;

Expand Down
3 changes: 3 additions & 0 deletions Src/java/cql-to-elm-cli/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ application {

dependencies {
implementation project(':cql-to-elm')
implementation project(':cqf-fhir')
implementation project(':cqf-fhir-npm')
implementation project(':quick')
implementation project(':qdm')
implementation project(':model-jaxb')
Expand All @@ -17,6 +19,7 @@ dependencies {
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'
implementation "ca.uhn.hapi.fhir:hapi-fhir-structures-r5"
testImplementation project(':model-jaxb')
testImplementation project(':elm-jaxb')
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,14 @@
import org.cqframework.cql.cql2elm.*;
import org.cqframework.cql.cql2elm.quick.FhirLibrarySourceProvider;
import org.cqframework.cql.elm.tracking.TrackBack;
import org.cqframework.fhir.npm.LibraryLoader;
import org.cqframework.fhir.npm.NpmLibrarySourceProvider;
import org.cqframework.fhir.npm.NpmPackageManager;
import org.cqframework.fhir.utilities.IGContext;
import org.hl7.cql.model.ModelIdentifier;
import org.hl7.cql.model.ModelInfoProvider;
import org.hl7.cql.model.NamespaceInfo;
import org.hl7.cql.model.NamespaceManager;
import org.hl7.elm_modelinfo.r1.ModelInfo;
import org.hl7.elm_modelinfo.r1.serializing.ModelInfoReaderFactory;

Expand Down Expand Up @@ -54,6 +60,7 @@ private static void writeELM(
Path outPath,
org.cqframework.cql.cql2elm.CqlTranslator.Format format,
ModelInfoProvider modelProvider,
IGContext igContext,
CqlCompilerOptions options)
throws IOException {

Expand All @@ -77,7 +84,28 @@ private static void writeELM(
.registerModelInfoProvider(new DefaultModelInfoProvider(inPath.getParent()), true);
libraryManager.getLibrarySourceLoader().registerProvider(new DefaultLibrarySourceProvider(inPath.getParent()));
libraryManager.getLibrarySourceLoader().registerProvider(new FhirLibrarySourceProvider());
org.cqframework.cql.cql2elm.CqlTranslator translator = fromFile(inPath.toFile(), libraryManager);

NamespaceManager namespaceManager = libraryManager.getNamespaceManager();
NamespaceInfo namespaceInfo = null;
if (igContext != null) {
NpmPackageManager pm = new NpmPackageManager(igContext.getSourceIg());
pm.getNpmList().forEach(npm -> {
NamespaceInfo newNamespace = new NamespaceInfo(npm.id(), npm.canonical());
namespaceManager.ensureNamespaceRegistered(newNamespace);
});
LibraryLoader reader = new LibraryLoader(igContext.getFhirVersion());
NpmLibrarySourceProvider sp = new NpmLibrarySourceProvider(pm.getNpmList(), reader, pm);
libraryManager.getLibrarySourceLoader().registerProvider(sp);

String packageId = igContext.getPackageId();
String canonicalBase = igContext.getCanonicalBase();

if (packageId != null && !packageId.isEmpty() && canonicalBase != null && !canonicalBase.isEmpty()) {
namespaceInfo = new NamespaceInfo(packageId, canonicalBase);
}
}

CqlTranslator translator = fromFile(namespaceInfo, inPath.toFile(), libraryManager);
libraryManager.getLibrarySourceLoader().clearProviders();

if (!translator.getErrors().isEmpty()) {
Expand Down Expand Up @@ -135,6 +163,10 @@ public static void main(String[] args) throws IOException, InterruptedException
.ofType(org.cqframework.cql.cql2elm.CqlTranslator.Format.class)
.defaultsTo(org.cqframework.cql.cql2elm.CqlTranslator.Format.XML)
.describedAs("The target format for the output");
OptionSpec<File> rootDir = parser.accepts("root-dir")
.withOptionalArg()
.ofType(File.class)
.describedAs("Root directory of a FHIR IG project, used to resolve CQL namespaces");
OptionSpec disableDefaultModelInfoLoad = parser.accepts("disable-default-modelinfo-load");
OptionSpec verify = parser.accepts("verify");
OptionSpec optimization = parser.accepts("date-range-optimization");
Expand Down Expand Up @@ -231,6 +263,12 @@ public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IO
throw new IllegalArgumentException("input and output file must be different!");
}

IGContext igContext = null;
if (options.has(rootDir)) {
igContext = new IGContext();
igContext.initializeFromIni(options.valueOf("root-dir") + File.separator + "ig.ini");
}

ModelInfoProvider modelProvider = null;
if (options.has(model)) {
final File modelFile = options.valueOf(model);
Expand All @@ -245,6 +283,7 @@ public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IO
out,
outputFormat,
modelProvider,
igContext,
new CqlCompilerOptions(
options.has(optimization),
options.has(debug) || options.has(annotations),
Expand Down