Search in sources :

Example 36 with ProfileComparison

use of org.hl7.fhir.dstu3.conformance.ProfileComparer.ProfileComparison in project org.hl7.fhir.core by hapifhir.

the class ProfileComparer method compareProfiles.

/**
 * Compare left and right structure definitions to see whether they are consistent or not
 *
 * Note that left and right are arbitrary choices. In one respect, left
 * is 'preferred' - the left's example value and data sets will be selected
 * over the right ones in the common structure definition
 * @throws DefinitionException
 * @throws IOException
 * @throws FHIRFormatError
 *
 * @
 */
public ProfileComparison compareProfiles(StructureDefinition left, StructureDefinition right) throws DefinitionException, IOException, FHIRFormatError {
    ProfileComparison outcome = new ProfileComparison();
    outcome.left = left;
    outcome.right = right;
    if (left == null)
        throw new DefinitionException("No StructureDefinition provided (left)");
    if (right == null)
        throw new DefinitionException("No StructureDefinition provided (right)");
    if (!left.hasSnapshot())
        throw new DefinitionException("StructureDefinition has no snapshot (left: " + outcome.leftName() + ")");
    if (!right.hasSnapshot())
        throw new DefinitionException("StructureDefinition has no snapshot (right: " + outcome.rightName() + ")");
    if (left.getSnapshot().getElement().isEmpty())
        throw new DefinitionException("StructureDefinition snapshot is empty (left: " + outcome.leftName() + ")");
    if (right.getSnapshot().getElement().isEmpty())
        throw new DefinitionException("StructureDefinition snapshot is empty (right: " + outcome.rightName() + ")");
    for (ProfileComparison pc : comparisons) if (pc.left.getUrl().equals(left.getUrl()) && pc.right.getUrl().equals(right.getUrl()))
        return pc;
    outcome.id = Integer.toString(comparisons.size() + 1);
    comparisons.add(outcome);
    DefinitionNavigator ln = new DefinitionNavigator(context, left);
    DefinitionNavigator rn = new DefinitionNavigator(context, right);
    // from here on in, any issues go in messages
    outcome.superset = new StructureDefinition();
    outcome.subset = new StructureDefinition();
    if (outcome.ruleEqual(ln.path(), null, ln.path(), rn.path(), "Base Type is not compatible", false)) {
        if (compareElements(outcome, ln.path(), ln, rn)) {
            outcome.subset.setName("intersection of " + outcome.leftName() + " and " + outcome.rightName());
            outcome.subset.setStatus(PublicationStatus.DRAFT);
            outcome.subset.setKind(outcome.left.getKind());
            outcome.subset.setType(outcome.left.getType());
            outcome.subset.setBaseDefinition("http://hl7.org/fhir/StructureDefinition/" + outcome.subset.getType());
            outcome.subset.setDerivation(TypeDerivationRule.CONSTRAINT);
            outcome.subset.setAbstract(false);
            outcome.superset.setName("union of " + outcome.leftName() + " and " + outcome.rightName());
            outcome.superset.setStatus(PublicationStatus.DRAFT);
            outcome.superset.setKind(outcome.left.getKind());
            outcome.superset.setType(outcome.left.getType());
            outcome.superset.setBaseDefinition("http://hl7.org/fhir/StructureDefinition/" + outcome.subset.getType());
            outcome.superset.setAbstract(false);
            outcome.superset.setDerivation(TypeDerivationRule.CONSTRAINT);
        } else {
            outcome.subset = null;
            outcome.superset = null;
        }
    }
    return outcome;
}
Also used : StructureDefinition(org.hl7.fhir.r4.model.StructureDefinition) DefinitionNavigator(org.hl7.fhir.r4.utils.DefinitionNavigator) DefinitionException(org.hl7.fhir.exceptions.DefinitionException)

Example 37 with ProfileComparison

use of org.hl7.fhir.dstu3.conformance.ProfileComparer.ProfileComparison in project org.hl7.fhir.core by hapifhir.

the class ProfileComparer method checkAddTypeUnion.

