Search in sources :

Example 71 with ResourceDefn

use of org.hl7.fhir.definitions.model.ResourceDefn in project kindling by HL7.

the class ProfileGenerator method generate.

public StructureDefinition generate(Profile pack, ResourceDefn r, String usage, boolean logical) throws Exception {
    StructureDefinition p = new StructureDefinition();
    p.setId(r.getRoot().getName());
    p.setUrl("http://hl7.org/fhir/StructureDefinition/" + r.getRoot().getName());
    if (logical)
        p.setKind(StructureDefinitionKind.LOGICAL);
    else
        p.setKind(StructureDefinitionKind.RESOURCE);
    if (r.isInterface()) {
        ToolingExtensions.addBooleanExtension(p, ToolingExtensions.EXT_RESOURCE_INTERFACE, true);
    }
    IniFile cini = new IniFile(Utilities.path(rootFolder, "temp", "categories.ini"));
    String cat = cini.getStringProperty("category", r.getName());
    if (!Utilities.noString(cat))
        ToolingExtensions.setStringExtension(p, ToolingExtensions.EXT_RESOURCE_CATEGORY, cat);
    p.setAbstract(r.isAbstract());
    assert !Utilities.noString(r.getRoot().typeCode());
    if (!Utilities.noString(r.getRoot().typeCode())) {
        p.setBaseDefinition("http://hl7.org/fhir/StructureDefinition/" + r.getRoot().typeCode());
        p.setDerivation(TypeDerivationRule.SPECIALIZATION);
    // if (r.getTemplate() != null)
    // ToolingExtensions.addStringExtension(p.getBaseDefinitionElement(), ToolingExtensions.EXT_CODE_GENERATION_PARENT, r.getTemplate().getName());
    }
    p.setType(r.getRoot().getName());
    p.setUserData("filename", r.getName().toLowerCase());
    p.setUserData("path", r.getName().toLowerCase() + ".html");
    p.setTitle(pack.metadata("display"));
    p.setFhirVersion(version);
    p.setVersion(version.toCode());
    ToolingExtensions.setStandardsStatus(p, r.getStatus(), r.getNormativeVersion());
    if (r.getFmmLevel() != null)
        ToolingExtensions.addIntegerExtension(p, ToolingExtensions.EXT_FMM_LEVEL, Integer.parseInt(r.getFmmLevel()));
    if (r.getSecurityCategorization() != null)
        ToolingExtensions.addCodeExtension(p, ToolingExtensions.EXT_SEC_CAT, r.getSecurityCategorization().toCode());
    ToolResourceUtilities.updateUsage(p, usage);
    p.setName(r.getRoot().getName());
    p.setPublisher("Health Level Seven International" + (r.getWg() == null ? "" : " (" + r.getWg().getName() + ")"));
    p.addContact().getTelecom().add(Factory.newContactPoint(ContactPointSystem.URL, "http://hl7.org/fhir"));
    if (r.getWg() != null)
        p.addContact().getTelecom().add(Factory.newContactPoint(ContactPointSystem.URL, r.getWg().getUrl()));
    ToolingExtensions.setCodeExtension(p, ToolingExtensions.EXT_WORKGROUP, r.getWg().getCode());
    p.setDescription(r.getDefinition());
    p.setPurpose(r.getRoot().getRequirements());
    if (!p.hasPurpose())
        p.setPurpose(r.getRoot().getRequirements());
    p.setDate(genDate.getTime());
    // DSTU
    p.setStatus(r.getStatus() == 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(), r.getRoot(), r.getRoot().getName(), containedSlices, new ArrayList<ProfileGenerator.SliceHandle>(), SnapShotMode.None, true, "BackboneElement", r.getRoot().typeCode(), false);
    reset();
    // now. the snapshot'
    p.setSnapshot(new StructureDefinitionSnapshotComponent());
    defineElement(null, p, p.getSnapshot().getElement(), r.getRoot(), r.getRoot().getName(), containedSlices, new ArrayList<ProfileGenerator.SliceHandle>(), SnapShotMode.Resource, true, "BackboneElement", r.getRoot().typeCode(), true);
    for (ElementDefinition ed : p.getSnapshot().getElement()) if (!ed.hasBase() && !logical)
        generateElementDefinition(p, ed, getParent(ed, p.getSnapshot().getElement()));
    if (!logical && !r.isInterface()) {
        List<String> names = new ArrayList<String>();
        names.addAll(r.getSearchParams().keySet());
        Collections.sort(names);
        // 1st, non composites
        for (String pn : names) {
            SearchParameterDefn sp = r.getSearchParams().get(pn);
            if (sp.getType() != SearchType.composite)
                pack.getSearchParameters().add(makeSearchParam(p, r.getName() + "-" + pn.replace("_", ""), r.getName(), sp, r));
        }
        for (String pn : names) {
            SearchParameterDefn sp = r.getSearchParams().get(pn);
            if (sp.getType() == SearchType.composite)
                pack.getSearchParameters().add(makeSearchParam(p, r.getName() + "-" + pn.replace("_", ""), r.getName(), sp, r));
        }
    }
    containedSlices.clear();
    addElementConstraintToSnapshot(p);
    p.getDifferential().getElement().get(0).getType().clear();
    p.getSnapshot().getElement().get(0).getType().clear();
    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 : IniFile(org.hl7.fhir.utilities.IniFile) SearchParameterDefn(org.hl7.fhir.definitions.model.SearchParameterDefn) ArrayList(java.util.ArrayList) XhtmlNode(org.hl7.fhir.utilities.xhtml.XhtmlNode) 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)

