Search in sources :

Example 21 with Rule

use of org.hl7.fhir.utilities.xml.SchematronWriter.Rule in project org.hl7.fhir.core by hapifhir.

the class StructureMapUtilities method analyseRule.

private void analyseRule(String indent, TransformContext context, StructureMap map, VariablesForProfiling vars, StructureMapGroupComponent group, StructureMapGroupRuleComponent rule, StructureMapAnalysis result) throws FHIRException {
    log(indent + "Analyse rule : " + rule.getName());
    XhtmlNode tr = result.summary.addTag("tr");
    XhtmlNode xs = tr.addTag("td");
    XhtmlNode xt = tr.addTag("td");
    VariablesForProfiling srcVars = vars.copy();
    if (rule.getSource().size() != 1)
        throw new FHIRException("Rule \"" + rule.getName() + "\": not handled yet");
    VariablesForProfiling source = analyseSource(rule.getName(), context, srcVars, rule.getSourceFirstRep(), xs);
    TargetWriter tw = new TargetWriter();
    for (StructureMapGroupRuleTargetComponent t : rule.getTarget()) {
        analyseTarget(rule.getName(), context, source, map, t, rule.getSourceFirstRep().getVariable(), tw, result.profiles, rule.getName());
    }
    tw.commit(xt);
    for (StructureMapGroupRuleComponent childrule : rule.getRule()) {
        analyseRule(indent + "  ", context, map, source, group, childrule, result);
    }
// for (StructureMapGroupRuleDependentComponent dependent : rule.getDependent()) {
// executeDependency(indent+"  ", context, map, v, group, dependent); // do we need group here?
// }
}
Also used : FHIRException(org.hl7.fhir.exceptions.FHIRException) XhtmlNode(org.hl7.fhir.utilities.xhtml.XhtmlNode)

Example 22 with Rule

use of org.hl7.fhir.utilities.xml.SchematronWriter.Rule 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(0), 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 Reference().setReference(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) NotImplementedException(org.apache.commons.lang3.NotImplementedException) DefinitionException(org.hl7.fhir.exceptions.DefinitionException) PathEngineException(org.hl7.fhir.exceptions.PathEngineException) FHIRLexerException(org.hl7.fhir.r4b.utils.FHIRLexer.FHIRLexerException) IOException(java.io.IOException) FHIRException(org.hl7.fhir.exceptions.FHIRException)

Example 23 with Rule

use of org.hl7.fhir.utilities.xml.SchematronWriter.Rule 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.addVariable("s");
            dep.addVariable("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.r4b.model.StructureMap) StructureMapGroupComponent(org.hl7.fhir.r4b.model.StructureMap.StructureMapGroupComponent) StringType(org.hl7.fhir.r4b.model.StringType) StructureMapGroupRuleDependentComponent(org.hl7.fhir.r4b.model.StructureMap.StructureMapGroupRuleDependentComponent) StructureMapGroupRuleComponent(org.hl7.fhir.r4b.model.StructureMap.StructureMapGroupRuleComponent) StructureMapGroupRuleSourceComponent(org.hl7.fhir.r4b.model.StructureMap.StructureMapGroupRuleSourceComponent) StructureMapGroupRuleTargetComponent(org.hl7.fhir.r4b.model.StructureMap.StructureMapGroupRuleTargetComponent) IdType(org.hl7.fhir.r4b.model.IdType)

Example 24 with Rule

use of org.hl7.fhir.utilities.xml.SchematronWriter.Rule 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.r4b.model.TypeDetails.ProfiledType) TypeRefComponent(org.hl7.fhir.r4b.model.ElementDefinition.TypeRefComponent) FHIRFormatError(org.hl7.fhir.exceptions.FHIRFormatError) FHIRException(org.hl7.fhir.exceptions.FHIRException) Property(org.hl7.fhir.r4b.elementmodel.Property)

Example 25 with Rule

use of org.hl7.fhir.utilities.xml.SchematronWriter.Rule in project org.hl7.fhir.core by hapifhir.

the class ProfileUtilities method generateForChildren.

