Search in sources :

Example 96 with ConceptDefinitionComponent

use of org.hl7.fhir.dstu2.model.ValueSet.ConceptDefinitionComponent in project org.hl7.fhir.core by hapifhir.

the class TerminologyCache method load.

private void load() throws FHIRException {
    for (String fn : new File(folder).list()) {
        if (fn.endsWith(".cache") && !fn.equals("validation.cache")) {
            try {
                // System.out.println("Load "+fn);
                String title = fn.substring(0, fn.lastIndexOf("."));
                NamedCache nc = new NamedCache();
                nc.name = title;
                caches.put(title, nc);
                System.out.print(" - load " + title + ".cache");
                String src = TextFile.fileToString(Utilities.path(folder, fn));
                if (src.startsWith("?"))
                    src = src.substring(1);
                int i = src.indexOf(ENTRY_MARKER);
                while (i > -1) {
                    String s = src.substring(0, i);
                    System.out.print(".");
                    src = src.substring(i + ENTRY_MARKER.length() + 1);
                    i = src.indexOf(ENTRY_MARKER);
                    if (!Utilities.noString(s)) {
                        int j = s.indexOf(BREAK);
                        String q = s.substring(0, j);
                        String p = s.substring(j + BREAK.length() + 1).trim();
                        CacheEntry ce = new CacheEntry();
                        ce.persistent = true;
                        ce.request = q;
                        boolean e = p.charAt(0) == 'e';
                        p = p.substring(3);
                        JsonObject o = (JsonObject) new com.google.gson.JsonParser().parse(p);
                        String error = loadJS(o.get("error"));
                        if (e) {
                            if (o.has("valueSet"))
                                ce.e = new ValueSetExpansionOutcome((ValueSet) new JsonParser().parse(o.getAsJsonObject("valueSet")), error, TerminologyServiceErrorClass.UNKNOWN);
                            else
                                ce.e = new ValueSetExpansionOutcome(error, TerminologyServiceErrorClass.UNKNOWN);
                        } else {
                            IssueSeverity severity = o.get("severity") instanceof JsonNull ? null : IssueSeverity.fromCode(o.get("severity").getAsString());
                            String display = loadJS(o.get("display"));
                            ce.v = new ValidationResult(severity, error, new ConceptDefinitionComponent().setDisplay(display));
                        }
                        nc.map.put(String.valueOf(hashNWS(ce.request)), ce);
                        nc.list.add(ce);
                    }
                }
                System.out.println("done");
            } catch (Exception e) {
                throw new FHIRException("Error loading " + fn + ": " + e.getMessage(), e);
            }
        }
    }
}
Also used : JsonObject(com.google.gson.JsonObject) ValidationResult(org.hl7.fhir.r4.context.IWorkerContext.ValidationResult) FHIRException(org.hl7.fhir.exceptions.FHIRException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) FHIRException(org.hl7.fhir.exceptions.FHIRException) JsonNull(com.google.gson.JsonNull) ConceptDefinitionComponent(org.hl7.fhir.r4.model.CodeSystem.ConceptDefinitionComponent) IssueSeverity(org.hl7.fhir.utilities.validation.ValidationMessage.IssueSeverity) ValueSetExpansionOutcome(org.hl7.fhir.r4.terminologies.ValueSetExpander.ValueSetExpansionOutcome) File(java.io.File) TextFile(org.hl7.fhir.utilities.TextFile) JsonParser(org.hl7.fhir.r4.formats.JsonParser)

Example 97 with ConceptDefinitionComponent

use of org.hl7.fhir.dstu2.model.ValueSet.ConceptDefinitionComponent in project org.hl7.fhir.core by hapifhir.

the class CodeSystemRenderer method addDefineRowToTable.

