Search in sources :

Example 6 with StructureMapGroupRuleSourceComponent

use of org.hl7.fhir.r4b.model.StructureMap.StructureMapGroupRuleSourceComponent in project org.hl7.fhir.core by hapifhir.

the class MappingSheetParser method getStructureMap.

public StructureMap getStructureMap() throws FHIRException {
    StructureMap map = new StructureMap();
    loadMetadata(map);
    if (metadata.containsKey("copyright"))
        map.setCopyright(metadata.get("copyright"));
    StructureMapGroupComponent grp = map.addGroup();
    for (MappingRow row : rows) {
        StructureMapGroupRuleComponent rule = grp.addRule();
        rule.setName(row.getSequence());
        StructureMapGroupRuleSourceComponent src = rule.getSourceFirstRep();
        src.setContext("src");
        src.setElement(row.getIdentifier());
        src.setMin(row.getCardinalityMin());
        src.setMax(row.getCardinalityMax());
        src.setType(row.getDataType());
        src.addExtension(ToolingExtensions.EXT_MAPPING_NAME, new StringType(row.getName()));
        if (row.getCondition() != null) {
            src.setCheck(processCondition(row.getCondition()));
        }
        StructureMapGroupRuleTargetComponent tgt = rule.getTargetFirstRep();
        tgt.setContext("tgt");
        tgt.setElement(row.getAttribute());
        tgt.addExtension(ToolingExtensions.EXT_MAPPING_TGTTYPE, new StringType(row.getType()));
        tgt.addExtension(ToolingExtensions.EXT_MAPPING_TGTCARD, new StringType(row.getMinMax()));
        if (row.getDtMapping() != null) {
            src.setVariable("s");
            tgt.setVariable("t");
            tgt.setTransform(StructureMapTransform.CREATE);
            StructureMapGroupRuleDependentComponent dep = rule.addDependent();
            dep.setName(row.getDtMapping());
            dep.addParameter().setValue(new IdType("s"));
            dep.addParameter().setValue(new IdType("t"));
        } else if (row.getVocabMapping() != null) {
            tgt.setTransform(StructureMapTransform.TRANSLATE);
            tgt.addParameter().setValue(new StringType(row.getVocabMapping()));
            tgt.addParameter().setValue(new IdType("src"));
        } else {
            tgt.setTransform(StructureMapTransform.COPY);
        }
        rule.setDocumentation(row.getComments());
        if (row.getDerived() != null) {
            tgt = rule.addTarget();
            tgt.setContext("tgt");
            tgt.setElement(row.getDerived());
            tgt.setTransform(StructureMapTransform.COPY);
            tgt.addParameter().setValue(new StringType(row.getDerivedMapping()));
        }
    }
    return map;
}
Also used : StructureMap(org.hl7.fhir.r5.model.StructureMap) StructureMapGroupComponent(org.hl7.fhir.r5.model.StructureMap.StructureMapGroupComponent) StringType(org.hl7.fhir.r5.model.StringType) StructureMapGroupRuleDependentComponent(org.hl7.fhir.r5.model.StructureMap.StructureMapGroupRuleDependentComponent) StructureMapGroupRuleComponent(org.hl7.fhir.r5.model.StructureMap.StructureMapGroupRuleComponent) StructureMapGroupRuleSourceComponent(org.hl7.fhir.r5.model.StructureMap.StructureMapGroupRuleSourceComponent) StructureMapGroupRuleTargetComponent(org.hl7.fhir.r5.model.StructureMap.StructureMapGroupRuleTargetComponent) IdType(org.hl7.fhir.r5.model.IdType)

Example 7 with StructureMapGroupRuleSourceComponent

use of org.hl7.fhir.r4b.model.StructureMap.StructureMapGroupRuleSourceComponent 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.types.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 : TypeDetails(org.hl7.fhir.r4.model.TypeDetails) ProfiledType(org.hl7.fhir.r4.model.TypeDetails.ProfiledType) TypeRefComponent(org.hl7.fhir.r4.model.ElementDefinition.TypeRefComponent) FHIRFormatError(org.hl7.fhir.exceptions.FHIRFormatError) FHIRException(org.hl7.fhir.exceptions.FHIRException) Property(org.hl7.fhir.r4.elementmodel.Property)

