Search in sources :

Example 6 with Invariant

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

the class OldSpreadsheetParser method parseCommonTypeColumns.

private ResourceDefn parseCommonTypeColumns(boolean isResource, boolean isTemplate) throws Exception {
    ResourceDefn resource = new ResourceDefn();
    resource.setInterface(isTemplate);
    Sheet sheet = loadSheet("Bindings");
    if (sheet != null)
        readBindings(sheet);
    sheet = loadSheet("Invariants");
    Map<String, Invariant> invariants = null;
    if (sheet != null)
        invariants = readInvariants(sheet, title, "Invariants");
    sheet = loadSheet("Data Elements");
    if (sheet == null)
        throw new Exception("No Sheet found for Data Elements");
    for (int row = 0; row < sheet.rows.size(); row++) {
        processLine(resource, sheet, row, invariants, false, null, row == 0);
    }
    // default...
    StandardsStatus ss = StandardsStatus.TRIAL_USE;
    String s = ini.getStringProperty("normative", resource.getName());
    if (!Utilities.noString(s))
        ss = StandardsStatus.NORMATIVE;
    resource.setStatus(ss);
    resource.setRequirements(resource.getRoot().getRequirements());
    resource.addHints(checkIgnoredColumns(sheet));
    if (template != null) {
        resource.setTemplate(template.getRoot());
        copySearchParameters(resource);
        copyInvariants(resource);
        template = null;
    }
    parseMetadata(resource);
    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");
                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())) {
                // This has been disabled for now, per Lloyd McKenzie's request via Skype - jamesagnew
                // throw new Exception("Type "+resource.getRoot().getName()+" Invariant "+inv.getId()+" ("+inv.getEnglish()+") has no Expression statement (in FHIRPath format)");
                } else {
                    fpUsages.add(new FHIRPathUsage(inv.getContext(), isResource ? resource.getName() : "DomainResource", inv.getContext(), null, inv.getExpression(), inv.getXpath()));
                }
            }
        }
    }
    // EK: Future types. But those won't get there.
    if (bindings != null)
        resource.getRoot().getNestedBindings().putAll(bindings);
    scanNestedTypes(resource, resource.getRoot(), resource.getName());
    resolveElementReferences(resource, resource.getRoot());
    resource.getRoot().setAbstractType(isAbstract);
    return resource;
}
Also used : Invariant(org.hl7.fhir.definitions.model.Invariant) ElementDefn(org.hl7.fhir.definitions.model.ElementDefn) FHIRPathUsage(org.hl7.fhir.definitions.validation.FHIRPathUsage) ResourceDefn(org.hl7.fhir.definitions.model.ResourceDefn) Sheet(org.hl7.fhir.utilities.xls.XLSXmlParser.Sheet) StandardsStatus(org.hl7.fhir.utilities.StandardsStatus) FHIRException(org.hl7.fhir.exceptions.FHIRException)

Example 7 with Invariant

use of org.hl7.fhir.definitions.model.Invariant 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);
    }
}
Also used : Invariant(org.hl7.fhir.definitions.model.Invariant) ProfiledType(org.hl7.fhir.definitions.model.ProfiledType) TypeRef(org.hl7.fhir.definitions.model.TypeRef) XLSXmlParser(org.hl7.fhir.utilities.xls.XLSXmlParser) XLSXmlNormaliser(org.hl7.fhir.utilities.xls.XLSXmlNormaliser) CSFile(org.hl7.fhir.utilities.CSFile) IOException(java.io.IOException) FHIRException(org.hl7.fhir.exceptions.FHIRException) FileNotFoundException(java.io.FileNotFoundException) SAXException(org.xml.sax.SAXException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) OldSpreadsheetParser(org.hl7.fhir.definitions.parsers.spreadsheets.OldSpreadsheetParser) TypeDefn(org.hl7.fhir.definitions.model.TypeDefn) StandardsStatus(org.hl7.fhir.utilities.StandardsStatus) IniFile(org.hl7.fhir.utilities.IniFile) File(java.io.File) CSFile(org.hl7.fhir.utilities.CSFile) TextFile(org.hl7.fhir.utilities.TextFile) Sheet(org.hl7.fhir.utilities.xls.XLSXmlParser.Sheet) CSFileInputStream(org.hl7.fhir.utilities.CSFileInputStream)

Example 8 with Invariant

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

the class ResourceParser method parseED.

