Search in sources :

Example 51 with Expression

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

the class RdfParser method composePlanDefinitionPlanDefinitionActionConditionComponent.

protected void composePlanDefinitionPlanDefinitionActionConditionComponent(Complex parent, String parentType, String name, PlanDefinition.PlanDefinitionActionConditionComponent element, int index) {
    if (element == null)
        return;
    Complex t;
    if (Utilities.noString(parentType))
        t = parent;
    else {
        t = parent.predicate("fhir:" + parentType + '.' + name);
    }
    composeBackboneElement(t, "condition", name, element, index);
    if (element.hasKindElement())
        composeEnum(t, "PlanDefinition", "kind", element.getKindElement(), -1);
    if (element.hasExpression())
        composeExpression(t, "PlanDefinition", "expression", element.getExpression(), -1);
}
Also used : Complex(org.hl7.fhir.r4.utils.formats.Turtle.Complex)

Example 52 with Expression

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

the class RdfParser method composePlanDefinitionPlanDefinitionActionDynamicValueComponent.

protected void composePlanDefinitionPlanDefinitionActionDynamicValueComponent(Complex parent, String parentType, String name, PlanDefinition.PlanDefinitionActionDynamicValueComponent element, int index) {
    if (element == null)
        return;
    Complex t;
    if (Utilities.noString(parentType))
        t = parent;
    else {
        t = parent.predicate("fhir:" + parentType + '.' + name);
    }
    composeBackboneElement(t, "dynamicValue", name, element, index);
    if (element.hasPathElement())
        composeString(t, "PlanDefinition", "path", element.getPathElement(), -1);
    if (element.hasExpression())
        composeExpression(t, "PlanDefinition", "expression", element.getExpression(), -1);
}
Also used : Complex(org.hl7.fhir.r4.utils.formats.Turtle.Complex)

Example 53 with Expression

use of org.hl7.fhir.r4.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 ElementDefinition evaluateDefinition(ExpressionNode expr, StructureDefinition profile, ElementDefinition element) throws DefinitionException {
    StructureDefinition sd = profile;
    ElementDefinition focus = null;
    if (expr.getKind() == Kind.Name) {
        if (element.hasSlicing()) {
            ElementDefinition slice = pickMandatorySlice(sd, element);
            if (slice == null)
                throw new DefinitionException("Error in discriminator at " + element.getId() + ": found a sliced element while resolving the fixed value for one of the slices");
            element = slice;
        }
        if (expr.getName().equals("$this")) {
            focus = element;
        } else {
            List<ElementDefinition> childDefinitions;
            childDefinitions = ProfileUtilities.getChildMap(sd, element);
            // if that's empty, get the children of the type
            if (childDefinitions.isEmpty()) {
                sd = fetchStructureByType(element);
                if (sd == null)
                    throw new DefinitionException("Problem with use of resolve() - profile '" + element.getType().get(0).getProfile() + "' on " + element.getId() + " could not be resolved");
                childDefinitions = ProfileUtilities.getChildMap(sd, sd.getSnapshot().getElementFirstRep());
            }
            for (ElementDefinition t : childDefinitions) {
                if (tailMatches(t, expr.getName())) {
                    focus = t;
                    break;
                }
            }
        }
    } else if (expr.getKind() == Kind.Function) {
        if ("resolve".equals(expr.getName())) {
            if (!element.hasType())
                throw new DefinitionException("illegal use of resolve() in discriminator - no type on element " + element.getId());
            if (element.getType().size() > 1)
                throw new DefinitionException("illegal use of resolve() in discriminator - Multiple possible types on " + element.getId());
            if (!element.getType().get(0).hasTarget())
                throw new DefinitionException("illegal use of resolve() in discriminator - type on " + element.getId() + " is not Reference (" + element.getType().get(0).getCode() + ")");
            if (element.getType().get(0).getTargetProfile().size() > 1)
                throw new DefinitionException("illegal use of resolve() in discriminator - Multiple possible target type profiles on " + element.getId());
            sd = worker.fetchResource(StructureDefinition.class, element.getType().get(0).getTargetProfile().get(0).getValue());
            if (sd == null)
                throw new DefinitionException("Problem with use of resolve() - profile '" + element.getType().get(0).getTargetProfile() + "' on " + element.getId() + " could not be resolved");
            focus = sd.getSnapshot().getElementFirstRep();
        } else if ("extension".equals(expr.getName())) {
            String targetUrl = expr.getParameters().get(0).getConstant().primitiveValue();
            List<ElementDefinition> childDefinitions = ProfileUtilities.getChildMap(sd, element);
            for (ElementDefinition t : childDefinitions) {
                if (t.getPath().endsWith(".extension") && t.hasSliceName()) {
                    StructureDefinition exsd = (t.getType() == null || t.getType().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.getUrl().equals(targetUrl)) {
                        if (ProfileUtilities.getChildMap(sd, t).isEmpty())
                            sd = exsd;
                        focus = t;
                        break;
                    }
                }
            }
        } else
            throw new DefinitionException("illegal function name " + expr.getName() + "() in discriminator");
    } else if (expr.getKind() == Kind.Group) {
        throw new DefinitionException("illegal expression syntax in discriminator (group)");
    } else if (expr.getKind() == Kind.Constant) {
        throw new DefinitionException("illegal expression syntax in discriminator (const)");
    }
    if (focus == null)
        throw new DefinitionException("Unable to resolve discriminator in definitions: " + expr.toString());
    else if (expr.getInner() == null)
        return focus;
    else {
        return evaluateDefinition(expr.getInner(), sd, focus);
    }
}
Also used : DefinitionException(org.hl7.fhir.exceptions.DefinitionException)

