Search in sources :

Example 61 with ProfileComparison

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

Example 62 with ProfileComparison

use of org.hl7.fhir.r5.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
 *
 * @
 */
public ProfileComparison compareProfiles(StructureDefinition left, StructureDefinition right) throws DefinitionException, IOException {
    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(ConformanceResourceStatus.DRAFT);
            outcome.subset.setKind(outcome.left.getKind());
            outcome.subset.setConstrainedType(outcome.left.getConstrainedType());
            outcome.subset.setBase("http://hl7.org/fhir/StructureDefinition/" + outcome.subset.getConstrainedType());
            outcome.subset.setAbstract(false);
            outcome.superset.setName("union of " + outcome.leftName() + " and " + outcome.rightName());
            outcome.superset.setStatus(ConformanceResourceStatus.DRAFT);
            outcome.superset.setKind(outcome.left.getKind());
            outcome.superset.setConstrainedType(outcome.left.getConstrainedType());
            outcome.superset.setBase("http://hl7.org/fhir/StructureDefinition/" + outcome.subset.getConstrainedType());
            outcome.superset.setAbstract(false);
        } else {
            outcome.subset = null;
            outcome.superset = null;
        }
    }
    return outcome;
}
Also used : StructureDefinition(org.hl7.fhir.dstu2.model.StructureDefinition) DefinitionException(org.hl7.fhir.exceptions.DefinitionException)

Example 63 with ProfileComparison

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

the class ProfileComparer method unite.

private ValueSet unite(ElementDefinition ed, ProfileComparison outcome, String path, ValueSet lvs, ValueSet rvs) {
    ValueSet vs = new ValueSet();
    if (lvs.hasCodeSystem())
        vs.getCompose().addInclude().setSystem(lvs.getCodeSystem().getSystem());
    if (rvs.hasCodeSystem())
        vs.getCompose().addInclude().setSystem(rvs.getCodeSystem().getSystem());
    if (lvs.hasCompose()) {
        for (UriType imp : lvs.getCompose().getImport()) vs.getCompose().getImport().add(imp);
        for (ConceptSetComponent inc : lvs.getCompose().getInclude()) vs.getCompose().getInclude().add(inc);
        if (lvs.getCompose().hasExclude()) {
            outcome.messages.add(new ValidationMessage(Source.ProfileComparer, IssueType.STRUCTURE, path, "The value sets " + lvs.getUrl() + " has exclude statements, and no union involving it can be correctly determined", IssueSeverity.ERROR));
            status(ed, ProfileUtilities.STATUS_ERROR);
        }
    }
    if (rvs.hasCompose()) {
        for (UriType imp : rvs.getCompose().getImport()) if (!vs.getCompose().hasImport(imp.getValue()))
            vs.getCompose().getImport().add(imp);
        for (ConceptSetComponent inc : rvs.getCompose().getInclude()) if (!mergeIntoExisting(vs.getCompose().getInclude(), inc))
            vs.getCompose().getInclude().add(inc);
        if (rvs.getCompose().hasExclude()) {
            outcome.messages.add(new ValidationMessage(Source.ProfileComparer, IssueType.STRUCTURE, path, "The value sets " + lvs.getUrl() + " has exclude statements, and no union involving it can be correctly determined", IssueSeverity.ERROR));
            status(ed, ProfileUtilities.STATUS_ERROR);
        }
    }
    return vs;
}
Also used : ConceptSetComponent(org.hl7.fhir.dstu2.model.ValueSet.ConceptSetComponent) ValidationMessage(org.hl7.fhir.utilities.validation.ValidationMessage) ValueSet(org.hl7.fhir.dstu2.model.ValueSet) UriType(org.hl7.fhir.dstu2.model.UriType)

Example 64 with ProfileComparison

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

Example 65 with ProfileComparison

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

the class ProfileUtilitiesTests method execute.

