Search in sources :

Example 16 with ProfiledType

use of org.hl7.fhir.definitions.model.ProfiledType in project org.hl7.fhir.core by hapifhir.

the class StructureMapUtilities method analyseSource.

private VariablesForProfiling analyseSource(String ruleId, TransformContext context, VariablesForProfiling vars, StructureMapGroupRuleSourceComponent src, XhtmlNode td) throws Exception {
    VariableForProfiling var = vars.get(VariableMode.INPUT, src.getContext());
    if (var == null)
        throw new FHIRException("Rule \"" + ruleId + "\": Unknown input variable " + src.getContext());
    PropertyWithType prop = var.getProperty();
    boolean optional = false;
    boolean repeating = false;
    if (src.hasCondition()) {
        optional = true;
    }
    if (src.hasElement()) {
        Property element = prop.getBaseProperty().getChild(prop.types.getType(), src.getElement());
        if (element == null)
            throw new Exception("Rule \"" + ruleId + "\": Unknown element name " + src.getElement());
        if (element.getDefinition().getMin() == 0)
            optional = true;
        if (element.getDefinition().getMax().equals("*"))
            repeating = true;
        VariablesForProfiling result = vars.copy(optional, repeating);
        TypeDetails type = new TypeDetails(CollectionStatus.SINGLETON);
        for (TypeRefComponent tr : element.getDefinition().getType()) {
            if (!tr.hasCode())
                throw new Error("Rule \"" + ruleId + "\": Element has no type");
            ProfiledType pt = new ProfiledType(tr.getCode());
            if (tr.hasProfile())
                pt.addProfile(tr.getProfile());
            if (element.getDefinition().hasBinding())
                pt.addBinding(element.getDefinition().getBinding());
            type.addType(pt);
        }
        td.addText(prop.getPath() + "." + src.getElement());
        if (src.hasVariable())
            result.add(VariableMode.INPUT, src.getVariable(), new PropertyWithType(prop.getPath() + "." + src.getElement(), element, null, type));
        return result;
    } else {
        // ditto!
        td.addText(prop.getPath());
        return vars.copy(optional, repeating);
    }
}
Also used : TypeDetails(org.hl7.fhir.dstu3.model.TypeDetails) ProfiledType(org.hl7.fhir.dstu3.model.TypeDetails.ProfiledType) TypeRefComponent(org.hl7.fhir.dstu3.model.ElementDefinition.TypeRefComponent) FHIRFormatError(org.hl7.fhir.exceptions.FHIRFormatError) FHIRException(org.hl7.fhir.exceptions.FHIRException) Property(org.hl7.fhir.dstu3.elementmodel.Property) FHIRLexerException(org.hl7.fhir.dstu3.utils.FHIRLexer.FHIRLexerException) DefinitionException(org.hl7.fhir.exceptions.DefinitionException) PathEngineException(org.hl7.fhir.exceptions.PathEngineException) IOException(java.io.IOException) FHIRException(org.hl7.fhir.exceptions.FHIRException)

Example 17 with ProfiledType

use of org.hl7.fhir.definitions.model.ProfiledType in project org.hl7.fhir.core by hapifhir.

the class FHIRPathEngine method getChildTypesByName.

