Search in sources :

Example 86 with Quantity

use of org.hl7.fhir.r4b.model.Quantity in project org.hl7.fhir.core by hapifhir.

the class FHIRPathEngine method opMinus.

private List<Base> opMinus(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 new PathEngineException("Error performing -: left operand has more than one value", expr.getStart(), expr.toString());
    if (!left.get(0).isPrimitive())
        throw new PathEngineException(String.format("Error performing -: left operand has the wrong type (%s)", left.get(0).fhirType()), expr.getStart(), expr.toString());
    if (right.size() > 1)
        throw new PathEngineException("Error performing -: right operand has more than one value", expr.getStart(), expr.toString());
    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 new PathEngineException(String.format("Error performing -: right operand has the wrong type (%s)", right.get(0).fhirType()), expr.getStart(), expr.toString());
    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()).subtract(new BigDecimal(r.primitiveValue()))));
    else if (l.isDateTime() && r.hasType("Quantity"))
        result.add(dateAdd((BaseDateTimeType) l, (Quantity) r, true, expr));
    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()), expr.getStart(), expr.toString());
    return result;
}
Also used : PathEngineException(org.hl7.fhir.exceptions.PathEngineException) BigDecimal(java.math.BigDecimal)

Example 87 with Quantity

use of org.hl7.fhir.r4b.model.Quantity 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 new PathEngineException("Error performing div: left operand has more than one value", expr.getStart(), expr.toString());
    if (!left.get(0).isPrimitive() && !(left.get(0) instanceof Quantity))
        throw new PathEngineException(String.format("Error performing div: left operand has the wrong type (%s)", left.get(0).fhirType()), expr.getStart(), expr.toString());
    if (right.size() > 1)
        throw new PathEngineException("Error performing div: right operand has more than one value", expr.getStart(), expr.toString());
    if (!right.get(0).isPrimitive() && !(right.get(0) instanceof Quantity))
        throw new PathEngineException(String.format("Error performing div: right operand has the wrong type (%s)", right.get(0).fhirType()), expr.getStart(), expr.toString());
    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()), expr.getStart(), expr.toString());
    return result;
}
Also used : BigDecimal(java.math.BigDecimal) Decimal(org.fhir.ucum.Decimal) UcumException(org.fhir.ucum.UcumException) PathEngineException(org.hl7.fhir.exceptions.PathEngineException)

Example 88 with Quantity

use of org.hl7.fhir.r4b.model.Quantity 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 new PathEngineException("Error performing +: left operand has more than one value", expr.getStart(), expr.toString());
    if (!left.get(0).isPrimitive())
        throw new PathEngineException(String.format("Error performing +: left operand has the wrong type (%s)", left.get(0).fhirType()), expr.getStart(), expr.toString());
    if (right.size() > 1)
        throw new PathEngineException("Error performing +: right operand has more than one value", expr.getStart(), expr.toString());
    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 new PathEngineException(String.format("Error performing +: right operand has the wrong type (%s)", right.get(0).fhirType()), expr.getStart(), expr.toString());
    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 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()), expr.getStart(), expr.toString());
    return result;
}
Also used : PathEngineException(org.hl7.fhir.exceptions.PathEngineException) BigDecimal(java.math.BigDecimal)

Example 89 with Quantity

use of org.hl7.fhir.r4b.model.Quantity in project org.hl7.fhir.core by hapifhir.

the class FHIRPathEngine method opTimes.

