Search in sources :

Example 26 with StructureMapGroupRuleTargetComponent

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

the class StructureMapUtilities method updateProfile.

private PropertyWithType updateProfile(VariableForProfiling var, String element, TypeDetails type, StructureMap map, List<StructureDefinition> profiles, String sliceName, DataType fixed, StructureMapGroupRuleTargetComponent tgt) throws FHIRException {
    if (var == null) {
        assert (Utilities.noString(element));
        // 1. start the new structure definition
        StructureDefinition sdn = worker.fetchResource(StructureDefinition.class, type.getType());
        if (sdn == null)
            throw new FHIRException("Unable to find definition for " + type.getType());
        ElementDefinition edn = sdn.getSnapshot().getElementFirstRep();
        PropertyWithType pn = createProfile(map, profiles, new PropertyWithType(sdn.getId(), new Property(worker, edn, sdn), null, type), sliceName, tgt);
        return pn;
    } else {
        assert (!Utilities.noString(element));
        Property pvb = var.getProperty().getBaseProperty();
        Property pvd = var.getProperty().getProfileProperty();
        Property pc = pvb.getChild(element, var.getProperty().getTypes());
        if (pc == null)
            throw new DefinitionException("Unable to find a definition for " + pvb.getDefinition().getPath() + "." + element);
        // the profile structure definition (derived)
        StructureDefinition sd = var.getProperty().getProfileProperty().getStructure();
        ElementDefinition ednew = sd.getDifferential().addElement();
        ednew.setPath(var.getProperty().getProfileProperty().getDefinition().getPath() + "." + pc.getName());
        ednew.setUserData("slice-name", sliceName);
        ednew.setFixed(fixed);
        for (ProfiledType pt : type.getProfiledTypes()) {
            if (pt.hasBindings())
                ednew.setBinding(pt.getBindings().get(0));
            if (pt.getUri().startsWith("http://hl7.org/fhir/StructureDefinition/")) {
                String t = pt.getUri().substring(40);
                t = checkType(t, pc, pt.getProfiles());
                if (t != null) {
                    if (pt.hasProfiles()) {
                        for (String p : pt.getProfiles()) if (t.equals("Reference"))
                            ednew.getType(t).addTargetProfile(p);
                        else
                            ednew.getType(t).addProfile(p);
                    } else
                        ednew.getType(t);
                }
            }
        }
        return new PropertyWithType(var.getProperty().getPath() + "." + element, pc, new Property(worker, ednew, sd), type);
    }
}
Also used : ProfiledType(org.hl7.fhir.r5.model.TypeDetails.ProfiledType) DefinitionException(org.hl7.fhir.exceptions.DefinitionException) FHIRException(org.hl7.fhir.exceptions.FHIRException) Property(org.hl7.fhir.r5.elementmodel.Property)

Example 27 with StructureMapGroupRuleTargetComponent

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

the class StructureMapUtilities method targetToString.

public static String targetToString(StructureMapGroupRuleTargetComponent rt) {
    StringBuilder b = new StringBuilder();
    renderTarget(b, rt, false);
    return b.toString();
}
Also used : CommaSeparatedStringBuilder(org.hl7.fhir.utilities.CommaSeparatedStringBuilder)

Example 28 with StructureMapGroupRuleTargetComponent

use of org.hl7.fhir.r5.model.StructureMap.StructureMapGroupRuleTargetComponent 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.r5.model.TypeDetails.ProfiledType) FHIRFormatError(org.hl7.fhir.exceptions.FHIRFormatError) FHIRException(org.hl7.fhir.exceptions.FHIRException)

Example 29 with StructureMapGroupRuleTargetComponent

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

the class StructureMapUtilitiesTest method assertSerializeDeserialize.

private void assertSerializeDeserialize(StructureMap structureMap) {
    Assertions.assertEquals("syntax", structureMap.getName());
    Assertions.assertEquals("Title of this map\r\nAuthor", structureMap.getDescription());
    Assertions.assertEquals("http://github.com/FHIR/fhir-test-cases/r5/fml/syntax", structureMap.getUrl());
    Assertions.assertEquals("Patient", structureMap.getStructure().get(0).getAlias());
    Assertions.assertEquals("http://hl7.org/fhir/StructureDefinition/Patient", structureMap.getStructure().get(0).getUrl());
    Assertions.assertEquals("Source Documentation", structureMap.getStructure().get(0).getDocumentation());
    Assertions.assertEquals("http://hl7.org/fhir/StructureDefinition/Patient", structureMap.getStructure().get(0).getUrl());
    Assertions.assertEquals("http://hl7.org/fhir/StructureDefinition/Basic", structureMap.getStructure().get(1).getUrl());
    Assertions.assertEquals("Target Documentation", structureMap.getStructure().get(1).getDocumentation());
    Assertions.assertEquals("Groups\r\nrule for patient group", structureMap.getGroup().get(0).getDocumentation());
    Assertions.assertEquals("Comment to rule", structureMap.getGroup().get(0).getRule().get(0).getDocumentation());
    Assertions.assertEquals("Copy identifier short syntax", structureMap.getGroup().get(0).getRule().get(1).getDocumentation());
    StructureMapGroupRuleTargetComponent target = structureMap.getGroup().get(0).getRule().get(2).getTarget().get(1);
    Assertions.assertEquals("'urn:uuid:' + r.lower()", target.getParameter().get(0).toString());
}
Also used : StructureMapGroupRuleTargetComponent(org.hl7.fhir.r4b.model.StructureMap.StructureMapGroupRuleTargetComponent)

