Search in sources :

Example 6 with BackboneElement

use of org.hl7.fhir.r5.model.BackboneElement in project kindling by HL7.

the class TurtleSpecGenerator method generateInner.

private void generateInner(ElementDefn root, boolean resource, boolean isAbstract) throws IOException, Exception {
    String rn;
    if (root.getName().equals("Extension"))
        rn = "extension|modifierExtension";
    else if (root.getName().equals("Meta"))
        rn = "meta";
    else if (root.getTypes().size() > 0 && (root.getTypes().get(0).getName().equals("Type") || (root.getTypes().get(0).getName().equals("Structure"))) || isAbstract)
        rn = "[name]";
    else
        rn = root.getName();
    write("@prefix fhir: <http://hl7.org/fhir/> .");
    if (resource)
        write("<span style=\"float: right\"><a title=\"Documentation for this format\" href=\"" + prefix + "rdf.html\"><img src=\"" + prefix + "help.png\" alt=\"doco\"/></a></span>\r\n");
    write("\r\n");
    write("\r\n");
    if (resource) {
        write("[ a fhir:");
        if (defPage == null)
            write("<span title=\"" + Utilities.escapeXml(root.getDefinition()) + "\"><b>");
        else
            write("<a href=\"" + (defPage + "#" + root.getName()) + "\" title=\"" + Utilities.escapeXml(root.getDefinition()) + "\" class=\"dict\"><b>");
        write(rn);
        if ((defPage == null))
            write("</b></span>;");
        else
            write("</b></a>;");
        write("\r\n  fhir:nodeRole fhir:treeRoot; # if this is the parser root\r\n");
    } else
        write("[");
    write("\r\n");
    if (rn.equals(root.getName()) && resource) {
        if (!Utilities.noString(root.typeCode())) {
            write("  # from <a href=\"" + prefix + "resource.html\">Resource</a>: <a href=\"" + prefix + "resource.html#id\">.id</a>, <a href=\"" + prefix + "resource.html#meta\">.meta</a>, <a href=\"" + prefix + "resource.html#implicitRules\">.implicitRules</a>, and <a href=\"" + prefix + "resource.html#language\">.language</a>\r\n");
            if (root.typeCode().equals("DomainResource"))
                write("  # from <a href=\"" + prefix + "domainresource.html\">DomainResource</a>: <a href=\"" + prefix + "narrative.html#Narrative\">.text</a>, <a href=\"" + prefix + "references.html#contained\">.contained</a>, <a href=\"" + prefix + "extensibility.html\">.extension</a>, and <a href=\"" + prefix + "extensibility.html#modifierExtension\">.modifierExtension</a>\r\n");
        }
    } else {
        if (root.typeCode().equals("BackboneElement"))
            write(" # from BackboneElement: <a href=\"" + prefix + "extensibility.html\">Element.extension</a>, <a href=\"" + prefix + "extensibility.html\">BackboneElement.modifierextension</a>\r\n");
        else
            write(" # from Element: <a href=\"" + prefix + "extensibility.html\">Element.extension</a>\r\n");
    }
    for (ElementDefn elem : root.getElements()) {
        generateCoreElem(elem, 1, root.getName(), rn.equals(root.getName()) && resource);
    }
    write("]\r\n");
}
Also used : ElementDefn(org.hl7.fhir.definitions.model.ElementDefn)

Example 7 with BackboneElement

use of org.hl7.fhir.r5.model.BackboneElement in project kindling by HL7.

the class XSDGenerator method generateType.

