Search in sources :

Example 26 with ProfileUtilities

use of org.hl7.fhir.r5.conformance.ProfileUtilities in project org.hl7.fhir.core by hapifhir.

the class SimpleWorkerContext method seeProfile.

private void seeProfile(String url, StructureDefinition p) throws FHIRException {
    if (Utilities.noString(url))
        url = p.getUrl();
    if (!p.hasSnapshot()) {
        if (!p.hasBaseDefinition())
            throw new DefinitionException("Profile " + p.getName() + " (" + p.getUrl() + ") has no base and no snapshot");
        StructureDefinition sd = fetchResource(StructureDefinition.class, p.getBaseDefinition());
        if (sd == null)
            throw new DefinitionException("Profile " + p.getName() + " (" + p.getUrl() + ") base " + p.getBaseDefinition() + " could not be resolved");
        List<ValidationMessage> msgs = new ArrayList<ValidationMessage>();
        ProfileUtilities pu = new ProfileUtilities(this, msgs, this);
        pu.generateSnapshot(sd, p, p.getUrl(), p.getName());
        for (ValidationMessage msg : msgs) {
            if (msg.getLevel() == IssueSeverity.ERROR || msg.getLevel() == IssueSeverity.FATAL)
                throw new DefinitionException("Profile " + p.getName() + " (" + p.getUrl() + "). Error generating snapshot: " + msg.getMessage());
        }
        if (!p.hasSnapshot())
            throw new DefinitionException("Profile " + p.getName() + " (" + p.getUrl() + "). Error generating snapshot");
        pu = null;
    }
    if (structures.containsKey(p.getUrl()))
        throw new DefinitionException("Duplicate structures " + p.getUrl());
    structures.put(p.getId(), p);
    structures.put(p.getUrl(), p);
    if (!p.getUrl().equals(url))
        structures.put(url, p);
}
Also used : StructureDefinition(org.hl7.fhir.dstu2016may.model.StructureDefinition) ValidationMessage(org.hl7.fhir.utilities.validation.ValidationMessage) ArrayList(java.util.ArrayList) DefinitionException(org.hl7.fhir.exceptions.DefinitionException)

Example 27 with ProfileUtilities

use of org.hl7.fhir.r5.conformance.ProfileUtilities in project org.hl7.fhir.core by hapifhir.

the class NarrativeGenerator method generate.

public boolean generate(ResourceContext rcontext, StructureDefinition sd, java.util.Set<String> outputTracker) throws EOperationOutcome, FHIRException, IOException {
    ProfileUtilities pu = new ProfileUtilities(context, null, pkp);
    XhtmlNode x = new XhtmlNode(NodeType.Element, "div");
    x.getChildNodes().add(pu.generateTable(definitionsTarget, sd, true, destDir, false, sd.getId(), false, corePath, "", false, false, outputTracker));
    inject(sd, x, NarrativeStatus.GENERATED);
    return true;
}
Also used : ProfileUtilities(org.hl7.fhir.dstu3.conformance.ProfileUtilities) XhtmlNode(org.hl7.fhir.utilities.xhtml.XhtmlNode)

Example 28 with ProfileUtilities

use of org.hl7.fhir.r5.conformance.ProfileUtilities in project org.hl7.fhir.core by hapifhir.

the class ProfileUtilitiesTests method testCardinalityChange.

/**
 * Change one cardinality.
 *
 * @param context2
 * @
 * @throws EOperationOutcome
 */
