Search in sources :

Example 11 with ProfileComparison

use of org.hl7.fhir.r5.comparison.ProfileComparer.ProfileComparison in project org.hl7.fhir.core by hapifhir.

the class ProfileComparer method resolveProfile.

private StructureDefinition resolveProfile(ElementDefinition ed, ProfileComparison outcome, String path, String url, String name) {
    StructureDefinition res = context.fetchResource(StructureDefinition.class, url);
    if (res == null) {
        outcome.messages.add(new ValidationMessage(Source.ProfileComparer, IssueType.INFORMATIONAL, path, "Unable to resolve profile " + url + " in profile " + name, IssueSeverity.INFORMATION));
        status(ed, ProfileUtilities.STATUS_HINT);
    }
    return res;
}
Also used : StructureDefinition(org.hl7.fhir.dstu2016may.model.StructureDefinition) ValidationMessage(org.hl7.fhir.utilities.validation.ValidationMessage)

Example 12 with ProfileComparison

use of org.hl7.fhir.r5.comparison.ProfileComparer.ProfileComparison in project org.hl7.fhir.core by hapifhir.

the class ProfileComparer method compareChildren.

private boolean compareChildren(ElementDefinition ed, ProfileComparison outcome, String path, DefinitionNavigator left, DefinitionNavigator right) throws DefinitionException, IOException {
    List<DefinitionNavigator> lc = left.children();
    List<DefinitionNavigator> rc = right.children();
    // walk into it
    if (lc.isEmpty() && !rc.isEmpty() && right.current().getType().size() == 1 && left.hasTypeChildren(right.current().getType().get(0)))
        lc = left.childrenFromType(right.current().getType().get(0));
    if (rc.isEmpty() && !lc.isEmpty() && left.current().getType().size() == 1 && right.hasTypeChildren(left.current().getType().get(0)))
        rc = right.childrenFromType(left.current().getType().get(0));
    if (lc.size() != rc.size()) {
        outcome.messages.add(new ValidationMessage(Source.ProfileComparer, IssueType.STRUCTURE, path, "Different number of children at " + path + " (" + Integer.toString(lc.size()) + "/" + Integer.toString(rc.size()) + ")", IssueSeverity.ERROR));
        status(ed, ProfileUtilities.STATUS_ERROR);
        return false;
    } else {
        for (int i = 0; i < lc.size(); i++) {
            DefinitionNavigator l = lc.get(i);
            DefinitionNavigator r = rc.get(i);
            String cpath = comparePaths(l.path(), r.path(), path, l.nameTail(), r.nameTail());
            if (cpath != null) {
                if (!compareElements(outcome, cpath, l, r))
                    return false;
            } else {
                outcome.messages.add(new ValidationMessage(Source.ProfileComparer, IssueType.STRUCTURE, path, "Different path at " + path + "[" + Integer.toString(i) + "] (" + l.path() + "/" + r.path() + ")", IssueSeverity.ERROR));
                status(ed, ProfileUtilities.STATUS_ERROR);
                return false;
            }
        }
    }
    return true;
}
Also used : ValidationMessage(org.hl7.fhir.utilities.validation.ValidationMessage)

Example 13 with ProfileComparison

use of org.hl7.fhir.r5.comparison.ProfileComparer.ProfileComparison in project org.hl7.fhir.core by hapifhir.

the class ProfileComparer method intersectTypes.

