From f51c7115ce8f3213ac88c262e099d692c449f9bb Mon Sep 17 00:00:00 2001 From: Grahame Grieve Date: Tue, 15 Jun 2021 08:25:59 +1000 Subject: [PATCH] Add support for $index on aggregators in FHIRPath --- .../org/hl7/fhir/r5/model/ExpressionNode.java | 2 +- .../org/hl7/fhir/r5/utils/FHIRPathEngine.java | 23 ++++++++++++++++++- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/model/ExpressionNode.java b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/model/ExpressionNode.java index 253a39b65..92f1c17cb 100644 --- a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/model/ExpressionNode.java +++ b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/model/ExpressionNode.java @@ -489,7 +489,7 @@ public class ExpressionNode { if (!name.startsWith("$")) return true; else - return Utilities.existsInList(name, "$this", "$total"); + return Utilities.existsInList(name, "$this", "$total", "$index"); } public Kind getKind() { diff --git a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/utils/FHIRPathEngine.java b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/utils/FHIRPathEngine.java index 23432ce6d..8fe761030 100644 --- a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/utils/FHIRPathEngine.java +++ b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/utils/FHIRPathEngine.java @@ -873,6 +873,7 @@ public class FHIRPathEngine { private Base thisItem; private List total; private Map aliases; + private int index; public ExecutionContext(Object appInfo, Base resource, Base rootResource, Base context, Map aliases, Base thisItem) { this.appInfo = appInfo; @@ -881,6 +882,7 @@ public class FHIRPathEngine { this.rootResource = rootResource; this.aliases = aliases; this.thisItem = thisItem; + this.index = 0; } public Base getFocusResource() { return focusResource; @@ -894,6 +896,14 @@ public class FHIRPathEngine { public List getTotal() { return total; } + + public void next() { + index++; + } + public Base getIndex() { + return new IntegerType(index); + } + public void addAlias(String name, List focus) throws FHIRException { if (aliases == null) { aliases = new HashMap(); @@ -908,6 +918,10 @@ public class FHIRPathEngine { public Base getAlias(String name) { return aliases == null ? null : aliases.get(name); } + public ExecutionContext setIndex(int i) { + index = i; + return this; + } } private class ExecutionTypeContext { @@ -1334,6 +1348,8 @@ public class FHIRPathEngine { work.add(context.getThisItem()); } else if (atEntry && exp.getName().equals("$total")) { work.addAll(context.getTotal()); + } else if (atEntry && exp.getName().equals("$index")) { + work.add(context.getIndex()); } else { for (Base item : focus) { List outcome = execute(context, item, exp, atEntry); @@ -1439,6 +1455,8 @@ public class FHIRPathEngine { result.update(context.getThisItem()); } else if (atEntry && exp.getName().equals("$total")) { result.update(anything(CollectionStatus.UNORDERED)); + } else if (atEntry && exp.getName().equals("$index")) { + result.addType(TypeDetails.FP_Integer); } else if (atEntry && focus == null) { result.update(executeContextType(context, exp.getName(), exp)); } else { @@ -4346,6 +4364,7 @@ public class FHIRPathEngine { for (Base item : focus) { ExecutionContext c = changeThis(context, item); c.total = total; + c.next(); total = execute(c, pc, exp.getParameters().get(0), true); } return total; @@ -5117,10 +5136,12 @@ public class FHIRPathEngine { private List funcSelect(ExecutionContext context, List focus, ExpressionNode exp) throws FHIRException { List result = new ArrayList(); List pc = new ArrayList(); + int i = 0; for (Base item : focus) { pc.clear(); pc.add(item); - result.addAll(execute(changeThis(context, item), pc, exp.getParameters().get(0), true)); + result.addAll(execute(changeThis(context, item).setIndex(i), pc, exp.getParameters().get(0), true)); + i++; } return result; }