Search in sources :

Example 31 with ElementDefinitionSlicingComponent

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

the class ProfileUtilities method generateForChildren.

private void generateForChildren(SchematronWriter sch, String xpath, ElementDefinition ed, StructureDefinition structure, StructureDefinition base) throws IOException {
    // generateForChild(txt, structure, child);
    List<ElementDefinition> children = getChildList(structure, ed);
    String sliceName = null;
    ElementDefinitionSlicingComponent slicing = null;
    for (ElementDefinition child : children) {
        String name = tail(child.getPath());
        if (child.hasSlicing()) {
            sliceName = name;
            slicing = child.getSlicing();
        } else if (!name.equals(sliceName))
            slicing = null;
        ElementDefinition based = getByPath(base, child.getPath());
        boolean doMin = (child.getMin() > 0) && (based == null || (child.getMin() != based.getMin()));
        boolean doMax = child.hasMax() && !child.getMax().equals("*") && (based == null || (!child.getMax().equals(based.getMax())));
        Slicer slicer = slicing == null ? new Slicer(true) : generateSlicer(child, slicing, structure);
        if (slicer.check) {
            if (doMin || doMax) {
                Section s = sch.section(xpath);
                Rule r = s.rule(xpath);
                if (doMin)
                    r.assrt("count(f:" + name + slicer.criteria + ") >= " + Integer.toString(child.getMin()), name + slicer.name + ": minimum cardinality of '" + name + "' is " + Integer.toString(child.getMin()));
                if (doMax)
                    r.assrt("count(f:" + name + slicer.criteria + ") <= " + child.getMax(), name + slicer.name + ": maximum cardinality of '" + name + "' is " + child.getMax());
            }
        }
    }
    for (ElementDefinitionConstraintComponent inv : ed.getConstraint()) {
        if (inv.hasXpath()) {
            Section s = sch.section(ed.getPath());
            Rule r = s.rule(xpath);
            r.assrt(inv.getXpath(), (inv.hasId() ? inv.getId() + ": " : "") + inv.getHuman() + (inv.hasUserData(IS_DERIVED) ? " (inherited)" : ""));
        }
    }
    if (!ed.hasContentReference()) {
        for (ElementDefinition child : children) {
            String name = tail(child.getPath());
            generateForChildren(sch, xpath + "/f:" + name, child, structure, base);
        }
    }
}
Also used : ElementDefinitionSlicingComponent(org.hl7.fhir.r4b.model.ElementDefinition.ElementDefinitionSlicingComponent) ElementDefinition(org.hl7.fhir.r4b.model.ElementDefinition) TypeDerivationRule(org.hl7.fhir.r4b.model.StructureDefinition.TypeDerivationRule) Rule(org.hl7.fhir.utilities.xml.SchematronWriter.Rule) Section(org.hl7.fhir.utilities.xml.SchematronWriter.Section) ElementDefinitionConstraintComponent(org.hl7.fhir.r4b.model.ElementDefinition.ElementDefinitionConstraintComponent)

Example 32 with ElementDefinitionSlicingComponent

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

the class ProfileUtilities method generateSlicer.

private Slicer generateSlicer(ElementDefinition child, ElementDefinitionSlicingComponent slicing, StructureDefinition structure) {
    // given a child in a structure, it's sliced. figure out the slicing xpath
    if (child.getPath().endsWith(".extension")) {
        ElementDefinition ued = getUrlFor(structure, child);
        if ((ued == null || !ued.hasFixed()) && !(child.hasType() && (child.getType().get(0).hasProfile())))
            return new Slicer(false);
        else {
            Slicer s = new Slicer(true);
            String url = (ued == null || !ued.hasFixed()) ? child.getType().get(0).getProfile().get(0).getValue() : ((UriType) ued.getFixed()).asStringValue();
            s.name = " with URL = '" + url + "'";
            s.criteria = "[@url = '" + url + "']";
            return s;
        }
    } else
        return new Slicer(false);
}
Also used : ElementDefinition(org.hl7.fhir.r4b.model.ElementDefinition)

Example 33 with ElementDefinitionSlicingComponent

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

the class ProfileUtilities method generateForChildren.