private void parseED(ProfileUtilities pu, ElementDefn ed, ElementDefinition focus, StructureDefinition sd, String parentName) throws IOException {
    ed.setMinCardinality(focus.getMin());
    ed.setMaxCardinality("*".equals(focus.getMax()) ? Integer.MAX_VALUE : Integer.parseInt(focus.getMax()));
    ed.setIsModifier(focus.getIsModifier());
    ed.setModifierReason(focus.getIsModifierReason());
    ed.setMustSupport(focus.getMustSupport());
    ed.setSummaryItem(focus.getIsSummary());
    ed.setRegex(ToolingExtensions.readStringExtension(focus, ToolingExtensions.EXT_REGEX));
    ed.setXmlAttribute(focus.hasRepresentation(PropertyRepresentation.XMLATTR));
    if (ToolingExtensions.hasExtension(focus, BuildExtensions.EXT_UML_DIR)) {
        ed.setUmlDir(ToolingExtensions.readStringExtension(focus, BuildExtensions.EXT_UML_DIR));
    }
    if (ToolingExtensions.hasExtension(focus, BuildExtensions.EXT_UML_BREAK)) {
        ed.setUmlBreak(ToolingExtensions.readBoolExtension(focus, BuildExtensions.EXT_UML_BREAK));
    }
    if (BuildExtensions.hasExtension(focus, BuildExtensions.EXT_SVG)) {
        String svg = BuildExtensions.readStringExtension(focus, BuildExtensions.EXT_SVG);
        if (svg.contains("w=")) {
            ed.setSvgWidth(Integer.parseInt(svg.substring(svg.indexOf("w=") + 2)));
            svg = svg.substring(0, svg.indexOf(";"));
        }
        ed.setSvgLeft(Integer.parseInt(svg.substring(0, svg.indexOf(","))));
        ed.setSvgTop(Integer.parseInt(svg.substring(svg.indexOf(",") + 1)));
    }
    ed.setName(tail(focus.getPath()));
    ed.setShortDefn(focus.getShort());
    ed.setDefinition(focus.getDefinition());
    ed.setRequirements(focus.getRequirements());
    ed.setComments(focus.getComment());
    if (BuildExtensions.hasExtension(focus, BuildExtensions.EXT_TODO)) {
        ed.setTodo(BuildExtensions.readStringExtension(focus, BuildExtensions.EXT_TODO));
    }
    if (BuildExtensions.hasExtension(focus, BuildExtensions.EXT_COMMITTEE_NOTES)) {
        ed.setCommitteeNotes(BuildExtensions.readStringExtension(focus, BuildExtensions.EXT_COMMITTEE_NOTES));
    }
    if (BuildExtensions.hasExtension(focus, BuildExtensions.EXT_HINT)) {
        ed.setDisplayHint(BuildExtensions.readStringExtension(focus, BuildExtensions.EXT_HINT));
    }
    if (focus.hasExtension(BuildExtensions.EXT_NO_BINDING)) {
        ed.setNoBindingAllowed(focus.getExtensionString(BuildExtensions.EXT_NO_BINDING).equals("true"));
    }
    for (StringType t : focus.getAlias()) {
        ed.getAliases().add(t.getValue());
    }
    if (focus.hasMaxLength()) {
        ed.setMaxLength(Integer.toString(focus.getMaxLength()));
    }
    ed.setExample(focus.getExampleFirstRep().getValue());
    ed.setMeaningWhenMissing(focus.getMeaningWhenMissing());
    if (ToolingExtensions.hasExtension(focus, BuildExtensions.EXT_TRANSLATABLE)) {
        ed.setTranslatable(ToolingExtensions.readBoolExtension(focus, BuildExtensions.EXT_TRANSLATABLE));
    }
    ed.setOrderMeaning(focus.getOrderMeaning());
    if (BuildExtensions.hasExtension(focus, BuildExtensions.EXT_STANDARDS_STATUS)) {
        ed.setStandardsStatus(StandardsStatus.fromCode(BuildExtensions.readStringExtension(focus, BuildExtensions.EXT_STANDARDS_STATUS)));
    }
    if (BuildExtensions.hasExtension(focus, BuildExtensions.EXT_NORMATIVE_VERSION)) {
        ed.setNormativeVersion(BuildExtensions.readStringExtension(focus, BuildExtensions.EXT_NORMATIVE_VERSION));
    }
    for (ElementDefinitionConstraintComponent cst : focus.getConstraint()) {
        Invariant inv = new Invariant();
        inv.setContext(focus.getPath());
        inv.setEnglish(cst.getHuman());
        if (cst.hasExtension(BuildExtensions.EXT_OCL)) {
            inv.setOcl(cst.getExtensionString(BuildExtensions.EXT_OCL));
        }
        inv.setXpath(cst.getXpath());
        inv.setId(cst.getKey());
        if (cst.hasExtension(BuildExtensions.EXT_FIXED_NAME)) {
            inv.setFixedName(cst.getExtensionString(BuildExtensions.EXT_FIXED_NAME));
        }
        inv.setSeverity(cst.getSeverity().toCode());
        if (cst.hasExtension(BuildExtensions.EXT_BEST_PRACTICE)) {
            inv.setSeverity("best-practice");
        }
        if (cst.hasExtension(BuildExtensions.EXT_TURTLE)) {
            inv.setTurtle(cst.getExtensionString(BuildExtensions.EXT_TURTLE));
        }
        inv.setRequirements(cst.getRequirements());
        inv.setExpression(cst.getExpression());
        if (cst.hasExtension(BuildExtensions.EXT_BEST_PRACTICE_EXPLANATION)) {
            inv.setExplanation(cst.getExtensionString(BuildExtensions.EXT_BEST_PRACTICE_EXPLANATION));
        }
        ed.getInvariants().put(inv.getId(), inv);
        invariants.put(inv.getId(), inv);
    }
    for (IdType cnd : focus.getCondition()) {
        Invariant inv = invariants.get(cnd.primitiveValue());
        if (inv == null) {
            System.out.println("Unable to find invariant " + cnd.primitiveValue());
        } else {
            ed.getStatedInvariants().add(inv);
        }
    }
    for (ElementDefinitionMappingComponent map : focus.getMapping()) {
        String uri = getMappingUri(sd, map.getIdentity());
        if ("http://hl7.org/fhir/fivews".equals(uri)) {
            ed.setW5(reverseW5(map.getMap()));
        } else {
            ed.getMappings().put(uri, map.getMap());
        }
    }
    if (focus.hasContentReference()) {
        ed.getTypes().add(new TypeRef("@" + focus.getContentReference().substring(1)));
    } else {
        for (TypeRefComponent tr : focus.getType()) {
            if (!Utilities.existsInList(tr.getCode(), "Element", "BackboneElement")) {
                TypeRef t = new TypeRef();
                ed.getTypes().add(t);
                if (tr.hasExtension("http://hl7.org/fhir/StructureDefinition/structuredefinition-fhir-type")) {
                    t.setName(tr.getExtensionString("http://hl7.org/fhir/StructureDefinition/structuredefinition-fhir-type"));
                } else {
                    t.setName(tr.getCode());
                }
                if (ToolingExtensions.hasExtension(tr, BuildExtensions.EXT_HIERARCHY)) {
                    ed.setHierarchy(ToolingExtensions.readBoolExtension(tr, BuildExtensions.EXT_HIERARCHY));
                }
                for (CanonicalType u : tr.getProfile()) {
                    t.setProfile(u.getValue().replace("http://hl7.org/fhir/StructureDefinition/", ""));
                }
                for (CanonicalType u : tr.getTargetProfile()) {
                    String s = u.getValue().replace("http://hl7.org/fhir/StructureDefinition/", "");
                    if ("Resource".equals(s)) {
                        t.getParams().add("Any");
                    } else {
                        t.getParams().add(s);
                    }
                }
                if (t.getName().equals("Quantity") && "SimpleQuantity".equals(t.getProfile())) {
                    t.setName("SimpleQuantity");
                    t.setProfile(null);
                }
                if ("Resource".equals(t.getProfile())) {
                    t.setProfile("Any");
                }
                if (t.getParams().toString().equals("[ActivityDefinition, EventDefinition, EvidenceVariable, Measure, OperationDefinition, PlanDefinition, Questionnaire, SubscriptionTopic]")) {
                    t.getParams().clear();
                    t.getParams().add("Definition");
                }
            }
        }
        if (ed.getTypes().size() == STAR_TYPES_COUNT) {
            ed.getTypes().clear();
            ed.getTypes().add(new TypeRef("*"));
        }
    }
    String name = parentName + Utilities.capitalize(ed.getName());
    if (focus.hasExtension("http://hl7.org/fhir/StructureDefinition/structuredefinition-explicit-type-name")) {
        ed.setStatedType(focus.getExtensionString("http://hl7.org/fhir/StructureDefinition/structuredefinition-explicit-type-name"));
        ed.setDeclaredTypeName(ed.getStatedType());
    } else if (ed.getTypes().isEmpty() && !focus.hasContentReference()) {
        ed.setDeclaredTypeName(name + "Component");
    }
    if (focus.hasBinding()) {
        ed.setBinding(parseBinding(focus.getBinding()));
    }
    for (ElementDefinition child : pu.getChildList(sd, focus, true, false)) {
        ElementDefn c = new ElementDefn();
        ed.getElements().add(c);
        parseED(pu, c, child, sd, name);
    }
// todo:
// private ElementDefinition derivation;
}
Also used : Invariant(org.hl7.fhir.definitions.model.Invariant) StringType(org.hl7.fhir.r5.model.StringType) TypeRefComponent(org.hl7.fhir.r5.model.ElementDefinition.TypeRefComponent) TypeRef(org.hl7.fhir.definitions.model.TypeRef) ElementDefn(org.hl7.fhir.definitions.model.ElementDefn) ElementDefinition(org.hl7.fhir.r5.model.ElementDefinition) ElementDefinitionConstraintComponent(org.hl7.fhir.r5.model.ElementDefinition.ElementDefinitionConstraintComponent) ElementDefinitionMappingComponent(org.hl7.fhir.r5.model.ElementDefinition.ElementDefinitionMappingComponent) CanonicalType(org.hl7.fhir.r5.model.CanonicalType) IdType(org.hl7.fhir.r5.model.IdType)

