Search in sources :

Example 11 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.r5.comparison.ValueSetComparer.ValueSetComparison) ElementDefinitionBindingComponent(org.hl7.fhir.r5.model.ElementDefinition.ElementDefinitionBindingComponent) ValueSet(org.hl7.fhir.r5.model.ValueSet)

Example 12 with ValueSetComparison

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

the class ValueSetComparer method compare.

public ValueSetComparison compare(ValueSet left, ValueSet right) {
    if (left == null)
        throw new DefinitionException("No ValueSet provided (left)");
    if (right == null)
        throw new DefinitionException("No ValueSet provided (right)");
    ValueSetComparison res = new ValueSetComparison(left, right);
    session.identify(res);
    ValueSet vs = new ValueSet();
    res.setUnion(vs);
    session.identify(vs);
    vs.setName("Union" + left.getName() + "And" + right.getName());
    vs.setTitle("Union of " + left.getTitle() + " And " + right.getTitle());
    vs.setStatus(left.getStatus());
    vs.setDate(new Date());
    ValueSet vs1 = new ValueSet();
    res.setIntersection(vs1);
    session.identify(vs1);
    vs1.setName("Intersection" + left.getName() + "And" + right.getName());
    vs1.setTitle("Intersection of " + left.getTitle() + " And " + right.getTitle());
    vs1.setStatus(left.getStatus());
    vs1.setDate(new Date());
    compareMetadata(left, right, res.getMetadata(), res);
    comparePrimitives("immutable", left.getImmutableElement(), right.getImmutableElement(), res.getMetadata(), IssueSeverity.WARNING, res);
    if (left.hasCompose() || right.hasCompose()) {
        comparePrimitives("compose.lockedDate", left.getCompose().getLockedDateElement(), right.getCompose().getLockedDateElement(), res.getMetadata(), IssueSeverity.WARNING, res);
        comparePrimitives("compose.inactive", left.getCompose().getInactiveElement(), right.getCompose().getInactiveElement(), res.getMetadata(), IssueSeverity.WARNING, res);
    }
    compareCompose(left.getCompose(), right.getCompose(), res, res.getUnion().getCompose(), res.getIntersection().getCompose());
    compareExpansions(left, right, res);
    return res;
}
Also used : DefinitionException(org.hl7.fhir.exceptions.DefinitionException) ValueSet(org.hl7.fhir.r4b.model.ValueSet) Date(java.util.Date)

Example 13 with ValueSetComparison

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

the class ValueSetComparer method compareConcepts.

private void compareConcepts(List<ValueSetExpansionContainsComponent> left, List<ValueSetExpansionContainsComponent> right, StructuralMatch<ValueSetExpansionContainsComponent> combined, List<ValueSetExpansionContainsComponent> union, List<ValueSetExpansionContainsComponent> intersection, String path, ValueSetComparison res) {
    List<ValueSetExpansionContainsComponent> matchR = new ArrayList<>();
    for (ValueSetExpansionContainsComponent l : left) {
        ValueSetExpansionContainsComponent r = findInList(right, l);
        if (r == null) {
            union.add(l);
            combined.getChildren().add(new StructuralMatch<ValueSetExpansionContainsComponent>(l, vmI(IssueSeverity.INFORMATION, "Removed from expansion", path)));
        } else {
            matchR.add(r);
            ValueSetExpansionContainsComponent ccU = merge(l, r);
            ValueSetExpansionContainsComponent ccI = intersect(l, r);
            union.add(ccU);
            intersection.add(ccI);
            StructuralMatch<ValueSetExpansionContainsComponent> sm = new StructuralMatch<ValueSetExpansionContainsComponent>(l, r);
            compareItem(sm.getMessages(), path, l, r, res);
            combined.getChildren().add(sm);
            compareConcepts(l.getContains(), r.getContains(), sm, ccU.getContains(), ccI.getContains(), path + ".where(code = '" + l.getCode() + "').contains", res);
        }
    }
    for (ValueSetExpansionContainsComponent r : right) {
        if (!matchR.contains(r)) {
            union.add(r);
            combined.getChildren().add(new StructuralMatch<ValueSetExpansionContainsComponent>(vmI(IssueSeverity.INFORMATION, "Added to expansion", path), r));
        }
    }
}
Also used : ValueSetExpansionContainsComponent(org.hl7.fhir.r4b.model.ValueSet.ValueSetExpansionContainsComponent) ArrayList(java.util.ArrayList)