private void testCardinalityChange() throws EOperationOutcome, Exception {
    StructureDefinition focus = new StructureDefinition();
    StructureDefinition base = context.fetchResource(StructureDefinition.class, "http://hl7.org/fhir/StructureDefinition/Patient").copy();
    focus.setUrl(Utilities.makeUuidUrn());
    focus.setBaseDefinition(base.getUrl());
    focus.setType(base.getType());
    focus.setDerivation(TypeDerivationRule.CONSTRAINT);
    ElementDefinition id = focus.getDifferential().addElement();
    id.setPath("Patient.identifier");
    id.setMin(1);
    List<ValidationMessage> messages = new ArrayList<ValidationMessage>();
    new ProfileUtilities(context, messages, null).generateSnapshot(base, focus, focus.getUrl(), "Simple Test");
    boolean ok = base.getSnapshot().getElement().size() == focus.getSnapshot().getElement().size();
    for (int i = 0; i < base.getSnapshot().getElement().size(); i++) {
        if (ok) {
            ElementDefinition b = base.getSnapshot().getElement().get(i);
            ElementDefinition f = focus.getSnapshot().getElement().get(i);
            if (!f.hasBase() || !b.getPath().equals(f.getBase().getPath()))
                ok = false;
            else {
                f.setBase(null);
                if (f.getPath().equals("Patient.identifier")) {
                    ok = f.getMin() == 1;
                    if (ok)
                        f.setMin(0);
                }
                ok = ok && Base.compareDeep(b, f, true);
            }
        }
    }
    if (!ok) {
        compareXml(base, focus);
        throw new FHIRException("Snap shot generation chenge cardinality test failed");
    } else
        System.out.println("Snap shot generation chenge cardinality test passed");
}
Also used : StructureDefinition(org.hl7.fhir.dstu3.model.StructureDefinition) ValidationMessage(org.hl7.fhir.utilities.validation.ValidationMessage) ProfileUtilities(org.hl7.fhir.dstu3.conformance.ProfileUtilities) ArrayList(java.util.ArrayList) ElementDefinition(org.hl7.fhir.dstu3.model.ElementDefinition) FHIRException(org.hl7.fhir.exceptions.FHIRException)

Example 29 with ProfileUtilities

use of org.hl7.fhir.r5.conformance.ProfileUtilities in project org.hl7.fhir.core by hapifhir.

the class ProfileUtilitiesTests method testSlicingExtension.

/**
 * we're going to slice Patient.extension and refer to extension by profile
 *
 * implicit: whether to rely on implicit extension slicing
 */