private void checkAddTypeUnion(String path, List<TypeRefComponent> results, TypeRefComponent nw) throws DefinitionException, IOException, FHIRFormatError {
    boolean pfound = false;
    boolean tfound = false;
    nw = nw.copy();
    if (nw.hasAggregation())
        throw new DefinitionException("Aggregation not supported: " + path);
    for (TypeRefComponent ex : results) {
        if (Utilities.equals(ex.getWorkingCode(), nw.getWorkingCode())) {
            if (!ex.hasProfile() && !nw.hasProfile())
                pfound = true;
            else if (!ex.hasProfile()) {
                pfound = true;
            } else if (!nw.hasProfile()) {
                pfound = true;
                ex.setProfile(null);
            } else {
                // both have profiles. Is one derived from the other?
                StructureDefinition sdex = context.fetchResource(StructureDefinition.class, ex.getProfile().get(0).getValue());
                StructureDefinition sdnw = context.fetchResource(StructureDefinition.class, nw.getProfile().get(0).getValue());
                if (sdex != null && sdnw != null) {
                    if (sdex == sdnw) {
                        pfound = true;
                    } else if (derivesFrom(sdex, sdnw)) {
                        ex.setProfile(nw.getProfile());
                        pfound = true;
                    } else if (derivesFrom(sdnw, sdex)) {
                        pfound = true;
                    } else if (sdnw.getSnapshot().getElement().get(0).getPath().equals(sdex.getSnapshot().getElement().get(0).getPath())) {
                        ProfileComparison comp = compareProfiles(sdex, sdnw);
                        if (comp.getSuperset() != null) {
                            pfound = true;
                            ex.addProfile("#" + comp.id);
                        }
                    }
                }
            }
            if (!ex.hasTargetProfile() && !nw.hasTargetProfile())
                tfound = true;
            else if (!ex.hasTargetProfile()) {
                tfound = true;
            } else if (!nw.hasTargetProfile()) {
                tfound = true;
                ex.setTargetProfile(null);
            } else {
                // both have profiles. Is one derived from the other?
                StructureDefinition sdex = context.fetchResource(StructureDefinition.class, ex.getTargetProfile().get(0).getValue());
                StructureDefinition sdnw = context.fetchResource(StructureDefinition.class, nw.getTargetProfile().get(0).getValue());
                if (sdex != null && sdnw != null) {
                    if (sdex == sdnw) {
                        tfound = true;
                    } else if (derivesFrom(sdex, sdnw)) {
                        ex.setTargetProfile(nw.getTargetProfile());
                        tfound = true;
                    } else if (derivesFrom(sdnw, sdex)) {
                        tfound = true;
                    } else if (sdnw.getSnapshot().getElement().get(0).getPath().equals(sdex.getSnapshot().getElement().get(0).getPath())) {
                        ProfileComparison comp = compareProfiles(sdex, sdnw);
                        if (comp.getSuperset() != null) {
                            tfound = true;
                            ex.addTargetProfile("#" + comp.id);
                        }
                    }
                }
            }
        }
    }
    if (!tfound || !pfound)
        results.add(nw);
}
Also used : StructureDefinition(org.hl7.fhir.r4.model.StructureDefinition) TypeRefComponent(org.hl7.fhir.r4.model.ElementDefinition.TypeRefComponent) DefinitionException(org.hl7.fhir.exceptions.DefinitionException)

Example 38 with ProfileComparison

use of org.hl7.fhir.dstu3.conformance.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, FHIRFormatError {
    List<TypeRefComponent> result = new ArrayList<TypeRefComponent>();
    for (TypeRefComponent l : left) {
        if (l.hasAggregation())
            throw new DefinitionException("Aggregation not supported: " + path);
        boolean pfound = false;
        boolean tfound = false;
        TypeRefComponent c = l.copy();
        for (TypeRefComponent r : right) {
            if (r.hasAggregation())
                throw new DefinitionException("Aggregation not supported: " + path);
            if (!l.hasProfile() && !r.hasProfile()) {
                pfound = true;
            } else if (!r.hasProfile()) {
                pfound = true;
            } else if (!l.hasProfile()) {
                pfound = true;
                c.setProfile(r.getProfile());
            } else {
                StructureDefinition sdl = resolveProfile(ed, outcome, path, l.getProfile().get(0).getValue(), outcome.leftName());
                StructureDefinition sdr = resolveProfile(ed, outcome, path, r.getProfile().get(0).getValue(), outcome.rightName());
                if (sdl != null && sdr != null) {
                    if (sdl == sdr) {
                        pfound = true;
                    } else if (derivesFrom(sdl, sdr)) {
                        pfound = true;
                    } else if (derivesFrom(sdr, sdl)) {
                        c.setProfile(r.getProfile());
                        pfound = true;
                    } else if (sdl.getType().equals(sdr.getType())) {
                        ProfileComparison comp = compareProfiles(sdl, sdr);
                        if (comp.getSubset() != null) {
                            pfound = true;
                            c.addProfile("#" + comp.id);
                        }
                    }
                }
            }
            if (!l.hasTargetProfile() && !r.hasTargetProfile()) {
                tfound = true;
            } else if (!r.hasTargetProfile()) {
                tfound = true;
            } else if (!l.hasTargetProfile()) {
                tfound = true;
                c.setTargetProfile(r.getTargetProfile());
            } else {
                StructureDefinition sdl = resolveProfile(ed, outcome, path, l.getProfile().get(0).getValue(), outcome.leftName());
                StructureDefinition sdr = resolveProfile(ed, outcome, path, r.getProfile().get(0).getValue(), outcome.rightName());
                if (sdl != null && sdr != null) {
                    if (sdl == sdr) {
                        tfound = true;
                    } else if (derivesFrom(sdl, sdr)) {
                        tfound = true;
                    } else if (derivesFrom(sdr, sdl)) {
                        c.setTargetProfile(r.getTargetProfile());
                        tfound = true;
                    } else if (sdl.getType().equals(sdr.getType())) {
                        ProfileComparison comp = compareProfiles(sdl, sdr);
                        if (comp.getSubset() != null) {
                            tfound = true;
                            c.addTargetProfile("#" + comp.id);
                        }
                    }
                }
            }
        }
        if (pfound && tfound)
            result.add(c);
    }
    return result;
}
Also used : StructureDefinition(org.hl7.fhir.r4.model.StructureDefinition) TypeRefComponent(org.hl7.fhir.r4.model.ElementDefinition.TypeRefComponent) ArrayList(java.util.ArrayList) DefinitionException(org.hl7.fhir.exceptions.DefinitionException)