private void getChildTypesByName(String type, String name, TypeDetails result, ExpressionNode expr) throws PathEngineException, DefinitionException {
    if (Utilities.noString(type))
        throw new PathEngineException("No type provided in BuildToolPathEvaluator.getChildTypesByName", expr.getStart(), expr.toString());
    if (type.equals("http://hl7.org/fhir/StructureDefinition/xhtml"))
        return;
    if (type.startsWith(Constants.NS_SYSTEM_TYPE))
        return;
    if (type.equals(TypeDetails.FP_SimpleTypeInfo)) {
        getSimpleTypeChildTypesByName(name, result);
    } else if (type.equals(TypeDetails.FP_ClassInfo)) {
        getClassInfoChildTypesByName(name, result);
    } else {
        String url = null;
        if (type.contains("#")) {
            url = type.substring(0, type.indexOf("#"));
        } else {
            url = 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.substring(type.indexOf("#") + 1), false, expr);
        if (m != null && hasDataType(m.definition)) {
            if (m.fixedType != null) {
                StructureDefinition dt = worker.fetchResource(StructureDefinition.class, ProfileUtilities.sdNs(m.fixedType, worker.getOverrideVersionNs()));
                if (dt == null)
                    throw new DefinitionException("unknown data type " + m.fixedType);
                sdl.add(dt);
            } else
                for (TypeRefComponent t : m.definition.getType()) {
                    StructureDefinition dt = worker.fetchResource(StructureDefinition.class, ProfileUtilities.sdNs(t.getCode(), worker.getOverrideVersionNs()));
                    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("#") + 1);
                tail = tail.substring(tail.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 = sdi.getType() + "#" + ed.getPath();
                                else
                                    tn = t.getCode();
                                if (t.getCode().equals("Resource")) {
                                    for (String rn : worker.getResourceNames()) {
                                        if (!result.hasType(worker, rn)) {
                                            getChildTypesByName(result.addType(rn), "**", result, expr);
                                        }
                                    }
                                } else if (!result.hasType(worker, tn)) {
                                    getChildTypesByName(result.addType(tn), "**", result, expr);
                                }
                            }
                        }
                }
            } 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 (// Element.id or Extension.url
                            Utilities.noString(t.getCode()))
                                result.addType("System.string");
                            else if (t.getCode().equals("Element") || t.getCode().equals("BackboneElement"))
                                result.addType(sdi.getType() + "#" + 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, expr);
                if (ed != null) {
                    if (!Utilities.noString(ed.getFixedType()))
                        result.addType(ed.getFixedType());
                    else
                        for (TypeRefComponent t : ed.getDefinition().getType()) {
                            if (Utilities.noString(t.getCode())) {
                                if (Utilities.existsInList(ed.getDefinition().getId(), "Element.id", "Extension.url") || Utilities.existsInList(ed.getDefinition().getBase().getPath(), "Resource.id", "Element.id", "Extension.url"))
                                    result.addType(TypeDetails.FP_NS, "string");
                                // throw new PathEngineException("Illegal reference to primitive value attribute @ "+path);
                                break;
                            }
                            ProfiledType pt = null;
                            if (t.getCode().equals("Element") || t.getCode().equals("BackboneElement"))
                                pt = new ProfiledType(sdi.getUrl() + "#" + path);
                            else if (t.getCode().equals("Resource"))
                                result.addTypes(worker.getResourceNames());
                            else
                                pt = new ProfiledType(t.getCode());
                            if (pt != null) {
                                if (t.hasProfile())
                                    pt.addProfiles(t.getProfile());
                                if (ed.getDefinition().hasBinding())
                                    pt.addBinding(ed.getDefinition().getBinding());
                                result.addType(pt);
                            }
                        }
                }
            }
        }
    }
}
Also used : ProfiledType(org.hl7.fhir.r4.model.TypeDetails.ProfiledType) TypeRefComponent(org.hl7.fhir.r4.model.ElementDefinition.TypeRefComponent) DefinitionException(org.hl7.fhir.exceptions.DefinitionException) PathEngineException(org.hl7.fhir.exceptions.PathEngineException)

Example 18 with ProfiledType

use of org.hl7.fhir.definitions.model.ProfiledType in project org.hl7.fhir.core by hapifhir.

the class StructureMapUtilities method analyseTransform.

