Search in sources :

Example 11 with SchematronWriter

use of org.hl7.fhir.utilities.xml.SchematronWriter in project org.hl7.fhir.core by hapifhir.

the class ProfileUtilities method generateForChildren.

private void generateForChildren(SchematronWriter sch, String xpath, ElementDefinition ed, StructureDefinition structure, StructureDefinition base) throws IOException {
    // generateForChild(txt, structure, child);
    List<ElementDefinition> children = getChildList(structure, ed);
    String sliceName = null;
    ElementDefinitionSlicingComponent slicing = null;
    for (ElementDefinition child : children) {
        String name = tail(child.getPath());
        if (child.hasSlicing()) {
            sliceName = name;
            slicing = child.getSlicing();
        } else if (!name.equals(sliceName))
            slicing = null;
        ElementDefinition based = getByPath(base, child.getPath());
        boolean doMin = (child.getMin() > 0) && (based == null || (child.getMin() != based.getMin()));
        boolean doMax = child.hasMax() && !child.getMax().equals("*") && (based == null || (!child.getMax().equals(based.getMax())));
        Slicer slicer = slicing == null ? new Slicer(true) : generateSlicer(child, slicing, structure);
        if (slicer.check) {
            if (doMin || doMax) {
                Section s = sch.section(xpath);
                Rule r = s.rule(xpath);
                if (doMin)
                    r.assrt("count(f:" + name + slicer.criteria + ") >= " + Integer.toString(child.getMin()), name + slicer.name + ": minimum cardinality of '" + name + "' is " + Integer.toString(child.getMin()));
                if (doMax)
                    r.assrt("count(f:" + name + slicer.criteria + ") <= " + child.getMax(), name + slicer.name + ": maximum cardinality of '" + name + "' is " + child.getMax());
            }
        }
    }
    for (ElementDefinitionConstraintComponent inv : ed.getConstraint()) {
        if (inv.hasXpath()) {
            Section s = sch.section(ed.getPath());
            Rule r = s.rule(xpath);
            r.assrt(inv.getXpath(), (inv.hasId() ? inv.getId() + ": " : "") + inv.getHuman() + (inv.hasUserData(IS_DERIVED) ? " (inherited)" : ""));
        }
    }
    if (!ed.hasContentReference()) {
        for (ElementDefinition child : children) {
            String name = tail(child.getPath());
            generateForChildren(sch, xpath + "/f:" + name, child, structure, base);
        }
    }
}
Also used : ElementDefinitionSlicingComponent(org.hl7.fhir.r4b.model.ElementDefinition.ElementDefinitionSlicingComponent) ElementDefinition(org.hl7.fhir.r4b.model.ElementDefinition) TypeDerivationRule(org.hl7.fhir.r4b.model.StructureDefinition.TypeDerivationRule) Rule(org.hl7.fhir.utilities.xml.SchematronWriter.Rule) Section(org.hl7.fhir.utilities.xml.SchematronWriter.Section) ElementDefinitionConstraintComponent(org.hl7.fhir.r4b.model.ElementDefinition.ElementDefinitionConstraintComponent)

Example 12 with SchematronWriter

use of org.hl7.fhir.utilities.xml.SchematronWriter in project org.hl7.fhir.core by hapifhir.

the class ProfileUtilities method generateSchematrons.

// generate schematrons for the rules in a structure definition
public void generateSchematrons(OutputStream dest, StructureDefinition structure) throws IOException, DefinitionException {
    if (structure.getDerivation() != TypeDerivationRule.CONSTRAINT)
        throw new DefinitionException(context.formatMessage(I18nConstants.NOT_THE_RIGHT_KIND_OF_STRUCTURE_TO_GENERATE_SCHEMATRONS_FOR));
    if (!structure.hasSnapshot())
        throw new DefinitionException(context.formatMessage(I18nConstants.NEEDS_A_SNAPSHOT));
    StructureDefinition base = context.fetchResource(StructureDefinition.class, structure.getBaseDefinition());
    if (base != null) {
        SchematronWriter sch = new SchematronWriter(dest, SchematronType.PROFILE, base.getName());
        ElementDefinition ed = structure.getSnapshot().getElement().get(0);
        generateForChildren(sch, "f:" + ed.getPath(), ed, structure, base);
        sch.dump();
    }
}
Also used : StructureDefinition(org.hl7.fhir.r4b.model.StructureDefinition) SchematronWriter(org.hl7.fhir.utilities.xml.SchematronWriter) DefinitionException(org.hl7.fhir.exceptions.DefinitionException) ElementDefinition(org.hl7.fhir.r4b.model.ElementDefinition)

Example 13 with SchematronWriter

use of org.hl7.fhir.utilities.xml.SchematronWriter in project org.hl7.fhir.core by hapifhir.