Example 39 with ProfileComparison

use of org.hl7.fhir.dstu3.conformance.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) throws FHIRFormatError {
    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, ValidationMessage.IssueType.STRUCTURE, path, "Example/preferred bindings differ at " + path + " using binding from " + outcome.rightName(), ValidationMessage.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, ValidationMessage.IssueType.STRUCTURE, path, "Example/preferred bindings differ at " + path + " using binding from " + outcome.leftName(), ValidationMessage.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, ValidationMessage.IssueType.STRUCTURE, path, "No left Value set at " + path, ValidationMessage.IssueSeverity.ERROR));
        return true;
    } else if (!right.hasValueSet()) {
        outcome.messages.add(new ValidationMessage(Source.ProfileComparer, ValidationMessage.IssueType.STRUCTURE, path, "No right Value set at " + path, ValidationMessage.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, ValidationMessage.IssueType.STRUCTURE, path, "Unable to resolve left value set " + left.getValueSet().toString() + " at " + path, ValidationMessage.IssueSeverity.ERROR));
            return true;
        } else if (rvs == null) {
            outcome.messages.add(new ValidationMessage(Source.ProfileComparer, ValidationMessage.IssueType.STRUCTURE, path, "Unable to resolve right value set " + right.getValueSet().toString() + " at " + path, ValidationMessage.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, false);
                    re = context.expandVS(rvs, true, false);
                    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, ValidationMessage.IssueType.STRUCTURE, path, "The value sets " + lvs.getUrl() + " and " + rvs.getUrl() + " do not intersect", ValidationMessage.IssueSeverity.ERROR));
                        status(subset, ProfileUtilities.STATUS_ERROR);
                        return false;
                    }
                } catch (Exception e) {
                    outcome.messages.add(new ValidationMessage(Source.ProfileComparer, ValidationMessage.IssueType.STRUCTURE, path, "Unable to expand or process value sets " + lvs.getUrl() + " and " + rvs.getUrl() + ": " + e.getMessage(), ValidationMessage.IssueSeverity.ERROR));
                    status(subset, ProfileUtilities.STATUS_ERROR);
                    return false;
                }
            }
            subBinding.setValueSet("#" + addValueSet(cvs));
            superBinding.setValueSet("#" + addValueSet(unite(superset, outcome, path, lvs, rvs)));
        }
    }
    return false;
}
Also used : ValidationMessage(org.hl7.fhir.utilities.validation.ValidationMessage) ValueSetExpansionOutcome(org.hl7.fhir.r4.terminologies.ValueSetExpander.ValueSetExpansionOutcome) DefinitionException(org.hl7.fhir.exceptions.DefinitionException) ElementDefinitionBindingComponent(org.hl7.fhir.r4.model.ElementDefinition.ElementDefinitionBindingComponent) ValueSet(org.hl7.fhir.r4.model.ValueSet) DefinitionException(org.hl7.fhir.exceptions.DefinitionException) IOException(java.io.IOException)

Example 40 with ProfileComparison

use of org.hl7.fhir.dstu3.conformance.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, FHIRFormatError {
    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, ValidationMessage.IssueType.STRUCTURE, path, "Different number of children at " + path + " (" + Integer.toString(lc.size()) + "/" + Integer.toString(rc.size()) + ")", ValidationMessage.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, ValidationMessage.IssueType.STRUCTURE, path, "Different path at " + path + "[" + Integer.toString(i) + "] (" + l.path() + "/" + r.path() + ")", ValidationMessage.IssueSeverity.ERROR));
                status(ed, ProfileUtilities.STATUS_ERROR);
                return false;
            }
        }
    }
    return true;
}
Also used : DefinitionNavigator(org.hl7.fhir.r4.utils.DefinitionNavigator) ValidationMessage(org.hl7.fhir.utilities.validation.ValidationMessage)

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