private void generateType(ElementDefn root, String name, ElementDefn struc, boolean isResource) throws IOException, Exception {
    write("  <xs:complexType name=\"" + name + "\">\r\n");
    write("    <xs:annotation>\r\n");
    write("      <xs:documentation xml:lang=\"en\">" + Utilities.escapeXml(root.getDefinition()) + "</xs:documentation>\r\n");
    if (isResource)
        write("      <xs:documentation xml:lang=\"en\">If the element is present, it must have either a @value, an @id, or extensions</xs:documentation>\r\n");
    write("    </xs:annotation>\r\n");
    write("    <xs:complexContent>\r\n");
    if (isResource)
        write("      <xs:extension base=\"" + root.typeCode() + "\">\r\n");
    else
        write("      <xs:extension base=\"BackboneElement\">\r\n");
    write("        <xs:sequence>\r\n");
    for (ElementDefn e : struc.getElements()) {
        if (e.getName().equals("[type]"))
            generateAny(root, e);
        else
            generateElement(root, e);
    }
    write("        </xs:sequence>\r\n");
    write("      </xs:extension>\r\n");
    write("    </xs:complexContent>\r\n");
    write("  </xs:complexType>\r\n");
}
Also used : ElementDefn(org.hl7.fhir.definitions.model.ElementDefn)

Example 8 with BackboneElement

use of org.hl7.fhir.r5.model.BackboneElement in project kindling by HL7.

the class ProfileGenerator method generate.

public StructureDefinition generate(TypeDefn t) throws Exception {
    genUml(t);
    StructureDefinition p = new StructureDefinition();
    p.setId(t.getName());
    p.setUrl("http://hl7.org/fhir/StructureDefinition/" + t.getName());
    p.setKind(StructureDefinitionKind.COMPLEXTYPE);
    p.setAbstract(t.isAbstractType());
    p.setUserData("filename", t.getName().toLowerCase());
    p.setUserData("path", definitions.getSrcFile(t.getName()) + ".html#" + t.getName());
    assert !Utilities.noString(t.typeCode());
    String b = (t.typeCode().equals("Type") ? "Element" : t.typeCode().equals("Structure") ? "BackboneElement" : t.typeCode());
    if (!Utilities.noString(b)) {
        p.setBaseDefinition("http://hl7.org/fhir/StructureDefinition/" + b);
        p.setDerivation(TypeDerivationRule.SPECIALIZATION);
    }
    p.setType(t.getName());
    p.setFhirVersion(version);
    p.setVersion(version.toCode());
    ToolingExtensions.setStandardsStatus(p, t.getStandardsStatus(), t.getNormativeVersion());
    ToolResourceUtilities.updateUsage(p, "core");
    p.setName(t.getName());
    p.setPublisher("HL7 FHIR Standard");
    p.addContact().getTelecom().add(Factory.newContactPoint(ContactPointSystem.URL, "http://hl7.org/fhir"));
    p.setDescription("Base StructureDefinition for " + t.getName() + " Type: " + t.getDefinition());
    p.setPurpose(t.getRequirements());
    p.setDate(genDate.getTime());
    p.setStatus(t.getStandardsStatus() == StandardsStatus.NORMATIVE ? PublicationStatus.fromCode("active") : PublicationStatus.fromCode("draft"));
    Set<String> containedSlices = new HashSet<String>();
    // first, the differential
    p.setDifferential(new StructureDefinitionDifferentialComponent());
    defineElement(null, p, p.getDifferential().getElement(), t, t.getName(), containedSlices, new ArrayList<ProfileGenerator.SliceHandle>(), SnapShotMode.None, true, "Element", b, false);
    p.getDifferential().getElement().get(0).setIsSummaryElement(null);
    reset();
    // now. the snapshot
    p.setSnapshot(new StructureDefinitionSnapshotComponent());
    defineElement(null, p, p.getSnapshot().getElement(), t, t.getName(), containedSlices, new ArrayList<ProfileGenerator.SliceHandle>(), SnapShotMode.DataType, true, "Element", b, true);
    for (ElementDefinition ed : p.getSnapshot().getElement()) if (ed.getBase().getPath().equals(ed.getPath()) && ed.getPath().contains("."))
        generateElementDefinition(p, ed, getParent(ed, p.getSnapshot().getElement()));
    containedSlices.clear();
    addElementConstraintToSnapshot(p);
    p.getDifferential().getElement().get(0).getType().clear();
    p.getSnapshot().getElement().get(0).getType().clear();
    p.getSnapshot().getElement().get(0).setIsSummaryElement(null);
    XhtmlNode div = new XhtmlNode(NodeType.Element, "div");
    div.addText("to do");
    p.setText(new Narrative());
    p.getText().setStatus(NarrativeStatus.GENERATED);
    p.getText().setDiv(div);
    checkHasTypes(p);
    return p;
}
Also used : StructureDefinition(org.hl7.fhir.r5.model.StructureDefinition) Narrative(org.hl7.fhir.r5.model.Narrative) StructureDefinitionSnapshotComponent(org.hl7.fhir.r5.model.StructureDefinition.StructureDefinitionSnapshotComponent) StructureDefinitionDifferentialComponent(org.hl7.fhir.r5.model.StructureDefinition.StructureDefinitionDifferentialComponent) ElementDefinition(org.hl7.fhir.r5.model.ElementDefinition) HashSet(java.util.HashSet) XhtmlNode(org.hl7.fhir.utilities.xhtml.XhtmlNode)

