Search in sources :

Example 21 with TerminologyServiceException

use of org.hl7.fhir.exceptions.TerminologyServiceException in project org.hl7.fhir.core by hapifhir.

the class ValueSetRenderer method getConceptsForCodes.

private Map<String, ConceptDefinitionComponent> getConceptsForCodes(CodeSystem e, ConceptSetComponent inc) {
    if (e == null) {
        e = getContext().getWorker().fetchCodeSystem(inc.getSystem());
    }
    ValueSetExpansionComponent vse = null;
    if (!context.isNoSlowLookup() && !getContext().getWorker().hasCache()) {
        try {
            ValueSetExpansionOutcome vso = getContext().getWorker().expandVS(inc, false);
            ValueSet valueset = vso.getValueset();
            if (valueset == null)
                throw new TerminologyServiceException("Error Expanding ValueSet: " + vso.getError());
            vse = valueset.getExpansion();
        } catch (TerminologyServiceException e1) {
            return null;
        }
    }
    Map<String, ConceptDefinitionComponent> results = new HashMap<>();
    List<CodingValidationRequest> serverList = new ArrayList<>();
    // 1st pass, anything we can resolve internally
    for (ConceptReferenceComponent cc : inc.getConcept()) {
        String code = cc.getCode();
        ConceptDefinitionComponent v = null;
        if (e != null) {
            v = getConceptForCode(e.getConcept(), code);
        }
        if (v == null && vse != null) {
            v = getConceptForCodeFromExpansion(vse.getContains(), code);
        }
        if (v != null) {
            results.put(code, v);
        } else {
            serverList.add(new CodingValidationRequest(new Coding(inc.getSystem(), code, null)));
        }
    }
    if (!context.isNoSlowLookup() && !serverList.isEmpty()) {
        getContext().getWorker().validateCodeBatch(getContext().getTerminologyServiceOptions(), serverList, null);
        for (CodingValidationRequest vr : serverList) {
            ConceptDefinitionComponent v = vr.getResult().asConceptDefinition();
            if (v != null) {
                results.put(vr.getCoding().getCode(), v);
            }
        }
    }
    return results;
}
Also used : ValueSetExpansionComponent(org.hl7.fhir.r4b.model.ValueSet.ValueSetExpansionComponent) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ConceptReferenceComponent(org.hl7.fhir.r4b.model.ValueSet.ConceptReferenceComponent) ConceptDefinitionComponent(org.hl7.fhir.r4b.model.CodeSystem.ConceptDefinitionComponent) Coding(org.hl7.fhir.r4b.model.Coding) ValueSetExpansionOutcome(org.hl7.fhir.r4b.terminologies.ValueSetExpander.ValueSetExpansionOutcome) TerminologyServiceException(org.hl7.fhir.exceptions.TerminologyServiceException) ValueSet(org.hl7.fhir.r4b.model.ValueSet) CodingValidationRequest(org.hl7.fhir.r4b.context.IWorkerContext.CodingValidationRequest)

Example 22 with TerminologyServiceException

use of org.hl7.fhir.exceptions.TerminologyServiceException in project org.hl7.fhir.core by hapifhir.

the class ValueSetExpanderSimple method doInternalIncludeCodes.

