Search in sources :

Example 1 with TrackBack

use of org.cqframework.cql.elm.tracking.TrackBack 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();
}
Also used : ArrayList(java.util.ArrayList) TrackBack(org.cqframework.cql.elm.tracking.TrackBack) CqlTranslatorException(org.cqframework.cql.cql2elm.CqlTranslatorException)

Example 2 with TrackBack

use of org.cqframework.cql.elm.tracking.TrackBack 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 3 with TrackBack

use of org.cqframework.cql.elm.tracking.TrackBack 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();
}
Also used : TrackBack(org.cqframework.cql.elm.tracking.TrackBack) CqlTranslatorException(org.cqframework.cql.cql2elm.CqlTranslatorException)

Example 4 with TrackBack

use of org.cqframework.cql.elm.tracking.TrackBack 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 5 with TrackBack

use of org.cqframework.cql.elm.tracking.TrackBack in project clinical_quality_language by cqframework.

the class TranslationTests method testIdentifierLocation.

@Test
public void testIdentifierLocation() throws IOException {
    CqlTranslator translator = TestUtils.createTranslator("TranslatorTests/UnknownIdentifier.cql");
    assertEquals(1, translator.getErrors().size());
    CqlCompilerException e = translator.getErrors().get(0);
    TrackBack tb = e.getLocator();
    assertEquals(6, tb.getStartLine());
    assertEquals(6, tb.getEndLine());
    assertEquals(5, tb.getStartChar());
    assertEquals(10, tb.getEndChar());
}
Also used : TrackBack(org.cqframework.cql.elm.tracking.TrackBack) Test(org.testng.annotations.Test)

Aggregations

TrackBack (org.cqframework.cql.elm.tracking.TrackBack)8 CqlTranslatorException (org.cqframework.cql.cql2elm.CqlTranslatorException)4 ArrayList (java.util.ArrayList)3 CqlTranslator (org.cqframework.cql.cql2elm.CqlTranslator)2 UcumEssenceService (org.fhir.ucum.UcumEssenceService)2 UcumService (org.fhir.ucum.UcumService)2 Test (org.testng.annotations.Test)2 IOException (java.io.IOException)1 StringReader (java.io.StringReader)1 JAXBException (javax.xml.bind.JAXBException)1 FhirLibrarySourceProvider (org.cqframework.cql.cql2elm.FhirLibrarySourceProvider)1 LibraryManager (org.cqframework.cql.cql2elm.LibraryManager)1 ModelManager (org.cqframework.cql.cql2elm.ModelManager)1 Library (org.cqframework.cql.elm.execution.Library)1 BeforeTest (org.testng.annotations.BeforeTest)1