Example 72 with ResourceDefn

use of org.hl7.fhir.definitions.model.ResourceDefn in project kindling by HL7.

the class ProfileGenerator method generate.

public OperationDefinition generate(String name, String id, String resourceName, Operation op, ResourceDefn rd) throws Exception {
    OperationDefinition opd = new OperationDefinition();
    op.setResource(opd);
    if (Utilities.noString(op.getFmm()))
        ToolingExtensions.addIntegerExtension(opd, ToolingExtensions.EXT_FMM_LEVEL, Integer.parseInt(rd.getFmmLevel()));
    else
        ToolingExtensions.addIntegerExtension(opd, ToolingExtensions.EXT_FMM_LEVEL, Integer.parseInt(op.getFmm()));
    ToolingExtensions.setStandardsStatus(opd, op.getStandardsStatus() == null ? rd.getStatus() : op.getStandardsStatus(), op.getNormativeVersion());
    opd.setId(FormatUtilities.makeId(id));
    opd.setUrl("http://hl7.org/fhir/OperationDefinition/" + id);
    opd.setName(op.getName());
    opd.setTitle(op.getTitle());
    opd.setVersion(version.toCode());
    opd.setPublisher("HL7 (FHIR Project)");
    opd.addContact().getTelecom().add(org.hl7.fhir.r5.model.Factory.newContactPoint(ContactPointSystem.URL, "http://hl7.org/fhir"));
    opd.getContact().get(0).getTelecom().add(org.hl7.fhir.r5.model.Factory.newContactPoint(ContactPointSystem.EMAIL, "fhir@lists.hl7.org"));
    opd.setDescription(preProcessMarkdown(op.getDoco(), "Operation Documentation"));
    opd.setStatus(op.getStandardsStatus() == StandardsStatus.NORMATIVE ? PublicationStatus.ACTIVE : PublicationStatus.DRAFT);
    opd.setDate(genDate.getTime());
    if (op.getKind().toLowerCase().equals("operation"))
        opd.setKind(OperationKind.OPERATION);
    else if (op.getKind().toLowerCase().equals("query"))
        opd.setKind(OperationKind.QUERY);
    else {
        throw new Exception("Unrecognized operation kind: '" + op.getKind() + "' for operation " + name);
    }
    opd.setCode(op.getName());
    opd.setComment(preProcessMarkdown(op.getFooter(), "Operation Comments"));
    opd.setSystem(op.isSystem());
    opd.addResource(resourceName);
    opd.setType(op.isType());
    opd.setInstance(op.isInstance());
    if (op.getIdempotent() == null) {
        throw new Error("Operation " + opd.getId() + " is not marked as Idempotent or not");
    } else {
        opd.setAffectsState(!op.getIdempotent());
    }
    for (OperationParameter p : op.getParameters()) {
        produceOpParam(op.getName(), opd.getParameter(), p, null);
    }
    OperationDefinitionRenderer opr = new OperationDefinitionRenderer(rc);
    opr.render(opd);
    return opd;
}
Also used : OperationDefinitionRenderer(org.hl7.fhir.r5.renderers.OperationDefinitionRenderer) OperationParameter(org.hl7.fhir.definitions.model.OperationParameter) FHIRFormatError(org.hl7.fhir.exceptions.FHIRFormatError) OperationDefinition(org.hl7.fhir.r5.model.OperationDefinition) FHIRException(org.hl7.fhir.exceptions.FHIRException) URISyntaxException(java.net.URISyntaxException)

