Search in sources :

Example 41 with DecimalType

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

the class FHIRPathEngine method opDivideBy.

private List<Base> opDivideBy(List<Base> left, List<Base> right, ExpressionNode expr) throws PathEngineException {
    if (left.size() == 0 || right.size() == 0) {
        return new ArrayList<Base>();
    }
    if (left.size() > 1) {
        throw makeException(expr, I18nConstants.FHIRPATH_LEFT_VALUE_PLURAL, "/");
    }
    if (!left.get(0).isPrimitive() && !(left.get(0) instanceof Quantity)) {
        throw makeException(expr, I18nConstants.FHIRPATH_LEFT_VALUE_WRONG_TYPE, "/", left.get(0).fhirType());
    }
    if (right.size() > 1) {
        throw makeException(expr, I18nConstants.FHIRPATH_RIGHT_VALUE_PLURAL, "/");
    }
    if (!right.get(0).isPrimitive() && !(right.get(0) instanceof Quantity)) {
        throw makeException(expr, I18nConstants.FHIRPATH_RIGHT_VALUE_WRONG_TYPE, "/", 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", "unsignedInt", "positiveInt") && r.hasType("integer", "decimal", "unsignedInt", "positiveInt")) {
        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) {
        // just return nothing
        }
    } else if (l instanceof Quantity && r instanceof Quantity && worker.getUcumService() != null) {
        Pair pl = qtyToPair((Quantity) l);
        Pair pr = qtyToPair((Quantity) r);
        Pair p;
        try {
            p = worker.getUcumService().divideBy(pl, pr);
            result.add(pairToQty(p));
        } catch (UcumException e) {
        // just return nothing
        }
    } else {
        throw makeException(expr, I18nConstants.FHIRPATH_OP_INCOMPATIBLE, "/", 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) Quantity(org.hl7.fhir.r5.model.Quantity) DecimalType(org.hl7.fhir.r5.model.DecimalType) UcumException(org.fhir.ucum.UcumException) Base(org.hl7.fhir.r5.model.Base) Pair(org.fhir.ucum.Pair)

Example 42 with DecimalType

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

the class FHIRPathEngine method funcRound.

private List<Base> funcRound(ExecutionContext context, List<Base> focus, ExpressionNode expr) {
    if (focus.size() != 1) {
        throw makeException(expr, I18nConstants.FHIRPATH_FOCUS_PLURAL, "round", focus.size());
    }
    Base base = focus.get(0);
    List<Base> result = new ArrayList<Base>();
    if (base.hasType("integer", "decimal", "unsignedInt", "positiveInt")) {
        int i = 0;
        if (expr.getParameters().size() == 1) {
            List<Base> n1 = execute(context, focus, expr.getParameters().get(0), true);
            if (n1.size() != 1) {
                throw makeException(expr, I18nConstants.FHIRPATH_WRONG_PARAM_TYPE, "power", "0", "Multiple Values", "integer");
            }
            i = Integer.parseInt(n1.get(0).primitiveValue());
        }
        BigDecimal d = new BigDecimal(base.primitiveValue());
        result.add(new DecimalType(d.setScale(i, RoundingMode.HALF_UP)));
    } else {
        makeException(expr, I18nConstants.FHIRPATH_WRONG_PARAM_TYPE, "round", "(focus)", base.fhirType(), "integer or decimal");
    }
    return result;
}
Also used : ArrayList(java.util.ArrayList) DecimalType(org.hl7.fhir.r5.model.DecimalType) Base(org.hl7.fhir.r5.model.Base) BigDecimal(java.math.BigDecimal)

Example 43 with DecimalType

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

the class FHIRPathEngine method funcPower.

private List<Base> funcPower(ExecutionContext context, List<Base> focus, ExpressionNode expr) {
    if (focus.size() != 1) {
        throw makeException(expr, I18nConstants.FHIRPATH_FOCUS_PLURAL, "power", focus.size());
    }
    Base base = focus.get(0);
    List<Base> result = new ArrayList<Base>();
    if (base.hasType("integer", "decimal", "unsignedInt", "positiveInt")) {
        List<Base> n1 = execute(context, focus, expr.getParameters().get(0), true);
        if (n1.size() != 1) {
            throw makeException(expr, I18nConstants.FHIRPATH_WRONG_PARAM_TYPE, "power", "0", "Multiple Values", "integer or decimal");
        }
        Double e = Double.parseDouble(n1.get(0).primitiveValue());
        Double d = Double.parseDouble(base.primitiveValue());
        try {
            result.add(new DecimalType(Math.pow(d, e)));
        } catch (Exception ex) {
        // just return nothing
        }
    } else {
        makeException(expr, I18nConstants.FHIRPATH_WRONG_PARAM_TYPE, "power", "(focus)", base.fhirType(), "integer or decimal");
    }
    return result;
}
Also used : ArrayList(java.util.ArrayList) DecimalType(org.hl7.fhir.r5.model.DecimalType) Base(org.hl7.fhir.r5.model.Base) UcumException(org.fhir.ucum.UcumException) NotImplementedException(org.apache.commons.lang3.NotImplementedException) DefinitionException(org.hl7.fhir.exceptions.DefinitionException) PathEngineException(org.hl7.fhir.exceptions.PathEngineException) FHIRException(org.hl7.fhir.exceptions.FHIRException) FHIRLexerException(org.hl7.fhir.r5.utils.FHIRLexer.FHIRLexerException)

Example 44 with DecimalType

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

the class FHIRPathEngine method opPlus.

private List<Base> opPlus(List<Base> left, List<Base> right, ExpressionNode expr) throws PathEngineException {
    if (left.size() == 0 || right.size() == 0) {
        return new ArrayList<Base>();
    }
    if (left.size() > 1) {
        throw makeException(expr, I18nConstants.FHIRPATH_LEFT_VALUE_PLURAL, "+");
    }
    if (!left.get(0).isPrimitive()) {
        throw makeException(expr, I18nConstants.FHIRPATH_LEFT_VALUE_WRONG_TYPE, "+", left.get(0).fhirType());
    }
    if (right.size() > 1) {
        throw makeException(expr, I18nConstants.FHIRPATH_RIGHT_VALUE_PLURAL, "+");
    }
    if (!right.get(0).isPrimitive() && !((left.get(0).isDateTime() || "0".equals(left.get(0).primitiveValue()) || left.get(0).hasType("Quantity")) && right.get(0).hasType("Quantity"))) {
        throw makeException(expr, I18nConstants.FHIRPATH_RIGHT_VALUE_WRONG_TYPE, "+", right.get(0).fhirType());
    }
    List<Base> result = new ArrayList<Base>();
    Base l = left.get(0);
    Base r = right.get(0);
    if (l.hasType(FHIR_TYPES_STRING) && r.hasType(FHIR_TYPES_STRING)) {
        result.add(new StringType(l.primitiveValue() + r.primitiveValue()));
    } else 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()).add(new BigDecimal(r.primitiveValue()))));
    } else if (l.isDateTime() && r.hasType("Quantity")) {
        result.add(dateAdd((BaseDateTimeType) l, (Quantity) r, false, expr));
    } else {
        throw makeException(expr, I18nConstants.FHIRPATH_OP_INCOMPATIBLE, "+", left.get(0).fhirType(), right.get(0).fhirType());
    }
    return result;
}
Also used : IntegerType(org.hl7.fhir.r5.model.IntegerType) StringType(org.hl7.fhir.r5.model.StringType) ArrayList(java.util.ArrayList) DecimalType(org.hl7.fhir.r5.model.DecimalType) Base(org.hl7.fhir.r5.model.Base) BigDecimal(java.math.BigDecimal)

