Search in sources :

Example 1 with FhirLibrarySourceProvider

use of org.cqframework.cql.cql2elm.FhirLibrarySourceProvider in project cqf-ruler by DBCG.

the class CqlBuilder method validate.

private void validate(String cql) {
    ModelManager modelManager = new ModelManager();
    LibraryManager libraryManager = new LibraryManager(modelManager);
    libraryManager.getLibrarySourceLoader().registerProvider(new FhirLibrarySourceProvider());
    CqlTranslator translator;
    try {
        translator = CqlTranslator.fromStream(new ByteArrayInputStream(cql.getBytes()), modelManager, libraryManager);
    } catch (IOException e) {
        throw new CqlTranslatorException("Error validating cql", e);
    }
    if (translator.getErrors().size() > 0) {
        throw new CqlTranslatorException("Error validating cql: " + Translators.errorsToString(translator.getErrors()));
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) CqlTranslator(org.cqframework.cql.cql2elm.CqlTranslator) LibraryManager(org.cqframework.cql.cql2elm.LibraryManager) IOException(java.io.IOException) ModelManager(org.cqframework.cql.cql2elm.ModelManager) FhirLibrarySourceProvider(org.cqframework.cql.cql2elm.FhirLibrarySourceProvider) CqlTranslatorException(org.cqframework.cql.cql2elm.CqlTranslatorException)

Example 2 with FhirLibrarySourceProvider

use of org.cqframework.cql.cql2elm.FhirLibrarySourceProvider in project CRD by HL7-DaVinci.

the class CqlExecution method translateToElm.

public static String translateToElm(String cql, LibrarySourceProvider librarySourceProvider) throws Exception {
    ModelManager modelManager = new ModelManager();
    LibraryManager libraryManager = new LibraryManager(modelManager);
    libraryManager.getLibrarySourceLoader().registerProvider(new FhirLibrarySourceProvider());
    if (librarySourceProvider != null) {
        libraryManager.getLibrarySourceLoader().registerProvider(librarySourceProvider);
    }
    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()]));
    libraryManager.getLibrarySourceLoader().clearProviders();
    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());
    }
    return translator.toJson();
}
Also used : CqlTranslator(org.cqframework.cql.cql2elm.CqlTranslator) ArrayList(java.util.ArrayList) ModelManager(org.cqframework.cql.cql2elm.ModelManager) CqlTranslatorException(org.cqframework.cql.cql2elm.CqlTranslatorException) UcumEssenceService(org.fhir.ucum.UcumEssenceService) UcumService(org.fhir.ucum.UcumService) LibraryManager(org.cqframework.cql.cql2elm.LibraryManager) TrackBack(org.cqframework.cql.elm.tracking.TrackBack) FhirLibrarySourceProvider(org.cqframework.cql.cql2elm.FhirLibrarySourceProvider)

Aggregations

CqlTranslator (org.cqframework.cql.cql2elm.CqlTranslator)2 CqlTranslatorException (org.cqframework.cql.cql2elm.CqlTranslatorException)2 FhirLibrarySourceProvider (org.cqframework.cql.cql2elm.FhirLibrarySourceProvider)2 LibraryManager (org.cqframework.cql.cql2elm.LibraryManager)2 ModelManager (org.cqframework.cql.cql2elm.ModelManager)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 TrackBack (org.cqframework.cql.elm.tracking.TrackBack)1 UcumEssenceService (org.fhir.ucum.UcumEssenceService)1 UcumService (org.fhir.ucum.UcumService)1