Search in sources :

Example 66 with Expression

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

the class QuestionnaireResponseRenderer method addExpression.

private void addExpression(Piece p, Expression exp, String label, String url) {
    XhtmlNode x = new XhtmlNode(NodeType.Element, "li").style("font-size: 11px");
    p.addHtml(x);
    x.ah(url).tx(label);
    x.tx(": ");
    x.code(exp.getExpression());
}
Also used : XhtmlNode(org.hl7.fhir.utilities.xhtml.XhtmlNode)

Example 67 with Expression

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

the class SearchParameterRenderer method render.

public boolean render(XhtmlNode x, SearchParameter spd) throws IOException, FHIRException, EOperationOutcome {
    x.h2().addText(spd.getName());
    XhtmlNode p = x.para();
    p.tx("Parameter ");
    p.code().tx(spd.getCode());
    p.tx(":");
    p.code().tx(spd.getType().toCode());
    addMarkdown(x, spd.getDescription());
    XhtmlNode tbl = x.table("grid");
    XhtmlNode tr = tbl.tr();
    tr.td().tx(Utilities.pluralize("Resource", spd.getBase().size()));
    XhtmlNode td = tr.td();
    for (CodeType t : spd.getBase()) {
        StructureDefinition sd = context.getWorker().fetchTypeDefinition(t.toString());
        if (sd != null && sd.hasUserData("path")) {
            td.sep(", ").ah(sd.getUserString("path")).tx(t.getCode());
        } else {
            td.sep(", ").tx(t.getCode());
        }
    }
    tr = tbl.tr();
    tr.td().tx("Expression");
    if (spd.hasExpression()) {
        tr.td().code().tx(spd.getExpression());
    } else {
        tr.td().tx("(none)");
    }
    if (spd.hasXpathUsage()) {
        tr = tbl.tr();
        tr.td().tx("Usage");
        tr.td().tx(spd.getXpathUsage().getDisplay());
    }
    if (spd.hasXpath()) {
        tr = tbl.tr();
        tr.td().tx("XPath");
        tr.td().code().tx(spd.getXpath());
    }
    if (spd.hasTarget()) {
        tr = tbl.tr();
        tr.td().tx(Utilities.pluralize("Target Resources", spd.getTarget().size()));
        td = tr.td();
        for (CodeType t : spd.getTarget()) {
            StructureDefinition sd = context.getWorker().fetchTypeDefinition(t.toString());
            if (sd != null && sd.hasUserData("path")) {
                td.sep(", ").ah(sd.getUserString("path")).tx(t.getCode());
            } else {
                td.sep(", ").tx(t.getCode());
            }
        }
    }
    tr = tbl.tr();
    tr.td().tx("Multiples");
    if (spd.getMultipleAnd() && spd.getMultipleOr()) {
        tr.td().tx("The parameter can repeat (and) and can have repeating values (or)");
    } else if (spd.getMultipleOr()) {
        tr.td().tx("The parameter can repeat (and) but each repeat can only have one value");
    } else if (spd.getMultipleAnd()) {
        tr.td().tx("The parameter cannot repeat (and) but the single parameter can have multiple values (or)");
    } else {
        tr.td().tx("The parameter cannot repeat or have multiple values");
    }
    if (spd.hasComparator()) {
        tr = tbl.tr();
        tr.td().tx("Comparators");
        td = tr.td().tx("Allowed: ");
        for (Enumeration<SearchComparator> t : spd.getComparator()) {
            td.sep(", ").tx(t.asStringValue());
        }
    }
    if (spd.hasModifier()) {
        tr = tbl.tr();
        tr.td().tx("Modifiers");
        td = tr.td().tx("Allowed: ");
        for (Enumeration<SearchModifierCode> t : spd.getModifier()) {
            td.sep(", ").tx(t.asStringValue());
        }
    }
    if (spd.hasChain()) {
        tr = tbl.tr();
        tr.td().tx("Chains");
        td = tr.td().tx("Allowed: ");
        for (StringType t : spd.getChain()) {
            td.sep(", ").tx(t.asStringValue());
        }
    }
    if (spd.hasComponent()) {
        x.para().b().tx("Components");
        tbl = x.table("grid");
        for (SearchParameterComponentComponent t : spd.getComponent()) {
            tr = tbl.tr();
            SearchParameter tsp = context.getWorker().fetchResource(SearchParameter.class, t.getDefinition());
            if (tsp != null && tsp.hasUserData("path")) {
                tr.td().ah(tsp.getUserString("path")).tx(tsp.present());
            } else {
                tr.td().tx(t.getDefinition());
            }
            tr.td().code().tx(t.getExpression());
        }
    }
    return false;
}
Also used : StructureDefinition(org.hl7.fhir.r4b.model.StructureDefinition) StringType(org.hl7.fhir.r4b.model.StringType) SearchModifierCode(org.hl7.fhir.r4b.model.SearchParameter.SearchModifierCode) CodeType(org.hl7.fhir.r4b.model.CodeType) SearchComparator(org.hl7.fhir.r4b.model.SearchParameter.SearchComparator) SearchParameter(org.hl7.fhir.r4b.model.SearchParameter) XhtmlNode(org.hl7.fhir.utilities.xhtml.XhtmlNode) SearchParameterComponentComponent(org.hl7.fhir.r4b.model.SearchParameter.SearchParameterComponentComponent)

