Search in sources :

Example 21 with Decimal

use of org.fhir.ucum.Decimal in project org.hl7.fhir.core by hapifhir.

the class FHIRPathEngine method qtyToCanonicalDecimal.

private DecimalType qtyToCanonicalDecimal(Quantity q) {
    if (!"http://unitsofmeasure.org".equals(q.getSystem())) {
        return null;
    }
    try {
        Pair p = new Pair(new Decimal(q.getValue().toPlainString()), q.getCode() == null ? "1" : q.getCode());
        Pair c = worker.getUcumService().getCanonicalForm(p);
        return new DecimalType(c.getValue().asDecimal());
    } catch (UcumException e) {
        return null;
    }
}
Also used : BigDecimal(java.math.BigDecimal) Decimal(org.fhir.ucum.Decimal) DecimalType(org.hl7.fhir.r5.model.DecimalType) UcumException(org.fhir.ucum.UcumException) Pair(org.fhir.ucum.Pair)

Example 22 with Decimal

use of org.fhir.ucum.Decimal in project org.hl7.fhir.core by hapifhir.

the class FHIRPathEngine method opDiv.

private List<Base> opDiv(List<Base> left, List<Base> right) throws PathEngineException {
    if (left.size() == 0)
        throw new PathEngineException("Error performing div: left operand has no value");
    if (left.size() > 1)
        throw new PathEngineException("Error performing div: left operand has more than one value");
    if (!left.get(0).isPrimitive())
        throw new PathEngineException(String.format("Error performing div: left operand has the wrong type (%s)", left.get(0).fhirType()));
    if (right.size() == 0)
        throw new PathEngineException("Error performing div: right operand has no value");
    if (right.size() > 1)
        throw new PathEngineException("Error performing div: right operand has more than one value");
    if (!right.get(0).isPrimitive())
        throw new PathEngineException(String.format("Error performing div: 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")) {
        Decimal d1;
        try {
            d1 = new Decimal(l.primitiveValue());
            Decimal d2 = new Decimal(r.primitiveValue());
            result.add(new IntegerType(d1.divInt(d2).asDecimal()));
        } catch (UcumException e) {
            throw new PathEngineException(e);
        }
    } else
        throw new PathEngineException(String.format("Error performing div: 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) BigDecimal(java.math.BigDecimal) Decimal(org.fhir.ucum.Decimal) ArrayList(java.util.ArrayList) 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)

Example 23 with Decimal

use of org.fhir.ucum.Decimal in project org.hl7.fhir.core by hapifhir.

the class FHIRPathEngine method opMod.

private List<Base> opMod(List<Base> left, List<Base> right) throws PathEngineException {
    if (left.size() == 0)
        throw new PathEngineException("Error performing mod: left operand has no value");
    if (left.size() > 1)
        throw new PathEngineException("Error performing mod: left operand has more than one value");
    if (!left.get(0).isPrimitive())
        throw new PathEngineException(String.format("Error performing mod: left operand has the wrong type (%s)", left.get(0).fhirType()));
    if (right.size() == 0)
        throw new PathEngineException("Error performing mod: right operand has no value");
    if (right.size() > 1)
        throw new PathEngineException("Error performing mod: right operand has more than one value");
    if (!right.get(0).isPrimitive())
        throw new PathEngineException(String.format("Error performing mod: 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")) {
        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 new PathEngineException(String.format("Error performing mod: 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) 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)

Example 24 with Decimal

use of org.fhir.ucum.Decimal in project org.hl7.fhir.core by hapifhir.

the class FHIRPathEngine method qtyToCanonicalDecimal.

private DecimalType qtyToCanonicalDecimal(Quantity q) {
    if (!"http://unitsofmeasure.org".equals(q.getSystem())) {
        return null;
    }
    try {
        Pair p = new Pair(new Decimal(q.getValue().toPlainString()), q.getCode() == null ? "1" : q.getCode());
        Pair c = worker.getUcumService().getCanonicalForm(p);
        return new DecimalType(c.getValue().asDecimal());
    } catch (UcumException e) {
        return null;
    }
}
Also used : BigDecimal(java.math.BigDecimal) Decimal(org.fhir.ucum.Decimal) DecimalType(org.hl7.fhir.r4b.model.DecimalType) UcumException(org.fhir.ucum.UcumException) Pair(org.fhir.ucum.Pair)

Example 25 with Decimal

use of org.fhir.ucum.Decimal in project org.hl7.fhir.core by hapifhir.

the class FHIRPathEngine method opDiv.

private List<Base> opDiv(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, "div");
    }
    if (!left.get(0).isPrimitive() && !(left.get(0) instanceof Quantity)) {
        throw makeException(expr, I18nConstants.FHIRPATH_LEFT_VALUE_WRONG_TYPE, "div", left.get(0).fhirType());
    }
    if (right.size() > 1) {
        throw makeException(expr, I18nConstants.FHIRPATH_RIGHT_VALUE_PLURAL, "div");
    }
    if (!right.get(0).isPrimitive() && !(right.get(0) instanceof Quantity)) {
        throw makeException(expr, I18nConstants.FHIRPATH_RIGHT_VALUE_WRONG_TYPE, "div", 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 divisor = Integer.parseInt(r.primitiveValue());
        if (divisor != 0) {
            result.add(new IntegerType(Integer.parseInt(l.primitiveValue()) / divisor));
        }
    } 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 IntegerType(d1.divInt(d2).asDecimal()));
        } catch (UcumException e) {
        // just return nothing
        }
    } else {
        throw makeException(expr, I18nConstants.FHIRPATH_OP_INCOMPATIBLE, "div", 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) Quantity(org.hl7.fhir.r4b.model.Quantity) UcumException(org.fhir.ucum.UcumException) Base(org.hl7.fhir.r4b.model.Base)

Aggregations

BigDecimal (java.math.BigDecimal)25 Decimal (org.fhir.ucum.Decimal)25 UcumException (org.fhir.ucum.UcumException)23 PathEngineException (org.hl7.fhir.exceptions.PathEngineException)15 ArrayList (java.util.ArrayList)12 Pair (org.fhir.ucum.Pair)8 Base (org.hl7.fhir.dstu2.model.Base)3 ParserBase (org.hl7.fhir.dstu2016may.metamodel.ParserBase)3 Base (org.hl7.fhir.dstu2016may.model.Base)3 Base (org.hl7.fhir.r4b.model.Base)3 DecimalType (org.hl7.fhir.r4b.model.DecimalType)3 Base (org.hl7.fhir.r5.model.Base)3 DecimalType (org.hl7.fhir.r5.model.DecimalType)3 Quantity (org.hl7.fhir.r5.model.Quantity)3 DecimalType (org.hl7.fhir.dstu2.model.DecimalType)2 IntegerType (org.hl7.fhir.dstu2.model.IntegerType)2 DecimalType (org.hl7.fhir.dstu2016may.model.DecimalType)2 IntegerType (org.hl7.fhir.dstu2016may.model.IntegerType)2 IntegerType (org.hl7.fhir.r4b.model.IntegerType)2 Quantity (org.hl7.fhir.r4b.model.Quantity)2