use of org.hl7.fhir.definitions.model.TypeDefn in project kindling by HL7.
the class JsonGenerator method generateElement.
private void generateElement(ElementDefn root, ElementDefn e, Set<String> required, JsonObject props, boolean relative) throws Exception {
if (e.getTypes().size() > 1 || (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");
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;
TypeDefn td = null;
if (definitions.getConstraints().containsKey(t.getName()))
td = definitions.getElementDefn(definitions.getConstraints().get(t.getName()).getBaseType());
else if (definitions.hasElementDefn(t.getName()))
td = definitions.getElementDefn(t.getName());
String en = e.getName().replace("[x]", "");
props.add(en + upFirst(td == null ? t.getName() : td.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 {
JsonObject property = new JsonObject();
JsonObject property_ = null;
props.add(e.getName(), property);
property.addProperty("description", e.getDefinition());
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, null);
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);
tref = "#/definitions/" + tn;
if (definitions.getPrimitives().containsKey(e.typeCode())) {
DefinedCode def = definitions.getPrimitives().get(e.typeCode());
if (e.getName().equals("id")) {
tref = (relative ? "#" : tn.replace(".", "_") + ".schema.json#") + "/definitions/" + tn.replace(".", "_");
property.addProperty("$ref", tref);
} else {
property_ = new JsonObject();
props.add("_" + e.getName(), property_);
property_.addProperty("description", "Extensions for " + e.getName());
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 {
property.addProperty("$ref", tref);
}
tref = (relative ? "#" : "Element.schema.json#") + "/definitions/Element";
}
} 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 (property_ != null) {
if (!Utilities.noString(type))
property.addProperty("type", type);
if (!Utilities.noString(pattern))
property.addProperty("pattern", "^" + pattern + "$");
if (!Utilities.noString(tref))
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.unbounded()) {
makeArray(property);
if (property_ != null)
makeArray(property_);
}
if (e.getMinCardinality() > 0 && property_ == null)
required.add(e.getName());
}
}
use of org.hl7.fhir.definitions.model.TypeDefn in project kindling by HL7.
the class FhirTurtleGenerator method genElementDefn.
/**
* TypeDefinition generator (e.g. code, id, markdown, uuid)
*
* @param td definition to generate
* @throws Exception
*/
private void genElementDefn(TypeDefn td) throws Exception {
String typeName = td.getName();
StructureDefinition typeSd = td.getProfile();
String parentURL = typeSd.getBaseDefinitionElement().getValue();
String parentName = null;
// TODO: Figure out how to do this properly
if (parentURL != null)
parentName = parentURL.substring(parentURL.lastIndexOf("/") + 1);
FHIRResource typeRes = (td.getTypes().isEmpty() ? fact.fhir_class(typeName) : fact.fhir_class(typeName, parentName)).addTitle(td.getShortDefn()).addDefinition(td.getDefinition());
processTypes(typeName, typeRes, td, typeName, false);
}
use of org.hl7.fhir.definitions.model.TypeDefn in project kindling by HL7.
the class OldSpreadsheetParser method processLine.
private ElementDefn processLine(ResourceDefn root, Sheet sheet, int row, Map<String, Invariant> invariants, boolean profile, Profile pack, boolean firstTime) throws Exception {
ElementDefn e;
String path = sheet.getColumn(row, "Element");
if (path.startsWith("!"))
return null;
if (Utilities.noString(path))
throw new Exception("Error reading definitions - no path found @ " + getLocation(row));
if (path.contains("#"))
throw new Exception("Old path style @ " + getLocation(row));
String profileName = isProfile ? sheet.getColumn(row, "Profile Name") : "";
String discriminator = isProfile ? sheet.getColumn(row, "Discriminator") : "";
boolean isRoot = !path.contains(".");
if (isRoot) {
if (root.getRoot() != null)
throw new Exception("Definitions in " + getLocation(row) + " contain two roots: " + path + " in " + root.getName());
root.setName(path);
e = new TypeDefn();
e.setName(path);
root.setRoot((TypeDefn) e);
if (template != null)
e.copyFrom(template.getRoot(), root.getName(), templateTitle);
} else {
e = makeFromPath(root.getRoot(), path, row, profileName, true);
if (template != null) {
ElementDefn ted = getTemplateDefinition(path);
if (ted != null) {
e.copyFrom(ted, root.getName(), templateTitle);
}
}
}
e.setStandardsStatus(StandardsStatus.fromCode(sheet.getColumn(row, "Standards-Status")));
e.setNormativeVersion(sheet.getColumn(row, "Normative-Version"));
if (e.getName().startsWith("@")) {
e.setName(e.getName().substring(1));
e.setXmlAttribute(true);
}
String c = sheet.getColumn(row, "Card.");
if (c == null || c.equals("") || c.startsWith("!")) {
if (!isRoot && !profile && (template == null))
throw new Exception("Missing cardinality at " + getLocation(row) + " on " + path);
if (isRoot && (template == null)) {
e.setMinCardinality(0);
e.setMaxCardinality(Integer.MAX_VALUE);
}
} else {
String[] card = c.split("\\.\\.");
if (card.length != 2 || !Utilities.isInteger(card[0]) || (!"*".equals(card[1]) && !Utilities.isInteger(card[1])))
throw new Exception("Unable to parse Cardinality '" + c + "' " + c + " in " + getLocation(row) + " on " + path);
e.setMinCardinality(Integer.parseInt(card[0]));
e.setMaxCardinality("*".equals(card[1]) ? Integer.MAX_VALUE : Integer.parseInt(card[1]));
}
if (profileName.startsWith("#"))
throw new Exception("blah: " + profileName);
e.setProfileName(profileName);
e.setSliceDescription(isProfile ? sheet.getColumn(row, "Slice Description") : "");
for (String d : discriminator.split("\\,")) if (!Utilities.noString(d))
e.getDiscriminator().add(d);
doAliases(sheet, row, e);
if (sheet.hasColumn(row, "Must Understand"))
throw new Exception("Column 'Must Understand' has been renamed to 'Is Modifier'");
if (sheet.hasColumn(row, "Is Modifier")) {
e.setIsModifier(parseBoolean(sheet.getColumn(row, "Is Modifier"), row, null));
String reason = sheet.getColumn(row, "Modifier Reason");
if (Utilities.noString(reason) && e.getName().toLowerCase().contains("status") && sheet.getColumn(row, "Short Name").contains("error"))
reason = "This element is labelled as a modifier because it is a status element that contains status entered-in-error which means that the resource should not be treated as valid";
if (Utilities.noString(reason) && e.getName().equals("active"))
reason = "This element is labelled as a modifier because it is a status element that can indicate that a record should not be treated as valid";
if (Utilities.noString(reason)) {
System.out.println("Missing IsModifierReason on " + path);
reason = "Not known why this is labelled a modifier";
}
e.setModifierReason(reason);
}
if (isProfile) {
// later, this will get hooked in from the underlying definitions, but we need to know this now to validate the extension modifier matching
if (e.getName().equals("modifierExtension"))
e.setIsModifier(true);
e.setMustSupport(parseBoolean(sheet.getColumn(row, "Must Support"), row, null));
}
if (sheet.hasColumn(row, "Summary"))
e.setSummaryItem(parseBoolean(sheet.getColumn(row, "Summary"), row, null));
e.setRegex(sheet.getColumn(row, "Regex"));
String uml = sheet.getColumn(row, "UML");
if (uml != null) {
if (uml.contains(";")) {
String[] parts = uml.split("\\;");
e.setSvgLeft(Integer.parseInt(parts[0]));
e.setSvgTop(Integer.parseInt(parts[1]));
if (parts.length > 2)
e.setSvgWidth(Integer.parseInt(parts[2]));
e.setUmlDir("");
} else if (uml.startsWith("break:")) {
e.setUmlBreak(true);
e.setUmlDir(uml.substring(6));
} else {
e.setUmlDir(uml);
}
}
String s = sheet.getColumn(row, "Condition");
if (s != null && !s.equals(""))
throw new Exception("Found Condition in spreadsheet " + getLocation(row));
s = sheet.getColumn(row, "Inv.");
if (s != null && !s.equals("")) {
for (String sn : s.split(",")) {
if (!sn.startsWith("!")) {
Invariant inv = invariants.get(sn);
if (inv == null)
throw new Exception("unable to find Invariant '" + sn + "' " + getLocation(row));
e.getStatedInvariants().add(inv);
}
}
}
TypeParser tp = new TypeParser(version.toCode());
e.getTypes().addAll(tp.parse(sheet.getColumn(row, "Type"), isProfile, profileExtensionBase, context, !path.contains("."), this.name));
if (isRoot && e.getTypes().size() == 1 && definitions != null) {
String t = e.getTypes().get(0).getName();
if (definitions.getResourceTemplates().containsKey(t)) {
// we've got a template in play.
template = definitions.getResourceTemplates().get(t);
templateTitle = Utilities.unCamelCase(e.getName());
e.getTypes().get(0).setName(template.getRoot().getTypes().get(0).getName());
} else if (definitions.getBaseResources().containsKey(t) && definitions.getBaseResources().get(t).isInterface()) {
// we've got a template in play.
template = definitions.getBaseResources().get(t);
templateTitle = Utilities.unCamelCase(e.getName());
}
}
if (isProfile && ((path.endsWith(".extension") || path.endsWith(".modifierExtension")) && (e.getTypes().size() == 1) && e.getTypes().get(0).hasProfile()) && Utilities.noString(profileName))
throw new Exception("need to have a profile name if a profiled extension is referenced for " + e.getTypes().get(0).getProfile());
if (sheet.hasColumn(row, "Concept Domain"))
throw new Exception("Column 'Concept Domain' has been retired in " + path);
String bindingName = sheet.getColumn(row, "Binding");
if (!Utilities.noString(bindingName)) {
BindingSpecification binding = bindings.get(bindingName);
if (binding == null && definitions != null)
binding = definitions.getCommonBindings().get(bindingName);
if (binding == null) {
if (bindingName.startsWith("!"))
e.setNoBindingAllowed(true);
else
throw new Exception("Binding name " + bindingName + " could not be resolved in local spreadsheet");
}
e.setBinding(binding);
if (binding != null && !binding.getUseContexts().contains(name))
binding.getUseContexts().add(name);
} else if (e.getBinding() != null) {
if (!e.getBinding().getUseContexts().contains(name))
e.getBinding().getUseContexts().add(name);
}
if (!Utilities.noString(sheet.getColumn(row, "Short Label")))
throw new Exception("Short Label is no longer used");
if (// todo: make this a warning when a fair chunk of the spreadsheets have been converted
sheet.hasColumn(row, "Short Name"))
if (sheet.getColumn(row, "Short Name").startsWith("&"))
e.setShortDefn(e.getShortDefn() + sheet.getColumn(row, "Short Name").substring(1));
else
e.setShortDefn(sheet.getColumn(row, "Short Name"));
if (!isProfile && e.getShortDefn() == null)
throw new Exception("A short definition is required for " + e.getName() + " at " + getLocation(row));
if (sheet.hasColumn(row, "Definition"))
if (sheet.getColumn(row, "Definition").startsWith("&"))
e.setDefinition(Utilities.appendPeriod(e.getDefinition() + processDefinition(sheet.getColumn(row, "Definition")).substring(1)));
else
e.setDefinition(Utilities.appendPeriod(processDefinition(sheet.getColumn(row, "Definition"))));
if (isRoot) {
root.setDefinition(e.getDefinition());
}
if (isProfile || isLogicalModel)
e.setMaxLength(sheet.getColumn(row, "Max Length"));
if (sheet.hasColumn(row, "Requirements"))
if (sheet.getColumn(row, "Requirements").startsWith("&"))
e.setRequirements(Utilities.appendPeriod(e.getRequirements() + sheet.getColumn(row, "Requirements").substring(1)));
else
e.setRequirements(Utilities.appendPeriod(sheet.getColumn(row, "Requirements")));
if (sheet.hasColumn(row, "Comments"))
if (sheet.getColumn(row, "Comments").startsWith("&"))
e.setComments(Utilities.appendPeriod(e.getComments() + Utilities.appendPeriod(sheet.getColumn(row, "Comments").substring(1))));
else
e.setComments(Utilities.appendPeriod(Utilities.appendPeriod(sheet.getColumn(row, "Comments"))));
for (String n : mappings.keySet()) {
String ms = sheet.getColumn(row, mappings.get(n).getColumnName());
if (mappings.get(n).getColumnName().equals("Snomed Code") && !Utilities.noString(ms))
System.out.println("!!");
e.addMapping(n, ms.trim());
}
if (pack != null) {
for (String n : pack.getMappingSpaces().keySet()) {
e.addMapping(n, sheet.getColumn(row, pack.getMappingSpaces().get(n).getColumnName()).trim());
}
}
if (sheet.hasColumn("Hierarchy"))
e.setHierarchy(parseBoolean(sheet.getColumn(row, "Hierarchy"), row, null));
if (sheet.hasColumn(row, "To Do"))
e.setTodo(Utilities.appendPeriod(sheet.getColumn(row, "To Do")));
if (sheet.hasColumn(row, "Example"))
e.setExample(processValue(sheet, row, "Example", sheet.getColumn(row, "Example"), e));
processOtherExamples(e, sheet, row);
if (sheet.hasColumn(row, "Committee Notes"))
e.setCommitteeNotes(Utilities.appendPeriod(sheet.getColumn(row, "Committee Notes")));
if (sheet.hasColumn(row, "Display Hint"))
e.setDisplayHint(sheet.getColumn(row, "Display Hint"));
if (isProfile) {
e.setFixed(processValue(sheet, row, "Value", sheet.getColumn(row, "Value"), e));
e.setPattern(processValue(sheet, row, "Pattern", sheet.getColumn(row, "Pattern"), e));
} else {
if (sheet.hasColumn(row, "Default Value"))
errors.add(path + ": Default value '" + sheet.getColumn(row, "Default Value") + "' found @ " + getLocation(row));
if (sheet.hasColumn(row, "Missing Meaning"))
e.setMeaningWhenMissing(sheet.getColumn(row, "Missing Meaning"));
}
if (sheet.hasColumn(row, "w5"))
e.setW5(checkW5(sheet.getColumn(row, "w5"), path));
if (sheet.hasColumn(row, "Translatable"))
e.setTranslatable(parseBoolean(sheet.getColumn(row, "Translatable"), row, false));
if (sheet.hasColumn(row, "Order Meaning"))
e.setOrderMeaning(sheet.getColumn(row, "Order Meaning"));
return e;
}
use of org.hl7.fhir.definitions.model.TypeDefn in project kindling by HL7.
the class SourceParser method convertToExpression.
private String convertToExpression(ResourceDefn rd, List<String> pn, Set<String> targets, SearchParameterDefn sp) throws Exception {
StringBuilder b = new StringBuilder();
boolean first = true;
for (String p : pn) {
StringBuilder bp = new StringBuilder();
ElementDefn ed;
List<ElementDefn> trace = new ArrayList<ElementDefn>();
if (p.startsWith(rd.getName() + ".")) {
ed = rd.getRoot().getElementByName(p, true, definitions, "search parameter generation", true, trace);
} else if (p.startsWith("{{name}}.")) {
ed = rd.getRoot().getElementByName(p.replace("{{name}}", rd.getName()), true, definitions, "search parameter generation", true, trace);
} else
throw new Exception("huh?");
if (ed == null)
throw new Exception("not found: " + p);
for (ElementDefn t : trace) {
if (t.getStandardsStatus() != null && t.getStandardsStatus().isLowerThan(sp.getStandardsStatus()))
sp.setStandardsStatus(t.getStandardsStatus(), t.getNormativeVersion(rd));
try {
if (definitions.hasPrimitiveType(t.typeCodeNoParams())) {
sp.setStandardsStatus(StandardsStatus.NORMATIVE, t.getNormativeVersion(rd));
} else if (Utilities.noString(t.typeCode())) {
// nothing, this is part of the resource.
} else if (t.getTypes().size() == 1 && !t.getTypes().get(0).getName().startsWith("@")) {
TypeDefn tt = definitions.getElementDefn(t.typeCodeNoParams());
if (tt.getStandardsStatus() != null && tt.getStandardsStatus().isLowerThan(sp.getStandardsStatus())) {
sp.setStandardsStatus(tt.getStandardsStatus(), t.getNormativeVersion(rd));
}
}
} catch (Exception e) {
// nothing
}
}
if (ed.getName().endsWith("[x]"))
if (p.endsWith("[x]"))
bp.append(p.substring(0, p.length() - 3));
else {
int lp = p.lastIndexOf(".") + ed.getName().length() - 2;
String tn = p.substring(lp);
if (definitions.hasPrimitiveType(Utilities.uncapitalize(tn)))
bp.append("(" + p.substring(0, lp) + " as " + Utilities.uncapitalize(tn) + ")");
else
bp.append("(" + p.substring(0, lp) + " as " + tn + ")");
}
else
bp.append(p);
if (!targets.isEmpty() && !matchesElementDefinition(targets, ed.getTypes())) {
bp.append(".where(");
boolean innerFirst = true;
for (String t : targets) {
if (innerFirst)
innerFirst = false;
else
bp.append(" or ");
bp.append("resolve() is " + t);
}
bp.append(")");
}
if (first)
first = false;
else
b.append(" | ");
b.append(bp.toString());
}
return b.toString();
}
use of org.hl7.fhir.definitions.model.TypeDefn in project kindling by HL7.
the class Publisher method getSnapShotForProfile.
public StructureDefinition getSnapShotForProfile(String base) throws Exception {
String[] parts = base.split("#");
if (parts[0].startsWith("http://hl7.org/fhir/StructureDefinition/") && parts.length == 1) {
String name = base.substring(40);
if (page.getDefinitions().hasResource(name))
return page.getDefinitions().getSnapShotForType(name);
else if (page.getDefinitions().hasType(name)) {
TypeDefn t = page.getDefinitions().getElementDefn(name);
if (t.getProfile().hasSnapshot())
return t.getProfile();
throw new Exception("unable to find snapshot for " + name);
}
// else
// throw new Exception("unable to find base definition for "+name);
}
StructureDefinition p = new ProfileUtilities(page.getWorkerContext(), page.getValidationErrors(), page).getProfile(null, parts[0]);
if (p == null)
throw new Exception("unable to find base definition for " + base);
if (parts.length == 1) {
if (p.getSnapshot() == null)
// or else we could fill it in?
throw new Exception("StructureDefinition " + base + " has no snapshot");
return p;
}
for (Resource r : p.getContained()) {
if (r instanceof StructureDefinition && r.getId().equals(parts[1])) {
StructureDefinition pc = (StructureDefinition) r;
if (pc.getSnapshot() == null) {
StructureDefinition ps = getSnapShotForProfile(pc.getBaseDefinition());
processProfile(pc);
}
return pc;
}
}
throw new Exception("Unable to find snapshot for " + base);
}
Aggregations