Search in sources :

Example 11 with TypeDetails

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

the class FFHIRPathHostServices method resolveConstantType.

@Override
public TypeDetails resolveConstantType(Object appContext, String name) throws PathEngineException {
    if (!(appContext instanceof VariablesForProfiling))
        throw new Error("Internal Logic Error (wrong type '" + appContext.getClass().getName() + "' in resolveConstantType)");
    VariablesForProfiling vars = (VariablesForProfiling) appContext;
    VariableForProfiling v = vars.get(null, name);
    if (v == null)
        throw new PathEngineException("Unknown variable '" + name + "' from variables " + vars.summary());
    return v.getProperty().getTypes();
}
Also used : PathEngineException(org.hl7.fhir.exceptions.PathEngineException)

Example 12 with TypeDetails

use of org.hl7.fhir.r5.model.TypeDetails 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)

Example 13 with TypeDetails

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

the class FHIRPathEngine method checkParamTypes.

private void checkParamTypes(ExpressionNode expr, String funcName, List<TypeDetails> paramTypes, TypeDetails... typeSet) throws PathEngineException {
    int i = 0;
    for (TypeDetails pt : typeSet) {
        if (i == paramTypes.size()) {
            return;
        }
        TypeDetails actual = paramTypes.get(i);
        i++;
        for (String a : actual.getTypes()) {
            if (!pt.hasType(worker, a)) {
                throw makeException(expr, I18nConstants.FHIRPATH_WRONG_PARAM_TYPE, funcName, i, a, pt.toString());
            }
        }
    }
}
Also used : TypeDetails(org.hl7.fhir.r5.model.TypeDetails)

Example 14 with TypeDetails

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

the class FHIRPathEngine method getChildTypesByName.

private void getChildTypesByName(String type, String name, TypeDetails result) throws PathEngineException, DefinitionException {
    if (Utilities.noString(type))
        throw new PathEngineException("No type provided in BuildToolPathEvaluator.getChildTypesByName");
    if (type.equals("http://hl7.org/fhir/StructureDefinition/xhtml"))
        return;
    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);
    if (m != null && hasDataType(m.definition)) {
        if (m.fixedType != null) {
            StructureDefinition dt = worker.fetchTypeDefinition(m.fixedType);
            if (dt == null)
                throw new DefinitionException("unknown data type " + m.fixedType);
            sdl.add(dt);
        } else
            for (TypeRefComponent t : m.definition.getType()) {
                StructureDefinition dt = worker.fetchTypeDefinition(t.getCode());
                if (dt == null)
                    throw new DefinitionException("unknown data type " + t.getCode());
                sdl.add(dt);
            }
    } else {
        sdl.add(sd);
        if (type.contains("#")) {
            tail = type.substring(type.indexOf("#") + 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);
                                    }
                                }
                            } else if (!result.hasType(worker, tn)) {
                                getChildTypesByName(result.addType(tn), "**", result);
                            }
                        }
                    }
            }
        } else if (name.equals("*")) {
            assert (result.getCollectionStatus() == CollectionStatus.UNORDERED);
            for (ElementDefinition ed : sdi.getSnapshot().getElement()) {
                if (ed.getPath().startsWith(path) && !ed.getPath().substring(path.length()).contains("."))
                    for (TypeRefComponent t : ed.getType()) {
                        if (t.getCode().equals("Element") || t.getCode().equals("BackboneElement"))
                            result.addType(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);
            if (ed != null) {
                if (!Utilities.noString(ed.getFixedType()))
                    result.addType(ed.getFixedType());
                else
                    for (TypeRefComponent t : ed.getDefinition().getType()) {
                        if (Utilities.noString(t.getCode()))
                            // throw new PathEngineException("Illegal reference to primitive value attribute @ "+path);
                            break;
                        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.addProfile(t.getProfile());
                            if (ed.getDefinition().hasBinding())
                                pt.addBinding(ed.getDefinition().getBinding());
                            result.addType(pt);
                        }
                    }
            }
        }
    }
}
Also used : ProfiledType(org.hl7.fhir.dstu3.model.TypeDetails.ProfiledType) TypeRefComponent(org.hl7.fhir.dstu3.model.ElementDefinition.TypeRefComponent) DefinitionException(org.hl7.fhir.exceptions.DefinitionException) PathEngineException(org.hl7.fhir.exceptions.PathEngineException)

Example 15 with TypeDetails

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

the class ComparisonRenderer method resolveConstantType.

@Override
public TypeDetails resolveConstantType(Object appContext, String name) throws PathEngineException {
    @SuppressWarnings("unchecked") Map<String, Base> vars = (Map<String, Base>) appContext;
    Base b = vars.get(name);
    return new TypeDetails(CollectionStatus.SINGLETON, b == null ? "Base" : b.fhirType());
}
Also used : TypeDetails(org.hl7.fhir.r5.model.TypeDetails) HashMap(java.util.HashMap) Map(java.util.Map) Base(org.hl7.fhir.r5.model.Base)

Aggregations

DefinitionException (org.hl7.fhir.exceptions.DefinitionException)18 PathEngineException (org.hl7.fhir.exceptions.PathEngineException)18 FHIRException (org.hl7.fhir.exceptions.FHIRException)16 FHIRFormatError (org.hl7.fhir.exceptions.FHIRFormatError)12 ArrayList (java.util.ArrayList)9 ProfiledType (org.hl7.fhir.dstu3.model.TypeDetails.ProfiledType)6 ProfiledType (org.hl7.fhir.r4.model.TypeDetails.ProfiledType)6 TypeDetails (org.hl7.fhir.dstu2016may.model.ExpressionNode.TypeDetails)5 TypeDetails (org.hl7.fhir.dstu2.model.ExpressionNode.TypeDetails)4 TypeDetails (org.hl7.fhir.dstu3.model.TypeDetails)4 TypeDetails (org.hl7.fhir.r4.model.TypeDetails)4 TypeDetails (org.hl7.fhir.r5.model.TypeDetails)4 NodeType (org.hl7.fhir.utilities.xhtml.NodeType)4 Property (org.hl7.fhir.dstu3.elementmodel.Property)3 TypeRefComponent (org.hl7.fhir.r4.model.ElementDefinition.TypeRefComponent)3 Property (org.hl7.fhir.r4b.elementmodel.Property)3 TypeRefComponent (org.hl7.fhir.r4b.model.ElementDefinition.TypeRefComponent)3 TypeDetails (org.hl7.fhir.r4b.model.TypeDetails)3 ProfiledType (org.hl7.fhir.r4b.model.TypeDetails.ProfiledType)3 Property (org.hl7.fhir.r5.elementmodel.Property)3