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