Example 68 with Expression

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

the class StructureMapUtilitiesTest method testParseRuleName.

@Test
public void testParseRuleName() throws IOException, FHIRException {
    StructureMapUtilities scu = new StructureMapUtilities(context, this);
    String fileMap = TestingUtilities.loadTestResource("r5", "structure-mapping", "ActivityDefinition.map");
    StructureMap structureMap = scu.parse(fileMap, "ActivityDefinition3To4");
    // StructureMap/ActivityDefinition3to4: StructureMap.group[3].rule[2].name error id value '"expression"' is not valid
    Assertions.assertEquals("expression", structureMap.getGroup().get(2).getRule().get(1).getName());
}
Also used : StructureMap(org.hl7.fhir.r4b.model.StructureMap) StructureMapUtilities(org.hl7.fhir.r4b.utils.structuremap.StructureMapUtilities) Test(org.junit.jupiter.api.Test)

Example 69 with Expression

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

the class DataRenderer method renderExpression.

protected void renderExpression(XhtmlNode x, Expression expr) {
    // we start with what it is, and then how it's desceibed
    if (expr.hasExpression()) {
        XhtmlNode c = x;
        if (expr.hasReference()) {
            c = x.ah(expr.getReference());
        }
        if (expr.hasLanguage()) {
            c = c.span(null, expr.getLanguage());
        }
        c.code().tx(expr.getExpression());
    } else if (expr.hasReference()) {
        x.ah(expr.getReference()).tx("source");
    }
    if (expr.hasName() || expr.hasDescription()) {
        x.tx("(");
        if (expr.hasName()) {
            x.b().tx(expr.getName());
        }
        if (expr.hasDescription()) {
            x.tx("\"");
            x.tx(expr.getDescription());
            x.tx("\"");
        }
        x.tx(")");
    }
}
Also used : XhtmlNode(org.hl7.fhir.utilities.xhtml.XhtmlNode)

Example 70 with Expression

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

the class FHIRPathEngine method evaluateDefinition.

/**
 * given an element definition in a profile, what element contains the differentiating fixed
 * for the element, given the differentiating expresssion. The expression is only allowed to
 * use a subset of FHIRPath
 *
 * @param profile
 * @param element
 * @return
 * @throws PathEngineException
 * @throws DefinitionException
 */