private boolean addDefineRowToTable(XhtmlNode t, ConceptDefinitionComponent c, int level, boolean hasHierarchy, boolean hasDisplay, boolean hasDefinitions, boolean comment, boolean version, boolean deprecated, List<UsedConceptMap> maps, String system, CodeSystem cs, List<PropertyComponent> properties, CodeSystemNavigator csNav, List<String> langs, boolean isSupplement) throws FHIRFormatError, DefinitionException, IOException {
    boolean hasExtensions = false;
    XhtmlNode tr = t.tr();
    XhtmlNode td = tr.td();
    if (hasHierarchy) {
        td.addText(Integer.toString(level + 1));
        td = tr.td();
        String s = Utilities.padLeft("", '\u00A0', level * 2);
        td.addText(s);
    }
    String link = isSupplement ? getLinkForCode(cs.getSupplements(), null, c.getCode()) : null;
    if (link != null) {
        td.ah(link).attribute("style", "white-space:nowrap").addText(c.getCode());
    } else {
        td.attribute("style", "white-space:nowrap").addText(c.getCode());
    }
    XhtmlNode a;
    if (c.hasCodeElement()) {
        td.an(cs.getId() + "-" + Utilities.nmtokenize(c.getCode()));
    }
    for (ConceptDefinitionDesignationComponent cd : c.getDesignation()) {
        if (cd.hasLanguage() && !langs.contains(cd.getLanguage()) && (!cs.hasLanguage() || !cs.getLanguage().equals(cd.getLanguage()))) {
            langs.add(cd.getLanguage());
        }
    }
    if (hasDisplay) {
        td = tr.td();
        renderDisplayName(c, cs, td);
    }
    if (hasDefinitions) {
        td = tr.td();
        if (c != null && c.hasDefinitionElement()) {
            if (getContext().getLang() == null) {
                if (hasMarkdownInDefinitions(cs))
                    addMarkdown(td, c.getDefinition());
                else
                    td.addText(c.getDefinition());
            } else if (getContext().getLang().equals("*")) {
                boolean sl = false;
                for (ConceptDefinitionDesignationComponent cd : c.getDesignation()) if (cd.getUse().is("http://terminology.hl7.org/CodeSystem/designation-usage", "definition") && cd.hasLanguage() && !c.getDefinition().equalsIgnoreCase(cd.getValue()))
                    sl = true;
                td.addText((sl ? cs.getLanguage("en") + ": " : "") + c.getDefinition());
                for (ConceptDefinitionDesignationComponent cd : c.getDesignation()) {
                    if (cd.getUse().is("http://terminology.hl7.org/CodeSystem/designation-usage", "definition") && cd.hasLanguage() && !c.getDefinition().equalsIgnoreCase(cd.getValue())) {
                        td.br();
                        td.addText(cd.getLanguage() + ": " + cd.getValue());
                    }
                }
            } else if (getContext().getLang().equals(cs.getLanguage()) || (getContext().getLang().equals("en") && !cs.hasLanguage())) {
                td.addText(c.getDefinition());
            } else {
                for (ConceptDefinitionDesignationComponent cd : c.getDesignation()) {
                    if (cd.getUse().is("http://terminology.hl7.org/CodeSystem/designation-usage", "definition") && cd.hasLanguage() && cd.getLanguage().equals(getContext().getLang())) {
                        td.addText(cd.getValue());
                    }
                }
            }
        }
    }
    if (deprecated) {
        td = tr.td();
        Boolean b = CodeSystemUtilities.isDeprecated(cs, c, false);
        if (b != null && b) {
            smartAddText(td, getContext().getWorker().translator().translate("xhtml-gen-cs", "Deprecated", getContext().getLang()));
            hasExtensions = true;
            if (ToolingExtensions.hasExtension(c, ToolingExtensions.EXT_REPLACED_BY)) {
                Coding cc = (Coding) ToolingExtensions.getExtension(c, ToolingExtensions.EXT_REPLACED_BY).getValue();
                td.tx(" (replaced by ");
                String url = getCodingReference(cc, system);
                if (url != null) {
                    td.ah(url).addText(cc.getCode());
                    td.tx(": " + cc.getDisplay() + ")");
                } else
                    td.addText(cc.getCode() + " '" + cc.getDisplay() + "' in " + cc.getSystem() + ")");
            }
        }
    }
    if (comment) {
        td = tr.td();
        Extension ext = c.getExtensionByUrl(ToolingExtensions.EXT_CS_COMMENT);
        if (ext != null) {
            hasExtensions = true;
            String bc = ext.hasValue() ? ext.getValue().primitiveValue() : null;
            Map<String, String> translations = ToolingExtensions.getLanguageTranslations(ext.getValue());
            if (getContext().getLang() == null) {
                if (bc != null)
                    td.addText(bc);
            } else if (getContext().getLang().equals("*")) {
                boolean sl = false;
                for (String l : translations.keySet()) if (bc == null || !bc.equalsIgnoreCase(translations.get(l)))
                    sl = true;
                if (bc != null) {
                    td.addText((sl ? cs.getLanguage("en") + ": " : "") + bc);
                }
                for (String l : translations.keySet()) {
                    if (bc == null || !bc.equalsIgnoreCase(translations.get(l))) {
                        if (!td.getChildNodes().isEmpty())
                            td.br();
                        td.addText(l + ": " + translations.get(l));
                    }
                }
            } else if (getContext().getLang().equals(cs.getLanguage()) || (getContext().getLang().equals("en") && !cs.hasLanguage())) {
                if (bc != null)
                    td.addText(bc);
            } else {
                if (bc != null)
                    translations.put(cs.getLanguage("en"), bc);
                for (String l : translations.keySet()) {
                    if (l.equals(getContext().getLang())) {
                        td.addText(translations.get(l));
                    }
                }
            }
        }
    }
    if (version) {
        td = tr.td();
        if (c.hasUserData("cs.version.notes"))
            td.addText(c.getUserString("cs.version.notes"));
    }
    if (properties != null) {
        for (PropertyComponent pc : properties) {
            td = tr.td();
            boolean first = true;
            List<ConceptPropertyComponent> pcvl = CodeSystemUtilities.getPropertyValues(c, pc.getCode());
            for (ConceptPropertyComponent pcv : pcvl) {
                if (pcv.hasValue()) {
                    if (first)
                        first = false;
                    else
                        td.addText(", ");
                    if (pcv.hasValueCoding()) {
                        td.addText(pcv.getValueCoding().getCode());
                    } else if (pcv.hasValueStringType() && Utilities.isAbsoluteUrlLinkable(pcv.getValue().primitiveValue())) {
                        td.ah(pcv.getValue().primitiveValue()).tx(pcv.getValue().primitiveValue());
                    } else {
                        td.addText(pcv.getValue().primitiveValue());
                    }
                }
            }
        }
    }
    for (UsedConceptMap m : maps) {
        td = tr.td();
        List<TargetElementComponentWrapper> mappings = findMappingsForCode(c.getCode(), m.getMap());
        boolean first = true;
        for (TargetElementComponentWrapper mapping : mappings) {
            if (!first)
                td.br();
            first = false;
            XhtmlNode span = td.span(null, mapping.comp.hasEquivalence() ? mapping.comp.getEquivalence().toCode() : "");
            span.addText(getCharForRelationship(mapping.comp));
            a = td.ah(getContext().getSpecificationLink() + m.getLink() + "#" + makeAnchor(mapping.group.getTarget(), mapping.comp.getCode()));
            a.addText(mapping.comp.getCode());
            if (!Utilities.noString(mapping.comp.getComment()))
                td.i().tx("(" + mapping.comp.getComment() + ")");
        }
    }
    List<ConceptDefinitionComponent> ocl = csNav.getOtherChildren(c);
    for (ConceptDefinitionComponent cc : csNav.getConcepts(c)) {
        hasExtensions = addDefineRowToTable(t, cc, level + 1, hasHierarchy, hasDisplay, hasDefinitions, comment, version, deprecated, maps, system, cs, properties, csNav, langs, isSupplement) || hasExtensions;
    }
    for (ConceptDefinitionComponent cc : ocl) {
        tr = t.tr();
        td = tr.td();
        td.addText(Integer.toString(level + 2));
        td = tr.td();
        String s = Utilities.padLeft("", '\u00A0', (level + 1) * 2);
        td.addText(s);
        td.attribute("style", "white-space:nowrap");
        a = td.ah("#" + cs.getId() + "-" + Utilities.nmtokenize(cc.getCode()));
        a.addText(cc.getCode());
        if (hasDisplay) {
            td = tr.td();
            renderDisplayName(cc, cs, td);
        }
        int w = 1 + (deprecated ? 1 : 0) + (comment ? 1 : 0) + (version ? 1 : 0) + maps.size();
        if (properties != null) {
            w = w + properties.size();
        }
        td = tr.td().colspan(Integer.toString(w));
    }
    return hasExtensions;
}
Also used : ConceptPropertyComponent(org.hl7.fhir.r4b.model.CodeSystem.ConceptPropertyComponent) XhtmlNode(org.hl7.fhir.utilities.xhtml.XhtmlNode) Extension(org.hl7.fhir.r4b.model.Extension) ConceptPropertyComponent(org.hl7.fhir.r4b.model.CodeSystem.ConceptPropertyComponent) PropertyComponent(org.hl7.fhir.r4b.model.CodeSystem.PropertyComponent) ConceptDefinitionComponent(org.hl7.fhir.r4b.model.CodeSystem.ConceptDefinitionComponent) ConceptDefinitionDesignationComponent(org.hl7.fhir.r4b.model.CodeSystem.ConceptDefinitionDesignationComponent) Coding(org.hl7.fhir.r4b.model.Coding)