Example 30 with StructureMapGroupRuleTargetComponent

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

the class StructureMapUtilities method runTransform.

private Base runTransform(String ruleId, TransformContext context, StructureMap map, StructureMapGroupComponent group, StructureMapGroupRuleTargetComponent tgt, Variables vars, Base dest, String element, String srcVar, boolean root) throws FHIRException {
    try {
        switch(tgt.getTransform()) {
            case CREATE:
                String tn;
                if (tgt.getParameter().isEmpty()) {
                    // we have to work out the type. First, we see if there is a single type for the target. If there is, we use that
                    String[] types = dest.getTypesForProperty(element.hashCode(), element);
                    if (types.length == 1 && !"*".equals(types[0]) && !types[0].equals("Resource"))
                        tn = types[0];
                    else if (srcVar != null) {
                        tn = determineTypeFromSourceType(map, group, vars.get(VariableMode.INPUT, srcVar), types);
                    } else
                        throw new Error("Cannot determine type implicitly because there is no single input variable");
                } else {
                    tn = getParamStringNoNull(vars, tgt.getParameter().get(0), tgt.toString());
                    // ok, now we resolve the type name against the import statements
                    for (StructureMapStructureComponent uses : map.getStructure()) {
                        if (uses.getMode() == StructureMapModelMode.TARGET && uses.hasAlias() && tn.equals(uses.getAlias())) {
                            tn = uses.getUrl();
                            break;
                        }
                    }
                }
                Base res = services != null ? services.createType(context.getAppInfo(), tn) : ResourceFactory.createResourceOrType(tn);
                if (res.isResource() && !res.fhirType().equals("Parameters")) {
                    // res.setIdBase(tgt.getParameter().size() > 1 ? getParamString(vars, tgt.getParameter().get(0)) : UUID.randomUUID().toString().toLowerCase());
                    if (services != null)
                        res = services.createResource(context.getAppInfo(), res, root);
                }
                if (tgt.hasUserData("profile"))
                    res.setUserData("profile", tgt.getUserData("profile"));
                return res;
            case COPY:
                return getParam(vars, tgt.getParameter().get(0));
            case EVALUATE:
                ExpressionNode expr = (ExpressionNode) tgt.getUserData(MAP_EXPRESSION);
                if (expr == null) {
                    expr = fpe.parse(getParamStringNoNull(vars, tgt.getParameter().get(1), tgt.toString()));
                    tgt.setUserData(MAP_WHERE_EXPRESSION, expr);
                }
                List<Base> v = fpe.evaluate(vars, null, null, tgt.getParameter().size() == 2 ? getParam(vars, tgt.getParameter().get(0)) : new BooleanType(false), expr);
                if (v.size() == 0)
                    return null;
                else if (v.size() != 1)
                    throw new FHIRException("Rule \"" + ruleId + "\": Evaluation of " + expr.toString() + " returned " + v.size() + " objects");
                else
                    return v.get(0);
            case TRUNCATE:
                String src = getParamString(vars, tgt.getParameter().get(0));
                String len = getParamStringNoNull(vars, tgt.getParameter().get(1), tgt.toString());
                if (Utilities.isInteger(len)) {
                    int l = Integer.parseInt(len);
                    if (src.length() > l)
                        src = src.substring(0, l);
                }
                return new StringType(src);
            case ESCAPE:
                throw new Error("Rule \"" + ruleId + "\": Transform " + tgt.getTransform().toCode() + " not supported yet");
            case CAST:
                src = getParamString(vars, tgt.getParameter().get(0));
                if (tgt.getParameter().size() == 1)
                    throw new FHIRException("Implicit type parameters on cast not yet supported");
                String t = getParamString(vars, tgt.getParameter().get(1));
                if (t.equals("string"))
                    return new StringType(src);
                else
                    throw new FHIRException("cast to " + t + " not yet supported");
            case APPEND:
                StringBuilder sb = new StringBuilder(getParamString(vars, tgt.getParameter().get(0)));
                for (int i = 1; i < tgt.getParameter().size(); i++) sb.append(getParamString(vars, tgt.getParameter().get(i)));
                return new StringType(sb.toString());
            case TRANSLATE:
                return translate(context, map, vars, tgt.getParameter());
            case REFERENCE:
                Base b = getParam(vars, tgt.getParameter().get(0));
                if (b == null)
                    throw new FHIRException("Rule \"" + ruleId + "\": Unable to find parameter " + ((IdType) tgt.getParameter().get(0).getValue()).asStringValue());
                if (!b.isResource())
                    throw new FHIRException("Rule \"" + ruleId + "\": Transform engine cannot point at an element of type " + b.fhirType());
                else {
                    String id = b.getIdBase();
                    if (id == null) {
                        id = UUID.randomUUID().toString().toLowerCase();
                        b.setIdBase(id);
                    }
                    return new StringType(b.fhirType() + "/" + id);
                }
            case DATEOP:
                throw new Error("Rule \"" + ruleId + "\": Transform " + tgt.getTransform().toCode() + " not supported yet");
            case UUID:
                return new IdType(UUID.randomUUID().toString());
            case POINTER:
                b = getParam(vars, tgt.getParameter().get(0));
                if (b instanceof Resource)
                    return new UriType("urn:uuid:" + ((Resource) b).getId());
                else
                    throw new FHIRException("Rule \"" + ruleId + "\": Transform engine cannot point at an element of type " + b.fhirType());
            case CC:
                CodeableConcept cc = new CodeableConcept();
                cc.addCoding(buildCoding(getParamStringNoNull(vars, tgt.getParameter().get(0), tgt.toString()), getParamStringNoNull(vars, tgt.getParameter().get(1), tgt.toString())));
                return cc;
            case C:
                Coding c = buildCoding(getParamStringNoNull(vars, tgt.getParameter().get(0), tgt.toString()), getParamStringNoNull(vars, tgt.getParameter().get(1), tgt.toString()));
                return c;
            default:
                throw new Error("Rule \"" + ruleId + "\": Transform Unknown: " + tgt.getTransform().toCode());
        }
    } catch (Exception e) {
        throw new FHIRException("Exception executing transform " + tgt.toString() + " on Rule \"" + ruleId + "\": " + e.getMessage(), e);
    }
}
Also used : CommaSeparatedStringBuilder(org.hl7.fhir.utilities.CommaSeparatedStringBuilder) FHIRFormatError(org.hl7.fhir.exceptions.FHIRFormatError) FHIRException(org.hl7.fhir.exceptions.FHIRException) DefinitionException(org.hl7.fhir.exceptions.DefinitionException) IOException(java.io.IOException) FHIRException(org.hl7.fhir.exceptions.FHIRException) FHIRLexerException(org.hl7.fhir.r5.utils.FHIRLexer.FHIRLexerException)

