Search in sources :

Example 41 with StructureMapGroupRuleTargetComponent

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

the class StructureMapUtilities method describeTransformCCorC.

@SuppressWarnings("rawtypes")
private String describeTransformCCorC(StructureMapGroupRuleTargetComponent tgt) throws FHIRException {
    if (tgt.getParameter().size() < 2)
        return null;
    Type p1 = tgt.getParameter().get(0).getValue();
    Type p2 = tgt.getParameter().get(1).getValue();
    if (p1 instanceof IdType || p2 instanceof IdType)
        return null;
    if (!(p1 instanceof PrimitiveType) || !(p2 instanceof PrimitiveType))
        return null;
    String uri = ((PrimitiveType) p1).asStringValue();
    String code = ((PrimitiveType) p2).asStringValue();
    if (Utilities.noString(uri))
        throw new FHIRException("Describe Transform, but the uri is blank");
    if (Utilities.noString(code))
        throw new FHIRException("Describe Transform, but the code is blank");
    Coding c = buildCoding(uri, code);
    return NarrativeGenerator.describeSystem(c.getSystem()) + "#" + c.getCode() + (c.hasDisplay() ? "(" + c.getDisplay() + ")" : "");
}
Also used : NodeType(org.hl7.fhir.utilities.xhtml.NodeType) PrimitiveType(org.hl7.fhir.r4.model.PrimitiveType) ProfiledType(org.hl7.fhir.r4.model.TypeDetails.ProfiledType) StringType(org.hl7.fhir.r4.model.StringType) IntegerType(org.hl7.fhir.r4.model.IntegerType) StructureMapContextType(org.hl7.fhir.r4.model.StructureMap.StructureMapContextType) BooleanType(org.hl7.fhir.r4.model.BooleanType) UriType(org.hl7.fhir.r4.model.UriType) Type(org.hl7.fhir.r4.model.Type) DecimalType(org.hl7.fhir.r4.model.DecimalType) CanonicalType(org.hl7.fhir.r4.model.CanonicalType) IdType(org.hl7.fhir.r4.model.IdType) CodeType(org.hl7.fhir.r4.model.CodeType) Coding(org.hl7.fhir.r4.model.Coding) PrimitiveType(org.hl7.fhir.r4.model.PrimitiveType) FHIRException(org.hl7.fhir.exceptions.FHIRException) IdType(org.hl7.fhir.r4.model.IdType)

Example 42 with StructureMapGroupRuleTargetComponent

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

the class StructureMapUtilities method analyseTarget.

private void analyseTarget(String ruleId, TransformContext context, VariablesForProfiling vars, StructureMap map, StructureMapGroupRuleTargetComponent tgt, String tv, TargetWriter tw, List<StructureDefinition> profiles, String sliceName) throws FHIRException {
    VariableForProfiling var = null;
    if (tgt.hasContext()) {
        var = vars.get(VariableMode.OUTPUT, tgt.getContext());
        if (var == null)
            throw new FHIRException("Rule \"" + ruleId + "\": target context not known: " + tgt.getContext());
        if (!tgt.hasElement())
            throw new FHIRException("Rule \"" + ruleId + "\": Not supported yet");
    }
    TypeDetails type = null;
    if (tgt.hasTransform()) {
        type = analyseTransform(context, map, tgt, var, vars);
    // profiling: dest.setProperty(tgt.getElement().hashCode(), tgt.getElement(), v);
    } else {
        Property vp = var.property.baseProperty.getChild(tgt.getElement(), tgt.getElement());
        if (vp == null)
            throw new FHIRException("Unknown Property " + tgt.getElement() + " on " + var.property.path);
        type = new TypeDetails(CollectionStatus.SINGLETON, vp.getType(tgt.getElement()));
    }
    if (tgt.getTransform() == StructureMapTransform.CREATE) {
        String s = getParamString(vars, tgt.getParameter().get(0));
        if (worker.getResourceNames().contains(s))
            tw.newResource(tgt.getVariable(), s);
    } else {
        boolean mapsSrc = false;
        for (StructureMapGroupRuleTargetParameterComponent p : tgt.getParameter()) {
            Type pr = p.getValue();
            if (pr instanceof IdType && ((IdType) pr).asStringValue().equals(tv))
                mapsSrc = true;
        }
        if (mapsSrc) {
            if (var == null)
                throw new Error("Rule \"" + ruleId + "\": Attempt to assign with no context");
            tw.valueAssignment(tgt.getContext(), var.property.getPath() + "." + tgt.getElement() + getTransformSuffix(tgt.getTransform()));
        } else if (tgt.hasContext()) {
            if (isSignificantElement(var.property, tgt.getElement())) {
                String td = describeTransform(tgt);
                if (td != null)
                    tw.keyAssignment(tgt.getContext(), var.property.getPath() + "." + tgt.getElement() + " = " + td);
            }
        }
    }
    Type fixed = generateFixedValue(tgt);
    PropertyWithType prop = updateProfile(var, tgt.getElement(), type, map, profiles, sliceName, fixed, tgt);
    if (tgt.hasVariable())
        if (tgt.hasElement())
            vars.add(VariableMode.OUTPUT, tgt.getVariable(), prop);
        else
            vars.add(VariableMode.OUTPUT, tgt.getVariable(), prop);
}
Also used : TypeDetails(org.hl7.fhir.r4.model.TypeDetails) NodeType(org.hl7.fhir.utilities.xhtml.NodeType) PrimitiveType(org.hl7.fhir.r4.model.PrimitiveType) ProfiledType(org.hl7.fhir.r4.model.TypeDetails.ProfiledType) StringType(org.hl7.fhir.r4.model.StringType) IntegerType(org.hl7.fhir.r4.model.IntegerType) StructureMapContextType(org.hl7.fhir.r4.model.StructureMap.StructureMapContextType) BooleanType(org.hl7.fhir.r4.model.BooleanType) UriType(org.hl7.fhir.r4.model.UriType) Type(org.hl7.fhir.r4.model.Type) DecimalType(org.hl7.fhir.r4.model.DecimalType) CanonicalType(org.hl7.fhir.r4.model.CanonicalType) IdType(org.hl7.fhir.r4.model.IdType) CodeType(org.hl7.fhir.r4.model.CodeType) FHIRFormatError(org.hl7.fhir.exceptions.FHIRFormatError) StructureMapGroupRuleTargetParameterComponent(org.hl7.fhir.r4.model.StructureMap.StructureMapGroupRuleTargetParameterComponent) FHIRException(org.hl7.fhir.exceptions.FHIRException) Property(org.hl7.fhir.r4.elementmodel.Property) IdType(org.hl7.fhir.r4.model.IdType)

