use of org.fhir.ucum.UcumEssenceService in project org.hl7.fhir.core by hapifhir.
the class ValidationEngine method loadCoreDefinitions.
/**
* @param src
* @param recursive
* @param terminologyCachePath
* @param userAgent
* @param tt
* @param loggingService
* @throws FHIRException
* @throws IOException
*
* @see IgLoader#loadIgSource(String, boolean, boolean) loadIgSource for detailed description of the src parameter
*/
private void loadCoreDefinitions(String src, boolean recursive, String terminologyCachePath, String userAgent, TimeTracker tt, IWorkerContext.ILoggingService loggingService) throws FHIRException, IOException {
NpmPackage npm = getPcm().loadPackage(src, null);
if (npm != null) {
version = npm.fhirVersion();
SimpleWorkerContext.SimpleWorkerContextBuilder contextBuilder = new SimpleWorkerContext.SimpleWorkerContextBuilder().withLoggingService(loggingService);
if (terminologyCachePath != null)
contextBuilder = contextBuilder.withTerminologyCachePath(terminologyCachePath);
if (userAgent != null) {
contextBuilder.withUserAgent(userAgent);
}
context = contextBuilder.fromPackage(npm, ValidatorUtils.loaderForVersion(version));
} else {
Map<String, byte[]> source = igLoader.loadIgSource(src, recursive, true);
if (version == null) {
version = getVersionFromPack(source);
}
SimpleWorkerContext.SimpleWorkerContextBuilder contextBuilder = new SimpleWorkerContext.SimpleWorkerContextBuilder();
if (terminologyCachePath != null)
contextBuilder = contextBuilder.withTerminologyCachePath(terminologyCachePath);
if (userAgent != null) {
contextBuilder.withUserAgent(userAgent);
}
context = contextBuilder.fromDefinitions(source, ValidatorUtils.loaderForVersion(version), new PackageVersion(src, new Date()));
ValidatorUtils.grabNatives(getBinaries(), source, "http://hl7.org/fhir");
}
// on https://chat.fhir.org/#narrow/stream/179167-hapi
try {
ClassLoader classLoader = ValidationEngine.class.getClassLoader();
InputStream ue = classLoader.getResourceAsStream("ucum-essence.xml");
context.setUcumService(new UcumEssenceService(ue));
} catch (Exception e) {
throw new FHIRException("Error loading UCUM from embedded ucum-essence.xml: " + e.getMessage(), e);
}
initContext(tt);
}
use of org.fhir.ucum.UcumEssenceService in project org.hl7.fhir.core by hapifhir.
the class TestingUtilities method getWorkerContext.
public static IWorkerContext getWorkerContext(String version) {
FilesystemPackageCacheManager pcm;
try {
pcm = new FilesystemPackageCacheManager(true, ToolsVersion.TOOLS_VERSION);
IWorkerContext fcontext = getWorkerContext(pcm.loadPackage(VersionUtilities.packageForVersion(version), version));
fcontext.setUcumService(new UcumEssenceService(TestingUtilities.loadTestResourceStream("ucum", "ucum-essence.xml")));
fcontext.setExpansionProfile(new Parameters());
if (!fcontext.hasPackage("hl7.terminology", null)) {
NpmPackage utg = new FilesystemPackageCacheManager(true, ToolsVersion.TOOLS_VERSION).loadPackage("hl7.terminology");
System.out.println("Loading THO: " + utg.name() + "#" + utg.version());
fcontext.loadFromPackage(utg, new TestPackageLoader(new String[] { "CodeSystem", "ValueSet" }));
}
return fcontext;
} catch (Exception e) {
e.printStackTrace();
throw new Error(e);
}
}
use of org.fhir.ucum.UcumEssenceService in project org.hl7.fhir.core by hapifhir.
the class UtilitiesXTests method context.
public static IWorkerContext context(String version) {
if (fcontexts == null) {
fcontexts = new HashMap<>();
}
if (!fcontexts.containsKey(version)) {
FilesystemPackageCacheManager pcm;
try {
pcm = new FilesystemPackageCacheManager(true, ToolsVersion.TOOLS_VERSION);
IWorkerContext fcontext = TestingUtilities.getWorkerContext(pcm.loadPackage(VersionUtilities.packageForVersion(version), version), loaderForVersion(version));
fcontext.setUcumService(new UcumEssenceService(UtilitiesXTests.loadTestResourceStream("ucum", "ucum-essence.xml")));
fcontext.setExpansionProfile(new Parameters());
fcontexts.put(version, fcontext);
} catch (Exception e) {
throw new Error(e);
}
}
return fcontexts.get(version);
}
use of org.fhir.ucum.UcumEssenceService in project org.hl7.fhir.core by hapifhir.
the class TestingUtilities method context.
public static IWorkerContext context(String version) {
if ("4.5.0".equals(version)) {
// temporary work around
version = "4.4.0";
}
String v = VersionUtilities.getMajMin(version);
if (fcontexts == null) {
fcontexts = new HashMap<>();
}
if (!fcontexts.containsKey(v)) {
FilesystemPackageCacheManager pcm;
try {
pcm = new FilesystemPackageCacheManager(true, ToolsVersion.TOOLS_VERSION);
IWorkerContext fcontext = SimpleWorkerContext.fromPackage(pcm.loadPackage(VersionUtilities.packageForVersion(version), version));
fcontext.setUcumService(new UcumEssenceService(TestingUtilities.loadTestResourceStream("ucum", "ucum-essence.xml")));
fcontext.setExpansionProfile(new Parameters());
// ((SimpleWorkerContext) fcontext).connectToTSServer(new TerminologyClientR5("http://tx.fhir.org/r4"), null);
fcontexts.put(v, fcontext);
} catch (Exception e) {
e.printStackTrace();
throw new Error(e);
}
}
return fcontexts.get(v);
}
use of org.fhir.ucum.UcumEssenceService 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();
}
Aggregations