Example 9 with BackboneElement

use of org.hl7.fhir.r5.model.BackboneElement in project kindling by HL7.

the class FhirTurtleGenerator method processTypes.

private void processTypes(String baseResourceName, FHIRResource baseResource, ElementDefn td, String predicateBase, boolean innerIsBackbone) throws Exception {
    for (ElementDefn ed : td.getElements()) {
        String predicateName = predicateBase + "." + (ed.getName().endsWith("[x]") ? ed.getName().substring(0, ed.getName().length() - 3) : ed.getName());
        FHIRResource predicateResource;
        if (ed.getName().endsWith("[x]")) {
            predicateResource = fact.fhir_objectProperty(predicateName);
            // Choice entry
            if (ed.typeCode().equals("*")) {
                // Wild card -- any element works (probably should be more restrictive but...)
                Resource targetResource = RDFNamespace.FHIR.resourceRef("Element");
                baseResource.restriction(fact.fhir_cardinality_restriction(predicateResource.resource, targetResource, ed.getMinCardinality(), ed.getMaxCardinality()));
                predicateResource.domain(baseResource);
                predicateResource.range(targetResource);
            } else {
                // Create a restriction on the union of possible types
                List<Resource> typeOpts = new ArrayList<Resource>();
                for (TypeRef tr : ed.getTypes()) {
                    // TODO: Figure out how to get the type reference code
                    String trName = tr.getName();
                    if (trName.equals("SimpleQuantity"))
                        trName = "Quantity";
                    String qualifiedPredicateName = predicateName + Utilities.capitalize(trName);
                    Resource targetRes = fact.fhir_class(tr.getName()).resource;
                    FHIRResource qualifiedPredicate = fact.fhir_objectProperty(qualifiedPredicateName, predicateResource.resource).domain(baseResource).range(targetRes);
                    typeOpts.add(fact.fhir_cardinality_restriction(qualifiedPredicate.resource, targetRes, ed.getMinCardinality(), ed.getMaxCardinality()));
                }
                baseResource.restriction(fact.fhir_union(typeOpts));
            }
        } else {
            FHIRResource baseDef;
            if (ed.getTypes().isEmpty()) {
                predicateResource = fact.fhir_objectProperty(predicateName);
                String targetClassName = mapComponentName(baseResourceName, ed.getDeclaredTypeName());
                baseDef = fact.fhir_class(targetClassName, innerIsBackbone ? "BackboneElement" : "Element").addDefinition(ed.getDefinition());
                processTypes(targetClassName, baseDef, ed, predicateName, innerIsBackbone);
            } else {
                TypeRef targetType = ed.getTypes().get(0);
                String targetName = targetType.getName();
                if (targetName.startsWith("@")) {
                    // Link to earlier definition
                    ElementDefn targetRef = getElementForPath(targetName.substring(1));
                    String targetRefName = targetRef.getName();
                    String targetClassName = baseResourceName + Character.toUpperCase(targetRefName.charAt(0)) + targetRefName.substring(1);
                    baseDef = fact.fhir_class(targetClassName, innerIsBackbone ? "BackboneElement" : "Element").addDefinition(ed.getDefinition()).addTitle(ed.getShortDefn());
                    if (!processing.contains(targetRefName)) {
                        processing.add(targetRefName);
                        processTypes(targetClassName, baseDef, targetRef, predicateName, innerIsBackbone);
                        processing.remove(targetRefName);
                    }
                } else {
                    // A placeholder entry.  The rest of the information will be supplied elsewhere
                    baseDef = fact.fhir_class(targetName);
                }
                // XHTML the exception, in that the html doesn't derive from Primitive
                if (targetName.equals("xhtml"))
                    predicateResource = fact.fhir_dataProperty(predicateName);
                else
                    predicateResource = fact.fhir_objectProperty(predicateName);
            }
            predicateResource.addTitle(ed.getShortDefn()).addDefinition(ed.getDefinition()).domain(baseResource);
            baseResource.restriction(fact.fhir_cardinality_restriction(predicateResource.resource, baseDef.resource, ed.getMinCardinality(), ed.getMaxCardinality()));
            predicateResource.range(baseDef.resource);
            if (!Utilities.noString(ed.getW5()))
                predicateResource.addObjectProperty(RDFS.subPropertyOf, RDFNamespace.W5.resourceRef(ed.getW5()));
        }
    }
}
Also used : FHIRResource(org.hl7.fhir.rdf.FHIRResource) TypeRef(org.hl7.fhir.definitions.model.TypeRef) ElementDefn(org.hl7.fhir.definitions.model.ElementDefn) Resource(org.apache.jena.rdf.model.Resource) FHIRResource(org.hl7.fhir.rdf.FHIRResource) ArrayList(java.util.ArrayList)