Example 54 with Expression

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

the class OperationOutcomeUtilities method convertToIssue.

public static OperationOutcomeIssueComponent convertToIssue(ValidationMessage message, OperationOutcome op) {
    OperationOutcomeIssueComponent issue = new OperationOutcome.OperationOutcomeIssueComponent();
    issue.setUserData("source.vm", message);
    issue.setCode(convert(message.getType()));
    if (message.getLocation() != null) {
        // message location has a fhirPath in it. We need to populate the expression
        issue.addExpression(message.getLocation());
    }
    // pass through line/col if they're present
    if (message.getLine() >= 0)
        issue.addExtension().setUrl(ToolingExtensions.EXT_ISSUE_LINE).setValue(new IntegerType(message.getLine()));
    if (message.getCol() >= 0)
        issue.addExtension().setUrl(ToolingExtensions.EXT_ISSUE_COL).setValue(new IntegerType(message.getCol()));
    issue.setSeverity(convert(message.getLevel()));
    CodeableConcept c = new CodeableConcept();
    c.setText(message.getMessage());
    issue.setDetails(c);
    if (message.getSource() != null) {
        issue.getExtension().add(ToolingExtensions.makeIssueSource(message.getSource()));
    }
    issue.setUserData("source.msg", message);
    return issue;
}
Also used : IntegerType(org.hl7.fhir.r4b.model.IntegerType) OperationOutcomeIssueComponent(org.hl7.fhir.r4b.model.OperationOutcome.OperationOutcomeIssueComponent) CodeableConcept(org.hl7.fhir.r4b.model.CodeableConcept)

Example 55 with Expression

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

the class GraphDefinitionEngine method processLinkPath.

private void processLinkPath(String focusPath, Resource focus, GraphDefinitionLinkComponent link, int depth) {
    String path = focusPath + " -> " + link.getPath();
    check(link.hasPath(), "Path is needed at " + path);
    check(!link.hasSliceName(), "SliceName is not yet supported at " + path);
    ExpressionNode node;
    if (link.getPathElement().hasUserData(TAG_NAME)) {
        node = (ExpressionNode) link.getPathElement().getUserData(TAG_NAME);
    } else {
        node = engine.parse(link.getPath());
        link.getPathElement().setUserData(TAG_NAME, node);
    }
    List<Base> matches = engine.evaluate(null, focus, focus, focus, node);
    check(!validating || matches.size() >= (link.hasMin() ? link.getMin() : 0), "Link at path " + path + " requires at least " + link.getMin() + " matches, but only found " + matches.size());
    check(!validating || matches.size() <= (link.hasMax() ? Integer.parseInt(link.getMax()) : Integer.MAX_VALUE), "Link at path " + path + " requires at most " + link.getMax() + " matches, but found " + matches.size());
    for (Base sel : matches) {
        // todo: should a URL be ok?
        check(sel.fhirType().equals("Reference"), "Selected node from an expression must be a Reference");
        ReferenceResolution res = services.lookup(appInfo, focus, (Reference) sel);
        if (res != null) {
            // todo
            check(res.getTargetContext() != focus, "how to handle contained resources is not yet resolved");
            for (GraphDefinitionLinkTargetComponent tl : link.getTarget()) {
                if (tl.getType().equals(res.getTarget().fhirType())) {
                    Resource r = (Resource) res.getTarget();
                    if (!isInBundle(r)) {
                        addToBundle(r);
                        for (GraphDefinitionLinkComponent l : graphDefinition.getLink()) {
                            processLink(focus.fhirType(), r, l, depth + 1);
                        }
                    }
                }
            }
        }
    }
}
Also used : GraphDefinitionLinkComponent(org.hl7.fhir.r4b.model.GraphDefinition.GraphDefinitionLinkComponent) ExpressionNode(org.hl7.fhir.r4b.model.ExpressionNode) ReferenceResolution(org.hl7.fhir.utilities.graphql.IGraphQLStorageServices.ReferenceResolution) Resource(org.hl7.fhir.r4b.model.Resource) IBaseResource(org.hl7.fhir.instance.model.api.IBaseResource) GraphDefinitionLinkTargetComponent(org.hl7.fhir.r4b.model.GraphDefinition.GraphDefinitionLinkTargetComponent) Base(org.hl7.fhir.r4b.model.Base)

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