public void doInternalIncludeCodes(ConceptSetComponent inc, ValueSetExpansionComponent exp, Parameters expParams, List<ValueSet> imports, CodeSystem cs) throws NoTerminologyServiceException, TerminologyServiceException, FHIRException {
    if (cs == null) {
        if (context.isNoTerminologyServer())
            throw failTSE("Unable to find code system " + inc.getSystem().toString());
        else
            throw failTSE("Unable to find code system " + inc.getSystem().toString());
    }
    cs.checkNoModifiers("Code System", "expanding");
    if (cs.getContent() != CodeSystemContentMode.COMPLETE && cs.getContent() != CodeSystemContentMode.FRAGMENT)
        throw failTSE("Code system " + inc.getSystem().toString() + " is incomplete");
    if (cs.hasVersion())
        if (!existsInParams(exp.getParameter(), "version", new UriType(cs.getUrl() + "|" + cs.getVersion())))
            exp.getParameter().add(new ValueSetExpansionParameterComponent().setName("version").setValue(new UriType(cs.getUrl() + "|" + cs.getVersion())));
    if (inc.getConcept().size() == 0 && inc.getFilter().size() == 0) {
        // special case - add all the code system
        for (ConceptDefinitionComponent def : cs.getConcept()) {
            addCodeAndDescendents(cs, inc.getSystem(), def, null, expParams, imports, null, new AllConceptsFilter());
        }
        if (cs.getContent() == CodeSystemContentMode.FRAGMENT) {
            addFragmentWarning(exp, cs);
        }
        if (cs.getContent() == CodeSystemContentMode.EXAMPLE) {
            addExampleWarning(exp, cs);
        }
    }
    if (!inc.getConcept().isEmpty()) {
        canBeHeirarchy = false;
        for (ConceptReferenceComponent c : inc.getConcept()) {
            c.checkNoModifiers("Code in Code System", "expanding");
            ConceptDefinitionComponent def = CodeSystemUtilities.findCode(cs.getConcept(), c.getCode());
            // default is true if we're a fragment and
            Boolean inactive = false;
            if (def == null) {
                if (cs.getContent() == CodeSystemContentMode.FRAGMENT) {
                    addFragmentWarning(exp, cs);
                } else if (cs.getContent() == CodeSystemContentMode.EXAMPLE) {
                    addExampleWarning(exp, cs);
                } else {
                    if (checkCodesWhenExpanding) {
                        throw failTSE("Unable to find code '" + c.getCode() + "' in code system " + cs.getUrl());
                    }
                }
            } else {
                inactive = CodeSystemUtilities.isInactive(cs, def);
            }
            addCode(inc.getSystem(), c.getCode(), !Utilities.noString(c.getDisplay()) ? c.getDisplay() : def == null ? null : def.getDisplay(), null, convertDesignations(c.getDesignation()), expParams, false, inactive, imports);
        }
    }
    if (inc.getFilter().size() > 1) {
        // which will bt the case if we get around to supporting this
        canBeHeirarchy = false;
        // need to and them, and this isn't done yet. But this shouldn't arise in non loinc and snomed value sets
        throw failTSE("Multiple filters not handled yet");
    }
    if (inc.getFilter().size() == 1) {
        if (cs.getContent() == CodeSystemContentMode.FRAGMENT) {
            addFragmentWarning(exp, cs);
        }
        ConceptSetFilterComponent fc = inc.getFilter().get(0);
        if ("concept".equals(fc.getProperty()) && fc.getOp() == FilterOperator.ISA) {
            // special: all codes in the target code system under the value
            ConceptDefinitionComponent def = getConceptForCode(cs.getConcept(), fc.getValue());
            if (def == null)
                throw failTSE("Code '" + fc.getValue() + "' not found in system '" + inc.getSystem() + "'");
            addCodeAndDescendents(cs, inc.getSystem(), def, null, expParams, imports, null, new AllConceptsFilter());
        } else if ("concept".equals(fc.getProperty()) && fc.getOp() == FilterOperator.ISNOTA) {
            // special: all codes in the target code system that are not under the value
            ConceptDefinitionComponent defEx = getConceptForCode(cs.getConcept(), fc.getValue());
            if (defEx == null)
                throw failTSE("Code '" + fc.getValue() + "' not found in system '" + inc.getSystem() + "'");
            for (ConceptDefinitionComponent def : cs.getConcept()) {
                addCodeAndDescendents(cs, inc.getSystem(), def, null, expParams, imports, defEx, new AllConceptsFilter());
            }
        } else if ("concept".equals(fc.getProperty()) && fc.getOp() == FilterOperator.DESCENDENTOF) {
            // special: all codes in the target code system under the value
            ConceptDefinitionComponent def = getConceptForCode(cs.getConcept(), fc.getValue());
            if (def == null)
                throw failTSE("Code '" + fc.getValue() + "' not found in system '" + inc.getSystem() + "'");
            for (ConceptDefinitionComponent c : def.getConcept()) addCodeAndDescendents(cs, inc.getSystem(), c, null, expParams, imports, null, new AllConceptsFilter());
            if (def.hasUserData(CodeSystemUtilities.USER_DATA_CROSS_LINK)) {
                List<ConceptDefinitionComponent> children = (List<ConceptDefinitionComponent>) def.getUserData(CodeSystemUtilities.USER_DATA_CROSS_LINK);
                for (ConceptDefinitionComponent c : children) addCodeAndDescendents(cs, inc.getSystem(), c, null, expParams, imports, null, new AllConceptsFilter());
            }
        } else if ("display".equals(fc.getProperty()) && fc.getOp() == FilterOperator.EQUAL) {
            // gg; note: wtf is this: if the filter is display=v, look up the code 'v', and see if it's diplsay is 'v'?
            canBeHeirarchy = false;
            ConceptDefinitionComponent def = getConceptForCode(cs.getConcept(), fc.getValue());
            if (def != null) {
                if (isNotBlank(def.getDisplay()) && isNotBlank(fc.getValue())) {
                    if (def.getDisplay().contains(fc.getValue())) {
                        addCode(inc.getSystem(), def.getCode(), def.getDisplay(), null, def.getDesignation(), expParams, CodeSystemUtilities.isNotSelectable(cs, def), CodeSystemUtilities.isInactive(cs, def), imports);
                    }
                }
            }
        } else if (isDefinedProperty(cs, fc.getProperty())) {
            for (ConceptDefinitionComponent def : cs.getConcept()) {
                addCodeAndDescendents(cs, inc.getSystem(), def, null, expParams, imports, null, new PropertyFilter(fc, getPropertyDefinition(cs, fc.getProperty())));
            }
        } else {
            throw fail("Search by property[" + fc.getProperty() + "] and op[" + fc.getOp() + "] is not supported yet");
        }
    }
}
Also used : ConceptSetFilterComponent(org.hl7.fhir.r4b.model.ValueSet.ConceptSetFilterComponent) ConceptDefinitionComponent(org.hl7.fhir.r4b.model.CodeSystem.ConceptDefinitionComponent) ValueSetExpansionParameterComponent(org.hl7.fhir.r4b.model.ValueSet.ValueSetExpansionParameterComponent) ArrayList(java.util.ArrayList) List(java.util.List) ConceptReferenceComponent(org.hl7.fhir.r4b.model.ValueSet.ConceptReferenceComponent) UriType(org.hl7.fhir.r4b.model.UriType)

