Search in sources :

Example 11 with Library

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());
}
Also used : VersionedIdentifier(org.cqframework.cql.elm.execution.VersionedIdentifier) TranslatingCqlLibraryProvider(com.ibm.cohort.cql.translation.TranslatingCqlLibraryProvider) CqlToElmTranslator(com.ibm.cohort.cql.translation.CqlToElmTranslator) TranslatingCqlLibraryProvider(com.ibm.cohort.cql.translation.TranslatingCqlLibraryProvider) Library(org.cqframework.cql.elm.execution.Library) LibraryLoader(org.opencds.cqf.cql.engine.execution.LibraryLoader) Test(org.junit.Test)

Example 12 with Library

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;
}
Also used : CqlTranslator(org.cqframework.cql.cql2elm.CqlTranslator) JAXBException(javax.xml.bind.JAXBException) ArrayList(java.util.ArrayList) IOException(java.io.IOException) CqlTranslatorException(org.cqframework.cql.cql2elm.CqlTranslatorException) UcumEssenceService(org.fhir.ucum.UcumEssenceService) UcumService(org.fhir.ucum.UcumService) StringReader(java.io.StringReader) TrackBack(org.cqframework.cql.elm.tracking.TrackBack) Library(org.cqframework.cql.elm.execution.Library)

Example 13 with 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;
}
Also used : Marshaller(javax.xml.bind.Marshaller) JAXBException(javax.xml.bind.JAXBException) ArrayList(java.util.ArrayList) JAXBContext(javax.xml.bind.JAXBContext) IOException(java.io.IOException) CqlTranslatorOptions(org.cqframework.cql.cql2elm.CqlTranslatorOptions) CqlTranslatorException(org.cqframework.cql.cql2elm.CqlTranslatorException) StringWriter(java.io.StringWriter) ObjectFactory(org.hl7.elm.r1.ObjectFactory) StringReader(java.io.StringReader) Library(org.cqframework.cql.elm.execution.Library)

Example 14 with 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;
}
Also used : Library(org.cqframework.cql.elm.execution.Library)

Aggregations

Library (org.cqframework.cql.elm.execution.Library)14 VersionedIdentifier (org.cqframework.cql.elm.execution.VersionedIdentifier)6 LibraryLoader (org.opencds.cqf.cql.engine.execution.LibraryLoader)5 CqlLibrary (com.ibm.cohort.cql.library.CqlLibrary)4 HashMap (java.util.HashMap)4 Map (java.util.Map)4 Test (org.junit.Test)4 CqlToElmTranslator (com.ibm.cohort.cql.translation.CqlToElmTranslator)3 JAXBException (javax.xml.bind.JAXBException)3 CqlLibraryDescriptor (com.ibm.cohort.cql.library.CqlLibraryDescriptor)2 CqlLibraryProvider (com.ibm.cohort.cql.library.CqlLibraryProvider)2 CqlVersionedIdentifier (com.ibm.cohort.cql.library.CqlVersionedIdentifier)2 ProviderBasedLibraryLoader (com.ibm.cohort.cql.library.ProviderBasedLibraryLoader)2 TranslatingCqlLibraryProvider (com.ibm.cohort.cql.translation.TranslatingCqlLibraryProvider)2 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 StringReader (java.io.StringReader)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Collectors (java.util.stream.Collectors)2