Search in sources :

Example 6 with ProfileComparison

use of org.hl7.fhir.dstu2.utils.ProfileComparer.ProfileComparison in project org.hl7.fhir.core by hapifhir.

the class ProfileComparer method compareElements.

/**
 * left and right refer to the same element. Are they compatible?
 * @param outcome
 * @param outcome
 * @param path
 * @param left
 * @param right
 * @- if there's a problem that needs fixing in this code
 * @throws DefinitionException
 * @throws IOException
 */
private boolean compareElements(ProfileComparison outcome, String path, DefinitionNavigator left, DefinitionNavigator right) throws DefinitionException, IOException {
    // preconditions:
    assert (path != null);
    assert (left != null);
    assert (right != null);
    assert (left.path().equals(right.path()));
    // we ignore slicing right now - we're going to clone the root one anyway, and then think about clones
    // simple stuff
    ElementDefinition subset = new ElementDefinition();
    subset.setPath(left.path());
    // not allowed to be different:
    // can't be bothered even testing this one
    subset.getRepresentation().addAll(left.current().getRepresentation());
    if (!outcome.ruleCompares(subset, left.current().getDefaultValue(), right.current().getDefaultValue(), path + ".defaultValue[x]", BOTH_NULL))
        return false;
    subset.setDefaultValue(left.current().getDefaultValue());
    if (!outcome.ruleEqual(path, subset, left.current().getMeaningWhenMissing(), right.current().getMeaningWhenMissing(), "meaningWhenMissing Must be the same", true))
        return false;
    subset.setMeaningWhenMissing(left.current().getMeaningWhenMissing());
    if (!outcome.ruleEqual(subset, left.current().getIsModifier(), right.current().getIsModifier(), path, "isModifier"))
        return false;
    subset.setIsModifier(left.current().getIsModifier());
    if (!outcome.ruleEqual(subset, left.current().getIsSummary(), right.current().getIsSummary(), path, "isSummary"))
        return false;
    subset.setIsSummary(left.current().getIsSummary());
    // descriptive properties from ElementDefinition - merge them:
    subset.setLabel(mergeText(subset, outcome, path, "label", left.current().getLabel(), right.current().getLabel()));
    subset.setShort(mergeText(subset, outcome, path, "short", left.current().getShort(), right.current().getShort()));
    subset.setDefinition(mergeText(subset, outcome, path, "definition", left.current().getDefinition(), right.current().getDefinition()));
    subset.setComments(mergeText(subset, outcome, path, "comments", left.current().getComments(), right.current().getComments()));
    subset.setRequirements(mergeText(subset, outcome, path, "requirements", left.current().getRequirements(), right.current().getRequirements()));
    subset.getCode().addAll(mergeCodings(left.current().getCode(), right.current().getCode()));
    subset.getAlias().addAll(mergeStrings(left.current().getAlias(), right.current().getAlias()));
    subset.getMapping().addAll(mergeMappings(left.current().getMapping(), right.current().getMapping()));
    // left will win for example
    subset.setExample(left.current().hasExample() ? left.current().getExample() : right.current().getExample());
    subset.setMustSupport(left.current().getMustSupport() || right.current().getMustSupport());
    ElementDefinition superset = subset.copy();
    // compare and intersect
    superset.setMin(unionMin(left.current().getMin(), right.current().getMin()));
    superset.setMax(unionMax(left.current().getMax(), right.current().getMax()));
    subset.setMin(intersectMin(left.current().getMin(), right.current().getMin()));
    subset.setMax(intersectMax(left.current().getMax(), right.current().getMax()));
    outcome.rule(subset, subset.getMax().equals("*") || Integer.parseInt(subset.getMax()) >= subset.getMin(), path, "Cardinality Mismatch: " + card(left) + "/" + card(right));
    superset.getType().addAll(unionTypes(path, left.current().getType(), right.current().getType()));
    subset.getType().addAll(intersectTypes(subset, outcome, path, left.current().getType(), right.current().getType()));
    outcome.rule(subset, !subset.getType().isEmpty() || (!left.current().hasType() && !right.current().hasType()), path, "Type Mismatch:\r\n  " + typeCode(left) + "\r\n  " + typeCode(right));
    // <fixed[x]><!-- ?? 0..1 * Value must be exactly this --></fixed[x]>
    // <pattern[x]><!-- ?? 0..1 * Value must have at least these property values --></pattern[x]>
    superset.setMaxLengthElement(unionMaxLength(left.current().getMaxLength(), right.current().getMaxLength()));
    subset.setMaxLengthElement(intersectMaxLength(left.current().getMaxLength(), right.current().getMaxLength()));
    if (left.current().hasBinding() || right.current().hasBinding()) {
        compareBindings(outcome, subset, superset, path, left.current(), right.current());
    }
    // note these are backwards
    superset.getConstraint().addAll(intersectConstraints(path, left.current().getConstraint(), right.current().getConstraint()));
    subset.getConstraint().addAll(unionConstraints(subset, outcome, path, left.current().getConstraint(), right.current().getConstraint()));
    // now process the slices
    if (left.current().hasSlicing() || right.current().hasSlicing()) {
        if (isExtension(left.path()))
            return compareExtensions(outcome, path, superset, subset, left, right);
        else
            throw new DefinitionException("Slicing is not handled yet");
    // todo: name
    }
    // add the children
    outcome.subset.getSnapshot().getElement().add(subset);
    outcome.superset.getSnapshot().getElement().add(superset);
    return compareChildren(subset, outcome, path, left, right);
}
Also used : ElementDefinition(org.hl7.fhir.dstu2.model.ElementDefinition) DefinitionException(org.hl7.fhir.exceptions.DefinitionException)