Example 43 with StructureMapGroupRuleTargetComponent

use of org.hl7.fhir.r4.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);
        // //  throw new Error("Transform "+tgt.getTransform().toCode()+" not supported yet");
        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))).property.types;
                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.property.getProfileProperty().getStructure().getUrl();
            TypeDetails td = new TypeDetails(CollectionStatus.SINGLETON);
            td.addType("Reference", profile);
            return td;
        // //    throw new FHIRException("Transform engine cannot point at an element of type "+b.fhirType());
        default:
            throw new Error("Transform Unknown or not handled yet: " + tgt.getTransform().toCode());
    }
}
Also used : TypeDetails(org.hl7.fhir.r4.model.TypeDetails) ProfiledType(org.hl7.fhir.r4.model.TypeDetails.ProfiledType) ExpressionNode(org.hl7.fhir.r4.model.ExpressionNode) FHIRFormatError(org.hl7.fhir.exceptions.FHIRFormatError) FHIRException(org.hl7.fhir.exceptions.FHIRException)

Example 44 with StructureMapGroupRuleTargetComponent

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

the class StructureMapUtilities method renderRule.

private static void renderRule(StringBuilder b, StructureMapGroupRuleComponent r, int indent) {
    for (int i = 0; i < indent; i++) b.append(' ');
    boolean canBeAbbreviated = checkisSimple(r);
    boolean first = true;
    for (StructureMapGroupRuleSourceComponent rs : r.getSource()) {
        if (first)
            first = false;
        else
            b.append(", ");
        renderSource(b, rs, canBeAbbreviated);
    }
    if (r.getTarget().size() > 1) {
        b.append(" -> ");
        first = true;
        for (StructureMapGroupRuleTargetComponent rt : r.getTarget()) {
            if (first)
                first = false;
            else
                b.append(", ");
            if (RENDER_MULTIPLE_TARGETS_ONELINE)
                b.append(' ');
            else {
                b.append("\r\n");
                for (int i = 0; i < indent + 4; i++) b.append(' ');
            }
            renderTarget(b, rt, false);
        }
    } else if (r.hasTarget()) {
        b.append(" -> ");
        renderTarget(b, r.getTarget().get(0), canBeAbbreviated);
    }
    if (r.hasRule()) {
        b.append(" then {\r\n");
        renderDoco(b, r.getDocumentation());
        for (StructureMapGroupRuleComponent ir : r.getRule()) {
            renderRule(b, ir, indent + 2);
        }
        for (int i = 0; i < indent; i++) b.append(' ');
        b.append("}");
    } else {
        if (r.hasDependent()) {
            b.append(" then ");
            first = true;
            for (StructureMapGroupRuleDependentComponent rd : r.getDependent()) {
                if (first)
                    first = false;
                else
                    b.append(", ");
                b.append(rd.getName());
                b.append("(");
                boolean ifirst = true;
                for (StringType rdp : rd.getVariable()) {
                    if (ifirst)
                        ifirst = false;
                    else
                        b.append(", ");
                    b.append(rdp.asStringValue());
                }
                b.append(")");
            }
        }
    }
    if (r.hasName()) {
        String n = ntail(r.getName());
        if (!n.startsWith("\""))
            n = "\"" + n + "\"";
        if (!matchesName(n, r.getSource())) {
            b.append(" ");
            b.append(n);
        }
    }
    b.append(";");
    renderDoco(b, r.getDocumentation());
    b.append("\r\n");
}
Also used : StringType(org.hl7.fhir.r4.model.StringType) StructureMapGroupRuleDependentComponent(org.hl7.fhir.r4.model.StructureMap.StructureMapGroupRuleDependentComponent) ContactPoint(org.hl7.fhir.r4.model.ContactPoint) StructureMapGroupRuleSourceComponent(org.hl7.fhir.r4.model.StructureMap.StructureMapGroupRuleSourceComponent) StructureMapGroupRuleTargetComponent(org.hl7.fhir.r4.model.StructureMap.StructureMapGroupRuleTargetComponent) StructureMapGroupRuleComponent(org.hl7.fhir.r4.model.StructureMap.StructureMapGroupRuleComponent)