private void generateForChildren(SchematronWriter sch, String xpath, ElementDefinition ed, StructureDefinition structure, StructureDefinition base) throws IOException {
    // generateForChild(txt, structure, child);
    List<ElementDefinition> children = getChildList(structure, ed);
    String sliceName = null;
    ElementDefinitionSlicingComponent slicing = null;
    for (ElementDefinition child : children) {
        String name = tail(child.getPath());
        if (child.hasSlicing()) {
            sliceName = name;
            slicing = child.getSlicing();
        } else if (!name.equals(sliceName))
            slicing = null;
        ElementDefinition based = getByPath(base, child.getPath());
        boolean doMin = (child.getMin() > 0) && (based == null || (child.getMin() != based.getMin()));
        boolean doMax = child.hasMax() && !child.getMax().equals("*") && (based == null || (!child.getMax().equals(based.getMax())));
        Slicer slicer = slicing == null ? new Slicer(true) : generateSlicer(child, slicing, structure);
        if (slicer.check) {
            if (doMin || doMax) {
                Section s = sch.section(xpath);
                Rule r = s.rule(xpath);
                if (doMin)
                    r.assrt("count(f:" + name + slicer.criteria + ") >= " + Integer.toString(child.getMin()), name + slicer.name + ": minimum cardinality of '" + name + "' is " + Integer.toString(child.getMin()));
                if (doMax)
                    r.assrt("count(f:" + name + slicer.criteria + ") <= " + child.getMax(), name + slicer.name + ": maximum cardinality of '" + name + "' is " + child.getMax());
            }
        }
    }
    for (ElementDefinitionConstraintComponent inv : ed.getConstraint()) {
        if (inv.hasXpath()) {
            Section s = sch.section(ed.getPath());
            Rule r = s.rule(xpath);
            r.assrt(inv.getXpath(), (inv.hasId() ? inv.getId() + ": " : "") + inv.getHuman() + (inv.hasUserData(IS_DERIVED) ? " (inherited)" : ""));
        }
    }
    for (ElementDefinition child : children) {
        String name = tail(child.getPath());
        generateForChildren(sch, xpath + "/f:" + name, child, structure, base);
    }
}
Also used : ElementDefinitionSlicingComponent(org.hl7.fhir.dstu3.model.ElementDefinition.ElementDefinitionSlicingComponent) ElementDefinition(org.hl7.fhir.dstu3.model.ElementDefinition) TypeDerivationRule(org.hl7.fhir.dstu3.model.StructureDefinition.TypeDerivationRule) Rule(org.hl7.fhir.utilities.xml.SchematronWriter.Rule) Section(org.hl7.fhir.utilities.xml.SchematronWriter.Section) ElementDefinitionConstraintComponent(org.hl7.fhir.dstu3.model.ElementDefinition.ElementDefinitionConstraintComponent)

Example 34 with ElementDefinitionSlicingComponent

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

the class ProfileUtilities method processPaths.

/**
 * @param trimDifferential
 * @throws DefinitionException, FHIRException
 * @throws Exception
 */
