Search in sources :

Example 1 with ValueSetComparison

use of org.hl7.fhir.r4b.comparison.ValueSetComparer.ValueSetComparison in project org.hl7.fhir.core by hapifhir.

the class ProfileComparer method compareBindings.

private boolean compareBindings(ProfileComparison comp, StructuralMatch<ElementDefinitionNode> res, ElementDefinition subset, ElementDefinition superset, String path, ElementDefinition lDef, ElementDefinition rDef) throws FHIRFormatError, DefinitionException, IOException {
    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)) {
            vm(IssueSeverity.INFORMATION, "Example/preferred bindings differ at " + path + " using binding from " + comp.getRight().getName(), path, comp.getMessages(), res.getMessages());
            subset.setBinding(right);
            superset.setBinding(unionBindings(comp, res, path, left, right));
        } else {
            if ((right.getStrength() != BindingStrength.EXAMPLE || left.getStrength() != BindingStrength.EXAMPLE) && !Base.compareDeep(left.getValueSet(), right.getValueSet(), false)) {
                vm(IssueSeverity.INFORMATION, "Example/preferred bindings differ at " + path + " using binding from " + comp.getLeft().getName(), path, comp.getMessages(), res.getMessages());
            }
            subset.setBinding(left);
            superset.setBinding(unionBindings(comp, res, path, left, right));
        }
        return true;
    }
    // if either of them are extensible/required, then it wins
    if (isPreferredOrExample(left)) {
        subset.setBinding(right);
        superset.setBinding(unionBindings(comp, res, path, left, right));
        return true;
    }
    if (isPreferredOrExample(right)) {
        subset.setBinding(left);
        superset.setBinding(unionBindings(comp, res, 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(comp, res, path, "description", left.getDescription(), right.getDescription(), false));
    superBinding.setDescription(mergeText(comp, res, path, "description", left.getDescription(), right.getDescription(), false));
    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()) {
        vm(IssueSeverity.ERROR, "No left Value set at " + path, path, comp.getMessages(), res.getMessages());
        return true;
    } else if (!right.hasValueSet()) {
        vm(IssueSeverity.ERROR, "No right Value set at " + path, path, comp.getMessages(), res.getMessages());
        return true;
    } else {
        // ok, now we compare the value sets. This may be unresolvable.
        ValueSet lvs = resolveVS(comp.getLeft(), left.getValueSet(), session.getContextLeft());
        ValueSet rvs = resolveVS(comp.getRight(), right.getValueSet(), session.getContextRight());
        if (lvs == null) {
            vm(IssueSeverity.ERROR, "Unable to resolve left value set " + left.getValueSet().toString() + " at " + path, path, comp.getMessages(), res.getMessages());
            return true;
        } else if (rvs == null) {
            vm(IssueSeverity.ERROR, "Unable to resolve right value set " + right.getValueSet().toString() + " at " + path, path, comp.getMessages(), res.getMessages());
            return true;
        } else if (sameValueSets(lvs, rvs)) {
            subBinding.setValueSet(lvs.getUrl());
            superBinding.setValueSet(lvs.getUrl());
        } else {
            ValueSetComparison compP = (ValueSetComparison) session.compare(lvs, rvs);
            if (compP != null) {
                subBinding.setValueSet(compP.getIntersection().getUrl());
                superBinding.setValueSet(compP.getUnion().getUrl());
            }
        }
    }
    return false;
}
Also used : ValueSetComparison(org.hl7.fhir.r4b.comparison.ValueSetComparer.ValueSetComparison) ElementDefinitionBindingComponent(org.hl7.fhir.r4b.model.ElementDefinition.ElementDefinitionBindingComponent) ValueSet(org.hl7.fhir.r4b.model.ValueSet)

Example 2 with ValueSetComparison

use of org.hl7.fhir.r4b.comparison.ValueSetComparer.ValueSetComparison in project org.hl7.fhir.core by hapifhir.

the class ComparisonSession method compare.

