Search in sources :

Example 21 with Function

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

the class FHIRPathEngine method execute.

private List<Base> execute(ExecutionContext context, List<Base> focus, ExpressionNode exp, boolean atEntry) throws FHIRException {
    List<Base> work = new ArrayList<Base>();
    switch(exp.getKind()) {
        case Name:
            if (atEntry && exp.getName().equals("$this"))
                work.add(context.getThisItem());
            else
                for (Base item : focus) {
                    List<Base> outcome = execute(context, item, exp, atEntry);
                    for (Base base : outcome) if (base != null)
                        work.add(base);
                }
            break;
        case Function:
            List<Base> work2 = evaluateFunction(context, focus, exp);
            work.addAll(work2);
            break;
        case Constant:
            Base b = processConstant(context, exp.getConstant());
            if (b != null)
                work.add(b);
            break;
        case Group:
            work2 = execute(context, focus, exp.getGroup(), atEntry);
            work.addAll(work2);
    }
    if (exp.getInner() != null)
        work = execute(context, work, exp.getInner(), false);
    if (exp.isProximal() && exp.getOperation() != null) {
        ExpressionNode next = exp.getOpNext();
        ExpressionNode last = exp;
        while (next != null) {
            List<Base> work2 = preOperate(work, last.getOperation());
            if (work2 != null)
                work = work2;
            else if (last.getOperation() == Operation.Is || last.getOperation() == Operation.As) {
                work2 = executeTypeName(context, focus, next, false);
                work = operate(work, last.getOperation(), work2);
            } else {
                work2 = execute(context, focus, next, true);
                work = operate(work, last.getOperation(), work2);
            }
            last = next;
            next = next.getOpNext();
        }
    }
    return work;
}
Also used : ExpressionNode(org.hl7.fhir.dstu2016may.model.ExpressionNode) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) ParserBase(org.hl7.fhir.dstu2016may.metamodel.ParserBase) Base(org.hl7.fhir.dstu2016may.model.Base)

Example 22 with Function

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

the class FHIRPathEngine method parseExpression.

private ExpressionNode parseExpression(FHIRLexer lexer, boolean proximal) throws FHIRLexerException {
    ExpressionNode result = new ExpressionNode(lexer.nextId());
    SourceLocation c = lexer.getCurrentStartLocation();
    result.setStart(lexer.getCurrentLocation());
    // special:
    if (lexer.getCurrent().equals("-")) {
        lexer.take();
        lexer.setCurrent("-" + lexer.getCurrent());
    }
    if (lexer.isConstant(false)) {
        checkConstant(lexer.getCurrent(), lexer);
        result.setConstant(lexer.take());
        result.setKind(Kind.Constant);
        result.setEnd(lexer.getCurrentLocation());
    } else if ("(".equals(lexer.getCurrent())) {
        lexer.next();
        result.setKind(Kind.Group);
        result.setGroup(parseExpression(lexer, true));
        if (!")".equals(lexer.getCurrent()))
            throw lexer.error("Found " + lexer.getCurrent() + " expecting a \")\"");
        result.setEnd(lexer.getCurrentLocation());
        lexer.next();
    } else {
        if (!lexer.isToken() && !lexer.getCurrent().startsWith("\""))
            throw lexer.error("Found " + lexer.getCurrent() + " expecting a token name");
        if (lexer.getCurrent().startsWith("\""))
            result.setName(lexer.readConstant("Path Name"));
        else
            result.setName(lexer.take());
        result.setEnd(lexer.getCurrentLocation());
        if (!result.checkName())
            throw lexer.error("Found " + result.getName() + " expecting a valid token name");
        if ("(".equals(lexer.getCurrent())) {
            Function f = Function.fromCode(result.getName());
            FunctionDetails details = null;
            if (f == null) {
                details = hostServices.resolveFunction(result.getName());
                if (details == null)
                    throw lexer.error("The name " + result.getName() + " is not a valid function name");
                f = Function.Custom;
            }
            result.setKind(Kind.Function);
            result.setFunction(f);
            lexer.next();
            while (!")".equals(lexer.getCurrent())) {
                result.getParameters().add(parseExpression(lexer, true));
                if (",".equals(lexer.getCurrent()))
                    lexer.next();
                else if (!")".equals(lexer.getCurrent()))
                    throw lexer.error("The token " + lexer.getCurrent() + " is not expected here - either a \",\" or a \")\" expected");
            }
            result.setEnd(lexer.getCurrentLocation());
            lexer.next();
            checkParameters(lexer, c, result, details);
        } else
            result.setKind(Kind.Name);
    }
    ExpressionNode focus = result;
    if ("[".equals(lexer.getCurrent())) {
        lexer.next();
        ExpressionNode item = new ExpressionNode(lexer.nextId());
        item.setKind(Kind.Function);
        item.setFunction(ExpressionNode.Function.Item);
        item.getParameters().add(parseExpression(lexer, true));
        if (!lexer.getCurrent().equals("]"))
            throw lexer.error("The token " + lexer.getCurrent() + " is not expected here - a \"]\" expected");
        lexer.next();
        result.setInner(item);
        focus = item;
    }
    if (".".equals(lexer.getCurrent())) {
        lexer.next();
        focus.setInner(parseExpression(lexer, false));
    }
    result.setProximal(proximal);
    if (proximal) {
        while (lexer.isOp()) {
            focus.setOperation(ExpressionNode.Operation.fromCode(lexer.getCurrent()));
            focus.setOpStart(lexer.getCurrentStartLocation());
            focus.setOpEnd(lexer.getCurrentLocation());
            lexer.next();
            focus.setOpNext(parseExpression(lexer, false));
            focus = focus.getOpNext();
        }
        result = organisePrecedence(lexer, result);
    }
    return result;
}
Also used : SourceLocation(org.hl7.fhir.dstu2016may.model.ExpressionNode.SourceLocation) Function(org.hl7.fhir.dstu2016may.model.ExpressionNode.Function) FunctionDetails(org.hl7.fhir.dstu2016may.utils.FHIRPathEngine.IEvaluationContext.FunctionDetails) ExpressionNode(org.hl7.fhir.dstu2016may.model.ExpressionNode)

