Search in sources :

Example 16 with PathEngineException

use of org.hl7.fhir.exceptions.PathEngineException in project org.hl7.fhir.core by hapifhir.

the class FHIRPathEngine method opGreater.

private List<Base> opGreater(List<Base> left, List<Base> right) throws PathEngineException {
    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("string") && r.hasType("string"))
            return makeBoolean(l.primitiveValue().compareTo(r.primitiveValue()) > 0);
        else if ((l.hasType("integer", "decimal")) && (r.hasType("integer", "decimal")))
            return makeBoolean(new Double(l.primitiveValue()) > new Double(r.primitiveValue()));
        else if ((l.hasType("date", "dateTime", "instant")) && (r.hasType("date", "dateTime", "instant")))
            return makeBoolean(l.primitiveValue().compareTo(r.primitiveValue()) > 0);
        else if ((l.hasType("time")) && (r.hasType("time")))
            return makeBoolean(l.primitiveValue().compareTo(r.primitiveValue()) > 0);
    } 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"));
        } else {
            throw new PathEngineException("Canonical Comparison isn't done yet");
        }
    }
    return new ArrayList<Base>();
}
Also used : ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) PathEngineException(org.hl7.fhir.exceptions.PathEngineException) Base(org.hl7.fhir.dstu2.model.Base)

Example 17 with PathEngineException

use of org.hl7.fhir.exceptions.PathEngineException 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 PathEngineException {
    if (exp.getParameters().size() == 1) {
        List<Base> result = new ArrayList<Base>();
        List<Base> pc = new ArrayList<Base>();
        boolean all = true;
        for (Base item : focus) {
            pc.clear();
            pc.add(item);
            if (!convertToBoolean(execute(changeThis(context, item), pc, exp.getParameters().get(0), false))) {
                all = false;
                break;
            }
        }
        result.add(new BooleanType(all));
        return result;
    } else {
        // (exp.getParameters().size() == 0) {
        List<Base> result = new ArrayList<Base>();
        boolean all = true;
        for (Base item : focus) {
            boolean v = false;
            if (item instanceof BooleanType) {
                v = ((BooleanType) item).booleanValue();
            } else
                v = item != null;
            if (!v) {
                all = false;
                break;
            }
        }
        result.add(new BooleanType(all));
        return result;
    }
}
Also used : ArrayList(java.util.ArrayList) BooleanType(org.hl7.fhir.dstu2.model.BooleanType) Base(org.hl7.fhir.dstu2.model.Base)

Example 18 with PathEngineException

use of org.hl7.fhir.exceptions.PathEngineException in project org.hl7.fhir.core by hapifhir.

the class FHIRPathEngine method funcExtension.

private List<Base> funcExtension(ExecutionContext context, List<Base> focus, ExpressionNode exp) throws PathEngineException {
    List<Base> result = new ArrayList<Base>();
    List<Base> nl = execute(context, focus, exp.getParameters().get(0), true);
    String url = nl.get(0).primitiveValue();
    for (Base item : focus) {
        List<Base> ext = new ArrayList<Base>();
        getChildrenByName(item, "extension", ext);
        getChildrenByName(item, "modifierExtension", ext);
        for (Base ex : ext) {
            List<Base> vl = new ArrayList<Base>();
            getChildrenByName(ex, "url", vl);
            if (convertToString(vl).equals(url))
                result.add(ex);
        }
    }
    return result;
}
Also used : ArrayList(java.util.ArrayList) Base(org.hl7.fhir.dstu2.model.Base)

Example 19 with PathEngineException

use of org.hl7.fhir.exceptions.PathEngineException in project org.hl7.fhir.core by hapifhir.

the class FHIRPathEngine method opTimes.

private List<Base> opTimes(List<Base> left, List<Base> right) throws PathEngineException {
    if (left.size() == 0)
        throw new PathEngineException("Error performing *: left operand has no value");
    if (left.size() > 1)
        throw new PathEngineException("Error performing *: left operand has more than one value");
    if (!left.get(0).isPrimitive())
        throw new PathEngineException(String.format("Error performing +: left operand has the wrong type (%s)", left.get(0).fhirType()));
    if (right.size() == 0)
        throw new PathEngineException("Error performing *: right operand has no value");
    if (right.size() > 1)
        throw new PathEngineException("Error performing *: right operand has more than one value");
    if (!right.get(0).isPrimitive())
        throw new PathEngineException(String.format("Error performing *: 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"))
        result.add(new DecimalType(new BigDecimal(l.primitiveValue()).multiply(new BigDecimal(r.primitiveValue()))));
    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()));
    return result;
}
Also used : IntegerType(org.hl7.fhir.dstu2.model.IntegerType) ArrayList(java.util.ArrayList) DecimalType(org.hl7.fhir.dstu2.model.DecimalType) PathEngineException(org.hl7.fhir.exceptions.PathEngineException) Base(org.hl7.fhir.dstu2.model.Base) BigDecimal(java.math.BigDecimal)

Example 20 with PathEngineException

use of org.hl7.fhir.exceptions.PathEngineException in project org.hl7.fhir.core by hapifhir.

the class FHIRPathEngine method evaluate.

/**
 * evaluate a path and return the matching elements
 *
 * @param base - the object against which the path is being evaluated
 * @param path - the FHIR Path statement to use
 * @return
 * @throws PathEngineException
 * @throws FHIRLexerException
 * @
 * @
 */
public List<Base> evaluate(Object appContext, Resource resource, Base base, String path) throws PathEngineException, FHIRLexerException {
    ExpressionNode exp = parse(path);
    List<Base> list = new ArrayList<Base>();
    if (base != null)
        list.add(base);
    log = new StringBuilder();
    return execute(new ExecutionContext(appContext, resource, base, base), list, exp, true);
}
Also used : ExpressionNode(org.hl7.fhir.dstu2.model.ExpressionNode) ArrayList(java.util.ArrayList) Base(org.hl7.fhir.dstu2.model.Base)

Aggregations

ArrayList (java.util.ArrayList)56 PathEngineException (org.hl7.fhir.exceptions.PathEngineException)53 BigDecimal (java.math.BigDecimal)36 Base (org.hl7.fhir.dstu2.model.Base)22 UcumException (org.fhir.ucum.UcumException)21 Decimal (org.fhir.ucum.Decimal)18 Base (org.hl7.fhir.r4b.model.Base)11 Base (org.hl7.fhir.r5.model.Base)11 List (java.util.List)9 Pair (org.fhir.ucum.Pair)8 ParserBase (org.hl7.fhir.dstu2016may.metamodel.ParserBase)7 Base (org.hl7.fhir.dstu2016may.model.Base)7 ExpressionNode (org.hl7.fhir.dstu2.model.ExpressionNode)6 DecimalType (org.hl7.fhir.r4b.model.DecimalType)6 DecimalType (org.hl7.fhir.r5.model.DecimalType)6 DecimalType (org.hl7.fhir.dstu2.model.DecimalType)5 IntegerType (org.hl7.fhir.dstu2.model.IntegerType)5 DecimalType (org.hl7.fhir.dstu2016may.model.DecimalType)5 ExpressionNode (org.hl7.fhir.dstu2016may.model.ExpressionNode)5 TypeDetails (org.hl7.fhir.dstu2016may.model.ExpressionNode.TypeDetails)5