public ResourceComparison compare(CanonicalResource left, CanonicalResource right) throws DefinitionException, FHIRFormatError, IOException {
    if (left != null && right != null) {
        String key = key(left.getUrl(), left.getVersion(), right.getUrl(), right.getVersion());
        if (compares.containsKey(key)) {
            // this can happen when profiles refer to each other
            return compares.get(key);
        }
        compares.put(key, null);
        try {
            if (left instanceof CodeSystem && right instanceof CodeSystem) {
                CodeSystemComparer cs = new CodeSystemComparer(this);
                CodeSystemComparison csc = cs.compare((CodeSystem) left, (CodeSystem) right);
                compares.put(key, csc);
                return csc;
            } else if (left instanceof ValueSet && right instanceof ValueSet) {
                ValueSetComparer cs = new ValueSetComparer(this);
                ValueSetComparison csc = cs.compare((ValueSet) left, (ValueSet) right);
                compares.put(key, csc);
                return csc;
            } else if (left instanceof StructureDefinition && right instanceof StructureDefinition) {
                ProfileComparer cs = new ProfileComparer(this, new ProfileUtilities(contextLeft, null, pkp), new ProfileUtilities(contextRight, null, pkp));
                ProfileComparison csc = cs.compare((StructureDefinition) left, (StructureDefinition) right);
                compares.put(key, csc);
                return csc;
            } else {
                throw new FHIRException("Unable to compare resources of type " + left.fhirType() + " and " + right.fhirType());
            }
        } catch (Throwable e) {
            ResourceComparer.PlaceHolderComparison csc = new ResourceComparer.PlaceHolderComparison(left, right, e);
            compares.put(key, csc);
            return csc;
        }
    } else if (left != null) {
        String key = key(left.getUrl(), left.getVersion(), left.getUrl(), left.getVersion());
        if (compares.containsKey(key)) {
            return compares.get(key);
        }
        ResourceComparer.PlaceHolderComparison csc = new ResourceComparer.PlaceHolderComparison(left, right);
        compares.put(key, csc);
        return csc;
    } else {
        String key = key(right.getUrl(), right.getVersion(), right.getUrl(), right.getVersion());
        if (compares.containsKey(key)) {
            return compares.get(key);
        }
        ResourceComparer.PlaceHolderComparison csc = new ResourceComparer.PlaceHolderComparison(left, right);
        compares.put(key, csc);
        return csc;
    }
}
Also used : ProfileComparison(org.hl7.fhir.r4b.comparison.ProfileComparer.ProfileComparison) ValueSetComparison(org.hl7.fhir.r4b.comparison.ValueSetComparer.ValueSetComparison) CodeSystem(org.hl7.fhir.r4b.model.CodeSystem) FHIRException(org.hl7.fhir.exceptions.FHIRException) StructureDefinition(org.hl7.fhir.r4b.model.StructureDefinition) CodeSystemComparison(org.hl7.fhir.r4b.comparison.CodeSystemComparer.CodeSystemComparison) ProfileUtilities(org.hl7.fhir.r4b.conformance.ProfileUtilities) ValueSet(org.hl7.fhir.r4b.model.ValueSet)

Example 3 with ValueSetComparison

use of org.hl7.fhir.r4b.comparison.ValueSetComparer.ValueSetComparison in project org.hl7.fhir.core by hapifhir.

the class ValueSetComparer method compareExpansions.

private void compareExpansions(ValueSet left, ValueSet right, ValueSetComparison res) {
    ValueSet expL = left.hasExpansion() ? left : expand(left, res, "left", session.getContextLeft());
    ValueSet expR = right.hasExpansion() ? right : expand(right, res, "right", session.getContextRight());
    if (expL != null && expR != null) {
        // ignore the parameters for now
        compareConcepts(expL.getExpansion().getContains(), expR.getExpansion().getContains(), res.forceExpansion(), res.getUnion().getExpansion().getContains(), res.getIntersection().getExpansion().getContains(), "ValueSet.expansion.contains", res);
    }
}
Also used : ValueSet(org.hl7.fhir.r4b.model.ValueSet)

Example 4 with ValueSetComparison

use of org.hl7.fhir.r4b.comparison.ValueSetComparer.ValueSetComparison in project org.hl7.fhir.core by hapifhir.

the class ValueSetComparer method renderCompose.

public XhtmlNode renderCompose(ValueSetComparison csc, String id, String prefix) throws FHIRException, IOException {
    HierarchicalTableGenerator gen = new HierarchicalTableGenerator(Utilities.path("[tmp]", "comparison"), false);
    TableModel model = gen.new TableModel(id, true);
    model.setAlternating(true);
    model.getTitles().add(gen.new Title(null, null, "Item", "The type of item being compared", null, 100));
    model.getTitles().add(gen.new Title(null, null, "Property", "The system for the concept", null, 100, 2));
    model.getTitles().add(gen.new Title(null, null, "Value", "The display for the concept", null, 200, 2));
    model.getTitles().add(gen.new Title(null, null, "Comments", "Additional information about the comparison", null, 200));
    for (StructuralMatch<Element> t : csc.getIncludes().getChildren()) {
        addComposeRow(gen, model.getRows(), t, "include");
    }
    for (StructuralMatch<Element> t : csc.getExcludes().getChildren()) {
        addComposeRow(gen, model.getRows(), t, "exclude");
    }
    return gen.generate(model, prefix, 0, null);
}
Also used : Element(org.hl7.fhir.r4b.model.Element) HierarchicalTableGenerator(org.hl7.fhir.utilities.xhtml.HierarchicalTableGenerator) TableModel(org.hl7.fhir.utilities.xhtml.HierarchicalTableGenerator.TableModel)

Example 5 with ValueSetComparison

