Search in sources :

Example 56 with ProfileComparison

use of org.hl7.fhir.r4b.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, ValidationMessage.IssueType.INFORMATIONAL, path, "Unable to resolve profile " + url + " in profile " + name, ValidationMessage.IssueSeverity.WARNING));
        status(ed, ProfileUtilities.STATUS_HINT);
    }
    return res;
}
Also used : StructureDefinition(org.hl7.fhir.dstu3.model.StructureDefinition) ValidationMessage(org.hl7.fhir.utilities.validation.ValidationMessage)

Example 57 with ProfileComparison

use of org.hl7.fhir.r4b.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) 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(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.dstu3.model.Reference) ValueSetExpansionOutcome(org.hl7.fhir.dstu3.terminologies.ValueSetExpander.ValueSetExpansionOutcome) DefinitionException(org.hl7.fhir.exceptions.DefinitionException) ElementDefinitionBindingComponent(org.hl7.fhir.dstu3.model.ElementDefinition.ElementDefinitionBindingComponent) ValueSet(org.hl7.fhir.dstu3.model.ValueSet) DefinitionException(org.hl7.fhir.exceptions.DefinitionException) IOException(java.io.IOException)

Example 58 with ProfileComparison

use of org.hl7.fhir.r4b.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, 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(), outcome.leftName());
                StructureDefinition sdr = resolveProfile(ed, outcome, path, r.getProfile(), 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.setProfile("#" + 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(), outcome.leftName());
                StructureDefinition sdr = resolveProfile(ed, outcome, path, r.getProfile(), 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.setTargetProfile("#" + comp.id);
                        }
                    }
                }
            }
        }
        if (pfound && tfound)
            result.add(c);
    }
    return result;
}
Also used : StructureDefinition(org.hl7.fhir.dstu3.model.StructureDefinition) TypeRefComponent(org.hl7.fhir.dstu3.model.ElementDefinition.TypeRefComponent) ArrayList(java.util.ArrayList) DefinitionException(org.hl7.fhir.exceptions.DefinitionException)

Example 59 with ProfileComparison

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

the class ProfileComparer method unionConstraints.

// we can't really know about constraints. We create warnings, and collate them
private List<ElementDefinitionConstraintComponent> unionConstraints(ElementDefinition ed, ProfileComparison outcome, String path, List<ElementDefinitionConstraintComponent> left, List<ElementDefinitionConstraintComponent> right) {
    List<ElementDefinitionConstraintComponent> result = new ArrayList<ElementDefinitionConstraintComponent>();
    for (ElementDefinitionConstraintComponent l : left) {
        boolean found = false;
        for (ElementDefinitionConstraintComponent r : right) if (Utilities.equals(r.getId(), l.getId()) || (Utilities.equals(r.getXpath(), l.getXpath()) && r.getSeverity() == l.getSeverity()))
            found = true;
        if (!found) {
            outcome.messages.add(new ValidationMessage(Source.ProfileComparer, ValidationMessage.IssueType.STRUCTURE, path, "StructureDefinition " + outcome.leftName() + " has a constraint that is not found in " + outcome.rightName() + " and it is uncertain whether they are compatible (" + l.getXpath() + ")", ValidationMessage.IssueSeverity.INFORMATION));
            status(ed, ProfileUtilities.STATUS_WARNING);
        }
        result.add(l);
    }
    for (ElementDefinitionConstraintComponent r : right) {
        boolean found = false;
        for (ElementDefinitionConstraintComponent l : left) if (Utilities.equals(r.getId(), l.getId()) || (Utilities.equals(r.getXpath(), l.getXpath()) && r.getSeverity() == l.getSeverity()))
            found = true;
        if (!found) {
            outcome.messages.add(new ValidationMessage(Source.ProfileComparer, ValidationMessage.IssueType.STRUCTURE, path, "StructureDefinition " + outcome.rightName() + " has a constraint that is not found in " + outcome.leftName() + " and it is uncertain whether they are compatible (" + r.getXpath() + ")", ValidationMessage.IssueSeverity.INFORMATION));
            status(ed, ProfileUtilities.STATUS_WARNING);
            result.add(r);
        }
    }
    return result;
}
Also used : ValidationMessage(org.hl7.fhir.utilities.validation.ValidationMessage) ArrayList(java.util.ArrayList) ElementDefinitionConstraintComponent(org.hl7.fhir.dstu3.model.ElementDefinition.ElementDefinitionConstraintComponent)

Example 60 with ProfileComparison

use of org.hl7.fhir.r4b.comparison.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.dstu3.model.StructureDefinition) DefinitionNavigator(org.hl7.fhir.dstu3.utils.DefinitionNavigator) DefinitionException(org.hl7.fhir.exceptions.DefinitionException)

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