private TypeDetails analyseTransform(TransformContext context, StructureMap map, StructureMapGroupRuleTargetComponent tgt, VariableForProfiling var, VariablesForProfiling vars) throws FHIRException {
    switch(tgt.getTransform()) {
        case CREATE:
            String p = getParamString(vars, tgt.getParameter().get(0));
            return new TypeDetails(CollectionStatus.SINGLETON, p);
        case COPY:
            return getParam(vars, tgt.getParameter().get(0));
        case EVALUATE:
            ExpressionNode expr = (ExpressionNode) tgt.getUserData(MAP_EXPRESSION);
            if (expr == null) {
                expr = fpe.parse(getParamString(vars, tgt.getParameter().get(tgt.getParameter().size() - 1)));
                tgt.setUserData(MAP_WHERE_EXPRESSION, expr);
            }
            return fpe.check(vars, null, expr);
        case TRANSLATE:
            return new TypeDetails(CollectionStatus.SINGLETON, "CodeableConcept");
        case CC:
            ProfiledType res = new ProfiledType("CodeableConcept");
            if (tgt.getParameter().size() >= 2 && isParamId(vars, tgt.getParameter().get(1))) {
                TypeDetails td = vars.get(null, getParamId(vars, tgt.getParameter().get(1))).getProperty().getTypes();
                if (td != null && td.hasBinding())
                    // todo: do we need to check that there's no implicit translation her? I don't think we do...
                    res.addBinding(td.getBinding());
            }
            return new TypeDetails(CollectionStatus.SINGLETON, res);
        case C:
            return new TypeDetails(CollectionStatus.SINGLETON, "Coding");
        case QTY:
            return new TypeDetails(CollectionStatus.SINGLETON, "Quantity");
        case REFERENCE:
            VariableForProfiling vrs = vars.get(VariableMode.OUTPUT, getParamId(vars, tgt.getParameterFirstRep()));
            if (vrs == null)
                throw new FHIRException("Unable to resolve variable \"" + getParamId(vars, tgt.getParameterFirstRep()) + "\"");
            String profile = vrs.getProperty().getProfileProperty().getStructure().getUrl();
            TypeDetails td = new TypeDetails(CollectionStatus.SINGLETON);
            td.addType("Reference", profile);
            return td;
        default:
            throw new Error("Transform Unknown or not handled yet: " + tgt.getTransform().toCode());
    }
}
Also used : ProfiledType(org.hl7.fhir.r4b.model.TypeDetails.ProfiledType) FHIRFormatError(org.hl7.fhir.exceptions.FHIRFormatError) FHIRException(org.hl7.fhir.exceptions.FHIRException)

Example 19 with ProfiledType

use of org.hl7.fhir.definitions.model.ProfiledType in project org.hl7.fhir.core by hapifhir.

the class StructureMapUtilities method analyseSource.

private VariablesForProfiling analyseSource(String ruleId, TransformContext context, VariablesForProfiling vars, StructureMapGroupRuleSourceComponent src, XhtmlNode td) throws FHIRException {
    VariableForProfiling var = vars.get(VariableMode.INPUT, src.getContext());
    if (var == null)
        throw new FHIRException("Rule \"" + ruleId + "\": Unknown input variable " + src.getContext());
    PropertyWithType prop = var.getProperty();
    boolean optional = false;
    boolean repeating = false;
    if (src.hasCondition()) {
        optional = true;
    }
    if (src.hasElement()) {
        Property element = prop.getBaseProperty().getChild(prop.getTypes().getType(), src.getElement());
        if (element == null)
            throw new FHIRException("Rule \"" + ruleId + "\": Unknown element name " + src.getElement());
        if (element.getDefinition().getMin() == 0)
            optional = true;
        if (element.getDefinition().getMax().equals("*"))
            repeating = true;
        VariablesForProfiling result = vars.copy(optional, repeating);
        TypeDetails type = new TypeDetails(CollectionStatus.SINGLETON);
        for (TypeRefComponent tr : element.getDefinition().getType()) {
            if (!tr.hasCode())
                throw new Error("Rule \"" + ruleId + "\": Element has no type");
            ProfiledType pt = new ProfiledType(tr.getWorkingCode());
            if (tr.hasProfile())
                pt.addProfiles(tr.getProfile());
            if (element.getDefinition().hasBinding())
                pt.addBinding(element.getDefinition().getBinding());
            type.addType(pt);
        }
        td.addText(prop.getPath() + "." + src.getElement());
        if (src.hasVariable())
            result.add(VariableMode.INPUT, src.getVariable(), new PropertyWithType(prop.getPath() + "." + src.getElement(), element, null, type));
        return result;
    } else {
        // ditto!
        td.addText(prop.getPath());
        return vars.copy(optional, repeating);
    }
}
Also used : ProfiledType(org.hl7.fhir.r4b.model.TypeDetails.ProfiledType) TypeRefComponent(org.hl7.fhir.r4b.model.ElementDefinition.TypeRefComponent) FHIRFormatError(org.hl7.fhir.exceptions.FHIRFormatError) FHIRException(org.hl7.fhir.exceptions.FHIRException) Property(org.hl7.fhir.r4b.elementmodel.Property)

