Search in sources :

Example 96 with ExpressionNode

use of org.hl7.fhir.r5.model.ExpressionNode in project org.hl7.fhir.core by hapifhir.

the class FHIRPathEngine method funcAlias.

private List<Base> funcAlias(ExecutionContext context, List<Base> focus, ExpressionNode exp) throws FHIRException {
    List<Base> nl = execute(context, focus, exp.getParameters().get(0), true);
    String name = nl.get(0).primitiveValue();
    List<Base> res = new ArrayList<Base>();
    Base b = context.getAlias(name);
    if (b != null) {
        res.add(b);
    }
    return res;
}
Also used : ArrayList(java.util.ArrayList) Base(org.hl7.fhir.r5.model.Base)

Example 97 with ExpressionNode

use of org.hl7.fhir.r5.model.ExpressionNode in project org.hl7.fhir.core by hapifhir.

the class FHIRPathEngine method opGreater.

private List<Base> opGreater(List<Base> left, List<Base> right, ExpressionNode expr) throws FHIRException {
    if (left.size() == 0 || right.size() == 0)
        return new ArrayList<Base>();
    if (left.size() == 1 && right.size() == 1 && left.get(0).isPrimitive() && right.get(0).isPrimitive()) {
        Base l = left.get(0);
        Base r = right.get(0);
        if (l.hasType(FHIR_TYPES_STRING) && r.hasType(FHIR_TYPES_STRING)) {
            return makeBoolean(l.primitiveValue().compareTo(r.primitiveValue()) > 0);
        } else if ((l.hasType("integer", "decimal", "unsignedInt", "positiveInt")) && (r.hasType("integer", "decimal", "unsignedInt", "positiveInt"))) {
            return makeBoolean(new Double(l.primitiveValue()) > new Double(r.primitiveValue()));
        } else if ((l.hasType("date", "dateTime", "instant")) && (r.hasType("date", "dateTime", "instant"))) {
            Integer i = compareDateTimeElements(l, r, false);
            if (i == null) {
                return makeNull();
            } else {
                return makeBoolean(i > 0);
            }
        } else if ((l.hasType("time")) && (r.hasType("time"))) {
            Integer i = compareTimeElements(l, r, false);
            if (i == null) {
                return makeNull();
            } else {
                return makeBoolean(i > 0);
            }
        } else {
            throw makeException(expr, I18nConstants.FHIRPATH_CANT_COMPARE, l.fhirType(), r.fhirType());
        }
    } else if (left.size() == 1 && right.size() == 1 && left.get(0).fhirType().equals("Quantity") && right.get(0).fhirType().equals("Quantity")) {
        List<Base> lUnit = left.get(0).listChildrenByName("unit");
        List<Base> rUnit = right.get(0).listChildrenByName("unit");
        if (Base.compareDeep(lUnit, rUnit, true)) {
            return opGreater(left.get(0).listChildrenByName("value"), right.get(0).listChildrenByName("value"), expr);
        } else {
            if (worker.getUcumService() == null) {
                return makeBoolean(false);
            } else {
                List<Base> dl = new ArrayList<Base>();
                dl.add(qtyToCanonicalDecimal((Quantity) left.get(0)));
                List<Base> dr = new ArrayList<Base>();
                dr.add(qtyToCanonicalDecimal((Quantity) right.get(0)));
                return opGreater(dl, dr, expr);
            }
        }
    }
    return new ArrayList<Base>();
}
Also used : ArrayList(java.util.ArrayList) MergedList(org.hl7.fhir.utilities.MergedList) List(java.util.List) ArrayList(java.util.ArrayList) Base(org.hl7.fhir.r5.model.Base)

Example 98 with ExpressionNode

use of org.hl7.fhir.r5.model.ExpressionNode 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 99 with ExpressionNode

use of org.hl7.fhir.r5.model.ExpressionNode in project org.hl7.fhir.core by hapifhir.

the class FHIRPathEngine method funcAll.