public TypedElementDefinition evaluateDefinition(ExpressionNode expr, StructureDefinition profile, TypedElementDefinition element, StructureDefinition source, boolean dontWalkIntoReferences) throws DefinitionException {
    StructureDefinition sd = profile;
    TypedElementDefinition focus = null;
    boolean okToNotResolve = false;
    if (expr.getKind() == Kind.Name) {
        if (element.getElement().hasSlicing()) {
            ElementDefinition slice = pickMandatorySlice(sd, element.getElement());
            if (slice == null) {
                throw makeException(expr, I18nConstants.FHIRPATH_DISCRIMINATOR_NAME_ALREADY_SLICED, element.getElement().getId());
            }
            element = new TypedElementDefinition(slice);
        }
        if (expr.getName().equals("$this")) {
            focus = element;
        } else {
            List<ElementDefinition> childDefinitions;
            childDefinitions = profileUtilities.getChildMap(sd, element.getElement());
            // if that's empty, get the children of the type
            if (childDefinitions.isEmpty()) {
                sd = fetchStructureByType(element, expr);
                if (sd == null) {
                    throw makeException(expr, I18nConstants.FHIRPATH_DISCRIMINATOR_THIS_CANNOT_FIND, element.getElement().getType().get(0).getProfile(), element.getElement().getId());
                }
                childDefinitions = profileUtilities.getChildMap(sd, sd.getSnapshot().getElementFirstRep());
            }
            for (ElementDefinition t : childDefinitions) {
                if (tailMatches(t, expr.getName()) && !t.hasSlicing()) {
                    // GG: slicing is a problem here. This is for an exetnsion with a fixed value (type slicing)
                    focus = new TypedElementDefinition(t);
                    break;
                }
            }
        }
    } else if (expr.getKind() == Kind.Function) {
        if ("resolve".equals(expr.getName())) {
            if (element.getTypes().size() == 0) {
                throw makeException(expr, I18nConstants.FHIRPATH_DISCRIMINATOR_RESOLVE_NO_TYPE, element.getElement().getId());
            }
            if (element.getTypes().size() > 1) {
                throw makeException(expr, I18nConstants.FHIRPATH_DISCRIMINATOR_RESOLVE_MULTIPLE_TYPES, element.getElement().getId());
            }
            if (!element.getTypes().get(0).hasTarget()) {
                throw makeException(expr, I18nConstants.FHIRPATH_DISCRIMINATOR_RESOLVE_NOT_REFERENCE, element.getElement().getId(), element.getElement().getType().get(0).getCode() + ")");
            }
            if (element.getTypes().get(0).getTargetProfile().size() > 1) {
                throw makeException(expr, I18nConstants.FHIRPATH_RESOLVE_DISCRIMINATOR_NO_TARGET, element.getElement().getId());
            }
            sd = worker.fetchResource(StructureDefinition.class, element.getTypes().get(0).getTargetProfile().get(0).getValue());
            if (sd == null) {
                throw makeException(expr, I18nConstants.FHIRPATH_RESOLVE_DISCRIMINATOR_CANT_FIND, element.getTypes().get(0).getTargetProfile(), element.getElement().getId());
            }
            focus = new TypedElementDefinition(sd.getSnapshot().getElementFirstRep());
        } else if ("extension".equals(expr.getName())) {
            String targetUrl = expr.getParameters().get(0).getConstant().primitiveValue();
            List<ElementDefinition> childDefinitions = profileUtilities.getChildMap(sd, element.getElement());
            for (ElementDefinition t : childDefinitions) {
                if (t.getPath().endsWith(".extension") && t.hasSliceName()) {
                    System.out.println("t: " + t.getId());
                    StructureDefinition exsd = (t.getType() == null || t.getType().isEmpty() || t.getType().get(0).getProfile().isEmpty()) ? null : worker.fetchResource(StructureDefinition.class, t.getType().get(0).getProfile().get(0).getValue());
                    while (exsd != null && !exsd.getBaseDefinition().equals("http://hl7.org/fhir/StructureDefinition/Extension")) {
                        exsd = worker.fetchResource(StructureDefinition.class, exsd.getBaseDefinition());
                    }
                    if (exsd != null && exsd.getUrl().equals(targetUrl)) {
                        if (profileUtilities.getChildMap(sd, t).isEmpty()) {
                            sd = exsd;
                        }
                        focus = new TypedElementDefinition(t);
                        break;
                    }
                }
            }
            if (focus == null) {
                throw makeException(expr, I18nConstants.FHIRPATH_DISCRIMINATOR_CANT_FIND_EXTENSION, expr.toString(), targetUrl, element.getElement().getId(), sd.getUrl());
            }
        } else if ("ofType".equals(expr.getName())) {
            if (!element.getElement().hasType()) {
                throw makeException(expr, I18nConstants.FHIRPATH_DISCRIMINATOR_TYPE_NONE, element.getElement().getId());
            }
            List<String> atn = new ArrayList<>();
            for (TypeRefComponent tr : element.getTypes()) {
                if (!tr.hasCode()) {
                    throw makeException(expr, I18nConstants.FHIRPATH_DISCRIMINATOR_NO_CODE, element.getElement().getId());
                }
                atn.add(tr.getCode());
            }
            String stn = expr.getParameters().get(0).getName();
            okToNotResolve = true;
            if ((atn.contains(stn))) {
                if (element.getTypes().size() > 1) {
                    focus = new TypedElementDefinition(element.getElement(), stn);
                } else {
                    focus = element;
                }
            }
        } else {
            throw makeException(expr, I18nConstants.FHIRPATH_DISCRIMINATOR_BAD_NAME, expr.getName());
        }
    } else if (expr.getKind() == Kind.Group) {
        throw makeException(expr, I18nConstants.FHIRPATH_DISCRIMINATOR_BAD_SYNTAX_GROUP, expr.toString());
    } else if (expr.getKind() == Kind.Constant) {
        throw makeException(expr, I18nConstants.FHIRPATH_DISCRIMINATOR_BAD_SYNTAX_CONST);
    }
    if (focus == null) {
        if (okToNotResolve) {
            return null;
        } else {
            throw makeException(expr, I18nConstants.FHIRPATH_DISCRIMINATOR_CANT_FIND, expr.toString(), source.getUrl(), element.getElement().getId(), profile.getUrl());
        }
    } else {
        // gdg 26-02-2022. If we're walking towards a resolve() and we're on a reference, and  we try to walk into the reference
        // then we don't do that. .resolve() is allowed on the Reference.reference, but the target of the reference will be defined
        // on the Reference, not the reference.reference.
        ExpressionNode next = expr.getInner();
        if (dontWalkIntoReferences && focus.hasType("Reference") && next != null && next.getKind() == Kind.Name && next.getName().equals("reference")) {
            next = next.getInner();
        }
        if (next == null) {
            return focus;
        } else {
            return evaluateDefinition(next, sd, focus, profile, dontWalkIntoReferences);
        }
    }
}
Also used : StructureDefinition(org.hl7.fhir.r5.model.StructureDefinition) TypeRefComponent(org.hl7.fhir.r5.model.ElementDefinition.TypeRefComponent) ExpressionNode(org.hl7.fhir.r5.model.ExpressionNode) MergedList(org.hl7.fhir.utilities.MergedList) List(java.util.List) ArrayList(java.util.ArrayList) ElementDefinition(org.hl7.fhir.r5.model.ElementDefinition)

