Search in sources :

Example 11 with StructureMapStructureComponent

use of org.hl7.fhir.dstu3.model.StructureMap.StructureMapStructureComponent in project org.hl7.fhir.core by hapifhir.

the class StructureMapUtilities method parseUses.

private void parseUses(StructureMap result, FHIRLexer lexer) throws FHIRException {
    lexer.token("uses");
    StructureMapStructureComponent st = result.addStructure();
    st.setUrl(lexer.readConstant("url"));
    if (lexer.hasToken("alias")) {
        lexer.token("alias");
        st.setAlias(lexer.take());
    }
    lexer.token("as");
    st.setMode(StructureMapModelMode.fromCode(lexer.take()));
    lexer.skipToken(";");
    if (lexer.hasComment()) {
        st.setDocumentation(lexer.take().substring(2).trim());
    }
    lexer.skipComments();
}
Also used : StructureMapStructureComponent(org.hl7.fhir.r4.model.StructureMap.StructureMapStructureComponent)

Example 12 with StructureMapStructureComponent

use of org.hl7.fhir.dstu3.model.StructureMap.StructureMapStructureComponent in project org.hl7.fhir.core by hapifhir.

the class StructureMapUtilities method getTargetType.

public StructureDefinition getTargetType(StructureMap map) throws FHIRException {
    boolean found = false;
    StructureDefinition res = null;
    for (StructureMapStructureComponent uses : map.getStructure()) {
        if (uses.getMode() == StructureMapModelMode.TARGET) {
            if (found)
                throw new FHIRException("Multiple targets found in map " + map.getUrl());
            found = true;
            res = worker.fetchResource(StructureDefinition.class, uses.getUrl());
            if (res == null)
                throw new FHIRException("Unable to find " + uses.getUrl() + " referenced from map " + map.getUrl());
        }
    }
    if (res == null)
        throw new FHIRException("No targets found in map " + map.getUrl());
    return res;
}
Also used : StructureDefinition(org.hl7.fhir.r4.model.StructureDefinition) StructureMapStructureComponent(org.hl7.fhir.r4.model.StructureMap.StructureMapStructureComponent) FHIRException(org.hl7.fhir.exceptions.FHIRException)

Example 13 with StructureMapStructureComponent

use of org.hl7.fhir.dstu3.model.StructureMap.StructureMapStructureComponent in project org.hl7.fhir.core by hapifhir.

the class StructureMapUtilities method getTargetType.

public StructureDefinition getTargetType(StructureMap map) throws FHIRException {
    boolean found = false;
    StructureDefinition res = null;
    for (StructureMapStructureComponent uses : map.getStructure()) {
        if (uses.getMode() == StructureMapModelMode.TARGET) {
            if (found)
                throw new FHIRException("Multiple targets found in map " + map.getUrl());
            found = true;
            res = worker.fetchResource(StructureDefinition.class, uses.getUrl());
            if (res == null)
                throw new FHIRException("Unable to find " + uses.getUrl() + " referenced from map " + map.getUrl());
        }
    }
    if (res == null)
        throw new FHIRException("No targets found in map " + map.getUrl());
    return res;
}
Also used : FHIRException(org.hl7.fhir.exceptions.FHIRException)

Example 14 with StructureMapStructureComponent

use of org.hl7.fhir.dstu3.model.StructureMap.StructureMapStructureComponent in project org.hl7.fhir.core by hapifhir.

the class StructureMap method setProperty.

@Override
public void setProperty(String name, Base value) throws FHIRException {
    if (name.equals("url"))
        // UriType
        this.url = castToUri(value);
    else if (name.equals("identifier"))
        this.getIdentifier().add(castToIdentifier(value));
    else if (name.equals("version"))
        // StringType
        this.version = castToString(value);
    else if (name.equals("name"))
        // StringType
        this.name = castToString(value);
    else if (name.equals("status"))
        // Enumeration<ConformanceResourceStatus>
        this.status = new ConformanceResourceStatusEnumFactory().fromType(value);
    else if (name.equals("experimental"))
        // BooleanType
        this.experimental = castToBoolean(value);
    else if (name.equals("publisher"))
        // StringType
        this.publisher = castToString(value);
    else if (name.equals("contact"))
        this.getContact().add((StructureMapContactComponent) value);
    else if (name.equals("date"))
        // DateTimeType
        this.date = castToDateTime(value);
    else if (name.equals("description"))
        // StringType
        this.description = castToString(value);
    else if (name.equals("useContext"))
        this.getUseContext().add(castToCodeableConcept(value));
    else if (name.equals("requirements"))
        // StringType
        this.requirements = castToString(value);
    else if (name.equals("copyright"))
        // StringType
        this.copyright = castToString(value);
    else if (name.equals("structure"))
        this.getStructure().add((StructureMapStructureComponent) value);
    else if (name.equals("import"))
        this.getImport().add(castToUri(value));
    else if (name.equals("group"))
        this.getGroup().add((StructureMapGroupComponent) value);
    else
        super.setProperty(name, value);
}
Also used : ConformanceResourceStatusEnumFactory(org.hl7.fhir.dstu2016may.model.Enumerations.ConformanceResourceStatusEnumFactory)

Example 15 with StructureMapStructureComponent

use of org.hl7.fhir.dstu3.model.StructureMap.StructureMapStructureComponent 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)7 StructureMapStructureComponent (org.hl7.fhir.r4.model.StructureMap.StructureMapStructureComponent)5 ArrayList (java.util.ArrayList)4 StructureMapStructureComponent (org.hl7.fhir.dstu3.model.StructureMap.StructureMapStructureComponent)4 IOException (java.io.IOException)3 DefinitionException (org.hl7.fhir.exceptions.DefinitionException)3 FHIRFormatError (org.hl7.fhir.exceptions.FHIRFormatError)3 CommaSeparatedStringBuilder (org.hl7.fhir.utilities.CommaSeparatedStringBuilder)3 NotImplementedException (org.apache.commons.lang3.NotImplementedException)2 StructureMapStructureComponent (org.hl7.fhir.dstu2016may.model.StructureMap.StructureMapStructureComponent)2 PathEngineException (org.hl7.fhir.exceptions.PathEngineException)2 ConformanceResourceStatusEnumFactory (org.hl7.fhir.dstu2016may.model.Enumerations.ConformanceResourceStatusEnumFactory)1 StructureDefinition (org.hl7.fhir.dstu3.model.StructureDefinition)1 StructureMap (org.hl7.fhir.dstu3.model.StructureMap)1 Base (org.hl7.fhir.r4.model.Base)1 BooleanType (org.hl7.fhir.r4.model.BooleanType)1 CodeableConcept (org.hl7.fhir.r4.model.CodeableConcept)1 Coding (org.hl7.fhir.r4.model.Coding)1 ContactPoint (org.hl7.fhir.r4.model.ContactPoint)1 ExpressionNode (org.hl7.fhir.r4.model.ExpressionNode)1