use of org.hl7.fhir.dstu2016may.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;
}
use of org.hl7.fhir.dstu2016may.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>();
}
use of org.hl7.fhir.dstu2016may.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;
}
use of org.hl7.fhir.dstu2016may.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;
}
use of org.hl7.fhir.dstu2016may.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;
}
Aggregations