Example 9 with Invariant

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

the class XmlSpecGenerator method getInvariants.

private String getInvariants(ElementDefn elem) {
    StringBuilder b = new StringBuilder();
    boolean first = true;
    for (Invariant i : elem.getStatedInvariants()) {
        if (!first)
            b.append("; ");
        first = false;
        b.append(i.getId() + ": " + i.getEnglish());
    }
    return b.toString();
}
Also used : Invariant(org.hl7.fhir.definitions.model.Invariant) CommaSeparatedStringBuilder(org.hl7.fhir.utilities.CommaSeparatedStringBuilder)

Example 10 with Invariant

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

the class JsonSpecGenerator method getInvariants.

private String getInvariants(ElementDefn elem) {
    StringBuilder b = new StringBuilder();
    boolean first = true;
    for (Invariant i : elem.getStatedInvariants()) {
        if (!first)
            b.append("; ");
        first = false;
        b.append(i.getId() + ": " + i.getEnglish());
    }
    return b.toString();
}
Also used : Invariant(org.hl7.fhir.definitions.model.Invariant) CommaSeparatedStringBuilder(org.hl7.fhir.utilities.CommaSeparatedStringBuilder)

Aggregations

Invariant (org.hl7.fhir.definitions.model.Invariant)21 ElementDefn (org.hl7.fhir.definitions.model.ElementDefn)11 FHIRException (org.hl7.fhir.exceptions.FHIRException)10 ArrayList (java.util.ArrayList)7 TypeRef (org.hl7.fhir.definitions.model.TypeRef)6 CommaSeparatedStringBuilder (org.hl7.fhir.utilities.CommaSeparatedStringBuilder)6 ElementDefinitionConstraintComponent (org.hl7.fhir.r5.model.ElementDefinition.ElementDefinitionConstraintComponent)5 Sheet (org.hl7.fhir.utilities.xls.XLSXmlParser.Sheet)5 ResourceDefn (org.hl7.fhir.definitions.model.ResourceDefn)4 ElementDefinition (org.hl7.fhir.r5.model.ElementDefinition)4 ProfiledType (org.hl7.fhir.definitions.model.ProfiledType)3 TypeRefComponent (org.hl7.fhir.r5.model.ElementDefinition.TypeRefComponent)3 StringType (org.hl7.fhir.r5.model.StringType)3 BindingSpecification (org.hl7.fhir.definitions.model.BindingSpecification)2 TypeDefn (org.hl7.fhir.definitions.model.TypeDefn)2 FHIRFormatError (org.hl7.fhir.exceptions.FHIRFormatError)2 StructureDefinitionContextComponent (org.hl7.fhir.r5.model.StructureDefinition.StructureDefinitionContextComponent)2 UriType (org.hl7.fhir.r5.model.UriType)2 StandardsStatus (org.hl7.fhir.utilities.StandardsStatus)2 FhirContext (ca.uhn.fhir.context.FhirContext)1