Example 45 with DecimalType

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

the class QuestionnaireBuilder method convertType.

@SuppressWarnings("unchecked")
private DataType convertType(Base value, QuestionnaireItemType af, ValueSet vs, String path) throws FHIRException {
    switch(af) {
        // simple cases
        case BOOLEAN:
            if (value instanceof BooleanType)
                return (DataType) value;
            break;
        case DECIMAL:
            if (value instanceof DecimalType)
                return (DataType) value;
            break;
        case INTEGER:
            if (value instanceof IntegerType)
                return (DataType) value;
            break;
        case DATE:
            if (value instanceof DateType)
                return (DataType) value;
            break;
        case DATETIME:
            if (value instanceof DateTimeType)
                return (DataType) value;
            break;
        case TIME:
            if (value instanceof TimeType)
                return (DataType) value;
            break;
        case STRING:
            if (value instanceof StringType)
                return (DataType) value;
            else if (value instanceof UriType)
                return new StringType(((UriType) value).asStringValue());
            break;
        case TEXT:
            if (value instanceof StringType)
                return (DataType) value;
            break;
        case QUANTITY:
            if (value instanceof Quantity)
                return (DataType) value;
            break;
        // ? QuestionnaireItemTypeAttachment: ...?
        case CODING:
            if (value instanceof Coding)
                return (DataType) value;
            else if (value instanceof Enumeration) {
                Coding cc = new Coding();
                cc.setCode(((Enumeration<Enum<?>>) value).asStringValue());
                cc.setSystem(getSystemForCode(vs, cc.getCode(), path));
                return cc;
            } else if (value instanceof StringType) {
                Coding cc = new Coding();
                cc.setCode(((StringType) value).asStringValue());
                cc.setSystem(getSystemForCode(vs, cc.getCode(), path));
                return cc;
            }
            break;
        case REFERENCE:
            if (value instanceof Reference)
                return (DataType) value;
            else if (value instanceof StringType) {
                Reference r = new Reference();
                r.setReference(((StringType) value).asStringValue());
            }
            break;
        default:
            break;
    }
    throw new FHIRException("Unable to convert from '" + value.getClass().toString() + "' for Answer Format " + af.toCode() + ", path = " + path);
}
Also used : Enumeration(org.hl7.fhir.r5.model.Enumeration) StringType(org.hl7.fhir.r5.model.StringType) Reference(org.hl7.fhir.r5.model.Reference) BooleanType(org.hl7.fhir.r5.model.BooleanType) Quantity(org.hl7.fhir.r5.model.Quantity) FHIRException(org.hl7.fhir.exceptions.FHIRException) TimeType(org.hl7.fhir.r5.model.TimeType) DateTimeType(org.hl7.fhir.r5.model.DateTimeType) UriType(org.hl7.fhir.r5.model.UriType) IntegerType(org.hl7.fhir.r5.model.IntegerType) DateTimeType(org.hl7.fhir.r5.model.DateTimeType) Coding(org.hl7.fhir.r5.model.Coding) DecimalType(org.hl7.fhir.r5.model.DecimalType) DateType(org.hl7.fhir.r5.model.DateType)

