Search in sources :

Example 1 with CacheToken

use of org.hl7.fhir.r5.context.TerminologyCache.CacheToken in project org.hl7.fhir.core by hapifhir.

the class TerminologyCache method generateValidationToken.

public CacheToken generateValidationToken(ValidationOptions options, CodeableConcept code, ValueSet vs) {
    CacheToken ct = new CacheToken();
    for (Coding c : code.getCoding()) {
        if (c.hasSystem())
            ct.setName(getNameForSystem(c.getSystem()));
    }
    JsonParser json = new JsonParser();
    json.setOutputStyle(OutputStyle.PRETTY);
    ValueSet vsc = getVSEssense(vs);
    try {
        ct.request = "{\"code\" : " + json.composeString(code, "codeableConcept") + ", \"valueSet\" :" + extracted(json, vsc) + (options == null ? "" : ", " + options.toJson()) + "}";
    } catch (IOException e) {
        throw new Error(e);
    }
    ct.key = String.valueOf(hashNWS(ct.request));
    return ct;
}
Also used : Coding(org.hl7.fhir.r4b.model.Coding) IOException(java.io.IOException) ValueSet(org.hl7.fhir.r4b.model.ValueSet) JsonParser(org.hl7.fhir.r4b.formats.JsonParser)

Example 2 with CacheToken

use of org.hl7.fhir.r5.context.TerminologyCache.CacheToken in project org.hl7.fhir.core by hapifhir.

the class TerminologyCache method generateValidationToken.

public CacheToken generateValidationToken(ValidationOptions options, Coding code, ValueSet vs) {
    CacheToken ct = new CacheToken();
    if (code.hasSystem())
        ct.name = getNameForSystem(code.getSystem());
    else
        ct.name = NAME_FOR_NO_SYSTEM;
    JsonParser json = new JsonParser();
    json.setOutputStyle(OutputStyle.PRETTY);
    ValueSet vsc = getVSEssense(vs);
    try {
        ct.request = "{\"code\" : " + json.composeString(code, "code") + ", \"valueSet\" :" + (vsc == null ? "null" : extracted(json, vsc)) + (options == null ? "" : ", " + options.toJson()) + "}";
    } catch (IOException e) {
        throw new Error(e);
    }
    ct.key = String.valueOf(hashNWS(ct.request));
    return ct;
}
Also used : IOException(java.io.IOException) ValueSet(org.hl7.fhir.r4b.model.ValueSet) JsonParser(org.hl7.fhir.r4b.formats.JsonParser)

Example 3 with CacheToken

use of org.hl7.fhir.r5.context.TerminologyCache.CacheToken in project org.hl7.fhir.core by hapifhir.

the class TerminologyCache method generateExpandToken.

public CacheToken generateExpandToken(ValueSet vs, boolean heirarchical) {
    CacheToken ct = new CacheToken();
    ValueSet vsc = getVSEssense(vs);
    for (ConceptSetComponent inc : vs.getCompose().getInclude()) if (inc.hasSystem())
        ct.setName(getNameForSystem(inc.getSystem()));
    for (ConceptSetComponent inc : vs.getCompose().getExclude()) if (inc.hasSystem())
        ct.setName(getNameForSystem(inc.getSystem()));
    for (ValueSetExpansionContainsComponent inc : vs.getExpansion().getContains()) if (inc.hasSystem())
        ct.setName(getNameForSystem(inc.getSystem()));
    JsonParser json = new JsonParser();
    json.setOutputStyle(OutputStyle.PRETTY);
    try {
        ct.request = "{\"hierarchical\" : " + (heirarchical ? "true" : "false") + ", \"valueSet\" :" + extracted(json, vsc) + "}\r\n";
    } catch (IOException e) {
        throw new Error(e);
    }
    ct.key = String.valueOf(hashNWS(ct.request));
    return ct;
}
Also used : ConceptSetComponent(org.hl7.fhir.r4b.model.ValueSet.ConceptSetComponent) ValueSetExpansionContainsComponent(org.hl7.fhir.r4b.model.ValueSet.ValueSetExpansionContainsComponent) IOException(java.io.IOException) ValueSet(org.hl7.fhir.r4b.model.ValueSet) JsonParser(org.hl7.fhir.r4b.formats.JsonParser)

Example 4 with CacheToken

use of org.hl7.fhir.r5.context.TerminologyCache.CacheToken in project org.hl7.fhir.core by hapifhir.

the class BaseWorkerContext method expandVS.