Aggregations

FHIRException (org.hl7.fhir.exceptions.FHIRException)35 FHIRFormatError (org.hl7.fhir.exceptions.FHIRFormatError)12 DefinitionException (org.hl7.fhir.exceptions.DefinitionException)10 CommaSeparatedStringBuilder (org.hl7.fhir.utilities.CommaSeparatedStringBuilder)7 IOException (java.io.IOException)6 StringType (org.hl7.fhir.dstu3.model.StringType)6 StringType (org.hl7.fhir.r4.model.StringType)6 IdType (org.hl7.fhir.dstu3.model.IdType)4 StructureMapGroupRuleTargetComponent (org.hl7.fhir.dstu3.model.StructureMap.StructureMapGroupRuleTargetComponent)4 ProfiledType (org.hl7.fhir.dstu3.model.TypeDetails.ProfiledType)4 PathEngineException (org.hl7.fhir.exceptions.PathEngineException)4 StructureMapGroupRuleTargetComponent (org.hl7.fhir.r4.model.StructureMap.StructureMapGroupRuleTargetComponent)4 NodeType (org.hl7.fhir.utilities.xhtml.NodeType)4 StringType (org.hl7.fhir.dstu2016may.model.StringType)3 StructureMapGroupRuleTargetComponent (org.hl7.fhir.dstu2016may.model.StructureMap.StructureMapGroupRuleTargetComponent)3 Base (org.hl7.fhir.dstu3.model.Base)3 BooleanType (org.hl7.fhir.dstu3.model.BooleanType)3 UriType (org.hl7.fhir.dstu3.model.UriType)3 Base (org.hl7.fhir.r4.model.Base)3 IdType (org.hl7.fhir.r4.model.IdType)3