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()));
}
}
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)));
}
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();
}
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;
}
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);
}
}
}
Aggregations