Skip to content

Commit

Permalink
Add aggregate, exclude
Browse files Browse the repository at this point in the history
  • Loading branch information
projkov committed Dec 15, 2023
1 parent be093db commit eb07742
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 3 deletions.
5 changes: 5 additions & 0 deletions fhirpathpy/engine/evaluators/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ def this_invocation(ctx, parentData, node):
return util.arraify(ctx["$this"])


def total_invocation(ctx, parentData, node):
return util.arraify(ctx["$total"])


def op_expression(ctx, parentData, node):
op = node["terminalNodeText"][0]
return engine.infix_invoke(ctx, op, parentData, node["children"])
Expand Down Expand Up @@ -313,6 +317,7 @@ def polarity_expression(ctx, parentData, node):
"MemberInvocation": member_invocation,
"FunctionInvocation": function_invocation,
"IndexInvocation": this_invocation,
"TotalInvocation": total_invocation,
# expressions
"PolarityExpression": polarity_expression,
"IndexerExpression": indexer_expression,
Expand Down
7 changes: 7 additions & 0 deletions fhirpathpy/engine/invocations/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@
import fhirpathpy.engine.invocations.datetime as datetime
import fhirpathpy.engine.invocations.types as types
import fhirpathpy.engine.invocations.aggregate as aggregate
from fhirpathpy.engine.nodes import FP_DateTime, FP_Quantity, FP_Time

invocations = {
"empty": {"fn": existence.empty_fn},
"not": {"fn": existence.not_fn},
"exists": {"fn": existence.exists_macro, "arity": {0: [], 1: ["Expr"]}},
"all": {"fn": existence.all_macro, "arity": {1: ["Expr"]}},
"union": {"fn": combining.union_op, "arity": {1: ["AnyAtRoot"]}},
"exclude": {"fn": combining.exclude_fn, "arity": {1: ["AnyAtRoot"]}},
"allTrue": {"fn": existence.all_true_fn},
"anyTrue": {"fn": existence.any_true_fn},
"allFalse": {"fn": existence.all_false_fn},
Expand Down Expand Up @@ -122,8 +124,13 @@
"sum": {"fn": aggregate.sum_fn},
"min": {"fn": aggregate.min_fn},
"max": {"fn": aggregate.max_fn},
"aggregate": {"fn": aggregate.aggregate_macro, "arity": {1: ["Expr"], 2: ["Expr", "Any"]}},
"convertsToBoolean": {"fn": misc.create_converts_to_fn(misc.to_boolean, 'bool')},
"convertsToInteger": {"fn": misc.create_converts_to_fn(misc.to_integer, 'int')},
"convertsToDecimal": {"fn": misc.create_converts_to_fn(misc.to_decimal, Decimal)},
"convertsToString": {"fn": misc.create_converts_to_fn(misc.to_string, 'str')},
"convertsToDate": {"fn": misc.create_converts_to_fn(misc.to_date, FP_DateTime)},
"convertsToDateTime": {"fn": misc.create_converts_to_fn(misc.to_date_time, FP_DateTime)},
"convertsToTime": {"fn": misc.create_converts_to_fn(misc.to_time, FP_Time)},
"convertsToQuantity": {"fn": misc.create_converts_to_fn(misc.to_quantity, FP_Quantity)},
}
8 changes: 8 additions & 0 deletions fhirpathpy/engine/invocations/aggregate.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,11 @@ def max_fn(ctx, x):
return []

return max(x)


def aggregate_macro(ctx, data, expr, initial_value=None):
ctx["$total"] = initial_value
for i, x in enumerate(data):
ctx["$index"] = i
ctx["$total"] = expr(x)
return ctx["$total"]
4 changes: 4 additions & 0 deletions fhirpathpy/engine/invocations/combining.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,7 @@ def union_op(ctx, coll1, coll2):

def combine_fn(ctx, coll1, coll2):
return coll1 + coll2


def exclude_fn(ctx, coll1, coll2):
return [element for element in coll1 if element not in coll2]
3 changes: 3 additions & 0 deletions fhirpathpy/engine/invocations/math.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ def intdiv(ctx, x, y):


def mod(ctx, x, y):
if y == 0:
return []

return x % y


Expand Down
6 changes: 3 additions & 3 deletions fhirpathpy/engine/invocations/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ def to_date_time(ctx, coll):
if dateTimeObject:
rtn.append(dateTimeObject)

return rtn
return util.get_data(rtn[0])


def to_time(ctx, coll):
Expand All @@ -161,7 +161,7 @@ def to_time(ctx, coll):
if timeObject:
rtn.append(timeObject)

return rtn
return util.get_data(rtn[0])


def to_date(ctx, coll):
Expand All @@ -179,7 +179,7 @@ def to_date(ctx, coll):
if dateObject:
rtn.append(dateObject)

return rtn
return util.get_data(rtn[0])


def create_converts_to_fn(to_function, _type):
Expand Down

0 comments on commit eb07742

Please sign in to comment.