Example 73 with ResourceDefn

use of org.hl7.fhir.definitions.model.ResourceDefn in project kindling by HL7.

the class SchemaGenerator method generate.

public void generate(Definitions definitions, IniFile ini, String tmpResDir, String xsdDir, String dstDir, String srcDir, String version, String genDate, BuildWorkerContext workerContext) throws Exception {
    this.genDate = genDate;
    this.version = version;
    this.workerContext = workerContext;
    File dir = new CSFile(xsdDir);
    File[] list = dir.listFiles();
    if (list != null) {
        for (File f : list) {
            if (!f.isDirectory() && f.getName().endsWith(".schema.json"))
                f.delete();
        }
    }
    JsonObject schema = new JsonObject();
    schema.addProperty("$schema", "http://json-schema.org/draft-06/schema#");
    schema.addProperty("id", "http://hl7.org/fhir/json-schema/" + version.substring(0, version.lastIndexOf(".")));
    // schema.addProperty("$ref", "#/definitions/ResourceList");
    schema.addProperty("description", "see http://hl7.org/fhir/json.html#schema for information about the FHIR Json Schemas");
    List<String> names = new ArrayList<String>();
    names.addAll(definitions.getResources().keySet());
    names.addAll(definitions.getBaseResources().keySet());
    addAllResourcesChoice(definitions, schema, names);
    names.clear();
    names.addAll(definitions.getPrimitives().keySet());
    Collections.sort(names);
    for (String n : names) {
        new JsonGenerator(definitions, workerContext, definitions.getKnownTypes(), version).generate(definitions.getPrimitives().get(n), version, genDate, schema);
    }
    new JsonGenerator(definitions, workerContext, definitions.getKnownTypes(), version).generate(new DefinedCode().setCode("xhtml").setDefinition("xhtml - escaped html (see specfication)"), version, genDate, schema);
    for (TypeRef tr : definitions.getKnownTypes()) {
        if (!definitions.getPrimitives().containsKey(tr.getName()) && !definitions.getConstraints().containsKey(tr.getName())) {
            TypeDefn root = definitions.getElementDefn(tr.getName());
            if (!isBackboneElement(root.getName())) {
                JsonObject s = new JsonGenerator(definitions, workerContext, definitions.getKnownTypes(), version).generate(root, version, genDate, null);
                save(s, tmpResDir + root.getName().replace(".", "_") + ".schema.json");
                new JsonGenerator(definitions, workerContext, definitions.getKnownTypes(), version).generate(root, version, genDate, schema);
            }
        }
    }
    names.clear();
    names.addAll(definitions.getResources().keySet());
    names.addAll(definitions.getBaseResources().keySet());
    Collections.sort(names);
    for (String name : names) {
        ResourceDefn root = definitions.getResourceByName(name);
        JsonObject s = new JsonGenerator(definitions, workerContext, definitions.getKnownTypes(), version).generate(root.getRoot(), version, genDate, null);
        save(s, tmpResDir + root.getName().replace(".", "_") + ".schema.json");
        if (!root.isAbstract()) {
            new JsonGenerator(definitions, workerContext, definitions.getKnownTypes(), version).generate(root.getRoot(), version, genDate, schema);
        }
    }
    // save(generateAllResourceChoice(names), xsdDir+"ResourceList.schema.json");
    save(schema, xsdDir + "fhir.schema.json");
    dir = new CSFile(xsdDir);
    list = dir.listFiles();
    for (File f : list) {
        if (!f.isDirectory() && f.getName().endsWith(".schema.json"))
            Utilities.copyFile(f, new CSFile(dstDir + f.getName()));
    }
}
Also used : TypeDefn(org.hl7.fhir.definitions.model.TypeDefn) DefinedCode(org.hl7.fhir.definitions.model.DefinedCode) TypeRef(org.hl7.fhir.definitions.model.TypeRef) ArrayList(java.util.ArrayList) JsonObject(com.google.gson.JsonObject) CSFile(org.hl7.fhir.utilities.CSFile) CSFile(org.hl7.fhir.utilities.CSFile) File(java.io.File) IniFile(org.hl7.fhir.utilities.IniFile) TextFile(org.hl7.fhir.utilities.TextFile) ResourceDefn(org.hl7.fhir.definitions.model.ResourceDefn)

Example 74 with ResourceDefn

use of org.hl7.fhir.definitions.model.ResourceDefn in project kindling by HL7.