Example 8 with StructureMapGroupRuleSourceComponent

use of org.hl7.fhir.r4b.model.StructureMap.StructureMapGroupRuleSourceComponent in project org.hl7.fhir.core by hapifhir.

the class StructureMapUtilities method processSource.

private List<Variables> processSource(String ruleId, TransformContext context, Variables vars, StructureMapGroupRuleSourceComponent src, String pathForErrors, String indent) throws FHIRException {
    List<Base> items;
    if (src.getContext().equals("@search")) {
        ExpressionNode expr = (ExpressionNode) src.getUserData(MAP_SEARCH_EXPRESSION);
        if (expr == null) {
            expr = fpe.parse(src.getElement());
            src.setUserData(MAP_SEARCH_EXPRESSION, expr);
        }
        // string is a holder of nothing to ensure that variables are processed correctly
        String search = fpe.evaluateToString(vars, null, null, new StringType(), expr);
        items = services.performSearch(context.getAppInfo(), search);
    } else {
        items = new ArrayList<Base>();
        Base b = vars.get(VariableMode.INPUT, src.getContext());
        if (b == null)
            throw new FHIRException("Unknown input variable " + src.getContext() + " in " + pathForErrors + " rule " + ruleId + " (vars = " + vars.summary() + ")");
        if (!src.hasElement())
            items.add(b);
        else {
            getChildrenByName(b, src.getElement(), items);
            if (items.size() == 0 && src.hasDefaultValue())
                items.add(src.getDefaultValueElement());
        }
    }
    if (src.hasType()) {
        List<Base> remove = new ArrayList<Base>();
        for (Base item : items) {
            if (item != null && !isType(item, src.getType())) {
                remove.add(item);
            }
        }
        items.removeAll(remove);
    }
    if (src.hasCondition()) {
        ExpressionNode expr = (ExpressionNode) src.getUserData(MAP_WHERE_EXPRESSION);
        if (expr == null) {
            expr = fpe.parse(src.getCondition());
            // fpe.check(context.appInfo, ??, ??, expr)
            src.setUserData(MAP_WHERE_EXPRESSION, expr);
        }
        List<Base> remove = new ArrayList<Base>();
        for (Base item : items) {
            if (!fpe.evaluateToBoolean(vars, null, null, item, expr)) {
                log(indent + "  condition [" + src.getCondition() + "] for " + item.toString() + " : false");
                remove.add(item);
            } else
                log(indent + "  condition [" + src.getCondition() + "] for " + item.toString() + " : true");
        }
        items.removeAll(remove);
    }
    if (src.hasCheck()) {
        ExpressionNode expr = (ExpressionNode) src.getUserData(MAP_WHERE_CHECK);
        if (expr == null) {
            expr = fpe.parse(src.getCheck());
            // fpe.check(context.appInfo, ??, ??, expr)
            src.setUserData(MAP_WHERE_CHECK, expr);
        }
        List<Base> remove = new ArrayList<Base>();
        for (Base item : items) {
            if (!fpe.evaluateToBoolean(vars, null, null, item, expr))
                throw new FHIRException("Rule \"" + ruleId + "\": Check condition failed");
        }
    }
    if (src.hasLogMessage()) {
        ExpressionNode expr = (ExpressionNode) src.getUserData(MAP_WHERE_LOG);
        if (expr == null) {
            expr = fpe.parse(src.getLogMessage());
            // fpe.check(context.appInfo, ??, ??, expr)
            src.setUserData(MAP_WHERE_LOG, expr);
        }
        CommaSeparatedStringBuilder b = new CommaSeparatedStringBuilder();
        for (Base item : items) b.appendIfNotNull(fpe.evaluateToString(vars, null, null, item, expr));
        if (b.length() > 0)
            services.log(b.toString());
    }
    if (src.hasListMode() && !items.isEmpty()) {
        switch(src.getListMode()) {
            case FIRST:
                Base bt = items.get(0);
                items.clear();
                items.add(bt);
                break;
            case NOTFIRST:
                if (items.size() > 0)
                    items.remove(0);
                break;
            case LAST:
                bt = items.get(items.size() - 1);
                items.clear();
                items.add(bt);
                break;
            case NOTLAST:
                if (items.size() > 0)
                    items.remove(items.size() - 1);
                break;
            case ONLYONE:
                if (items.size() > 1)
                    throw new FHIRException("Rule \"" + ruleId + "\": Check condition failed: the collection has more than one item");
                break;
            case NULL:
        }
    }
    List<Variables> result = new ArrayList<Variables>();
    for (Base r : items) {
        Variables v = vars.copy();
        if (src.hasVariable())
            v.add(VariableMode.INPUT, src.getVariable(), r);
        result.add(v);
    }
    return result;
}
Also used : CommaSeparatedStringBuilder(org.hl7.fhir.utilities.CommaSeparatedStringBuilder) FHIRException(org.hl7.fhir.exceptions.FHIRException)