private ElementDefinition processPaths(String indent, StructureDefinitionSnapshotComponent result, StructureDefinitionSnapshotComponent base, StructureDefinitionDifferentialComponent differential, int baseCursor, int diffCursor, int baseLimit, int diffLimit, String url, String profileName, String contextPathSrc, String contextPathDst, boolean trimDifferential, String contextName, String resultPathBase, boolean slicingDone) throws DefinitionException, FHIRException {
    // System.out.println(indent+"PP @ "+resultPathBase+": base = "+baseCursor+" to "+baseLimit+", diff = "+diffCursor+" to "+diffLimit+" (slicing = "+slicingDone+")");
    ElementDefinition res = null;
    // just repeat processing entries until we run out of our allowed scope (1st entry, the allowed scope is all the entries)
    while (baseCursor <= baseLimit) {
        // get the current focus of the base, and decide what to do
        ElementDefinition currentBase = base.getElement().get(baseCursor);
        String cpath = fixedPath(contextPathSrc, currentBase.getPath());
        // System.out.println(indent+" - "+cpath+": base = "+baseCursor+" to "+baseLimit+", diff = "+diffCursor+" to "+diffLimit+" (slicingDone = "+slicingDone+")");
        // get a list of matching elements in scope
        List<ElementDefinition> diffMatches = getDiffMatches(differential, cpath, diffCursor, diffLimit, profileName, url);
        // in the simple case, source is not sliced.
        if (!currentBase.hasSlicing()) {
            if (diffMatches.isEmpty()) {
                // the differential doesn't say anything about this item
                // so we just copy it in
                ElementDefinition outcome = updateURLs(url, currentBase.copy());
                outcome.setPath(fixedPath(contextPathDst, outcome.getPath()));
                updateFromBase(outcome, currentBase);
                markDerived(outcome);
                if (resultPathBase == null)
                    resultPathBase = outcome.getPath();
                else if (!outcome.getPath().startsWith(resultPathBase))
                    throw new DefinitionException("Adding wrong path");
                result.getElement().add(outcome);
                if (hasInnerDiffMatches(differential, cpath, diffCursor, diffLimit, base.getElement())) {
                    // well, the profile walks into this, so we need to as well
                    if (outcome.getType().size() > 1) {
                        for (TypeRefComponent t : outcome.getType()) {
                            if (!t.getCode().equals("Reference"))
                                throw new DefinitionException(diffMatches.get(0).getPath() + " has children (" + differential.getElement().get(diffCursor).getPath() + ") and multiple types (" + typeCode(outcome.getType()) + ") in profile " + profileName);
                        }
                    }
                    StructureDefinition dt = getProfileForDataType(outcome.getType().get(0));
                    if (dt == null)
                        throw new DefinitionException(cpath + " has children for type " + typeCode(outcome.getType()) + " in profile " + profileName + ", but can't find type");
                    contextName = dt.getUrl();
                    int start = diffCursor;
                    while (differential.getElement().size() > diffCursor && pathStartsWith(differential.getElement().get(diffCursor).getPath(), cpath + ".")) diffCursor++;
                    processPaths(indent + "  ", result, dt.getSnapshot(), differential, 1, /* starting again on the data type, but skip the root */
                    start, dt.getSnapshot().getElement().size() - 1, diffCursor - 1, url, profileName, cpath, outcome.getPath(), trimDifferential, contextName, resultPathBase, false);
                }
                baseCursor++;
            } else if (diffMatches.size() == 1 && (slicingDone || !(diffMatches.get(0).hasSlicing() || (isExtension(diffMatches.get(0)) && diffMatches.get(0).hasSliceName())))) {
                // one matching element in the differential
                ElementDefinition template = null;
                if (diffMatches.get(0).hasType() && diffMatches.get(0).getType().size() == 1 && diffMatches.get(0).getType().get(0).hasProfile() && !diffMatches.get(0).getType().get(0).getCode().equals("Reference")) {
                    String p = diffMatches.get(0).getType().get(0).getProfile();
                    StructureDefinition sd = context.fetchResource(StructureDefinition.class, p);
                    if (sd != null) {
                        if (!sd.hasSnapshot()) {
                            StructureDefinition sdb = context.fetchResource(StructureDefinition.class, sd.getBaseDefinition());
                            if (sdb == null)
                                throw new DefinitionException("no base for " + sd.getBaseDefinition());
                            generateSnapshot(sdb, sd, sd.getUrl(), sd.getName());
                        }
                        template = sd.getSnapshot().getElement().get(0).copy().setPath(currentBase.getPath());
                        template.setSliceName(null);
                        // temporary work around
                        if (!diffMatches.get(0).getType().get(0).getCode().equals("Extension")) {
                            template.setMin(currentBase.getMin());
                            template.setMax(currentBase.getMax());
                        }
                    }
                }
                if (template == null)
                    template = currentBase.copy();
                else
                    // some of what's in currentBase overrides template
                    template = overWriteWithCurrent(template, currentBase);
                ElementDefinition outcome = updateURLs(url, template);
                outcome.setPath(fixedPath(contextPathDst, outcome.getPath()));
                res = outcome;
                updateFromBase(outcome, currentBase);
                if (diffMatches.get(0).hasSliceName())
                    outcome.setSliceName(diffMatches.get(0).getSliceName());
                outcome.setSlicing(null);
                updateFromDefinition(outcome, diffMatches.get(0), profileName, trimDifferential, url);
                if (// if the base profile allows multiple types, but the profile only allows one, rename it
                outcome.getPath().endsWith("[x]") && outcome.getType().size() == 1 && !outcome.getType().get(0).getCode().equals("*"))
                    outcome.setPath(outcome.getPath().substring(0, outcome.getPath().length() - 3) + Utilities.capitalize(outcome.getType().get(0).getCode()));
                if (resultPathBase == null)
                    resultPathBase = outcome.getPath();
                else if (!outcome.getPath().startsWith(resultPathBase))
                    throw new DefinitionException("Adding wrong path");
                result.getElement().add(outcome);
                baseCursor++;
                diffCursor = differential.getElement().indexOf(diffMatches.get(0)) + 1;
                if (differential.getElement().size() > diffCursor && outcome.getPath().contains(".") && (isDataType(outcome.getType()) || outcome.hasContentReference())) {
                    // don't want to do this for the root, since that's base, and we're already processing it
                    if (pathStartsWith(differential.getElement().get(diffCursor).getPath(), diffMatches.get(0).getPath() + ".") && !baseWalksInto(base.getElement(), baseCursor)) {
                        if (outcome.getType().size() > 1) {
                            for (TypeRefComponent t : outcome.getType()) {
                                if (!t.getCode().equals("Reference"))
                                    throw new DefinitionException(diffMatches.get(0).getPath() + " has children (" + differential.getElement().get(diffCursor).getPath() + ") and multiple types (" + typeCode(outcome.getType()) + ") in profile " + profileName);
                            }
                        }
                        int start = diffCursor;
                        while (differential.getElement().size() > diffCursor && pathStartsWith(differential.getElement().get(diffCursor).getPath(), diffMatches.get(0).getPath() + ".")) diffCursor++;
                        if (outcome.hasContentReference()) {
                            ElementDefinition tgt = getElementById(base.getElement(), outcome.getContentReference());
                            if (tgt == null)
                                throw new DefinitionException("Unable to resolve reference to " + outcome.getContentReference());
                            replaceFromContentReference(outcome, tgt);
                            int nbc = base.getElement().indexOf(tgt) + 1;
                            int nbl = nbc;
                            while (nbl < base.getElement().size() && base.getElement().get(nbl).getPath().startsWith(tgt.getPath() + ".")) nbl++;
                            processPaths(indent + "  ", result, base, differential, nbc, start - 1, nbl - 1, diffCursor - 1, url, profileName, tgt.getPath(), diffMatches.get(0).getPath(), trimDifferential, contextName, resultPathBase, false);
                        } else {
                            StructureDefinition dt = getProfileForDataType(outcome.getType().get(0));
                            if (dt == null)
                                throw new DefinitionException(diffMatches.get(0).getPath() + " has children (" + differential.getElement().get(diffCursor).getPath() + ") for type " + typeCode(outcome.getType()) + " in profile " + profileName + ", but can't find type");
                            contextName = dt.getUrl();
                            processPaths(indent + "  ", result, dt.getSnapshot(), differential, 1, /* starting again on the data type, but skip the root */
                            start - 1, dt.getSnapshot().getElement().size() - 1, diffCursor - 1, url, profileName + pathTail(diffMatches, 0), diffMatches.get(0).getPath(), outcome.getPath(), trimDifferential, contextName, resultPathBase, false);
                        }
                    }
                }
            } else {
                // ok, the differential slices the item. Let's check our pre-conditions to ensure that this is correct
                if (!unbounded(currentBase) && !isSlicedToOneOnly(diffMatches.get(0)))
                    // (but you might do that in order to split up constraints by type)
                    throw new DefinitionException("Attempt to a slice an element that does not repeat: " + currentBase.getPath() + "/" + currentBase.getSliceName() + " from " + contextName + " in " + url);
                if (// well, the diff has set up a slice, but hasn't defined it. this is an error
                !diffMatches.get(0).hasSlicing() && !isExtension(currentBase))
                    throw new DefinitionException("differential does not have a slice: " + currentBase.getPath() + " in profile " + url);
                // well, if it passed those preconditions then we slice the dest.
                int start = 0;
                int nbl = findEndOfElement(base, baseCursor);
                if (diffMatches.size() > 1 && diffMatches.get(0).hasSlicing() && differential.getElement().indexOf(diffMatches.get(1)) > differential.getElement().indexOf(diffMatches.get(0)) + 1) {
                    int ndc = differential.getElement().indexOf(diffMatches.get(0));
                    int ndl = findEndOfElement(differential, ndc);
                    processPaths(indent + "  ", result, base, differential, baseCursor, ndc, nbl, ndl, url, profileName + pathTail(diffMatches, 0), contextPathSrc, contextPathDst, trimDifferential, contextName, resultPathBase, true).setSlicing(diffMatches.get(0).getSlicing());
                    start++;
                } else {
                    // we're just going to accept the differential slicing at face value
                    ElementDefinition outcome = updateURLs(url, currentBase.copy());
                    outcome.setPath(fixedPath(contextPathDst, outcome.getPath()));
                    updateFromBase(outcome, currentBase);
                    if (!diffMatches.get(0).hasSlicing())
                        outcome.setSlicing(makeExtensionSlicing());
                    else
                        outcome.setSlicing(diffMatches.get(0).getSlicing().copy());
                    if (!outcome.getPath().startsWith(resultPathBase))
                        throw new DefinitionException("Adding wrong path");
                    result.getElement().add(outcome);
                    // differential - if the first one in the list has a name, we'll process it. Else we'll treat it as the base definition of the slice.
                    if (!diffMatches.get(0).hasSliceName()) {
                        updateFromDefinition(outcome, diffMatches.get(0), profileName, trimDifferential, url);
                        if (!outcome.hasContentReference() && !outcome.hasType()) {
                            throw new DefinitionException("not done yet");
                        }
                        start++;
                    // result.getElement().remove(result.getElement().size()-1);
                    } else
                        checkExtensionDoco(outcome);
                }
                // now, for each entry in the diff matches, we're going to process the base item
                // our processing scope for base is all the children of the current path
                int ndc = diffCursor;
                int ndl = diffCursor;
                for (int i = start; i < diffMatches.size(); i++) {
                    // our processing scope for the differential is the item in the list, and all the items before the next one in the list
                    ndc = differential.getElement().indexOf(diffMatches.get(i));
                    ndl = findEndOfElement(differential, ndc);
                    /*            if (skipSlicingElement && i == 0) {
              ndc = ndc + 1;
              if (ndc > ndl)
                continue;
            }*/
                    // now we process the base scope repeatedly for each instance of the item in the differential list
                    processPaths(indent + "  ", result, base, differential, baseCursor, ndc, nbl, ndl, url, profileName + pathTail(diffMatches, i), contextPathSrc, contextPathDst, trimDifferential, contextName, resultPathBase, true);
                }
                // ok, done with that - next in the base list
                baseCursor = nbl + 1;
                diffCursor = ndl + 1;
            }
        } else {
            // the item is already sliced in the base profile.
            // here's the rules
            // 1. irrespective of whether the slicing is ordered or not, the definition order must be maintained
            // 2. slice element names have to match.
            // 3. new slices must be introduced at the end
            // corallory: you can't re-slice existing slices. is that ok?
            // we're going to need this:
            String path = currentBase.getPath();
            ElementDefinition original = currentBase;
            if (diffMatches.isEmpty()) {
                // copy across the currentbase, and all of its children and siblings
                while (baseCursor < base.getElement().size() && base.getElement().get(baseCursor).getPath().startsWith(path)) {
                    ElementDefinition outcome = updateURLs(url, base.getElement().get(baseCursor).copy());
                    outcome.setPath(fixedPath(contextPathDst, outcome.getPath()));
                    if (!outcome.getPath().startsWith(resultPathBase))
                        throw new DefinitionException("Adding wrong path in profile " + profileName + ": " + outcome.getPath() + " vs " + resultPathBase);
                    // so we just copy it in
                    result.getElement().add(outcome);
                    baseCursor++;
                }
            } else {
                // first - check that the slicing is ok
                boolean closed = currentBase.getSlicing().getRules() == SlicingRules.CLOSED;
                int diffpos = 0;
                boolean isExtension = cpath.endsWith(".extension") || cpath.endsWith(".modifierExtension");
                if (diffMatches.get(0).hasSlicing()) {
                    // it might be null if the differential doesn't want to say anything about slicing
                    if (!isExtension)
                        // if there's a slice on the first, we'll ignore any content it has
                        diffpos++;
                    ElementDefinitionSlicingComponent dSlice = diffMatches.get(0).getSlicing();
                    ElementDefinitionSlicingComponent bSlice = currentBase.getSlicing();
                    if (dSlice.hasOrderedElement() && bSlice.hasOrderedElement() && !orderMatches(dSlice.getOrderedElement(), bSlice.getOrderedElement()))
                        throw new DefinitionException("Slicing rules on differential (" + summariseSlicing(dSlice) + ") do not match those on base (" + summariseSlicing(bSlice) + ") - order @ " + path + " (" + contextName + ")");
                    if (!discriminatorMatches(dSlice.getDiscriminator(), bSlice.getDiscriminator()))
                        throw new DefinitionException("Slicing rules on differential (" + summariseSlicing(dSlice) + ") do not match those on base (" + summariseSlicing(bSlice) + ") - disciminator @ " + path + " (" + contextName + ")");
                    if (!ruleMatches(dSlice.getRules(), bSlice.getRules()))
                        throw new DefinitionException("Slicing rules on differential (" + summariseSlicing(dSlice) + ") do not match those on base (" + summariseSlicing(bSlice) + ") - rule @ " + path + " (" + contextName + ")");
                }
                if (diffMatches.size() > 1 && diffMatches.get(0).hasSlicing() && differential.getElement().indexOf(diffMatches.get(1)) > differential.getElement().indexOf(diffMatches.get(0)) + 1) {
                    throw new Error("Not done yet");
                }
                ElementDefinition outcome = updateURLs(url, currentBase.copy());
                outcome.setPath(fixedPath(contextPathDst, outcome.getPath()));
                updateFromBase(outcome, currentBase);
                if (diffMatches.get(0).hasSlicing()) /*&& !isExtension*/
                {
                    updateFromSlicing(outcome.getSlicing(), diffMatches.get(0).getSlicing());
                    // if there's no slice, we don't want to update the unsliced description
                    updateFromDefinition(outcome, diffMatches.get(0), profileName, closed, url);
                } else if (!diffMatches.get(0).hasSliceName())
                    // because of updateFromDefinition isn't called
                    diffMatches.get(0).setUserData(GENERATED_IN_SNAPSHOT, true);
                result.getElement().add(outcome);
                if (!diffMatches.get(0).hasSliceName()) {
                    // it's not real content, just the slice
                    diffpos++;
                }
                // now, we have two lists, base and diff. we're going to work through base, looking for matches in diff.
                List<ElementDefinition> baseMatches = getSiblings(base.getElement(), currentBase);
                for (ElementDefinition baseItem : baseMatches) {
                    baseCursor = base.getElement().indexOf(baseItem);
                    outcome = updateURLs(url, baseItem.copy());
                    updateFromBase(outcome, currentBase);
                    outcome.setPath(fixedPath(contextPathDst, outcome.getPath()));
                    outcome.setSlicing(null);
                    if (!outcome.getPath().startsWith(resultPathBase))
                        throw new DefinitionException("Adding wrong path");
                    if (diffpos < diffMatches.size() && diffMatches.get(diffpos).getSliceName().equals(outcome.getSliceName())) {
                        // if there's a diff, we update the outcome with diff
                        // no? updateFromDefinition(outcome, diffMatches.get(diffpos), profileName, closed, url);
                        // then process any children
                        int nbl = findEndOfElement(base, baseCursor);
                        int ndc = differential.getElement().indexOf(diffMatches.get(diffpos));
                        int ndl = findEndOfElement(differential, ndc);
                        // now we process the base scope repeatedly for each instance of the item in the differential list
                        processPaths(indent + "  ", result, base, differential, baseCursor, ndc, nbl, ndl, url, profileName + pathTail(diffMatches, diffpos), contextPathSrc, contextPathDst, closed, contextName, resultPathBase, true);
                        // ok, done with that - now set the cursors for if this is the end
                        baseCursor = nbl;
                        diffCursor = ndl + 1;
                        diffpos++;
                    } else {
                        result.getElement().add(outcome);
                        baseCursor++;
                        // just copy any children on the base
                        while (baseCursor < base.getElement().size() && base.getElement().get(baseCursor).getPath().startsWith(path) && !base.getElement().get(baseCursor).getPath().equals(path)) {
                            outcome = updateURLs(url, base.getElement().get(baseCursor).copy());
                            outcome.setPath(fixedPath(contextPathDst, outcome.getPath()));
                            if (!outcome.getPath().startsWith(resultPathBase))
                                throw new DefinitionException("Adding wrong path");
                            result.getElement().add(outcome);
                            baseCursor++;
                        }
                        // Lloyd - add this for test T15
                        baseCursor--;
                    }
                }
                // finally, we process any remaining entries in diff, which are new (and which are only allowed if the base wasn't closed
                if (closed && diffpos < diffMatches.size())
                    throw new DefinitionException("The base snapshot marks a slicing as closed, but the differential tries to extend it in " + profileName + " at " + path + " (" + cpath + ")");
                if (diffpos == diffMatches.size()) {
                    diffCursor++;
                } else {
                    while (diffpos < diffMatches.size()) {
                        ElementDefinition diffItem = diffMatches.get(diffpos);
                        for (ElementDefinition baseItem : baseMatches) if (baseItem.getSliceName().equals(diffItem.getSliceName()))
                            throw new DefinitionException("Named items are out of order in the slice");
                        outcome = updateURLs(url, currentBase.copy());
                        // outcome = updateURLs(url, diffItem.copy());
                        outcome.setPath(fixedPath(contextPathDst, outcome.getPath()));
                        updateFromBase(outcome, currentBase);
                        outcome.setSlicing(null);
                        if (!outcome.getPath().startsWith(resultPathBase))
                            throw new DefinitionException("Adding wrong path");
                        result.getElement().add(outcome);
                        updateFromDefinition(outcome, diffItem, profileName, trimDifferential, url);
                        // --- LM Added this
                        diffCursor = differential.getElement().indexOf(diffItem) + 1;
                        if (!outcome.getType().isEmpty() && (/*outcome.getType().get(0).getCode().equals("Extension") || */
                        differential.getElement().size() > diffCursor) && outcome.getPath().contains(".") && isDataType(outcome.getType())) {
                            // don't want to do this for the root, since that's base, and we're already processing it
                            if (!baseWalksInto(base.getElement(), baseCursor)) {
                                if (differential.getElement().size() > diffCursor && pathStartsWith(differential.getElement().get(diffCursor).getPath(), diffMatches.get(0).getPath() + ".")) {
                                    if (outcome.getType().size() > 1)
                                        for (TypeRefComponent t : outcome.getType()) {
                                            if (!t.getCode().equals("Reference"))
                                                throw new DefinitionException(diffMatches.get(0).getPath() + " has children (" + differential.getElement().get(diffCursor).getPath() + ") and multiple types (" + typeCode(outcome.getType()) + ") in profile " + profileName);
                                        }
                                    TypeRefComponent t = outcome.getType().get(0);
                                    StructureDefinition dt = getProfileForDataType(outcome.getType().get(0));
                                    // }
                                    if (dt == null)
                                        throw new DefinitionException(diffMatches.get(0).getPath() + " has children (" + differential.getElement().get(diffCursor).getPath() + ") for type " + typeCode(outcome.getType()) + " in profile " + profileName + ", but can't find type");
                                    contextName = dt.getUrl();
                                    int start = diffCursor;
                                    while (differential.getElement().size() > diffCursor && pathStartsWith(differential.getElement().get(diffCursor).getPath(), diffMatches.get(0).getPath() + ".")) diffCursor++;
                                    processPaths(indent + "  ", result, dt.getSnapshot(), differential, 1, /* starting again on the data type, but skip the root */
                                    start - 1, dt.getSnapshot().getElement().size() - 1, diffCursor - 1, url, profileName + pathTail(diffMatches, 0), diffMatches.get(0).getPath(), outcome.getPath(), trimDifferential, contextName, resultPathBase, false);
                                } else if (outcome.getType().get(0).getCode().equals("Extension")) {
                                    // Force URL to appear if we're dealing with an extension.  (This is a kludge - may need to drill down in other cases where we're slicing and the type has a profile declaration that could be setting the fixed value)
                                    StructureDefinition dt = getProfileForDataType(outcome.getType().get(0));
                                    for (ElementDefinition extEd : dt.getSnapshot().getElement()) {
                                        // We only want the children that aren't the root
                                        if (extEd.getPath().contains(".")) {
                                            ElementDefinition extUrlEd = updateURLs(url, extEd.copy());
                                            extUrlEd.setPath(fixedPath(outcome.getPath(), extUrlEd.getPath()));
                                            // updateFromBase(extUrlEd, currentBase);
                                            markDerived(extUrlEd);
                                            result.getElement().add(extUrlEd);
                                        }
                                    }
                                }
                            }
                        }
                        // ---
                        diffpos++;
                    }
                }
                baseCursor++;
            }
        }
    }
    int i = 0;
    for (ElementDefinition e : result.getElement()) {
        i++;
        if (e.hasMinElement() && e.getMinElement().getValue() == null)
            throw new Error("null min");
    }
    return res;
}
Also used : StructureDefinition(org.hl7.fhir.dstu3.model.StructureDefinition) TypeRefComponent(org.hl7.fhir.dstu3.model.ElementDefinition.TypeRefComponent) ElementDefinitionSlicingComponent(org.hl7.fhir.dstu3.model.ElementDefinition.ElementDefinitionSlicingComponent) FHIRFormatError(org.hl7.fhir.exceptions.FHIRFormatError) ElementDefinition(org.hl7.fhir.dstu3.model.ElementDefinition) DefinitionException(org.hl7.fhir.exceptions.DefinitionException)