Example 23 with TerminologyServiceException

use of org.hl7.fhir.exceptions.TerminologyServiceException 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 24 with TerminologyServiceException

use of org.hl7.fhir.exceptions.TerminologyServiceException in project org.hl7.fhir.core by hapifhir.

the class ValueSetRenderer method getConceptsForCodes.

private Map<String, ConceptDefinitionComponent> getConceptsForCodes(CodeSystem e, ConceptSetComponent inc) {
    if (e == null) {
        e = getContext().getWorker().fetchCodeSystem(inc.getSystem());
    }
    ValueSetExpansionComponent vse = null;
    if (!context.isNoSlowLookup() && !getContext().getWorker().hasCache()) {
        try {
            ValueSetExpansionOutcome vso = getContext().getWorker().expandVS(inc, false, false);
            ValueSet valueset = vso.getValueset();
            if (valueset == null)
                throw new TerminologyServiceException("Error Expanding ValueSet: " + vso.getError());
            vse = valueset.getExpansion();
        } catch (TerminologyServiceException e1) {
            return null;
        }
    }
    Map<String, ConceptDefinitionComponent> results = new HashMap<>();
    List<CodingValidationRequest> serverList = new ArrayList<>();
    // 1st pass, anything we can resolve internally
    for (ConceptReferenceComponent cc : inc.getConcept()) {
        String code = cc.getCode();
        ConceptDefinitionComponent v = null;
        if (e != null) {
            v = getConceptForCode(e.getConcept(), code);
        }
        if (v == null && vse != null) {
            v = getConceptForCodeFromExpansion(vse.getContains(), code);
        }
        if (v != null) {
            results.put(code, v);
        } else {
            serverList.add(new CodingValidationRequest(new Coding(inc.getSystem(), code, null)));
        }
    }
    if (!context.isNoSlowLookup() && !serverList.isEmpty()) {
        getContext().getWorker().validateCodeBatch(getContext().getTerminologyServiceOptions(), serverList, null);
        for (CodingValidationRequest vr : serverList) {
            ConceptDefinitionComponent v = vr.getResult().asConceptDefinition();
            if (v != null) {
                results.put(vr.getCoding().getCode(), v);
            }
        }
    }
    return results;
}
Also used : ValueSetExpansionComponent(org.hl7.fhir.r5.model.ValueSet.ValueSetExpansionComponent) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ConceptReferenceComponent(org.hl7.fhir.r5.model.ValueSet.ConceptReferenceComponent) ConceptDefinitionComponent(org.hl7.fhir.r5.model.CodeSystem.ConceptDefinitionComponent) Coding(org.hl7.fhir.r5.model.Coding) ValueSetExpansionOutcome(org.hl7.fhir.r5.terminologies.ValueSetExpander.ValueSetExpansionOutcome) TerminologyServiceException(org.hl7.fhir.exceptions.TerminologyServiceException) ValueSet(org.hl7.fhir.r5.model.ValueSet) CodingValidationRequest(org.hl7.fhir.r5.context.IWorkerContext.CodingValidationRequest)

