use of org.hl7.fhir.r4.terminologies.ValueSetExpanderSimple in project org.hl7.fhir.core by hapifhir.
the class ValueSetExpanderSimple method importValueSet.
private ValueSet importValueSet(String value, List<ValueSetExpansionParameterComponent> params, Parameters expParams) throws ETooCostly, TerminologyServiceException, FileNotFoundException, IOException, FHIRFormatError {
if (value == null)
throw new TerminologyServiceException("unable to find value set with no identity");
ValueSet vs = context.fetchResource(ValueSet.class, value);
if (vs == null)
throw new TerminologyServiceException("Unable to find imported value set " + value);
ValueSetExpansionOutcome vso = new ValueSetExpanderSimple(context).expand(vs, expParams);
if (vso.getError() != null)
throw new TerminologyServiceException("Unable to expand imported value set: " + vso.getError());
if (vs.hasVersion())
if (!existsInParams(params, "version", new UriType(vs.getUrl() + "|" + vs.getVersion())))
params.add(new ValueSetExpansionParameterComponent().setName("version").setValue(new UriType(vs.getUrl() + "|" + vs.getVersion())));
for (ValueSetExpansionParameterComponent p : vso.getValueset().getExpansion().getParameter()) {
if (!existsInParams(params, p.getName(), p.getValue()))
params.add(p);
}
// if we're importing a value set, we have to be combining, so we won't try for a heirarchy
canBeHeirarchy = false;
return vso.getValueset();
}
use of org.hl7.fhir.r4.terminologies.ValueSetExpanderSimple in project org.hl7.fhir.core by hapifhir.
the class BaseWorkerContext method expandVS.
public ValueSetExpansionOutcome expandVS(ValueSet vs, boolean cacheOk, boolean heirarchical, Parameters p) {
if (p == null)
throw new Error("No Parameters provided to expandVS");
if (vs.hasExpansion()) {
return new ValueSetExpansionOutcome(vs.copy());
}
if (!vs.hasUrl())
throw new Error("no value set");
CacheToken cacheToken = txCache.generateExpandToken(vs, heirarchical);
ValueSetExpansionOutcome res;
if (cacheOk) {
res = txCache.getExpansion(cacheToken);
if (res != null)
return res;
}
p.setParameter("includeDefinition", false);
p.setParameter("excludeNested", !heirarchical);
// ok, first we try to expand locally
try {
ValueSetExpanderSimple vse = new ValueSetExpanderSimple(this);
res = vse.doExpand(vs, p);
if (!res.getValueset().hasUrl())
throw new Error("no url in expand value set");
txCache.cacheExpansion(cacheToken, res, TerminologyCache.TRANSIENT);
return res;
} catch (Exception e) {
}
// if that failed, we try to expand on the server
if (noTerminologyServer)
return new ValueSetExpansionOutcome("Error expanding ValueSet: running without terminology services", TerminologyServiceErrorClass.NOSERVICE);
Map<String, String> params = new HashMap<String, String>();
params.put("_limit", Integer.toString(expandCodesLimit));
params.put("_incomplete", "true");
tlog("$expand on " + txCache.summary(vs));
try {
ValueSet result = txClient.expandValueset(vs, p, params);
if (!result.hasUrl())
result.setUrl(vs.getUrl());
if (!result.hasUrl())
throw new Error("no url in expand value set 2");
res = new ValueSetExpansionOutcome(result).setTxLink(txLog.getLastId());
} catch (Exception e) {
res = new ValueSetExpansionOutcome(e.getMessage() == null ? e.getClass().getName() : e.getMessage(), TerminologyServiceErrorClass.UNKNOWN).setTxLink(txLog == null ? null : txLog.getLastId());
}
txCache.cacheExpansion(cacheToken, res, TerminologyCache.PERMANENT);
return res;
}
use of org.hl7.fhir.r4.terminologies.ValueSetExpanderSimple in project org.hl7.fhir.core by hapifhir.
the class SimpleWorkerContextTests method testExpandValueSet4ArgsWithClient.
@Test
public void testExpandValueSet4ArgsWithClient() throws IOException {
ValueSet vs = new ValueSet();
vs.setUrl(DUMMY_URL);
Mockito.doReturn(cacheToken).when(terminologyCache).generateExpandToken(vs, true);
Parameters pIn = new Parameters();
ValueSet expectedValueSet = new ValueSet();
expectedValueSet.setUrl("dummyUrl2");
Mockito.doReturn(expectedExpansionResult).when(valueSetExpanderSimple).expand(eq(vs), argThat(new ParametersMatcher(pInWithDependentResources)));
Mockito.doReturn(valueSetExpanderSimple).when(context).constructValueSetExpanderSimple();
Mockito.doReturn(expectedValueSet).when(terminologyClient).expandValueset(eq(vs), argThat(new ParametersMatcher(pInWithDependentResources)), eq(params));
ValueSetExpander.ValueSetExpansionOutcome actualExpansionResult = context.expandVS(vs, true, true, true, pIn);
assertEquals(expectedValueSet, actualExpansionResult.getValueset());
Mockito.verify(terminologyCache).getExpansion(cacheToken);
Mockito.verify(terminologyCache).cacheExpansion(cacheToken, actualExpansionResult, true);
}
use of org.hl7.fhir.r4.terminologies.ValueSetExpanderSimple in project org.hl7.fhir.core by hapifhir.
the class SimpleWorkerContextTests method testExpandValueSet4ArgsWithValueSetExpanderSimple.
@Test
public void testExpandValueSet4ArgsWithValueSetExpanderSimple() throws IOException {
ValueSet vs = new ValueSet();
vs.setUrl(DUMMY_URL);
Mockito.doReturn(cacheToken).when(terminologyCache).generateExpandToken(vs, true);
Parameters pIn = new Parameters();
Mockito.doReturn(vs).when(expectedExpansionResult).getValueset();
Mockito.doReturn(expectedExpansionResult).when(valueSetExpanderSimple).expand(eq(vs), argThat(new ParametersMatcher(pInWithDependentResources)));
Mockito.doReturn(valueSetExpanderSimple).when(context).constructValueSetExpanderSimple();
ValueSetExpander.ValueSetExpansionOutcome actualExpansionResult = context.expandVS(vs, true, true, true, pIn);
assertEquals(expectedExpansionResult, actualExpansionResult);
Mockito.verify(terminologyCache).getExpansion(cacheToken);
Mockito.verify(terminologyCache).cacheExpansion(cacheToken, actualExpansionResult, false);
Mockito.verify(terminologyClient, times(0)).expandValueset(any(), any(), any());
}
use of org.hl7.fhir.r4.terminologies.ValueSetExpanderSimple in project org.hl7.fhir.core by hapifhir.
the class BaseWorkerContext method expandVS.
public ValueSetExpansionOutcome expandVS(ValueSet vs, boolean cacheOk, boolean heirarchical, boolean incompleteOk, Parameters p) {
if (p == null) {
throw new Error(formatMessage(I18nConstants.NO_PARAMETERS_PROVIDED_TO_EXPANDVS));
}
if (vs.hasExpansion()) {
return new ValueSetExpansionOutcome(vs.copy());
}
if (!vs.hasUrl()) {
throw new Error(formatMessage(I18nConstants.NO_VALUE_SET_IN_URL));
}
for (ConceptSetComponent inc : vs.getCompose().getInclude()) {
codeSystemsUsed.add(inc.getSystem());
}
for (ConceptSetComponent inc : vs.getCompose().getExclude()) {
codeSystemsUsed.add(inc.getSystem());
}
CacheToken cacheToken = txCache.generateExpandToken(vs, heirarchical);
ValueSetExpansionOutcome res;
if (cacheOk) {
res = txCache.getExpansion(cacheToken);
if (res != null) {
return res;
}
}
p.setParameter("includeDefinition", false);
p.setParameter("excludeNested", !heirarchical);
if (incompleteOk) {
p.setParameter("incomplete-ok", true);
}
List<String> allErrors = new ArrayList<>();
// ok, first we try to expand locally
ValueSetExpanderSimple vse = new ValueSetExpanderSimple(this);
try {
res = vse.expand(vs, p);
allErrors.addAll(vse.getAllErrors());
if (res.getValueset() != null) {
if (!res.getValueset().hasUrl()) {
throw new Error(formatMessage(I18nConstants.NO_URL_IN_EXPAND_VALUE_SET));
}
txCache.cacheExpansion(cacheToken, res, TerminologyCache.TRANSIENT);
return res;
}
} catch (Exception e) {
allErrors.addAll(vse.getAllErrors());
e.printStackTrace();
}
// if that failed, we try to expand on the server
if (addDependentResources(p, vs)) {
p.addParameter().setName("cache-id").setValue(new StringType(cacheId));
}
if (noTerminologyServer) {
return new ValueSetExpansionOutcome(formatMessage(I18nConstants.ERROR_EXPANDING_VALUESET_RUNNING_WITHOUT_TERMINOLOGY_SERVICES), TerminologyServiceErrorClass.NOSERVICE, allErrors);
}
Map<String, String> params = new HashMap<String, String>();
params.put("_limit", Integer.toString(expandCodesLimit));
params.put("_incomplete", "true");
tlog("$expand on " + txCache.summary(vs));
try {
ValueSet result = txClient.expandValueset(vs, p, params);
if (!result.hasUrl()) {
result.setUrl(vs.getUrl());
}
if (!result.hasUrl()) {
throw new Error(formatMessage(I18nConstants.NO_URL_IN_EXPAND_VALUE_SET_2));
}
res = new ValueSetExpansionOutcome(result).setTxLink(txLog.getLastId());
} catch (Exception e) {
res = new ValueSetExpansionOutcome(e.getMessage() == null ? e.getClass().getName() : e.getMessage(), TerminologyServiceErrorClass.UNKNOWN, allErrors).setTxLink(txLog == null ? null : txLog.getLastId());
}
txCache.cacheExpansion(cacheToken, res, TerminologyCache.PERMANENT);
return res;
}
Aggregations