Example 14 with ValueSetComparison

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

the class ProfileComparer method unionBindings.

private ElementDefinitionBindingComponent unionBindings(ProfileComparison comp, StructuralMatch<ElementDefinitionNode> res, String path, ElementDefinitionBindingComponent left, ElementDefinitionBindingComponent right) throws FHIRFormatError, DefinitionException, IOException {
    ElementDefinitionBindingComponent union = new ElementDefinitionBindingComponent();
    if (left.getStrength().compareTo(right.getStrength()) < 0)
        union.setStrength(left.getStrength());
    else
        union.setStrength(right.getStrength());
    union.setDescription(mergeText(comp, res, path, "binding.description", left.getDescription(), right.getDescription(), false));
    if (Base.compareDeep(left.getValueSet(), right.getValueSet(), false))
        union.setValueSet(left.getValueSet());
    else {
        ValueSet lvs = resolveVS(comp.getLeft(), left.getValueSet(), session.getContextLeft());
        ValueSet rvs = resolveVS(comp.getRight(), right.getValueSet(), session.getContextRight());
        if (lvs != null && rvs != null) {
            ValueSetComparison compP = (ValueSetComparison) session.compare(lvs, rvs);
            if (compP != null) {
                union.setValueSet(compP.getUnion().getUrl());
            }
        } else if (lvs != null) {
            union.setValueSet(lvs.getUrl());
        } else if (rvs != null) {
            union.setValueSet(rvs.getUrl());
        }
    }
    return union;
}
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 15 with ValueSetComparison

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

the class ComparisonTests method test.