Example 25 with TerminologyServiceException

use of org.hl7.fhir.exceptions.TerminologyServiceException in project org.hl7.fhir.core by hapifhir.

the class ValueSetExpanderSimple method importValueSet.

private ValueSet importValueSet(String value, List<ValueSetExpansionParameterComponent> params, ExpansionProfile profile) 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 = factory.getExpander().expand(vs, profile);
    if (vso.getError() != null)
        throw new TerminologyServiceException("Unable to expand imported value set: " + vso.getError());
    if (vso.getService() != null)
        throw new TerminologyServiceException("Unable to expand imported value set " + value);
    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 : NoTerminologyServiceException(org.hl7.fhir.exceptions.NoTerminologyServiceException) TerminologyServiceException(org.hl7.fhir.exceptions.TerminologyServiceException) ValueSet(org.hl7.fhir.dstu3.model.ValueSet)

Aggregations

TerminologyServiceException (org.hl7.fhir.exceptions.TerminologyServiceException)22 NoTerminologyServiceException (org.hl7.fhir.exceptions.NoTerminologyServiceException)12 IOException (java.io.IOException)9 FileNotFoundException (java.io.FileNotFoundException)8 FHIRException (org.hl7.fhir.exceptions.FHIRException)8 DefinitionException (org.hl7.fhir.exceptions.DefinitionException)7 NotImplementedException (org.apache.commons.lang3.NotImplementedException)6 HashMap (java.util.HashMap)5 ArrayList (java.util.ArrayList)4 UriType (org.hl7.fhir.r4.model.UriType)4 ValueSet (org.hl7.fhir.r4.model.ValueSet)4 ValueSet (org.hl7.fhir.r5.model.ValueSet)4 ValueSet (org.hl7.fhir.dstu3.model.ValueSet)3 ValueSetExpansionParameterComponent (org.hl7.fhir.r4.model.ValueSet.ValueSetExpansionParameterComponent)3 List (java.util.List)2 UriType (org.hl7.fhir.dstu2.model.UriType)2 ValueSet (org.hl7.fhir.dstu2.model.ValueSet)2 ValueSetExpansionParameterComponent (org.hl7.fhir.dstu2.model.ValueSet.ValueSetExpansionParameterComponent)2 UriType (org.hl7.fhir.dstu2016may.model.UriType)2 ValueSetExpansionParameterComponent (org.hl7.fhir.dstu2016may.model.ValueSet.ValueSetExpansionParameterComponent)2