Search in sources :

Example 6 with ValueSetExpanderSimple

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, ValueSetExpansionComponent exp, Parameters expParams, boolean noInactive, ValueSet valueSet) throws ETooCostly, TerminologyServiceException, FileNotFoundException, IOException, FHIRFormatError {
    if (value == null)
        throw fail("unable to find value set with no identity");
    ValueSet vs = context.fetchResource(ValueSet.class, value);
    if (vs == null) {
        if (context.fetchResource(CodeSystem.class, value) != null) {
            throw fail("Cannot include value set " + value + " because it's actually a code system");
        } else {
            throw fail("Unable to find imported value set " + value);
        }
    }
    if (noInactive) {
        expParams = expParams.copy();
        expParams.addParameter("activeOnly", true);
    }
    ValueSetExpansionOutcome vso = new ValueSetExpanderSimple(context, allErrors).expand(vs, expParams);
    if (vso.getError() != null) {
        addErrors(vso.getAllErrors());
        throw fail("Unable to expand imported value set " + vs.getUrl() + ": " + vso.getError());
    }
    if (vs.hasVersion())
        if (!existsInParams(exp.getParameter(), "version", new UriType(vs.getUrl() + "|" + vs.getVersion())))
            exp.getParameter().add(new ValueSetExpansionParameterComponent().setName("version").setValue(new UriType(vs.getUrl() + "|" + vs.getVersion())));
    for (Extension ex : vso.getValueset().getExpansion().getExtension()) {
        if (ex.getUrl().equals(ToolingExtensions.EXT_EXP_TOOCOSTLY)) {
            if (ex.getValue() instanceof BooleanType) {
                exp.getExtension().add(new Extension(ToolingExtensions.EXT_EXP_TOOCOSTLY).setValue(new UriType(value)));
            } else {
                exp.getExtension().add(ex);
            }
        }
    }
    for (ValueSetExpansionParameterComponent p : vso.getValueset().getExpansion().getParameter()) {
        if (!existsInParams(exp.getParameter(), p.getName(), p.getValue()))
            exp.getParameter().add(p);
    }
    if (isValueSetUnionImports(valueSet)) {
        copyExpansion(vso.getValueset().getExpansion().getContains());
    }
    // 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 : Extension(org.hl7.fhir.r5.model.Extension) ValueSetExpansionParameterComponent(org.hl7.fhir.r5.model.ValueSet.ValueSetExpansionParameterComponent) BooleanType(org.hl7.fhir.r5.model.BooleanType) ValueSet(org.hl7.fhir.r5.model.ValueSet) CodeSystem(org.hl7.fhir.r5.model.CodeSystem) UriType(org.hl7.fhir.r5.model.UriType)

Example 7 with ValueSetExpanderSimple

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, ValueSetExpansionComponent exp, Parameters expParams) throws ETooCostly, TerminologyServiceException, FileNotFoundException, IOException, FHIRFormatError {
    if (value == null)
        throw fail("unable to find value set with no identity");
    ValueSet vs = context.fetchResource(ValueSet.class, value);
    if (vs == null) {
        if (context.fetchResource(CodeSystem.class, value) != null) {
            throw fail("Cannot include value set " + value + " because it's actually a code system");
        } else {
            throw fail("Unable to find imported value set " + value);
        }
    }
    ValueSetExpansionOutcome vso = new ValueSetExpanderSimple(context, allErrors).expand(vs, expParams);
    if (vso.getError() != null) {
        addErrors(vso.getAllErrors());
        throw fail("Unable to expand imported value set " + vs.getUrl() + ": " + vso.getError());
    }
    if (vs.hasVersion())
        if (!existsInParams(exp.getParameter(), "version", new UriType(vs.getUrl() + "|" + vs.getVersion())))
            exp.getParameter().add(new ValueSetExpansionParameterComponent().setName("version").setValue(new UriType(vs.getUrl() + "|" + vs.getVersion())));
    for (Extension ex : vso.getValueset().getExpansion().getExtension()) {
        if (ex.getUrl().equals(ToolingExtensions.EXT_EXP_TOOCOSTLY)) {
            if (ex.getValue() instanceof BooleanType) {
                exp.getExtension().add(new Extension(ToolingExtensions.EXT_EXP_TOOCOSTLY).setValue(new UriType(value)));
            } else {
                exp.getExtension().add(ex);
            }
        }
    }
    for (ValueSetExpansionParameterComponent p : vso.getValueset().getExpansion().getParameter()) {
        if (!existsInParams(exp.getParameter(), p.getName(), p.getValue()))
            exp.getParameter().add(p);
    }
    copyExpansion(vso.getValueset().getExpansion().getContains());
    // 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 : Extension(org.hl7.fhir.r4b.model.Extension) ValueSetExpansionParameterComponent(org.hl7.fhir.r4b.model.ValueSet.ValueSetExpansionParameterComponent) BooleanType(org.hl7.fhir.r4b.model.BooleanType) ValueSet(org.hl7.fhir.r4b.model.ValueSet) CodeSystem(org.hl7.fhir.r4b.model.CodeSystem) UriType(org.hl7.fhir.r4b.model.UriType)

Example 8 with ValueSetExpanderSimple

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 hierarchical, boolean incompleteOk, Parameters pIn) {
    if (pIn == null) {
        throw new Error(formatMessage(I18nConstants.NO_PARAMETERS_PROVIDED_TO_EXPANDVS));
    }
    Parameters p = pIn.copy();
    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, hierarchical);
    ValueSetExpansionOutcome res;
    if (cacheOk) {
        res = txCache.getExpansion(cacheToken);
        if (res != null) {
            return res;
        }
    }
    p.setParameter("includeDefinition", false);
    p.setParameter("excludeNested", !hierarchical);
    if (incompleteOk) {
        p.setParameter("incomplete-ok", true);
    }
    List<String> allErrors = new ArrayList<>();
    // ok, first we try to expand locally
    ValueSetExpanderSimple vse = constructValueSetExpanderSimple();
    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");
    txLog("$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 : Parameters(org.hl7.fhir.r5.model.Parameters) StringType(org.hl7.fhir.r5.model.StringType) HashMap(java.util.HashMap) CacheToken(org.hl7.fhir.r5.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.r5.model.ValueSet.ConceptSetComponent) ValueSetExpansionOutcome(org.hl7.fhir.r5.terminologies.ValueSetExpander.ValueSetExpansionOutcome) ValueSetExpanderSimple(org.hl7.fhir.r5.terminologies.ValueSetExpanderSimple) ValueSet(org.hl7.fhir.r5.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