Example 35 with ElementDefinitionSlicingComponent

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

the class ProfileUtilities method makeExtensionSlicing.

private ElementDefinitionSlicingComponent makeExtensionSlicing() {
    ElementDefinitionSlicingComponent slice = new ElementDefinitionSlicingComponent();
    slice.addDiscriminator("url");
    slice.setOrdered(false);
    slice.setRules(SlicingRules.OPEN);
    return slice;
}
Also used : ElementDefinitionSlicingComponent(org.hl7.fhir.dstu2.model.ElementDefinition.ElementDefinitionSlicingComponent)

Aggregations

CommaSeparatedStringBuilder (org.hl7.fhir.utilities.CommaSeparatedStringBuilder)11 DefinitionException (org.hl7.fhir.exceptions.DefinitionException)7 ElementDefinitionSlicingDiscriminatorComponent (org.hl7.fhir.r5.model.ElementDefinition.ElementDefinitionSlicingDiscriminatorComponent)6 Rule (org.hl7.fhir.utilities.xml.SchematronWriter.Rule)6 Section (org.hl7.fhir.utilities.xml.SchematronWriter.Section)6 FHIRException (org.hl7.fhir.exceptions.FHIRException)4 FHIRFormatError (org.hl7.fhir.exceptions.FHIRFormatError)4 ElementDefinition (org.hl7.fhir.r4.model.ElementDefinition)4 ElementDefinitionSlicingComponent (org.hl7.fhir.r4.model.ElementDefinition.ElementDefinitionSlicingComponent)4 ElementDefinition (org.hl7.fhir.r4b.model.ElementDefinition)4 ElementDefinition (org.hl7.fhir.r5.model.ElementDefinition)4 ElementDefinitionSlicingComponent (org.hl7.fhir.r5.model.ElementDefinition.ElementDefinitionSlicingComponent)4 ArrayList (java.util.ArrayList)3 List (java.util.List)3 ElementDefinition (org.hl7.fhir.dstu2.model.ElementDefinition)3 ElementDefinitionSlicingComponent (org.hl7.fhir.dstu2.model.ElementDefinition.ElementDefinitionSlicingComponent)3 ElementDefinition (org.hl7.fhir.dstu2016may.model.ElementDefinition)3 ElementDefinitionSlicingComponent (org.hl7.fhir.dstu2016may.model.ElementDefinition.ElementDefinitionSlicingComponent)3 ElementDefinition (org.hl7.fhir.dstu3.model.ElementDefinition)3 ElementDefinitionSlicingComponent (org.hl7.fhir.dstu3.model.ElementDefinition.ElementDefinitionSlicingComponent)3