@ParameterizedTest(name = "{index}: id {0}")
@MethodSource("data")
public void test(String name, JsonObject content) throws Exception {
    this.content = content;
    if (content.has("use-test") && !content.get("use-test").getAsBoolean())
        return;
    if (context == null) {
        System.out.println("---- Load R5 ----------------------------------------------------------------");
        context = TestingUtilities.getSharedWorkerContext();
        FilesystemPackageCacheManager pcm = new FilesystemPackageCacheManager(true, ToolsVersion.TOOLS_VERSION);
        NpmPackage npm = pcm.loadPackage("hl7.fhir.us.core#3.1.0");
        BaseWorkerContext bc = (BaseWorkerContext) context;
        boolean dupl = bc.isAllowLoadingDuplicates();
        bc.setAllowLoadingDuplicates(true);
        context.loadFromPackage(npm, new R4ToR5Loader(new String[] { "CapabilityStatement", "StructureDefinition", "ValueSet", "CodeSystem", "SearchParameter", "OperationDefinition", "Questionnaire", "ConceptMap", "StructureMap", "NamingSystem" }, new NullLoaderKnowledgeProviderR5(), context.getVersion()));
        bc.setAllowLoadingDuplicates(dupl);
    }
    if (!new File(Utilities.path("[tmp]", "comparison")).exists()) {
        System.out.println("---- Set up Output ----------------------------------------------------------");
        Utilities.createDirectory(Utilities.path("[tmp]", "comparison"));
        FilesystemPackageCacheManager pcm = new FilesystemPackageCacheManager(true, ToolsVersion.TOOLS_VERSION);
        NpmPackage npm = pcm.loadPackage(CommonPackages.ID_PUBPACK, CommonPackages.VER_PUBPACK);
        for (String f : npm.list("other")) {
            TextFile.streamToFile(npm.load("other", f), Utilities.path("[tmp]", "comparison", f));
        }
    }
    System.out.println("---- " + name + " ----------------------------------------------------------------");
    CanonicalResource left = load("left");
    CanonicalResource right = load("right");
    ComparisonSession session = new ComparisonSession(context, context, "Comparison Tests", null);
    if (left instanceof CodeSystem && right instanceof CodeSystem) {
        CodeSystemComparer cs = new CodeSystemComparer(session);
        CodeSystemComparison csc = cs.compare((CodeSystem) left, (CodeSystem) right);
        Assertions.assertTrue(csc.getUnion().getConcept().size() > csc.getIntersection().getConcept().size());
        new org.hl7.fhir.r5.formats.JsonParser().setOutputStyle(OutputStyle.PRETTY).compose(new FileOutputStream(Utilities.path("[tmp]", "comparison", name + "-union.json")), csc.getUnion());
        new org.hl7.fhir.r5.formats.JsonParser().setOutputStyle(OutputStyle.PRETTY).compose(new FileOutputStream(Utilities.path("[tmp]", "comparison", name + "-intersection.json")), csc.getIntersection());
        String xmle = new XhtmlComposer(true).compose(cs.renderErrors(csc));
        String xml1 = new XhtmlComposer(true).compose(cs.renderMetadata(csc, "", ""));
        String xml2 = new XhtmlComposer(true).compose(cs.renderConcepts(csc, "", ""));
        TextFile.stringToFile(HEADER + hd("Messages") + xmle + BREAK + hd("Metadata") + xml1 + BREAK + hd("Concepts") + xml2 + FOOTER, Utilities.path("[tmp]", "comparison", name + ".html"));
        checkOutcomes(csc.getMessages(), content);
    } else if (left instanceof ValueSet && right instanceof ValueSet) {
        ValueSetComparer cs = new ValueSetComparer(session);
        ValueSetComparison csc = cs.compare((ValueSet) left, (ValueSet) right);
        new org.hl7.fhir.r5.formats.JsonParser().setOutputStyle(OutputStyle.PRETTY).compose(new FileOutputStream(Utilities.path("[tmp]", "comparison", name + "-union.json")), csc.getUnion());
        new org.hl7.fhir.r5.formats.JsonParser().setOutputStyle(OutputStyle.PRETTY).compose(new FileOutputStream(Utilities.path("[tmp]", "comparison", name + "-intersection.json")), csc.getIntersection());
        String xmle = new XhtmlComposer(true).compose(cs.renderErrors(csc));
        String xml1 = new XhtmlComposer(true).compose(cs.renderMetadata(csc, "", ""));
        String xml2 = new XhtmlComposer(true).compose(cs.renderCompose(csc, "", ""));
        String xml3 = new XhtmlComposer(true).compose(cs.renderExpansion(csc, "", ""));
        TextFile.stringToFile(HEADER + hd("Messages") + xmle + BREAK + hd("Metadata") + xml1 + BREAK + hd("Definition") + xml2 + BREAK + hd("Expansion") + xml3 + FOOTER, Utilities.path("[tmp]", "comparison", name + ".html"));
        checkOutcomes(csc.getMessages(), content);
    } else if (left instanceof StructureDefinition && right instanceof StructureDefinition) {
        ProfileUtilities utils = new ProfileUtilities(context, null, null);
        genSnapshot(utils, (StructureDefinition) left);
        genSnapshot(utils, (StructureDefinition) right);
        ProfileComparer pc = new ProfileComparer(session, utils, utils);
        ProfileComparison csc = pc.compare((StructureDefinition) left, (StructureDefinition) right);
        new org.hl7.fhir.r5.formats.JsonParser().setOutputStyle(OutputStyle.PRETTY).compose(new FileOutputStream(Utilities.path("[tmp]", "comparison", name + "-union.json")), csc.getUnion());
        new org.hl7.fhir.r5.formats.JsonParser().setOutputStyle(OutputStyle.PRETTY).compose(new FileOutputStream(Utilities.path("[tmp]", "comparison", name + "-intersection.json")), csc.getIntersection());
        String xmle = new XhtmlComposer(true).compose(pc.renderErrors(csc));
        String xml1 = new XhtmlComposer(true).compose(pc.renderMetadata(csc, "", ""));
        String xml2 = new XhtmlComposer(true).compose(pc.renderStructure(csc, "", "", "http://hl7.org/fhir"));
        // String xml3 = new XhtmlComposer(true).compose(cs.renderExpansion(csc, "", ""));
        TextFile.stringToFile(HEADER + hd("Messages") + xmle + BREAK + hd("Metadata") + xml1 + BREAK + hd("Structure") + xml2 + FOOTER, Utilities.path("[tmp]", "comparison", name + ".html"));
        checkOutcomes(csc.getMessages(), content);
    } else if (left instanceof CapabilityStatement && right instanceof CapabilityStatement) {
        CapabilityStatementComparer pc = new CapabilityStatementComparer(session);
        CapabilityStatementComparison csc = pc.compare((CapabilityStatement) left, (CapabilityStatement) right);
        new org.hl7.fhir.r5.formats.JsonParser().setOutputStyle(OutputStyle.PRETTY).compose(new FileOutputStream(Utilities.path("[tmp]", "comparison", name + "-union.json")), csc.getUnion());
        new org.hl7.fhir.r5.formats.JsonParser().setOutputStyle(OutputStyle.PRETTY).compose(new FileOutputStream(Utilities.path("[tmp]", "comparison", name + "-intersection.json")), csc.getIntersection());
        String xmle = new XhtmlComposer(true).compose(pc.renderErrors(csc));
        String xml1 = new XhtmlComposer(true).compose(pc.renderMetadata(csc, "", ""));
        String xml2 = new XhtmlComposer(true).compose(pc.renderStatements(csc, "", ""));
        // String xml3 = new XhtmlComposer(true).compose(cs.renderExpansion(csc, "", ""));
        TextFile.stringToFile(HEADER + hd("Messages") + xmle + BREAK + hd("Metadata") + xml1 + BREAK + hd("Structure") + xml2 + FOOTER, Utilities.path("[tmp]", "comparison", name + ".html"));
        checkOutcomes(csc.getMessages(), content);
    } else {
        throw new FHIRException("Can't compare " + left.fhirType() + " to " + right.fhirType());
    }
}
Also used : ProfileComparison(org.hl7.fhir.r5.comparison.ProfileComparer.ProfileComparison) ComparisonSession(org.hl7.fhir.r5.comparison.ComparisonSession) ProfileComparer(org.hl7.fhir.r5.comparison.ProfileComparer) BaseWorkerContext(org.hl7.fhir.r5.context.BaseWorkerContext) StructureDefinition(org.hl7.fhir.r5.model.StructureDefinition) CapabilityStatementComparison(org.hl7.fhir.r5.comparison.CapabilityStatementComparer.CapabilityStatementComparison) XhtmlComposer(org.hl7.fhir.utilities.xhtml.XhtmlComposer) CapabilityStatement(org.hl7.fhir.r5.model.CapabilityStatement) CapabilityStatementComparer(org.hl7.fhir.r5.comparison.CapabilityStatementComparer) ValueSet(org.hl7.fhir.r5.model.ValueSet) JsonParser(org.hl7.fhir.r5.formats.JsonParser) FilesystemPackageCacheManager(org.hl7.fhir.utilities.npm.FilesystemPackageCacheManager) ValueSetComparison(org.hl7.fhir.r5.comparison.ValueSetComparer.ValueSetComparison) CodeSystem(org.hl7.fhir.r5.model.CodeSystem) JsonParser(org.hl7.fhir.r5.formats.JsonParser) FHIRException(org.hl7.fhir.exceptions.FHIRException) CodeSystemComparer(org.hl7.fhir.r5.comparison.CodeSystemComparer) NpmPackage(org.hl7.fhir.utilities.npm.NpmPackage) CodeSystemComparison(org.hl7.fhir.r5.comparison.CodeSystemComparer.CodeSystemComparison) ProfileUtilities(org.hl7.fhir.r5.conformance.ProfileUtilities) R4ToR5Loader(org.hl7.fhir.convertors.loaders.loaderR5.R4ToR5Loader) FileOutputStream(java.io.FileOutputStream) ValueSetComparer(org.hl7.fhir.r5.comparison.ValueSetComparer) File(java.io.File) TextFile(org.hl7.fhir.utilities.TextFile) CanonicalResource(org.hl7.fhir.r5.model.CanonicalResource) NullLoaderKnowledgeProviderR5(org.hl7.fhir.convertors.loaders.loaderR5.NullLoaderKnowledgeProviderR5) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

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