private List<Base> opTimes(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 new PathEngineException("Error performing *: left operand has more than one value", expr.getStart(), expr.toString());
    if (!left.get(0).isPrimitive() && !(left.get(0) instanceof Quantity))
        throw new PathEngineException(String.format("Error performing +: left operand has the wrong type (%s)", left.get(0).fhirType()), expr.getStart(), expr.toString());
    if (right.size() > 1)
        throw new PathEngineException("Error performing *: right operand has more than one value", expr.getStart(), expr.toString());
    if (!right.get(0).isPrimitive() && !(right.get(0) instanceof Quantity))
        throw new PathEngineException(String.format("Error performing *: right operand has the wrong type (%s)", right.get(0).fhirType()), expr.getStart(), expr.toString());
    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 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().multiply(pl, pr);
            result.add(pairToQty(p));
        } catch (UcumException e) {
            throw new PathEngineException(e.getMessage(), expr.getOpStart(), expr.toString(), 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()), expr.getStart(), expr.toString());
    return result;
}
Also used : UcumException(org.fhir.ucum.UcumException) PathEngineException(org.hl7.fhir.exceptions.PathEngineException) BigDecimal(java.math.BigDecimal) Pair(org.fhir.ucum.Pair)

Example 90 with Quantity

use of org.hl7.fhir.r4b.model.Quantity in project org.hl7.fhir.core by hapifhir.

the class Element method getAsICoding.

public ICoding getAsICoding() throws FHIRException {
    if ("code".equals(fhirType())) {
        if (property.getDefinition().getBinding().getStrength() != BindingStrength.REQUIRED)
            return null;
        ICodingImpl c = new ICodingImpl(true, true, false, false);
        c.code = primitiveValue();
        ValueSetExpansionOutcome vse = property.getContext().expandVS(property.getDefinition().getBinding(), true, false);
        if (vse.getValueset() == null)
            return null;
        for (ValueSetExpansionContainsComponent cc : vse.getValueset().getExpansion().getContains()) {
            if (cc.getCode().equals(c.code)) {
                c.system = cc.getSystem();
                if (cc.hasVersion()) {
                    c.doesVersion = true;
                    c.version = cc.getVersion();
                }
                if (cc.hasDisplay()) {
                    c.doesDisplay = true;
                    c.display = cc.getDisplay();
                }
            }
        }
        if (c.system == null)
            return null;
        return c;
    } else if ("Coding".equals(fhirType())) {
        ICodingImpl c = new ICodingImpl(true, true, true, true);
        c.system = getNamedChildValue("system");
        c.code = getNamedChildValue("code");
        c.display = getNamedChildValue("display");
        c.version = getNamedChildValue("version");
        return c;
    } else if ("Quantity".equals(fhirType())) {
        ICodingImpl c = new ICodingImpl(true, true, false, false);
        c.system = getNamedChildValue("system");
        c.code = getNamedChildValue("code");
        return c;
    } else
        return null;
}
Also used : ValueSetExpansionContainsComponent(org.hl7.fhir.r4b.model.ValueSet.ValueSetExpansionContainsComponent) ValueSetExpansionOutcome(org.hl7.fhir.r4b.terminologies.ValueSetExpander.ValueSetExpansionOutcome)

Aggregations

Quantity (org.hl7.fhir.r4.model.Quantity)58 ArrayList (java.util.ArrayList)47 Test (org.junit.jupiter.api.Test)40 BigDecimal (java.math.BigDecimal)39 Quantity (org.hl7.fhir.dstu3.model.Quantity)39 Complex (org.hl7.fhir.r4.utils.formats.Turtle.Complex)37 Test (org.junit.Test)25 List (java.util.List)24 Complex (org.hl7.fhir.dstu2016may.formats.RdfGenerator.Complex)23 Complex (org.hl7.fhir.dstu3.utils.formats.Turtle.Complex)23 NotImplementedException (org.apache.commons.lang3.NotImplementedException)19 FHIRException (org.hl7.fhir.exceptions.FHIRException)18 PathEngineException (org.hl7.fhir.exceptions.PathEngineException)18 Observation (org.hl7.fhir.r4.model.Observation)18 UcumException (org.fhir.ucum.UcumException)16 BundleEntryComponent (org.hl7.fhir.r4.model.Bundle.BundleEntryComponent)16 Resource (org.hl7.fhir.r4.model.Resource)16 Quantity (org.hl7.fhir.r5.model.Quantity)16 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)15 Coding (org.hl7.fhir.r4.model.Coding)13