private Collection<? extends TypeRefComponent> intersectTypes(ElementDefinition ed, ProfileComparison outcome, String path, List<TypeRefComponent> left, List<TypeRefComponent> right) throws DefinitionException, IOException {
    List<TypeRefComponent> result = new ArrayList<TypeRefComponent>();
    for (TypeRefComponent l : left) {
        if (l.getProfile().size() > 1)
            throw new DefinitionException("Multiple profiles not supported: " + path + ": " + listProfiles(l.getProfile()));
        if (l.hasAggregation())
            throw new DefinitionException("Aggregation not supported: " + path);
        boolean found = false;
        TypeRefComponent c = l.copy();
        for (TypeRefComponent r : right) {
            if (r.getProfile().size() > 1)
                throw new DefinitionException("Multiple profiles not supported: " + path + ": " + listProfiles(l.getProfile()));
            if (r.hasAggregation())
                throw new DefinitionException("Aggregation not supported: " + path);
            if (!l.hasProfile() && !r.hasProfile()) {
                found = true;
            } else if (!r.hasProfile()) {
                found = true;
            } else if (!l.hasProfile()) {
                found = true;
                c.getProfile().add(r.getProfile().get(0));
            } else {
                StructureDefinition sdl = resolveProfile(ed, outcome, path, l.getProfile().get(0).getValueAsString(), outcome.leftName());
                StructureDefinition sdr = resolveProfile(ed, outcome, path, r.getProfile().get(0).getValueAsString(), outcome.rightName());
                if (sdl != null && sdr != null) {
                    if (sdl == sdr) {
                        found = true;
                    } else if (derivesFrom(sdl, sdr)) {
                        found = true;
                    } else if (derivesFrom(sdr, sdl)) {
                        c.getProfile().clear();
                        c.getProfile().add(r.getProfile().get(0));
                        found = true;
                    } else if (sdl.hasBaseType() && sdr.hasBaseType() && sdl.getBaseType().equals(sdr.getBaseType())) {
                        ProfileComparison comp = compareProfiles(sdl, sdr);
                        if (comp.getSubset() != null) {
                            found = true;
                            c.addProfile("#" + comp.id);
                        }
                    }
                }
            }
        }
        if (found)
            result.add(c);
    }
    return result;
}
Also used : StructureDefinition(org.hl7.fhir.dstu2016may.model.StructureDefinition) TypeRefComponent(org.hl7.fhir.dstu2016may.model.ElementDefinition.TypeRefComponent) ArrayList(java.util.ArrayList) DefinitionException(org.hl7.fhir.exceptions.DefinitionException)

Example 14 with ProfileComparison

use of org.hl7.fhir.r5.comparison.ProfileComparer.ProfileComparison in project org.hl7.fhir.core by hapifhir.

the class ProfileComparer method unionBindings.

private ElementDefinitionBindingComponent unionBindings(ElementDefinition ed, ProfileComparison outcome, String path, ElementDefinitionBindingComponent left, ElementDefinitionBindingComponent right) {
    ElementDefinitionBindingComponent union = new ElementDefinitionBindingComponent();
    if (left.getStrength().compareTo(right.getStrength()) < 0)
        union.setStrength(left.getStrength());
    else
        union.setStrength(right.getStrength());
    union.setDescription(mergeText(ed, outcome, path, "binding.description", left.getDescription(), right.getDescription()));
    if (Base.compareDeep(left.getValueSet(), right.getValueSet(), false))
        union.setValueSet(left.getValueSet());
    else {
        ValueSet lvs = resolveVS(outcome.left, left.getValueSet());
        ValueSet rvs = resolveVS(outcome.left, right.getValueSet());
        if (lvs != null && rvs != null)
            union.setValueSet(new Reference().setReference("#" + addValueSet(unite(ed, outcome, path, lvs, rvs))));
        else if (lvs != null)
            union.setValueSet(new Reference().setReference("#" + addValueSet(lvs)));
        else if (rvs != null)
            union.setValueSet(new Reference().setReference("#" + addValueSet(rvs)));
    }
    return union;
}
Also used : Reference(org.hl7.fhir.dstu2016may.model.Reference) ElementDefinitionBindingComponent(org.hl7.fhir.dstu2016may.model.ElementDefinition.ElementDefinitionBindingComponent) ValueSet(org.hl7.fhir.dstu2016may.model.ValueSet)

Example 15 with ProfileComparison

use of org.hl7.fhir.r5.comparison.ProfileComparer.ProfileComparison in project org.hl7.fhir.core by hapifhir.

the class ProfileComparer method compareBindings.