the class ProfileUtilities method generateForChildren.

private void generateForChildren(SchematronWriter sch, String xpath, ElementDefinition ed, StructureDefinition structure, StructureDefinition base) throws IOException {
    // generateForChild(txt, structure, child);
    List<ElementDefinition> children = getChildList(structure, ed);
    String sliceName = null;
    ElementDefinitionSlicingComponent slicing = null;
    for (ElementDefinition child : children) {
        String name = tail(child.getPath());
        if (child.hasSlicing()) {
            sliceName = name;
            slicing = child.getSlicing();
        } else if (!name.equals(sliceName))
            slicing = null;
        ElementDefinition based = getByPath(base, child.getPath());
        boolean doMin = (child.getMin() > 0) && (based == null || (child.getMin() != based.getMin()));
        boolean doMax = child.hasMax() && !child.getMax().equals("*") && (based == null || (!child.getMax().equals(based.getMax())));
        Slicer slicer = slicing == null ? new Slicer(true) : generateSlicer(child, slicing, structure);
        if (slicer.check) {
            if (doMin || doMax) {
                Section s = sch.section(xpath);
                Rule r = s.rule(xpath);
                if (doMin)
                    r.assrt("count(f:" + name + slicer.criteria + ") >= " + Integer.toString(child.getMin()), name + slicer.name + ": minimum cardinality of '" + name + "' is " + Integer.toString(child.getMin()));
                if (doMax)
                    r.assrt("count(f:" + name + slicer.criteria + ") <= " + child.getMax(), name + slicer.name + ": maximum cardinality of '" + name + "' is " + child.getMax());
            }
        }
    }
    for (ElementDefinitionConstraintComponent inv : ed.getConstraint()) {
        if (inv.hasXpath()) {
            Section s = sch.section(ed.getPath());
            Rule r = s.rule(xpath);
            r.assrt(inv.getXpath(), (inv.hasId() ? inv.getId() + ": " : "") + inv.getHuman() + (inv.hasUserData(IS_DERIVED) ? " (inherited)" : ""));
        }
    }
    for (ElementDefinition child : children) {
        String name = tail(child.getPath());
        generateForChildren(sch, xpath + "/f:" + name, child, structure, base);
    }
}
Also used : ElementDefinitionSlicingComponent(org.hl7.fhir.dstu3.model.ElementDefinition.ElementDefinitionSlicingComponent) ElementDefinition(org.hl7.fhir.dstu3.model.ElementDefinition) TypeDerivationRule(org.hl7.fhir.dstu3.model.StructureDefinition.TypeDerivationRule) Rule(org.hl7.fhir.utilities.xml.SchematronWriter.Rule) Section(org.hl7.fhir.utilities.xml.SchematronWriter.Section) ElementDefinitionConstraintComponent(org.hl7.fhir.dstu3.model.ElementDefinition.ElementDefinitionConstraintComponent)

Example 14 with SchematronWriter

use of org.hl7.fhir.utilities.xml.SchematronWriter in project org.hl7.fhir.core by hapifhir.

the class ProfileUtilities method generateForChildren.

private void generateForChildren(SchematronWriter sch, String xpath, ElementDefinition ed, StructureDefinition structure, StructureDefinition base) throws IOException {
    // generateForChild(txt, structure, child);
    List<ElementDefinition> children = getChildList(structure, ed);
    String sliceName = null;
    ElementDefinitionSlicingComponent slicing = null;
    for (ElementDefinition child : children) {
        String name = tail(child.getPath());
        if (child.hasSlicing()) {
            sliceName = name;
            slicing = child.getSlicing();
        } else if (!name.equals(sliceName))
            slicing = null;
        ElementDefinition based = getByPath(base, child.getPath());
        boolean doMin = (child.getMin() > 0) && (based == null || (child.getMin() != based.getMin()));
        boolean doMax = !child.getMax().equals("*") && (based == null || (!child.getMax().equals(based.getMax())));
        Slicer slicer = slicing == null ? new Slicer(true) : generateSlicer(child, slicing, structure);
        if (slicer.check) {
            if (doMin || doMax) {
                Section s = sch.section(xpath);
                Rule r = s.rule(xpath);
                if (doMin)
                    r.assrt("count(f:" + name + slicer.criteria + ") >= " + Integer.toString(child.getMin()), name + slicer.name + ": minimum cardinality of '" + name + "' is " + Integer.toString(child.getMin()));
                if (doMax)
                    r.assrt("count(f:" + name + slicer.criteria + ") <= " + child.getMax(), name + slicer.name + ": maximum cardinality of '" + name + "' is " + child.getMax());
            }
        }
    }
    for (ElementDefinitionConstraintComponent inv : ed.getConstraint()) {
        if (inv.hasXpath()) {
            Section s = sch.section(ed.getPath());
            Rule r = s.rule(xpath);
            r.assrt(inv.getXpath(), (inv.hasId() ? inv.getId() + ": " : "") + inv.getHuman() + (inv.hasUserData(IS_DERIVED) ? " (inherited)" : ""));
        }
    }
    for (ElementDefinition child : children) {
        String name = tail(child.getPath());
        generateForChildren(sch, xpath + "/f:" + name, child, structure, base);
    }
}
Also used : ElementDefinitionSlicingComponent(org.hl7.fhir.dstu2016may.model.ElementDefinition.ElementDefinitionSlicingComponent) ElementDefinition(org.hl7.fhir.dstu2016may.model.ElementDefinition) Rule(org.hl7.fhir.utilities.xml.SchematronWriter.Rule) TypeDerivationRule(org.hl7.fhir.dstu2016may.model.StructureDefinition.TypeDerivationRule) Section(org.hl7.fhir.utilities.xml.SchematronWriter.Section) ElementDefinitionConstraintComponent(org.hl7.fhir.dstu2016may.model.ElementDefinition.ElementDefinitionConstraintComponent)

