use of org.cqframework.cql.elm.execution.Library in project quality-measure-and-cohort-service by Alvearie.
the class MapCqlLibraryProviderZipIntegrationTest method testLibraryFoundInZipSuccess.
@Test
public void testLibraryFoundInZipSuccess() throws Exception {
MapCqlLibraryProviderFactory factory = new MapCqlLibraryProviderFactory();
CqlLibraryProvider provider = factory.fromZipFile(Paths.get("src/test/resources/cql/zip/breast_cancer_screening_v1_0_0_cql.zip"));
CqlLibraryProvider fhirClasspathProvider = new ClasspathCqlLibraryProvider();
provider = new PriorityCqlLibraryProvider(provider, fhirClasspathProvider);
CqlToElmTranslator translator = new CqlToElmTranslator();
provider = new TranslatingCqlLibraryProvider(provider, translator, false);
LibraryLoader libraryLoader = new ProviderBasedLibraryLoader(provider);
VersionedIdentifier id = new VersionedIdentifier().withId("BreastCancerScreening");
Library library = libraryLoader.load(id);
Assert.assertEquals("BreastCancerScreening", library.getIdentifier().getId());
}
use of org.cqframework.cql.elm.execution.Library in project CRD by HL7-DaVinci.
the class CqlExecution method translate.
public static Library translate(String cql, LibraryManager libraryManager, ModelManager modelManager) throws Exception {
ArrayList<CqlTranslator.Options> options = new ArrayList<>();
options.add(CqlTranslator.Options.EnableDateRangeOptimization);
UcumService ucumService = new UcumEssenceService(UcumEssenceService.class.getResourceAsStream("/ucum-essence.xml"));
CqlTranslator translator = CqlTranslator.fromText(cql, modelManager, libraryManager, ucumService, options.toArray(new CqlTranslator.Options[options.size()]));
if (translator.getErrors().size() > 0) {
ArrayList<String> errors = new ArrayList<>();
for (CqlTranslatorException error : translator.getErrors()) {
TrackBack tb = error.getLocator();
String lines = tb == null ? "[n/a]" : String.format("[%d:%d, %d:%d]", tb.getStartLine(), tb.getStartChar(), tb.getEndLine(), tb.getEndChar());
errors.add(lines + error.getMessage());
}
throw new IllegalArgumentException(errors.toString());
}
Library library = null;
try {
library = CqlLibraryReader.read(new StringReader(translator.toXml()));
} catch (IOException e) {
e.printStackTrace();
} catch (JAXBException e) {
e.printStackTrace();
}
return library;
}
use of org.cqframework.cql.elm.execution.Library in project CRD by HL7-DaVinci.
the class LocalLibraryLoader method loadLibrary.
private Library loadLibrary(VersionedIdentifier libraryIdentifier) {
List<CqlTranslatorException> errors = new ArrayList<>();
org.hl7.elm.r1.VersionedIdentifier identifier = new org.hl7.elm.r1.VersionedIdentifier().withId(libraryIdentifier.getId()).withSystem(libraryIdentifier.getSystem()).withVersion(libraryIdentifier.getVersion());
CqlTranslatorOptions options = new CqlTranslatorOptions();
org.cqframework.cql.cql2elm.model.TranslatedLibrary translatedLibrary = libraryManager.resolveLibrary(identifier, options, errors);
String xml;
try {
JAXBContext jc = JAXBContext.newInstance(org.hl7.elm.r1.Library.class, Annotation.class);
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
StringWriter writer = new StringWriter();
marshaller.marshal(new ObjectFactory().createLibrary(translatedLibrary.getLibrary()), writer);
xml = writer.getBuffer().toString();
} catch (JAXBException e) {
throw new RuntimeException(String.format("Errors encountered while loading library %s: %s", libraryIdentifier.getId(), e.getMessage()));
}
Library library = null;
try {
library = CqlLibraryReader.read(new StringReader(xml));
} catch (IOException | JAXBException e) {
throw new RuntimeException(String.format("Errors encountered while loading library %s: %s", libraryIdentifier.getId(), e.getMessage()));
}
return library;
}
use of org.cqframework.cql.elm.execution.Library in project CRD by HL7-DaVinci.
the class LocalLibraryLoader method resolveLibrary.
private Library resolveLibrary(VersionedIdentifier libraryIdentifier) {
if (libraryIdentifier == null) {
throw new IllegalArgumentException("Library identifier is null.");
}
if (libraryIdentifier.getId() == null) {
throw new IllegalArgumentException("Library identifier id is null.");
}
Library library = libraries.get(libraryIdentifier.getId());
if (library != null && libraryIdentifier.getVersion() != null && !libraryIdentifier.getVersion().equals(library.getIdentifier().getVersion())) {
throw new IllegalArgumentException(String.format("Could not load library %s, version %s because version %s is already loaded.", libraryIdentifier.getId(), libraryIdentifier.getVersion(), library.getIdentifier().getVersion()));
} else {
library = loadLibrary(libraryIdentifier);
libraries.put(libraryIdentifier.getId(), library);
}
return library;
}
Aggregations