Aggregations

HashMap (java.util.HashMap)33 Test (org.junit.Test)30 Test (org.junit.jupiter.api.Test)30 Patient (org.hl7.fhir.r4.model.Patient)29 CqlEvaluator (com.ibm.cohort.cql.evaluation.CqlEvaluator)28 CqlVersionedIdentifier (com.ibm.cohort.cql.library.CqlVersionedIdentifier)28 ArrayList (java.util.ArrayList)27 Expression (org.hl7.elm.r1.Expression)26 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)25 CqlEvaluationResult (com.ibm.cohort.cql.evaluation.CqlEvaluationResult)24 FHIRException (org.hl7.fhir.exceptions.FHIRException)19 FhirServerConfig (com.ibm.cohort.fhir.client.config.FhirServerConfig)16 Coding (org.hl7.fhir.r4.model.Coding)15 Complex (org.hl7.fhir.r4.utils.formats.Turtle.Complex)14 Row (org.apache.spark.sql.Row)12 List (java.util.List)11 Complex (org.hl7.fhir.dstu3.utils.formats.Turtle.Complex)11 FhirPath (au.csiro.pathling.fhirpath.FhirPath)10 Nonnull (javax.annotation.Nonnull)10 RestIntegrationTest (org.opencds.cqf.ruler.test.RestIntegrationTest)9