private List<Base> funcAll(ExecutionContext context, List<Base> focus, ExpressionNode exp) throws FHIRException {
    List<Base> result = new ArrayList<Base>();
    if (exp.getParameters().size() == 1) {
        List<Base> pc = new ArrayList<Base>();
        boolean all = true;
        for (Base item : focus) {
            pc.clear();
            pc.add(item);
            Equality eq = asBool(execute(changeThis(context, item), pc, exp.getParameters().get(0), true), exp);
            if (eq != Equality.True) {
                all = false;
                break;
            }
        }
        result.add(new BooleanType(all).noExtensions());
    } else {
        // (exp.getParameters().size() == 0) {
        boolean all = true;
        for (Base item : focus) {
            Equality eq = asBool(item, true);
            if (eq != Equality.True) {
                all = false;
                break;
            }
        }
        result.add(new BooleanType(all).noExtensions());
    }
    return result;
}
Also used : ArrayList(java.util.ArrayList) BooleanType(org.hl7.fhir.r5.model.BooleanType) Base(org.hl7.fhir.r5.model.Base)

Example 100 with ExpressionNode

use of org.hl7.fhir.r5.model.ExpressionNode in project org.hl7.fhir.core by hapifhir.

the class FHIRPathEngine method funcAllFalse.

private List<Base> funcAllFalse(ExecutionContext context, List<Base> focus, ExpressionNode exp) throws FHIRException {
    List<Base> result = new ArrayList<Base>();
    if (exp.getParameters().size() == 1) {
        boolean all = true;
        List<Base> pc = new ArrayList<Base>();
        for (Base item : focus) {
            pc.clear();
            pc.add(item);
            List<Base> res = execute(context, pc, exp.getParameters().get(0), true);
            Equality v = asBool(res, exp);
            if (v != Equality.False) {
                all = false;
                break;
            }
        }
        result.add(new BooleanType(all).noExtensions());
    } else {
        boolean all = true;
        for (Base item : focus) {
            if (!canConvertToBoolean(item)) {
                throw new FHIRException("Unable to convert '" + convertToString(item) + "' to a boolean");
            }
            Equality v = asBool(item, true);
            if (v != Equality.False) {
                all = false;
                break;
            }
        }
        result.add(new BooleanType(all).noExtensions());
    }
    return result;
}
Also used : ArrayList(java.util.ArrayList) BooleanType(org.hl7.fhir.r5.model.BooleanType) FHIRException(org.hl7.fhir.exceptions.FHIRException) Base(org.hl7.fhir.r5.model.Base)

Aggregations

ArrayList (java.util.ArrayList)190 Base (org.hl7.fhir.r4b.model.Base)68 Base (org.hl7.fhir.r5.model.Base)67 FHIRException (org.hl7.fhir.exceptions.FHIRException)52 PathEngineException (org.hl7.fhir.exceptions.PathEngineException)49 UcumException (org.fhir.ucum.UcumException)31 DefinitionException (org.hl7.fhir.exceptions.DefinitionException)29 NotImplementedException (org.apache.commons.lang3.NotImplementedException)25 Base (org.hl7.fhir.dstu2016may.model.Base)25 BigDecimal (java.math.BigDecimal)23 Base (org.hl7.fhir.dstu2.model.Base)21 ExpressionNode (org.hl7.fhir.r5.model.ExpressionNode)21 ParserBase (org.hl7.fhir.dstu2016may.metamodel.ParserBase)20 CommaSeparatedStringBuilder (org.hl7.fhir.utilities.CommaSeparatedStringBuilder)20 ExpressionNode (org.hl7.fhir.r4b.model.ExpressionNode)19 ExpressionNode (org.hl7.fhir.dstu2016may.model.ExpressionNode)18 ExpressionNode (org.hl7.fhir.dstu3.model.ExpressionNode)16 ExpressionNode (org.hl7.fhir.r4.model.ExpressionNode)16 List (java.util.List)15 BooleanType (org.hl7.fhir.r5.model.BooleanType)15