Example 10 with BackboneElement

use of org.hl7.fhir.r5.model.BackboneElement in project org.hl7.fhir.core by hapifhir.

the class FHIRPathEngine method getChildTypesByName.

private void getChildTypesByName(String type, String name, TypeDetails result) throws PathEngineException, DefinitionException {
    if (Utilities.noString(type))
        throw new PathEngineException("No type provided in BuildToolPathEvaluator.getChildTypesByName");
    if (type.equals("xhtml"))
        return;
    String url = null;
    if (type.contains(".")) {
        url = "http://hl7.org/fhir/StructureDefinition/" + type.substring(0, type.indexOf("."));
    } else {
        url = "http://hl7.org/fhir/StructureDefinition/" + type;
    }
    String tail = "";
    StructureDefinition sd = worker.fetchResource(StructureDefinition.class, url);
    if (sd == null)
        // this really is an error, because we can only get to here if the internal infrastrucgture is wrong
        throw new DefinitionException("Unknown type " + type);
    List<StructureDefinition> sdl = new ArrayList<StructureDefinition>();
    ElementDefinitionMatch m = null;
    if (type.contains("."))
        m = getElementDefinition(sd, type, false);
    if (m != null && hasDataType(m.definition)) {
        if (m.fixedType != null) {
            StructureDefinition dt = worker.fetchTypeDefinition(m.fixedType);
            if (dt == null)
                throw new DefinitionException("unknown data type " + m.fixedType);
            sdl.add(dt);
        } else
            for (TypeRefComponent t : m.definition.getType()) {
                StructureDefinition dt = worker.fetchTypeDefinition(t.getCode());
                if (dt == null)
                    throw new DefinitionException("unknown data type " + t.getCode());
                sdl.add(dt);
            }
    } else {
        sdl.add(sd);
        if (type.contains("."))
            tail = type.substring(type.indexOf("."));
    }
    for (StructureDefinition sdi : sdl) {
        String path = sdi.getSnapshot().getElement().get(0).getPath() + tail + ".";
        if (name.equals("**")) {
            assert (result.getCollectionStatus() == CollectionStatus.UNORDERED);
            for (ElementDefinition ed : sdi.getSnapshot().getElement()) {
                if (ed.getPath().startsWith(path))
                    for (TypeRefComponent t : ed.getType()) {
                        if (t.hasCode() && t.getCodeElement().hasValue()) {
                            String tn = null;
                            if (t.getCode().equals("Element") || t.getCode().equals("BackboneElement"))
                                tn = ed.getPath();
                            else
                                tn = t.getCode();
                            if (t.getCode().equals("Resource")) {
                                for (String rn : worker.getResourceNames()) {
                                    if (!result.hasType(worker, rn)) {
                                        result.addType(rn);
                                        getChildTypesByName(rn, "**", result);
                                    }
                                }
                            } else if (!result.hasType(worker, tn)) {
                                result.addType(tn);
                                getChildTypesByName(tn, "**", result);
                            }
                        }
                    }
            }
        } else if (name.equals("*")) {
            assert (result.getCollectionStatus() == CollectionStatus.UNORDERED);
            for (ElementDefinition ed : sdi.getSnapshot().getElement()) {
                if (ed.getPath().startsWith(path) && !ed.getPath().substring(path.length()).contains("."))
                    for (TypeRefComponent t : ed.getType()) {
                        if (t.getCode().equals("Element") || t.getCode().equals("BackboneElement"))
                            result.addType(ed.getPath());
                        else if (t.getCode().equals("Resource"))
                            result.addTypes(worker.getResourceNames());
                        else
                            result.addType(t.getCode());
                    }
            }
        } else {
            path = sdi.getSnapshot().getElement().get(0).getPath() + tail + "." + name;
            ElementDefinitionMatch ed = getElementDefinition(sdi, path, false);
            if (ed != null) {
                if (!Utilities.noString(ed.getFixedType()))
                    result.addType(ed.getFixedType());
                else
                    for (TypeRefComponent t : ed.getDefinition().getType()) {
                        if (Utilities.noString(t.getCode()))
                            // throw new PathEngineException("Illegal reference to primitive value attribute @ "+path);
                            break;
                        if (t.getCode().equals("Element") || t.getCode().equals("BackboneElement"))
                            result.addType(path);
                        else if (t.getCode().equals("Resource"))
                            result.addTypes(worker.getResourceNames());
                        else
                            result.addType(t.getCode());
                    }
            }
        }
    }
}
Also used : StructureDefinition(org.hl7.fhir.dstu2.model.StructureDefinition) TypeRefComponent(org.hl7.fhir.dstu2.model.ElementDefinition.TypeRefComponent) ArrayList(java.util.ArrayList) DefinitionException(org.hl7.fhir.exceptions.DefinitionException) ElementDefinition(org.hl7.fhir.dstu2.model.ElementDefinition) PathEngineException(org.hl7.fhir.exceptions.PathEngineException)

