Search in sources :

Example 21 with UcumException

use of org.fhir.ucum.UcumException 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 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");
    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", "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) {
            throw new PathEngineException(e);
        }
    } 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 : BigDecimal(java.math.BigDecimal) Decimal(org.fhir.ucum.Decimal) UcumException(org.fhir.ucum.UcumException) PathEngineException(org.hl7.fhir.exceptions.PathEngineException) Pair(org.fhir.ucum.Pair)

Example 22 with UcumException

use of org.fhir.ucum.UcumException 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.r5.model.IntegerType) BigDecimal(java.math.BigDecimal) Decimal(org.fhir.ucum.Decimal) ArrayList(java.util.ArrayList) DecimalType(org.hl7.fhir.r5.model.DecimalType) UcumException(org.fhir.ucum.UcumException) PathEngineException(org.hl7.fhir.exceptions.PathEngineException) Base(org.hl7.fhir.r5.model.Base)

Example 23 with UcumException

use of org.fhir.ucum.UcumException 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 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") && 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 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) ArrayList(java.util.ArrayList) Quantity(org.hl7.fhir.r5.model.Quantity) DecimalType(org.hl7.fhir.r5.model.DecimalType) UcumException(org.fhir.ucum.UcumException) PathEngineException(org.hl7.fhir.exceptions.PathEngineException) Base(org.hl7.fhir.r5.model.Base) BigDecimal(java.math.BigDecimal) Pair(org.fhir.ucum.Pair)

Example 24 with UcumException

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

the class FHIRPathEngine method qtyToCanonicalPair.

private Pair qtyToCanonicalPair(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 c;
    } catch (UcumException e) {
        return null;
    }
}
Also used : BigDecimal(java.math.BigDecimal) Decimal(org.fhir.ucum.Decimal) UcumException(org.fhir.ucum.UcumException) Pair(org.fhir.ucum.Pair)

Example 25 with UcumException

use of org.fhir.ucum.UcumException 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)

Aggregations

UcumException (org.fhir.ucum.UcumException)32 BigDecimal (java.math.BigDecimal)26 Decimal (org.fhir.ucum.Decimal)23 PathEngineException (org.hl7.fhir.exceptions.PathEngineException)20 ArrayList (java.util.ArrayList)17 Pair (org.fhir.ucum.Pair)11 Base (org.hl7.fhir.r4b.model.Base)5 Base (org.hl7.fhir.r5.model.Base)5 DecimalType (org.hl7.fhir.r4b.model.DecimalType)4 Quantity (org.hl7.fhir.r4b.model.Quantity)4 DecimalType (org.hl7.fhir.r5.model.DecimalType)4 Quantity (org.hl7.fhir.r5.model.Quantity)4 FileNotFoundException (java.io.FileNotFoundException)3 IOException (java.io.IOException)3 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)3 NotImplementedException (org.apache.commons.lang3.NotImplementedException)3 UcumEssenceService (org.fhir.ucum.UcumEssenceService)3 Base (org.hl7.fhir.dstu2.model.Base)3 ParserBase (org.hl7.fhir.dstu2016may.metamodel.ParserBase)3 Base (org.hl7.fhir.dstu2016may.model.Base)3