Example 20 with ProfiledType

use of org.hl7.fhir.definitions.model.ProfiledType in project org.hl7.fhir.core by hapifhir.

the class FHIRPathEngine method getChildTypesByName.

private void getChildTypesByName(String type, String name, TypeDetails result, ExpressionNode expr) throws PathEngineException, DefinitionException {
    if (Utilities.noString(type)) {
        throw makeException(expr, I18nConstants.FHIRPATH_NO_TYPE, "", "getChildTypesByName");
    }
    if (type.equals("http://hl7.org/fhir/StructureDefinition/xhtml")) {
        return;
    }
    if (type.startsWith(Constants.NS_SYSTEM_TYPE)) {
        return;
    }
    if (type.equals(TypeDetails.FP_SimpleTypeInfo)) {
        getSimpleTypeChildTypesByName(name, result);
    } else if (type.equals(TypeDetails.FP_ClassInfo)) {
        getClassInfoChildTypesByName(name, result);
    } else {
        String url = null;
        if (type.contains("#")) {
            url = type.substring(0, type.indexOf("#"));
        } else {
            url = type;
        }
        String tail = "";
        StructureDefinition sd = worker.fetchResource(StructureDefinition.class, url);
        if (sd == null) {
            throw makeException(expr, I18nConstants.FHIRPATH_NO_TYPE, url, "getChildTypesByName");
        }
        List<StructureDefinition> sdl = new ArrayList<StructureDefinition>();
        ElementDefinitionMatch m = null;
        if (type.contains("#"))
            m = getElementDefinition(sd, type.substring(type.indexOf("#") + 1), false, expr);
        if (m != null && hasDataType(m.definition)) {
            if (m.fixedType != null) {
                StructureDefinition dt = worker.fetchResource(StructureDefinition.class, ProfileUtilities.sdNs(m.fixedType, worker.getOverrideVersionNs()));
                if (dt == null) {
                    throw makeException(expr, I18nConstants.FHIRPATH_NO_TYPE, ProfileUtilities.sdNs(m.fixedType, worker.getOverrideVersionNs()), "getChildTypesByName");
                }
                sdl.add(dt);
            } else
                for (TypeRefComponent t : m.definition.getType()) {
                    StructureDefinition dt = worker.fetchResource(StructureDefinition.class, ProfileUtilities.sdNs(t.getCode(), worker.getOverrideVersionNs()));
                    if (dt == null) {
                        throw makeException(expr, I18nConstants.FHIRPATH_NO_TYPE, ProfileUtilities.sdNs(t.getCode(), worker.getOverrideVersionNs()), "getChildTypesByName");
                    }
                    sdl.add(dt);
                }
        } else {
            sdl.add(sd);
            if (type.contains("#")) {
                tail = type.substring(type.indexOf("#") + 1);
                tail = tail.substring(tail.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 = sdi.getType() + "#" + ed.getPath();
                                } else {
                                    tn = t.getCode();
                                }
                                if (t.getCode().equals("Resource")) {
                                    for (String rn : worker.getResourceNames()) {
                                        if (!result.hasType(worker, rn)) {
                                            getChildTypesByName(result.addType(rn), "**", result, expr);
                                        }
                                    }
                                } else if (!result.hasType(worker, tn)) {
                                    getChildTypesByName(result.addType(tn), "**", result, expr);
                                }
                            }
                        }
                }
            } 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 (Utilities.noString(t.getCode())) {
                                // Element.id or Extension.url
                                result.addType("System.string");
                            } else if (t.getCode().equals("Element") || t.getCode().equals("BackboneElement")) {
                                result.addType(sdi.getType() + "#" + 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, isAllowPolymorphicNames(), expr);
                if (ed != null) {
                    if (!Utilities.noString(ed.getFixedType()))
                        result.addType(ed.getFixedType());
                    else {
                        for (TypeRefComponent t : ed.getDefinition().getType()) {
                            if (Utilities.noString(t.getCode())) {
                                if (Utilities.existsInList(ed.getDefinition().getId(), "Element.id", "Extension.url") || Utilities.existsInList(ed.getDefinition().getBase().getPath(), "Resource.id", "Element.id", "Extension.url")) {
                                    result.addType(TypeDetails.FP_NS, "string");
                                }
                                // throw new PathEngineException("Illegal reference to primitive value attribute @ "+path);
                                break;
                            }
                            ProfiledType pt = null;
                            if (t.getCode().equals("Element") || t.getCode().equals("BackboneElement")) {
                                pt = new ProfiledType(sdi.getUrl() + "#" + path);
                            } else if (t.getCode().equals("Resource")) {
                                result.addTypes(worker.getResourceNames());
                            } else {
                                pt = new ProfiledType(t.getCode());
                            }
                            if (pt != null) {
                                if (t.hasProfile()) {
                                    pt.addProfiles(t.getProfile());
                                }
                                if (ed.getDefinition().hasBinding()) {
                                    pt.addBinding(ed.getDefinition().getBinding());
                                }
                                result.addType(pt);
                            }
                        }
                    }
                }
            }
        }
    }
}
Also used : StructureDefinition(org.hl7.fhir.r5.model.StructureDefinition) ProfiledType(org.hl7.fhir.r5.model.TypeDetails.ProfiledType) TypeRefComponent(org.hl7.fhir.r5.model.ElementDefinition.TypeRefComponent) MergedList(org.hl7.fhir.utilities.MergedList) List(java.util.List) ArrayList(java.util.ArrayList) ElementDefinition(org.hl7.fhir.r5.model.ElementDefinition)

