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("not the right kind of structure to generate schematrons for");
if (!structure.hasSnapshot())
throw new DefinitionException("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();
}
}
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);
}
}
use of org.hl7.fhir.utilities.xml.SchematronWriter in project kindling by HL7.
the class SchematronGenerator method generate.
public void generate(OutputStream out, ResourceDefn root, Definitions definitions) throws Exception {
SchematronWriter sch = new SchematronWriter(out, SchematronType.RESOURCE, root.getName());
insertGlobalRules(sch);
Section s = sch.section(root.getName());
ArrayList<String> parents = new ArrayList<String>();
generateInvariants(s, null, root.getRoot(), definitions, parents, root.getName());
sch.dump();
sch.close();
}
use of org.hl7.fhir.utilities.xml.SchematronWriter in project kindling by HL7.
the class SchematronGenerator method insertGlobalRules.
private void insertGlobalRules(SchematronWriter sch) throws IOException {
Section s = sch.section("Global");
s.rule("f:*").assrt("@value|f:*|h:div", "global-1: All FHIR elements must have a @value or children");
s.rule("f:extension").assrt("exists(f:extension)!=exists(f:*[starts-with(local-name(.), 'value')])", "ext-1: Must have either extensions or value[x], not both");
s.rule("f:modifierExtension").assrt("exists(f:extension)!=exists(f:*[starts-with(local-name(.), 'value')])", "ext-1: Must have either extensions or value[x], not both");
}
use of org.hl7.fhir.utilities.xml.SchematronWriter in project kindling by HL7.
the class SchematronGenerator method generate.
public void generate(OutputStream out, Definitions definitions) throws Exception {
SchematronWriter sch = new SchematronWriter(out, SchematronType.ALL_RESOURCES, "All Resources");
insertGlobalRules(sch);
for (String rn : definitions.sortedResourceNames()) {
ResourceDefn root = definitions.getResources().get(rn);
Section s = sch.section(root.getName());
ArrayList<String> parents = new ArrayList<String>();
generateInvariants(s, null, root.getRoot(), definitions, parents, root.getName());
}
Set<StructureDefinition> processed = new HashSet<StructureDefinition>();
for (StructureDefinition exd : page.getWorkerContext().getExtensionDefinitions()) {
if (exd.getSnapshot().getElement().get(0).hasConstraint() && !processed.contains(exd)) {
processed.add(exd);
Section s = sch.section("Extension: " + exd.getName());
Rule r = s.rule("f:" + (exd.getSnapshot().getElementFirstRep().getIsModifier() ? "modifierExtension" : "extension") + "[@url='" + exd.getUrl() + "']");
for (ElementDefinitionConstraintComponent inv : exd.getSnapshot().getElement().get(0).getConstraint()) {
if (!isGlobal(inv.getKey()))
r.assrt(inv.getXpath().replace("\"", "'"), inv.getKey() + ": " + inv.getHuman());
}
}
}
sch.dump();
sch.close();
}
Aggregations