Search in sources :

Example 46 with DecimalType

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

the class FHIRPathEngine method opMod.

private List<Base> opMod(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, "mod");
    }
    if (!left.get(0).isPrimitive()) {
        throw makeException(expr, I18nConstants.FHIRPATH_LEFT_VALUE_WRONG_TYPE, "mod", left.get(0).fhirType());
    }
    if (right.size() > 1) {
        throw makeException(expr, I18nConstants.FHIRPATH_RIGHT_VALUE_PLURAL, "mod");
    }
    if (!right.get(0).isPrimitive()) {
        throw makeException(expr, I18nConstants.FHIRPATH_RIGHT_VALUE_WRONG_TYPE, "mod", 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")) {
        int modulus = Integer.parseInt(r.primitiveValue());
        if (modulus != 0) {
            result.add(new IntegerType(Integer.parseInt(l.primitiveValue()) % modulus));
        }
    } else if (l.hasType("decimal", "integer") && r.hasType("decimal", "integer")) {
        Decimal d1;
        try {
            d1 = new Decimal(l.primitiveValue());
            Decimal d2 = new Decimal(r.primitiveValue());
            result.add(new DecimalType(d1.modulo(d2).asDecimal()));
        } catch (UcumException e) {
            throw new PathEngineException(e);
        }
    } else {
        throw makeException(expr, I18nConstants.FHIRPATH_OP_INCOMPATIBLE, "mod", left.get(0).fhirType(), right.get(0).fhirType());
    }
    return result;
}
Also used : IntegerType(org.hl7.fhir.r4b.model.IntegerType) BigDecimal(java.math.BigDecimal) Decimal(org.fhir.ucum.Decimal) ArrayList(java.util.ArrayList) DecimalType(org.hl7.fhir.r4b.model.DecimalType) UcumException(org.fhir.ucum.UcumException) PathEngineException(org.hl7.fhir.exceptions.PathEngineException) Base(org.hl7.fhir.r4b.model.Base)

Example 47 with DecimalType

use of org.hl7.fhir.r4.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.r4b.model.Quantity) DecimalType(org.hl7.fhir.r4b.model.DecimalType) UcumException(org.fhir.ucum.UcumException) Base(org.hl7.fhir.r4b.model.Base) Pair(org.fhir.ucum.Pair)

Example 48 with DecimalType

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

the class FHIRPathEngine method qtyEqual.

private Boolean qtyEqual(Quantity left, Quantity right) {
    if (!left.hasValue() && !right.hasValue()) {
        return true;
    }
    if (!left.hasValue() || !right.hasValue()) {
        return null;
    }
    if (worker.getUcumService() != null) {
        Pair dl = qtyToCanonicalPair(left);
        Pair dr = qtyToCanonicalPair(right);
        if (dl != null && dr != null) {
            if (dl.getCode().equals(dr.getCode())) {
                return doEquals(new DecimalType(dl.getValue().asDecimal()), new DecimalType(dr.getValue().asDecimal()));
            } else {
                return false;
            }
        }
    }
    if (left.hasCode() || right.hasCode()) {
        if (!(left.hasCode() && right.hasCode()) || !left.getCode().equals(right.getCode())) {
            return null;
        }
    } else if (!left.hasUnit() || right.hasUnit()) {
        if (!(left.hasUnit() && right.hasUnit()) || !left.getUnit().equals(right.getUnit())) {
            return null;
        }
    }
    return doEquals(new DecimalType(left.getValue()), new DecimalType(right.getValue()));
}
Also used : DecimalType(org.hl7.fhir.r4b.model.DecimalType) Pair(org.fhir.ucum.Pair)

Example 49 with DecimalType

use of org.hl7.fhir.r4.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.r4b.model.DecimalType) Base(org.hl7.fhir.r4b.model.Base) BigDecimal(java.math.BigDecimal)

Example 50 with DecimalType

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

the class FHIRPathEngine method funcExp.

private List<Base> funcExp(ExecutionContext context, List<Base> focus, ExpressionNode expr) {
    if (focus.size() != 1) {
        throw makeException(expr, I18nConstants.FHIRPATH_FOCUS_PLURAL, "exp", focus.size());
    }
    Base base = focus.get(0);
    List<Base> result = new ArrayList<Base>();
    if (base.hasType("integer", "decimal", "unsignedInt", "positiveInt")) {
        Double d = Double.parseDouble(base.primitiveValue());
        try {
            result.add(new DecimalType(Math.exp(d)));
        } catch (Exception e) {
        // just return nothing
        }
    } else {
        makeException(expr, I18nConstants.FHIRPATH_WRONG_PARAM_TYPE, "exp", "(focus)", base.fhirType(), "integer or decimal");
    }
    return result;
}
Also used : ArrayList(java.util.ArrayList) DecimalType(org.hl7.fhir.r4b.model.DecimalType) Base(org.hl7.fhir.r4b.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) FHIRLexerException(org.hl7.fhir.r4b.utils.FHIRLexer.FHIRLexerException) FHIRException(org.hl7.fhir.exceptions.FHIRException)

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