private boolean compareBindings(ProfileComparison outcome, ElementDefinition subset, ElementDefinition superset, String path, ElementDefinition lDef, ElementDefinition rDef) {
    assert (lDef.hasBinding() || rDef.hasBinding());
    if (!lDef.hasBinding()) {
        subset.setBinding(rDef.getBinding());
        // technically, the super set is unbound, but that's not very useful - so we use the provided on as an example
        superset.setBinding(rDef.getBinding().copy());
        superset.getBinding().setStrength(BindingStrength.EXAMPLE);
        return true;
    }
    if (!rDef.hasBinding()) {
        subset.setBinding(lDef.getBinding());
        superset.setBinding(lDef.getBinding().copy());
        superset.getBinding().setStrength(BindingStrength.EXAMPLE);
        return true;
    }
    ElementDefinitionBindingComponent left = lDef.getBinding();
    ElementDefinitionBindingComponent right = rDef.getBinding();
    if (Base.compareDeep(left, right, false)) {
        subset.setBinding(left);
        superset.setBinding(right);
    }
    // superset:
    if (isPreferredOrExample(left) && isPreferredOrExample(right)) {
        if (right.getStrength() == BindingStrength.PREFERRED && left.getStrength() == BindingStrength.EXAMPLE && !Base.compareDeep(left.getValueSet(), right.getValueSet(), false)) {
            outcome.messages.add(new ValidationMessage(Source.ProfileComparer, IssueType.STRUCTURE, path, "Example/preferred bindings differ at " + path + " using binding from " + outcome.rightName(), IssueSeverity.INFORMATION));
            status(subset, ProfileUtilities.STATUS_HINT);
            subset.setBinding(right);
            superset.setBinding(unionBindings(superset, outcome, path, left, right));
        } else {
            if ((right.getStrength() != BindingStrength.EXAMPLE || left.getStrength() != BindingStrength.EXAMPLE) && !Base.compareDeep(left.getValueSet(), right.getValueSet(), false)) {
                outcome.messages.add(new ValidationMessage(Source.ProfileComparer, IssueType.STRUCTURE, path, "Example/preferred bindings differ at " + path + " using binding from " + outcome.leftName(), IssueSeverity.INFORMATION));
                status(subset, ProfileUtilities.STATUS_HINT);
            }
            subset.setBinding(left);
            superset.setBinding(unionBindings(superset, outcome, path, left, right));
        }
        return true;
    }
    // if either of them are extensible/required, then it wins
    if (isPreferredOrExample(left)) {
        subset.setBinding(right);
        superset.setBinding(unionBindings(superset, outcome, path, left, right));
        return true;
    }
    if (isPreferredOrExample(right)) {
        subset.setBinding(left);
        superset.setBinding(unionBindings(superset, outcome, path, left, right));
        return true;
    }
    // ok, both are extensible or required.
    ElementDefinitionBindingComponent subBinding = new ElementDefinitionBindingComponent();
    subset.setBinding(subBinding);
    ElementDefinitionBindingComponent superBinding = new ElementDefinitionBindingComponent();
    superset.setBinding(superBinding);
    subBinding.setDescription(mergeText(subset, outcome, path, "description", left.getDescription(), right.getDescription()));
    superBinding.setDescription(mergeText(subset, outcome, null, "description", left.getDescription(), right.getDescription()));
    if (left.getStrength() == BindingStrength.REQUIRED || right.getStrength() == BindingStrength.REQUIRED)
        subBinding.setStrength(BindingStrength.REQUIRED);
    else
        subBinding.setStrength(BindingStrength.EXTENSIBLE);
    if (left.getStrength() == BindingStrength.EXTENSIBLE || right.getStrength() == BindingStrength.EXTENSIBLE)
        superBinding.setStrength(BindingStrength.EXTENSIBLE);
    else
        superBinding.setStrength(BindingStrength.REQUIRED);
    if (Base.compareDeep(left.getValueSet(), right.getValueSet(), false)) {
        subBinding.setValueSet(left.getValueSet());
        superBinding.setValueSet(left.getValueSet());
        return true;
    } else if (!left.hasValueSet()) {
        outcome.messages.add(new ValidationMessage(Source.ProfileComparer, IssueType.STRUCTURE, path, "No left Value set at " + path, IssueSeverity.ERROR));
        return true;
    } else if (!right.hasValueSet()) {
        outcome.messages.add(new ValidationMessage(Source.ProfileComparer, IssueType.STRUCTURE, path, "No right Value set at " + path, IssueSeverity.ERROR));
        return true;
    } else {
        // ok, now we compare the value sets. This may be unresolvable.
        ValueSet lvs = resolveVS(outcome.left, left.getValueSet());
        ValueSet rvs = resolveVS(outcome.right, right.getValueSet());
        if (lvs == null) {
            outcome.messages.add(new ValidationMessage(Source.ProfileComparer, IssueType.STRUCTURE, path, "Unable to resolve left value set " + left.getValueSet().toString() + " at " + path, IssueSeverity.ERROR));
            return true;
        } else if (rvs == null) {
            outcome.messages.add(new ValidationMessage(Source.ProfileComparer, IssueType.STRUCTURE, path, "Unable to resolve right value set " + right.getValueSet().toString() + " at " + path, IssueSeverity.ERROR));
            return true;
        } else {
            // first, we'll try to do it by definition
            ValueSet cvs = intersectByDefinition(lvs, rvs);
            if (cvs == null) {
                // if that didn't work, we'll do it by expansion
                ValueSetExpansionOutcome le;
                ValueSetExpansionOutcome re;
                try {
                    le = context.expandVS(lvs, true);
                    re = context.expandVS(rvs, true);
                    if (!closed(le.getValueset()) || !closed(re.getValueset()))
                        throw new DefinitionException("unclosed value sets are not handled yet");
                    cvs = intersectByExpansion(lvs, rvs);
                    if (!cvs.getCompose().hasInclude()) {
                        outcome.messages.add(new ValidationMessage(Source.ProfileComparer, IssueType.STRUCTURE, path, "The value sets " + lvs.getUrl() + " and " + rvs.getUrl() + " do not intersect", IssueSeverity.ERROR));
                        status(subset, ProfileUtilities.STATUS_ERROR);
                        return false;
                    }
                } catch (Exception e) {
                    outcome.messages.add(new ValidationMessage(Source.ProfileComparer, IssueType.STRUCTURE, path, "Unable to expand or process value sets " + lvs.getUrl() + " and " + rvs.getUrl() + ": " + e.getMessage(), IssueSeverity.ERROR));
                    status(subset, ProfileUtilities.STATUS_ERROR);
                    return false;
                }
            }
            subBinding.setValueSet(new Reference().setReference("#" + addValueSet(cvs)));
            superBinding.setValueSet(new Reference().setReference("#" + addValueSet(unite(superset, outcome, path, lvs, rvs))));
        }
    }
    return false;
}
Also used : ValidationMessage(org.hl7.fhir.utilities.validation.ValidationMessage) Reference(org.hl7.fhir.dstu2016may.model.Reference) ValueSetExpansionOutcome(org.hl7.fhir.dstu2016may.terminologies.ValueSetExpander.ValueSetExpansionOutcome) DefinitionException(org.hl7.fhir.exceptions.DefinitionException) ElementDefinitionBindingComponent(org.hl7.fhir.dstu2016may.model.ElementDefinition.ElementDefinitionBindingComponent) ValueSet(org.hl7.fhir.dstu2016may.model.ValueSet) DefinitionException(org.hl7.fhir.exceptions.DefinitionException) IOException(java.io.IOException)