Example 7 with ProfileComparison

use of org.hl7.fhir.dstu2.utils.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 {
    boolean found = false;
    nw = nw.copy();
    if (nw.getProfile().size() > 1)
        throw new DefinitionException("Multiple profiles not supported: " + path);
    if (nw.hasAggregation())
        throw new DefinitionException("Aggregation not supported: " + path);
    for (TypeRefComponent ex : results) {
        if (Utilities.equals(ex.getCode(), nw.getCode())) {
            if (!ex.hasProfile() && !nw.hasProfile())
                found = true;
            else if (!ex.hasProfile()) {
                found = true;
            } else if (!nw.hasProfile()) {
                found = true;
                ex.getProfile().clear();
            } else {
                // both have profiles. Is one derived from the other?
                StructureDefinition sdex = context.fetchResource(StructureDefinition.class, ex.getProfile().get(0).getValueAsString());
                StructureDefinition sdnw = context.fetchResource(StructureDefinition.class, nw.getProfile().get(0).getValueAsString());
                if (sdex != null && sdnw != null) {
                    if (sdex == sdnw) {
                        found = true;
                    } else if (derivesFrom(sdex, sdnw)) {
                        ex.getProfile().clear();
                        ex.getProfile().add(nw.getProfile().get(0));
                        found = true;
                    } else if (derivesFrom(sdnw, sdex)) {
                        found = 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) {
                            found = true;
                            ex.getProfile().clear();
                            ex.addProfile("#" + comp.id);
                        }
                    }
                }
            }
        }
    }
    if (!found)
        results.add(nw);
}
Also used : StructureDefinition(org.hl7.fhir.dstu2.model.StructureDefinition) TypeRefComponent(org.hl7.fhir.dstu2.model.ElementDefinition.TypeRefComponent) DefinitionException(org.hl7.fhir.exceptions.DefinitionException)

Example 8 with ProfileComparison

use of org.hl7.fhir.dstu2.utils.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, 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() + ")", 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, 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() + ")", 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.dstu2.model.ElementDefinition.ElementDefinitionConstraintComponent)

Example 9 with ProfileComparison

use of org.hl7.fhir.dstu2.utils.ProfileComparer.ProfileComparison in project org.hl7.fhir.core by hapifhir.

the class ProfileComparer method mergeText.

// result.addAll(left);
// for (TypeRefComponent r : right) {
// boolean found = false;
// TypeRefComponent c = r.copy();
// for (TypeRefComponent l : left)
// if (Utilities.equals(l.getCode(), r.getCode())) {
// 
// }
// if (l.getCode().equals("Reference") && r.getCode().equals("Reference")) {
// if (Base.compareDeep(l.getProfile(), r.getProfile(), false)) {
// found = true;
// }
// } else
// found = true;
// // todo: compare profiles
// // todo: compare aggregation values
// }
// if (!found)
// result.add(c);
// }
// }
private String mergeText(ElementDefinition ed, ProfileComparison outcome, String path, String name, String left, String right) {
    if (left == null && right == null)
        return null;
    if (left == null)
        return right;
    if (right == null)
        return left;
    if (left.equalsIgnoreCase(right))
        return left;
    if (path != null) {
        outcome.messages.add(new ValidationMessage(Source.ProfileComparer, IssueType.INFORMATIONAL, path, "Elements differ in definition for " + name + ":\r\n  \"" + left + "\"\r\n  \"" + right + "\"", "Elements differ in definition for " + name + ":<br/>\"" + Utilities.escapeXml(left) + "\"<br/>\"" + Utilities.escapeXml(right) + "\"", IssueSeverity.INFORMATION));
        status(ed, ProfileUtilities.STATUS_HINT);
    }
    return "left: " + left + "; right: " + right;
}
Also used : ValidationMessage(org.hl7.fhir.utilities.validation.ValidationMessage)

Example 10 with ProfileComparison

use of org.hl7.fhir.dstu2.utils.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.dstu2.model.Reference) ElementDefinitionBindingComponent(org.hl7.fhir.dstu2.model.ElementDefinition.ElementDefinitionBindingComponent) ValueSet(org.hl7.fhir.dstu2.model.ValueSet)

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