public void execute(String[] args) throws FileNotFoundException, IOException, FHIRException {
    System.out.println("loading context");
    context = SimpleWorkerContext.fromPack(Utilities.path(root, "validation.zip"));
    comp = new ProfileComparer(context);
    compare("patient-daf-dafpatient.profile.xml", "patient-qicore-qicore-patient.profile.xml");
    compare("encounter-daf-dafencounter.profile.xml", "encounter-qicore-qicore-encounter.profile.xml");
    compare("substance-daf-dafsubstance.profile.xml", "substance-qicore-qicore-substance.profile.xml");
    compare("medication-daf-dafmedication.profile.xml", "medication-qicore-qicore-medication.profile.xml");
    compare("procedure-daf-dafprocedure.profile.xml", "procedure-qicore-qicore-procedure.profile.xml");
    compare("familymemberhistory-daf-daffamilymemberhistory.profile.xml", "familymemberhistory-qicore-qicore-familymemberhistory.profile.xml");
    compare("immunization-daf-dafimmunization.profile.xml", "immunization-qicore-qicore-immunization.profile.xml");
    compare("condition-daf-dafcondition.profile.xml", "condition-qicore-qicore-condition.profile.xml");
    compare("allergyintolerance-daf-dafallergyintolerance.profile.xml", "allergyintolerance-qicore-qicore-allergyintolerance.profile.xml");
    compare("medicationadministration-daf-dafmedicationadministration.profile.xml", "medicationadministration-qicore-qicore-medicationadministration.profile.xml");
    compare("medicationdispense-daf-dafmedicationdispense.profile.xml", "medicationdispense-qicore-qicore-medicationdispense.profile.xml");
    compare("medicationprescription-daf-dafmedicationprescription.profile.xml", "medicationprescription-qicore-qicore-medicationprescription.profile.xml");
    compare("medicationstatement-daf-dafmedicationstatement.profile.xml", "medicationstatement-qicore-qicore-medicationstatement.profile.xml");
    compare("observation-daf-smokingstatus-dafsmokingstatus.profile.xml", "observation-qicore-qicore-observation.profile.xml");
    compare("observation-daf-vitalsigns-dafvitalsigns.profile.xml", "observation-qicore-qicore-observation.profile.xml");
    // compare("observation-daf-results-dafresultobs.profile.xml", "observation-qicore-qicore-observation.profile.xml");
    // compare("diagnosticorder-daf-dafdiagnosticorder.profile.xml", "diagnosticorder-qicore-qicore-diagnosticorder.profile.xml");
    // compare("diagnosticreport-daf-dafdiagnosticreport.profile.xml", "diagnosticreport-qicore-qicore-diagnosticreport.profile.xml");
    System.out.println("processing output");
    for (ProfileComparison outcome : comp.getComparisons()) {
        if (outcome.getSubset() != null)
            new XmlParser().setOutputStyle(OutputStyle.PRETTY).compose(new FileOutputStream(Utilities.path("[tmp]", "intersection-" + outcome.getId() + ".xml")), outcome.getSubset());
        if (outcome.getSuperset() != null)
            new XmlParser().setOutputStyle(OutputStyle.PRETTY).compose(new FileOutputStream(Utilities.path("[tmp]", "union-" + outcome.getId() + ".xml")), outcome.getSuperset());
        System.out.println("\r\n" + outcome.getId() + ": Comparison of " + outcome.getLeft().getUrl() + " and " + outcome.getRight().getUrl());
        for (ValidationMessage vm : outcome.getMessages()) if (vm.getLevel() == IssueSeverity.INFORMATION)
            System.out.println(vm.summary());
        for (ValidationMessage vm : outcome.getMessages()) if (vm.getLevel() == IssueSeverity.WARNING)
            System.out.println(vm.summary());
        for (ValidationMessage vm : outcome.getMessages()) if (vm.getLevel() == IssueSeverity.ERROR)
            System.out.println(vm.summary());
        for (ValidationMessage vm : outcome.getMessages()) if (vm.getLevel() == IssueSeverity.FATAL)
            System.out.println(vm.summary());
        System.out.println("done. " + Integer.toString(outcome.getMessages().size()) + " messages");
        System.out.println("=================================================================");
    }
}
Also used : ProfileComparison(org.hl7.fhir.dstu2.utils.ProfileComparer.ProfileComparison) XmlParser(org.hl7.fhir.dstu2.formats.XmlParser) ValidationMessage(org.hl7.fhir.utilities.validation.ValidationMessage) ProfileComparer(org.hl7.fhir.dstu2.utils.ProfileComparer) FileOutputStream(java.io.FileOutputStream)

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