Search in sources :

Example 1 with ValueSetExpanderSimple

use of org.hl7.fhir.r5.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();
}
Also used : ValueSetExpansionParameterComponent(org.hl7.fhir.r4.model.ValueSet.ValueSetExpansionParameterComponent) NoTerminologyServiceException(org.hl7.fhir.exceptions.NoTerminologyServiceException) TerminologyServiceException(org.hl7.fhir.exceptions.TerminologyServiceException) ValueSet(org.hl7.fhir.r4.model.ValueSet) UriType(org.hl7.fhir.r4.model.UriType)

Example 2 with ValueSetExpanderSimple

use of org.hl7.fhir.r5.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;
}
Also used : HashMap(java.util.HashMap) CacheToken(org.hl7.fhir.r4.context.TerminologyCache.CacheToken) ValueSetExpansionOutcome(org.hl7.fhir.r4.terminologies.ValueSetExpander.ValueSetExpansionOutcome) ValueSetExpanderSimple(org.hl7.fhir.r4.terminologies.ValueSetExpanderSimple) DefinitionException(org.hl7.fhir.exceptions.DefinitionException) IOException(java.io.IOException) TerminologyServiceException(org.hl7.fhir.exceptions.TerminologyServiceException) FileNotFoundException(java.io.FileNotFoundException) FHIRException(org.hl7.fhir.exceptions.FHIRException)

Example 3 with ValueSetExpanderSimple

use of org.hl7.fhir.r5.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);
}
Also used : Parameters(org.hl7.fhir.r5.model.Parameters) ValueSet(org.hl7.fhir.r5.model.ValueSet) ValueSetExpander(org.hl7.fhir.r5.terminologies.ValueSetExpander) Test(org.junit.jupiter.api.Test)

Example 4 with ValueSetExpanderSimple

use of org.hl7.fhir.r5.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());
}
Also used : Parameters(org.hl7.fhir.r5.model.Parameters) ValueSet(org.hl7.fhir.r5.model.ValueSet) ValueSetExpander(org.hl7.fhir.r5.terminologies.ValueSetExpander) Test(org.junit.jupiter.api.Test)

Example 5 with ValueSetExpanderSimple

use of org.hl7.fhir.r5.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;
}
Also used : StringType(org.hl7.fhir.r4b.model.StringType) HashMap(java.util.HashMap) CacheToken(org.hl7.fhir.r4b.context.TerminologyCache.CacheToken) ArrayList(java.util.ArrayList) TerminologyServiceException(org.hl7.fhir.exceptions.TerminologyServiceException) FileNotFoundException(java.io.FileNotFoundException) NoTerminologyServiceException(org.hl7.fhir.exceptions.NoTerminologyServiceException) DefinitionException(org.hl7.fhir.exceptions.DefinitionException) IOException(java.io.IOException) FHIRException(org.hl7.fhir.exceptions.FHIRException) ConceptSetComponent(org.hl7.fhir.r4b.model.ValueSet.ConceptSetComponent) ValueSetExpansionOutcome(org.hl7.fhir.r4b.terminologies.ValueSetExpander.ValueSetExpansionOutcome) ValueSetExpanderSimple(org.hl7.fhir.r4b.terminologies.ValueSetExpanderSimple) ValueSet(org.hl7.fhir.r4b.model.ValueSet)

Aggregations

TerminologyServiceException (org.hl7.fhir.exceptions.TerminologyServiceException)4 ValueSet (org.hl7.fhir.r5.model.ValueSet)4 FileNotFoundException (java.io.FileNotFoundException)3 IOException (java.io.IOException)3 HashMap (java.util.HashMap)3 DefinitionException (org.hl7.fhir.exceptions.DefinitionException)3 FHIRException (org.hl7.fhir.exceptions.FHIRException)3 NoTerminologyServiceException (org.hl7.fhir.exceptions.NoTerminologyServiceException)3 Parameters (org.hl7.fhir.r5.model.Parameters)3 ArrayList (java.util.ArrayList)2 ValueSet (org.hl7.fhir.r4b.model.ValueSet)2 ValueSetExpander (org.hl7.fhir.r5.terminologies.ValueSetExpander)2 Test (org.junit.jupiter.api.Test)2 CacheToken (org.hl7.fhir.r4.context.TerminologyCache.CacheToken)1 UriType (org.hl7.fhir.r4.model.UriType)1 ValueSet (org.hl7.fhir.r4.model.ValueSet)1 ValueSetExpansionParameterComponent (org.hl7.fhir.r4.model.ValueSet.ValueSetExpansionParameterComponent)1 ValueSetExpansionOutcome (org.hl7.fhir.r4.terminologies.ValueSetExpander.ValueSetExpansionOutcome)1 ValueSetExpanderSimple (org.hl7.fhir.r4.terminologies.ValueSetExpanderSimple)1 CacheToken (org.hl7.fhir.r4b.context.TerminologyCache.CacheToken)1