Search in sources :

Example 1 with ModelManager

use of org.cqframework.cql.cql2elm.ModelManager 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 ModelManager

use of org.cqframework.cql.cql2elm.ModelManager in project quality-measure-and-cohort-service by Alvearie.

the class CQLParameterTests method toLibrary.

public Library toLibrary(String text) throws Exception {
    ModelManager modelManager = new ModelManager();
    LibraryManager libraryManager = new LibraryManager(modelManager);
    CqlTranslator translator = CqlTranslator.fromText(text, modelManager, libraryManager);
    assertEquals(translator.getErrors().toString(), 0, translator.getErrors().size());
    String xml = translator.toXml();
    return CqlLibraryReader.read(new ByteArrayInputStream(xml.getBytes(StandardCharsets.UTF_8)));
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) CqlTranslator(org.cqframework.cql.cql2elm.CqlTranslator) LibraryManager(org.cqframework.cql.cql2elm.LibraryManager) ModelManager(org.cqframework.cql.cql2elm.ModelManager)

Example 3 with ModelManager

use of org.cqframework.cql.cql2elm.ModelManager 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)

Example 4 with ModelManager

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

the class CqlExecutionContextBuilder method getExecutionContext.

public static Context getExecutionContext(CqlRule cqlRule, HashMap<String, Resource> cqlParams, String baseUrl) {
    ModelManager modelManager = new ModelManager();
    LibraryManager libraryManager = new LibraryManager(modelManager);
    libraryManager.getLibrarySourceLoader().clearProviders();
    Library library = null;
    LibraryLoader libraryLoader = null;
    if (cqlRule.isPrecompiled()) {
    // todo
    } else {
        libraryManager.getLibrarySourceLoader().registerProvider(cqlRule.getRawCqlLibrarySourceProvider(CQL_VERSION));
        libraryLoader = new LocalLibraryLoader(libraryManager);
        try {
            library = CqlExecution.translate(cqlRule.getRawMainCqlLibrary(CQL_VERSION), libraryManager, modelManager);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    FhirContext fhirContext = FhirContext.forR4();
    Context context = new Context(library);
    context.registerLibraryLoader(libraryLoader);
    context.setExpressionCaching(true);
    R4FhirModelResolver modelResolver = new R4FhirModelResolver();
    RestFhirRetrieveProvider retrieveProvider = new RestFhirRetrieveProvider(new SearchParameterResolver(fhirContext), fhirContext.newRestfulGenericClient("http://fhirtest.uhn.ca/baseR4"));
    CompositeDataProvider provider = new CompositeDataProvider(modelResolver, retrieveProvider);
    context.registerDataProvider("http://hl7.org/fhir", provider);
    for (Map.Entry<String, org.hl7.fhir.r4.model.Resource> entry : cqlParams.entrySet()) {
        context.setParameter(null, entry.getKey(), entry.getValue());
    }
    context.setParameter(null, "base_url", baseUrl);
    return context;
}
Also used : Context(org.opencds.cqf.cql.engine.execution.Context) FhirContext(ca.uhn.fhir.context.FhirContext) FhirContext(ca.uhn.fhir.context.FhirContext) Resource(org.hl7.fhir.r4.model.Resource) ModelManager(org.cqframework.cql.cql2elm.ModelManager) LibraryLoader(org.opencds.cqf.cql.engine.execution.LibraryLoader) LocalLibraryLoader(org.hl7.davinci.endpoint.cql.LocalLibraryLoader) R4FhirModelResolver(org.opencds.cqf.cql.engine.fhir.model.R4FhirModelResolver) SearchParameterResolver(org.opencds.cqf.cql.engine.fhir.searchparam.SearchParameterResolver) LocalLibraryLoader(org.hl7.davinci.endpoint.cql.LocalLibraryLoader) RestFhirRetrieveProvider(org.opencds.cqf.cql.engine.fhir.retrieve.RestFhirRetrieveProvider) LibraryManager(org.cqframework.cql.cql2elm.LibraryManager) CompositeDataProvider(org.opencds.cqf.cql.engine.data.CompositeDataProvider) Library(org.cqframework.cql.elm.execution.Library) HashMap(java.util.HashMap) Map(java.util.Map)

Example 5 with ModelManager

use of org.cqframework.cql.cql2elm.ModelManager in project clinical_quality_language by cqframework.

the class CqlComparisonOperatorsTest method setup.

@BeforeTest
public void setup() throws IOException {
    ModelManager modelManager = new ModelManager();
    CqlTranslator translator = CqlTranslator.fromStream(CqlComparisonOperatorsTest.class.getResourceAsStream("../OperatorTests/CqlComparisonOperators.cql"), modelManager, new LibraryManager(modelManager));
    assertThat(translator.getErrors().size(), is(0));
    Library library = translator.toELM();
    defs = new HashMap<>();
    if (library.getStatements() != null) {
        for (ExpressionDef def : library.getStatements().getDef()) {
            defs.put(def.getName(), def);
        }
    }
}
Also used : ExpressionDef(org.hl7.elm.r1.ExpressionDef) CqlTranslator(org.cqframework.cql.cql2elm.CqlTranslator) LibraryManager(org.cqframework.cql.cql2elm.LibraryManager) Library(org.hl7.elm.r1.Library) ModelManager(org.cqframework.cql.cql2elm.ModelManager) BeforeTest(org.testng.annotations.BeforeTest)

Aggregations

ModelManager (org.cqframework.cql.cql2elm.ModelManager)21 LibraryManager (org.cqframework.cql.cql2elm.LibraryManager)19 CqlTranslator (org.cqframework.cql.cql2elm.CqlTranslator)17 BeforeTest (org.testng.annotations.BeforeTest)14 ByteArrayInputStream (java.io.ByteArrayInputStream)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 CqlTranslatorException (org.cqframework.cql.cql2elm.CqlTranslatorException)2 FhirLibrarySourceProvider (org.cqframework.cql.cql2elm.FhirLibrarySourceProvider)2 Library (org.cqframework.cql.elm.execution.Library)2 ExpressionDef (org.hl7.elm.r1.ExpressionDef)2 Library (org.hl7.elm.r1.Library)2 FhirContext (ca.uhn.fhir.context.FhirContext)1 CqlEvaluationRequest (com.ibm.cohort.cql.evaluation.CqlEvaluationRequest)1 CqlEvaluationRequests (com.ibm.cohort.cql.evaluation.CqlEvaluationRequests)1 CqlLibrary (com.ibm.cohort.cql.library.CqlLibrary)1 CqlLibraryDescriptor (com.ibm.cohort.cql.library.CqlLibraryDescriptor)1 CqlLibraryProvider (com.ibm.cohort.cql.library.CqlLibraryProvider)1 Format (com.ibm.cohort.cql.library.Format)1 ContextDefinition (com.ibm.cohort.cql.spark.aggregation.ContextDefinition)1