Aggregations

ValidationMessage (org.hl7.fhir.utilities.validation.ValidationMessage)26 DefinitionException (org.hl7.fhir.exceptions.DefinitionException)24 ArrayList (java.util.ArrayList)14 FileOutputStream (java.io.FileOutputStream)5 StructureDefinition (org.hl7.fhir.r5.model.StructureDefinition)5 IOException (java.io.IOException)4 StructureDefinition (org.hl7.fhir.dstu2.model.StructureDefinition)4 StructureDefinition (org.hl7.fhir.dstu2016may.model.StructureDefinition)4 StructureDefinition (org.hl7.fhir.dstu3.model.StructureDefinition)4 StructureDefinition (org.hl7.fhir.r4.model.StructureDefinition)4 ValueSet (org.hl7.fhir.dstu2.model.ValueSet)3 ValueSet (org.hl7.fhir.dstu3.model.ValueSet)3 FHIRException (org.hl7.fhir.exceptions.FHIRException)3 ValueSet (org.hl7.fhir.r4.model.ValueSet)3 StructureDefinition (org.hl7.fhir.r4b.model.StructureDefinition)3 ValueSetComparison (org.hl7.fhir.r5.comparison.ValueSetComparer.ValueSetComparison)3 ValueSet (org.hl7.fhir.r5.model.ValueSet)3 Date (java.util.Date)2 HashMap (java.util.HashMap)2 ElementDefinitionBindingComponent (org.hl7.fhir.dstu2.model.ElementDefinition.ElementDefinitionBindingComponent)2