use of org.hl7.fhir.definitions.model.TypeRef in project kindling by HL7.
the class TypeParser method convert.
public List<TypeRefComponent> convert(IWorkerContext context, String path, List<TypeRef> types, boolean resource, ElementDefinition ed) throws Exception {
List<TypeRefComponent> list = new ArrayList<TypeRefComponent>();
for (TypeRef t : types) {
// Expand any Resource(A|B|C) references
if (t.hasParams() && !("Reference".equals(t.getName()) || "canonical".equals(t.getName()))) {
throw new Exception("Only resource references can specify parameters. Path " + path);
}
if (t.getParams().size() > 0) {
if (t.getProfile() != null && t.getParams().size() != 1) {
throw new Exception("Cannot declare profile on a resource reference declaring multiple resource types. Path " + path);
}
if (t.getProfile() != null) {
TypeRefComponent childType = getTypeComponent(list, t.getName());
if (t.getVersioning() != null)
childType.setVersioning(t.getVersioning());
if (t.getName().equals("Reference") || t.getName().equals("canonical"))
childType.addTargetProfile(t.getProfile());
else
childType.addProfile(t.getProfile());
} else
for (String param : t.getParams()) {
TypeRefComponent childType = getTypeComponent(list, t.getName());
if (t.getVersioning() != null)
childType.setVersioning(t.getVersioning());
String p = "Any".equals(param) ? "Resource" : param;
if (t.getName().equals("Reference") || t.getName().equals("canonical"))
childType.addTargetProfile("http://hl7.org/fhir/StructureDefinition/" + p);
else
childType.addProfile("http://hl7.org/fhir/StructureDefinition/" + p);
}
} 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)) {
TypeRefComponent tc = new TypeRefComponent().setCode(n);
if (t.getVersioning() != null)
tc.setVersioning(t.getVersioning());
list.add(tc);
}
} else if (Utilities.noString(t.getName()) && t.getProfile() != null) {
StructureDefinition sd = context.fetchResource(StructureDefinition.class, t.getProfile());
TypeRefComponent tc = getTypeComponent(list, sd != null ? sd.getType() : t.getName());
if (t.getVersioning() != null)
tc.setVersioning(t.getVersioning());
if (t.getName().equals("Reference"))
tc.addTargetProfile(t.getProfile());
else
tc.addProfile(t.getProfile());
} else if (t.getName().startsWith("=")) {
if (resource)
list.add(new TypeRefComponent().setCode("BackboneElement"));
else
list.add(new TypeRefComponent().setCode("Element"));
ToolingExtensions.addStringExtension(ed, "http://hl7.org/fhir/StructureDefinition/structuredefinition-explicit-type-name", t.getName().substring(1));
} else {
StructureDefinition sd = context.fetchTypeDefinition(t.getName());
if (sd == null)
throw new Exception("Unknown type '" + t.getName() + "'");
TypeRefComponent tc = getTypeComponent(list, sd.getType());
if (t.getVersioning() != null)
tc.setVersioning(t.getVersioning());
if (t.getName().equals("Reference")) {
if (t.hasProfile())
tc.addTargetProfile(t.getProfile());
} else if (t.hasProfile())
tc.addProfile(t.getProfile());
}
}
// no duplicates
for (TypeRefComponent tr1 : list) {
for (TypeRefComponent tr2 : list) {
if (tr1 != tr2) {
if (tr1.getWorkingCode().equals(tr2.getWorkingCode()))
throw new Exception("duplicate code " + tr1.getWorkingCode());
}
}
}
return list;
}
use of org.hl7.fhir.definitions.model.TypeRef in project kindling by HL7.
the class ValueSetGenerator method genDataTypes.
private void genDataTypes(ValueSet vs) throws Exception {
if (!vs.hasCompose())
vs.setCompose(new ValueSetComposeComponent());
vs.getCompose().addInclude().setSystem("http://hl7.org/fhir/data-types");
vs.setUserData("filename", "valueset-" + vs.getId());
if (!vs.hasExtension(ToolingExtensions.EXT_WORKGROUP)) {
vs.addExtension().setUrl(ToolingExtensions.EXT_WORKGROUP).setValue(new CodeType("fhir"));
} else {
String ec = ToolingExtensions.readStringExtension(vs, ToolingExtensions.EXT_WORKGROUP);
if (!ec.equals("fhir"))
System.out.println("ValueSet " + vs.getUrl() + " WG mismatch 6: is " + ec + ", want to set to " + "fhir");
}
vs.setUserData("path", "valueset-" + vs.getId() + ".html");
CodeSystem cs = new CodeSystem();
CodeSystemConvertor.populate(cs, vs);
cs.setUrl("http://hl7.org/fhir/data-types");
cs.setVersion(version);
cs.setCaseSensitive(true);
cs.setContent(CodeSystemContentMode.COMPLETE);
definitions.getCodeSystems().see(cs, packageInfo);
List<String> codes = new ArrayList<String>();
for (TypeRef t : definitions.getKnownTypes()) codes.add(t.getName());
Collections.sort(codes);
for (String s : codes) {
if (!definitions.dataTypeIsSharedInfo(s)) {
ConceptDefinitionComponent c = cs.addConcept();
c.setCode(s);
c.setDisplay(s);
if (definitions.getPrimitives().containsKey(s))
c.setDefinition(definitions.getPrimitives().get(s).getDefinition());
else if (definitions.getConstraints().containsKey(s))
// don't add these: c.setDefinition(definitions.getConstraints().get(s).getDefinition());
;
else if (definitions.hasElementDefn(s))
c.setDefinition(definitions.getElementDefn(s).getDefinition());
else
c.setDefinition("...to do...");
}
}
ToolingExtensions.addCSComment(cs.addConcept().setCode("xhtml").setDisplay("XHTML").setDefinition("XHTML format, as defined by W3C, but restricted usage (mainly, no active content)"), "Special case: xhtml can only be used in the narrative Data Type");
markSpecialStatus(vs, cs, true);
}
use of org.hl7.fhir.definitions.model.TypeRef in project kindling by HL7.
the class OldSpreadsheetParser method parseProfileSheet.
private ConstraintStructure parseProfileSheet(Definitions definitions, Profile ap, String n, List<String> namedSheets, boolean published, String usage, List<ValidationMessage> issues, WorkGroup wg, String fmm) throws Exception {
Sheet sheet;
ResourceDefn resource = new ResourceDefn();
sheet = loadSheet(n + "-Inv");
Map<String, Invariant> invariants = null;
if (sheet != null) {
invariants = readInvariants(sheet, n, n + "-Inv");
} else {
invariants = new HashMap<String, Invariant>();
}
sheet = loadSheet(n);
if (sheet == null)
throw new Exception("The StructureDefinition referred to a tab by the name of '" + n + "', but no tab by the name could be found");
for (int row = 0; row < sheet.rows.size(); row++) {
ElementDefn e = processLine(resource, sheet, row, invariants, true, ap, row == 0);
if (e != null)
for (TypeRef t : e.getTypes()) {
if (t.getProfile() != null && !t.getName().equals("Extension") && t.getProfile().startsWith("#")) {
if (!namedSheets.contains(t.getProfile().substring(1)))
namedSheets.add(t.getProfile().substring(1));
}
}
}
sheet = loadSheet(n + "-Extensions");
if (sheet != null) {
int row = 0;
while (row < sheet.rows.size()) {
if (sheet.getColumn(row, "Code").startsWith("!"))
row++;
else
row = processExtension(resource.getRoot().getElementByName(definitions, "extensions", true, false), sheet, row, definitions, ap.metadata("extension.uri"), ap, issues, invariants, wg);
}
}
sheet = loadSheet(n + "-Search");
if (sheet != null) {
readSearchParams(resource, sheet, true);
}
if (invariants != null) {
for (Invariant inv : invariants.values()) {
if (Utilities.noString(inv.getContext()))
throw new Exception("Type " + resource.getRoot().getName() + " Invariant " + inv.getId() + " has no context");
else {
ElementDefn ed = findContext(resource.getRoot(), inv.getContext(), "Type " + resource.getRoot().getName() + " Invariant " + inv.getId() + " Context");
// TODO: Need to resolve context based on element name, not just path
if (ed.getName().endsWith("[x]") && !inv.getContext().endsWith("[x]"))
inv.setFixedName(inv.getContext().substring(inv.getContext().lastIndexOf(".") + 1));
ed.getInvariants().put(inv.getId(), inv);
if (Utilities.noString(inv.getXpath())) {
throw new Exception("Type " + resource.getRoot().getName() + " Invariant " + inv.getId() + " (" + inv.getEnglish() + ") has no XPath statement");
} else if (inv.getXpath().contains("\""))
throw new Exception("Type " + resource.getRoot().getName() + " Invariant " + inv.getId() + " (" + inv.getEnglish() + ") contains a \" character");
// if (Utilities.noString(inv.getExpression()))
// throw new Exception("Type "+resource.getRoot().getName()+" Invariant "+inv.getId()+" ("+inv.getEnglish()+") has no Expression statement (in FHIRPath format)");
}
}
}
resource.getRoot().setProfileName(n);
if (n.toLowerCase().equals(ap.getId()))
throw new Exception("Duplicate Profile Name: Package id " + ap.getId() + " and profile id " + n.toLowerCase() + " are the same");
if (profileIds.containsKey(n.toLowerCase()))
throw new Exception("Duplicate Profile Name: " + n.toLowerCase() + " in " + ap.getId() + ", already registered in " + profileIds.get(n.toLowerCase()).getOwner());
ConstraintStructure p = new ConstraintStructure(n.toLowerCase(), resource.getRoot().getProfileName(), resource, ig != null ? ig : definitions.getUsageIG(usage, "Parsing " + name), wg, fmm, Utilities.existsInList(ap.metadata("Experimental"), "y", "Y", "true", "TRUE", "1"));
p.setOwner(ap.getId());
profileIds.put(n.toLowerCase(), p);
return p;
}
use of org.hl7.fhir.definitions.model.TypeRef in project kindling by HL7.
the class OldSpreadsheetParser method resolveElementReferences.
private void resolveElementReferences(ResourceDefn parent, ElementDefn root) throws Exception {
for (TypeRef ref : root.getTypes()) {
if (ref.isElementReference()) {
ElementDefn referredElement = parent.getRoot().getElementByName(definitions, ref.getName().substring(1), true, false, null);
if (referredElement == null)
throw new Exception("Element reference " + ref.getName() + " cannot be found in type " + parent.getName());
if (referredElement.getDeclaredTypeName() == null)
throw new Exception("Element reference " + ref.getName() + " in " + parent.getName() + " refers to an anonymous group of elements. Please specify names with the '=<name>' construct in the typename column.");
ref.setResolvedTypeName(referredElement.getDeclaredTypeName());
}
}
for (ElementDefn element : root.getElements()) {
resolveElementReferences(parent, element);
}
}
use of org.hl7.fhir.definitions.model.TypeRef in project kindling by HL7.
the class SourceParser method loadCompositeType.
private String loadCompositeType(String n, Map<String, org.hl7.fhir.definitions.model.TypeDefn> map, String fmm, boolean isAbstract) throws Exception {
TypeParser tp = new TypeParser(version.toString());
List<TypeRef> ts = tp.parse(n, false, null, context, true);
definitions.getKnownTypes().addAll(ts);
StandardsStatus status = loadStatus(n);
String nv = loadNormativeVersion(n);
try {
TypeRef t = ts.get(0);
File csv = new CSFile(dtDir + t.getName().toLowerCase() + ".xml");
if (csv.exists()) {
OldSpreadsheetParser p = new OldSpreadsheetParser("core", new CSFileInputStream(csv), csv.getName(), csv.getAbsolutePath(), definitions, srcDir, logger, registry, version, context, genDate, isAbstract, page, true, ini, wg("fhir"), definitions.getProfileIds(), fpUsages, page.getConceptMaps(), exceptionIfExcelNotNormalised, page.packageInfo(), page.getRc());
org.hl7.fhir.definitions.model.TypeDefn el = p.parseCompositeType();
el.setFmmLevel(fmm);
el.setStandardsStatus(status);
el.setNormativeVersion(nv);
map.put(t.getName(), el);
genTypeProfile(el);
errors.addAll(p.getErrors());
return el.getName();
} else {
String p = ini.getStringProperty("types", n);
csv = new CSFile(dtDir + p.toLowerCase() + ".xml");
if (!csv.exists())
throw new Exception("unable to find a definition for " + n + " in " + p);
XLSXmlParser xls = new XLSXmlParser(new CSFileInputStream(csv), csv.getAbsolutePath());
new XLSXmlNormaliser(csv.getAbsolutePath(), exceptionIfExcelNotNormalised).go();
Sheet sheet = xls.getSheets().get("Restrictions");
boolean found = false;
for (int i = 0; i < sheet.rows.size(); i++) {
if (sheet.getColumn(i, "Name").equals(n)) {
found = true;
Invariant inv = new Invariant();
inv.setId(n);
inv.setEnglish(sheet.getColumn(i, "Rules"));
inv.setOcl(sheet.getColumn(i, "OCL"));
inv.setXpath(sheet.getColumn(i, "XPath"));
inv.setExpression(sheet.getColumn(i, "Expression"));
inv.setExplanation(sheet.getColumn(i, "Explanation"));
inv.setTurtle(sheet.getColumn(i, "RDF"));
ProfiledType pt = new ProfiledType();
pt.setDefinition(sheet.getColumn(i, "Definition"));
pt.setDescription(sheet.getColumn(i, "Rules"));
String structure = sheet.getColumn(i, "Structure");
if (!Utilities.noString(structure)) {
String[] parts = structure.split("\\;");
for (String pp : parts) {
String[] words = pp.split("\\=");
pt.getRules().put(words[0], words[1]);
}
}
pt.setName(n);
pt.setBaseType(p);
pt.setInvariant(inv);
definitions.getConstraints().put(n, pt);
}
}
if (!found)
throw new Exception("Unable to find definition for " + n);
return n;
}
} catch (Exception e) {
throw new Exception("Unable to load " + n + ": " + e.getMessage(), e);
}
}
Aggregations