use of org.hl7.fhir.definitions.model.ElementDefn in project kindling by HL7.
the class ProfileGenerator method defineElement.
/**
* note: snapshot implies that we are generating a resource or a data type; for other profiles, the snapshot is generated elsewhere
* @param isInterface
*/
private ElementDefinition defineElement(Profile ap, StructureDefinition p, List<ElementDefinition> elements, ElementDefn e, String path, Set<String> slices, List<SliceHandle> parentSlices, SnapShotMode snapshot, boolean root, String defType, String inheritedType, boolean defaults) throws Exception {
boolean handleDiscriminator = true;
if (!Utilities.noString(e.getProfileName()) && !e.getDiscriminator().isEmpty() && !slices.contains(path)) {
handleDiscriminator = false;
// hey, we jumped straight into the slices with setting up the slicing (allowed in the spreadsheets, but not otherwise)
ElementDefinition slicer = new ElementDefinition();
elements.add(slicer);
slicer.setId(path);
slicer.setPath(path);
processDiscriminator(e, path, slicer);
if (e.getMaxCardinality() != null)
slicer.setMax(e.getMaxCardinality() == Integer.MAX_VALUE ? "*" : e.getMaxCardinality().toString());
slices.add(path);
}
// todo for task 12259
// if (ap != null) {
// String base = isImplicitTypeConstraint(path);
// if (base != null) {
// ElementDefinition typeConstrainer = new ElementDefinition(ElementDefinition.NOT_MODIFIER, ElementDefinition.NOT_IN_SUMMARY);
// elements.add(typeConstrainer);
// typeConstrainer.setId(base);
// typeConstrainer.setPath(base);
// String type = path.substring(base.length()-3);
// if (definitions.hasPrimitiveType(Utilities.uncapitalize(type)))
// type = Utilities.uncapitalize(type);
// typeConstrainer.addType().setCode(type);
// }
// }
ElementDefinition ce = new ElementDefinition(defaults, ElementDefinition.NOT_MODIFIER, ElementDefinition.NOT_IN_SUMMARY);
elements.add(ce);
if (e.getStandardsStatus() != null)
ToolingExtensions.setStandardsStatus(ce, e.getStandardsStatus(), e.getNormativeVersion());
ce.setId(path);
ce.setPath(path);
if (e.isXmlAttribute())
ce.addRepresentation(PropertyRepresentation.XMLATTR);
List<SliceHandle> myParents = new ArrayList<ProfileGenerator.SliceHandle>();
myParents.addAll(parentSlices);
// which holds Slicing information)
if (e.hasDescriminator() || !Utilities.noString(e.getProfileName())) {
if (e.getDiscriminator().size() > 0 && !slices.contains(path) && handleDiscriminator) {
processDiscriminator(e, path, ce);
slices.add(path);
}
if (!Utilities.noString(e.getProfileName())) {
SliceHandle hnd = new SliceHandle();
// though this it not used?
hnd.name = path;
myParents.add(hnd);
if (path.contains(".")) {
// We don't want a slice name on the root
ce.setSliceName(e.getProfileName());
ce.setId(ce.getId() + ":" + e.getProfileName());
}
}
}
if (e.isTranslatable()) {
ce.addExtension(BuildExtensions.EXT_TRANSLATABLE, new BooleanType(true));
}
if (Utilities.existsInList(ce.getPath(), "Element.extension", "DomainResource.extension", "DomainResource.modifierExtension") && !ce.hasSlicing() && !ce.hasSliceName()) {
ce.getSlicing().setDescription("Extensions are always sliced by (at least) url").setRules(SlicingRules.OPEN).addDiscriminator().setType(DiscriminatorType.VALUE).setPath("url");
}
if (!Utilities.noString(inheritedType) && snapshot != SnapShotMode.None) {
ElementDefn inh = definitions.getElementDefn(inheritedType);
buildDefinitionFromElement(path, ce, inh, ap, p, inheritedType, definitions.getBaseResources().containsKey(inheritedType) && definitions.getBaseResources().get(inheritedType).isInterface());
} else if (path.contains(".") && Utilities.noString(e.typeCode()) && snapshot != SnapShotMode.None) {
addElementConstraints(defType, ce);
}
buildDefinitionFromElement(path, ce, e, ap, p, null, false);
if (!Utilities.noString(e.getStatedType())) {
ToolingExtensions.addStringExtension(ce, "http://hl7.org/fhir/StructureDefinition/structuredefinition-explicit-type-name", e.getStatedType());
}
if (e.isNoBindingAllowed()) {
ToolingExtensions.addBooleanExtension(ce, BuildExtensions.EXT_NO_BINDING, true);
}
if (!root) {
if (e.typeCode().startsWith("@")) {
ce.setContentReference("#" + getIdForPath(elements, e.typeCode().substring(1)));
} else if (Utilities.existsInList(path, "Element.id", "Extension.url") || path.endsWith(".id")) {
TypeRefComponent tr = ce.addType();
tr.getFormatCommentsPre().add("Note: special primitive values have a FHIRPath system type. e.g. this is compiler magic (j)");
tr.setCode(Constants.NS_SYSTEM_TYPE + "String");
if (path.equals("Extension.url")) {
ToolingExtensions.addUriExtension(tr, ToolingExtensions.EXT_FHIR_TYPE, "uri");
} else if (p.getKind() == StructureDefinitionKind.RESOURCE) {
ToolingExtensions.addUriExtension(tr, ToolingExtensions.EXT_FHIR_TYPE, "id");
} else {
ToolingExtensions.addUriExtension(tr, ToolingExtensions.EXT_FHIR_TYPE, "string");
}
} else {
List<TypeRef> expandedTypes = new ArrayList<TypeRef>();
for (TypeRef t : e.getTypes()) {
// Expand any Resource(A|B|C) references
if (t.hasParams() && !Utilities.existsInList(t.getName(), "Reference", "canonical", "CodeableReference")) {
throw new Exception("Only resource types can specify parameters. Path " + path + " in profile " + p.getName());
}
if (t.getParams().size() > 1) {
if (t.getProfile() != null && t.getParams().size() != 1) {
throw new Exception("Cannot declare profile on a resource reference declaring multiple resource types. Path " + path + " in profile " + p.getName());
}
for (String param : t.getParams()) {
if (definitions.hasLogicalModel(param)) {
for (String pn : definitions.getLogicalModel(param).getImplementations()) {
TypeRef childType = new TypeRef(t.getName());
childType.getParams().add(pn);
childType.getAggregations().addAll(t.getAggregations());
expandedTypes.add(childType);
}
} else {
TypeRef childType = new TypeRef(t.getName());
childType.getParams().add(param);
childType.getAggregations().addAll(t.getAggregations());
expandedTypes.add(childType);
}
}
} else if (t.isWildcardType()) {
// this list is filled out manually because it may be running before the types referred to have been loaded
for (String n : TypesUtilities.wildcardTypes(version.toString())) expandedTypes.add(new TypeRef(n));
} else if (!t.getName().startsWith("=")) {
if (definitions.isLoaded() && (!definitions.hasResource(t.getName()) && !definitions.hasType(t.getName()) && !definitions.hasElementDefn(t.getName()) && !definitions.getBaseResources().containsKey(t.getName()) && !t.getName().equals("xhtml"))) {
throw new Exception("Bad Type '" + t.getName() + "' at " + path + " in profile " + p.getUrl());
}
expandedTypes.add(t);
}
}
if (expandedTypes.isEmpty()) {
if (defType != null)
ce.addType().setCode(defType);
} else
for (TypeRef t : expandedTypes) {
String profile = null;
String tc = null;
if (definitions.getConstraints().containsKey(t.getName())) {
ProfiledType pt = definitions.getConstraints().get(t.getName());
tc = pt.getBaseType();
profile = "http://hl7.org/fhir/StructureDefinition/" + pt.getName();
} else {
tc = t.getName();
profile = t.getProfile();
}
TypeRefComponent type = ce.getType(tc);
if (profile == null && t.hasParams()) {
profile = t.getParams().get(0);
}
if (t.getPatterns() != null) {
for (String s : t.getPatterns()) {
type.addExtension("http://hl7.org/fhir/StructureDefinition/elementdefinition-pattern", new CanonicalType("http://hl7.org/fhir/StructureDefinition/" + s));
}
}
if (profile != null) {
if (type.getWorkingCode().equals("Extension")) {
// check that the extension is being used correctly:
StructureDefinition ext = context.getExtensionStructure(null, profile);
if (ext == null) {
throw new Exception("Unable to resolve extension definition: " + profile);
}
boolean srcMod = ext.getSnapshot().getElement().get(0).getIsModifier();
boolean tgtMod = e.isModifier();
if (srcMod && !tgtMod)
throw new Exception("The extension '" + profile + "' is a modifier extension, but is being used as if it is not a modifier extension");
if (!srcMod && tgtMod)
throw new Exception("The extension '" + profile + "' is not a modifier extension, but is being used as if it is a modifier extension");
}
List<String> pr = new ArrayList<>();
if (profile.startsWith("http:") || profile.startsWith("#")) {
pr.add(profile);
} else if (definitions.hasLogicalModel(profile)) {
for (String pn : definitions.getLogicalModel(profile).getImplementations()) pr.add("http://hl7.org/fhir/StructureDefinition/" + pn);
} else
pr.add("http://hl7.org/fhir/StructureDefinition/" + (profile.equals("Any") ? "Resource" : profile));
if (type.getWorkingCode().equals("Reference") || type.getWorkingCode().equals("canonical") || type.getWorkingCode().equals("CodeableReference")) {
for (String pn : pr) {
type.addTargetProfile(pn);
if (e.hasHierarchy())
ToolingExtensions.addBooleanExtension(type, ToolingExtensions.EXT_HIERARCHY, e.getHierarchy());
}
} else
for (String pn : pr) {
type.addProfile(pn);
}
}
for (String aggregation : t.getAggregations()) {
type.addAggregation(AggregationMode.fromCode(aggregation));
}
}
}
}
String w5 = translateW5(e.getW5());
if (w5 != null)
addMapping(p, ce, "http://hl7.org/fhir/fivews", w5, ap);
if (e.isTranslatable())
ce.addExtension("http://hl7.org/fhir/StructureDefinition/elementdefinition-translatable", new BooleanType(true));
if (!Utilities.noString(e.getOrderMeaning()))
ce.setOrderMeaning(e.getOrderMeaning());
if (e.hasBinding()) {
ce.setBinding(generateBinding(e.getBinding()));
}
if (snapshot != SnapShotMode.None && !e.getElements().isEmpty()) {
// makeExtensionSlice("extension", p, c, e, path);
// if (snapshot == SnapShotMode.Resource) {
// makeExtensionSlice("modifierExtension", p, c, e, path);
// if (!path.contains(".")) {
// c.getElement().add(createBaseDefinition(p, path, definitions.getBaseResources().get("Resource").getRoot().getElementByName("language")));
// c.getElement().add(createBaseDefinition(p, path, definitions.getBaseResources().get("DomainResource").getRoot().getElementByName("text")));
// c.getElement().add(createBaseDefinition(p, path, definitions.getBaseResources().get("DomainResource").getRoot().getElementByName("contained")));
// }
// }
}
if (defaults)
ce.makeBase();
Set<String> containedSlices = new HashSet<String>();
if (snapshot != SnapShotMode.None) {
if (!root && Utilities.noString(e.typeCode())) {
if (snapshot == SnapShotMode.Resource)
defineAncestorElements("BackboneElement", path, snapshot, containedSlices, p, elements, defType, defaults);
else
defineAncestorElements("Element", path, snapshot, containedSlices, p, elements, defType, defaults);
} else if (root && !Utilities.noString(e.typeCode()))
defineAncestorElements(e.typeCode(), path, snapshot, containedSlices, p, elements, defType, defaults);
}
for (ElementDefn child : e.getElements()) defineElement(ap, p, elements, child, path + "." + child.getName(), containedSlices, myParents, snapshot, false, defType, null, defaults);
return ce;
}
use of org.hl7.fhir.definitions.model.ElementDefn in project kindling by HL7.
the class ProfileGenerator method convertConstraints.
private void convertConstraints(ElementDefn e, ElementDefinition ce, String source) throws FHIRException {
for (String in : e.getInvariants().keySet()) {
ElementDefinitionConstraintComponent con = new ElementDefinitionConstraintComponent();
Invariant inv = e.getInvariants().get(in);
con.setKey(inv.getId());
if (!con.hasKey()) {
profileCounter++;
con.setKey("prf-" + Integer.toString(profileCounter));
}
con.setRequirements(inv.getRequirements());
if (Utilities.noString(inv.getSeverity()))
con.setSeverity(ConstraintSeverity.ERROR);
else if (inv.getSeverity().equals("best-practice")) {
con.setSeverity(ConstraintSeverity.WARNING);
ToolingExtensions.addBooleanExtension(con, ToolingExtensions.EXT_BEST_PRACTICE, true);
if (Utilities.noString(inv.getExplanation()))
throw new FHIRException("Best Practice Invariants need to have an explanation");
con.addExtension().setUrl(ToolingExtensions.EXT_BEST_PRACTICE_EXPLANATION).setValue(new MarkdownType(inv.getExplanation()));
} else
con.setSeverity(ConstraintSeverity.fromCode(inv.getSeverity()));
con.setHuman(inv.getEnglish());
con.setXpath(inv.getXpath());
if (!Utilities.isAbsoluteUrl(source)) {
throw new Error("source : " + source);
}
con.setSource(source);
if (!"n/a".equals(inv.getExpression()))
con.setExpression(inv.getExpression());
ce.getConstraint().add(con);
}
}
use of org.hl7.fhir.definitions.model.ElementDefn in project kindling by HL7.
the class ProfileGenerator method defineAncestorElements.
private void defineAncestorElements(String type, String path, SnapShotMode snapshot, Set<String> containedSlices, StructureDefinition p, List<ElementDefinition> elements, String dt, boolean defaults) throws Exception {
ElementDefn e = definitions.getElementDefn(actualTypeName(type));
if (!Utilities.noString(e.typeCode()))
defineAncestorElements(e.typeCode(), path, snapshot, containedSlices, p, elements, dt, defaults);
if (!definitions.getBaseResources().containsKey(e.getName()) || !definitions.getBaseResources().get(e.getName()).isInterface()) {
for (ElementDefn child : e.getElements()) {
ElementDefinition ed = defineElement(null, p, elements, child, path + "." + child.getName(), containedSlices, new ArrayList<ProfileGenerator.SliceHandle>(), snapshot, false, dt, null, defaults);
if (!ed.hasBase())
ed.setBase(new ElementDefinitionBaseComponent());
ed.getBase().setPath(e.getName() + "." + child.getName());
if (child.getMinCardinality() != null)
ed.getBase().setMin(child.getMinCardinality());
if (child.getMaxCardinality() != null)
ed.getBase().setMax(child.getMaxCardinality() == Integer.MAX_VALUE ? "*" : child.getMaxCardinality().toString());
if (snapshot == SnapShotMode.DataType && ed.getPath().endsWith(".extension") && !ed.hasSlicing())
ed.getSlicing().setDescription("Extensions are always sliced by (at least) url").setRules(SlicingRules.OPEN).addDiscriminator().setType(DiscriminatorType.VALUE).setPath("url");
}
}
}
use of org.hl7.fhir.definitions.model.ElementDefn in project kindling by HL7.
the class JsonLDGenerator method generate.
public void generate(JsonObject context, ElementDefn root, FHIRVersion version, String genDate) throws Exception {
enums.clear();
enumDefs.clear();
scanTypes(root, root);
generateType(context, root, root.getName(), root, true, context, new HashSet<String>());
for (ElementDefn e : types.keySet()) {
generateType(context, root, types.get(e), e, true, context, new HashSet<String>());
}
}
use of org.hl7.fhir.definitions.model.ElementDefn in project kindling by HL7.
the class JsonLDGenerator method generateElement.
private void generateElement(ElementDefn root, String name, ElementDefn e, JsonObject base, Set<String> types) throws Exception {
if ((e.getTypes().size() == 1 && e.getTypes().get(0).isWildcardType())) {
if (!e.getName().contains("[x]"))
throw new Exception("Element " + e.getName() + " in " + root.getName() + " has multiple types as a choice doesn't have a [x] in the element name");
for (TypeRef tr : datatypes) {
String tn = tr.getName();
if (tn.equals("SimpleQuantity"))
tn = "Quantity";
else
tn = Utilities.capitalize(tn);
String en = e.getName().substring(0, e.getName().length() - 3) + tn;
JsonObject property = new JsonObject();
base.add(name + "." + en, property);
property.addProperty("@id", "http://hl7.org/fhir/" + name + "." + en);
}
// if (e.getTypes().size() == 1)
// generateAny(root, e, e.getName().replace("[x]", ""), props, relative);
// else {
// for (TypeRef t : e.getTypes()) {
// JsonObject property = new JsonObject();
// JsonObject property_ = null;
// String en = e.getName().replace("[x]", "");
// props.add(en+upFirst(t.getName()), property);
// property.addProperty("description", e.getDefinition());
// String tref = null;
// String type = null;
// String pattern = null;
// if (definitions.getPrimitives().containsKey(t.getName())) {
// DefinedCode def = definitions.getPrimitives().get(t.getName());
// type = def.getJsonType();
// pattern = def.getRegex();
// if (!Utilities.noString(pattern))
// property.addProperty("pattern", pattern);
//
// property.addProperty("type", type);
// property_ = new JsonObject();
// props.add("_"+en+upFirst(t.getName()), property_);
// property_.addProperty("description", "Extensions for "+en+upFirst(t.getName()));
// tref = (relative ? "#" : "Element.schema.json#") +"/definitions/Element";
// property_.addProperty("$ref", tref);
// } else {
// String tn = encodeType(e, t, true);
// tref = (relative ? "#" : tn.replace(".", "_")+".schema.json#") +"/definitions/"+tn.replace(".", "_");
// property.addProperty("$ref", tref);
// }
// }
// }
} else if (e.getName().endsWith("[x]")) {
for (TypeRef tr : e.getTypes()) {
String tn = tr.getName();
if (tn.equals("SimpleQuantity"))
tn = "Quantity";
else
tn = Utilities.capitalize(tn);
String en = e.getName().substring(0, e.getName().length() - 3) + tn;
JsonObject property = new JsonObject();
base.add(name + "." + en, property);
property.addProperty("@id", "http://hl7.org/fhir/" + name + "." + en);
}
} else {
JsonObject property = new JsonObject();
base.add(name + "." + e.getName(), property);
if (e.getPath() == null)
property.addProperty("@id", "http://hl7.org/fhir/" + name + "." + e.getName());
else
property.addProperty("@id", "http://hl7.org/fhir/" + e.getPath());
// if we're using lists:
// if (e.unbounded())
// property.addProperty("@container", "@list");
// property.addProperty("fhir-@type", "http://hl7.org/fhir/"+e.typeCode());
// String tref = null;
// String type = null;
// String pattern = null;
//
// if (e.usesCompositeType()/* && types.containsKey(root.getElementByName(e.typeCode().substring(1)))*/) {
// ElementDefn ref = root.getElementByName(definitions, e.typeCode().substring(1), true, false);
// String rtn = types.get(ref);
// if (rtn == null)
// throw new Exception("logic error in schema generator (null composite reference in "+types.toString()+")");
//
// if(rtn == "Type")
// rtn = "Element";
// type=rtn;
// tref = "#/definitions/"+rtn.replace(".", "_");
// } else if (e.getTypes().size() == 0 && e.getElements().size() > 0){
// tref = "#/definitions/"+types.get(e).replace(".", "_");
// type=types.get(e).replace(".", "_");
// } else if (e.getTypes().size() == 1) {
// String tn = encodeType(e, e.getTypes().get(0), true);
// type=tn;
// if (definitions.getPrimitives().containsKey(e.typeCode())) {
// DefinedCode def = definitions.getPrimitives().get(e.typeCode());
// type = def.getJsonType();
// pattern = def.getRegex();
// property_ = new JsonObject();
// props.add("_"+e.getName(), property_);
// property_.addProperty("description", "Extensions for "+e.getName());
// tref = (relative ? "#" : "Element.schema.json#") +"/definitions/Element";
// BindingSpecification cd = e.getBinding();
//
// if (cd != null && (cd.getBinding() == BindingSpecification.BindingMethod.CodeList)) {
// ValueSet vs = cd.getValueSet();
// if (vs!= null) {
// ValueSet ex = workerContext.expandVS(vs, true, false).getValueset();
// JsonArray enums = new JsonArray();
// for (ValueSetExpansionContainsComponent cc : ex.getExpansion().getContains()) {
// enums.add(new JsonPrimitive(cc.getCode()));
// }
// property.add("enum", enums);
// pattern = null;
// }
// }
// } else {
// tref = (relative ? "#" : tn.replace(".", "_")+".schema.json#") +"/definitions/"+tn.replace(".", "_");
// }
// } else
// throw new Exception("how do we get here? "+e.getName()+" in "+root.getName()+" "+Integer.toString(e.getTypes().size()));
//
// if (e.unbounded()) {
// property.addProperty("type", "array");
// if (property_ != null) {
// property_.addProperty("type", "array");
// JsonObject items = new JsonObject();
// property.add("items", items);
// items.addProperty("type", type);
// if (!Utilities.noString(pattern))
// items.addProperty("pattern", pattern);
//
// items = new JsonObject();
// property_.add("items", items);
// items.addProperty("$ref", tref);
// } else {
// JsonObject items = new JsonObject();
// property.add("items", items);
// items.addProperty("$ref", tref);
// }
// } else {
// if (property_ != null) {
// property.addProperty("type", type);
// if (!Utilities.noString(pattern))
// property.addProperty("pattern", pattern);
//
// property_.addProperty("$ref", tref);
// } else if("div".equals(e.getName()) && "xhtml".equals(type)) {
// // Is there a better type, or ref for html?
// property.addProperty("type", "string");
// } else {
// property.addProperty("$ref", tref);
// }
// }
// if (e.getMinCardinality() > 0 && property_ == null)
// required.add(e.getName());
}
}
Aggregations