Example 98 with ConceptDefinitionComponent

use of org.hl7.fhir.dstu2.model.ValueSet.ConceptDefinitionComponent in project org.hl7.fhir.core by hapifhir.

the class CodeSystemRenderer method addLanguageRow.

private void addLanguageRow(ConceptDefinitionComponent c, XhtmlNode t, List<String> langs) {
    XhtmlNode tr = t.tr();
    tr.td().addText(c.getCode());
    for (String lang : langs) {
        ConceptDefinitionDesignationComponent d = null;
        for (ConceptDefinitionDesignationComponent designation : c.getDesignation()) {
            if (designation.hasLanguage()) {
                if (lang.equals(designation.getLanguage()))
                    d = designation;
            }
        }
        tr.td().addText(d == null ? "" : d.getValue());
    }
}
Also used : ConceptDefinitionDesignationComponent(org.hl7.fhir.r4b.model.CodeSystem.ConceptDefinitionDesignationComponent) XhtmlNode(org.hl7.fhir.utilities.xhtml.XhtmlNode)

Example 99 with ConceptDefinitionComponent

use of org.hl7.fhir.dstu2.model.ValueSet.ConceptDefinitionComponent in project org.hl7.fhir.core by hapifhir.

the class InstanceValidator method checkCode.

// public API
private boolean checkCode(List<ValidationMessage> errors, Element element, String path, String code, String system, String version, String display, boolean checkDisplay, NodeStack stack) throws TerminologyServiceException {
    long t = System.nanoTime();
    boolean ss = context.supportsSystem(system);
    timeTracker.tx(t, "ss " + system);
    if (ss) {
        t = System.nanoTime();
        ValidationResult s = checkCodeOnServer(stack, code, system, version, display, checkDisplay);
        timeTracker.tx(t, "vc " + system + "#" + code + " '" + display + "'");
        if (s == null)
            return true;
        if (s.isOk()) {
            if (s.getMessage() != null)
                txWarning(errors, s.getTxLink(), IssueType.CODEINVALID, element.line(), element.col(), path, s == null, I18nConstants.TERMINOLOGY_PASSTHROUGH_TX_MESSAGE, s.getMessage(), system, code);
            return true;
        }
        if (s.getErrorClass() != null && s.getErrorClass().isInfrastructure())
            txWarning(errors, s.getTxLink(), IssueType.CODEINVALID, element.line(), element.col(), path, s == null, s.getMessage());
        else if (s.getSeverity() == IssueSeverity.INFORMATION)
            txHint(errors, s.getTxLink(), IssueType.CODEINVALID, element.line(), element.col(), path, s == null, s.getMessage());
        else if (s.getSeverity() == IssueSeverity.WARNING)
            txWarning(errors, s.getTxLink(), IssueType.CODEINVALID, element.line(), element.col(), path, s == null, s.getMessage());
        else
            return txRule(errors, s.getTxLink(), IssueType.CODEINVALID, element.line(), element.col(), path, s == null, I18nConstants.TERMINOLOGY_PASSTHROUGH_TX_MESSAGE, s.getMessage(), system, code);
        return true;
    } else if (system.startsWith("http://build.fhir.org") || system.startsWith("https://build.fhir.org")) {
        rule(errors, IssueType.CODEINVALID, element.line(), element.col(), path, false, I18nConstants.TERMINOLOGY_TX_SYSTEM_WRONG_BUILD, system, suggestSystemForBuild(system));
        return false;
    } else if (system.startsWith("http://hl7.org/fhir") || system.startsWith("https://hl7.org/fhir") || system.startsWith("http://www.hl7.org/fhir") || system.startsWith("https://www.hl7.org/fhir")) {
        if (SIDUtilities.isknownCodeSystem(system)) {
            // else don't check these (for now)
            return true;
        } else if (system.startsWith("http://hl7.org/fhir/test")) {
            // we don't validate these
            return true;
        } else if (system.endsWith(".html")) {
            rule(errors, IssueType.CODEINVALID, element.line(), element.col(), path, false, I18nConstants.TERMINOLOGY_TX_SYSTEM_WRONG_HTML, system, suggestSystemForPage(system));
            return false;
        } else {
            CodeSystem cs = getCodeSystem(system);
            if (rule(errors, IssueType.CODEINVALID, element.line(), element.col(), path, cs != null, I18nConstants.TERMINOLOGY_TX_SYSTEM_UNKNOWN, system)) {
                ConceptDefinitionComponent def = getCodeDefinition(cs, code);
                if (warning(errors, IssueType.CODEINVALID, element.line(), element.col(), path, def != null, I18nConstants.TERMINOLOGY_TX_CODE_UNKNOWN, system, code))
                    return warning(errors, IssueType.CODEINVALID, element.line(), element.col(), path, display == null || display.equals(def.getDisplay()), I18nConstants.TERMINOLOGY_TX_DISPLAY_WRONG, def.getDisplay());
            }
            return false;
        }
    } else if (context.isNoTerminologyServer() && Utilities.existsInList(system, "http://loinc.org", "http://unitsofmeasure.org", "http://hl7.org/fhir/sid/icd-9-cm", "http://snomed.info/sct", "http://www.nlm.nih.gov/research/umls/rxnorm")) {
        // no checks in this case
        return true;
    } else if (startsWithButIsNot(system, "http://snomed.info/sct", "http://loinc.org", "http://unitsofmeasure.org", "http://www.nlm.nih.gov/research/umls/rxnorm")) {
        rule(errors, IssueType.CODEINVALID, element.line(), element.col(), path, false, I18nConstants.TERMINOLOGY_TX_SYSTEM_INVALID, system);
        return false;
    } else {
        try {
            if (context.fetchResourceWithException(ValueSet.class, system) != null) {
                rule(errors, IssueType.CODEINVALID, element.line(), element.col(), path, false, I18nConstants.TERMINOLOGY_TX_SYSTEM_VALUESET, system);
            // Lloyd: This error used to prohibit checking for downstream issues, but there are some cases where that checking needs to occur.  Please talk to me before changing the code back.
            }
            boolean done = false;
            if (system.startsWith("https:") && system.length() > 7) {
                String ns = "http:" + system.substring(6);
                CodeSystem cs = getCodeSystem(ns);
                if (cs != null || Utilities.existsInList(system, "https://loinc.org", "https://unitsofmeasure.org", "https://snomed.info/sct", "https://www.nlm.nih.gov/research/umls/rxnorm")) {
                    rule(errors, IssueType.CODEINVALID, element.line(), element.col(), path, false, I18nConstants.TERMINOLOGY_TX_SYSTEM_HTTPS, system);
                    done = true;
                }
            }
            hint(errors, IssueType.UNKNOWN, element.line(), element.col(), path, done, I18nConstants.TERMINOLOGY_TX_SYSTEM_NOTKNOWN, system);
            return true;
        } catch (Exception e) {
            return true;
        }
    }
}
Also used : ConceptDefinitionComponent(org.hl7.fhir.r5.model.CodeSystem.ConceptDefinitionComponent) ValidationResult(org.hl7.fhir.r5.context.IWorkerContext.ValidationResult) CodeSystem(org.hl7.fhir.r5.model.CodeSystem) ValueSet(org.hl7.fhir.r5.model.ValueSet) TerminologyServiceException(org.hl7.fhir.exceptions.TerminologyServiceException) DefinitionException(org.hl7.fhir.exceptions.DefinitionException) IOException(java.io.IOException) FHIRException(org.hl7.fhir.exceptions.FHIRException) NotImplementedException(org.apache.commons.lang3.NotImplementedException) PathEngineException(org.hl7.fhir.exceptions.PathEngineException) FHIRLexerException(org.hl7.fhir.r5.utils.FHIRLexer.FHIRLexerException)