Aggregations

FHIRException (org.hl7.fhir.exceptions.FHIRException)19 ProfiledType (org.hl7.fhir.definitions.model.ProfiledType)14 ArrayList (java.util.ArrayList)11 FHIRFormatError (org.hl7.fhir.exceptions.FHIRFormatError)9 ElementDefn (org.hl7.fhir.definitions.model.ElementDefn)7 TypeRef (org.hl7.fhir.definitions.model.TypeRef)7 DefinitionException (org.hl7.fhir.exceptions.DefinitionException)7 StructureDefinition (org.hl7.fhir.r5.model.StructureDefinition)6 IOException (java.io.IOException)5 ElementDefinition (org.hl7.fhir.r5.model.ElementDefinition)5 URISyntaxException (java.net.URISyntaxException)4 ProfiledType (org.hl7.fhir.dstu3.model.TypeDetails.ProfiledType)4 ProfiledType (org.hl7.fhir.r4.model.TypeDetails.ProfiledType)4 ProfiledType (org.hl7.fhir.r4b.model.TypeDetails.ProfiledType)4 TypeRefComponent (org.hl7.fhir.r5.model.ElementDefinition.TypeRefComponent)4 ProfiledType (org.hl7.fhir.r5.model.TypeDetails.ProfiledType)4 FileNotFoundException (java.io.FileNotFoundException)3 UnsupportedEncodingException (java.io.UnsupportedEncodingException)3 List (java.util.List)3 DefinedCode (org.hl7.fhir.definitions.model.DefinedCode)3