Search in sources :

Example 6 with ExpressionNode

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

the class FHIRPathEngine method executeTypeName.

private List<Base> executeTypeName(ExecutionContext context, List<Base> focus, ExpressionNode next, boolean atEntry) {
    List<Base> result = new ArrayList<Base>();
    result.add(new StringType(next.getName()));
    return result;
}
Also used : StringType(org.hl7.fhir.dstu2.model.StringType) ArrayList(java.util.ArrayList) Base(org.hl7.fhir.dstu2.model.Base)

Example 7 with ExpressionNode

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

the class FHIRPathEngine method parse.

// --- public API -------------------------------------------------------
/**
 * Parse a path for later use using execute
 *
 * @param path
 * @return
 * @throws PathEngineException
 * @throws Exception
 */
public ExpressionNode parse(String path) throws FHIRLexerException {
    FHIRLexer lexer = new FHIRLexer(path);
    if (lexer.done())
        throw lexer.error("Path cannot be empty");
    ExpressionNode result = parseExpression(lexer, true);
    if (!lexer.done())
        throw lexer.error("Premature ExpressionNode termination at unexpected token \"" + lexer.getCurrent() + "\"");
    result.check();
    return result;
}
Also used : ExpressionNode(org.hl7.fhir.dstu2.model.ExpressionNode)

Example 8 with ExpressionNode

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

the class FHIRPathEngine method funcSelect.

private List<Base> funcSelect(ExecutionContext context, List<Base> focus, ExpressionNode exp) throws PathEngineException {
    List<Base> result = new ArrayList<Base>();
    List<Base> pc = new ArrayList<Base>();
    for (Base item : focus) {
        pc.clear();
        pc.add(item);
        result.addAll(execute(changeThis(context, item), pc, exp.getParameters().get(0), true));
    }
    return result;
}
Also used : ArrayList(java.util.ArrayList) Base(org.hl7.fhir.dstu2.model.Base)

Example 9 with ExpressionNode

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

the class FHIRPathEngine method executeType.

private TypeDetails executeType(ExecutionTypeContext context, TypeDetails focus, ExpressionNode exp, boolean atEntry) throws PathEngineException, DefinitionException {
    // System.out.println("Evaluate {'"+exp.toString()+"'} on "+focus.toString());
    TypeDetails result = new TypeDetails(null);
    switch(exp.getKind()) {
        case Name:
            if (atEntry && exp.getName().equals("$this"))
                result.update(context.getThisItem());
            else {
                for (String s : focus.getTypes()) {
                    result.update(executeType(s, exp, atEntry));
                }
                if (result.hasNoTypes())
                    throw new PathEngineException("The name " + exp.getName() + " is not valid for any of the possible types: " + focus.describe());
            }
            break;
        case Function:
            result.update(evaluateFunctionType(context, focus, exp));
            break;
        case Constant:
            result.addType(readConstantType(context, exp.getConstant()));
            break;
        case Group:
            result.update(executeType(context, focus, exp.getGroup(), atEntry));
    }
    exp.setTypes(result);
    if (exp.getInner() != null) {
        result = executeType(context, result, exp.getInner(), false);
    }
    if (exp.isProximal() && exp.getOperation() != null) {
        ExpressionNode next = exp.getOpNext();
        ExpressionNode last = exp;
        while (next != null) {
            TypeDetails work;
            if (last.getOperation() == Operation.Is || last.getOperation() == Operation.As)
                work = executeTypeName(context, focus, next, atEntry);
            else
                work = executeType(context, focus, next, atEntry);
            result = operateTypes(result, last.getOperation(), work);
            last = next;
            next = next.getOpNext();
        }
        exp.setOpTypes(result);
    }
    return result;
}
Also used : TypeDetails(org.hl7.fhir.dstu2.model.ExpressionNode.TypeDetails) ExpressionNode(org.hl7.fhir.dstu2.model.ExpressionNode) PathEngineException(org.hl7.fhir.exceptions.PathEngineException)

Example 10 with ExpressionNode

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

the class FHIRPathEngine method gatherPrecedence.

private ExpressionNode gatherPrecedence(FHIRLexer lexer, ExpressionNode start, EnumSet<Operation> ops) {
    assert (start.isProximal());
    // is there anything to do?
    boolean work = false;
    ExpressionNode focus = start.getOpNext();
    if (ops.contains(start.getOperation())) {
        while (focus != null && focus.getOperation() != null) {
            work = work || !ops.contains(focus.getOperation());
            focus = focus.getOpNext();
        }
    } else {
        while (focus != null && focus.getOperation() != null) {
            work = work || ops.contains(focus.getOperation());
            focus = focus.getOpNext();
        }
    }
    if (!work)
        return start;
    // entry point: tricky
    ExpressionNode group;
    if (ops.contains(start.getOperation())) {
        group = newGroup(lexer, start);
        group.setProximal(true);
        focus = start;
        start = group;
    } else {
        ExpressionNode node = start;
        focus = node.getOpNext();
        while (!ops.contains(focus.getOperation())) {
            node = focus;
            focus = focus.getOpNext();
        }
        group = newGroup(lexer, focus);
        node.setOpNext(group);
    }
    // focus points at the group.group
    do {
        // run until we find the end of the sequence
        while (ops.contains(focus.getOperation())) focus = focus.getOpNext();
        if (focus.getOperation() != null) {
            group.setOperation(focus.getOperation());
            group.setOpNext(focus.getOpNext());
            focus.setOperation(null);
            focus.setOpNext(null);
            // now look for another sequence, and start it
            ExpressionNode node = group;
            focus = group.getOpNext();
            if (focus != null) {
                while (focus == null && !ops.contains(focus.getOperation())) {
                    node = focus;
                    focus = focus.getOpNext();
                }
                if (focus != null) {
                    // && (focus.Operation in Ops) - must be true
                    group = newGroup(lexer, focus);
                    node.setOpNext(group);
                }
            }
        }
    } while (focus != null && focus.getOperation() != null);
    return start;
}
Also used : ExpressionNode(org.hl7.fhir.dstu2.model.ExpressionNode)

Aggregations

ArrayList (java.util.ArrayList)190 Base (org.hl7.fhir.r4b.model.Base)68 Base (org.hl7.fhir.r5.model.Base)67 FHIRException (org.hl7.fhir.exceptions.FHIRException)52 PathEngineException (org.hl7.fhir.exceptions.PathEngineException)49 UcumException (org.fhir.ucum.UcumException)31 DefinitionException (org.hl7.fhir.exceptions.DefinitionException)29 NotImplementedException (org.apache.commons.lang3.NotImplementedException)25 Base (org.hl7.fhir.dstu2016may.model.Base)25 BigDecimal (java.math.BigDecimal)23 Base (org.hl7.fhir.dstu2.model.Base)21 ExpressionNode (org.hl7.fhir.r5.model.ExpressionNode)21 ParserBase (org.hl7.fhir.dstu2016may.metamodel.ParserBase)20 CommaSeparatedStringBuilder (org.hl7.fhir.utilities.CommaSeparatedStringBuilder)20 ExpressionNode (org.hl7.fhir.r4b.model.ExpressionNode)19 ExpressionNode (org.hl7.fhir.dstu2016may.model.ExpressionNode)18 ExpressionNode (org.hl7.fhir.dstu3.model.ExpressionNode)16 ExpressionNode (org.hl7.fhir.r4.model.ExpressionNode)16 List (java.util.List)15 BooleanType (org.hl7.fhir.r5.model.BooleanType)15