Example 15 with SchematronWriter

use of org.hl7.fhir.utilities.xml.SchematronWriter in project org.hl7.fhir.core by hapifhir.

the class ProfileUtilities method generateSchematrons.

// generate schematrons for the rules in a structure definition
public void generateSchematrons(OutputStream dest, StructureDefinition structure) throws IOException, DefinitionException {
    if (structure.getDerivation() != TypeDerivationRule.CONSTRAINT)
        throw new DefinitionException(context.formatMessage(I18nConstants.NOT_THE_RIGHT_KIND_OF_STRUCTURE_TO_GENERATE_SCHEMATRONS_FOR));
    if (!structure.hasSnapshot())
        throw new DefinitionException(context.formatMessage(I18nConstants.NEEDS_A_SNAPSHOT));
    StructureDefinition base = context.fetchResource(StructureDefinition.class, structure.getBaseDefinition());
    if (base != null) {
        SchematronWriter sch = new SchematronWriter(dest, SchematronType.PROFILE, base.getName());
        ElementDefinition ed = structure.getSnapshot().getElement().get(0);
        generateForChildren(sch, "f:" + ed.getPath(), ed, structure, base);
        sch.dump();
    }
}
Also used : StructureDefinition(org.hl7.fhir.r5.model.StructureDefinition) SchematronWriter(org.hl7.fhir.utilities.xml.SchematronWriter) DefinitionException(org.hl7.fhir.exceptions.DefinitionException) ElementDefinition(org.hl7.fhir.r5.model.ElementDefinition)

Aggregations

Section (org.hl7.fhir.utilities.xml.SchematronWriter.Section)9 SchematronWriter (org.hl7.fhir.utilities.xml.SchematronWriter)8 Rule (org.hl7.fhir.utilities.xml.SchematronWriter.Rule)7 DefinitionException (org.hl7.fhir.exceptions.DefinitionException)6 ArrayList (java.util.ArrayList)2 ElementDefinition (org.hl7.fhir.dstu2.model.ElementDefinition)2 ElementDefinition (org.hl7.fhir.dstu2016may.model.ElementDefinition)2 ElementDefinition (org.hl7.fhir.dstu3.model.ElementDefinition)2 ElementDefinition (org.hl7.fhir.r4.model.ElementDefinition)2 ElementDefinition (org.hl7.fhir.r4b.model.ElementDefinition)2 ElementDefinition (org.hl7.fhir.r5.model.ElementDefinition)2 ElementDefinitionConstraintComponent (org.hl7.fhir.r5.model.ElementDefinition.ElementDefinitionConstraintComponent)2 StructureDefinition (org.hl7.fhir.r5.model.StructureDefinition)2 HashSet (java.util.HashSet)1 ResourceDefn (org.hl7.fhir.definitions.model.ResourceDefn)1 ElementDefinitionConstraintComponent (org.hl7.fhir.dstu2.model.ElementDefinition.ElementDefinitionConstraintComponent)1 ElementDefinitionSlicingComponent (org.hl7.fhir.dstu2.model.ElementDefinition.ElementDefinitionSlicingComponent)1 StructureDefinition (org.hl7.fhir.dstu2.model.StructureDefinition)1 ElementDefinitionConstraintComponent (org.hl7.fhir.dstu2016may.model.ElementDefinition.ElementDefinitionConstraintComponent)1 ElementDefinitionSlicingComponent (org.hl7.fhir.dstu2016may.model.ElementDefinition.ElementDefinitionSlicingComponent)1