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>();
}
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;
}
}
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;
}
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;
}
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);
}
Aggregations