private void generateForChildren(SchematronWriter sch, String xpath, ElementDefinition ed, StructureDefinition structure, StructureDefinition base) throws IOException {
    // generateForChild(txt, structure, child);
    List<ElementDefinition> children = getChildList(structure, ed);
    String sliceName = null;
    ElementDefinitionSlicingComponent slicing = null;
    for (ElementDefinition child : children) {
        String name = tail(child.getPath());
        if (child.hasSlicing()) {
            sliceName = name;
            slicing = child.getSlicing();
        } else if (!name.equals(sliceName))
            slicing = null;
        ElementDefinition based = getByPath(base, child.getPath());
        boolean doMin = (child.getMin() > 0) && (based == null || (child.getMin() != based.getMin()));
        boolean doMax = child.hasMax() && !child.getMax().equals("*") && (based == null || (!child.getMax().equals(based.getMax())));
        Slicer slicer = slicing == null ? new Slicer(true) : generateSlicer(child, slicing, structure);
        if (slicer.check) {
            if (doMin || doMax) {
                Section s = sch.section(xpath);
                Rule r = s.rule(xpath);
                if (doMin)
                    r.assrt("count(f:" + name + slicer.criteria + ") >= " + Integer.toString(child.getMin()), name + slicer.name + ": minimum cardinality of '" + name + "' is " + Integer.toString(child.getMin()));
                if (doMax)
                    r.assrt("count(f:" + name + slicer.criteria + ") <= " + child.getMax(), name + slicer.name + ": maximum cardinality of '" + name + "' is " + child.getMax());
            }
        }
    }
    for (ElementDefinitionConstraintComponent inv : ed.getConstraint()) {
        if (inv.hasXpath()) {
            Section s = sch.section(ed.getPath());
            Rule r = s.rule(xpath);
            r.assrt(inv.getXpath(), (inv.hasId() ? inv.getId() + ": " : "") + inv.getHuman() + (inv.hasUserData(IS_DERIVED) ? " (inherited)" : ""));
        }
    }
    if (!ed.hasContentReference()) {
        for (ElementDefinition child : children) {
            String name = tail(child.getPath());
            generateForChildren(sch, xpath + "/f:" + name, child, structure, base);
        }
    }
}
Also used : ElementDefinitionSlicingComponent(org.hl7.fhir.r5.model.ElementDefinition.ElementDefinitionSlicingComponent) ElementDefinition(org.hl7.fhir.r5.model.ElementDefinition) Rule(org.hl7.fhir.utilities.xml.SchematronWriter.Rule) TypeDerivationRule(org.hl7.fhir.r5.model.StructureDefinition.TypeDerivationRule) Section(org.hl7.fhir.utilities.xml.SchematronWriter.Section) ElementDefinitionConstraintComponent(org.hl7.fhir.r5.model.ElementDefinition.ElementDefinitionConstraintComponent)

Aggregations

FHIRException (org.hl7.fhir.exceptions.FHIRException)76 ArrayList (java.util.ArrayList)46 Element (org.hl7.fhir.r5.elementmodel.Element)38 IOException (java.io.IOException)28 DefinitionException (org.hl7.fhir.exceptions.DefinitionException)26 NodeStack (org.hl7.fhir.validation.instance.utils.NodeStack)23 PathEngineException (org.hl7.fhir.exceptions.PathEngineException)20 StructureDefinition (org.hl7.fhir.r5.model.StructureDefinition)20 ValidationMessage (org.hl7.fhir.utilities.validation.ValidationMessage)19 IndexedElement (org.hl7.fhir.validation.instance.utils.IndexedElement)17 NotImplementedException (org.apache.commons.lang3.NotImplementedException)16 FHIRFormatError (org.hl7.fhir.exceptions.FHIRFormatError)14 FHIRLexerException (org.hl7.fhir.r5.utils.FHIRLexer.FHIRLexerException)14 Complex (org.hl7.fhir.dstu3.utils.formats.Turtle.Complex)13 TerminologyServiceException (org.hl7.fhir.exceptions.TerminologyServiceException)13 NamedElement (org.hl7.fhir.r5.elementmodel.ParserBase.NamedElement)13 ContactPoint (org.hl7.fhir.r5.model.ContactPoint)13 ValueSet (org.hl7.fhir.r5.model.ValueSet)13 XhtmlNode (org.hl7.fhir.utilities.xhtml.XhtmlNode)13 SpecialElement (org.hl7.fhir.r5.elementmodel.Element.SpecialElement)12