Search in sources :

Example 6 with PathEngineException

use of org.hl7.fhir.exceptions.PathEngineException 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 PathEngineException {
    // System.out.println("Evaluate {'"+exp.toString()+"'} on "+focus.toString());
    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);
            // System.out.println("Result of {'"+last.toString()+" "+last.getOperation().toCode()+" "+next.toString()+"'}: "+focus.toString());
            }
            last = next;
            next = next.getOpNext();
        }
    }
    // System.out.println("Result of {'"+exp.toString()+"'}: "+work.toString());
    return work;
}
Also used : ExpressionNode(org.hl7.fhir.dstu2.model.ExpressionNode) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) Base(org.hl7.fhir.dstu2.model.Base)

Example 7 with PathEngineException

use of org.hl7.fhir.exceptions.PathEngineException 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 PathEngineException

use of org.hl7.fhir.exceptions.PathEngineException 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 PathEngineException

use of org.hl7.fhir.exceptions.PathEngineException 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 PathEngineException

use of org.hl7.fhir.exceptions.PathEngineException in project org.hl7.fhir.core by hapifhir.

the class FHIRPathEngine method opLessOrEqual.

private List<Base> opLessOrEqual(List<Base> left, List<Base> right) throws PathEngineException {
    if (left.size() == 1 && right.size() == 1 && left.get(0).isPrimitive() && right.get(0).isPrimitive()) {
        Base l = left.get(0);
        Base r = right.get(0);
        if (l.hasType("string") && r.hasType("string"))
            return makeBoolean(l.primitiveValue().compareTo(r.primitiveValue()) <= 0);
        else if ((l.hasType("integer", "decimal")) && (r.hasType("integer", "decimal")))
            return makeBoolean(new Double(l.primitiveValue()) <= new Double(r.primitiveValue()));
        else if ((l.hasType("date", "dateTime", "instant")) && (r.hasType("date", "dateTime", "instant")))
            return makeBoolean(l.primitiveValue().compareTo(r.primitiveValue()) <= 0);
        else if ((l.hasType("time")) && (r.hasType("time")))
            return makeBoolean(l.primitiveValue().compareTo(r.primitiveValue()) <= 0);
    } else if (left.size() == 1 && right.size() == 1 && left.get(0).fhirType().equals("Quantity") && right.get(0).fhirType().equals("Quantity")) {
        List<Base> lUnits = left.get(0).listChildrenByName("unit");
        String lunit = lUnits.size() == 1 ? lUnits.get(0).primitiveValue() : null;
        List<Base> rUnits = right.get(0).listChildrenByName("unit");
        String runit = rUnits.size() == 1 ? rUnits.get(0).primitiveValue() : null;
        if ((lunit == null && runit == null) || lunit.equals(runit)) {
            return opLessOrEqual(left.get(0).listChildrenByName("value"), right.get(0).listChildrenByName("value"));
        } else {
            throw new PathEngineException("Canonical Comparison isn't done yet");
        }
    }
    return new ArrayList<Base>();
}
Also used : ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) PathEngineException(org.hl7.fhir.exceptions.PathEngineException) Base(org.hl7.fhir.dstu2.model.Base)

Aggregations

ArrayList (java.util.ArrayList)56 PathEngineException (org.hl7.fhir.exceptions.PathEngineException)53 BigDecimal (java.math.BigDecimal)36 Base (org.hl7.fhir.dstu2.model.Base)22 UcumException (org.fhir.ucum.UcumException)21 Decimal (org.fhir.ucum.Decimal)18 Base (org.hl7.fhir.r4b.model.Base)11 Base (org.hl7.fhir.r5.model.Base)11 List (java.util.List)9 Pair (org.fhir.ucum.Pair)8 ParserBase (org.hl7.fhir.dstu2016may.metamodel.ParserBase)7 Base (org.hl7.fhir.dstu2016may.model.Base)7 ExpressionNode (org.hl7.fhir.dstu2.model.ExpressionNode)6 DecimalType (org.hl7.fhir.r4b.model.DecimalType)6 DecimalType (org.hl7.fhir.r5.model.DecimalType)6 DecimalType (org.hl7.fhir.dstu2.model.DecimalType)5 IntegerType (org.hl7.fhir.dstu2.model.IntegerType)5 DecimalType (org.hl7.fhir.dstu2016may.model.DecimalType)5 ExpressionNode (org.hl7.fhir.dstu2016may.model.ExpressionNode)5 TypeDetails (org.hl7.fhir.dstu2016may.model.ExpressionNode.TypeDetails)5