Aggregations

ArrayList (java.util.ArrayList)18 ElementDefinition (org.hl7.fhir.r5.model.ElementDefinition)15 StructureDefinition (org.hl7.fhir.r5.model.StructureDefinition)12 ElementDefn (org.hl7.fhir.definitions.model.ElementDefn)11 FHIRException (org.hl7.fhir.exceptions.FHIRException)11 TypeRefComponent (org.hl7.fhir.r5.model.ElementDefinition.TypeRefComponent)11 List (java.util.List)8 TypeRef (org.hl7.fhir.definitions.model.TypeRef)8 DefinitionException (org.hl7.fhir.exceptions.DefinitionException)8 CanonicalType (org.hl7.fhir.r5.model.CanonicalType)8 HashSet (java.util.HashSet)6 PathEngineException (org.hl7.fhir.exceptions.PathEngineException)5 CommaSeparatedStringBuilder (org.hl7.fhir.utilities.CommaSeparatedStringBuilder)5 FHIRFormatError (org.hl7.fhir.exceptions.FHIRFormatError)4 Cell (org.hl7.fhir.utilities.xhtml.HierarchicalTableGenerator.Cell)4 Row (org.hl7.fhir.utilities.xhtml.HierarchicalTableGenerator.Row)4 ProfiledType (org.hl7.fhir.definitions.model.ProfiledType)3 ElementDefinition (org.hl7.fhir.r4.model.ElementDefinition)3 ElementDefinition (org.hl7.fhir.r4b.model.ElementDefinition)3 StructureDefinition (org.hl7.fhir.r4b.model.StructureDefinition)3