public ElementDefn getElementByProfileName(String name) { for (int i = elements.size() - 1; i >= 0; i--) { ElementDefn e = elements.get(i); if (e.getProfileName().equalsIgnoreCase(name)) return e; } return null; }
public void generate() throws Exception { outputFile.createNewFile(); GenBlock fileBlock = new GenBlock(); generateMainHeader(fileBlock); ElementDefn root = getRootDefinition(); generateResourceHeader(fileBlock, root); // first loop through and generate all of the embedded types from this // schema for (Iterator<ElementDefn> iterator = root.getElements().iterator(); iterator.hasNext(); ) { ElementDefn elementDefinition = iterator.next(); generateEmbeddedType(outputFile.getParentFile(), fileBlock, elementDefinition); } generatePostEmbedded(fileBlock, root); // next loop through and generate the filed elements for (Iterator<ElementDefn> iterator = root.getElements().iterator(); iterator.hasNext(); ) { ElementDefn elementDefinition = iterator.next(); if (!elementDefinition.isXmlAttribute() || xmlAttributeAsField) generateElement(fileBlock, elementDefinition); } generateResourceFooter(fileBlock); generateMainFooter(fileBlock); Writer modelFile = new BufferedWriter(new FileWriter(outputFile)); modelFile.write(fileBlock.toString()); modelFile.flush(); modelFile.close(); }
private boolean nameMatches(String n, ElementDefn e, boolean throughChoice) { if (e.getName().equals(n)) return true; else if (!throughChoice || !e.getName().endsWith("[x]")) return false; else { String b = e.getName().substring(0, e.getName().indexOf("[")); if (!n.startsWith(b)) return false; String tn = n.substring(b.length()); for (TypeRef t : e.getTypes()) if (t.getName().equalsIgnoreCase(tn)) return true; return false; } }
public ElementDefn getElementByName(String name, boolean throughChoice) { String n = name.contains(".") ? name.substring(0, name.indexOf(".")) : name; String t = name.contains(".") ? name.substring(name.indexOf(".") + 1) : null; if (n.equals(this.name) && t != null) return getElementByName(t); for (int i = elements.size() - 1; i >= 0; i--) { ElementDefn e = elements.get(i); if (nameMatches(n, e, throughChoice)) return t == null ? e : e.getElementByName(t); } return null; }
private ElementDefn getElementForPath(String pathname) throws Exception { String[] path = pathname.split("\\."); ElementDefn res = definitions.getElementDefn(path[0]); for (int i = 1; i < path.length; i++) { String en = path[i]; if (en.length() == 0) throw new Exception("Improper path " + pathname); ElementDefn t = res.getElementByName(definitions, en, true, false); if (t == null) { throw new Exception("unable to resolve " + pathname); } res = t; } return res; }
private void scan(ElementDefn e, String path) throws Exception { if (e.hasBinding()) { BindingSpecification cd = e.getBinding(); if (!txusages.containsKey(cd)) { txusages.put(cd, new ArrayList<CDUsage>()); c++; txusages.get(cd).add(new CDUsage(String.valueOf(c), null)); } txusages.get(cd).add(new CDUsage(path, e)); } for (ElementDefn c : e.getElements()) { scan(c, path + "." + c.getName()); } }
public ElementDefn getElementForPath(String pathname, Definitions definitions, String purpose) throws Exception { String[] path = pathname.split("\\."); if (!path[0].equals(getName())) throw new Exception( "Element Path '" + pathname + "' is not legal in this context (" + purpose + ")"); ElementDefn res = this; for (int i = 1; i < path.length; i++) { String en = path[i]; if (en.length() == 0) throw new Exception("Improper path " + pathname); ElementDefn t = null; if (res.typeCode().startsWith("@")) { res = this.getElementForPath(res.typeCode().substring(1), definitions, purpose); } else if (definitions.dataTypeIsSharedInfo(res.typeCode())) { res = definitions.getElementDefn(res.typeCode()); } else if (definitions.hasType(res.typeCode())) { res = definitions.getElementDefn(res.typeCode()); } t = res.getElementByName(en); if (t == null) { throw new Exception("unable to resolve " + pathname); } res = t; } return res; }
protected String getEmbeddedClassName(ElementDefn elementDefinition, TypeRef typeRef) { if (typeRef != null) return typeRef.getResolvedTypeName(); String typeName = generateTypeName(elementDefinition, null); String cname = elementDefinition.getDeclaredTypeName(); cname = (cname == null) ? typeName : cname; return Character.toUpperCase(cname.charAt(0)) + cname.substring(1); }
public ElementDefn(ElementDefn pattern) { super(); types.addAll(pattern.types); for (ElementDefn c : pattern.getElements()) elements.add(new ElementDefn(c)); minCardinality = pattern.minCardinality; maxCardinality = pattern.maxCardinality; statedInvariants.addAll(pattern.statedInvariants); modifier = pattern.modifier; mustSupport = pattern.mustSupport; binding = pattern.binding; name = pattern.name; shortDefn = pattern.shortDefn; definition = pattern.definition; requirements = pattern.requirements; mappings.putAll(pattern.mappings); comments = pattern.comments; todo = pattern.todo; committeeNotes = pattern.committeeNotes; condition = pattern.condition; example = pattern.example; profileName = pattern.profileName; fixed = pattern.fixed; inherited = pattern.inherited; }
public ElementDefn getElementByName(String name) { String n = name.contains(".") ? name.substring(0, name.indexOf(".")) : name; String t = name.contains(".") ? name.substring(name.indexOf(".") + 1) : null; if (n.equals(this.name) && t != null) return getElementByName(t); for (int i = elements.size() - 1; i >= 0; i--) { ElementDefn e = elements.get(i); if (nameMatches(n, e, false, null)) return t == null ? e : e.getElementByName(t); // if (e.getName().length() > name.length() // && e.getName().substring(0, name.length()) // .equalsIgnoreCase(name) // && e.getElements().size() == 1 // && e.getElements().get(0).getName().equalsIgnoreCase(name)) // return e.getElements().get(0); } return null; }
private void processMappings(Section section, String subject, ElementDefn e) throws Exception { for (String m : e.getMappings().keySet()) { if (m.equals("http://hl7.org/orim")) section.importTtl(e.getMappings().get(m)); if (m.equals("http://snomed.info")) { System.out.println("sct = " + e.getMappings().get(m)); throw new Error( "Snomed CT mappings are not done for RDF"); // reminder to actually do this // // http://snomed.info/id/{sctid} } if (m.equals("http://loinc.org")) { String[] sl = e.getMappings().get(m).split("\\,"); for (String sp : sl) { String s = sp.contains("(") ? sp.substring(0, sp.indexOf("(")).trim() : sp.trim(); if (!s.contains(" ") && !s.contains("/")) section.triple(subject, "fhir:loinc", "loinc:" + s); } } } }
private String loadCompositeType(String n, Map<String, ElementDefn> map) throws Exception { TypeParser tp = new TypeParser(); List<TypeRef> ts = tp.parse(n); definitions.getKnownTypes().addAll(ts); try { TypeRef t = ts.get(0); File csv = new CSFile(dtDir + t.getName().toLowerCase() + ".xml"); if (csv.exists()) { SpreadsheetParser p = new SpreadsheetParser( new CSFileInputStream(csv), csv.getName(), definitions, srcDir, logger, registry); ElementDefn el = p.parseCompositeType(); map.put(t.getName(), el); el.getAcceptableGenericTypes().addAll(ts.get(0).getParams()); 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()); 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")); definitions.getConstraints().put(n, new DefinedCode(n, sheet.getColumn(i, "Rules"), p)); definitions.getConstraintInvariants().put(n, inv); } } 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); } }
public ElementDefn getElementByName( String name, boolean throughChoice, Definitions definitions, String purpose) throws Exception { String n = name.contains(".") ? name.substring(0, name.indexOf(".")) : name; String t = name.contains(".") ? name.substring(name.indexOf(".") + 1) : null; if (n.equals(this.name) && t != null) return getElementByName(t); ElementDefn focus = this; if (typeCode().startsWith("@")) { String s = typeCode().substring(1); focus = definitions.getElementDefn(s.substring(0, s.indexOf("."))); focus = focus.getElementForPath(s, definitions, purpose, throughChoice); } for (int i = focus.elements.size() - 1; i >= 0; i--) { ElementDefn e = focus.elements.get(i); if (nameMatches(n, e, throughChoice, definitions)) return t == null ? e : e.getElementByName(t); } return null; }
protected void generateElement(GenBlock block, ElementDefn elementDefinition) { List<TypeRef> types = elementDefinition.getTypes(); boolean multipleCardinality = elementDefinition.getMaxCardinality() == null || elementDefinition.getMaxCardinality() > 1; if (types.size() > 0) { for (TypeRef typeRef : types) { String elementType = typeRef.getName(); if (elementType.startsWith("@")) { handleField(block, FieldType.EMBEDDED, multipleCardinality, elementDefinition, typeRef); } else { handleField( block, FieldType.getFieldType(elementType), multipleCardinality, elementDefinition, typeRef); dataTypes.add(elementType); } } } else if (types.size() == 0) { handleField(block, FieldType.EMBEDDED, multipleCardinality, elementDefinition, null); } }
protected String generateTypeName(ElementDefn elementDefinition, TypeRef type, boolean fixTypes) { String elementName = elementDefinition.getName().replace("[x]", ""); if (elementDefinition.getTypes().size() > 1) { String typeName = type.getName(); typeName = Character.toUpperCase(typeName.charAt(0)) + typeName.substring(1); if (FieldType.QUANTITY == FieldType.getFieldType(typeName)) { elementName += "Quantity"; } else { elementName += typeName; } } if (!fixTypes) { return elementName; } else if (elementName.equals("type")) { elementName = "fhirType"; } else if (elementName.equals("collection")) { elementName = "fhirCollection"; } else if (elementName.equals("deleted")) { elementName = "fhirDeleted"; } else if (elementName.equals("version")) { elementName = "versionNum"; } else if (elementName.equals("class")) { elementName = "fhirClass"; } else if (elementName.equals("hash")) { elementName = "fhirHash"; } else if (elementName.equals("identity")) { elementName = "fhirIdentity"; } else if (elementName.equals("modifier")) { elementName = "fhirModifier"; } else if (elementName.equals("validated")) { elementName = "fhirValidated"; } return elementName; }
private boolean nameMatches( String n, ElementDefn e, boolean throughChoice, Definitions definitions) { if (e.getName().equals(n)) return true; else if (!throughChoice || !e.getName().endsWith("[x]")) return false; else { String b = e.getName().substring(0, e.getName().indexOf("[")); if (!n.startsWith(b)) return false; String tn = n.substring(b.length()); if (e.typeCode().equals("*") && definitions != null) { for (TypeRef t : definitions.getKnownTypes()) { if (!definitions.getInfrastructure().containsKey(t.getName()) && !definitions.getConstraints().containsKey(t.getName())) { if (t.getName().equalsIgnoreCase(tn)) return true; } } } else for (TypeRef t : e.getTypes()) if (t.getName().equalsIgnoreCase(tn)) return true; return false; } }
protected boolean isResource(ElementDefn elementDefinition) { for (TypeRef type : elementDefinition.getTypes()) { if (type.getName().endsWith(RESOURCE_TYPE)) return true; } return false; }
public void generate(String prefix, ElementDefn root) throws Exception { scan(root, root.getName()); gen(prefix, txusages); flush(); close(); }
private void genAnon(AnonTypeInfo at) throws Exception { if (at.isType()) at.getSection().triple("fhir:" + at.getName(), "a", "fhir:Element"); else at.getSection().triple("fhir:" + at.getName(), "a", "fhir:BackboneElement"); at.getSection().comment("fhir:" + at.getName(), at.getDefn().getDefinition()); processMappings(at.getSection(), "fhir:" + at.getName(), at.getDefn()); for (ElementDefn e : at.getDefn().getElements()) { if (e.getName().endsWith("[x]")) { String cn = e.getName().substring(0, e.getName().length() - 3); at.getSection().triple("fhir:" + at.getName() + "." + cn, "a", "rdf:Property"); at.getSection().comment("fhir:" + at.getName() + "." + cn, e.getDefinition()); processMappings(at.getSection(), "fhir:" + at.getName() + "." + cn, e); for (TypeRef tr : e.typeCode().equals("*") ? getAnyTypes() : e.getTypes()) { String en = cn + Utilities.capitalize(tr.getName()); at.getSection() .triple( "fhir:" + at.getName() + "." + en, "rdfs:subPropertyOf", "fhir:" + at.getName() + "." + cn); at.getSection() .triple("fhir:" + at.getName() + "." + en, "rdfs:domain", "fhir:" + at.getName()); genRange(at.getSection(), at.getName(), en, e, tr, at.isType()); } } else { at.getSection().triple("fhir:" + at.getName() + "." + e.getName(), "a", "rdf:Property"); at.getSection().comment("fhir:" + at.getName() + "." + e.getName(), e.getDefinition()); processMappings(at.getSection(), "fhir:" + at.getName() + "." + e.getName(), e); at.getSection() .triple( "fhir:" + at.getName() + "." + e.getName(), "rdfs:domain", "fhir:" + at.getName()); genRange( at.getSection(), at.getName(), e.getName(), e, e.getTypes().isEmpty() ? null : e.getTypes().get(0), at.isType()); } } }
private void gen(ResourceDefn rd) throws Exception { Section section = section(rd.getName()); ElementDefn t = rd.getRoot(); if (t.getTypes().isEmpty()) section.triple("fhir:" + t.getName(), "a", "rdfs:Class"); else section.triple("fhir:" + t.getName(), "rdfs:subClassOf", processType(t.typeCode())); section.comment("fhir:" + t.getName(), rd.getDefinition()); if (!Utilities.noString(t.getW5())) section.triple( "fhir:" + t.getName(), "fhir:w5", complex().predicate("a", "fhir:w5\\#" + t.getW5())); processMappings(section, "fhir:" + rd.getName(), rd.getRoot()); for (ElementDefn e : t.getElements()) { if (e.getName().endsWith("[x]")) { String cn = e.getName().substring(0, e.getName().length() - 3); section.triple("fhir:" + t.getName() + "." + cn, "a", "rdf:Property"); section.comment("fhir:" + t.getName() + "." + cn, e.getDefinition()); processMappings(section, "fhir:" + t.getName() + "." + cn, e); for (TypeRef tr : e.typeCode().equals("*") ? getAnyTypes() : e.getTypes()) { String en = cn + Utilities.capitalize(tr.getName()); section.triple( "fhir:" + t.getName() + "." + en, "rdfs:subPropertyOf", "fhir:" + t.getName() + "." + cn); section.triple("fhir:" + t.getName() + "." + en, "rdfs:domain", "fhir:" + rd.getName()); genRange(section, t.getName(), en, e, tr, false); } } else { section.triple("fhir:" + t.getName() + "." + e.getName(), "a", "rdf:Property"); section.comment("fhir:" + t.getName() + "." + e.getName(), e.getDefinition()); section.triple( "fhir:" + t.getName() + "." + e.getName(), "rdfs:domain", "fhir:" + rd.getName()); processMappings(section, "fhir:" + t.getName() + "." + e.getName(), e); genRange( section, t.getName(), e.getName(), e, e.getTypes().isEmpty() ? null : e.getTypes().get(0), false); } } processAnonTypes(); }
private void genRange( Section section, String tn, String en, ElementDefn e, TypeRef tr, boolean datatype) throws Exception { // metadata if (e.isModifier()) section.triple( "fhir:" + tn + "." + en, "fhir:hasFlag", complex().predicate("a", "fhir:isModifier")); if (e.isXmlAttribute()) section.triple( "fhir:" + tn + "." + en, "fhir:hasFlag", complex().predicate("a", "fhir:isXmlAtribute")); if (e.hasMustSupport() && e.isMustSupport()) section.triple( "fhir:" + tn + "." + en, "fhir:hasFlag", complex().predicate("a", "fhir:isMustSupport")); if (e.hasSummaryItem() && e.isSummaryItem()) section.triple( "fhir:" + tn + "." + en, "fhir:hasFlag", complex().predicate("a", "fhir:isSummaryItem")); if (!Utilities.noString(e.getW5())) section.triple( "fhir:" + tn + "." + en, "fhir:w5", complex().predicate("a", "fhir:w5\\#" + e.getW5())); if (e.hasMeaningWhenMissing()) section.triple( "fhir:" + tn + "." + en, "fhir:missingMeaning", literal(e.getMeaningWhenMissing())); // cardinality cardinality( section, "fhir:" + tn + "." + en, e.getMinCardinality().toString(), e.getMaxCardinality() == Integer.MAX_VALUE ? "*" : e.getMaxCardinality().toString()); // section.triple("fhir:"+tn+"."+en, "fhir:minCardinality", // literal(e.getMinCardinality().toString())); // section.triple("fhir:"+tn+"."+en, "fhir:maxCardinality", literal(e.getMaxCardinality() == // Integer.MAX_VALUE ? "*" : e.getMaxCardinality().toString())); // now in OWL: if (e.getMinCardinality() > 0) section.triple( "fhir:" + tn, "rdfs:subClassOf", complex() .predicate("a", "owl:Restriction") .predicate("owl:onProperty", "fhir:" + tn + "." + en) .predicate( "owl:minCardinality", literalTyped(e.getMinCardinality().toString(), "nonNegativeInteger"))); if (e.getMaxCardinality() < Integer.MAX_VALUE) section.triple( "fhir:" + tn, "rdfs:subClassOf", complex() .predicate("a", "owl:Restriction") .predicate("owl:onProperty", "fhir:" + tn + "." + en) .predicate( "owl:maxCardinality", literalTyped(e.getMaxCardinality().toString(), "nonNegativeInteger"))); // define if (tr == null) { section.triple("fhir:" + tn + "." + en, "rdfs:range", "fhir:" + e.getDeclaredTypeName()); anonTypes.push(new AnonTypeInfo(section, e.getDeclaredTypeName(), e, datatype)); } else if (tr.getName().startsWith("@")) { ElementDefn r = getElementForPath(tr.getName().substring(1)); section.triple("fhir:" + tn + "." + en, "rdfs:range", "fhir:" + r.getDeclaredTypeName()); } else { if (e.hasBinding()) { BindingSpecification bs = e.getBinding(); if (bs.getValueSet() != null) { String bn = getPNameForUri(bs.getValueSet().getUrl()); section.triple("fhir:" + tn + "." + en, "rdfs:range", processType(tr.getName())); section.triple("fhir:" + tn + "." + en, "fhir:binding", bn); if (!bn.startsWith("vs:")) // a v3 valueset valuesets.put(bn, bs.getValueSet()); } else if (!Utilities.noString(bs.getReference())) { section.triple("fhir:" + tn + "." + en, "rdfs:range", processType(tr.getName())); section.triple("fhir:" + tn + "." + en, "fhir:binding", "<" + bs.getReference() + ">"); } // if (bs.hasMax()) // need to figure out how this should be represented in Turtle section.triple( "fhir:" + tn + "." + en, "fhir:bindingStrength", complex().predicate("a", "fhir:binding-strength\\#" + bs.getStrength().toCode())); } else section.triple("fhir:" + tn + "." + en, "rdfs:range", processType(tr.getName())); } if (e.getDefaultValue() != null) { if (e.getDefaultValue() instanceof DecimalType) section.triple( "fhir:" + tn + "." + en, "fhir:default", complex() .predicate("a", "fhir:decimal") .predicate( "fhir:value", literal(((DecimalType) e.getDefaultValue()).asStringValue()))); else if (e.getDefaultValue() instanceof BooleanType) section.triple( "fhir:" + tn + "." + en, "fhir:default", complex() .predicate("a", "fhir:boolean") .predicate( "fhir:value", literal(((BooleanType) e.getDefaultValue()).asStringValue()))); else if (e.getDefaultValue() instanceof IntegerType) section.triple( "fhir:" + tn + "." + en, "fhir:default", complex() .predicate("a", "fhir:integer") .predicate( "fhir:value", literal(((IntegerType) e.getDefaultValue()).asStringValue()))); else if (e.getDefaultValue() instanceof CodeType) section.triple( "fhir:" + tn + "." + en, "fhir:default", complex() .predicate("a", "fhir:integer") .predicate( "fhir:value", literal(((CodeType) e.getDefaultValue()).asStringValue()))); else throw new Error( "default of type " + e.getDefaultValue().getClass().getName() + " not handled yet"); } }
private void gen(TypeDefn t) throws Exception { Section section = section(t.getName()); if (t.getTypes().isEmpty()) section.triple("fhir:" + t.getName(), "a", "rdfs:Class"); else section.triple("fhir:" + t.getName(), "rdfs:subClassOf", "fhir:Element"); section.label("fhir:" + t.getName(), t.getShortDefn()); section.comment("fhir:" + t.getName(), t.getDefinition()); if (t.getName().equals("Reference")) section.triple( "fhir:" + t.getName(), "a", "fhir:Resource"); // This is so that a reference can be replaced by a direct reference to // it's target nilInstance.predicate("a", "fhir:" + t.getName()); processMappings(section, "fhir:" + t.getName(), t); for (ElementDefn e : t.getElements()) { if (e.getName().endsWith("[x]")) { String cn = e.getName().substring(0, e.getName().length() - 3); section.triple("fhir:" + t.getName() + "." + cn, "a", "rdf:Property"); section.label("fhir:" + t.getName() + "." + cn, e.getShortDefn()); section.comment("fhir:" + t.getName() + "." + cn, e.getDefinition()); processMappings(section, "fhir:" + t.getName() + "." + cn, e); for (TypeRef tr : e.typeCode().equals("*") ? getAnyTypes() : e.getTypes()) { String en = cn + Utilities.capitalize(tr.getName()); section.triple( "fhir:" + t.getName() + "." + en, "rdfs:subPropertyOf", "fhir:" + t.getName() + "." + cn); section.triple("fhir:" + t.getName() + "." + en, "rdfs:domain", "fhir:" + t.getName()); genRange(section, t.getName(), en, e, tr, true); } } else { section.triple("fhir:" + t.getName() + "." + e.getName(), "a", "rdf:Property"); section.comment("fhir:" + t.getName() + "." + e.getName(), e.getDefinition()); section.triple( "fhir:" + t.getName() + "." + e.getName(), "rdfs:domain", "fhir:" + t.getName()); processMappings(section, "fhir:" + t.getName() + "." + e.getName(), e); genRange( section, t.getName(), e.getName(), e, e.getTypes().isEmpty() ? null : e.getTypes().get(0), true); } } processAnonTypes(); }