use of org.cqframework.cql.cql2elm.CqlTranslatorException in project cqf-ruler by DBCG.
the class Translators method errorsToString.
public static String errorsToString(Iterable<CqlTranslatorException> exceptions) {
ArrayList<String> errors = new ArrayList<>();
for (CqlTranslatorException error : exceptions) {
TrackBack tb = error.getLocator();
String lines = tb == null ? "[n/a]" : String.format("%s[%d:%d, %d:%d]", (tb.getLibrary() != null ? tb.getLibrary().getId() + (tb.getLibrary().getVersion() != null ? ("-" + tb.getLibrary().getVersion()) : "") : ""), tb.getStartLine(), tb.getStartChar(), tb.getEndLine(), tb.getEndChar());
errors.add(lines + error.getMessage());
}
return errors.toString();
}
use of org.cqframework.cql.cql2elm.CqlTranslatorException 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.CqlTranslatorException in project quality-measure-and-cohort-service by Alvearie.
the class CqlToElmTranslator method translate.
public CqlTranslationResult translate(CqlLibrary primaryLibrary, CqlLibrarySourceProvider sourceProvider) {
LibraryBuilder.SignatureLevel signatureLevel = LibraryBuilder.SignatureLevel.None;
LibraryManager libraryManager = newLibraryManager(sourceProvider);
Collection<CqlLibrary> dependencies = new ArrayList<>();
CqlTranslator cqlTranslator = CqlTranslator.fromText(primaryLibrary.getContent(), libraryManager.getModelManager(), libraryManager, /* ucumService= */
null, CqlTranslatorException.ErrorSeverity.Info, signatureLevel, getOptionsArray());
if (cqlTranslator.getErrors().size() > 0) {
throw new CqlTranslatorException("There were errors during cql translation: " + formatMsg(cqlTranslator.getErrors()));
} else if (cqlTranslator.getExceptions().size() > 0) {
throw new CqlTranslatorException("There were exceptions during cql translation: " + formatMsg(cqlTranslator.getExceptions()));
} else if (cqlTranslator.getWarnings().size() > 0) {
String msg = "There were warnings during cql translation:\n" + cqlTranslator.getWarnings().stream().map(Object::toString).collect(Collectors.joining("\n"));
LOG.warn(msg);
}
CqlLibrary translatedLibrary = new CqlLibrary().setDescriptor(new CqlLibraryDescriptor().setLibraryId(primaryLibrary.getDescriptor().getLibraryId()).setVersion(primaryLibrary.getDescriptor().getVersion()).setFormat(Format.ELM)).setContent(cqlTranslator.toXml());
try {
for (TranslatedLibrary tl : libraryManager.getTranslatedLibraries().values()) {
CqlLibrary library = new CqlLibrary().setDescriptor(new CqlLibraryDescriptor().setLibraryId(tl.getLibrary().getIdentifier().getId()).setVersion(tl.getLibrary().getIdentifier().getVersion()).setFormat(Format.ELM)).setContent(CqlTranslator.convertToXml(tl.getLibrary()));
dependencies.add(library);
}
} catch (JAXBException ex) {
throw new RuntimeException("ELM serialization failure", ex);
}
return new CqlTranslationResult(translatedLibrary, dependencies);
}
use of org.cqframework.cql.cql2elm.CqlTranslatorException 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.CqlTranslatorException in project quality-measure-and-cohort-service by Alvearie.
the class CqlToElmTranslator method formatMsg.
/**
* Some of this was adapted from the CQL Translation Server TranslationFailureException.
*
* @param translationErrs List of translation errors.
* @return String representation of the list of translation errors.
*/
private static String formatMsg(List<CqlTranslatorException> translationErrs) {
StringBuilder msg = new StringBuilder();
for (CqlTranslatorException error : translationErrs) {
TrackBack tb = error.getLocator();
String lines = tb == null ? "[n/a]" : String.format("[%s:%s (start:%d:%d, end:%d:%d)]", tb.getLibrary().getId(), tb.getLibrary().getVersion(), tb.getStartLine(), tb.getStartChar(), tb.getEndLine(), tb.getEndChar());
msg.append(String.format("%s %s%n", lines, error.getMessage()));
}
return msg.toString();
}
Aggregations