the class SchemaGenerator method generate.

public void generate(Definitions definitions, IniFile ini, String tmpResDir, String xsdDir, String dstDir, String srcDir, String version, String genDate, boolean forCodeGeneration, BuildWorkerContext workerContext) throws Exception {
    this.genDate = genDate;
    this.version = version;
    this.workerContext = workerContext;
    if (!forCodeGeneration) {
        File dir = new CSFile(xsdDir);
        File[] list = dir.listFiles();
        if (list != null) {
            for (File f : list) {
                if (!f.isDirectory())
                    f.delete();
            }
        }
    }
    XSDBaseGenerator xsdb = new XSDBaseGenerator(new OutputStreamWriter(new FileOutputStream(new CSFile(xsdDir + "fhir-base.xsd")), "UTF-8"), forCodeGeneration, workerContext, new HashSet<>(), version);
    xsdb.setDefinitions(definitions);
    xsdb.generate(version, genDate, true);
    xsdb.getWriter().close();
    Set<String> allenums = new HashSet<String>();
    List<String> names = new ArrayList<String>();
    names.addAll(definitions.getResources().keySet());
    names.add("Parameters");
    Collections.sort(names);
    for (String name : names) {
        ResourceDefn root = definitions.getResourceByName(name);
        XSDGenerator sgen = new XSDGenerator(new OutputStreamWriter(new FileOutputStream(new CSFile(xsdDir + root.getName().toLowerCase() + ".xsd")), "UTF-8"), definitions, forCodeGeneration, workerContext, allenums, version);
        sgen.setDataTypes(definitions.getKnownTypes());
        sgen.generate(root.getRoot(), version, genDate, true);
        sgen.getWriter().close();
    }
    OutputStreamWriter single = new OutputStreamWriter(new FileOutputStream(new CSFile(xsdDir + "fhir-single.xsd")), "UTF-8");
    single.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n");
    single.write("<!-- \r\n");
    single.write(Config.FULL_LICENSE_CODE);
    single.write("\r\n");
    single.write("  Generated on " + genDate + " for FHIR v" + version + " \r\n");
    single.write("-->\r\n");
    single.write("<xs:schema xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" xmlns=\"http://hl7.org/fhir\" xmlns:xhtml=\"http://www.w3.org/1999/xhtml\" " + "xmlns:xml=\"http://www.w3.org/XML/1998/namespace\" targetNamespace=\"http://hl7.org/fhir\" elementFormDefault=\"qualified\" version=\"" + version + "\">\r\n");
    single.write("  <!-- Note: When using this schema with some tools, it may also be necessary to declare xmlns:xml=\"http://www.w3.org/XML/1998/namespace\", however this causes performance issues with other tools and thus is not in the base schemas. -->\r\n");
    allenums = new HashSet<String>();
    xsdb = new XSDBaseGenerator(single, forCodeGeneration, workerContext, allenums, version);
    xsdb.setDefinitions(definitions);
    xsdb.generate(version, genDate, false);
    // 
    for (String name : names) {
        ResourceDefn root = definitions.getResourceByName(name);
        XSDGenerator sgen = new XSDGenerator(single, definitions, forCodeGeneration, workerContext, allenums, version);
        sgen.setDataTypes(definitions.getKnownTypes());
        sgen.generate(root.getRoot(), version, genDate, false);
    }
    single.write("</xs:schema>\r\n");
    single.flush();
    single.close();
    for (String n : ini.getPropertyNames("schema")) {
        String xsd = TextFile.fileToString(srcDir + n);
        xsd = processSchemaIncludes(definitions, n, xsd, false);
        TextFile.stringToFile(xsd, xsdDir + n);
    }
    produceCombinedSchema(definitions, xsdDir, dstDir, srcDir);
    if (!forCodeGeneration) {
        File dir = new CSFile(xsdDir);
        File[] list = dir.listFiles();
        for (File f : list) {
            if (!f.isDirectory())
                Utilities.copyFile(f, new CSFile(dstDir + f.getName()));
        }
    }
}
Also used : ArrayList(java.util.ArrayList) CSFile(org.hl7.fhir.utilities.CSFile) ResourceDefn(org.hl7.fhir.definitions.model.ResourceDefn) FileOutputStream(java.io.FileOutputStream) OutputStreamWriter(java.io.OutputStreamWriter) CSFile(org.hl7.fhir.utilities.CSFile) File(java.io.File) IniFile(org.hl7.fhir.utilities.IniFile) TextFile(org.hl7.fhir.utilities.TextFile) HashSet(java.util.HashSet)