Example 100 with ConceptDefinitionComponent

use of org.hl7.fhir.dstu2.model.ValueSet.ConceptDefinitionComponent in project org.hl7.fhir.core by hapifhir.

the class CodeSystemValidator method checkCodes.

private void checkCodes(Set<String> codes, List<ConceptDefinitionComponent> list, String path, List<ValidationMessage> errors) {
    for (ConceptDefinitionComponent cc : list) {
        String npath = path + ".concept.descendents().where(code = '" + cc.getCode() + "')";
        rule(errors, IssueType.BUSINESSRULE, npath, !codes.contains(cc.getCode()), "Duplicate Code " + cc.getCode());
        codes.add(cc.getCode());
        checkCodes(codes, cc.getConcept(), npath, errors);
    }
}
Also used : ConceptDefinitionComponent(org.hl7.fhir.r5.model.CodeSystem.ConceptDefinitionComponent)

Aggregations

ConceptDefinitionComponent (org.hl7.fhir.r5.model.CodeSystem.ConceptDefinitionComponent)54 ArrayList (java.util.ArrayList)26 XhtmlNode (org.hl7.fhir.utilities.xhtml.XhtmlNode)26 ConceptDefinitionComponent (org.hl7.fhir.r4b.model.CodeSystem.ConceptDefinitionComponent)22 CodeSystem (org.hl7.fhir.r5.model.CodeSystem)21 ConceptDefinitionComponent (org.hl7.fhir.dstu3.model.CodeSystem.ConceptDefinitionComponent)17 ConceptDefinitionComponent (org.hl7.fhir.r4.model.CodeSystem.ConceptDefinitionComponent)16 ValueSet (org.hl7.fhir.dstu2.model.ValueSet)15 FHIRException (org.hl7.fhir.exceptions.FHIRException)14 CommaSeparatedStringBuilder (org.hl7.fhir.utilities.CommaSeparatedStringBuilder)12 ConceptPropertyComponent (org.hl7.fhir.r4b.model.CodeSystem.ConceptPropertyComponent)10 ConceptPropertyComponent (org.hl7.fhir.r5.model.CodeSystem.ConceptPropertyComponent)10 IOException (java.io.IOException)9 HashMap (java.util.HashMap)9 NotImplementedException (org.apache.commons.lang3.NotImplementedException)9 TerminologyServiceException (org.hl7.fhir.exceptions.TerminologyServiceException)9 FileNotFoundException (java.io.FileNotFoundException)8 ConceptDefinitionComponent (org.hl7.fhir.dstu2016may.model.CodeSystem.ConceptDefinitionComponent)8 ValidationResult (org.hl7.fhir.r5.context.IWorkerContext.ValidationResult)8 ConceptDefinitionDesignationComponent (org.hl7.fhir.r5.model.CodeSystem.ConceptDefinitionDesignationComponent)8