Aggregations

DecimalType (org.hl7.fhir.r4.model.DecimalType)48 Test (org.junit.jupiter.api.Test)47 Coding (org.hl7.fhir.r4.model.Coding)44 CodeableConcept (org.hl7.fhir.r4.model.CodeableConcept)42 Money (org.hl7.fhir.r4.model.Money)41 BigDecimal (java.math.BigDecimal)40 ArrayList (java.util.ArrayList)38 PathEngineException (org.hl7.fhir.exceptions.PathEngineException)36 BenefitComponent (org.hl7.fhir.r4.model.ExplanationOfBenefit.BenefitComponent)31 UcumException (org.fhir.ucum.UcumException)29 DecimalType (org.hl7.fhir.r4b.model.DecimalType)22 DecimalType (org.hl7.fhir.r5.model.DecimalType)20 FHIRException (org.hl7.fhir.exceptions.FHIRException)19 Decimal (org.fhir.ucum.Decimal)14 DefinitionException (org.hl7.fhir.exceptions.DefinitionException)14 NotImplementedException (org.apache.commons.lang3.NotImplementedException)12 Pair (org.fhir.ucum.Pair)12 Base (org.hl7.fhir.r4b.model.Base)12 Base (org.hl7.fhir.r5.model.Base)12 AdjudicationComponent (org.hl7.fhir.r4.model.ExplanationOfBenefit.AdjudicationComponent)8