@Override
public ValueSetExpansionOutcome expandVS(ConceptSetComponent inc, boolean hierarchical) throws TerminologyServiceException {
    ValueSet vs = new ValueSet();
    vs.setStatus(PublicationStatus.ACTIVE);
    vs.setCompose(new ValueSetComposeComponent());
    vs.getCompose().getInclude().add(inc);
    CacheToken cacheToken = txCache.generateExpandToken(vs, hierarchical);
    ValueSetExpansionOutcome res;
    res = txCache.getExpansion(cacheToken);
    if (res != null) {
        return res;
    }
    Parameters p = expParameters.copy();
    p.setParameter("includeDefinition", false);
    p.setParameter("excludeNested", !hierarchical);
    boolean cached = addDependentResources(p, vs);
    if (cached) {
        p.addParameter().setName("cache-id").setValue(new StringType(cacheId));
    }
    for (ConceptSetComponent incl : vs.getCompose().getInclude()) {
        codeSystemsUsed.add(incl.getSystem());
    }
    for (ConceptSetComponent incl : vs.getCompose().getExclude()) {
        codeSystemsUsed.add(incl.getSystem());
    }
    if (noTerminologyServer) {
        return new ValueSetExpansionOutcome(formatMessage(I18nConstants.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);
        res = new ValueSetExpansionOutcome(result).setTxLink(txLog.getLastId());
    } catch (Exception e) {
        res = new ValueSetExpansionOutcome(e.getMessage() == null ? e.getClass().getName() : e.getMessage(), TerminologyServiceErrorClass.UNKNOWN);
        if (txLog != null) {
            res.setTxLink(txLog.getLastId());
        }
    }
    txCache.cacheExpansion(cacheToken, res, TerminologyCache.PERMANENT);
    return res;
}
Also used : ConceptSetComponent(org.hl7.fhir.r4b.model.ValueSet.ConceptSetComponent) Parameters(org.hl7.fhir.r4b.model.Parameters) StringType(org.hl7.fhir.r4b.model.StringType) HashMap(java.util.HashMap) CacheToken(org.hl7.fhir.r4b.context.TerminologyCache.CacheToken) ValueSetExpansionOutcome(org.hl7.fhir.r4b.terminologies.ValueSetExpander.ValueSetExpansionOutcome) ValueSet(org.hl7.fhir.r4b.model.ValueSet) ValueSetComposeComponent(org.hl7.fhir.r4b.model.ValueSet.ValueSetComposeComponent) 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)

Example 5 with CacheToken

use of org.hl7.fhir.r5.context.TerminologyCache.CacheToken in project org.hl7.fhir.core by hapifhir.

the class BaseWorkerContext method validateCode.

@Override
public ValidationResult validateCode(ValidationOptions options, CodeableConcept code, ValueSet vs) {
    CacheToken cacheToken = txCache.generateValidationToken(options, code, vs);
    ValidationResult res = txCache.getValidation(cacheToken);
    if (res != null) {
        return res;
    }
    for (Coding c : code.getCoding()) {
        if (c.hasSystem()) {
            codeSystemsUsed.add(c.getSystem());
        }
    }
    if (options.isUseClient()) {
        // ok, first we try to validate locally
        try {
            ValueSetCheckerSimple vsc = new ValueSetCheckerSimple(options, vs, this);
            res = vsc.validateCode(code);
            txCache.cacheValidation(cacheToken, res, TerminologyCache.TRANSIENT);
            return res;
        } catch (Exception e) {
            if (e instanceof NoTerminologyServiceException) {
                return new ValidationResult(IssueSeverity.ERROR, "No Terminology Service", TerminologyServiceErrorClass.NOSERVICE);
            }
        }
    }
    if (!options.isUseServer()) {
        return new ValidationResult(IssueSeverity.WARNING, "Unable to validate code without using server", TerminologyServiceErrorClass.BLOCKED_BY_OPTIONS);
    }
    // if that failed, we try to validate on the server
    if (noTerminologyServer) {
        return new ValidationResult(IssueSeverity.ERROR, "Error validating code: running without terminology services", TerminologyServiceErrorClass.NOSERVICE);
    }
    tlog("$validate " + txCache.summary(code) + " for " + txCache.summary(vs));
    try {
        Parameters pIn = new Parameters();
        pIn.addParameter().setName("codeableConcept").setValue(code);
        setTerminologyOptions(options, pIn);
        res = validateOnServer(vs, pIn, options);
    } catch (Exception e) {
        res = new ValidationResult(IssueSeverity.ERROR, e.getMessage() == null ? e.getClass().getName() : e.getMessage()).setTxLink(txLog.getLastId());
    }
    txCache.cacheValidation(cacheToken, res, TerminologyCache.PERMANENT);
    return res;
}
Also used : NoTerminologyServiceException(org.hl7.fhir.exceptions.NoTerminologyServiceException) Parameters(org.hl7.fhir.r4b.model.Parameters) Coding(org.hl7.fhir.r4b.model.Coding) CacheToken(org.hl7.fhir.r4b.context.TerminologyCache.CacheToken) ValueSetCheckerSimple(org.hl7.fhir.r4b.terminologies.ValueSetCheckerSimple) 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)

Aggregations

IOException (java.io.IOException)21 ValueSet (org.hl7.fhir.r5.model.ValueSet)20 Test (org.junit.jupiter.api.Test)18 FileNotFoundException (java.io.FileNotFoundException)12 DefinitionException (org.hl7.fhir.exceptions.DefinitionException)12 FHIRException (org.hl7.fhir.exceptions.FHIRException)12 TerminologyServiceException (org.hl7.fhir.exceptions.TerminologyServiceException)12 Coding (org.hl7.fhir.r5.model.Coding)11 NoTerminologyServiceException (org.hl7.fhir.exceptions.NoTerminologyServiceException)8 Parameters (org.hl7.fhir.r5.model.Parameters)7 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)7 HashMap (java.util.HashMap)6 CodeableConcept (org.hl7.fhir.r5.model.CodeableConcept)6 ValueSet (org.hl7.fhir.r4b.model.ValueSet)5 ValueSetExpander (org.hl7.fhir.r5.terminologies.ValueSetExpander)5 ValidationOptions (org.hl7.fhir.utilities.validation.ValidationOptions)5 JsonElement (com.google.gson.JsonElement)4 CacheToken (org.hl7.fhir.r4.context.TerminologyCache.CacheToken)4 CacheToken (org.hl7.fhir.r4b.context.TerminologyCache.CacheToken)4 CacheToken (org.hl7.fhir.r5.context.TerminologyCache.CacheToken)4