private void testSlicingExtension(boolean implicit) throws EOperationOutcome, Exception {
    StructureDefinition focus = new StructureDefinition();
    StructureDefinition base = context.fetchResource(StructureDefinition.class, "http://hl7.org/fhir/StructureDefinition/Patient").copy();
    focus.setUrl(Utilities.makeUuidUrn());
    focus.setBaseDefinition(base.getUrl());
    focus.setType(base.getType());
    focus.setDerivation(TypeDerivationRule.CONSTRAINT);
    // set the slice up
    ElementDefinition id;
    if (!implicit) {
        id = focus.getDifferential().addElement();
        id.setPath("Patient.extension");
        id.getSlicing().setOrdered(false).setRules(SlicingRules.OPEN).addDiscriminator().setPath("url").setType(DiscriminatorType.VALUE);
        id.setMax("3");
    }
    // first slice:
    id = focus.getDifferential().addElement();
    id.setPath("Patient.extension");
    id.setSliceName("name1");
    id.addType().setCode("Extension").setProfile("http://hl7.org/fhir/StructureDefinition/patient-birthTime");
    id.setMin(1);
    // second slice:
    id = focus.getDifferential().addElement();
    id.setPath("Patient.extension");
    id.setSliceName("name2");
    id.addType().setCode("Extension").setProfile("http://hl7.org/fhir/StructureDefinition/patient-mothersMaidenName");
    List<ValidationMessage> messages = new ArrayList<ValidationMessage>();
    ProfileUtilities pu = new ProfileUtilities(context, messages, null);
    pu.generateSnapshot(base, focus, focus.getUrl(), "Simple Test");
    // 2 different: extension slices
    boolean ok = base.getSnapshot().getElement().size() == focus.getSnapshot().getElement().size() - 2;
    for (int i = 0; i < base.getSnapshot().getElement().size(); i++) {
        if (ok) {
            ElementDefinition b = base.getSnapshot().getElement().get(i);
            ElementDefinition f = focus.getSnapshot().getElement().get(i <= 7 ? i : i + 2);
            if (!f.hasBase() || !b.getPath().equals(f.getBase().getPath()))
                ok = false;
            else {
                f.setBase(null);
                if (f.getPath().equals("Patient.extension")) {
                    ok = f.hasSlicing() && (implicit || f.getMax().equals("3"));
                    if (ok) {
                        f.setSlicing(null);
                        f.setMaxElement(b.getMaxElement());
                    }
                }
                if (// no compare that because the definitions get overwritten
                !f.getPath().equals("Patient.extension"))
                    ok = Base.compareDeep(b, f, true);
            }
        }
    }
    // now, check that the slices we skipped are correct:
    if (ok) {
        ElementDefinition d1 = focus.getSnapshot().getElement().get(8);
        ElementDefinition d2 = focus.getSnapshot().getElement().get(9);
        ok = d1.hasType() && d1.getType().get(0).hasProfile() && d2.hasType() && d2.getType().get(0).hasProfile() && !Base.compareDeep(d1.getType(), d2.getType(), true) && d1.getMin() == 1 && d2.getMin() == 0 && d1.getMax().equals("1") && d2.getMax().equals("1");
        if (ok) {
            d1.getType().clear();
            d2.getType().clear();
            d1.setSliceName("x");
            d2.setSliceName("x");
            d1.setMin(0);
        }
        ok = Base.compareDeep(d1, d2, true);
    // for throughness, we could check against extension too, but this is not done now.
    }
    if (!ok) {
        compareXml(base, focus);
        throw new FHIRException("Snap shot generation slicing extensions simple (" + (implicit ? "implicit" : "not implicit") + ") failed");
    } else
        System.out.println("Snap shot generation slicing extensions simple (" + (implicit ? "implicit" : "not implicit") + ") passed");
}
Also used : StructureDefinition(org.hl7.fhir.dstu3.model.StructureDefinition) ValidationMessage(org.hl7.fhir.utilities.validation.ValidationMessage) ProfileUtilities(org.hl7.fhir.dstu3.conformance.ProfileUtilities) ArrayList(java.util.ArrayList) ElementDefinition(org.hl7.fhir.dstu3.model.ElementDefinition) FHIRException(org.hl7.fhir.exceptions.FHIRException)

Example 30 with ProfileUtilities

use of org.hl7.fhir.r5.conformance.ProfileUtilities in project org.hl7.fhir.core by hapifhir.

the class ProfileUtilitiesTests method testSlicingSimple.

/**
 * we're going to slice Patient.identifier
 */
