Search in sources :

Example 21 with PathEngineException

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

the class FHIRPathEngine method checkParamTypes.

private void checkParamTypes(String funcName, List<TypeDetails> paramTypes, TypeDetails... typeSet) throws PathEngineException {
    int i = 0;
    for (TypeDetails pt : typeSet) {
        if (i == paramTypes.size())
            return;
        TypeDetails actual = paramTypes.get(i);
        i++;
        for (String a : actual.getTypes()) {
            if (!pt.hasType(a))
                throw new PathEngineException("The parameter type '" + a + "' is not legal for " + funcName + " parameter " + Integer.toString(i) + ". expecting " + pt.toString());
        }
    }
}
Also used : TypeDetails(org.hl7.fhir.dstu2016may.model.ExpressionNode.TypeDetails) PathEngineException(org.hl7.fhir.exceptions.PathEngineException)

Example 22 with PathEngineException

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

the class FHIRPathEngine method opTimes.

private List<Base> opTimes(List<Base> left, List<Base> right) throws PathEngineException {
    if (left.size() == 0)
        throw new PathEngineException("Error performing *: left operand has no value");
    if (left.size() > 1)
        throw new PathEngineException("Error performing *: left operand has more than one value");
    if (!left.get(0).isPrimitive())
        throw new PathEngineException(String.format("Error performing +: left operand has the wrong type (%s)", left.get(0).fhirType()));
    if (right.size() == 0)
        throw new PathEngineException("Error performing *: right operand has no value");
    if (right.size() > 1)
        throw new PathEngineException("Error performing *: right operand has more than one value");
    if (!right.get(0).isPrimitive())
        throw new PathEngineException(String.format("Error performing *: right operand has the wrong type (%s)", right.get(0).fhirType()));
    List<Base> result = new ArrayList<Base>();
    Base l = left.get(0);
    Base r = right.get(0);
    if (l.hasType("integer") && r.hasType("integer"))
        result.add(new IntegerType(Integer.parseInt(l.primitiveValue()) * Integer.parseInt(r.primitiveValue())));
    else if (l.hasType("decimal", "integer") && r.hasType("decimal", "integer"))
        result.add(new DecimalType(new BigDecimal(l.primitiveValue()).multiply(new BigDecimal(r.primitiveValue()))));
    else
        throw new PathEngineException(String.format("Error performing *: left and right operand have incompatible or illegal types (%s, %s)", left.get(0).fhirType(), right.get(0).fhirType()));
    return result;
}
Also used : IntegerType(org.hl7.fhir.dstu2016may.model.IntegerType) ArrayList(java.util.ArrayList) DecimalType(org.hl7.fhir.dstu2016may.model.DecimalType) PathEngineException(org.hl7.fhir.exceptions.PathEngineException) ParserBase(org.hl7.fhir.dstu2016may.metamodel.ParserBase) Base(org.hl7.fhir.dstu2016may.model.Base) BigDecimal(java.math.BigDecimal)

Example 23 with PathEngineException

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

the class FHIRPathEngine method funcIs.

private List<Base> funcIs(ExecutionContext context, List<Base> focus, ExpressionNode exp) throws PathEngineException {
    List<Base> result = new ArrayList<Base>();
    if (focus.size() == 0 || focus.size() > 1)
        result.add(new BooleanType(false));
    else {
        String tn = exp.getParameters().get(0).getName();
        result.add(new BooleanType(focus.get(0).hasType(tn)));
    }
    return result;
}
Also used : ArrayList(java.util.ArrayList) BooleanType(org.hl7.fhir.dstu2016may.model.BooleanType) ParserBase(org.hl7.fhir.dstu2016may.metamodel.ParserBase) Base(org.hl7.fhir.dstu2016may.model.Base)

Example 24 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 {
    TypeDetails result = new TypeDetails(null);
    switch(exp.getKind()) {
        case Name:
            if (atEntry && exp.getName().equals("$this"))
                result.update(context.getContext());
            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.dstu2016may.model.ExpressionNode.TypeDetails) ExpressionNode(org.hl7.fhir.dstu2016may.model.ExpressionNode) PathEngineException(org.hl7.fhir.exceptions.PathEngineException)

Example 25 with PathEngineException

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

the class FHIRPathEngine method opDivideBy.

private List<Base> opDivideBy(List<Base> left, List<Base> right) throws PathEngineException {
    if (left.size() == 0)
        throw new PathEngineException("Error performing /: left operand has no value");
    if (left.size() > 1)
        throw new PathEngineException("Error performing /: left operand has more than one value");
    if (!left.get(0).isPrimitive())
        throw new PathEngineException(String.format("Error performing -: left operand has the wrong type (%s)", left.get(0).fhirType()));
    if (right.size() == 0)
        throw new PathEngineException("Error performing /: right operand has no value");
    if (right.size() > 1)
        throw new PathEngineException("Error performing /: right operand has more than one value");
    if (!right.get(0).isPrimitive())
        throw new PathEngineException(String.format("Error performing /: right operand has the wrong type (%s)", right.get(0).fhirType()));
    List<Base> result = new ArrayList<Base>();
    Base l = left.get(0);
    Base r = right.get(0);
    if (l.hasType("integer", "decimal") && r.hasType("integer", "decimal")) {
        Decimal d1;
        try {
            d1 = new Decimal(l.primitiveValue());
            Decimal d2 = new Decimal(r.primitiveValue());
            result.add(new DecimalType(d1.divide(d2).asDecimal()));
        } catch (UcumException e) {
            throw new PathEngineException(e);
        }
    } else
        throw new PathEngineException(String.format("Error performing /: left and right operand have incompatible or illegal types (%s, %s)", left.get(0).fhirType(), right.get(0).fhirType()));
    return result;
}
Also used : BigDecimal(java.math.BigDecimal) Decimal(org.fhir.ucum.Decimal) ArrayList(java.util.ArrayList) DecimalType(org.hl7.fhir.dstu2016may.model.DecimalType) UcumException(org.fhir.ucum.UcumException) PathEngineException(org.hl7.fhir.exceptions.PathEngineException) ParserBase(org.hl7.fhir.dstu2016may.metamodel.ParserBase) Base(org.hl7.fhir.dstu2016may.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