Example 9 with StructureMapGroupRuleSourceComponent

use of org.hl7.fhir.r4b.model.StructureMap.StructureMapGroupRuleSourceComponent in project org.hl7.fhir.core by hapifhir.

the class StructureMapUtilities method sourceToString.

public static String sourceToString(StructureMapGroupRuleSourceComponent r) {
    StringBuilder b = new StringBuilder();
    renderSource(b, r, false);
    return b.toString();
}
Also used : CommaSeparatedStringBuilder(org.hl7.fhir.utilities.CommaSeparatedStringBuilder)

Example 10 with StructureMapGroupRuleSourceComponent

use of org.hl7.fhir.r4b.model.StructureMap.StructureMapGroupRuleSourceComponent 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.r5.model.TypeDetails.ProfiledType) TypeRefComponent(org.hl7.fhir.r5.model.ElementDefinition.TypeRefComponent) FHIRFormatError(org.hl7.fhir.exceptions.FHIRFormatError) FHIRException(org.hl7.fhir.exceptions.FHIRException) Property(org.hl7.fhir.r5.elementmodel.Property)

Aggregations

FHIRException (org.hl7.fhir.exceptions.FHIRException)9 CommaSeparatedStringBuilder (org.hl7.fhir.utilities.CommaSeparatedStringBuilder)7 FHIRFormatError (org.hl7.fhir.exceptions.FHIRFormatError)4 ArrayList (java.util.ArrayList)3 StringType (org.hl7.fhir.dstu3.model.StringType)3 StringType (org.hl7.fhir.r4.model.StringType)3 ExpressionNode (org.hl7.fhir.dstu2016may.model.ExpressionNode)2 StructureMapGroupRuleSourceComponent (org.hl7.fhir.dstu2016may.model.StructureMap.StructureMapGroupRuleSourceComponent)2 ExpressionNode (org.hl7.fhir.dstu3.model.ExpressionNode)2 StructureMapGroupRuleSourceComponent (org.hl7.fhir.dstu3.model.StructureMap.StructureMapGroupRuleSourceComponent)2 ExpressionNode (org.hl7.fhir.r4.model.ExpressionNode)2 StructureMapGroupRuleSourceComponent (org.hl7.fhir.r4.model.StructureMap.StructureMapGroupRuleSourceComponent)2 IOException (java.io.IOException)1 Base (org.hl7.fhir.dstu2016may.model.Base)1 StringType (org.hl7.fhir.dstu2016may.model.StringType)1 StructureMapGroupRuleDependentComponent (org.hl7.fhir.dstu2016may.model.StructureMap.StructureMapGroupRuleDependentComponent)1 StructureMapGroupRuleTargetComponent (org.hl7.fhir.dstu2016may.model.StructureMap.StructureMapGroupRuleTargetComponent)1 FHIRLexerException (org.hl7.fhir.dstu2016may.utils.FHIRLexer.FHIRLexerException)1 Property (org.hl7.fhir.dstu3.elementmodel.Property)1 Base (org.hl7.fhir.dstu3.model.Base)1