Stream: fhirpath
Topic: Custom Funtions
Grahame Grieve (Sep 21 2020 at 16:26):
The Reference Implementation of FHIRPath that Grahame developed provides support for custom functions, but unfortunately, custom functions don't have access (yet) to the left has side (the focus) of the expression
@Keith Boone I don't know what this means
Grahame Grieve (Sep 21 2020 at 16:27):
more generally... I think you should propose a standard server API to FHIR-I rather than just ad-hocing a bunch of custom functions
Keith Boone (Sep 21 2020 at 17:10):
this is how the memberOf() function gets called by FHIRPathEngine
case MemberOf : return funcMemberOf(context, focus, exp);
this is how a custom function gets called by FHIRPathEngine
hostServices.executeFunction(context.appInfo, exp.getName(), params);
This is what funcMemberOf can do:
private List<Base> funcMemberOf(ExecutionContext context, List<Base> focus, ExpressionNode exp) {
List<Base> valueSet = execute(context, focus, exp.getParameters().get(0), false);
Set<String> vs = resolveAndInitialize(context, valueSet.get(0).primitiveValue());
return Collections.singletonList(new BooleanType(doMemberOf(focus, vs)));
}
NOTE: It knows the collection being iterated over, and can thus determine if any in the collection ARE a member of the value set.
If I were to define a MyMemberOf function to do the same thing as memberOf(), it would NOT be able to do the same, because it doesn't have access to the focus.
instead of writing this: code.memberOf("MyValueSetURL"), I have to write THIS: memberOf(code, "MyValueSetURL"), otherwise it doesn't and can't do its work.
What I did to fix this in my fork of FHIRPathEngine was change this:
return hostServices.executeFunction(context.appInfo, exp.getName(), params);
to this:
return hostServices instanceof IEvaluationContext2 ?
((IEvaluationContext2) hostServices).executeFunction2(context.appInfo, exp.getName(), focus, params) :
hostServices.executeFunction(context.appInfo, exp.getName(), params);
Where IEvaluationContext2 extends IEvaluationContext and has an executeFunction2 method that accepts a parameter List<Base> focus which is the list of elements to evaluate the function against from the "left hand side" of the expression.
The IEvaluationContext2 interface is needed to ensure that existing implementations implementing custom functions continue to work properly, while allowing others which know about IEvaluationContext2 to get the enhanced support.
Last updated: Apr 12 2022 at 19:14 UTC