private void testSlicingSimple() throws EOperationOutcome, Exception {
    StructureDefinition focus = new StructureDefinition();
    StructureDefinition base = context.fetchResource(StructureDefinition.class, "http://hl7.org/fhir/StructureDefinition/Patient").copy();
    focus.setUrl(Utilities.makeUuidUrn());
    focus.setBaseDefinition(base.getUrl());
    focus.setType(base.getType());
    focus.setDerivation(TypeDerivationRule.CONSTRAINT);
    // set the slice up
    ElementDefinition id = focus.getDifferential().addElement();
    id.setPath("Patient.identifier");
    id.getSlicing().setOrdered(false).setRules(SlicingRules.OPEN).addDiscriminator().setPath("use").setType(DiscriminatorType.VALUE);
    // first slice:
    id = focus.getDifferential().addElement();
    id.setPath("Patient.identifier");
    id.setSliceName("name1");
    id = focus.getDifferential().addElement();
    id.setPath("Patient.identifier.use");
    id.setFixed(new CodeType("usual"));
    // second slice:
    id = focus.getDifferential().addElement();
    id.setPath("Patient.identifier");
    id.setSliceName("name2");
    id = focus.getDifferential().addElement();
    id.setPath("Patient.identifier.use");
    id.setFixed(new CodeType("official"));
    List<ValidationMessage> messages = new ArrayList<ValidationMessage>();
    new ProfileUtilities(context, messages, null).generateSnapshot(base, focus, focus.getUrl(), "Simple Test");
    // 18 different: identifier + 8 inner children * 2
    boolean ok = base.getSnapshot().getElement().size() == focus.getSnapshot().getElement().size() - 18;
    for (int i = 0; i < base.getSnapshot().getElement().size(); i++) {
        if (ok) {
            ElementDefinition b = base.getSnapshot().getElement().get(i);
            ElementDefinition f = focus.getSnapshot().getElement().get(i <= 9 ? i : i + 18);
            if (!f.hasBase() || !b.getPath().equals(f.getBase().getPath()))
                ok = false;
            else {
                f.setBase(null);
                if (f.getPath().equals("Patient.identifier")) {
                    ok = f.hasSlicing();
                    if (ok)
                        f.setSlicing(null);
                }
                ok = Base.compareDeep(b, f, true);
            }
        }
    }
    // now, check that the slices we skipped are correct:
    for (int i = 10; i <= 18; i++) {
        if (ok) {
            ElementDefinition d1 = focus.getSnapshot().getElement().get(i);
            ElementDefinition d2 = focus.getSnapshot().getElement().get(i + 9);
            if (d1.getPath().equals("Patient.identifier.use")) {
                ok = d1.hasFixed() && d2.hasFixed() && !Base.compareDeep(d1.getFixed(), d2.getFixed(), true);
                if (ok) {
                    d1.setFixed(null);
                    d2.setFixed(null);
                }
            }
            if (d1.getPath().equals("Patient.identifier")) {
                ok = d1.hasSliceName() && d2.hasSliceName() && !Base.compareDeep(d1.getSliceNameElement(), d2.getSliceNameElement(), true);
                if (ok) {
                    d1.setSliceName(null);
                    d2.setSliceName(null);
                }
            }
            ok = Base.compareDeep(d1, d2, true);
        }
    }
    if (!ok) {
        compareXml(base, focus);
        throw new FHIRException("Snap shot generation slicing failed");
    } else
        System.out.println("Snap shot generation slicing passed");
}
Also used : StructureDefinition(org.hl7.fhir.dstu3.model.StructureDefinition) ValidationMessage(org.hl7.fhir.utilities.validation.ValidationMessage) ProfileUtilities(org.hl7.fhir.dstu3.conformance.ProfileUtilities) ArrayList(java.util.ArrayList) CodeType(org.hl7.fhir.dstu3.model.CodeType) ElementDefinition(org.hl7.fhir.dstu3.model.ElementDefinition) FHIRException(org.hl7.fhir.exceptions.FHIRException)

Aggregations

ArrayList (java.util.ArrayList)74 FHIRException (org.hl7.fhir.exceptions.FHIRException)59 ValidationMessage (org.hl7.fhir.utilities.validation.ValidationMessage)52 ProfileUtilities (org.hl7.fhir.r5.conformance.ProfileUtilities)48 StructureDefinition (org.hl7.fhir.r5.model.StructureDefinition)38 ProfileUtilities (org.hl7.fhir.dstu3.conformance.ProfileUtilities)21 StructureDefinition (org.hl7.fhir.dstu3.model.StructureDefinition)20 StructureDefinition (org.hl7.fhir.dstu2.model.StructureDefinition)16 ElementDefinition (org.hl7.fhir.r5.model.ElementDefinition)16 FileOutputStream (java.io.FileOutputStream)15 ProfileUtilities (org.hl7.fhir.r4b.conformance.ProfileUtilities)14 IOException (java.io.IOException)13 ElementDefinition (org.hl7.fhir.dstu2.model.ElementDefinition)13 ProfileUtilities (org.hl7.fhir.dstu2.utils.ProfileUtilities)13 ElementDefinition (org.hl7.fhir.dstu3.model.ElementDefinition)13 DefinitionException (org.hl7.fhir.exceptions.DefinitionException)13 ProfileUtilities (org.hl7.fhir.r4.conformance.ProfileUtilities)12 StructureDefinition (org.hl7.fhir.r4b.model.StructureDefinition)12 Test (org.junit.jupiter.api.Test)12 FileNotFoundException (java.io.FileNotFoundException)11