use of org.hl7.fhir.r4b.comparison.ValueSetComparer.ValueSetComparison in project org.hl7.fhir.core by hapifhir.

the class ValueSetComparer method compareCompose.

private void compareCompose(ValueSetComposeComponent left, ValueSetComposeComponent right, ValueSetComparison res, ValueSetComposeComponent union, ValueSetComposeComponent intersection) {
    // first, the includes
    List<ConceptSetComponent> matchR = new ArrayList<>();
    for (ConceptSetComponent l : left.getInclude()) {
        ConceptSetComponent r = findInList(right.getInclude(), l, left.getInclude());
        if (r == null) {
            union.getInclude().add(l);
            res.getIncludes().getChildren().add(new StructuralMatch<Element>(l, vmI(IssueSeverity.INFORMATION, "Removed Include", "ValueSet.compose.include")));
        } else {
            matchR.add(r);
            ConceptSetComponent csM = new ConceptSetComponent();
            ConceptSetComponent csI = new ConceptSetComponent();
            union.getInclude().add(csM);
            intersection.getInclude().add(csI);
            StructuralMatch<Element> sm = new StructuralMatch<Element>(l, r);
            res.getIncludes().getChildren().add(sm);
            compareDefinitions(l, r, sm, csM, csI);
        }
    }
    for (ConceptSetComponent r : right.getInclude()) {
        if (!matchR.contains(r)) {
            union.getInclude().add(r);
            res.getIncludes().getChildren().add(new StructuralMatch<Element>(vmI(IssueSeverity.INFORMATION, "Added Include", "ValueSet.compose.include"), r));
        }
    }
    // now. the excludes
    matchR.clear();
    for (ConceptSetComponent l : left.getExclude()) {
        ConceptSetComponent r = findInList(right.getExclude(), l, left.getExclude());
        if (r == null) {
            union.getExclude().add(l);
            res.getExcludes().getChildren().add(new StructuralMatch<Element>(l, vmI(IssueSeverity.INFORMATION, "Removed Exclude", "ValueSet.compose.exclude")));
        } else {
            matchR.add(r);
            ConceptSetComponent csM = new ConceptSetComponent();
            ConceptSetComponent csI = new ConceptSetComponent();
            union.getExclude().add(csM);
            intersection.getExclude().add(csI);
            StructuralMatch<Element> sm = new StructuralMatch<Element>(l, r);
            res.getExcludes().getChildren().add(sm);
            compareDefinitions(l, r, sm, csM, csI);
        }
    }
    for (ConceptSetComponent r : right.getExclude()) {
        if (!matchR.contains(r)) {
            union.getExclude().add(r);
            res.getExcludes().getChildren().add(new StructuralMatch<Element>(vmI(IssueSeverity.INFORMATION, "Added Exclude", "ValueSet.compose.exclude"), r));
        }
    }
}
Also used : ConceptSetComponent(org.hl7.fhir.r4b.model.ValueSet.ConceptSetComponent) Element(org.hl7.fhir.r4b.model.Element) ArrayList(java.util.ArrayList)

Aggregations

ValueSet (org.hl7.fhir.r5.model.ValueSet)6 ValueSet (org.hl7.fhir.r4b.model.ValueSet)5 ArrayList (java.util.ArrayList)4 ValueSetComparison (org.hl7.fhir.r5.comparison.ValueSetComparer.ValueSetComparison)4 HierarchicalTableGenerator (org.hl7.fhir.utilities.xhtml.HierarchicalTableGenerator)4 TableModel (org.hl7.fhir.utilities.xhtml.HierarchicalTableGenerator.TableModel)4 FileOutputStream (java.io.FileOutputStream)3 FHIRException (org.hl7.fhir.exceptions.FHIRException)3 ValueSetComparison (org.hl7.fhir.r4b.comparison.ValueSetComparer.ValueSetComparison)3 XhtmlComposer (org.hl7.fhir.utilities.xhtml.XhtmlComposer)3 Date (java.util.Date)2 HashMap (java.util.HashMap)2 DefinitionException (org.hl7.fhir.exceptions.DefinitionException)2 Element (org.hl7.fhir.r4b.model.Element)2 ElementDefinitionBindingComponent (org.hl7.fhir.r4b.model.ElementDefinition.ElementDefinitionBindingComponent)2 ValueSetExpansionContainsComponent (org.hl7.fhir.r4b.model.ValueSet.ValueSetExpansionContainsComponent)2 CodeSystemComparison (org.hl7.fhir.r5.comparison.CodeSystemComparer.CodeSystemComparison)2 ProfileComparison (org.hl7.fhir.r5.comparison.ProfileComparer.ProfileComparison)2 ProfileUtilities (org.hl7.fhir.r5.conformance.ProfileUtilities)2 CodeSystem (org.hl7.fhir.r5.model.CodeSystem)2