Search in sources :

Example 1 with UcumService

use of org.fhir.ucum.UcumService 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 2 with UcumService

use of org.fhir.ucum.UcumService in project clinical_quality_language by cqframework.

the class CqlTranslator method writeELM.

private static void writeELM(Path inPath, Path outPath, CqlTranslator.Format format, CqlTranslatorOptions options) throws IOException {
    System.err.println("================================================================================");
    System.err.printf("TRANSLATE %s%n", inPath);
    ModelManager modelManager;
    if (options.getOptions().contains(CqlTranslatorOptions.Options.DisableDefaultModelInfoLoad)) {
        modelManager = new ModelManager(false);
    } else {
        modelManager = new ModelManager();
    }
    LibraryManager libraryManager = new LibraryManager(modelManager);
    UcumService ucumService = null;
    if (options.getValidateUnits()) {
        try {
            ucumService = new UcumEssenceService(UcumEssenceService.class.getResourceAsStream("/ucum-essence.xml"));
        } catch (UcumException e) {
            System.err.println("Could not create UCUM validation service:");
            e.printStackTrace();
        }
    }
    modelManager.getModelInfoLoader().registerModelInfoProvider(new DefaultModelInfoProvider(inPath.getParent()), true);
    libraryManager.getLibrarySourceLoader().registerProvider(new DefaultLibrarySourceProvider(inPath.getParent()));
    libraryManager.getLibrarySourceLoader().registerProvider(new FhirLibrarySourceProvider());
    CqlTranslator translator = fromFile(inPath.toFile(), modelManager, libraryManager, ucumService, options);
    libraryManager.getLibrarySourceLoader().clearProviders();
    if (translator.getErrors().size() > 0) {
        System.err.println("Translation failed due to errors:");
        outputExceptions(translator.getExceptions());
    } else if (!options.getVerifyOnly()) {
        if (translator.getExceptions().size() == 0) {
            System.err.println("Translation completed successfully.");
        } else {
            System.err.println("Translation completed with messages:");
            outputExceptions(translator.getExceptions());
        }
        try (PrintWriter pw = new PrintWriter(outPath.toFile(), "UTF-8")) {
            switch(format) {
                case COFFEE:
                    pw.print("module.exports = ");
                    pw.println(translator.toJson());
                    break;
                case JXSON:
                    pw.println(translator.toJxson());
                    break;
                case JSON:
                    pw.println(translator.toJson());
                    break;
                case XML:
                default:
                    pw.println(translator.toXml());
            }
            pw.println();
        }
        System.err.println(String.format("ELM output written to: %s", outPath.toString()));
    }
    System.err.println();
}
Also used : UcumEssenceService(org.fhir.ucum.UcumEssenceService) UcumService(org.fhir.ucum.UcumService) UcumException(org.fhir.ucum.UcumException)

Example 3 with UcumService

use of org.fhir.ucum.UcumService in project org.hl7.fhir.core by hapifhir.

the class IEEE11073Convertor method main.

/**
 * argument 1: path to the rosetta csv file
 * argument 2: basePath to produce files to
 *
 * @param args
 * @throws Exception
 */
public static void main(String[] args) throws Exception {
    UcumService ucum = new UcumEssenceService(UCUM_PATH);
    CodeSystem mdc = generateMDC(args[0], args[1], ucum);
    ConceptMap loinc = generateLoincMdcMap(mdc, args[1], args[2]);
}
Also used : UcumEssenceService(org.fhir.ucum.UcumEssenceService) UcumService(org.fhir.ucum.UcumService)

Example 4 with UcumService

use of org.fhir.ucum.UcumService 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)

Aggregations

UcumEssenceService (org.fhir.ucum.UcumEssenceService)4 UcumService (org.fhir.ucum.UcumService)4 ArrayList (java.util.ArrayList)2 CqlTranslator (org.cqframework.cql.cql2elm.CqlTranslator)2 CqlTranslatorException (org.cqframework.cql.cql2elm.CqlTranslatorException)2 TrackBack (org.cqframework.cql.elm.tracking.TrackBack)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 UcumException (org.fhir.ucum.UcumException)1