Example 75 with ResourceDefn

use of org.hl7.fhir.definitions.model.ResourceDefn in project kindling by HL7.

the class ResourceDependencyGenerator method addTypeToAnalysis.

private void addTypeToAnalysis(HierarchicalTableGenerator gen, Row row, Cell dc, boolean ref, StandardsStatus elementStatus, String type) throws Exception {
    String tgtFMM = null;
    StandardsStatus tgtSS = null;
    if (definitions.getConstraints().containsKey(type))
        type = definitions.getConstraints().get(type).getBaseType();
    if (definitions.hasResource(type)) {
        ResourceDefn r = definitions.getResourceByName(type);
        tgtFMM = r.getFmmLevel();
        tgtSS = r.getStatus();
    } else if (definitions.getBaseResources().containsKey(type)) {
        ResourceDefn r = definitions.getBaseResources().get(type);
        tgtFMM = r.getFmmLevel();
        tgtSS = r.getStatus();
    } else if ("Any".equals(type)) {
        tgtFMM = "1";
        tgtSS = StandardsStatus.TRIAL_USE;
    } else if (definitions.hasPrimitiveType(type)) {
        tgtFMM = "5";
        tgtSS = StandardsStatus.NORMATIVE;
    } else if ("*".equals(type)) {
        // todo: what...?
        tgtFMM = "2";
        tgtSS = StandardsStatus.TRIAL_USE;
    } else {
        TypeDefn t = definitions.getElementDefn(type);
        if (t != null) {
            tgtFMM = t.getFmmLevel();
            tgtSS = t.getStandardsStatus();
        }
    }
    if (elementStatus == null)
        elementStatus = sstatus;
    if (tgtFMM == null)
        addError(gen, row, dc, "Error: Unable to resolve type '" + type + "' to check dependencies", null);
    else {
        boolean ok = elementStatus.canDependOn(tgtSS);
        if (ok)
            ok = fmm.compareTo(tgtFMM) <= 0;
        if (ok)
            // addInfo(gen, row, dc, "OK ("+type+" = FMM"+tgtFMM+"-"+tgtSS.toDisplay()+" vs. Element = FMM"+fmm+"-"+elementStatus.toDisplay()+")", null);
            ;
        else if (ref)
            addWarning(gen, row, dc, "Type Warning: (" + type + " = FMM" + tgtFMM + "-" + (tgtSS == null ? "null" : tgtSS.toDisplay()) + " vs. Element = FMM" + fmm + "-" + elementStatus.toDisplay() + ")", null);
        else
            addError(gen, row, dc, "Type Error: (" + type + " = FMM" + tgtFMM + "-" + tgtSS.toDisplay() + " vs. Element = FMM" + fmm + "-" + elementStatus.toDisplay() + ")", null);
    }
}
Also used : TypeDefn(org.hl7.fhir.definitions.model.TypeDefn) StandardsStatus(org.hl7.fhir.utilities.StandardsStatus) ResourceDefn(org.hl7.fhir.definitions.model.ResourceDefn)

Aggregations

ResourceDefn (org.hl7.fhir.definitions.model.ResourceDefn)75 CommaSeparatedStringBuilder (org.hl7.fhir.utilities.CommaSeparatedStringBuilder)42 ArrayList (java.util.ArrayList)38 FHIRException (org.hl7.fhir.exceptions.FHIRException)36 ElementDefn (org.hl7.fhir.definitions.model.ElementDefn)31 StructureDefinition (org.hl7.fhir.r5.model.StructureDefinition)28 File (java.io.File)26 FileOutputStream (java.io.FileOutputStream)25 CSFile (org.hl7.fhir.utilities.CSFile)24 XmlParser (org.hl7.fhir.r5.formats.XmlParser)22 FileNotFoundException (java.io.FileNotFoundException)20 IOException (java.io.IOException)20 Example (org.hl7.fhir.definitions.model.Example)20 Profile (org.hl7.fhir.definitions.model.Profile)20 ContactPoint (org.hl7.fhir.r5.model.ContactPoint)18 IniFile (org.hl7.fhir.utilities.IniFile)18 ImplementationGuideDefn (org.hl7.fhir.definitions.model.ImplementationGuideDefn)16 TransformerException (javax.xml.transform.TransformerException)15 TextFile (org.hl7.fhir.utilities.TextFile)15 SearchParameterDefn (org.hl7.fhir.definitions.model.SearchParameterDefn)14