Example 23 with Function

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

the class RdfParser method composeMedicationAdministrationMedicationAdministrationPerformerComponent.

protected void composeMedicationAdministrationMedicationAdministrationPerformerComponent(Complex parent, String parentType, String name, MedicationAdministration.MedicationAdministrationPerformerComponent element, int index) {
    if (element == null)
        return;
    Complex t;
    if (Utilities.noString(parentType))
        t = parent;
    else {
        t = parent.predicate("fhir:" + parentType + '.' + name);
    }
    composeBackboneElement(t, "performer", name, element, index);
    if (element.hasFunction())
        composeCodeableConcept(t, "MedicationAdministration", "function", element.getFunction(), -1);
    if (element.hasActor())
        composeReference(t, "MedicationAdministration", "actor", element.getActor(), -1);
}
Also used : Complex(org.hl7.fhir.r4.utils.formats.Turtle.Complex)

Example 24 with Function

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

the class RdfParser method composeProcedureProcedurePerformerComponent.

protected void composeProcedureProcedurePerformerComponent(Complex parent, String parentType, String name, Procedure.ProcedurePerformerComponent element, int index) {
    if (element == null)
        return;
    Complex t;
    if (Utilities.noString(parentType))
        t = parent;
    else {
        t = parent.predicate("fhir:" + parentType + '.' + name);
    }
    composeBackboneElement(t, "performer", name, element, index);
    if (element.hasFunction())
        composeCodeableConcept(t, "Procedure", "function", element.getFunction(), -1);
    if (element.hasActor())
        composeReference(t, "Procedure", "actor", element.getActor(), -1);
    if (element.hasOnBehalfOf())
        composeReference(t, "Procedure", "onBehalfOf", element.getOnBehalfOf(), -1);
}
Also used : Complex(org.hl7.fhir.r4.utils.formats.Turtle.Complex)

Example 25 with Function

use of org.hl7.fhir.r5.model.ExpressionNode.Function 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)

Aggregations

Test (org.junit.jupiter.api.Test)17 List (java.util.List)8 FhirPath (au.csiro.pathling.fhirpath.FhirPath)7 Reference (org.hl7.fhir.r4.model.Reference)7 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)7 ParserContext (au.csiro.pathling.fhirpath.parser.ParserContext)6 MethodOutcome (ca.uhn.fhir.rest.api.MethodOutcome)6 URI (java.net.URI)6 Row (org.apache.spark.sql.Row)6 Session (org.eclipse.jetty.websocket.api.Session)6 ClientUpgradeRequest (org.eclipse.jetty.websocket.client.ClientUpgradeRequest)6 WebSocketClient (org.eclipse.jetty.websocket.client.WebSocketClient)6 IIdType (org.hl7.fhir.instance.model.api.IIdType)6 Complex (org.hl7.fhir.r4.utils.formats.Turtle.Complex)6 FhirReference (org.openmrs.module.fhir2.model.FhirReference)6 FhirTask (org.openmrs.module.fhir2.model.FhirTask)6 DatasetBuilder (au.csiro.pathling.test.builders.DatasetBuilder)5 ElementPathBuilder (au.csiro.pathling.test.builders.ElementPathBuilder)5 ParserContextBuilder (au.csiro.pathling.test.builders.ParserContextBuilder)5 ArrayList (java.util.ArrayList)5