Example 45 with StructureMapGroupRuleTargetComponent

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

the class StructureMapUtilities method parseTarget.

private void parseTarget(StructureMapGroupRuleComponent rule, FHIRLexer lexer) throws FHIRException {
    StructureMapGroupRuleTargetComponent target = rule.addTarget();
    String start = lexer.take();
    if (lexer.hasToken(".")) {
        target.setContext(start);
        target.setContextType(StructureMapContextType.VARIABLE);
        start = null;
        lexer.token(".");
        target.setElement(lexer.take());
    }
    String name;
    boolean isConstant = false;
    if (lexer.hasToken("=")) {
        if (start != null)
            target.setContext(start);
        lexer.token("=");
        isConstant = lexer.isConstant();
        name = lexer.take();
    } else
        name = start;
    if ("(".equals(name)) {
        // inline fluentpath expression
        target.setTransform(StructureMapTransform.EVALUATE);
        ExpressionNode node = fpe.parse(lexer);
        target.setUserData(MAP_EXPRESSION, node);
        target.addParameter().setValue(new StringType(node.toString()));
        lexer.token(")");
    } else if (lexer.hasToken("(")) {
        target.setTransform(StructureMapTransform.fromCode(name));
        lexer.token("(");
        if (target.getTransform() == StructureMapTransform.EVALUATE) {
            parseParameter(target, lexer);
            lexer.token(",");
            ExpressionNode node = fpe.parse(lexer);
            target.setUserData(MAP_EXPRESSION, node);
            target.addParameter().setValue(new StringType(node.toString()));
        } else {
            while (!lexer.hasToken(")")) {
                parseParameter(target, lexer);
                if (!lexer.hasToken(")"))
                    lexer.token(",");
            }
        }
        lexer.token(")");
    } else if (name != null) {
        target.setTransform(StructureMapTransform.COPY);
        if (!isConstant) {
            String id = name;
            while (lexer.hasToken(".")) {
                id = id + lexer.take() + lexer.take();
            }
            target.addParameter().setValue(new IdType(id));
        } else
            target.addParameter().setValue(readConstant(name, lexer));
    }
    if (lexer.hasToken("as")) {
        lexer.take();
        target.setVariable(lexer.take());
    }
    while (Utilities.existsInList(lexer.getCurrent(), "first", "last", "share", "collate")) {
        if (lexer.getCurrent().equals("share")) {
            target.addListMode(StructureMapTargetListMode.SHARE);
            lexer.next();
            target.setListRuleId(lexer.take());
        } else {
            if (lexer.getCurrent().equals("first"))
                target.addListMode(StructureMapTargetListMode.FIRST);
            else
                target.addListMode(StructureMapTargetListMode.LAST);
            lexer.next();
        }
    }
}
Also used : StringType(org.hl7.fhir.r4.model.StringType) ExpressionNode(org.hl7.fhir.r4.model.ExpressionNode) StructureMapGroupRuleTargetComponent(org.hl7.fhir.r4.model.StructureMap.StructureMapGroupRuleTargetComponent) IdType(org.hl7.fhir.r4.model.IdType)

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